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 namespace OHOS {
18 namespace StorageDaemon {
19 constexpr int ACTION_PRE_LEN = 7;
20 constexpr int DEVPATH_PRE_LEN = 8;
21 constexpr int SUBSYSTEM_PRE_LEN = 10;
22 constexpr int NL_PARAMS_MAX = 128;
23 constexpr const char *EMPTY_STRING = "";
24
Decode(const char * msg)25 void NetlinkData::Decode(const char *msg)
26 {
27 int32_t paramIdx = 0;
28
29 while (*msg) {
30 if (!strncmp(msg, "ACTION=", ACTION_PRE_LEN)) {
31 msg += ACTION_PRE_LEN;
32 auto iter = actionMaps.find(msg);
33 if (iter != actionMaps.end()) {
34 action_ = iter->second;
35 }
36 } else if (!strncmp(msg, "DEVPATH=", DEVPATH_PRE_LEN)) {
37 msg += DEVPATH_PRE_LEN;
38 devPath_ = std::string(msg);
39 sysPath_ = "/sys" + devPath_;
40 } else if (!strncmp(msg, "SUBSYSTEM=", SUBSYSTEM_PRE_LEN)) {
41 msg += SUBSYSTEM_PRE_LEN;
42 subSystem_ = std::string(msg);
43 } else if (paramIdx < NL_PARAMS_MAX) {
44 params_.push_back(std::string(msg));
45 ++paramIdx;
46 }
47 while (*msg++);
48 }
49 return;
50 }
51
GetSyspath()52 std::string NetlinkData::GetSyspath()
53 {
54 return sysPath_.empty() ? EMPTY_STRING : sysPath_;
55 }
56
GetDevpath()57 std::string NetlinkData::GetDevpath()
58 {
59 return devPath_.empty() ? EMPTY_STRING : devPath_;
60 }
61
GetSubsystem()62 std::string NetlinkData::GetSubsystem()
63 {
64 return subSystem_.empty() ? EMPTY_STRING : subSystem_;
65 }
66
GetAction()67 NetlinkData::Actions NetlinkData::GetAction()
68 {
69 return action_;
70 }
71
GetParam(const std::string paramName)72 const std::string NetlinkData::GetParam(const std::string paramName)
73 {
74 size_t len = paramName.size();
75
76 std::vector<std::string>::iterator iter;
77 for (iter = params_.begin(); iter != params_.end(); ++iter) {
78 const char *ptr = iter->c_str() + len;
79 if (strncmp(iter->c_str(), paramName.c_str(), len) == 0 && *ptr == '=') {
80 return ++ptr;
81 }
82 }
83
84 return EMPTY_STRING;
85 }
86 } // namespace StorageDaemon
87 } // namespace OHOS
88