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