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
16 #include "net_stats_service_stub.h"
17 #include "net_mgr_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
NetStatsServiceStub()21 NetStatsServiceStub::NetStatsServiceStub()
22 {
23 memberFuncMap_[CMD_GET_IFACE_STATS_DETAIL] = &NetStatsServiceStub::OnGetIfaceStatsDetail;
24 memberFuncMap_[CMD_GET_UID_STATS_DETAIL] = &NetStatsServiceStub::OnGetUidStatsDetail;
25 memberFuncMap_[CMD_UPDATE_IFACES_STATS] = &NetStatsServiceStub::OnUpdateIfacesStats;
26 memberFuncMap_[CMD_UPDATE_STATS_DATA] = &NetStatsServiceStub::OnUpdateStatsData;
27 memberFuncMap_[CMD_NSM_REGISTER_NET_STATS_CALLBACK] = &NetStatsServiceStub::OnRegisterNetStatsCallback;
28 memberFuncMap_[CMD_NSM_UNREGISTER_NET_STATS_CALLBACK] = &NetStatsServiceStub::OnUnregisterNetStatsCallback;
29 memberFuncMap_[CMD_NSM_RESET_FACTORY] = &NetStatsServiceStub::OnResetFactory;
30 }
31
~NetStatsServiceStub()32 NetStatsServiceStub::~NetStatsServiceStub() {}
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t NetStatsServiceStub::OnRemoteRequest(
35 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37 NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
38
39 std::u16string myDescripter = NetStatsServiceStub::GetDescriptor();
40 std::u16string remoteDescripter = data.ReadInterfaceToken();
41 if (myDescripter != remoteDescripter) {
42 NETMGR_LOG_D("descriptor checked fail");
43 return ERR_FLATTEN_OBJECT;
44 }
45
46 auto itFunc = memberFuncMap_.find(code);
47 if (itFunc != memberFuncMap_.end()) {
48 auto requestFunc = itFunc->second;
49 if (requestFunc != nullptr) {
50 return (this->*requestFunc)(data, reply);
51 }
52 }
53 NETMGR_LOG_D("stub default case, need check");
54 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55 }
56
OnRegisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)57 int32_t NetStatsServiceStub::OnRegisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
58 {
59 int32_t result = ERR_FLATTEN_OBJECT;
60 sptr<IRemoteObject> remote = data.ReadRemoteObject();
61 if (remote == nullptr) {
62 NETMGR_LOG_E("Callback ptr is nullptr.");
63 reply.WriteInt32(result);
64 return result;
65 }
66
67 sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
68 result = RegisterNetStatsCallback(callback);
69 reply.WriteInt32(result);
70 return result;
71 }
72
OnUnregisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)73 int32_t NetStatsServiceStub::OnUnregisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
74 {
75 int32_t result = ERR_FLATTEN_OBJECT;
76 sptr<IRemoteObject> remote = data.ReadRemoteObject();
77 if (remote == nullptr) {
78 NETMGR_LOG_E("callback ptr is nullptr.");
79 reply.WriteInt32(result);
80 return result;
81 }
82 sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
83 result = UnregisterNetStatsCallback(callback);
84 reply.WriteInt32(result);
85 return result;
86 }
87
OnGetIfaceStatsDetail(MessageParcel & data,MessageParcel & reply)88 int32_t NetStatsServiceStub::OnGetIfaceStatsDetail(MessageParcel &data, MessageParcel &reply)
89 {
90 std::string iface;
91 if (!data.ReadString(iface)) {
92 return ERR_FLATTEN_OBJECT;
93 }
94
95 uint32_t start;
96 if (!data.ReadUint32(start)) {
97 return ERR_FLATTEN_OBJECT;
98 }
99
100 uint32_t end;
101 if (!data.ReadUint32(end)) {
102 return ERR_FLATTEN_OBJECT;
103 }
104
105 NetStatsInfo stats;
106 int32_t result = static_cast<int32_t>(GetIfaceStatsDetail(iface, start, end, stats));
107 if (!NetStatsInfo::Marshalling(reply, stats)) {
108 NETMGR_LOG_E("proxy Marshalling failed");
109 return ERR_FLATTEN_OBJECT;
110 }
111 NETMGR_LOG_I("NetStatsServiceStub::OnGetIfaceStatsDetail, result[%{public}d]", result);
112 if (!reply.WriteInt32(result)) {
113 return ERR_FLATTEN_OBJECT;
114 }
115 return ERR_NONE;
116 }
117
OnGetUidStatsDetail(MessageParcel & data,MessageParcel & reply)118 int32_t NetStatsServiceStub::OnGetUidStatsDetail(MessageParcel &data, MessageParcel &reply)
119 {
120 std::string iface;
121 if (!data.ReadString(iface)) {
122 return ERR_FLATTEN_OBJECT;
123 }
124
125 uint32_t uid;
126 if (!data.ReadUint32(uid)) {
127 return ERR_FLATTEN_OBJECT;
128 }
129
130 uint32_t start;
131 if (!data.ReadUint32(start)) {
132 return ERR_FLATTEN_OBJECT;
133 }
134
135 uint32_t end;
136 if (!data.ReadUint32(end)) {
137 return ERR_FLATTEN_OBJECT;
138 }
139
140 NetStatsInfo stats;
141 int32_t result = static_cast<int32_t>(GetUidStatsDetail(iface, uid, start, end, stats));
142 if (!NetStatsInfo::Marshalling(reply, stats)) {
143 NETMGR_LOG_E("proxy Marshalling failed");
144 return ERR_FLATTEN_OBJECT;
145 }
146 NETMGR_LOG_I("NetStatsServiceStub::OnGetUidStatsDetail, result[%{public}d]", result);
147 if (!reply.WriteInt32(result)) {
148 return ERR_FLATTEN_OBJECT;
149 }
150 return ERR_NONE;
151 }
152
OnUpdateIfacesStats(MessageParcel & data,MessageParcel & reply)153 int32_t NetStatsServiceStub::OnUpdateIfacesStats(MessageParcel &data, MessageParcel &reply)
154 {
155 std::string iface;
156 if (!data.ReadString(iface)) {
157 return ERR_FLATTEN_OBJECT;
158 }
159
160 uint32_t start;
161 uint32_t end;
162 if (!data.ReadUint32(start) || !data.ReadUint32(end)) {
163 return ERR_FLATTEN_OBJECT;
164 }
165
166 NetStatsInfo stats;
167 if (!NetStatsInfo::Unmarshalling(data, stats)) {
168 NETMGR_LOG_E("NetStatsInfo::Unmarshalling failed");
169 }
170 int32_t ret = static_cast<int32_t>(UpdateIfacesStats(iface, start, end, stats));
171 if (!reply.WriteInt32(ret)) {
172 return ERR_FLATTEN_OBJECT;
173 }
174 return ERR_NONE;
175 }
176
OnUpdateStatsData(MessageParcel & data,MessageParcel & reply)177 int32_t NetStatsServiceStub::OnUpdateStatsData(MessageParcel &data, MessageParcel &reply)
178 {
179 if (!reply.WriteInt32(static_cast<int32_t>(UpdateStatsData()))) {
180 NETMGR_LOG_E("WriteInt32 failed");
181 return ERR_FLATTEN_OBJECT;
182 }
183 return ERR_NONE;
184 }
185
OnResetFactory(MessageParcel & data,MessageParcel & reply)186 int32_t NetStatsServiceStub::OnResetFactory(MessageParcel &data, MessageParcel &reply)
187 {
188 if (!reply.WriteInt32(static_cast<int32_t>(ResetFactory()))) {
189 NETMGR_LOG_E("WriteInt32 failed");
190 return ERR_FLATTEN_OBJECT;
191 }
192 return ERR_NONE;
193 }
194 } // namespace NetManagerStandard
195 } // namespace OHOS
196