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