• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "wifi_p2p_stub.h"
16 #include "wifi_logger.h"
17 #include "wifi_errcode.h"
18 #include "wifi_internal_event_dispatcher.h"
19 #include "wifi_p2p_callback_proxy.h"
20 #include "wifi_p2p_death_recipient.h"
21 #include "wifi_common_def.h"
22 #include "wifi_manager_service_ipc_interface_code.h"
23 
24 DEFINE_WIFILOG_P2P_LABEL("WifiP2pStub");
25 
26 namespace OHOS {
27 namespace Wifi {
WifiP2pStub()28 WifiP2pStub::WifiP2pStub() : mSingleCallback(false)
29 {
30     InitHandleMap();
31     deathRecipient_ = nullptr;
32 }
33 
~WifiP2pStub()34 WifiP2pStub::~WifiP2pStub()
35 {
36     deathRecipient_ = nullptr;
37 }
38 
InitHandleMapEx()39 void WifiP2pStub::InitHandleMapEx()
40 {
41     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REGISTER_CALLBACK)] =
42         &WifiP2pStub::OnRegisterCallBack;
43     handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES)] =
44         &WifiP2pStub::OnGetSupportedFeatures;
45     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME)] =
46         &WifiP2pStub::OnSetP2pDeviceName;
47     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO)] =
48         &WifiP2pStub::OnSetP2pWfdInfo;
49     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP)] =
50         &WifiP2pStub::OnHid2dRequestGcIp;
51     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE)] =
52         &WifiP2pStub::OnHid2dSharedlinkIncrease;
53     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE)] =
54         &WifiP2pStub::OnHid2dSharedlinkDecrease;
55     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP)] =
56         &WifiP2pStub::OnHid2dCreateGroup;
57     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP)] =
58         &WifiP2pStub::OnHid2dRemoveGcGroup;
59     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT)] =
60         &WifiP2pStub::OnHid2dConnect;
61     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP)] =
62         &WifiP2pStub::OnHid2dConfigIPAddr;
63     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP)] =
64         &WifiP2pStub::OnHid2dReleaseIPAddr;
65     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL)] =
66         &WifiP2pStub::OnHid2dGetRecommendChannel;
67     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST)] =
68         &WifiP2pStub::OnHid2dGetChannelListFor5G;
69     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG)] =
70         &WifiP2pStub::OnHid2dGetSelfWifiCfgInfo;
71     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG)] =
72         &WifiP2pStub::OnHid2dSetPeerWifiCfgInfo;
73     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE)] =
74         &WifiP2pStub::OnQueryP2pLocalDevice;
75     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE)] =
76         &WifiP2pStub::OnHid2dSetUpperScene;
77     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_PEERS)] =
78         &WifiP2pStub::OnDiscoverPeers;
79     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE_RANDOM_MAC)] =
80         &WifiP2pStub::OnDisableRandomMac;
81     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CHECK_CAN_USE_P2P)] =
82         &WifiP2pStub::OnCheckCanUseP2p;
83     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_WIDE_SUPPORTED)] =
84         &WifiP2pStub::OnHid2dIsWideBandwidthSupported;
85     return;
86 }
87 
InitHandleMap()88 void WifiP2pStub::InitHandleMap()
89 {
90     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_ENABLE)] = &WifiP2pStub::OnEnableP2p;
91     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE)] = &WifiP2pStub::OnDisableP2p;
92     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_DEVICES)] =
93         &WifiP2pStub::OnDiscoverDevices;
94     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES)] =
95         &WifiP2pStub::OnStopDiscoverDevices;
96     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_SERVICES)] =
97         &WifiP2pStub::OnDiscoverServices;
98     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES)] =
99         &WifiP2pStub::OnStopDiscoverServices;
100     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REQUEST_SERVICES)] =
101         &WifiP2pStub::OnRequestService;
102     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES)] =
103         &WifiP2pStub::OnPutLocalP2pService;
104     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES)] =
105         &WifiP2pStub::OnDeleteLocalP2pService;
106     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_START_LISTEN)] =
107         &WifiP2pStub::OnStartP2pListen;
108     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_LISTEN)] =
109         &WifiP2pStub::OnStopP2pListen;
110     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CREATE_GROUP)] =
111         &WifiP2pStub::OnCreateGroup;
112     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP)] =
113         &WifiP2pStub::OnRemoveGroup;
114     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP_CLIENT)] =
115         &WifiP2pStub::OnRemoveGroupClient;
116     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP)] =
117         &WifiP2pStub::OnDeleteGroup;
118     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT)] = &WifiP2pStub::OnP2pConnect;
119     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT)] =
120         &WifiP2pStub::OnP2pCancelConnect;
121     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO)] =
122         &WifiP2pStub::OnQueryP2pLinkedInfo;
123     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP)] =
124         &WifiP2pStub::OnGetCurrentGroup;
125     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS)] =
126         &WifiP2pStub::OnGetP2pEnableStatus;
127     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS)] =
128         &WifiP2pStub::OnGetP2pDiscoverStatus;
129     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS)] =
130         &WifiP2pStub::OnGetP2pConnectedStatus;
131     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES)] =
132         &WifiP2pStub::OnQueryP2pDevices;
133     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS)] =
134         &WifiP2pStub::OnQueryP2pGroups;
135     handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES)] =
136         &WifiP2pStub::OnQueryP2pServices;
137     InitHandleMapEx();
138     return;
139 }
140 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)141 int WifiP2pStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
142 {
143     if (data.ReadInterfaceToken() != GetDescriptor()) {
144         WIFI_LOGE("P2p stub token verification error: %{public}d", code);
145         return WIFI_OPT_FAILED;
146     }
147 
148     HandleFuncMap::iterator iter = handleFuncMap.find(code);
149     if (iter == handleFuncMap.end()) {
150         WIFI_LOGD("not find function to deal, code %{public}u", code);
151         reply.WriteInt32(0);
152         reply.WriteInt32(WIFI_OPT_NOT_SUPPORTED);
153         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
154     } else {
155         int exception = data.ReadInt32();
156         if (exception) {
157             return WIFI_OPT_FAILED;
158         }
159         (this->*(iter->second))(code, data, reply, option);
160     }
161     return 0;
162 }
163 
OnEnableP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)164 void WifiP2pStub::OnEnableP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
165 {
166     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
167     ErrCode ret = EnableP2p();
168     reply.WriteInt32(0);
169     reply.WriteInt32(ret);
170     return;
171 }
172 
OnDisableP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)173 void WifiP2pStub::OnDisableP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
174 {
175     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
176     ErrCode ret = DisableP2p();
177     reply.WriteInt32(0);
178     reply.WriteInt32(ret);
179     return;
180 }
181 
OnDiscoverDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)182 void WifiP2pStub::OnDiscoverDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
183 {
184     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
185     ErrCode ret = DiscoverDevices();
186     reply.WriteInt32(0);
187     reply.WriteInt32(ret);
188     return;
189 }
190 
OnStopDiscoverDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)191 void WifiP2pStub::OnStopDiscoverDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
192 {
193     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
194     ErrCode ret = StopDiscoverDevices();
195     reply.WriteInt32(0);
196     reply.WriteInt32(ret);
197     return;
198 }
199 
OnDiscoverServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)200 void WifiP2pStub::OnDiscoverServices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
201 {
202     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
203     ErrCode ret = DiscoverServices();
204     reply.WriteInt32(0);
205     reply.WriteInt32(ret);
206     return;
207 }
208 
OnStopDiscoverServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)209 void WifiP2pStub::OnStopDiscoverServices(
210     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
211 {
212     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
213     ErrCode ret = StopDiscoverServices();
214     reply.WriteInt32(0);
215     reply.WriteInt32(ret);
216     return;
217 }
218 
OnRequestService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)219 void WifiP2pStub::OnRequestService(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
220 {
221     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
222     WifiP2pDevice device;
223     WifiP2pServiceRequest request;
224     if (!ReadWifiP2pServiceRequest(data, device, request)) {
225         reply.WriteInt32(0);
226         reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
227         return;
228     }
229     ErrCode ret = RequestService(device, request);
230     reply.WriteInt32(0);
231     reply.WriteInt32(ret);
232     return;
233 }
234 
OnPutLocalP2pService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)235 void WifiP2pStub::OnPutLocalP2pService(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
236 {
237     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
238     WifiP2pServiceInfo config;
239     if (!ReadWifiP2pServiceInfo(data, config)) {
240         reply.WriteInt32(0);
241         reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
242         return;
243     }
244     ErrCode ret = PutLocalP2pService(config);
245     reply.WriteInt32(0);
246     reply.WriteInt32(ret);
247     return;
248 }
249 
OnDeleteLocalP2pService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)250 void WifiP2pStub::OnDeleteLocalP2pService(
251     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
252 {
253     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
254     WifiP2pServiceInfo config;
255     if (!ReadWifiP2pServiceInfo(data, config)) {
256         reply.WriteInt32(0);
257         reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
258         return;
259     }
260     ErrCode ret = DeleteLocalP2pService(config);
261     reply.WriteInt32(0);
262     reply.WriteInt32(ret);
263     return;
264 }
265 
OnStartP2pListen(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)266 void WifiP2pStub::OnStartP2pListen(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
267 {
268     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
269     int period = data.ReadInt32();
270     int interval = data.ReadInt32();
271     ErrCode ret = StartP2pListen(period, interval);
272     reply.WriteInt32(0);
273     reply.WriteInt32(ret);
274     return;
275 }
276 
OnStopP2pListen(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)277 void WifiP2pStub::OnStopP2pListen(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
278 {
279     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
280     ErrCode ret = StopP2pListen();
281     reply.WriteInt32(0);
282     reply.WriteInt32(ret);
283     return;
284 }
285 
OnCreateGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)286 void WifiP2pStub::OnCreateGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
287 {
288     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
289     WifiP2pConfig config;
290     ReadWifiP2pConfigData(data, config);
291     ErrCode ret = CreateGroup(config);
292     reply.WriteInt32(0);
293     reply.WriteInt32(ret);
294     return;
295 }
296 
OnRemoveGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)297 void WifiP2pStub::OnRemoveGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
298 {
299     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
300     ErrCode ret = RemoveGroup();
301     reply.WriteInt32(0);
302     reply.WriteInt32(ret);
303     return;
304 }
305 
OnRemoveGroupClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)306 void WifiP2pStub::OnRemoveGroupClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
307 {
308     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
309     GcInfo info;
310     info.ip = data.ReadString();
311     info.mac = data.ReadString();
312     info.host = data.ReadString();
313     ErrCode ret = RemoveGroupClient(info);
314     reply.WriteInt32(0);
315     reply.WriteInt32(ret);
316     return;
317 }
318 
OnDeleteGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)319 void WifiP2pStub::OnDeleteGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
320 {
321     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
322     WifiP2pGroupInfo config;
323     if (!ReadWifiP2pGroupData(data, config)) {
324         reply.WriteInt32(0);
325         reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
326         return;
327     }
328     ErrCode ret = DeleteGroup(config);
329     reply.WriteInt32(0);
330     reply.WriteInt32(ret);
331     return;
332 }
333 
OnP2pConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)334 void WifiP2pStub::OnP2pConnect(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
335 {
336     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
337     WifiP2pConfig config;
338     ReadWifiP2pConfigData(data, config);
339 
340     ErrCode ret = P2pConnect(config);
341     reply.WriteInt32(0);
342     reply.WriteInt32(ret);
343     return;
344 }
345 
OnP2pCancelConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)346 void WifiP2pStub::OnP2pCancelConnect(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
347 {
348     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
349     ErrCode ret = P2pCancelConnect();
350     reply.WriteInt32(0);
351     reply.WriteInt32(ret);
352     return;
353 }
354 
OnQueryP2pLinkedInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)355 void WifiP2pStub::OnQueryP2pLinkedInfo(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
356 {
357     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
358     WifiP2pLinkedInfo config;
359     ErrCode ret = QueryP2pLinkedInfo(config);
360 
361     reply.WriteInt32(0);
362     reply.WriteInt32(ret);
363     if (ret == WIFI_OPT_SUCCESS) {
364         reply.WriteInt32(static_cast<int>(config.GetConnectState()));
365         reply.WriteBool(config.IsGroupOwner());
366         reply.WriteString(config.GetGroupOwnerAddress());
367         std::vector<GcInfo> gcInfos = config.GetClientInfoList();
368         int size = gcInfos.size();
369         reply.WriteInt32(size);
370         for (int i = 0; i < size; i++) {
371             reply.WriteString(gcInfos[i].mac);
372             reply.WriteString(gcInfos[i].ip);
373             reply.WriteString(gcInfos[i].host);
374         }
375     }
376     return;
377 }
378 
OnGetCurrentGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)379 void WifiP2pStub::OnGetCurrentGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
380 {
381     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
382     WifiP2pGroupInfo config;
383     ErrCode ret = GetCurrentGroup(config);
384 
385     reply.WriteInt32(0);
386     reply.WriteInt32(ret);
387     if (ret == WIFI_OPT_SUCCESS) {
388         WriteWifiP2pGroupData(reply, config);
389     }
390     return;
391 }
392 
OnGetP2pEnableStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)393 void WifiP2pStub::OnGetP2pEnableStatus(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
394 {
395     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
396     int status = 0;
397     ErrCode ret = GetP2pEnableStatus(status);
398     reply.WriteInt32(0);
399     reply.WriteInt32(ret);
400     if (ret == WIFI_OPT_SUCCESS) {
401         reply.WriteInt32(status);
402     }
403     return;
404 }
405 
OnGetP2pDiscoverStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)406 void WifiP2pStub::OnGetP2pDiscoverStatus(
407     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
408 {
409     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
410     int status = 0;
411     ErrCode ret = GetP2pDiscoverStatus(status);
412     reply.WriteInt32(0);
413     reply.WriteInt32(ret);
414     if (ret == WIFI_OPT_SUCCESS) {
415         reply.WriteInt32(status);
416     }
417     return;
418 }
419 
OnGetP2pConnectedStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)420 void WifiP2pStub::OnGetP2pConnectedStatus(
421     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
422 {
423     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
424     int status = 0;
425     ErrCode ret = GetP2pConnectedStatus(status);
426     reply.WriteInt32(0);
427     reply.WriteInt32(ret);
428     if (ret == WIFI_OPT_SUCCESS) {
429         reply.WriteInt32(status);
430     }
431     return;
432 }
433 
OnQueryP2pDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)434 void WifiP2pStub::OnQueryP2pDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
435 {
436     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
437     std::vector<WifiP2pDevice> devices;
438     ErrCode ret = QueryP2pDevices(devices);
439 
440     reply.WriteInt32(0);
441     reply.WriteInt32(ret);
442     if (ret == WIFI_OPT_SUCCESS) {
443         int size = static_cast<int>(devices.size());
444         reply.WriteInt32(size);
445         for (int i = 0; i < size; ++i) {
446             WriteWifiP2pDeviceData(reply, devices[i]);
447         }
448     }
449     return;
450 }
451 
OnQueryP2pLocalDevice(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)452 void WifiP2pStub::OnQueryP2pLocalDevice(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
453 {
454     WIFI_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
455     WifiP2pDevice device;
456     ErrCode ret = QueryP2pLocalDevice(device);
457     reply.WriteInt32(0);
458     reply.WriteInt32(ret);
459     if (ret == WIFI_OPT_SUCCESS) {
460         WriteWifiP2pDeviceData(reply, device);
461     }
462     return;
463 }
464 
OnQueryP2pGroups(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)465 void WifiP2pStub::OnQueryP2pGroups(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
466 {
467     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
468     std::vector<WifiP2pGroupInfo> groups;
469     ErrCode ret = QueryP2pGroups(groups);
470 
471     reply.WriteInt32(0);
472     reply.WriteInt32(ret);
473 
474     if (ret == WIFI_OPT_SUCCESS) {
475         int size = static_cast<int>(groups.size());
476         reply.WriteInt32(size);
477         for (int i = 0; i < size; ++i) {
478             WriteWifiP2pGroupData(reply, groups[i]);
479         }
480     }
481     return;
482 }
483 
OnQueryP2pServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)484 void WifiP2pStub::OnQueryP2pServices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
485 {
486     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
487     std::vector<WifiP2pServiceInfo> services;
488     ErrCode ret = QueryP2pServices(services);
489     reply.WriteInt32(0);
490     reply.WriteInt32(ret);
491 
492     if (ret == WIFI_OPT_SUCCESS) {
493         int size = static_cast<int>(services.size());
494         reply.WriteInt32(size);
495         for (int i = 0; i < size; ++i) {
496             WriteWifiP2pServiceInfo(reply, services[i]);
497         }
498     }
499     return;
500 }
501 
OnDiscoverPeers(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)502 void WifiP2pStub::OnDiscoverPeers(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
503 {
504     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
505     int channelid = data.ReadInt32();
506     ErrCode ret = DiscoverPeers(channelid);
507     reply.WriteInt32(0);
508     reply.WriteInt32(ret);
509     return;
510 }
511 
OnDisableRandomMac(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)512 void WifiP2pStub::OnDisableRandomMac(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
513 {
514     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
515     int setmode = data.ReadInt32();
516     ErrCode ret = DisableRandomMac(setmode);
517     reply.WriteInt32(0);
518     reply.WriteInt32(ret);
519     return;
520 }
521 
OnCheckCanUseP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)522 void WifiP2pStub::OnCheckCanUseP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
523 {
524     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
525     ErrCode ret = CheckCanUseP2p();
526     reply.WriteInt32(0);
527     reply.WriteInt32(ret);
528     return;
529 }
530 
ReadWifiP2pServiceInfo(MessageParcel & data,WifiP2pServiceInfo & info)531 bool WifiP2pStub::ReadWifiP2pServiceInfo(MessageParcel &data, WifiP2pServiceInfo &info)
532 {
533     const char *readStr = nullptr;
534     constexpr int MAX_QUERY_SIZE = 256;
535     readStr = data.ReadCString();
536     info.SetServiceName((readStr != nullptr) ? readStr : "");
537     readStr = data.ReadCString();
538     info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
539     info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(data.ReadInt32()));
540     std::vector<std::string> queryList;
541     int size = data.ReadInt32();
542     if (size > MAX_QUERY_SIZE) {
543         return false;
544     }
545     for (int i = 0; i < size; i++) {
546         readStr = data.ReadCString();
547         std::string str = (readStr != nullptr) ? readStr : "";
548         queryList.push_back(str);
549     }
550     info.SetQueryList(queryList);
551     return true;
552 }
553 
ReadWifiP2pServiceRequest(MessageParcel & data,WifiP2pDevice & device,WifiP2pServiceRequest & request)554 bool WifiP2pStub::ReadWifiP2pServiceRequest(MessageParcel &data, WifiP2pDevice &device, WifiP2pServiceRequest &request)
555 {
556     constexpr int MAX_QUERY_SIZE = 256;
557     ReadWifiP2pDeviceData(data, device);
558     request.SetProtocolType(static_cast<P2pServicerProtocolType>(data.ReadInt32()));
559     request.SetTransactionId(data.ReadInt32());
560     int size = data.ReadInt32();
561     if (size > MAX_QUERY_SIZE) {
562         return false;
563     }
564     std::vector<unsigned char> query;
565     for (int i = 0; i < size; i++) {
566         unsigned char chr = data.ReadInt8();
567         query.push_back(chr);
568     }
569     request.SetQuery(query);
570     return true;
571 }
572 
WriteWifiP2pServiceInfo(MessageParcel & reply,const WifiP2pServiceInfo & info)573 void WifiP2pStub::WriteWifiP2pServiceInfo(MessageParcel &reply, const WifiP2pServiceInfo &info)
574 {
575     reply.WriteCString(info.GetServiceName().c_str());
576     reply.WriteCString(info.GetDeviceAddress().c_str());
577     reply.WriteInt32(static_cast<int>(info.GetServicerProtocolType()));
578     reply.WriteInt32(info.GetQueryList().size());
579     std::vector<std::string> queryList = info.GetQueryList();
580     for (auto it = queryList.begin(); it != queryList.end(); it++) {
581         reply.WriteCString((*it).c_str());
582     }
583     return;
584 }
585 
ReadWifiP2pDeviceData(MessageParcel & data,WifiP2pDevice & device)586 void WifiP2pStub::ReadWifiP2pDeviceData(MessageParcel &data, WifiP2pDevice &device)
587 {
588     device.SetDeviceName(data.ReadString());
589     device.SetDeviceAddress(data.ReadString());
590     device.SetDeviceAddressType(data.ReadInt32());
591     device.SetPrimaryDeviceType(data.ReadString());
592     device.SetSecondaryDeviceType(data.ReadString());
593     device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(data.ReadInt32()));
594     WifiP2pWfdInfo wfdInfo;
595     wfdInfo.SetWfdEnabled(data.ReadBool());
596     wfdInfo.SetDeviceInfo(data.ReadInt32());
597     wfdInfo.SetCtrlPort(data.ReadInt32());
598     wfdInfo.SetMaxThroughput(data.ReadInt32());
599     device.SetWfdInfo(wfdInfo);
600     device.SetWpsConfigMethod(data.ReadInt32());
601     device.SetDeviceCapabilitys(data.ReadInt32());
602     device.SetGroupCapabilitys(data.ReadInt32());
603 }
604 
WriteWifiP2pDeviceData(MessageParcel & reply,const WifiP2pDevice & device)605 void WifiP2pStub::WriteWifiP2pDeviceData(MessageParcel &reply, const WifiP2pDevice &device)
606 {
607     reply.WriteString(device.GetDeviceName());
608     reply.WriteString(device.GetDeviceAddress());
609     reply.WriteString(device.GetRandomDeviceAddress());
610     reply.WriteInt32(device.GetDeviceAddressType());
611     reply.WriteString(device.GetPrimaryDeviceType());
612     reply.WriteString(device.GetSecondaryDeviceType());
613     reply.WriteInt32(static_cast<int>(device.GetP2pDeviceStatus()));
614     reply.WriteBool(device.GetWfdInfo().GetWfdEnabled());
615     reply.WriteInt32(device.GetWfdInfo().GetDeviceInfo());
616     reply.WriteInt32(device.GetWfdInfo().GetCtrlPort());
617     reply.WriteInt32(device.GetWfdInfo().GetMaxThroughput());
618     reply.WriteInt32(device.GetWpsConfigMethod());
619     reply.WriteInt32(device.GetDeviceCapabilitys());
620     reply.WriteInt32(device.GetGroupCapabilitys());
621 }
622 
ReadWifiP2pGroupData(MessageParcel & data,WifiP2pGroupInfo & info)623 bool WifiP2pStub::ReadWifiP2pGroupData(MessageParcel &data, WifiP2pGroupInfo &info)
624 {
625     constexpr int MAX_DEV_SIZE = 256;
626     info.SetIsGroupOwner(data.ReadBool());
627     WifiP2pDevice device;
628     ReadWifiP2pDeviceData(data, device);
629     info.SetOwner(device);
630     info.SetPassphrase(data.ReadString());
631     info.SetInterface(data.ReadString());
632     info.SetGroupName(data.ReadString());
633     info.SetFrequency(data.ReadInt32());
634     info.SetIsPersistent(data.ReadBool());
635     info.SetP2pGroupStatus(static_cast<P2pGroupStatus>(data.ReadInt32()));
636     info.SetNetworkId(data.ReadInt32());
637     info.SetGoIpAddress(data.ReadString());
638     int size = data.ReadInt32();
639     if (size > MAX_DEV_SIZE) {
640         return false;
641     }
642     for (auto it = 0; it < size; ++it) {
643         WifiP2pDevice cliDev;
644         ReadWifiP2pDeviceData(data, cliDev);
645         info.AddClientDevice(cliDev);
646     }
647     return true;
648 }
649 
WriteWifiP2pGroupData(MessageParcel & reply,const WifiP2pGroupInfo & info)650 void WifiP2pStub::WriteWifiP2pGroupData(MessageParcel &reply, const WifiP2pGroupInfo &info)
651 {
652     reply.WriteBool(info.IsGroupOwner());
653     WriteWifiP2pDeviceData(reply, info.GetOwner());
654     reply.WriteString(info.GetPassphrase());
655     reply.WriteString(info.GetInterface());
656     reply.WriteString(info.GetGroupName());
657     reply.WriteInt32(info.GetFrequency());
658     reply.WriteBool(info.IsPersistent());
659     reply.WriteInt32(static_cast<int>(info.GetP2pGroupStatus()));
660     reply.WriteInt32(info.GetNetworkId());
661     reply.WriteString(info.GetGoIpAddress());
662     std::vector<WifiP2pDevice> deviceVec = info.GetClientDevices();
663     reply.WriteInt32(deviceVec.size());
664     for (auto it = deviceVec.begin(); it != deviceVec.end(); ++it) {
665         WriteWifiP2pDeviceData(reply, *it);
666     }
667 }
668 
ReadWifiP2pConfigData(MessageParcel & data,WifiP2pConfig & config)669 void WifiP2pStub::ReadWifiP2pConfigData(MessageParcel &data, WifiP2pConfig &config)
670 {
671     config.SetDeviceAddress(data.ReadString());
672     config.SetDeviceAddressType(data.ReadInt32());
673     config.SetPassphrase(data.ReadString());
674     config.SetGroupName(data.ReadString());
675     config.SetGoBand(static_cast<GroupOwnerBand>(data.ReadInt32()));
676     config.SetNetId(data.ReadInt32());
677     config.SetGroupOwnerIntent(data.ReadInt32());
678 }
679 
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)680 void WifiP2pStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
681 {
682     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
683     ErrCode ret = WIFI_OPT_FAILED;
684     do {
685         sptr<IRemoteObject> remote = data.ReadRemoteObject();
686         if (remote == nullptr) {
687             WIFI_LOGE("Failed to readRemoteObject!");
688             break;
689         }
690 
691         sptr<IWifiP2pCallback> callback_ = iface_cast<IWifiP2pCallback>(remote);
692         if (callback_ == nullptr) {
693             callback_ = new (std::nothrow) WifiP2pCallbackProxy(remote);
694             WIFI_LOGI("create new `WifiP2pCallbackProxy`!");
695         }
696 
697         int pid = data.ReadInt32();
698         int tokenId = data.ReadInt32();
699         int eventNum = data.ReadInt32();
700         std::vector<std::string> event;
701         if (eventNum > 0 && eventNum <= MAX_READ_EVENT_SIZE) {
702             for (int i = 0; i < eventNum; ++i) {
703                 event.emplace_back(data.ReadString());
704             }
705         }
706         WIFI_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
707 
708         if (mSingleCallback) {
709             ret = RegisterCallBack(callback_, event);
710         } else {
711             std::unique_lock<std::mutex> lock(deathRecipientMutex);
712             if (deathRecipient_ == nullptr) {
713                 deathRecipient_ = new (std::nothrow) WifiP2pDeathRecipient();
714             }
715             // Add death recipient to remote object if this is the first time to register callback.
716             if ((remote->IsProxyObject()) &&
717                 !WifiInternalEventDispatcher::GetInstance().HasP2pRemote(remote)) {
718                 remote->AddDeathRecipient(deathRecipient_);
719             }
720 
721             if (callback_ != nullptr) {
722                 for (const auto &eventName : event) {
723                     ret = WifiInternalEventDispatcher::GetInstance().AddP2pCallback(remote, callback_, pid,
724                         eventName, tokenId);
725                 }
726             }
727         }
728         MonitorCfgChange();
729     } while (0);
730 
731     reply.WriteInt32(0);
732     reply.WriteInt32(ret);
733     return;
734 }
735 
OnGetSupportedFeatures(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)736 void WifiP2pStub::OnGetSupportedFeatures(
737     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
738 {
739     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
740     long features = 0;
741     int ret = GetSupportedFeatures(features);
742     reply.WriteInt32(0);
743     reply.WriteInt32(ret);
744 
745     if (ret == WIFI_OPT_SUCCESS) {
746         reply.WriteInt64(features);
747     }
748 
749     return;
750 }
751 
OnSetP2pDeviceName(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)752 void WifiP2pStub::OnSetP2pDeviceName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
753 {
754     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
755     ErrCode ret = WIFI_OPT_FAILED;
756     std::string deviceName = data.ReadString();
757     ret = SetP2pDeviceName(deviceName);
758     reply.WriteInt32(0);
759     reply.WriteInt32(ret);
760 
761     return;
762 }
763 
OnSetP2pWfdInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)764 void WifiP2pStub::OnSetP2pWfdInfo(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
765 {
766     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
767     WifiP2pWfdInfo wfdInfo;
768     wfdInfo.SetWfdEnabled(data.ReadBool());
769     wfdInfo.SetDeviceInfo(data.ReadInt32());
770     wfdInfo.SetCtrlPort(data.ReadInt32());
771     wfdInfo.SetMaxThroughput(data.ReadInt32());
772 
773     int ret = SetP2pWfdInfo(wfdInfo);
774     reply.WriteInt32(0);
775     reply.WriteInt32(ret);
776     return;
777 }
778 
OnHid2dRequestGcIp(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)779 void WifiP2pStub::OnHid2dRequestGcIp(uint32_t code, MessageParcel &data,
780     MessageParcel &reply, MessageOption &option)
781 {
782     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
783 
784     ErrCode ret = WIFI_OPT_FAILED;
785     const char *readStr = data.ReadCString();
786     std::string ipAddr;
787     if (readStr == nullptr) {
788         ret = WIFI_OPT_INVALID_PARAM;
789     } else {
790         std::string gcMac = readStr;
791         ret = Hid2dRequestGcIp(gcMac, ipAddr);
792     }
793     reply.WriteInt32(0);
794     reply.WriteInt32(ret);
795     if (ret == WIFI_OPT_SUCCESS) {
796         reply.WriteCString(ipAddr.c_str());
797     }
798 }
799 
OnHid2dSharedlinkIncrease(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)800 void WifiP2pStub::OnHid2dSharedlinkIncrease(uint32_t code, MessageParcel &data,
801     MessageParcel &reply, MessageOption &option)
802 {
803     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
804 
805     int ret = Hid2dSharedlinkIncrease();
806     reply.WriteInt32(0);
807     reply.WriteInt32(ret);
808 }
809 
OnHid2dSharedlinkDecrease(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)810 void WifiP2pStub::OnHid2dSharedlinkDecrease(uint32_t code, MessageParcel &data,
811     MessageParcel &reply, MessageOption &option)
812 {
813     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
814     int ret = Hid2dSharedlinkDecrease();
815     reply.WriteInt32(0);
816     reply.WriteInt32(ret);
817 }
818 
OnHid2dCreateGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)819 void WifiP2pStub::OnHid2dCreateGroup(uint32_t code, MessageParcel &data,
820     MessageParcel &reply, MessageOption &option)
821 {
822     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
823 
824     int frequency = data.ReadInt32();
825     int type = data.ReadInt32();
826     int ret = Hid2dCreateGroup(frequency, FreqType(type));
827     reply.WriteInt32(0);
828     reply.WriteInt32(ret);
829 }
830 
OnHid2dRemoveGcGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)831 void WifiP2pStub::OnHid2dRemoveGcGroup(uint32_t code, MessageParcel &data,
832     MessageParcel &reply, MessageOption &option)
833 {
834     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
835 
836     ErrCode ret = WIFI_OPT_FAILED;
837     const char *readStr = data.ReadCString();
838     if (readStr == nullptr) {
839         ret = WIFI_OPT_INVALID_PARAM;
840     } else {
841         std::string gcIfName = readStr;
842         ret = Hid2dRemoveGcGroup(gcIfName);
843     }
844     reply.WriteInt32(0);
845     reply.WriteInt32(ret);
846 }
847 
OnHid2dConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)848 void WifiP2pStub::OnHid2dConnect(uint32_t code, MessageParcel &data,
849     MessageParcel &reply, MessageOption &option)
850 {
851     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
852 
853     ErrCode ret = WIFI_OPT_FAILED;
854     Hid2dConnectConfig config;
855     const char *ssidRead = data.ReadCString();
856     const char *bssidRead = data.ReadCString();
857     const char *preSharedKeyRead = data.ReadCString();
858     if (ssidRead == nullptr || bssidRead == nullptr || preSharedKeyRead == nullptr) {
859         ret = WIFI_OPT_INVALID_PARAM;
860     } else {
861         config.SetSsid(ssidRead);
862         config.SetBssid(bssidRead);
863         config.SetPreSharedKey(preSharedKeyRead);
864         config.SetFrequency(data.ReadInt32());
865         config.SetDhcpMode(DhcpMode(data.ReadInt32()));
866 
867         ret = Hid2dConnect(config);
868     }
869 
870     reply.WriteInt32(0);
871     reply.WriteInt32(ret);
872 }
873 
OnHid2dConfigIPAddr(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)874 void WifiP2pStub::OnHid2dConfigIPAddr(uint32_t code, MessageParcel &data,
875     MessageParcel &reply, MessageOption &option)
876 {
877     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
878 
879     ErrCode ret = WIFI_OPT_FAILED;
880     IpAddrInfo ipInfo;
881     const char *ifNameRead = data.ReadCString();
882     const char *ipRead = data.ReadCString();
883     const char *gatewayRead = data.ReadCString();
884     const char *netmaskRead = data.ReadCString();
885     if (ifNameRead == nullptr || ipRead == nullptr || gatewayRead == nullptr || netmaskRead == nullptr) {
886         ret = WIFI_OPT_INVALID_PARAM;
887     } else {
888         std::string ifName = ifNameRead;
889         ipInfo.ip = ipRead;
890         ipInfo.gateway = gatewayRead;
891         ipInfo.netmask = netmaskRead;
892         ret = Hid2dConfigIPAddr(ifName, ipInfo);
893     }
894     reply.WriteInt32(0);
895     reply.WriteInt32(ret);
896 }
897 
OnHid2dReleaseIPAddr(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)898 void WifiP2pStub::OnHid2dReleaseIPAddr(uint32_t code, MessageParcel &data,
899     MessageParcel &reply, MessageOption &option)
900 {
901     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
902 
903     ErrCode ret = WIFI_OPT_FAILED;
904     const char *ifNameRead = data.ReadCString();
905     if (ifNameRead == nullptr) {
906         ret = WIFI_OPT_INVALID_PARAM;
907     } else {
908         std::string ifName = ifNameRead;
909         ret = Hid2dReleaseIPAddr(ifName);
910     }
911     reply.WriteInt32(0);
912     reply.WriteInt32(ret);
913 }
914 
OnHid2dGetRecommendChannel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)915 void WifiP2pStub::OnHid2dGetRecommendChannel(
916     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
917 {
918     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
919     const char *readStr = nullptr;
920     RecommendChannelRequest request;
921     RecommendChannelResponse response;
922     readStr = data.ReadCString();
923     request.remoteIfName = (readStr != nullptr) ? readStr : "";
924     request.remoteIfMode = data.ReadInt32();
925     readStr = data.ReadCString();
926     request.localIfName = (readStr != nullptr) ? readStr : "";
927     request.localIfMode = data.ReadInt32();
928     request.prefBand = data.ReadInt32();
929     request.prefBandwidth = PreferBandwidth(data.ReadInt32());
930     ErrCode ret = Hid2dGetRecommendChannel(request, response);
931     reply.WriteInt32(0);
932     reply.WriteInt32(ret);
933     if (ret == WIFI_OPT_SUCCESS) {
934         reply.WriteInt32(static_cast<int>(response.status));
935         reply.WriteInt32(response.index);
936         reply.WriteInt32(response.centerFreq);
937         reply.WriteInt32(response.centerFreq1);
938         reply.WriteInt32(response.centerFreq2);
939         reply.WriteInt32(response.bandwidth);
940     }
941 }
942 
OnHid2dGetChannelListFor5G(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)943 void WifiP2pStub::OnHid2dGetChannelListFor5G(
944     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
945 {
946     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
947 
948     std::vector<int> vecChannelList;
949     ErrCode ret = Hid2dGetChannelListFor5G(vecChannelList);
950     reply.WriteInt32(0);
951     reply.WriteInt32(ret);
952     if (ret == WIFI_OPT_SUCCESS) {
953         reply.WriteInt32((int)vecChannelList.size());
954         for (auto& channel : vecChannelList) {
955             reply.WriteInt32(channel);
956         }
957     }
958 }
959 
OnHid2dGetSelfWifiCfgInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)960 void WifiP2pStub::OnHid2dGetSelfWifiCfgInfo(
961     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
962 {
963     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
964 
965     int cfgType = data.ReadInt32();
966     int len = 0;
967     char cfgData[CFG_DATA_MAX_BYTES];
968     if (memset_s(cfgData, CFG_DATA_MAX_BYTES, 0, CFG_DATA_MAX_BYTES) != EOK) {
969         WIFI_LOGE("`%{public}s` memset_s failed!", __func__);
970     }
971     ErrCode ret = Hid2dGetSelfWifiCfgInfo(SelfCfgType(cfgType), cfgData, &len);
972     reply.WriteInt32(0);
973     reply.WriteInt32(ret);
974     reply.WriteInt32(len);
975     if (ret == WIFI_OPT_SUCCESS && len > 0) {
976         reply.WriteBuffer(cfgData, len);
977     }
978 }
979 
OnHid2dSetPeerWifiCfgInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)980 void WifiP2pStub::OnHid2dSetPeerWifiCfgInfo(
981     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
982 {
983     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
984 
985     char cfgData[CFG_DATA_MAX_BYTES];
986     if (memset_s(cfgData, CFG_DATA_MAX_BYTES, 0, CFG_DATA_MAX_BYTES) != EOK) {
987         WIFI_LOGE("`%{public}s` memset_s failed!", __func__);
988     }
989     int cfgType = data.ReadInt32();
990     int len = data.ReadInt32();
991     const char *dataBuffer = (const char *)data.ReadBuffer(len);
992     if (memcpy_s(cfgData, CFG_DATA_MAX_BYTES, dataBuffer, len) != EOK) {
993         WIFI_LOGE("`%{public}s` memcpy_s failed!", __func__);
994         reply.WriteInt32(0);
995         reply.WriteInt32(WIFI_OPT_FAILED);
996         return;
997     }
998     ErrCode ret = Hid2dSetPeerWifiCfgInfo(PeerCfgType(cfgType), cfgData, len);
999     reply.WriteInt32(0);
1000     reply.WriteInt32(ret);
1001 }
1002 
OnHid2dSetUpperScene(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1003 void WifiP2pStub::OnHid2dSetUpperScene(
1004     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1005 {
1006     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1007 
1008     ErrCode ret = WIFI_OPT_FAILED;
1009     Hid2dUpperScene scene;
1010     const char *ifNameRead = data.ReadCString();
1011     const char *macRead = data.ReadCString();
1012     if (ifNameRead == nullptr || macRead == nullptr) {
1013         ret = WIFI_OPT_INVALID_PARAM;
1014     } else {
1015         std::string ifName = ifNameRead;
1016         scene.mac = macRead;
1017         scene.scene = data.ReadUint32();
1018         scene.fps = data.ReadInt32();
1019         scene.bw = data.ReadUint32();
1020         ret = Hid2dSetUpperScene(ifName, scene);
1021     }
1022     reply.WriteInt32(0);
1023     reply.WriteInt32(ret);
1024 }
1025 
IsSingleCallback() const1026 bool WifiP2pStub::IsSingleCallback() const
1027 {
1028     return mSingleCallback;
1029 }
1030 
SetSingleCallback(const bool isSingleCallback)1031 void WifiP2pStub::SetSingleCallback(const bool isSingleCallback)
1032 {
1033     mSingleCallback = true;
1034 }
1035 
OnHid2dIsWideBandwidthSupported(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1036 void WifiP2pStub::OnHid2dIsWideBandwidthSupported(
1037     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1038 {
1039     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1040     bool isSupport = false;
1041     ErrCode ret = Hid2dIsWideBandwidthSupported(isSupport);
1042     reply.WriteInt32(0);
1043     reply.WriteInt32(ret);
1044     if (ret == WIFI_OPT_SUCCESS) {
1045         reply.WriteInt32(isSupport ? 1 : 0);
1046     }
1047 }
1048 }  // namespace Wifi
1049 }  // namespace OHOS