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_ = strdup(msg);
42 sysPath_ = "/sys";
43 sysPath_ += devPath_;
44 } else if (!strncmp(msg, "SUBSYSTEM=", SUBSYSTEM_PRE_LEN)) {
45 msg += SUBSYSTEM_PRE_LEN;
46 subSystem_ = strdup(msg);
47 } else if (paramIdx < NL_PARAMS_MAX) {
48 params_.push_back(strdup(msg));
49 ++paramIdx;
50 }
51 while (*msg++);
52 }
53 return;
54 }
55
GetSyspath()56 std::string NetlinkData::GetSyspath()
57 {
58 return sysPath_.empty() ? "" : sysPath_;
59 }
60
GetDevpath()61 std::string NetlinkData::GetDevpath()
62 {
63 return devPath_.empty() ? "" : devPath_;
64 }
65
GetSubsystem()66 std::string NetlinkData::GetSubsystem()
67 {
68 return subSystem_.empty() ? "" : 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 "";
89 }
90 } // StorageDaemon
91 } // OHOS
92
93