1 /*
2 * Copyright (c) 2022-2023 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 "networkshare_service_stub.h"
17 #include "netmgr_ext_log_wrapper.h"
18 #include "net_manager_constants.h"
19 #include "networkshare_constants.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
NetworkShareServiceStub()23 NetworkShareServiceStub::NetworkShareServiceStub()
24 {
25 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_SHARING_SUPPORTED)] =
26 &NetworkShareServiceStub::ReplyIsNetworkSharingSupported;
27 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_IS_SHARING)] =
28 &NetworkShareServiceStub::ReplyIsSharing;
29 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_START_NETWORKSHARE)] =
30 &NetworkShareServiceStub::ReplyStartNetworkSharing;
31 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_STOP_NETWORKSHARE)] =
32 &NetworkShareServiceStub::ReplyStopNetworkSharing;
33 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_SHARABLE_REGEXS)] =
34 &NetworkShareServiceStub::ReplyGetSharableRegexs;
35 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_SHARING_STATE)] =
36 &NetworkShareServiceStub::ReplyGetSharingState;
37 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_SHARING_IFACES)] =
38 &NetworkShareServiceStub::ReplyGetNetSharingIfaces;
39 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_REGISTER_EVENT_CALLBACK)] =
40 &NetworkShareServiceStub::ReplyRegisterSharingEvent;
41 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_UNREGISTER_EVENT_CALLBACK)] =
42 &NetworkShareServiceStub::ReplyUnregisterSharingEvent;
43 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_RX_BYTES)] =
44 &NetworkShareServiceStub::ReplyGetStatsRxBytes;
45 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_TX_BYTES)] =
46 &NetworkShareServiceStub::ReplyGetStatsTxBytes;
47 memberFuncMap_[static_cast<uint32_t>(TetheringInterfaceCode::CMD_GET_TOTAL_BYTES)] =
48 &NetworkShareServiceStub::ReplyGetStatsTotalBytes;
49 }
50
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)51 int32_t NetworkShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
52 MessageOption &option)
53 {
54 std::u16string myDescripter = NetworkShareServiceStub::GetDescriptor();
55 std::u16string remoteDesc = data.ReadInterfaceToken();
56 if (myDescripter != remoteDesc) {
57 NETMGR_EXT_LOG_E("descriptor checked failed");
58 return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
59 }
60 auto itFunction = memberFuncMap_.find(code);
61 if (itFunction != memberFuncMap_.end()) {
62 auto requestFunc = itFunction->second;
63 if (requestFunc != nullptr) {
64 return (this->*requestFunc)(data, reply);
65 }
66 }
67
68 NETMGR_EXT_LOG_I("stub default case, need check");
69 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71
ReplyIsNetworkSharingSupported(MessageParcel & data,MessageParcel & reply)72 int32_t NetworkShareServiceStub::ReplyIsNetworkSharingSupported(MessageParcel &data, MessageParcel &reply)
73 {
74 int32_t supported = NETWORKSHARE_IS_UNSUPPORTED;
75 int32_t ret = IsNetworkSharingSupported(supported);
76 if (!reply.WriteInt32(supported)) {
77 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
78 }
79 if (!reply.WriteInt32(ret)) {
80 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
81 }
82 return NETMANAGER_EXT_SUCCESS;
83 }
84
ReplyIsSharing(MessageParcel & data,MessageParcel & reply)85 int32_t NetworkShareServiceStub::ReplyIsSharing(MessageParcel &data, MessageParcel &reply)
86 {
87 int32_t sharingStatus = NETWORKSHARE_IS_UNSHARING;
88 int32_t ret = IsSharing(sharingStatus);
89 if (!reply.WriteInt32(sharingStatus)) {
90 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
91 }
92 if (!reply.WriteInt32(ret)) {
93 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
94 }
95 return NETMANAGER_EXT_SUCCESS;
96 }
97
ReplyStartNetworkSharing(MessageParcel & data,MessageParcel & reply)98 int32_t NetworkShareServiceStub::ReplyStartNetworkSharing(MessageParcel &data, MessageParcel &reply)
99 {
100 int32_t type;
101 if (!data.ReadInt32(type)) {
102 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
103 }
104 SharingIfaceType shareType = static_cast<SharingIfaceType>(type);
105 int32_t ret = StartNetworkSharing(shareType);
106 if (!reply.WriteInt32(ret)) {
107 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
108 }
109 return NETMANAGER_EXT_SUCCESS;
110 }
111
ReplyStopNetworkSharing(MessageParcel & data,MessageParcel & reply)112 int32_t NetworkShareServiceStub::ReplyStopNetworkSharing(MessageParcel &data, MessageParcel &reply)
113 {
114 int32_t type;
115 if (!data.ReadInt32(type)) {
116 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
117 }
118 SharingIfaceType shareType = static_cast<SharingIfaceType>(type);
119 int32_t ret = StopNetworkSharing(shareType);
120 if (!reply.WriteInt32(ret)) {
121 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
122 }
123 return NETMANAGER_EXT_SUCCESS;
124 }
125
ReplyGetSharableRegexs(MessageParcel & data,MessageParcel & reply)126 int32_t NetworkShareServiceStub::ReplyGetSharableRegexs(MessageParcel &data, MessageParcel &reply)
127 {
128 int32_t type;
129 if (!data.ReadInt32(type)) {
130 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
131 }
132 SharingIfaceType shareType = static_cast<SharingIfaceType>(type);
133 std::vector<std::string> ifaceRegexs;
134 int32_t ret = GetSharableRegexs(shareType, ifaceRegexs);
135
136 if (!reply.WriteUint32(ifaceRegexs.size())) {
137 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
138 }
139 for (auto it = ifaceRegexs.begin(); it != ifaceRegexs.end(); ++it) {
140 if (!reply.WriteString(*it)) {
141 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
142 }
143 }
144 if (!reply.WriteInt32(ret)) {
145 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
146 }
147 return NETMANAGER_EXT_SUCCESS;
148 }
149
ReplyGetSharingState(MessageParcel & data,MessageParcel & reply)150 int32_t NetworkShareServiceStub::ReplyGetSharingState(MessageParcel &data, MessageParcel &reply)
151 {
152 int32_t type;
153 if (!data.ReadInt32(type)) {
154 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
155 }
156 SharingIfaceType shareType = static_cast<SharingIfaceType>(type);
157 SharingIfaceState shareState = SharingIfaceState::SHARING_NIC_CAN_SERVER;
158 int32_t ret = GetSharingState(shareType, shareState);
159
160 if (!reply.WriteInt32(static_cast<int32_t>(shareState))) {
161 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
162 }
163 if (!reply.WriteInt32(ret)) {
164 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
165 }
166 return NETMANAGER_EXT_SUCCESS;
167 }
168
ReplyGetNetSharingIfaces(MessageParcel & data,MessageParcel & reply)169 int32_t NetworkShareServiceStub::ReplyGetNetSharingIfaces(MessageParcel &data, MessageParcel &reply)
170 {
171 int32_t state;
172 if (!data.ReadInt32(state)) {
173 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
174 }
175 SharingIfaceState shareState = static_cast<SharingIfaceState>(state);
176 std::vector<std::string> ifaces;
177 int32_t ret = GetNetSharingIfaces(shareState, ifaces);
178
179 if (!reply.WriteUint32(ifaces.size())) {
180 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
181 }
182 for (auto it = ifaces.begin(); it != ifaces.end(); ++it) {
183 if (!reply.WriteString(*it)) {
184 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
185 }
186 }
187 if (!reply.WriteInt32(ret)) {
188 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
189 }
190 return NETMANAGER_EXT_SUCCESS;
191 }
192
ReplyRegisterSharingEvent(MessageParcel & data,MessageParcel & reply)193 int32_t NetworkShareServiceStub::ReplyRegisterSharingEvent(MessageParcel &data, MessageParcel &reply)
194 {
195 sptr<ISharingEventCallback> callback = iface_cast<ISharingEventCallback>(data.ReadRemoteObject());
196 if (callback == nullptr) {
197 NETMGR_EXT_LOG_E("ReplyRegisterSharingEvent callback is null.");
198 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
199 }
200
201 int32_t ret = RegisterSharingEvent(callback);
202 if (!reply.WriteInt32(ret)) {
203 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
204 }
205 return NETMANAGER_EXT_SUCCESS;
206 }
207
ReplyUnregisterSharingEvent(MessageParcel & data,MessageParcel & reply)208 int32_t NetworkShareServiceStub::ReplyUnregisterSharingEvent(MessageParcel &data, MessageParcel &reply)
209 {
210 sptr<ISharingEventCallback> callback = iface_cast<ISharingEventCallback>(data.ReadRemoteObject());
211 if (callback == nullptr) {
212 NETMGR_EXT_LOG_E("ReplyUnregisterSharingEvent callback is null.");
213 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
214 }
215
216 int32_t ret = UnregisterSharingEvent(callback);
217 if (!reply.WriteInt32(ret)) {
218 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
219 }
220 return NETMANAGER_EXT_SUCCESS;
221 }
222
ReplyGetStatsRxBytes(MessageParcel & data,MessageParcel & reply)223 int32_t NetworkShareServiceStub::ReplyGetStatsRxBytes(MessageParcel &data, MessageParcel &reply)
224 {
225 int32_t bytes = 0;
226 int32_t ret = GetStatsRxBytes(bytes);
227 if (!reply.WriteInt32(bytes)) {
228 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
229 }
230 if (!reply.WriteInt32(ret)) {
231 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
232 }
233 return NETMANAGER_EXT_SUCCESS;
234 }
235
ReplyGetStatsTxBytes(MessageParcel & data,MessageParcel & reply)236 int32_t NetworkShareServiceStub::ReplyGetStatsTxBytes(MessageParcel &data, MessageParcel &reply)
237 {
238 int32_t bytes = 0;
239 int32_t ret = GetStatsTxBytes(bytes);
240 if (!reply.WriteInt32(bytes)) {
241 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
242 }
243 if (!reply.WriteInt32(ret)) {
244 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
245 }
246 return NETMANAGER_EXT_SUCCESS;
247 }
248
ReplyGetStatsTotalBytes(MessageParcel & data,MessageParcel & reply)249 int32_t NetworkShareServiceStub::ReplyGetStatsTotalBytes(MessageParcel &data, MessageParcel &reply)
250 {
251 int32_t bytes = 0;
252 int32_t ret = GetStatsTotalBytes(bytes);
253 if (!reply.WriteInt32(bytes)) {
254 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
255 }
256 if (!reply.WriteInt32(ret)) {
257 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
258 }
259 return NETMANAGER_EXT_SUCCESS;
260 }
261 } // namespace NetManagerStandard
262 } // namespace OHOS
263