• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
16 #include "kits/c/wifi_p2p.h"
17 #include "kits/c/wifi_hid2d.h"
18 #include "wifi_logger.h"
19 #include "inner_api/wifi_p2p.h"
20 #include "wifi_c_utils.h"
21 #include "wifi_common_util.h"
22 #include "wifi_sa_event.h"
23 constexpr int INVALID_VALUE = -1;
24 #define STR_END '\0'
25 
26 DEFINE_WIFILOG_LABEL("WifiCP2P");
27 std::shared_ptr<OHOS::Wifi::WifiP2p> wifiP2pPtr = OHOS::Wifi::WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
28 
EnableP2p()29 NO_SANITIZE("cfi") WifiErrorCode EnableP2p()
30 {
31     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
32     EventManager::GetInstance().Init();
33     return GetCErrorCode(wifiP2pPtr->EnableP2p());
34 }
35 
DisableP2p()36 NO_SANITIZE("cfi") WifiErrorCode DisableP2p()
37 {
38     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
39     return GetCErrorCode(wifiP2pPtr->DisableP2p());
40 }
41 
GetP2pEnableStatus(P2pState * state)42 NO_SANITIZE("cfi") WifiErrorCode GetP2pEnableStatus(P2pState* state)
43 {
44     CHECK_PTR_RETURN(state, ERROR_WIFI_INVALID_ARGS);
45     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
46 
47     int p2pEnableStatus = INVALID_VALUE;
48     OHOS::Wifi::ErrCode ret = wifiP2pPtr->GetP2pEnableStatus(p2pEnableStatus);
49     *state = P2pState(p2pEnableStatus);
50     return GetCErrorCode(ret);
51 }
52 
DiscoverDevices()53 NO_SANITIZE("cfi") WifiErrorCode DiscoverDevices()
54 {
55     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
56     return GetCErrorCode(wifiP2pPtr->DiscoverDevices());
57 }
58 
StopDiscoverDevices()59 NO_SANITIZE("cfi") WifiErrorCode StopDiscoverDevices()
60 {
61     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
62     return GetCErrorCode(wifiP2pPtr->StopDiscoverDevices());
63 }
64 
DiscoverServices()65 NO_SANITIZE("cfi") WifiErrorCode DiscoverServices()
66 {
67     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
68     return GetCErrorCode(wifiP2pPtr->DiscoverServices());
69 }
70 
StopDiscoverServices()71 NO_SANITIZE("cfi") WifiErrorCode StopDiscoverServices()
72 {
73     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
74     return GetCErrorCode(wifiP2pPtr->StopDiscoverServices());
75 }
76 
StartP2pListen(int period,int interval)77 NO_SANITIZE("cfi") WifiErrorCode StartP2pListen(int period, int interval)
78 {
79     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
80     return GetCErrorCode(wifiP2pPtr->StartP2pListen(period, interval));
81 }
82 
StopP2pListen()83 NO_SANITIZE("cfi") WifiErrorCode StopP2pListen()
84 {
85     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
86     return GetCErrorCode(wifiP2pPtr->StopP2pListen());
87 }
88 
ConvertConfigCToCpp(const WifiP2pConfig * config,OHOS::Wifi::WifiP2pConfig & cppConfig)89 static void ConvertConfigCToCpp(const WifiP2pConfig* config, OHOS::Wifi::WifiP2pConfig& cppConfig)
90 {
91     CHECK_PTR_RETURN_VOID(config);
92     cppConfig.SetDeviceAddress(OHOS::Wifi::MacArrayToStr(config->devAddr));
93     cppConfig.SetDeviceAddressType(config->bssidType);
94     cppConfig.SetGoBand(OHOS::Wifi::GroupOwnerBand(static_cast<int>(config->goBand)));
95     cppConfig.SetNetId(config->netId);
96     cppConfig.SetPassphrase(config->passphrase);
97     cppConfig.SetGroupOwnerIntent(config->groupOwnerIntent);
98     cppConfig.SetGroupName(config->groupName);
99 }
100 
CreateGroup(const WifiP2pConfig * config)101 NO_SANITIZE("cfi") WifiErrorCode CreateGroup(const WifiP2pConfig* config)
102 {
103     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
104     CHECK_PTR_RETURN(config, ERROR_WIFI_INVALID_ARGS);
105     OHOS::Wifi::WifiP2pConfig cppConfig;
106     ConvertConfigCToCpp(config, cppConfig);
107     return GetCErrorCode(wifiP2pPtr->CreateGroup(cppConfig));
108 }
109 
RemoveGroup()110 NO_SANITIZE("cfi") WifiErrorCode RemoveGroup()
111 {
112     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
113     return GetCErrorCode(wifiP2pPtr->RemoveGroup());
114 }
115 
ConvertP2PDeviceCToCpp(const WifiP2pDevice & p2pDevice,OHOS::Wifi::WifiP2pDevice & cppDevice)116 static void ConvertP2PDeviceCToCpp(const WifiP2pDevice& p2pDevice, OHOS::Wifi::WifiP2pDevice& cppDevice)
117 {
118     cppDevice.SetDeviceName(p2pDevice.deviceName);
119     cppDevice.SetDeviceAddress(OHOS::Wifi::MacArrayToStr(p2pDevice.devAddr));
120     cppDevice.SetDeviceAddressType(p2pDevice.bssidType);
121     cppDevice.SetPrimaryDeviceType(p2pDevice.primaryDeviceType);
122     cppDevice.SetSecondaryDeviceType(p2pDevice.secondaryDeviceType);
123     cppDevice.SetP2pDeviceStatus(OHOS::Wifi::P2pDeviceStatus(static_cast<int>(p2pDevice.status)));
124 
125     OHOS::Wifi::WifiP2pWfdInfo wfdInfo;
126     wfdInfo.SetWfdEnabled((bool)p2pDevice.wfdInfo.wfdEnabled);
127     wfdInfo.SetDeviceInfo(p2pDevice.wfdInfo.deviceInfo);
128     wfdInfo.SetCtrlPort(p2pDevice.wfdInfo.ctrlPort);
129     wfdInfo.SetMaxThroughput(p2pDevice.wfdInfo.maxThroughput);
130     cppDevice.SetWfdInfo(wfdInfo);
131 
132     cppDevice.SetWpsConfigMethod(p2pDevice.supportWpsConfigMethods);
133     cppDevice.SetDeviceCapabilitys(p2pDevice.deviceCapabilitys);
134     cppDevice.SetGroupCapabilitys(p2pDevice.groupCapabilitys);
135 }
136 
ConvertGroupInfoCToCpp(const WifiP2pGroupInfo * group,OHOS::Wifi::WifiP2pGroupInfo & cppGroup)137 static void ConvertGroupInfoCToCpp(const WifiP2pGroupInfo* group, OHOS::Wifi::WifiP2pGroupInfo& cppGroup)
138 {
139     CHECK_PTR_RETURN_VOID(group);
140     OHOS::Wifi::WifiP2pDevice owner;
141     ConvertP2PDeviceCToCpp(group->owner, owner);
142     cppGroup.SetOwner(owner);
143     cppGroup.SetIsGroupOwner((bool)group->isP2pGroupOwner);
144     cppGroup.SetPassphrase(group->passphrase);
145     cppGroup.SetInterface(group->interface);
146     cppGroup.SetGroupName(group->groupName);
147     cppGroup.SetNetworkId(group->networkId);
148     cppGroup.SetFrequency(group->frequency);
149     cppGroup.SetIsPersistent((bool)group->isP2pPersistent);
150     cppGroup.SetP2pGroupStatus(OHOS::Wifi::P2pGroupStatus(static_cast<int>(group->groupStatus)));
151     std::vector<OHOS::Wifi::WifiP2pDevice> clientDevices;
152     for (int i = 0; i != group->clientDevicesSize && i < MAX_DEVICES_NUM; ++i) {
153         OHOS::Wifi::WifiP2pDevice p2pDevice;
154         ConvertP2PDeviceCToCpp(group->clientDevices[i], p2pDevice);
155         clientDevices.emplace_back(p2pDevice);
156     }
157     cppGroup.SetClientDevices(clientDevices);
158     cppGroup.SetGoIpAddress(group->goIpAddress);
159 }
160 
DeleteGroup(const WifiP2pGroupInfo * group)161 NO_SANITIZE("cfi") WifiErrorCode DeleteGroup(const WifiP2pGroupInfo* group)
162 {
163     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
164     CHECK_PTR_RETURN(group, ERROR_WIFI_INVALID_ARGS);
165     OHOS::Wifi::WifiP2pGroupInfo groupInfo;
166     ConvertGroupInfoCToCpp(group, groupInfo);
167     return GetCErrorCode(wifiP2pPtr->DeleteGroup(groupInfo));
168 }
169 
P2pConnect(const WifiP2pConfig * config)170 NO_SANITIZE("cfi") WifiErrorCode P2pConnect(const WifiP2pConfig* config)
171 {
172     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
173     CHECK_PTR_RETURN(config, ERROR_WIFI_INVALID_ARGS);
174     OHOS::Wifi::WifiP2pConfig deviceConfig;
175     ConvertConfigCToCpp(config, deviceConfig);
176     return GetCErrorCode(wifiP2pPtr->P2pConnect(deviceConfig));
177 }
178 
P2pCancelConnect()179 NO_SANITIZE("cfi") WifiErrorCode P2pCancelConnect()
180 {
181     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
182     return GetCErrorCode(wifiP2pPtr->P2pCancelConnect());
183 }
184 
ConvertP2PDeviceCppToC(const OHOS::Wifi::WifiP2pDevice & cppDevice,WifiP2pDevice * p2pDevice)185 static OHOS::Wifi::ErrCode ConvertP2PDeviceCppToC(const OHOS::Wifi::WifiP2pDevice& cppDevice, WifiP2pDevice* p2pDevice)
186 {
187     CHECK_PTR_RETURN(p2pDevice, OHOS::Wifi::WIFI_OPT_INVALID_PARAM);
188     if (memcpy_s(p2pDevice->deviceName, P2P_NAME_LENGTH,
189         cppDevice.GetDeviceName().c_str(), cppDevice.GetDeviceName().size() + 1) != EOK) {
190         WIFI_LOGE("memcpy_s device name failed!");
191         return OHOS::Wifi::WIFI_OPT_FAILED;
192     }
193     if (OHOS::Wifi::MacStrToArray(cppDevice.GetDeviceAddress(), p2pDevice->devAddr) != EOK) {
194         WIFI_LOGE("Mac str to array failed!");
195         return OHOS::Wifi::WIFI_OPT_FAILED;
196     }
197     if (OHOS::Wifi::MacStrToArray(cppDevice.GetRandomDeviceAddress(), p2pDevice->randomDevAddr) != EOK) {
198         WIFI_LOGI("randomDevAddr Mac str to array failed!");
199     }
200     p2pDevice->bssidType = cppDevice.GetDeviceAddressType();
201     if (memcpy_s(p2pDevice->primaryDeviceType, DEVICE_TYPE_LENGTH,
202         cppDevice.GetPrimaryDeviceType().c_str(), cppDevice.GetPrimaryDeviceType().size() + 1) != EOK) {
203         WIFI_LOGE("memcpy_s primary device type failed!");
204         return OHOS::Wifi::WIFI_OPT_FAILED;
205     }
206     if (memcpy_s(p2pDevice->secondaryDeviceType, DEVICE_TYPE_LENGTH,
207         cppDevice.GetSecondaryDeviceType().c_str(), cppDevice.GetSecondaryDeviceType().size() + 1) != EOK) {
208         WIFI_LOGE("memcpy_s secondary device type failed!");
209         return OHOS::Wifi::WIFI_OPT_FAILED;
210     }
211 
212     p2pDevice->status = P2pDeviceStatus(static_cast<int>(cppDevice.GetP2pDeviceStatus()));
213     p2pDevice->wfdInfo.wfdEnabled = cppDevice.GetWfdInfo().GetWfdEnabled();
214     p2pDevice->wfdInfo.deviceInfo = cppDevice.GetWfdInfo().GetDeviceInfo();
215     p2pDevice->wfdInfo.ctrlPort = cppDevice.GetWfdInfo().GetCtrlPort();
216     p2pDevice->wfdInfo.maxThroughput = cppDevice.GetWfdInfo().GetMaxThroughput();
217     p2pDevice->supportWpsConfigMethods = cppDevice.GetWpsConfigMethod();
218     p2pDevice->deviceCapabilitys = cppDevice.GetDeviceCapabilitys();
219     p2pDevice->groupCapabilitys = cppDevice.GetGroupCapabilitys();
220     return OHOS::Wifi::WIFI_OPT_SUCCESS;
221 }
222 
ConvertGroupInfoCppToC(const OHOS::Wifi::WifiP2pGroupInfo & cppGroup,WifiP2pGroupInfo * group)223 static OHOS::Wifi::ErrCode ConvertGroupInfoCppToC(const OHOS::Wifi::WifiP2pGroupInfo& cppGroup, WifiP2pGroupInfo* group)
224 {
225     CHECK_PTR_RETURN(group, OHOS::Wifi::WIFI_OPT_INVALID_PARAM);
226     if (ConvertP2PDeviceCppToC(cppGroup.GetOwner(), &group->owner) != OHOS::Wifi::WIFI_OPT_SUCCESS) {
227         return OHOS::Wifi::WIFI_OPT_FAILED;
228     }
229     group->isP2pGroupOwner = cppGroup.IsGroupOwner();
230     if (cppGroup.GetPassphrase().size() >= PASSPHRASE_LENGTH) {
231         WIFI_LOGE("passwork len is invaild");
232         if (memcpy_s(group->passphrase, PASSPHRASE_LENGTH,
233             cppGroup.GetPassphrase().c_str(), PASSPHRASE_LENGTH) != EOK) {
234             WIFI_LOGE("memcpy_s passphrase failed!");
235             return OHOS::Wifi::WIFI_OPT_FAILED;
236         }
237     } else {
238         if (memcpy_s(group->passphrase, PASSPHRASE_LENGTH,
239             cppGroup.GetPassphrase().c_str(), cppGroup.GetPassphrase().size()) != EOK) {
240             WIFI_LOGE("memcpy_s passphrase failed!");
241             return OHOS::Wifi::WIFI_OPT_FAILED;
242         }
243         group->passphrase[cppGroup.GetPassphrase().size()] = STR_END;
244     }
245     if (memcpy_s(group->interface, INTERFACE_LENGTH,
246         cppGroup.GetInterface().c_str(), cppGroup.GetInterface().size() + 1) != EOK) {
247         WIFI_LOGE("memcpy_s interface failed!");
248         return OHOS::Wifi::WIFI_OPT_FAILED;
249     }
250     if (memcpy_s(group->groupName, P2P_NAME_LENGTH,
251         cppGroup.GetGroupName().c_str(), cppGroup.GetGroupName().size() + 1) != EOK) {
252         WIFI_LOGE("memcpy_s group name failed!");
253         return OHOS::Wifi::WIFI_OPT_FAILED;
254     }
255     group->networkId = cppGroup.GetNetworkId();
256     group->frequency = cppGroup.GetFrequency();
257     group->isP2pPersistent = cppGroup.IsPersistent();
258     group->groupStatus = P2pGroupStatus(static_cast<int>(cppGroup.GetP2pGroupStatus()));
259     const std::vector<OHOS::Wifi::WifiP2pDevice>& vecDevices = cppGroup.GetClientDevices();
260     for (size_t i = 0; i != vecDevices.size() && i < MAX_DEVICES_NUM; ++i) {
261         if (ConvertP2PDeviceCppToC(vecDevices[i], &group->clientDevices[i]) != OHOS::Wifi::WIFI_OPT_SUCCESS) {
262             WIFI_LOGE("convert p2p device failed!");
263             return OHOS::Wifi::WIFI_OPT_FAILED;
264         }
265     }
266     group->clientDevicesSize = (int)vecDevices.size();
267     if (memcpy_s(group->goIpAddress, IP_ADDR_STR_LEN,
268         cppGroup.GetGoIpAddress().c_str(), cppGroup.GetGoIpAddress().size() + 1) != EOK) {
269         WIFI_LOGE("memcpy_s interface failed!");
270         return OHOS::Wifi::WIFI_OPT_FAILED;
271     }
272     return OHOS::Wifi::WIFI_OPT_SUCCESS;
273 }
274 
GetCurrentGroup(WifiP2pGroupInfo * groupInfo)275 NO_SANITIZE("cfi") WifiErrorCode GetCurrentGroup(WifiP2pGroupInfo* groupInfo)
276 {
277     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
278     CHECK_PTR_RETURN(groupInfo, ERROR_WIFI_INVALID_ARGS);
279     OHOS::Wifi::WifiP2pGroupInfo cppGroupInfo;
280     OHOS::Wifi::ErrCode ret = wifiP2pPtr->GetCurrentGroup(cppGroupInfo);
281     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
282         WIFI_LOGE("get current group info failed!");
283         return ERROR_WIFI_NOT_AVAILABLE;
284     }
285     return GetCErrorCode(ConvertGroupInfoCppToC(cppGroupInfo, groupInfo));
286 }
287 
GetP2pConnectedStatus(int * status)288 NO_SANITIZE("cfi") WifiErrorCode GetP2pConnectedStatus(int* status)
289 {
290     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
291     CHECK_PTR_RETURN(status, ERROR_WIFI_INVALID_ARGS);
292     int p2pStatus = -1;
293     OHOS::Wifi::ErrCode ret = wifiP2pPtr->GetP2pConnectedStatus(p2pStatus);
294     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
295         WIFI_LOGE("get p2p status failed!");
296     }
297     *status = p2pStatus;
298     return GetCErrorCode(ret);
299 }
300 
QueryP2pLocalDevice(WifiP2pDevice * deviceInfo)301 NO_SANITIZE("cfi") WifiErrorCode QueryP2pLocalDevice(WifiP2pDevice* deviceInfo)
302 {
303     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
304     CHECK_PTR_RETURN(deviceInfo, ERROR_WIFI_INVALID_ARGS);
305     OHOS::Wifi::WifiP2pDevice cppDeviceInfo;
306     OHOS::Wifi::ErrCode ret = wifiP2pPtr->QueryP2pLocalDevice(cppDeviceInfo);
307     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
308         WIFI_LOGE("QueryP2pLocalDevice return failed!");
309     }
310     return GetCErrorCode(ConvertP2PDeviceCppToC(cppDeviceInfo, deviceInfo));
311 }
312 
QueryP2pDevices(WifiP2pDevice * clientDevices,int size,int * retSize)313 NO_SANITIZE("cfi") WifiErrorCode QueryP2pDevices(WifiP2pDevice* clientDevices, int size, int* retSize)
314 {
315     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
316     CHECK_PTR_RETURN(clientDevices, ERROR_WIFI_INVALID_ARGS);
317     CHECK_PTR_RETURN(retSize, ERROR_WIFI_INVALID_ARGS);
318     std::vector<OHOS::Wifi::WifiP2pDevice> vecDevices;
319     OHOS::Wifi::ErrCode ret = wifiP2pPtr->QueryP2pDevices(vecDevices);
320     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
321         WIFI_LOGE("query p2p devices failed!");
322         return ERROR_WIFI_UNKNOWN;
323     }
324 
325     for (int i = 0; i != (int)vecDevices.size() && i < size; ++i) {
326         if (ConvertP2PDeviceCppToC(vecDevices[i], clientDevices++) != OHOS::Wifi::WIFI_OPT_SUCCESS) {
327             WIFI_LOGE("convert p2p device failed!");
328             return ERROR_WIFI_UNKNOWN;
329         }
330     }
331     *retSize = std::min(size, (int)vecDevices.size());
332     return WIFI_SUCCESS;
333 }
334 
QueryP2pGroups(WifiP2pGroupInfo * groupInfo,int size)335 NO_SANITIZE("cfi") WifiErrorCode QueryP2pGroups(WifiP2pGroupInfo* groupInfo, int size)
336 {
337     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
338     CHECK_PTR_RETURN(groupInfo, ERROR_WIFI_INVALID_ARGS);
339     std::vector<OHOS::Wifi::WifiP2pGroupInfo> groups;
340     OHOS::Wifi::ErrCode ret = wifiP2pPtr->QueryP2pGroups(groups);
341     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
342         WIFI_LOGE("query p2p devices failed!");
343         return ERROR_WIFI_UNKNOWN;
344     }
345 
346     for (int i = 0; i != (int)groups.size() && i < size; ++i) {
347         ret = ConvertGroupInfoCppToC(groups[i], groupInfo++);
348         if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
349             WIFI_LOGE("convert group info failed!");
350             return ERROR_WIFI_UNKNOWN;
351         }
352     }
353     return WIFI_SUCCESS;
354 }
355 
OnP2pStateChanged(int state)356 void WifiP2pCEventCallback::OnP2pStateChanged(int state)
357 {
358     WIFI_LOGI("received state changed event: %{public}d", state);
359     std::unique_lock<std::mutex> lock(p2pCallbackMutex);
360     if (stateChangeCb) {
361         stateChangeCb(P2pState(state));
362     }
363 }
364 
OnP2pPersistentGroupsChanged(void)365 void WifiP2pCEventCallback::OnP2pPersistentGroupsChanged(void)
366 {
367     WIFI_LOGI("received group changed event");
368     std::unique_lock<std::mutex> lock(p2pCallbackMutex);
369     if (groupChangeCb) {
370         groupChangeCb();
371     }
372 }
373 
OnP2pThisDeviceChanged(const OHOS::Wifi::WifiP2pDevice & device)374 void WifiP2pCEventCallback::OnP2pThisDeviceChanged(const OHOS::Wifi::WifiP2pDevice &device)
375 {
376     WIFI_LOGI("%{public}s, received this device changed event", __func__);
377 }
378 
OnP2pPeersChanged(const std::vector<OHOS::Wifi::WifiP2pDevice> & devices)379 void WifiP2pCEventCallback::OnP2pPeersChanged(const std::vector<OHOS::Wifi::WifiP2pDevice> &devices)
380 {
381     WIFI_LOGI("received peers changed event: %{public}d", (int)devices.size());
382     WifiP2pDevice *devicePtr = nullptr;
383     if (!devices.empty()) {
384         devicePtr = new (std::nothrow) WifiP2pDevice[(int)devices.size()];
385         if (devicePtr == nullptr) {
386             WIFI_LOGE("new WifiP2pDevice failed!");
387             return;
388         }
389         WifiP2pDevice *p = devicePtr;
390         for (auto& each : devices) {
391             if (ConvertP2PDeviceCppToC(each, p++) != OHOS::Wifi::WIFI_OPT_SUCCESS) {
392                 WIFI_LOGE("peers changed convert p2p device failed!");
393                 delete[] devicePtr;
394                 return;
395             }
396         }
397     }
398 
399     {
400         std::unique_lock<std::mutex> lock(p2pCallbackMutex);
401         if (peersChangeCb) {
402             peersChangeCb(devicePtr, (int)devices.size());
403         }
404     }
405 
406     if (devicePtr) {
407         delete[] devicePtr;
408         devicePtr = nullptr;
409     }
410 }
411 
OnP2pPrivatePeersChanged(const std::string & priWfdInfo)412 void WifiP2pCEventCallback::OnP2pPrivatePeersChanged(const std::string &priWfdInfo)
413 {
414     WIFI_LOGI("%{public}s, received p2p Private Peer changed event", __func__);
415     char* wfdInfo  = const_cast<char*>(priWfdInfo.c_str());
416     std::unique_lock<std::mutex> lock(p2pCallbackMutex);
417     if (privatepeerChangeCb) {
418         privatepeerChangeCb(wfdInfo);
419     }
420 }
421 
OnP2pServicesChanged(const std::vector<OHOS::Wifi::WifiP2pServiceInfo> & srvInfo)422 void WifiP2pCEventCallback::OnP2pServicesChanged(const std::vector<OHOS::Wifi::WifiP2pServiceInfo> &srvInfo)
423 {
424     WIFI_LOGI("%{public}s, received p2p services changed event", __func__);
425 }
426 
OnP2pConnectionChanged(const OHOS::Wifi::WifiP2pLinkedInfo & info)427 void WifiP2pCEventCallback::OnP2pConnectionChanged(const OHOS::Wifi::WifiP2pLinkedInfo &info)
428 {
429     WIFI_LOGI("received connection changed event");
430     std::unique_lock<std::mutex> lock(p2pCallbackMutex);
431     if (connectionChangeCb) {
432         connectionChangeCb(ConvertP2pLinkedInfo(info));
433     }
434 }
435 
OnP2pDiscoveryChanged(bool isChange)436 void WifiP2pCEventCallback::OnP2pDiscoveryChanged(bool isChange)
437 {
438     WIFI_LOGI("%{public}s, received p2p discovery changed event", __func__);
439 }
440 
OnP2pActionResult(OHOS::Wifi::P2pActionCallback action,OHOS::Wifi::ErrCode code)441 void WifiP2pCEventCallback::OnP2pActionResult(OHOS::Wifi::P2pActionCallback action, OHOS::Wifi::ErrCode code)
442 {
443     WIFI_LOGI("%{public}s, received p2p action results event", __func__);
444 }
445 
OnConfigChanged(OHOS::Wifi::CfgType type,char * data,int dataLen)446 void WifiP2pCEventCallback::OnConfigChanged(OHOS::Wifi::CfgType type, char* data, int dataLen)
447 {
448     WIFI_LOGI("received config change event: %{public}d", static_cast<int>(type));
449     std::unique_lock<std::mutex> lock(p2pCallbackMutex);
450     if (cfgChangeCallback) {
451         cfgChangeCallback(CfgType(type), data, dataLen);
452     }
453 }
454 
OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo & info)455 void WifiP2pCEventCallback::OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info)
456 {
457     WIFI_LOGI("%{public}s, received p2p gcJoin event", __func__);
458 }
459 
OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo & info)460 void WifiP2pCEventCallback::OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info)
461 {
462     WIFI_LOGI("%{public}s, received p2p gcLeave event", __func__);
463 }
464 
AsObject()465 OHOS::sptr<OHOS::IRemoteObject> WifiP2pCEventCallback::AsObject()
466 {
467     return nullptr;
468 }
469 
ConvertP2pLinkedInfo(const OHOS::Wifi::WifiP2pLinkedInfo & linkedInfo)470 WifiP2pLinkedInfo WifiP2pCEventCallback::ConvertP2pLinkedInfo(const OHOS::Wifi::WifiP2pLinkedInfo& linkedInfo)
471 {
472     WifiP2pLinkedInfo info;
473     info.connectState = P2pConnectionState(static_cast<int>(linkedInfo.GetConnectState()));
474     info.isP2pGroupOwner = linkedInfo.IsGroupOwner();
475     OHOS::Wifi::MacStrToArray(linkedInfo.GetGroupOwnerAddress(), info.groupOwnerAddress);
476     return info;
477 }
478 
479 OHOS::sptr<WifiP2pCEventCallback> sptrCallback =
480     OHOS::sptr<WifiP2pCEventCallback>(new (std::nothrow) WifiP2pCEventCallback());
481 
RegisterP2pStateChangedCallback(const P2pStateChangedCallback callback)482 NO_SANITIZE("cfi") WifiErrorCode RegisterP2pStateChangedCallback(const P2pStateChangedCallback callback)
483 {
484     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
485     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
486     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
487     EventManager::GetInstance().Init();
488     sptrCallback->stateChangeCb = callback;
489     std::vector<std::string> event = {EVENT_P2P_STATE_CHANGE};
490     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
491     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_STATE_CHANGE);
492     return WIFI_SUCCESS;
493 }
494 
495 NO_SANITIZE("cfi")
RegisterP2pPersistentGroupsChangedCallback(const P2pPersistentGroupsChangedCallback callback)496 WifiErrorCode RegisterP2pPersistentGroupsChangedCallback(const P2pPersistentGroupsChangedCallback callback)
497 {
498     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
499     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
500     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
501     EventManager::GetInstance().Init();
502     sptrCallback->groupChangeCb = callback;
503     std::vector<std::string> event = {EVENT_P2P_PERSISTENT_GROUP_CHANGE};
504     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
505     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_PERSISTENT_GROUP_CHANGE);
506     return WIFI_SUCCESS;
507 }
508 
RegisterP2pConnectionChangedCallback(const P2pConnectionChangedCallback callback)509 NO_SANITIZE("cfi") WifiErrorCode RegisterP2pConnectionChangedCallback(const P2pConnectionChangedCallback callback)
510 {
511     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
512     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
513     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
514     EventManager::GetInstance().Init();
515     sptrCallback->connectionChangeCb = callback;
516     std::vector<std::string> event = {EVENT_P2P_CONN_STATE_CHANGE};
517     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
518     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_CONN_STATE_CHANGE);
519     return WIFI_SUCCESS;
520 }
521 
RegisterP2pPeersChangedCallback(const P2pPeersChangedCallback callback)522 NO_SANITIZE("cfi") WifiErrorCode RegisterP2pPeersChangedCallback(const P2pPeersChangedCallback callback)
523 {
524     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
525     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
526     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
527     EventManager::GetInstance().Init();
528     sptrCallback->peersChangeCb = callback;
529     std::vector<std::string> event = {EVENT_P2P_PEER_DEVICE_CHANGE};
530     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
531     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_PEER_DEVICE_CHANGE);
532     return WIFI_SUCCESS;
533 }
534 
RegisterP2pPrivatePeersChangedCallback(const P2pPrivatePeersChangedCallback callback)535 NO_SANITIZE("cfi") WifiErrorCode RegisterP2pPrivatePeersChangedCallback(const P2pPrivatePeersChangedCallback callback)
536 {
537     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
538     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
539     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
540     EventManager::GetInstance().Init();
541     sptrCallback->privatepeerChangeCb = callback;
542     std::vector<std::string> event = {EVENT_P2P_PRIVATE_PEER_DEVICE_CHANGE};
543     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
544     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_PRIVATE_PEER_DEVICE_CHANGE);
545     return WIFI_SUCCESS;
546 }
547 
RegisterCfgChangCallback(const WifiCfgChangCallback callback)548 NO_SANITIZE("cfi") WifiErrorCode RegisterCfgChangCallback(const WifiCfgChangCallback callback)
549 {
550     CHECK_PTR_RETURN(callback, ERROR_WIFI_INVALID_ARGS);
551     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
552     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
553     EventManager::GetInstance().Init();
554     sptrCallback->cfgChangeCallback = callback;
555     std::vector<std::string> event = {EVENT_P2P_CONFIG_CHANGE};
556     wifiP2pPtr->RegisterCallBack(sptrCallback, event);
557     EventManager::GetInstance().SetP2PCallbackEvent(sptrCallback, EVENT_P2P_CONFIG_CHANGE);
558     return WIFI_SUCCESS;
559 }
560 
UnregisterCfgChangCallback(void)561 NO_SANITIZE("cfi") WifiErrorCode UnregisterCfgChangCallback(void)
562 {
563     CHECK_PTR_RETURN(sptrCallback, ERROR_WIFI_NOT_AVAILABLE);
564     sptrCallback->cfgChangeCallback = nullptr;
565     EventManager::GetInstance().RemoveP2PCallbackEvent(EVENT_P2P_CONFIG_CHANGE);
566     return WIFI_SUCCESS;
567 }
568 
DiscoverPeers(int32_t channelid)569 NO_SANITIZE("cfi") WifiErrorCode DiscoverPeers(int32_t channelid)
570 {
571     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
572     return GetCErrorCode(wifiP2pPtr->DiscoverPeers(channelid));
573 }
574 
DisableRandomMac(int setmode)575 NO_SANITIZE("cfi") WifiErrorCode DisableRandomMac(int setmode)
576 {
577     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
578     return GetCErrorCode(wifiP2pPtr->DisableRandomMac(setmode));
579 }
580 
CheckCanUseP2p()581 NO_SANITIZE("cfi") WifiErrorCode CheckCanUseP2p()
582 {
583     CHECK_PTR_RETURN(wifiP2pPtr, ERROR_WIFI_NOT_AVAILABLE);
584     return GetCErrorCode(wifiP2pPtr->CheckCanUseP2p());
585 }