1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "netlink/netlink_data.h"
16
17 #include "ipc/storage_daemon.h"
18 #include "storage_service_errno.h"
19 #include "storage_service_log.h"
20
21 constexpr int ACTION_PRE_LEN = 7;
22 constexpr int DEVPATH_PRE_LEN = 8;
23 constexpr int SUBSYSTEM_PRE_LEN = 10;
24 constexpr int NL_PARAMS_MAX = 128;
25
26 namespace OHOS {
27 namespace StorageDaemon {
Decode(const char * msg)28 void NetlinkData::Decode(const char *msg)
29 {
30 int32_t paramIdx = 0;
31
32 while (*msg) {
33 if (!strncmp(msg, "ACTION=", ACTION_PRE_LEN)) {
34 msg += ACTION_PRE_LEN;
35 auto iter = actionMaps.find(msg);
36 if (iter != actionMaps.end()) {
37 action_ = iter->second;
38 }
39 } else if (!strncmp(msg, "DEVPATH=", DEVPATH_PRE_LEN)) {
40 msg += DEVPATH_PRE_LEN;
41 devPath_ = std::string(msg);
42 sysPath_ = "/sys" + devPath_;
43 } else if (!strncmp(msg, "SUBSYSTEM=", SUBSYSTEM_PRE_LEN)) {
44 msg += SUBSYSTEM_PRE_LEN;
45 subSystem_ = std::string(msg);
46 } else if (paramIdx < NL_PARAMS_MAX) {
47 params_.push_back(std::string(msg));
48 ++paramIdx;
49 }
50 while (*msg++);
51 }
52 return;
53 }
54
GetSyspath()55 std::string NetlinkData::GetSyspath()
56 {
57 return sysPath_.empty() ? "" : sysPath_;
58 }
59
GetDevpath()60 std::string NetlinkData::GetDevpath()
61 {
62 return devPath_.empty() ? "" : devPath_;
63 }
64
GetSubsystem()65 std::string NetlinkData::GetSubsystem()
66 {
67 return subSystem_.empty() ? "" : subSystem_;
68 }
69
GetAction()70 NetlinkData::Actions NetlinkData::GetAction()
71 {
72 return action_;
73 }
74
GetParam(const std::string paramName)75 const std::string NetlinkData::GetParam(const std::string paramName)
76 {
77 size_t len = paramName.size();
78
79 std::vector<std::string>::iterator iter;
80 for (iter = params_.begin(); iter != params_.end(); ++iter) {
81 const char *ptr = iter->c_str() + len;
82 if (strncmp(iter->c_str(), paramName.c_str(), len) == 0 && *ptr == '=') {
83 return ++ptr;
84 }
85 }
86
87 return "";
88 }
89 } // StorageDaemon
90 } // OHOS
91