• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "wpa_common_cmd.h"
16 #include "wpa_p2p_cmd.h"
17 #include "hdi_wpa_common.h"
18 #include <securec.h>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include <osal_time.h>
22 #include <osal_mem.h>
23 #include <arpa/inet.h>
24 #include "utils/common.h"
25 #include "wpa_supplicant_i.h"
26 #include "main.h"
27 #include "wps_supplicant.h"
28 #include "p2p_supplicant.h"
29 #include "ctrl_iface.h"
30 #include "wpa_magiclink.h"
31 #include "wifi_display.h"
32 #include "bssid_ignore.h"
33 #include "config.h"
34 #include "v1_2/iwpa_callback.h"
35 #include "v1_2/iwpa_interface.h"
36 #include "wpa_p2p_hal.h"
37 
38 #define HEX_TO_DEC_MOVING 4
39 #define DEC_MAX_SCOPE 10
40 #define MIN_MAC_LEN 6
41 
42 struct HdiWpaKeyValue {
43     char key[CMD_SIZE];
44     char value[CMD_SIZE];
45 };
46 
GetStrKeyVal(char * src,const char * split,struct HdiWpaKeyValue * out)47 void GetStrKeyVal(char *src, const char *split, struct HdiWpaKeyValue *out)
48 {
49     if (src == NULL || split == NULL || out == NULL) {
50         return;
51     }
52     char *p = strstr(src, split);
53     if (p == NULL) {
54         if (strcpy_s(out->key, sizeof(out->key), src) != EOK) {
55             HDF_LOGE("%{public}s strcpy failed", __func__);
56         }
57         return;
58     }
59     *p = '\0';
60     if (strcpy_s(out->key, sizeof(out->key), src) != EOK) {
61         HDF_LOGE("%{public}s strcpy failed", __func__);
62     }
63     p += strlen(split);
64     if (strcpy_s(out->value, sizeof(out->value), p) != EOK) {
65         HDF_LOGE("%{public}s strcpy failed", __func__);
66     }
67     return;
68 }
69 
GetHalNetworkInfos(char * buf,struct HdiP2pNetworkInfo * info)70 void GetHalNetworkInfos(char *buf, struct HdiP2pNetworkInfo *info)
71 {
72     if (buf == NULL || info == NULL) {
73         return;
74     }
75     int len = strlen(buf);
76     int start = 0;
77     int end = 0;
78     int i = 0;
79     const int count = 2;
80     while (end < len) {
81         if (buf[end] != '\t') {
82             ++end;
83             continue;
84         }
85         buf[end] = '\0';
86         if (i == 0) {
87             info->id = atoi(buf);
88         } else if (i == 1) {
89             if (strcpy_s((char *)info->ssid, WIFI_SSID_LENGTH + 1, buf + start) != EOK) {
90                 break;
91             }
92             printf_decode((u8 *)info->ssid, WIFI_SSID_LENGTH + 1, (char *)info->ssid);
93         } else if (i == count) {
94             uint8_t tmpBssid[ETH_ADDR_LEN] = {0};
95             hwaddr_aton(buf + start, tmpBssid);
96             if (memcpy_s((char *)info->bssid, ETH_ADDR_LEN, (char *)tmpBssid, ETH_ADDR_LEN) != EOK) {
97                 break;
98             }
99             info->bssid[ETH_ADDR_LEN] = '\0';
100             start = end + 1;
101             if (strcpy_s((char *)info->flags, WIFI_NETWORK_FLAGS_LENGTH + 1, buf + start) != EOK) {
102                 break;
103             }
104             break;
105         }
106         ++i;
107         end++;
108         start = end;
109     }
110     return;
111 }
112 
WpaInterfaceP2pSetSsidPostfixName(struct IWpaInterface * self,const char * ifName,const char * name)113 int32_t WpaInterfaceP2pSetSsidPostfixName(struct IWpaInterface *self, const char *ifName, const char *name)
114 {
115     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
116     (void)self;
117     if (ifName == NULL || name == NULL) {
118         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
119         return HDF_ERR_INVALID_PARAM;
120     }
121     pthread_mutex_lock(GetInterfaceLock());
122     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
123     if (pMainIfc == NULL) {
124         pthread_mutex_unlock(GetInterfaceLock());
125         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
126         return HDF_ERR_INVALID_PARAM;
127     }
128     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetSsidPostfixName(pMainIfc, name);
129     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
130         pthread_mutex_unlock(GetInterfaceLock());
131         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
132         return HDF_FAILURE;
133     }
134     pthread_mutex_unlock(GetInterfaceLock());
135     HDF_LOGI("%{public}s success", __func__);
136     return HDF_SUCCESS;
137 }
138 
WpaInterfaceP2pSetWpsDeviceType(struct IWpaInterface * self,const char * ifName,const char * type)139 int32_t WpaInterfaceP2pSetWpsDeviceType(struct IWpaInterface *self, const char *ifName, const char *type)
140 {
141     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
142     (void)self;
143     if (ifName == NULL || type == NULL) {
144         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
145         return HDF_ERR_INVALID_PARAM;
146     }
147     pthread_mutex_lock(GetInterfaceLock());
148     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
149     if (pMainIfc == NULL) {
150         pthread_mutex_unlock(GetInterfaceLock());
151         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
152         return HDF_ERR_INVALID_PARAM;
153     }
154     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWpsDeviceType(pMainIfc, type);
155     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
156         pthread_mutex_unlock(GetInterfaceLock());
157         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
158         return HDF_FAILURE;
159     }
160     pthread_mutex_unlock(GetInterfaceLock());
161     HDF_LOGI("%{public}s success", __func__);
162     return HDF_SUCCESS;
163 }
164 
WpaInterfaceP2pSetWpsConfigMethods(struct IWpaInterface * self,const char * ifName,const char * methods)165 int32_t WpaInterfaceP2pSetWpsConfigMethods(struct IWpaInterface *self, const char *ifName, const char *methods)
166 {
167     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
168     (void)self;
169     if (ifName == NULL || methods == NULL) {
170         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
171         return HDF_ERR_INVALID_PARAM;
172     }
173     pthread_mutex_lock(GetInterfaceLock());
174     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
175     if (pMainIfc == NULL) {
176         pthread_mutex_unlock(GetInterfaceLock());
177         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
178         return HDF_ERR_INVALID_PARAM;
179     }
180     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWpsConfigMethods(pMainIfc, methods);
181     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
182         pthread_mutex_unlock(GetInterfaceLock());
183         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
184         return HDF_FAILURE;
185     }
186     pthread_mutex_unlock(GetInterfaceLock());
187     HDF_LOGI("%{public}s success", __func__);
188     return HDF_SUCCESS;
189 }
190 
WpaInterfaceP2pSetGroupMaxIdle(struct IWpaInterface * self,const char * ifName,int32_t time)191 int32_t WpaInterfaceP2pSetGroupMaxIdle(struct IWpaInterface *self, const char *ifName, int32_t time)
192 {
193     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
194     (void)self;
195     if (ifName == NULL) {
196         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
197         return HDF_ERR_INVALID_PARAM;
198     }
199     pthread_mutex_lock(GetInterfaceLock());
200     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
201     if (pMainIfc == NULL) {
202         pthread_mutex_unlock(GetInterfaceLock());
203         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
204         return HDF_ERR_INVALID_PARAM;
205     }
206     WifiWpaP2pGroupInterface *pGroupIfc = GetWifiWpaP2pGroupInterface(ifName);
207     if (pGroupIfc == NULL) {
208         pthread_mutex_unlock(GetInterfaceLock());
209         HDF_LOGE("%{public}s: pGroupIfc is null", __func__);
210         return HDF_ERR_INVALID_PARAM;
211     }
212     P2pSupplicantErrCode ret = pGroupIfc->wpaP2pCliCmdSetGroupIdle(pGroupIfc, time);
213     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
214         pthread_mutex_unlock(GetInterfaceLock());
215         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
216         return HDF_FAILURE;
217     }
218     pthread_mutex_unlock(GetInterfaceLock());
219     HDF_LOGI("%{public}s success", __func__);
220     return HDF_SUCCESS;
221 }
222 
WpaInterfaceP2pSetWfdEnable(struct IWpaInterface * self,const char * ifName,int32_t enable)223 int32_t WpaInterfaceP2pSetWfdEnable(struct IWpaInterface *self, const char *ifName, int32_t enable)
224 {
225     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
226     (void)self;
227     (void)ifName;
228     pthread_mutex_lock(GetInterfaceLock());
229     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
230     if (pMainIfc == NULL) {
231         pthread_mutex_unlock(GetInterfaceLock());
232         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
233         return HDF_ERR_INVALID_PARAM;
234     }
235     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWfdEnable(pMainIfc, enable);
236     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
237         pthread_mutex_unlock(GetInterfaceLock());
238         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
239         return HDF_FAILURE;
240     }
241     pthread_mutex_unlock(GetInterfaceLock());
242     HDF_LOGI("%{public}s success", __func__);
243     return HDF_SUCCESS;
244 }
245 
WpaInterfaceP2pSetPersistentReconnect(struct IWpaInterface * self,const char * ifName,int32_t status)246 int32_t WpaInterfaceP2pSetPersistentReconnect(struct IWpaInterface *self, const char *ifName, int32_t status)
247 {
248     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
249     (void)self;
250     (void)ifName;
251     pthread_mutex_lock(GetInterfaceLock());
252     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
253     if (pMainIfc == NULL) {
254         pthread_mutex_unlock(GetInterfaceLock());
255         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
256         return HDF_ERR_INVALID_PARAM;
257     }
258     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetPersistentReconnect(pMainIfc, status);
259     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
260         pthread_mutex_unlock(GetInterfaceLock());
261         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
262         return HDF_FAILURE;
263     }
264     pthread_mutex_unlock(GetInterfaceLock());
265     HDF_LOGI("%{public}s success", __func__);
266     return HDF_SUCCESS;
267 }
268 
WpaInterfaceP2pSetWpsSecondaryDeviceType(struct IWpaInterface * self,const char * ifName,const char * type)269 int32_t WpaInterfaceP2pSetWpsSecondaryDeviceType(struct IWpaInterface *self, const char *ifName, const char *type)
270 {
271     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
272     (void)self;
273     if (ifName == NULL || type == NULL) {
274         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
275         return HDF_ERR_INVALID_PARAM;
276     }
277     pthread_mutex_lock(GetInterfaceLock());
278     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
279     if (pMainIfc == NULL) {
280         pthread_mutex_unlock(GetInterfaceLock());
281         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
282         return HDF_ERR_INVALID_PARAM;
283     }
284     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWpsSecDeviceType(pMainIfc, type);
285     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
286         pthread_mutex_unlock(GetInterfaceLock());
287         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
288         return HDF_FAILURE;
289     }
290     pthread_mutex_unlock(GetInterfaceLock());
291     HDF_LOGI("%{public}s success", __func__);
292     return HDF_SUCCESS;
293 }
294 
WpaInterfaceP2pSetupWpsPbc(struct IWpaInterface * self,const char * ifName,const char * address)295 int32_t WpaInterfaceP2pSetupWpsPbc(struct IWpaInterface *self, const char *ifName, const char *address)
296 {
297     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
298     (void)self;
299     if (ifName == NULL || address == NULL) {
300         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
301         return HDF_ERR_INVALID_PARAM;
302     }
303     pthread_mutex_lock(GetInterfaceLock());
304     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
305     if (pMainIfc == NULL) {
306         pthread_mutex_unlock(GetInterfaceLock());
307         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
308         return HDF_ERR_INVALID_PARAM;
309     }
310     WifiWpaP2pGroupInterface *pGroupIfc = GetWifiWpaP2pGroupInterface(ifName);
311     if (pGroupIfc == NULL) {
312         pthread_mutex_unlock(GetInterfaceLock());
313         HDF_LOGE("%{public}s: pGroupIfc is null", __func__);
314         return HDF_ERR_INVALID_PARAM;
315     }
316     P2pSupplicantErrCode ret = pGroupIfc->wpaP2pCliCmdWpsPbc(pGroupIfc, address);
317     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
318         pthread_mutex_unlock(GetInterfaceLock());
319         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
320         return HDF_FAILURE;
321     }
322     pthread_mutex_unlock(GetInterfaceLock());
323     HDF_LOGI("%{public}s success", __func__);
324     return HDF_SUCCESS;
325 }
326 
WpaInterfaceP2pSetupWpsPin(struct IWpaInterface * self,const char * ifName,const char * address,const char * pin,char * result,uint32_t resultLen)327 int32_t WpaInterfaceP2pSetupWpsPin(struct IWpaInterface *self, const char *ifName, const char *address,
328     const char *pin, char *result, uint32_t resultLen)
329 {
330     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
331     (void)self;
332     if (ifName == NULL || address == NULL || pin == NULL || result == NULL || resultLen == 0) {
333         HDF_LOGE("%{public}s groupIfc, address, pin and result have NULL", __func__);
334         return HDF_FAILURE;
335     }
336 
337     P2pWpsPinDisplayArgv p2pWpsPinDisplay = {0};
338     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
339     WifiWpaP2pGroupInterface *pGroupIfc = GetWifiWpaP2pGroupInterface(ifName);
340     if (pMainIfc == NULL || pGroupIfc == NULL) {
341         HDF_LOGE("%{public}s: pMainIfc or pGroupIfc is null", __func__);
342         return HDF_ERR_INVALID_PARAM;
343     }
344     P2pSupplicantErrCode ret = pGroupIfc->wpaP2pCliCmdWpsPin(pGroupIfc, &p2pWpsPinDisplay);
345     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
346         HDF_LOGE("WpaP2pCliCmdWpsPin fail, ret = %{public}d", ret);
347         return HDF_FAILURE;
348     }
349     if (strlen(pin) > 0) {
350         p2pWpsPinDisplay.mode = P2P_PIN_KEYPAD;
351         if (strncpy_s(p2pWpsPinDisplay.pinCode, sizeof(p2pWpsPinDisplay.pinCode), pin, strlen(pin)) != EOK) {
352             HDF_LOGE("%{public}s: Failed to init pin code, the input pin code may be invalid!", __func__);
353             return HDF_FAILURE;
354         }
355     } else {
356         p2pWpsPinDisplay.mode = P2P_PIN_DISPLAY;
357         if ((strncpy_s(p2pWpsPinDisplay.bssid, sizeof(p2pWpsPinDisplay.bssid), address, strlen(address)) != EOK) ||
358             (strncpy_s(result, resultLen, p2pWpsPinDisplay.pinCode, strlen(p2pWpsPinDisplay.pinCode)) != EOK)) {
359             HDF_LOGE("%{public}s: Failed to init request message, the input message may be invalid!", __func__);
360             return HDF_FAILURE;
361         }
362     }
363     HDF_LOGI("%{public}s success", __func__);
364     return HDF_SUCCESS;
365 }
366 
WpaInterfaceP2pSetPowerSave(struct IWpaInterface * self,const char * ifName,int32_t enable)367 int32_t WpaInterfaceP2pSetPowerSave(struct IWpaInterface *self, const char *ifName, int32_t enable)
368 {
369     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
370     (void)self;
371     if (ifName == NULL) {
372         HDF_LOGE("P2pSetPowerSave, groupIfc is NULL");
373         return HDF_FAILURE;
374     }
375     pthread_mutex_lock(GetInterfaceLock());
376     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
377     if (pMainIfc == NULL) {
378         pthread_mutex_unlock(GetInterfaceLock());
379         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
380         return HDF_ERR_INVALID_PARAM;
381     }
382     WifiWpaP2pGroupInterface *pGroupIfc = GetWifiWpaP2pGroupInterface(ifName);
383     if (pGroupIfc == NULL) {
384         pthread_mutex_unlock(GetInterfaceLock());
385         HDF_LOGE("%{public}s: pGroupIfc is null", __func__);
386         return HDF_ERR_INVALID_PARAM;
387     }
388     P2pSupplicantErrCode ret = pGroupIfc->wpaP2pCliCmdSetPowerSave(pGroupIfc, enable);
389     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
390         pthread_mutex_unlock(GetInterfaceLock());
391         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
392         return HDF_FAILURE;
393     }
394     pthread_mutex_unlock(GetInterfaceLock());
395     HDF_LOGI("%{public}s success", __func__);
396     return HDF_SUCCESS;
397 }
398 
WpaInterfaceP2pSetDeviceName(struct IWpaInterface * self,const char * ifName,const char * name)399 int32_t WpaInterfaceP2pSetDeviceName(struct IWpaInterface *self, const char *ifName, const char *name)
400 {
401     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
402     (void)self;
403     if (ifName == NULL || name == NULL) {
404         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
405         return HDF_ERR_INVALID_PARAM;
406     }
407     pthread_mutex_lock(GetInterfaceLock());
408     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
409     if (pMainIfc == NULL) {
410         pthread_mutex_unlock(GetInterfaceLock());
411         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
412         return HDF_ERR_INVALID_PARAM;
413     }
414     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWpsName(pMainIfc, name);
415     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
416         pthread_mutex_unlock(GetInterfaceLock());
417         HDF_LOGE("%{public}s fail, ret = %{public}d", __func__, ret);
418         return HDF_FAILURE;
419     }
420     pthread_mutex_unlock(GetInterfaceLock());
421     HDF_LOGI("%{public}s success", __func__);
422     return HDF_SUCCESS;
423 }
424 
WpaInterfaceP2pSetWfdDeviceConfig(struct IWpaInterface * self,const char * ifName,const char * config)425 int32_t WpaInterfaceP2pSetWfdDeviceConfig(struct IWpaInterface *self, const char *ifName, const char *config)
426 {
427     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
428     (void)self;
429     if (ifName == NULL || config == NULL) {
430         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
431         return HDF_ERR_INVALID_PARAM;
432     }
433     pthread_mutex_lock(GetInterfaceLock());
434     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
435     if (pMainIfc == NULL) {
436         pthread_mutex_unlock(GetInterfaceLock());
437         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
438         return HDF_ERR_INVALID_PARAM;
439     }
440     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetWfdDeviceInfo(pMainIfc, config);
441     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
442         pthread_mutex_unlock(GetInterfaceLock());
443         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
444         return HDF_FAILURE;
445     }
446     pthread_mutex_unlock(GetInterfaceLock());
447     HDF_LOGI("%{public}s success", __func__);
448     return HDF_SUCCESS;
449 }
450 
WpaInterfaceP2pSetRandomMac(struct IWpaInterface * self,const char * ifName,int32_t networkId)451 int32_t WpaInterfaceP2pSetRandomMac(struct IWpaInterface *self, const char *ifName, int32_t networkId)
452 {
453     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
454     (void)self;
455     (void)ifName;
456     pthread_mutex_lock(GetInterfaceLock());
457     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
458     if (pMainIfc == NULL) {
459         pthread_mutex_unlock(GetInterfaceLock());
460         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
461         return HDF_ERR_INVALID_PARAM;
462     }
463     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetRandomMac(pMainIfc, networkId);
464     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
465         pthread_mutex_unlock(GetInterfaceLock());
466         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
467         return HDF_FAILURE;
468     }
469     pthread_mutex_unlock(GetInterfaceLock());
470     HDF_LOGI("%{public}s success", __func__);
471     return HDF_SUCCESS;
472 }
473 
WpaInterfaceP2pStartFind(struct IWpaInterface * self,const char * ifName,int32_t timeout)474 int32_t WpaInterfaceP2pStartFind(struct IWpaInterface *self, const char *ifName, int32_t timeout)
475 {
476     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
477     (void)self;
478     (void)ifName;
479     pthread_mutex_lock(GetInterfaceLock());
480     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
481     if (pMainIfc == NULL) {
482         pthread_mutex_unlock(GetInterfaceLock());
483         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
484         return HDF_ERR_INVALID_PARAM;
485     }
486     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdP2pFound(pMainIfc, timeout);
487     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
488         pthread_mutex_unlock(GetInterfaceLock());
489         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
490         return HDF_FAILURE;
491     }
492     pthread_mutex_unlock(GetInterfaceLock());
493     HDF_LOGI("%{public}s success", __func__);
494     return HDF_SUCCESS;
495 }
496 
WpaInterfaceP2pSetExtListen(struct IWpaInterface * self,const char * ifName,int32_t enable,int32_t period,int32_t interval)497 int32_t WpaInterfaceP2pSetExtListen(struct IWpaInterface *self, const char *ifName, int32_t enable,
498     int32_t period, int32_t interval)
499 {
500     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
501     (void)self;
502     (void)ifName;
503     pthread_mutex_lock(GetInterfaceLock());
504     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
505     if (pMainIfc == NULL) {
506         pthread_mutex_unlock(GetInterfaceLock());
507         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
508         return HDF_ERR_INVALID_PARAM;
509     }
510     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdExtListen(pMainIfc, enable, period, interval);
511     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
512         pthread_mutex_unlock(GetInterfaceLock());
513         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
514         return HDF_FAILURE;
515     }
516     pthread_mutex_unlock(GetInterfaceLock());
517     HDF_LOGI("%{public}s success", __func__);
518     return HDF_SUCCESS;
519 }
520 
WpaInterfaceP2pSetListenChannel(struct IWpaInterface * self,const char * ifName,int32_t channel,int32_t regClass)521 int32_t WpaInterfaceP2pSetListenChannel(struct IWpaInterface *self, const char *ifName,
522     int32_t channel, int32_t regClass)
523 {
524     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
525     (void)self;
526     (void)ifName;
527     pthread_mutex_lock(GetInterfaceLock());
528     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
529     if (pMainIfc == NULL) {
530         pthread_mutex_unlock(GetInterfaceLock());
531         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
532         return HDF_ERR_INVALID_PARAM;
533     }
534     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetListenChannel(pMainIfc, channel, regClass);
535     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
536         pthread_mutex_unlock(GetInterfaceLock());
537         HDF_LOGE("%{public}s fail, ret = %{public}d", __func__, ret);
538         return HDF_FAILURE;
539     }
540     pthread_mutex_unlock(GetInterfaceLock());
541     HDF_LOGI("%{public}s success", __func__);
542     return HDF_SUCCESS;
543 }
544 
WpaInterfaceP2pProvisionDiscovery(struct IWpaInterface * self,const char * ifName,const char * peerBssid,int32_t mode)545 int32_t WpaInterfaceP2pProvisionDiscovery(struct IWpaInterface *self, const char *ifName,
546     const char *peerBssid, int32_t mode)
547 {
548     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
549     (void)self;
550     if (ifName == NULL || peerBssid == NULL) {
551         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
552         return HDF_ERR_INVALID_PARAM;
553     }
554     pthread_mutex_lock(GetInterfaceLock());
555     P2pProvisionDiscoveryArgv p2pProvision;
556     if (memset_s(&p2pProvision, sizeof(p2pProvision), 0, sizeof(p2pProvision)) != EOK ||
557         strncpy_s(p2pProvision.peerbssid, sizeof(p2pProvision.peerbssid), peerBssid, strlen(peerBssid)) != EOK) {
558         pthread_mutex_unlock(GetInterfaceLock());
559         HDF_LOGE("Failed to init request message, the input message may be invalid!");
560         return HDF_FAILURE;
561     }
562     p2pProvision.mode = mode;
563     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
564     if (pMainIfc == NULL) {
565         pthread_mutex_unlock(GetInterfaceLock());
566         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
567         return HDF_ERR_INVALID_PARAM;
568     }
569     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdProvisionDiscovery(pMainIfc, &p2pProvision);
570     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
571         pthread_mutex_unlock(GetInterfaceLock());
572         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
573         return HDF_FAILURE;
574     }
575     pthread_mutex_unlock(GetInterfaceLock());
576     HDF_LOGI("%{public}s success", __func__);
577     return HDF_SUCCESS;
578 }
579 
WpaInterfaceP2pAddGroup(struct IWpaInterface * self,const char * ifName,int32_t isPersistent,int32_t networkId,int32_t freq)580 int32_t WpaInterfaceP2pAddGroup(struct IWpaInterface *self, const char *ifName, int32_t isPersistent,
581     int32_t networkId, int32_t freq)
582 {
583     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
584     (void)self;
585     (void)ifName;
586     pthread_mutex_lock(GetInterfaceLock());
587     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
588     if (pMainIfc == NULL) {
589         pthread_mutex_unlock(GetInterfaceLock());
590         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
591         return HDF_ERR_INVALID_PARAM;
592     }
593     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdGroupAdd(pMainIfc, isPersistent, networkId, freq);
594     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
595         pthread_mutex_unlock(GetInterfaceLock());
596         HDF_LOGE("%{public}s fail, ret = %{public}d", __func__, ret);
597         return HDF_FAILURE;
598     }
599     pthread_mutex_unlock(GetInterfaceLock());
600     HDF_LOGI("%{public}s success", __func__);
601     return HDF_SUCCESS;
602 }
603 
WpaInterfaceP2pAddService(struct IWpaInterface * self,const char * ifName,const struct HdiP2pServiceInfo * info)604 int32_t WpaInterfaceP2pAddService(struct IWpaInterface *self, const char *ifName,
605     const struct HdiP2pServiceInfo *info)
606 {
607     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
608     (void)self;
609     if (ifName == NULL || info == NULL) {
610         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
611         return HDF_ERR_INVALID_PARAM;
612     }
613     pthread_mutex_lock(GetInterfaceLock());
614     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
615     if (pMainIfc == NULL) {
616         pthread_mutex_unlock(GetInterfaceLock());
617         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
618         return HDF_ERR_INVALID_PARAM;
619     }
620     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdServiceAdd(pMainIfc, info);
621     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
622         pthread_mutex_unlock(GetInterfaceLock());
623         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
624         return HDF_FAILURE;
625     }
626     pthread_mutex_unlock(GetInterfaceLock());
627     HDF_LOGI("%{public}s success", __func__);
628     return HDF_SUCCESS;
629 }
630 
WpaInterfaceP2pRemoveService(struct IWpaInterface * self,const char * ifName,const struct HdiP2pServiceInfo * info)631 int32_t WpaInterfaceP2pRemoveService(struct IWpaInterface *self, const char *ifName,
632     const struct HdiP2pServiceInfo *info)
633 {
634     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
635     (void)self;
636     if (ifName == NULL || info == NULL) {
637         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
638         return HDF_ERR_INVALID_PARAM;
639     }
640     pthread_mutex_lock(GetInterfaceLock());
641     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
642     if (pMainIfc == NULL) {
643         pthread_mutex_unlock(GetInterfaceLock());
644         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
645         return HDF_ERR_INVALID_PARAM;
646     }
647     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdServiceDel(pMainIfc, info);
648     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
649         pthread_mutex_unlock(GetInterfaceLock());
650         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
651         return HDF_FAILURE;
652     }
653     pthread_mutex_unlock(GetInterfaceLock());
654     HDF_LOGI("%{public}s success", __func__);
655     return HDF_SUCCESS;
656 }
657 
WpaInterfaceP2pStopFind(struct IWpaInterface * self,const char * ifName)658 int32_t WpaInterfaceP2pStopFind(struct IWpaInterface *self, const char *ifName)
659 {
660     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
661     (void)self;
662     (void)ifName;
663     pthread_mutex_lock(GetInterfaceLock());
664     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
665     if (pMainIfc == NULL) {
666         pthread_mutex_unlock(GetInterfaceLock());
667         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
668         return HDF_ERR_INVALID_PARAM;
669     }
670     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdP2pStopFind(pMainIfc);
671     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
672         pthread_mutex_unlock(GetInterfaceLock());
673         HDF_LOGE("%{public}s fail, ret = %{public}d", __func__, ret);
674         return HDF_FAILURE;
675     }
676     pthread_mutex_unlock(GetInterfaceLock());
677     HDF_LOGI("%{public}s success", __func__);
678     return HDF_SUCCESS;
679 }
680 
WpaInterfaceP2pFlush(struct IWpaInterface * self,const char * ifName)681 int32_t WpaInterfaceP2pFlush(struct IWpaInterface *self, const char *ifName)
682 {
683     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
684     (void)self;
685     (void)ifName;
686     pthread_mutex_lock(GetInterfaceLock());
687     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
688     if (pMainIfc == NULL) {
689         pthread_mutex_unlock(GetInterfaceLock());
690         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
691         return HDF_ERR_INVALID_PARAM;
692     }
693     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdFlush(pMainIfc);
694     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
695         pthread_mutex_unlock(GetInterfaceLock());
696         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
697         return HDF_FAILURE;
698     }
699     pthread_mutex_unlock(GetInterfaceLock());
700     HDF_LOGI("%{public}s success", __func__);
701     return HDF_SUCCESS;
702 }
703 
WpaInterfaceP2pFlushService(struct IWpaInterface * self,const char * ifName)704 int32_t WpaInterfaceP2pFlushService(struct IWpaInterface *self, const char *ifName)
705 {
706     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
707     (void)self;
708     (void)ifName;
709     pthread_mutex_lock(GetInterfaceLock());
710     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
711     if (pMainIfc == NULL) {
712         pthread_mutex_unlock(GetInterfaceLock());
713         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
714         return HDF_ERR_INVALID_PARAM;
715     }
716     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdFlushService(pMainIfc);
717     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
718         pthread_mutex_unlock(GetInterfaceLock());
719         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
720         return HDF_FAILURE;
721     }
722     pthread_mutex_unlock(GetInterfaceLock());
723     HDF_LOGI("%{public}s success", __func__);
724     return HDF_SUCCESS;
725 }
726 
WpaInterfaceP2pRemoveNetwork(struct IWpaInterface * self,const char * ifName,int32_t networkId)727 int32_t WpaInterfaceP2pRemoveNetwork(struct IWpaInterface *self, const char *ifName, int32_t networkId)
728 {
729     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
730     (void)self;
731     (void)ifName;
732     pthread_mutex_lock(GetInterfaceLock());
733     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
734     if (pMainIfc == NULL) {
735         pthread_mutex_unlock(GetInterfaceLock());
736         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
737         return HDF_ERR_INVALID_PARAM;
738     }
739     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdRemoveNetwork(pMainIfc, networkId);
740     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
741         pthread_mutex_unlock(GetInterfaceLock());
742         HDF_LOGE("%{public}s fail, ret = %{public}d", __func__, ret);
743         return HDF_FAILURE;
744     }
745     pthread_mutex_unlock(GetInterfaceLock());
746     HDF_LOGI("%{public}s success", __func__);
747     return HDF_SUCCESS;
748 }
749 
WpaInterfaceP2pSetGroupConfig(struct IWpaInterface * self,const char * ifName,const int32_t networkId,const char * name,const char * value)750 int32_t WpaInterfaceP2pSetGroupConfig(struct IWpaInterface *self, const char *ifName, const int32_t networkId,
751     const char *name, const char *value)
752 {
753     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
754     char cmd[CMD_SIZE] = {0};
755     char buf[CMD_SIZE] = {0};
756     int32_t ret = 0;
757     (void)self;
758     if (ifName == NULL || name == NULL || value == NULL) {
759         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
760         return HDF_ERR_INVALID_PARAM;
761     }
762     pthread_mutex_lock(GetInterfaceLock());
763     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET_NETWORK %d %s %s",
764         ifName, networkId, name, value);
765     if (ret < 0) {
766         pthread_mutex_unlock(GetInterfaceLock());
767         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
768         return HDF_FAILURE;
769     }
770     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
771         pthread_mutex_unlock(GetInterfaceLock());
772         HDF_LOGE("%{public}s command failed!", __func__);
773         return HDF_FAILURE;
774     }
775     pthread_mutex_unlock(GetInterfaceLock());
776     HDF_LOGI("%{public}s success", __func__);
777     return HDF_SUCCESS;
778 }
779 
WpaInterfaceP2pInvite(struct IWpaInterface * self,const char * ifName,const char * peerBssid,const char * goBssid)780 int32_t WpaInterfaceP2pInvite(struct IWpaInterface *self, const char *ifName,
781     const char *peerBssid, const char *goBssid)
782 {
783     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
784     (void)self;
785     if (peerBssid == NULL || goBssid == NULL || ifName == NULL) {
786         HDF_LOGE("%{public}s: peerBssid, goBssid and ifname have NULL", __func__);
787         return HDF_FAILURE;
788     }
789     pthread_mutex_lock(GetInterfaceLock());
790     P2pHalInviteArgv p2pHalInvite;
791     if (memset_s(&p2pHalInvite, sizeof(p2pHalInvite), 0, sizeof(p2pHalInvite)) != EOK ||
792         strncpy_s(p2pHalInvite.peerbssid, sizeof(p2pHalInvite.peerbssid), peerBssid, strlen(peerBssid)) != EOK ||
793         strncpy_s(p2pHalInvite.gobssid, sizeof(p2pHalInvite.gobssid), goBssid, strlen(goBssid)) != EOK ||
794         strncpy_s(p2pHalInvite.ifname, sizeof(p2pHalInvite.ifname), ifName, strlen(ifName)) != EOK) {
795         pthread_mutex_unlock(GetInterfaceLock());
796         HDF_LOGE("Failed to init request message, the input message may be invalid!");
797         return HDF_FAILURE;
798     }
799     p2pHalInvite.persistent = 0;
800     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
801     if (pMainIfc == NULL) {
802         pthread_mutex_unlock(GetInterfaceLock());
803         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
804         return HDF_ERR_INVALID_PARAM;
805     }
806     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdInvite(pMainIfc, &p2pHalInvite);
807     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
808         pthread_mutex_unlock(GetInterfaceLock());
809         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
810         return HDF_FAILURE;
811     }
812     pthread_mutex_unlock(GetInterfaceLock());
813     HDF_LOGI("%{public}s success", __func__);
814     return HDF_SUCCESS;
815 }
816 
WpaInterfaceP2pReinvoke(struct IWpaInterface * self,const char * ifName,const int32_t networkId,const char * bssid)817 int32_t WpaInterfaceP2pReinvoke(struct IWpaInterface *self, const char *ifName, const int32_t networkId,
818     const char *bssid)
819 {
820     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
821     (void)self;
822     if (ifName == NULL || bssid == NULL) {
823         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
824         return HDF_ERR_INVALID_PARAM;
825     }
826     pthread_mutex_lock(GetInterfaceLock());
827     P2pHalReInviteArgv p2pHalReInvite;
828     if (memset_s(&p2pHalReInvite, sizeof(p2pHalReInvite), 0, sizeof(p2pHalReInvite)) != EOK ||
829         strncpy_s(p2pHalReInvite.peerbssid, sizeof(p2pHalReInvite.peerbssid), bssid, strlen(bssid)) != EOK) {
830         pthread_mutex_unlock(GetInterfaceLock());
831         HDF_LOGE("Failed to init request message, the input message may be invalid!");
832         return HDF_FAILURE;
833     }
834     p2pHalReInvite.networkId = networkId;
835     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
836     if (pMainIfc == NULL) {
837         pthread_mutex_unlock(GetInterfaceLock());
838         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
839         return HDF_ERR_INVALID_PARAM;
840     }
841     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdReInvite(pMainIfc, &p2pHalReInvite);
842     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
843         pthread_mutex_unlock(GetInterfaceLock());
844         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
845         return HDF_FAILURE;
846     }
847     pthread_mutex_unlock(GetInterfaceLock());
848     HDF_LOGI("%{public}s success", __func__);
849     return HDF_SUCCESS;
850 }
851 
WpaInterfaceP2pGetDeviceAddress(struct IWpaInterface * self,const char * ifName,char * deviceAddress,uint32_t deviceAddressLen)852 int32_t WpaInterfaceP2pGetDeviceAddress(struct IWpaInterface *self, const char *ifName, char *deviceAddress,
853     uint32_t deviceAddressLen)
854 {
855     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
856     if (ifName == NULL || deviceAddress == NULL) {
857         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
858         return HDF_ERR_INVALID_PARAM;
859     }
860     (void)self;
861     pthread_mutex_lock(GetInterfaceLock());
862     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
863     if (pMainIfc == NULL) {
864         pthread_mutex_unlock(GetInterfaceLock());
865         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
866         return HDF_ERR_INVALID_PARAM;
867     }
868     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdGetDeviceAddress(pMainIfc, deviceAddress, deviceAddressLen);
869     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
870         pthread_mutex_unlock(GetInterfaceLock());
871         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
872         return HDF_FAILURE;
873     }
874     pthread_mutex_unlock(GetInterfaceLock());
875     HDF_LOGI("%{public}s success", __func__);
876     return HDF_SUCCESS;
877 }
878 
WpaInterfaceP2pReqServiceDiscovery(struct IWpaInterface * self,const char * ifName,const struct HdiP2pReqService * reqService,char * replyDisc,uint32_t replyDiscLen)879 int32_t WpaInterfaceP2pReqServiceDiscovery(struct IWpaInterface *self, const char *ifName,
880     const struct HdiP2pReqService *reqService, char *replyDisc, uint32_t replyDiscLen)
881 {
882     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
883     if (ifName == NULL || reqService == NULL || replyDisc == NULL) {
884         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
885         return HDF_ERR_INVALID_PARAM;
886     }
887     (void)self;
888     char seq[WIFI_P2P_SERVER_DISCOVERY_SEQUENCE_LENGTH] = {0};
889     pthread_mutex_lock(GetInterfaceLock());
890     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
891     if (pMainIfc == NULL) {
892         pthread_mutex_unlock(GetInterfaceLock());
893         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
894         return HDF_ERR_INVALID_PARAM;
895     }
896     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdServDiscReq(pMainIfc, (char *)reqService->bssid,
897         (char *)reqService->msg, seq, sizeof(seq));
898     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
899         pthread_mutex_unlock(GetInterfaceLock());
900         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
901         return HDF_FAILURE;
902     }
903     if (strncpy_s(replyDisc, replyDiscLen, seq, strlen(seq)) != EOK) {
904         pthread_mutex_unlock(GetInterfaceLock());
905         HDF_LOGE("%{public}s: fail", __func__);
906         return HDF_FAILURE;
907     }
908     pthread_mutex_unlock(GetInterfaceLock());
909     HDF_LOGI("%{public}s success", __func__);
910     return HDF_SUCCESS;
911 }
912 
WpaInterfaceP2pCancelServiceDiscovery(struct IWpaInterface * self,const char * ifName,const char * id)913 int32_t WpaInterfaceP2pCancelServiceDiscovery(struct IWpaInterface *self, const char *ifName, const char *id)
914 {
915     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
916     (void)self;
917     if (ifName == NULL || id == NULL) {
918         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
919         return HDF_ERR_INVALID_PARAM;
920     }
921     pthread_mutex_lock(GetInterfaceLock());
922     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
923     if (pMainIfc == NULL) {
924         pthread_mutex_unlock(GetInterfaceLock());
925         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
926         return HDF_ERR_INVALID_PARAM;
927     }
928     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdServDiscCancelReq(pMainIfc, id);
929     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
930         pthread_mutex_unlock(GetInterfaceLock());
931         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
932         return HDF_FAILURE;
933     }
934     pthread_mutex_unlock(GetInterfaceLock());
935     HDF_LOGI("%{public}s success", __func__);
936     return HDF_SUCCESS;
937 }
938 
WpaInterfaceP2pRespServerDiscovery(struct IWpaInterface * self,const char * ifName,const struct HdiP2pServDiscReqInfo * info)939 int32_t WpaInterfaceP2pRespServerDiscovery(struct IWpaInterface *self, const char *ifName,
940     const struct HdiP2pServDiscReqInfo *info)
941 {
942     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
943     if (ifName == NULL || info == NULL) {
944         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
945         return HDF_ERR_INVALID_PARAM;
946     }
947     (void)self;
948     pthread_mutex_lock(GetInterfaceLock());
949     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
950     if (pMainIfc == NULL) {
951         pthread_mutex_unlock(GetInterfaceLock());
952         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
953         return HDF_ERR_INVALID_PARAM;
954     }
955     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdRespServerDiscovery(pMainIfc, info);
956     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
957         pthread_mutex_unlock(GetInterfaceLock());
958         HDF_LOGE("WpaP2pCliCmdRespServerDiscovery fail, ret = %{public}d", ret);
959         return HDF_FAILURE;
960     }
961     pthread_mutex_unlock(GetInterfaceLock());
962     HDF_LOGI("%{public}s success", __func__);
963     return HDF_SUCCESS;
964 }
965 
WpaInterfaceP2pConnect(struct IWpaInterface * self,const char * ifName,const struct HdiP2pConnectInfo * info,char * replyPin,uint32_t replyPinLen)966 int32_t WpaInterfaceP2pConnect(struct IWpaInterface *self, const char *ifName, const struct HdiP2pConnectInfo *info,
967     char *replyPin, uint32_t replyPinLen)
968 {
969     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
970     if (ifName == NULL || info == NULL || replyPin == NULL) {
971         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
972         return HDF_ERR_INVALID_PARAM;
973     }
974     pthread_mutex_lock(GetInterfaceLock());
975     char *reply;
976     const int replySize = REPLY_SIZE;
977     char cmd[CMD_SIZE] = {0};
978     char join[CMD_SIZE] = {0};
979     char mode[CMD_SIZE] = {0};
980     char pin[CMD_SIZE] = {0};
981     char peerDevAddr[CMD_SIZE] = {0};
982     if (memcpy_s(pin, CMD_SIZE, info->pin, info->pinLen) != EOK) {
983         pthread_mutex_unlock(GetInterfaceLock());
984         HDF_LOGE("%{public}s strcpy failed", __func__);
985         return HDF_FAILURE;
986     }
987 
988     if (memcpy_s(peerDevAddr, CMD_SIZE, info->peerDevAddr, info->peerDevAddrLen) != EOK) {
989         pthread_mutex_unlock(GetInterfaceLock());
990         HDF_LOGE("%{public}s strcpy failed", __func__);
991         return HDF_FAILURE;
992     }
993 
994     reply = (char *)malloc(replySize);
995     if (reply == NULL) {
996         pthread_mutex_unlock(GetInterfaceLock());
997         HDF_LOGE("%{public}s reply is NULL!", __func__);
998         return HDF_FAILURE;
999     }
1000 
1001     int32_t ret = 0;
1002     (void)self;
1003 
1004     if (info->mode != 0) {
1005         if (strcpy_s(join, sizeof(join), " join") != EOK) {
1006             HDF_LOGE("%{public}s strcpy failed", __func__);
1007         }
1008     } else {
1009         if (snprintf_s(join, sizeof(join), sizeof(join) - 1, " go_intent=%d", info->goIntent) < 0) {
1010             pthread_mutex_unlock(GetInterfaceLock());
1011             HDF_LOGE("%{public}s input parameter invalid!", __func__);
1012             free(reply);
1013             return HDF_ERR_INVALID_PARAM;
1014         }
1015     }
1016 
1017     if (info->provdisc == P2P_WPS_METHOD_DISPLAY) {
1018         if (strcpy_s(mode, sizeof(mode), " display") != EOK) {
1019             HDF_LOGE("%{public}s strcpy failed", __func__);
1020         }
1021     } else if (info->provdisc == P2P_WPS_METHOD_KEYPAD) {
1022         if (strcpy_s(mode, sizeof(mode), " keypad") != EOK) {
1023             HDF_LOGE("%{public}s strcpy failed", __func__);
1024         }
1025     } else if (info->provdisc == P2P_WPS_METHOD_PBC && info->pin != NULL && strlen((char *)info->pin) == 0) {
1026         if (strcpy_s(pin, CMD_SIZE, "pbc") != EOK) {
1027             HDF_LOGE("%{public}s strcpy failed", __func__);
1028         }
1029     } else {
1030         pthread_mutex_unlock(GetInterfaceLock());
1031         HDF_LOGE("%{public}s Mode value is invalid %{public}d!", __func__, info->provdisc);
1032         free(reply);
1033         return HDF_ERR_INVALID_PARAM;
1034     }
1035 
1036     if (info->peerDevAddr) {
1037         ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s P2P_CONNECT %s %s%s persistent=%d %s", ifName,
1038             MacToStr(info->peerDevAddr), pin, mode, info->persistent, join);
1039     }
1040     if (ret < 0) {
1041         pthread_mutex_unlock(GetInterfaceLock());
1042         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
1043         free(reply);
1044         return HDF_FAILURE;
1045     }
1046 
1047     if (WpaCliCmd(cmd, reply, REPLY_SIZE) != 0) {
1048         pthread_mutex_unlock(GetInterfaceLock());
1049         HDF_LOGE("P2P_CONNECT command failed!");
1050         free(reply);
1051         return HDF_FAILURE;
1052     }
1053 
1054     if (strncmp(reply, "FAIL", strlen("FAIL")) == 0) {
1055         pthread_mutex_unlock(GetInterfaceLock());
1056         HDF_LOGE("%{public}s P2p connect return %{public}s", __func__, reply);
1057         free(reply);
1058         return HDF_FAILURE;
1059     }
1060     if (info->provdisc == P2P_WPS_METHOD_DISPLAY && strcmp((char *)info->pin, "pin") == 0) {
1061         if (strncpy_s(replyPin, replyPinLen, reply, strlen(reply)) != 0) {
1062             pthread_mutex_unlock(GetInterfaceLock());
1063             HDF_LOGE("%{public}s Failed to copy response pin code info!", __func__);
1064             free(reply);
1065             return HDF_FAILURE;
1066         }
1067     }
1068     pthread_mutex_unlock(GetInterfaceLock());
1069     free(reply);
1070     HDF_LOGI("%{public}s success", __func__);
1071     return HDF_SUCCESS;
1072 }
1073 
WpaInterfaceP2pHid2dConnect(struct IWpaInterface * self,const char * ifName,const struct HdiHid2dConnectInfo * info)1074 int32_t WpaInterfaceP2pHid2dConnect(struct IWpaInterface *self, const char *ifName,
1075     const struct HdiHid2dConnectInfo *info)
1076 {
1077     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1078     if (ifName == NULL || info == NULL) {
1079         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1080         return HDF_ERR_INVALID_PARAM;
1081     }
1082     pthread_mutex_lock(GetInterfaceLock());
1083     char cmd[CMD_SIZE];
1084     char buf[CMD_SIZE];
1085     (void)self;
1086     int freq = (info->frequency >> 16);
1087     int isLegacyGo = (info->frequency & 0xffff);
1088     if (freq < 0) {
1089         HDF_LOGE("hid2dconnect freq is failed, freq=%{public}d", freq);
1090         freq = 0;
1091     }
1092     HDF_LOGI("hid2dconnect freq=%{public}d, isLegacyGo=%{public}d", freq, isLegacyGo);
1093     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s MAGICLINK \"%s\"\n%s\n\"%s\"\n%d\n%d", ifName,
1094             (char *)info->ssid, MacToStr(info->bssid), (char *)info->passphrase, freq, isLegacyGo) < 0) {
1095         pthread_mutex_unlock(GetInterfaceLock());
1096         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s.", __func__, cmd);
1097         return HDF_FAILURE;
1098     }
1099     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
1100         pthread_mutex_unlock(GetInterfaceLock());
1101         HDF_LOGE("hid2d_connect command failed!");
1102         return HDF_FAILURE;
1103     }
1104     if (strncmp(buf, "FAIL", strlen("FAIL")) == 0) {
1105         pthread_mutex_unlock(GetInterfaceLock());
1106         HDF_LOGE("%{public}s: return %{public}s", __func__, buf);
1107         return HDF_FAILURE;
1108     }
1109     pthread_mutex_unlock(GetInterfaceLock());
1110     HDF_LOGI("%{public}s success", __func__);
1111     return HDF_SUCCESS;
1112 }
1113 
WpaInterfaceP2pSetServDiscExternal(struct IWpaInterface * self,const char * ifName,int32_t mode)1114 int32_t WpaInterfaceP2pSetServDiscExternal(struct IWpaInterface *self, const char *ifName, int32_t mode)
1115 {
1116     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1117     (void)self;
1118     (void)ifName;
1119     pthread_mutex_lock(GetInterfaceLock());
1120     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
1121     if (pMainIfc == NULL) {
1122         pthread_mutex_unlock(GetInterfaceLock());
1123         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
1124         return HDF_ERR_INVALID_PARAM;
1125     }
1126     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdSetServDiscExternal(pMainIfc, mode);
1127     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
1128         pthread_mutex_unlock(GetInterfaceLock());
1129         HDF_LOGE("WpaP2pCliCmdSetServDiscExternal fail, ret = %{public}d", ret);
1130         return HDF_FAILURE;
1131     }
1132     pthread_mutex_unlock(GetInterfaceLock());
1133     HDF_LOGI("%{public}s success", __func__);
1134     return HDF_SUCCESS;
1135 }
1136 
WpaInterfaceP2pRemoveGroup(struct IWpaInterface * self,const char * ifName,const char * groupName)1137 int32_t WpaInterfaceP2pRemoveGroup(struct IWpaInterface *self, const char *ifName, const char *groupName)
1138 {
1139     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1140     (void)self;
1141     if (ifName == NULL || groupName == NULL) {
1142         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1143         return HDF_ERR_INVALID_PARAM;
1144     }
1145     pthread_mutex_lock(GetInterfaceLock());
1146     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
1147     if (pMainIfc == NULL) {
1148         pthread_mutex_unlock(GetInterfaceLock());
1149         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
1150         return HDF_ERR_INVALID_PARAM;
1151     }
1152     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdP2pRemoveGroup(pMainIfc, groupName);
1153     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
1154         pthread_mutex_unlock(GetInterfaceLock());
1155         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
1156         return HDF_FAILURE;
1157     }
1158     pthread_mutex_unlock(GetInterfaceLock());
1159     HDF_LOGI("%{public}s success", __func__);
1160     return HDF_SUCCESS;
1161 }
1162 
WpaInterfaceP2pCancelConnect(struct IWpaInterface * self,const char * ifName)1163 int32_t WpaInterfaceP2pCancelConnect(struct IWpaInterface *self, const char *ifName)
1164 {
1165     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1166     (void)self;
1167     (void)ifName;
1168     pthread_mutex_lock(GetInterfaceLock());
1169     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
1170     if (pMainIfc == NULL) {
1171         pthread_mutex_unlock(GetInterfaceLock());
1172         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
1173         return HDF_ERR_INVALID_PARAM;
1174     }
1175     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdCancelConnect(pMainIfc);
1176     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
1177         pthread_mutex_unlock(GetInterfaceLock());
1178         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
1179         return HDF_FAILURE;
1180     }
1181     pthread_mutex_unlock(GetInterfaceLock());
1182     HDF_LOGI("%{public}s success", __func__);
1183     return HDF_SUCCESS;
1184 }
1185 
WpaInterfaceP2pGetGroupConfig(struct IWpaInterface * self,const char * ifName,const int32_t networkId,const char * param,char * value,uint32_t valueLen)1186 int32_t WpaInterfaceP2pGetGroupConfig(struct IWpaInterface *self, const char *ifName, const int32_t networkId,
1187     const char *param, char *value, uint32_t valueLen)
1188 {
1189     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1190     char cmd[CMD_SIZE];
1191 
1192     int32_t ret = 0;
1193     (void)self;
1194     if (ifName == NULL || param == NULL || value == NULL) {
1195         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1196         return HDF_ERR_INVALID_PARAM;
1197     }
1198     pthread_mutex_lock(GetInterfaceLock());
1199     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_NETWORK %d %s", ifName, networkId, param);
1200     if (ret < 0) {
1201         pthread_mutex_unlock(GetInterfaceLock());
1202         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
1203         return HDF_FAILURE;
1204     }
1205     if (WpaCliCmd(cmd, value, valueLen) != 0) {
1206         pthread_mutex_unlock(GetInterfaceLock());
1207         HDF_LOGE("GET_NETWORK command failed!");
1208         return HDF_FAILURE;
1209     }
1210     pthread_mutex_unlock(GetInterfaceLock());
1211     HDF_LOGI("%{public}s success", __func__);
1212     return HDF_SUCCESS;
1213 }
1214 
WpaInterfaceP2pAddNetwork(struct IWpaInterface * self,const char * ifName,int32_t * networkId)1215 int32_t WpaInterfaceP2pAddNetwork(struct IWpaInterface *self, const char *ifName, int32_t *networkId)
1216 {
1217     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1218     if (ifName == NULL || networkId == NULL) {
1219         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1220         return HDF_ERR_INVALID_PARAM;
1221     }
1222     (void)self;
1223     pthread_mutex_lock(GetInterfaceLock());
1224     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
1225     if (pMainIfc == NULL) {
1226         pthread_mutex_unlock(GetInterfaceLock());
1227         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
1228         return HDF_ERR_INVALID_PARAM;
1229     }
1230     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdAddNetwork(pMainIfc, networkId);
1231     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
1232         pthread_mutex_unlock(GetInterfaceLock());
1233         HDF_LOGE("WpaP2pCliCmdAddNetwork fail, ret = %{public}d", ret);
1234         return HDF_FAILURE;
1235     }
1236     pthread_mutex_unlock(GetInterfaceLock());
1237     HDF_LOGI("%{public}s success", __func__);
1238     return HDF_SUCCESS;
1239 }
1240 
WpaInterfaceP2pGetPeer(struct IWpaInterface * self,const char * ifName,const char * bssid,struct HdiP2pDeviceInfo * info)1241 int32_t WpaInterfaceP2pGetPeer(struct IWpaInterface *self, const char *ifName, const char *bssid,
1242     struct HdiP2pDeviceInfo *info)
1243 {
1244     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1245     if (ifName == NULL || info == NULL || bssid == NULL) {
1246         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1247         return HDF_ERR_INVALID_PARAM;
1248     }
1249     pthread_mutex_lock(GetInterfaceLock());
1250     char *reply;
1251     const int replySize = REPLY_SIZE;
1252     char cmd[CMD_SIZE];
1253 
1254     reply = (char *)malloc(replySize);
1255     if (reply == NULL) {
1256         pthread_mutex_unlock(GetInterfaceLock());
1257         HDF_LOGE("%{public}s reply is NULL!", __func__);
1258         return HDF_FAILURE;
1259     }
1260 
1261     int32_t ret = 0;
1262     (void)self;
1263 
1264     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s P2P_PEER %s", ifName, bssid);
1265     if (ret < 0) {
1266         pthread_mutex_unlock(GetInterfaceLock());
1267         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
1268         free(reply);
1269         return HDF_FAILURE;
1270     }
1271 
1272     if (WpaCliCmd(cmd, reply, REPLY_SIZE) != 0) {
1273         pthread_mutex_unlock(GetInterfaceLock());
1274         HDF_LOGE("P2P_PEER command failed!");
1275         free(reply);
1276         return HDF_FAILURE;
1277     }
1278 
1279     if (strstr(reply, "\n") == NULL) {
1280         pthread_mutex_unlock(GetInterfaceLock());
1281         HDF_LOGE("%{public}s reply is error", __func__);
1282         free(reply);
1283         return HDF_FAILURE;
1284     }
1285     char *savedPtr = NULL;
1286     char *token = strtok_r(reply, "\n", &savedPtr);
1287     info->srcAddress = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * (ETH_ADDR_LEN + 1));
1288     if (info->srcAddress == NULL) {
1289         pthread_mutex_unlock(GetInterfaceLock());
1290         HDF_LOGE("malloc srcAddress failed!");
1291         free(reply);
1292         HdiP2pDeviceInfoFree(info, false);
1293         return HDF_FAILURE;
1294     }
1295     info->p2pDeviceAddress = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * (ETH_ADDR_LEN + 1));
1296     if (info->p2pDeviceAddress == NULL) {
1297         pthread_mutex_unlock(GetInterfaceLock());
1298         HDF_LOGE("malloc p2pDeviceAddress failed!");
1299         free(reply);
1300         HdiP2pDeviceInfoFree(info, false);
1301         return HDF_FAILURE;
1302     }
1303     info->primaryDeviceType = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_P2P_DEVICE_TYPE_LENGTH);
1304     if (info->primaryDeviceType == NULL) {
1305         pthread_mutex_unlock(GetInterfaceLock());
1306         HDF_LOGE("malloc primaryDeviceType failed!");
1307         free(reply);
1308         HdiP2pDeviceInfoFree(info, false);
1309         return HDF_FAILURE;
1310     }
1311     info->deviceName = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_P2P_DEVICE_NAME_LENGTH);
1312     if (info->deviceName == NULL) {
1313         pthread_mutex_unlock(GetInterfaceLock());
1314         HDF_LOGE("malloc deviceName failed!");
1315         free(reply);
1316         HdiP2pDeviceInfoFree(info, false);
1317         return HDF_FAILURE;
1318     }
1319     info->wfdDeviceInfo = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_P2P_WFD_DEVICE_INFO_LENGTH);
1320     if (info->wfdDeviceInfo == NULL) {
1321         pthread_mutex_unlock(GetInterfaceLock());
1322         HDF_LOGE("malloc wfdDeviceInfo failed!");
1323         free(reply);
1324         HdiP2pDeviceInfoFree(info, false);
1325         return HDF_FAILURE;
1326     }
1327     info->operSsid = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_P2P_DEVICE_NAME_LENGTH);
1328     if (info->operSsid == NULL) {
1329         pthread_mutex_unlock(GetInterfaceLock());
1330         HDF_LOGE("malloc operSsid failed!");
1331         free(reply);
1332         HdiP2pDeviceInfoFree(info, false);
1333         return HDF_FAILURE;
1334     }
1335     info->srcAddressLen = ETH_ADDR_LEN + 1;
1336     info->p2pDeviceAddressLen = ETH_ADDR_LEN + 1;
1337     info->primaryDeviceTypeLen = WIFI_P2P_DEVICE_TYPE_LENGTH;
1338     info->deviceNameLen = WIFI_P2P_DEVICE_NAME_LENGTH;
1339     info->wfdDeviceInfoLen = WIFI_P2P_WFD_DEVICE_INFO_LENGTH;
1340     info->operSsidLen = WIFI_P2P_DEVICE_NAME_LENGTH;
1341     uint8_t tmpBssid[ETH_ADDR_LEN] = {0};
1342     hwaddr_aton(token, tmpBssid);
1343     if (memcpy_s((char *)info->p2pDeviceAddress, ETH_ADDR_LEN, (char *)tmpBssid, ETH_ADDR_LEN) != EOK) {
1344         HDF_LOGE("%{public}s memcpy failed", __func__);
1345     }
1346     while (token != NULL) {
1347         struct HdiWpaKeyValue retMsg = {{0}, {0}};
1348         GetStrKeyVal(token, "=", &retMsg);
1349         if (strncmp(retMsg.key, "pri_dev_type", strlen("pri_dev_type")) == 0) {
1350             if (strcpy_s((char *)info->primaryDeviceType, WIFI_P2P_DEVICE_TYPE_LENGTH + 1, retMsg.value) != EOK) {
1351                 HDF_LOGE("%{public}s strcpy failed", __func__);
1352             }
1353         } else if (strncmp(retMsg.key, "device_name", strlen("device_name")) == 0) {
1354             if (strcpy_s((char *)info->deviceName, WIFI_P2P_DEVICE_NAME_LENGTH + 1, retMsg.value) != EOK) {
1355                 HDF_LOGE("%{public}s strcpy failed", __func__);
1356             }
1357         } else if (strncmp(retMsg.key, "config_methods", strlen("config_methods")) == 0) {
1358             info->configMethods = Hex2Dec(retMsg.value);
1359         } else if (strncmp(retMsg.key, "dev_capab", strlen("dev_capab")) == 0) {
1360             info->deviceCapabilities = Hex2Dec(retMsg.value);
1361         } else if (strncmp(retMsg.key, "group_capab", strlen("group_capab")) == 0) {
1362             info->groupCapabilities = Hex2Dec(retMsg.value);
1363         } else if (strncmp(retMsg.key, "oper_ssid", strlen("oper_ssid")) == 0) {
1364             if (strcpy_s((char *)info->operSsid, WIFI_P2P_DEVICE_NAME_LENGTH + 1, retMsg.value) != EOK) {
1365                 HDF_LOGE("%{public}s strcpy failed", __func__);
1366             }
1367             printf_decode((u8 *)info->operSsid, WIFI_P2P_DEVICE_NAME_LENGTH + 1, (char *)info->operSsid);
1368         }
1369         token = strtok_r(NULL, "\n", &savedPtr);
1370     }
1371     pthread_mutex_unlock(GetInterfaceLock());
1372     free(reply);
1373     HDF_LOGI("%{public}s success", __func__);
1374     return HDF_SUCCESS;
1375 }
1376 
WpaInterfaceP2pGetGroupCapability(struct IWpaInterface * self,const char * ifName,const char * bssid,int32_t * cap)1377 int32_t WpaInterfaceP2pGetGroupCapability(struct IWpaInterface *self, const char *ifName,
1378     const char *bssid, int32_t *cap)
1379 {
1380     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1381     if (ifName == NULL || bssid == NULL || cap == NULL) {
1382         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1383         return HDF_ERR_INVALID_PARAM;
1384     }
1385     pthread_mutex_lock(GetInterfaceLock());
1386     char *reply;
1387     const int replySize = REPLY_SIZE;
1388     char cmd[CMD_SIZE];
1389 
1390     reply = (char *)malloc(replySize);
1391     if (reply == NULL) {
1392         pthread_mutex_unlock(GetInterfaceLock());
1393         HDF_LOGE("%{public}s reply is NULL!", __func__);
1394         return HDF_FAILURE;
1395     }
1396 
1397     int32_t ret = 0;
1398     (void)self;
1399 
1400     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s P2P_PEER %s", ifName, bssid);
1401     if (ret < 0) {
1402         pthread_mutex_unlock(GetInterfaceLock());
1403         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
1404         free(reply);
1405         return HDF_FAILURE;
1406     }
1407 
1408     if (WpaCliCmd(cmd, reply, REPLY_SIZE) != 0) {
1409         pthread_mutex_unlock(GetInterfaceLock());
1410         HDF_LOGE("P2P_PEER command failed!");
1411         free(reply);
1412         return HDF_FAILURE;
1413     }
1414 
1415     if (strstr(reply, "\n") == NULL) {
1416         pthread_mutex_unlock(GetInterfaceLock());
1417         HDF_LOGE("%{public}s reply is error", __func__);
1418         free(reply);
1419         return HDF_FAILURE;
1420     }
1421     char *savedPtr = NULL;
1422     char *token = strtok_r(reply, "\n", &savedPtr);
1423 
1424     while (token != NULL) {
1425         struct HdiWpaKeyValue retMsg = {{0}, {0}};
1426         GetStrKeyVal(token, "=", &retMsg);
1427         if (strncmp(retMsg.key, "group_capab", strlen("group_capab")) == 0) {
1428             *cap = Hex2Dec(retMsg.value);
1429         }
1430         token = strtok_r(NULL, "\n", &savedPtr);
1431     }
1432     pthread_mutex_unlock(GetInterfaceLock());
1433     free(reply);
1434     HDF_LOGI("%{public}s success", __func__);
1435     return HDF_SUCCESS;
1436 }
1437 
WpaInterfaceP2pListNetworks(struct IWpaInterface * self,const char * ifName,struct HdiP2pNetworkList * infoList)1438 int32_t WpaInterfaceP2pListNetworks(struct IWpaInterface *self, const char *ifName,
1439     struct HdiP2pNetworkList *infoList)
1440 {
1441     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1442     if (ifName == NULL || infoList == NULL) {
1443         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1444         return HDF_ERR_INVALID_PARAM;
1445     }
1446     pthread_mutex_lock(GetInterfaceLock());
1447     char *reply;
1448     const int replySize = P2P_LIST_REPLY_SIZE;
1449     char cmd[CMD_SIZE];
1450     reply = (char *)malloc(replySize);
1451     if (reply == NULL) {
1452         pthread_mutex_unlock(GetInterfaceLock());
1453         HDF_LOGE("%{public}s reply is NULL!", __func__);
1454         return HDF_FAILURE;
1455     }
1456 
1457     (void)self;
1458     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s LIST_NETWORKS", ifName) < 0) {
1459         pthread_mutex_unlock(GetInterfaceLock());
1460         HDF_LOGE("snprintf err");
1461         free(reply);
1462         return HDF_FAILURE;
1463     }
1464     if (WpaCliCmd(cmd, reply, P2P_LIST_REPLY_SIZE) != 0) {
1465         pthread_mutex_unlock(GetInterfaceLock());
1466         HDF_LOGE("LIST_NETWORKS command failed!");
1467         free(reply);
1468         return HDF_FAILURE;
1469     }
1470 
1471     char *token = strstr(reply, "\n");
1472     if (token == NULL) {
1473         pthread_mutex_unlock(GetInterfaceLock());
1474         HDF_LOGE("%{public}s token is NULL!", __func__);
1475         free(reply);
1476         return HDF_FAILURE;
1477     }
1478     char *tmpPos = token + 1;
1479     while ((tmpPos = strstr(tmpPos, "\n")) != NULL) {
1480         infoList->infoNum += 1;
1481         ++tmpPos;
1482     }
1483     if (infoList->infoNum <= 0) {
1484         pthread_mutex_unlock(GetInterfaceLock());
1485         HDF_LOGE("%{public}s infoList->infoNum <= 0", __func__);
1486         free(reply);
1487         return HDF_FAILURE;
1488     }
1489     infoList->infos = (struct HdiP2pNetworkInfo *)OsalMemCalloc(sizeof(struct HdiP2pNetworkInfo) * infoList->infoNum);
1490     if (infoList->infos == NULL) {
1491         pthread_mutex_unlock(GetInterfaceLock());
1492         HDF_LOGE("malloc infos failed!");
1493         free(reply);
1494         return HDF_FAILURE;
1495     }
1496     infoList->infosLen = (uint32_t)infoList->infoNum;
1497     char *tmpBuf = token + 1;
1498     char *savedPtr = NULL;
1499     token = strtok_r(tmpBuf, "\n", &savedPtr);
1500     int index = 0;
1501     while (token != NULL) {
1502         if (index >= infoList->infoNum) {
1503             break;
1504         }
1505         infoList->infos[index].ssid = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_SSID_LENGTH);
1506         if (infoList->infos[index].ssid == NULL) {
1507             HDF_LOGE("malloc ssid failed!");
1508             HdiP2pNetworkInfoFree(&(infoList->infos[index]), true);
1509             break;
1510         }
1511         infoList->infos[index].ssidLen = WIFI_SSID_LENGTH;
1512         infoList->infos[index].bssid = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * (ETH_ADDR_LEN + 1));
1513         if (infoList->infos[index].bssid == NULL) {
1514             HDF_LOGE("malloc bssid failed!");
1515             HdiP2pNetworkInfoFree(&(infoList->infos[index]), true);
1516             break;
1517         }
1518         infoList->infos[index].bssidLen = ETH_ADDR_LEN + 1;
1519         infoList->infos[index].flags = (uint8_t *)OsalMemCalloc(sizeof(uint8_t) * WIFI_NETWORK_FLAGS_LENGTH);
1520         if (infoList->infos[index].flags == NULL) {
1521             HDF_LOGE("malloc flags failed!");
1522             HdiP2pNetworkInfoFree(&(infoList->infos[index]), true);
1523             break;
1524         }
1525         infoList->infos[index].flagsLen = WIFI_NETWORK_FLAGS_LENGTH;
1526         GetHalNetworkInfos(token, &(infoList->infos[index]));
1527         index++;
1528         token = strtok_r(NULL, "\n", &savedPtr);
1529     }
1530     pthread_mutex_unlock(GetInterfaceLock());
1531     free(reply);
1532     HDF_LOGI("%{public}s success", __func__);
1533     return HDF_SUCCESS;
1534 }
1535 
WpaInterfaceP2pSaveConfig(struct IWpaInterface * self,const char * ifName)1536 int32_t WpaInterfaceP2pSaveConfig(struct IWpaInterface *self, const char *ifName)
1537 {
1538     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1539     (void)self;
1540     (void)ifName;
1541     pthread_mutex_lock(GetInterfaceLock());
1542     WifiWpaP2pInterface *pMainIfc = GetWifiWapP2pInterface(ifName);
1543     if (pMainIfc == NULL) {
1544         pthread_mutex_unlock(GetInterfaceLock());
1545         HDF_LOGE("%{public}s: pMainIfc is null", __func__);
1546         return HDF_ERR_INVALID_PARAM;
1547     }
1548     P2pSupplicantErrCode ret = pMainIfc->wpaP2pCliCmdStoreConfig(pMainIfc);
1549     if (ret != P2P_SUP_ERRCODE_SUCCESS) {
1550         pthread_mutex_unlock(GetInterfaceLock());
1551         HDF_LOGE("%{public}s: fail, ret = %{public}d", __func__, ret);
1552         return HDF_FAILURE;
1553     }
1554     pthread_mutex_unlock(GetInterfaceLock());
1555     HDF_LOGI("%{public}s success", __func__);
1556     return HDF_SUCCESS;
1557 }
1558 
WpaInterfaceDeliverP2pData(struct IWpaInterface * self,const char * ifName,int32_t cmdType,int32_t dataType,const char * carryData)1559 int32_t WpaInterfaceDeliverP2pData(struct IWpaInterface *self, const char *ifName,
1560     int32_t cmdType, int32_t dataType, const char *carryData)
1561 {
1562     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1563     (void)self;
1564     (void)ifName;
1565     char cmd[CMD_SIZE] = {0};
1566     char buf[CMD_SIZE] = {0};
1567 
1568     int32_t ret = 0;
1569     if (ifName == NULL) {
1570         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
1571         return HDF_ERR_INVALID_PARAM;
1572     }
1573     pthread_mutex_lock(GetInterfaceLock());
1574     switch (cmdType) {
1575         case P2P_REMOVE_GROUP_CLIENT: {
1576             ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1,
1577                 "IFNAME=%s P2P_REMOVE_CLIENT %s", ifName, carryData);
1578             break;
1579         }
1580         default: {
1581             ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1,
1582                 "IFNAME=%s P2P_DELIVER_DATA cmdType=%d dataType=%d carryData=%s", ifName, cmdType, dataType, carryData);
1583             break;
1584         }
1585     }
1586     if (ret < 0) {
1587         pthread_mutex_unlock(GetInterfaceLock());
1588         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{private}s, count = %{public}d", __func__, cmd, ret);
1589         return HDF_FAILURE;
1590     }
1591     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
1592         pthread_mutex_unlock(GetInterfaceLock());
1593         HDF_LOGE("%{public}s command failed!", __func__);
1594         return HDF_FAILURE;
1595     }
1596     pthread_mutex_unlock(GetInterfaceLock());
1597     HDF_LOGI("%{public}s success", __func__);
1598     return HDF_SUCCESS;
1599 }
1600 
WpaInterfaceVendorExtProcessCmd(struct IWpaInterface * self,const char * ifName,const char * cmd)1601 int32_t WpaInterfaceVendorExtProcessCmd(struct IWpaInterface *self, const char *ifName, const char *cmd)
1602 {
1603 #define NEW_CMD_MAX_LEN 400
1604     HDF_LOGI("Ready to enter hdi %{public}s", __func__);
1605     int32_t ret = 0;
1606     (void)self;
1607     if (cmd == NULL || ifName == NULL) {
1608         HDF_LOGE("%{public}s input parameter invalid!", __func__);
1609         return HDF_ERR_INVALID_PARAM ;
1610     }
1611     pthread_mutex_lock(GetInterfaceLock());
1612     char *reply;
1613     const int replySize = REPLY_SIZE;
1614     reply = (char *)malloc(replySize);
1615     if (reply == NULL) {
1616         pthread_mutex_unlock(GetInterfaceLock());
1617         HDF_LOGE("%{public}s reply is NULL!", __func__);
1618         return HDF_FAILURE;
1619     }
1620 
1621     char newCmd[NEW_CMD_MAX_LEN] = {0};
1622     if (snprintf_s(newCmd, sizeof(newCmd), sizeof(newCmd) - 1, "IFNAME=%s %s", ifName, cmd) < 0) {
1623         pthread_mutex_unlock(GetInterfaceLock());
1624         HDF_LOGE("%{public}s: snprintf_s is failed, error code: %{public}d", __func__, ret);
1625         free(reply);
1626         return HDF_FAILURE;
1627     }
1628 
1629     if (WpaCliCmd(newCmd, reply, replySize) < 0) {
1630         pthread_mutex_unlock(GetInterfaceLock());
1631         HDF_LOGE("%{public}s WpaCliCmd failed!", __func__);
1632         free(reply);
1633         return HDF_FAILURE;
1634     }
1635 
1636     HDF_LOGI("%{public}s reply %{public}s !", __func__, reply);
1637     pthread_mutex_unlock(GetInterfaceLock());
1638     ret = atoi(reply);
1639     free(reply);
1640     return ret;
1641 }
1642 
WpaFillP2pDeviceFoundParam(struct P2pDeviceInfoParam * deviceInfoParam,struct HdiP2pDeviceInfoParam * hdiP2pDeviceInfoParam)1643 static int32_t WpaFillP2pDeviceFoundParam(struct P2pDeviceInfoParam *deviceInfoParam,
1644     struct HdiP2pDeviceInfoParam *hdiP2pDeviceInfoParam)
1645 {
1646     int32_t ret = 0;
1647     if (deviceInfoParam == NULL || hdiP2pDeviceInfoParam == NULL) {
1648         HDF_LOGE("%{public}s: deviceInfoParam or hdiP2pDeviceInfo is NULL!", __func__);
1649         return HDF_ERR_INVALID_PARAM;
1650     }
1651     hdiP2pDeviceInfoParam->configMethods = deviceInfoParam->configMethods;
1652     hdiP2pDeviceInfoParam->deviceCapabilities = deviceInfoParam->deviceCapabilities;
1653     hdiP2pDeviceInfoParam->groupCapabilities = deviceInfoParam->groupCapabilities;
1654     hdiP2pDeviceInfoParam->wfdLength = deviceInfoParam->wfdLength;
1655 
1656     do {
1657         if (FillData(&hdiP2pDeviceInfoParam->srcAddress, &hdiP2pDeviceInfoParam->srcAddressLen,
1658             deviceInfoParam->srcAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1659             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1660             ret = HDF_FAILURE;
1661             break;
1662         }
1663         if (FillData(&hdiP2pDeviceInfoParam->p2pDeviceAddress, &hdiP2pDeviceInfoParam->p2pDeviceAddressLen,
1664             deviceInfoParam->p2pDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1665             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1666             ret = HDF_FAILURE;
1667             break;
1668         }
1669         if (FillData(&hdiP2pDeviceInfoParam->primaryDeviceType, &hdiP2pDeviceInfoParam->primaryDeviceTypeLen,
1670             deviceInfoParam->primaryDeviceType, WIFI_P2P_DEVICE_TYPE_LENGTH) != HDF_SUCCESS) {
1671             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1672             ret = HDF_FAILURE;
1673             break;
1674         }
1675         if (FillData(&hdiP2pDeviceInfoParam->deviceName, &hdiP2pDeviceInfoParam->deviceNameLen,
1676             deviceInfoParam->deviceName, WIFI_P2P_DEVICE_NAME_LENGTH) != HDF_SUCCESS) {
1677             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1678             ret = HDF_FAILURE;
1679             break;
1680         }
1681         if (deviceInfoParam->wfdLength != 0 &&
1682             FillData(&hdiP2pDeviceInfoParam->wfdDeviceInfo, &hdiP2pDeviceInfoParam->wfdDeviceInfoLen,
1683             deviceInfoParam->wfdDeviceInfo, deviceInfoParam->wfdLength) != HDF_SUCCESS) {
1684             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1685             ret = HDF_FAILURE;
1686             break;
1687         }
1688         if (FillData(&hdiP2pDeviceInfoParam->operSsid, &hdiP2pDeviceInfoParam->operSsidLen,
1689             deviceInfoParam->operSsid, WIFI_P2P_DEVICE_NAME_LENGTH) != HDF_SUCCESS) {
1690             HDF_LOGE("%{public}s: fill reason fail!", __func__);
1691             ret = HDF_FAILURE;
1692         }
1693     } while (0);
1694 
1695     if (ret != HDF_SUCCESS) {
1696         if (hdiP2pDeviceInfoParam->srcAddress != NULL) {
1697             OsalMemFree(hdiP2pDeviceInfoParam->srcAddress);
1698             hdiP2pDeviceInfoParam->srcAddress = NULL;
1699         }
1700         if (hdiP2pDeviceInfoParam->p2pDeviceAddress != NULL) {
1701             OsalMemFree(hdiP2pDeviceInfoParam->p2pDeviceAddress);
1702             hdiP2pDeviceInfoParam->p2pDeviceAddress = NULL;
1703         }
1704         if (hdiP2pDeviceInfoParam->primaryDeviceType != NULL) {
1705             OsalMemFree(hdiP2pDeviceInfoParam->primaryDeviceType);
1706             hdiP2pDeviceInfoParam->primaryDeviceType = NULL;
1707         }
1708         if (hdiP2pDeviceInfoParam->deviceName != NULL) {
1709             OsalMemFree(hdiP2pDeviceInfoParam->deviceName);
1710             hdiP2pDeviceInfoParam->deviceName = NULL;
1711         }
1712         if (hdiP2pDeviceInfoParam->wfdDeviceInfo != NULL) {
1713             OsalMemFree(hdiP2pDeviceInfoParam->wfdDeviceInfo);
1714             hdiP2pDeviceInfoParam->wfdDeviceInfo = NULL;
1715         }
1716         if (hdiP2pDeviceInfoParam->operSsid != NULL) {
1717             OsalMemFree(hdiP2pDeviceInfoParam->operSsid);
1718             hdiP2pDeviceInfoParam->operSsid = NULL;
1719         }
1720     }
1721     return ret;
1722 }
1723 
WpaFillP2pDeviceLostParam(struct P2pDeviceLostParam * deviceLostParam,struct HdiP2pDeviceLostParam * hdiP2pDeviceLostParam)1724 static int32_t WpaFillP2pDeviceLostParam(struct P2pDeviceLostParam  *deviceLostParam,
1725     struct HdiP2pDeviceLostParam *hdiP2pDeviceLostParam)
1726 {
1727     int32_t ret = 0;
1728     if (deviceLostParam == NULL || hdiP2pDeviceLostParam == NULL) {
1729         HDF_LOGE("%{public}s: deviceLostParam or hdiP2pDeviceLostParam is NULL!", __func__);
1730         return HDF_ERR_INVALID_PARAM;
1731     }
1732     hdiP2pDeviceLostParam->networkId = deviceLostParam->networkId;
1733 
1734     if (FillData(&hdiP2pDeviceLostParam->p2pDeviceAddress, &hdiP2pDeviceLostParam->p2pDeviceAddressLen,
1735         deviceLostParam->p2pDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1736             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1737             ret = HDF_FAILURE;
1738     }
1739     if (ret != HDF_SUCCESS) {
1740         if (hdiP2pDeviceLostParam->p2pDeviceAddress != NULL) {
1741             OsalMemFree(hdiP2pDeviceLostParam->p2pDeviceAddress);
1742             hdiP2pDeviceLostParam->p2pDeviceAddress = NULL;
1743         }
1744     }
1745     return ret;
1746 }
1747 
WpaFillP2pGoNegotiationRequestParam(struct P2pGoNegotiationRequestParam * goNegotiationRequestParam,struct HdiP2pGoNegotiationRequestParam * hdiP2pGoNegotiationRequestParam)1748 static int32_t WpaFillP2pGoNegotiationRequestParam(struct P2pGoNegotiationRequestParam *goNegotiationRequestParam,
1749     struct HdiP2pGoNegotiationRequestParam *hdiP2pGoNegotiationRequestParam)
1750 {
1751     int32_t ret = 0;
1752     if (goNegotiationRequestParam == NULL || hdiP2pGoNegotiationRequestParam == NULL) {
1753         HDF_LOGE("%{public}s: goNegotiationRequestParam or hdiP2pGoNegotiationRequestParam is NULL!", __func__);
1754         return HDF_ERR_INVALID_PARAM;
1755     }
1756     hdiP2pGoNegotiationRequestParam->passwordId = goNegotiationRequestParam->passwordId;
1757 
1758     if (FillData(&hdiP2pGoNegotiationRequestParam->srcAddress, &hdiP2pGoNegotiationRequestParam->srcAddressLen,
1759         goNegotiationRequestParam->srcAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1760             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1761             ret = HDF_FAILURE;
1762     }
1763     if (ret != HDF_SUCCESS) {
1764         if (hdiP2pGoNegotiationRequestParam->srcAddress != NULL) {
1765             OsalMemFree(hdiP2pGoNegotiationRequestParam->srcAddress);
1766             hdiP2pGoNegotiationRequestParam->srcAddress = NULL;
1767         }
1768     }
1769     return ret;
1770 }
1771 
WpaFillP2pGoNegotiationCompletedParam(struct P2pGoNegotiationCompletedParam * goNegotiationCompletedParam,struct HdiP2pGoNegotiationCompletedParam * hdiP2pGoNegotiationCompletedParam)1772 static int32_t WpaFillP2pGoNegotiationCompletedParam(struct P2pGoNegotiationCompletedParam
1773     *goNegotiationCompletedParam, struct HdiP2pGoNegotiationCompletedParam *hdiP2pGoNegotiationCompletedParam)
1774 {
1775     int32_t ret = 0;
1776     if (goNegotiationCompletedParam == NULL || hdiP2pGoNegotiationCompletedParam == NULL) {
1777         HDF_LOGE("%{public}s: goNegotiationCompletedParam or hdiP2pGoNegotiationCompletedParam is NULL!", __func__);
1778         return HDF_ERR_INVALID_PARAM;
1779     }
1780     hdiP2pGoNegotiationCompletedParam->status = goNegotiationCompletedParam->status;
1781     return ret;
1782 }
1783 
WpaFillP2pInvitationReceivedParam(struct P2pInvitationReceivedParam * invitationReceivedParam,struct HdiP2pInvitationReceivedParam * hdiP2pInvitationReceivedParam)1784 static int32_t WpaFillP2pInvitationReceivedParam(struct P2pInvitationReceivedParam *invitationReceivedParam,
1785     struct HdiP2pInvitationReceivedParam *hdiP2pInvitationReceivedParam)
1786 {
1787     int32_t ret = HDF_SUCCESS;
1788     if (invitationReceivedParam == NULL || hdiP2pInvitationReceivedParam == NULL) {
1789         HDF_LOGE("%{public}s: invitationReceivedParam or hdiP2pInvitationReceivedParam is NULL!", __func__);
1790         return HDF_ERR_INVALID_PARAM;
1791     }
1792     hdiP2pInvitationReceivedParam->type = invitationReceivedParam->type;
1793     hdiP2pInvitationReceivedParam->persistentNetworkId = invitationReceivedParam->persistentNetworkId;
1794     hdiP2pInvitationReceivedParam->operatingFrequency = invitationReceivedParam->operatingFrequency;
1795 
1796     do {
1797         if (FillData(&hdiP2pInvitationReceivedParam->srcAddress, &hdiP2pInvitationReceivedParam->srcAddressLen,
1798             invitationReceivedParam->srcAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1799             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1800             ret = HDF_FAILURE;
1801             break;
1802         }
1803         if (FillData(&hdiP2pInvitationReceivedParam->goDeviceAddress,
1804             &hdiP2pInvitationReceivedParam->goDeviceAddressLen,
1805             invitationReceivedParam->goDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1806             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1807             ret = HDF_FAILURE;
1808             break;
1809         }
1810         if (FillData(&hdiP2pInvitationReceivedParam->bssid, &hdiP2pInvitationReceivedParam->bssidLen,
1811             invitationReceivedParam->bssid, ETH_ADDR_LEN) != HDF_SUCCESS) {
1812             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
1813             ret = HDF_FAILURE;
1814         }
1815     } while (0);
1816 
1817     if (ret != HDF_SUCCESS) {
1818         if (hdiP2pInvitationReceivedParam->srcAddress != NULL) {
1819             OsalMemFree(hdiP2pInvitationReceivedParam->srcAddress);
1820             hdiP2pInvitationReceivedParam->srcAddress = NULL;
1821         }
1822         if (hdiP2pInvitationReceivedParam->goDeviceAddress != NULL) {
1823             OsalMemFree(hdiP2pInvitationReceivedParam->goDeviceAddress);
1824             hdiP2pInvitationReceivedParam->goDeviceAddress = NULL;
1825         }
1826         if (hdiP2pInvitationReceivedParam->bssid != NULL) {
1827             OsalMemFree(hdiP2pInvitationReceivedParam->bssid);
1828             hdiP2pInvitationReceivedParam->bssid = NULL;
1829         }
1830     }
1831     return ret;
1832 }
1833 
WpaFillP2pInvitationResultParam(struct P2pInvitationResultParam * invitationResultParam,struct HdiP2pInvitationResultParam * hdiP2pInvitationResultParam)1834 static int32_t WpaFillP2pInvitationResultParam(struct P2pInvitationResultParam *invitationResultParam,
1835     struct HdiP2pInvitationResultParam *hdiP2pInvitationResultParam)
1836 {
1837     int32_t ret = HDF_SUCCESS;
1838     if (invitationResultParam == NULL || hdiP2pInvitationResultParam == NULL) {
1839         HDF_LOGE("%{public}s: invitationResultParam or hdiP2pInvitationResultParam is NULL!", __func__);
1840         return HDF_ERR_INVALID_PARAM;
1841     }
1842     hdiP2pInvitationResultParam->status = invitationResultParam->status;
1843 
1844     if (FillData(&hdiP2pInvitationResultParam->bssid, &hdiP2pInvitationResultParam->bssidLen,
1845         invitationResultParam->bssid, ETH_ADDR_LEN) != HDF_SUCCESS) {
1846             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1847             ret = HDF_FAILURE;
1848     }
1849     if (ret != HDF_SUCCESS) {
1850         if (hdiP2pInvitationResultParam->bssid != NULL) {
1851             OsalMemFree(hdiP2pInvitationResultParam->bssid);
1852             hdiP2pInvitationResultParam->bssid = NULL;
1853         }
1854     }
1855     return ret;
1856 }
1857 
FillHdiP2pGroupInfoStartedParam(struct P2pGroupStartedParam * groupStartedParam,struct HdiP2pGroupInfoStartedParam * hdiP2pGroupStartedParam)1858 static int32_t FillHdiP2pGroupInfoStartedParam(struct P2pGroupStartedParam *groupStartedParam,
1859     struct HdiP2pGroupInfoStartedParam *hdiP2pGroupStartedParam)
1860 {
1861     int32_t ret = HDF_SUCCESS;
1862     if (groupStartedParam == NULL || hdiP2pGroupStartedParam == NULL) {
1863         HDF_LOGE("%{public}s: groupStartedParam or hdiP2pGroupStartedParam is NULL!", __func__);
1864         return HDF_ERR_INVALID_PARAM;
1865     }
1866     do {
1867         if (FillData(&hdiP2pGroupStartedParam->groupIfName, &hdiP2pGroupStartedParam->groupIfNameLen,
1868             groupStartedParam->groupIfName, WIFI_P2P_GROUP_IFNAME_LENGTH) != HDF_SUCCESS) {
1869             HDF_LOGE("%{public}s: fill groupIfName fail!", __func__);
1870             ret = HDF_FAILURE;
1871             break;
1872         }
1873         if (FillData(&hdiP2pGroupStartedParam->ssid, &hdiP2pGroupStartedParam->ssidLen,
1874             groupStartedParam->ssid, WIFI_SSID_LENGTH) != HDF_SUCCESS) {
1875             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
1876             ret = HDF_FAILURE;
1877             break;
1878         }
1879         if (FillData(&hdiP2pGroupStartedParam->psk, &hdiP2pGroupStartedParam->pskLen,
1880             groupStartedParam->psk, WIFI_P2P_PASSWORD_SIZE) != HDF_SUCCESS) {
1881             HDF_LOGE("%{public}s: fill psk fail!", __func__);
1882             ret = HDF_FAILURE;
1883             break;
1884         }
1885         if (FillData(&hdiP2pGroupStartedParam->passphrase, &hdiP2pGroupStartedParam->passphraseLen,
1886             groupStartedParam->passphrase, WIFI_P2P_PASSWORD_SIZE) != HDF_SUCCESS) {
1887             HDF_LOGE("%{public}s: fill passphrase fail!", __func__);
1888             ret = HDF_FAILURE;
1889             break;
1890         }
1891         if (FillData(&hdiP2pGroupStartedParam->goDeviceAddress, &hdiP2pGroupStartedParam->goDeviceAddressLen,
1892             groupStartedParam->goDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1893             HDF_LOGE("%{public}s: fill goDeviceAddress fail!", __func__);
1894             ret = HDF_FAILURE;
1895         }
1896         if (FillData(&hdiP2pGroupStartedParam->goRandomDeviceAddress,
1897             &hdiP2pGroupStartedParam->goRandomDeviceAddressLen,
1898             groupStartedParam->goRandomDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1899             HDF_LOGE("%{public}s: fill goRandomDeviceAddress fail!", __func__);
1900             ret = HDF_FAILURE;
1901         }
1902     } while (0);
1903     return ret;
1904 }
1905 
WpaFillP2pGroupInfoStartedParam(struct P2pGroupStartedParam * groupStartedParam,struct HdiP2pGroupInfoStartedParam * hdiP2pGroupStartedParam)1906 static int32_t WpaFillP2pGroupInfoStartedParam(struct P2pGroupStartedParam *groupStartedParam,
1907     struct HdiP2pGroupInfoStartedParam *hdiP2pGroupStartedParam)
1908 {
1909     int32_t ret = HDF_SUCCESS;
1910     if (groupStartedParam == NULL || hdiP2pGroupStartedParam == NULL) {
1911         HDF_LOGE("%{public}s: groupStartedParam or hdiP2pGroupStartedParam is NULL!", __func__);
1912         return HDF_ERR_INVALID_PARAM;
1913     }
1914     hdiP2pGroupStartedParam->isGo = groupStartedParam->isGo;
1915     hdiP2pGroupStartedParam->isPersistent = groupStartedParam->isPersistent;
1916     hdiP2pGroupStartedParam->frequency = groupStartedParam->frequency;
1917     ret = FillHdiP2pGroupInfoStartedParam(groupStartedParam, hdiP2pGroupStartedParam);
1918     if (ret != HDF_SUCCESS) {
1919         if (hdiP2pGroupStartedParam->groupIfName != NULL) {
1920             OsalMemFree(hdiP2pGroupStartedParam->groupIfName);
1921             hdiP2pGroupStartedParam->groupIfName = NULL;
1922         }
1923         if (hdiP2pGroupStartedParam->ssid != NULL) {
1924             OsalMemFree(hdiP2pGroupStartedParam->ssid);
1925             hdiP2pGroupStartedParam->ssid = NULL;
1926         }
1927         if (hdiP2pGroupStartedParam->psk != NULL) {
1928             OsalMemFree(hdiP2pGroupStartedParam->psk);
1929             hdiP2pGroupStartedParam->psk = NULL;
1930         }
1931         if (hdiP2pGroupStartedParam->passphrase != NULL) {
1932             OsalMemFree(hdiP2pGroupStartedParam->passphrase);
1933             hdiP2pGroupStartedParam->passphrase = NULL;
1934         }
1935         if (hdiP2pGroupStartedParam->goDeviceAddress != NULL) {
1936             OsalMemFree(hdiP2pGroupStartedParam->goDeviceAddress);
1937             hdiP2pGroupStartedParam->goDeviceAddress = NULL;
1938         }
1939         if (hdiP2pGroupStartedParam->goRandomDeviceAddress != NULL) {
1940             OsalMemFree(hdiP2pGroupStartedParam->goRandomDeviceAddress);
1941             hdiP2pGroupStartedParam->goRandomDeviceAddress = NULL;
1942         }
1943     }
1944     return ret;
1945 }
1946 
WpaFillP2pGroupRemovedParam(struct P2pGroupRemovedParam * groupRemovedParam,struct HdiP2pGroupRemovedParam * hdiP2pGroupRemovedParam)1947 static int32_t WpaFillP2pGroupRemovedParam(struct P2pGroupRemovedParam *groupRemovedParam,
1948     struct HdiP2pGroupRemovedParam *hdiP2pGroupRemovedParam)
1949 {
1950     int32_t ret = HDF_SUCCESS;
1951     if (groupRemovedParam == NULL || hdiP2pGroupRemovedParam == NULL) {
1952         HDF_LOGE("%{public}s: groupStartedParam or hdiP2pGroupRemovedParam is NULL!", __func__);
1953         return HDF_ERR_INVALID_PARAM;
1954     }
1955     hdiP2pGroupRemovedParam->isGo = groupRemovedParam->isGo;
1956 
1957     if (FillData(&hdiP2pGroupRemovedParam->groupIfName, &hdiP2pGroupRemovedParam->groupIfNameLen,
1958         groupRemovedParam->groupIfName, WIFI_P2P_GROUP_IFNAME_LENGTH) != HDF_SUCCESS) {
1959             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1960             ret = HDF_FAILURE;
1961     }
1962     if (ret != HDF_SUCCESS) {
1963         if (hdiP2pGroupRemovedParam->groupIfName != NULL) {
1964             OsalMemFree(hdiP2pGroupRemovedParam->groupIfName);
1965             hdiP2pGroupRemovedParam->groupIfName = NULL;
1966         }
1967     }
1968     return ret;
1969 }
1970 
WpaFillP2pProvisionDiscoveryCompletedParam(struct P2pProvisionDiscoveryCompletedParam * provisionDiscoveryCompletedParam,struct HdiP2pProvisionDiscoveryCompletedParam * hdiP2pProvisionDiscoveryCompletedParam)1971 static int32_t WpaFillP2pProvisionDiscoveryCompletedParam(struct P2pProvisionDiscoveryCompletedParam
1972     *provisionDiscoveryCompletedParam,
1973     struct HdiP2pProvisionDiscoveryCompletedParam *hdiP2pProvisionDiscoveryCompletedParam)
1974 {
1975     int32_t ret = HDF_SUCCESS;
1976     if (provisionDiscoveryCompletedParam == NULL || hdiP2pProvisionDiscoveryCompletedParam == NULL) {
1977         HDF_LOGE("%{public}s: provisionDiscoveryCompletedParam or hdiP2pProvisionDiscoveryCompletedParam is NULL!",
1978             __func__);
1979         return HDF_ERR_INVALID_PARAM;
1980     }
1981     hdiP2pProvisionDiscoveryCompletedParam->isRequest = provisionDiscoveryCompletedParam->isRequest;
1982     hdiP2pProvisionDiscoveryCompletedParam->provDiscStatusCode = provisionDiscoveryCompletedParam->provDiscStatusCode;
1983     hdiP2pProvisionDiscoveryCompletedParam->configMethods = provisionDiscoveryCompletedParam->configMethods;
1984 
1985     do {
1986         if (FillData(&hdiP2pProvisionDiscoveryCompletedParam->p2pDeviceAddress,
1987             &hdiP2pProvisionDiscoveryCompletedParam->p2pDeviceAddressLen,
1988             provisionDiscoveryCompletedParam->p2pDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
1989             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
1990             ret = HDF_FAILURE;
1991             break;
1992         }
1993         if (FillData(&hdiP2pProvisionDiscoveryCompletedParam->generatedPin,
1994             &hdiP2pProvisionDiscoveryCompletedParam->generatedPinLen,
1995             provisionDiscoveryCompletedParam->generatedPin, WIFI_PIN_CODE_LENGTH) != HDF_SUCCESS) {
1996             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
1997             ret = HDF_FAILURE;
1998         }
1999     } while (0);
2000 
2001     if (ret != HDF_SUCCESS) {
2002         if (hdiP2pProvisionDiscoveryCompletedParam->p2pDeviceAddress != NULL) {
2003             OsalMemFree(hdiP2pProvisionDiscoveryCompletedParam->p2pDeviceAddress);
2004             hdiP2pProvisionDiscoveryCompletedParam->p2pDeviceAddress = NULL;
2005         }
2006         if (hdiP2pProvisionDiscoveryCompletedParam->generatedPin != NULL) {
2007             OsalMemFree(hdiP2pProvisionDiscoveryCompletedParam->generatedPin);
2008             hdiP2pProvisionDiscoveryCompletedParam->generatedPin = NULL;
2009         }
2010     }
2011     return ret;
2012 }
2013 
WpaFillP2pServDiscReqParam(struct P2pServDiscReqInfoParam * servDiscReqInfo,struct HdiP2pServDiscReqInfoParam * hdiP2pServDiscReqInfo)2014 static int32_t WpaFillP2pServDiscReqParam(struct P2pServDiscReqInfoParam *servDiscReqInfo,
2015     struct HdiP2pServDiscReqInfoParam *hdiP2pServDiscReqInfo)
2016 {
2017     int32_t ret = HDF_SUCCESS;
2018     if (servDiscReqInfo == NULL || hdiP2pServDiscReqInfo == NULL) {
2019         HDF_LOGE("%{public}s: servDiscReqInfo or hdiP2pServDiscReqInfo is NULL!", __func__);
2020         return HDF_ERR_INVALID_PARAM;
2021     }
2022     hdiP2pServDiscReqInfo->freq = servDiscReqInfo->freq;
2023     hdiP2pServDiscReqInfo->dialogToken = servDiscReqInfo->dialogToken;
2024     hdiP2pServDiscReqInfo->updateIndic = servDiscReqInfo->updateIndic;
2025 
2026     do {
2027         if (FillData(&hdiP2pServDiscReqInfo->mac, &hdiP2pServDiscReqInfo->macLen,
2028             servDiscReqInfo->mac, ETH_ADDR_LEN) != HDF_SUCCESS) {
2029             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
2030             ret = HDF_FAILURE;
2031             break;
2032         }
2033         if (FillData(&hdiP2pServDiscReqInfo->tlvs, &hdiP2pServDiscReqInfo->tlvsLen,
2034             servDiscReqInfo->tlvs, WIFI_P2P_TLVS_LENGTH) != HDF_SUCCESS) {
2035             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
2036             ret = HDF_FAILURE;
2037         }
2038     } while (0);
2039 
2040     if (ret != HDF_SUCCESS) {
2041         if (hdiP2pServDiscReqInfo->mac != NULL) {
2042             OsalMemFree(hdiP2pServDiscReqInfo->mac);
2043             hdiP2pServDiscReqInfo->mac = NULL;
2044         }
2045         if (hdiP2pServDiscReqInfo->tlvs != NULL) {
2046             OsalMemFree(hdiP2pServDiscReqInfo->tlvs);
2047             hdiP2pServDiscReqInfo->tlvs = NULL;
2048         }
2049     }
2050     return ret;
2051 }
2052 
WpaFillP2pServDiscRespParam(struct P2pServDiscRespParam * servDiscRespParam,struct HdiP2pServDiscRespParam * hdiP2pServDiscRespParam)2053 static int32_t WpaFillP2pServDiscRespParam(struct P2pServDiscRespParam *servDiscRespParam,
2054     struct HdiP2pServDiscRespParam *hdiP2pServDiscRespParam)
2055 {
2056     int32_t ret = HDF_SUCCESS;
2057     if (servDiscRespParam == NULL || hdiP2pServDiscRespParam == NULL) {
2058         HDF_LOGE("%{public}s: servDiscRespParam or hdiP2pServDiscRespParam is NULL!", __func__);
2059         return HDF_ERR_INVALID_PARAM;
2060     }
2061     hdiP2pServDiscRespParam->updateIndicator = servDiscRespParam->updateIndicator;
2062 
2063     do {
2064         if (FillData(&hdiP2pServDiscRespParam->srcAddress, &hdiP2pServDiscRespParam->srcAddressLen,
2065             servDiscRespParam->srcAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
2066             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
2067             ret = HDF_FAILURE;
2068             break;
2069         }
2070         if (FillData(&hdiP2pServDiscRespParam->tlvs, &hdiP2pServDiscRespParam->tlvsLen,
2071             servDiscRespParam->tlvs, WIFI_P2P_TLVS_LENGTH) != HDF_SUCCESS) {
2072             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
2073             ret = HDF_FAILURE;
2074         }
2075     } while (0);
2076 
2077     if (ret != HDF_SUCCESS) {
2078         if (hdiP2pServDiscRespParam->srcAddress != NULL) {
2079             OsalMemFree(hdiP2pServDiscRespParam->srcAddress);
2080             hdiP2pServDiscRespParam->srcAddress = NULL;
2081         }
2082         if (hdiP2pServDiscRespParam->tlvs != NULL) {
2083             OsalMemFree(hdiP2pServDiscRespParam->tlvs);
2084             hdiP2pServDiscRespParam->tlvs = NULL;
2085         }
2086     }
2087     return ret;
2088 }
2089 
WpaFillP2pStaConnectStateParam(struct P2pStaConnectStateParam * staConnectStateParam,struct HdiP2pStaConnectStateParam * hdiP2pStaConnectStateParam)2090 static int32_t WpaFillP2pStaConnectStateParam(struct P2pStaConnectStateParam *staConnectStateParam,
2091     struct HdiP2pStaConnectStateParam *hdiP2pStaConnectStateParam)
2092 {
2093     int32_t ret = HDF_SUCCESS;
2094     if (staConnectStateParam == NULL || hdiP2pStaConnectStateParam == NULL) {
2095         HDF_LOGE("%{public}s: staConnectStateParam or hdiP2pStaConnectStateParam is NULL!", __func__);
2096         return HDF_ERR_INVALID_PARAM;
2097     }
2098     hdiP2pStaConnectStateParam->state = staConnectStateParam->state;
2099     do {
2100         if (FillData(&hdiP2pStaConnectStateParam->srcAddress, &hdiP2pStaConnectStateParam->srcAddressLen,
2101             staConnectStateParam->srcAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
2102             HDF_LOGE("%{public}s: fill bssid fail!", __func__);
2103             ret = HDF_FAILURE;
2104             break;
2105         }
2106         if (FillData(&hdiP2pStaConnectStateParam->p2pDeviceAddress, &hdiP2pStaConnectStateParam->p2pDeviceAddressLen,
2107             staConnectStateParam->p2pDeviceAddress, ETH_ADDR_LEN) != HDF_SUCCESS) {
2108             HDF_LOGE("%{public}s: fill ssid fail!", __func__);
2109             ret = HDF_FAILURE;
2110         }
2111     } while (0);
2112 
2113     if (ret != HDF_SUCCESS) {
2114         if (hdiP2pStaConnectStateParam->srcAddress != NULL) {
2115             OsalMemFree(hdiP2pStaConnectStateParam->srcAddress);
2116             hdiP2pStaConnectStateParam->srcAddress = NULL;
2117         }
2118         if (hdiP2pStaConnectStateParam->p2pDeviceAddress != NULL) {
2119             OsalMemFree(hdiP2pStaConnectStateParam->p2pDeviceAddress);
2120             hdiP2pStaConnectStateParam->p2pDeviceAddress = NULL;
2121         }
2122     }
2123     return ret;
2124 }
2125 
WpaFillP2pIfaceCreatedParam(struct P2pIfaceCreatedParam * ifaceCreatedParam,struct HdiP2pIfaceCreatedParam * hdiP2pIfaceCreatedParam)2126 static int32_t WpaFillP2pIfaceCreatedParam(struct P2pIfaceCreatedParam *ifaceCreatedParam,
2127     struct HdiP2pIfaceCreatedParam *hdiP2pIfaceCreatedParam)
2128 {
2129     int32_t ret = HDF_SUCCESS;
2130     if (ifaceCreatedParam == NULL || hdiP2pIfaceCreatedParam == NULL) {
2131         HDF_LOGE("%{public}s: ifaceCreatedParam or hdiP2pIfaceCreatedParam is NULL!", __func__);
2132         return HDF_ERR_INVALID_PARAM;
2133     }
2134     hdiP2pIfaceCreatedParam->isGo = ifaceCreatedParam->isGo;
2135     return ret;
2136 }
2137 
ProcessEventP2pDeviceFound(struct HdfWpaRemoteNode * node,struct P2pDeviceInfoParam * deviceInfoParam,const char * ifName)2138 int32_t ProcessEventP2pDeviceFound(struct HdfWpaRemoteNode *node,
2139     struct P2pDeviceInfoParam *deviceInfoParam, const char *ifName)
2140 {
2141     struct HdiP2pDeviceInfoParam hdiP2pDeviceInfo = {0};
2142     int32_t ret = HDF_FAILURE;
2143     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventDeviceFound == NULL) {
2144         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2145         return HDF_ERR_INVALID_PARAM;
2146     }
2147     if (WpaFillP2pDeviceFoundParam(deviceInfoParam, &hdiP2pDeviceInfo) != HDF_SUCCESS) {
2148         HDF_LOGE("%{public}s: hdiP2pDeviceInfo is NULL or deviceInfoParam fialed!", __func__);
2149     } else {
2150         ret = node->callbackObj->OnEventDeviceFound(node->callbackObj, &hdiP2pDeviceInfo, ifName);
2151     }
2152     HdiP2pDeviceInfoParamFree(&hdiP2pDeviceInfo, false);
2153     return ret;
2154 }
2155 
ProcessEventP2pDeviceLost(struct HdfWpaRemoteNode * node,struct P2pDeviceLostParam * deviceLostParam,const char * ifName)2156 int32_t ProcessEventP2pDeviceLost(struct HdfWpaRemoteNode *node,
2157     struct P2pDeviceLostParam *deviceLostParam, const char *ifName)
2158 {
2159     struct HdiP2pDeviceLostParam hdiP2pDeviceLostParam = {0};
2160     int32_t ret = HDF_FAILURE;
2161     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventDeviceLost == NULL) {
2162         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2163         return HDF_ERR_INVALID_PARAM;
2164     }
2165     if (WpaFillP2pDeviceLostParam(deviceLostParam, &hdiP2pDeviceLostParam) != HDF_SUCCESS) {
2166         HDF_LOGE("%{public}s: hdiP2pDeviceLostParam is NULL or deviceLostParam fialed!", __func__);
2167     } else {
2168         ret = node->callbackObj->OnEventDeviceLost(node->callbackObj, &hdiP2pDeviceLostParam, ifName);
2169     }
2170     HdiP2pDeviceLostParamFree(&hdiP2pDeviceLostParam, false);
2171     return ret;
2172 }
2173 
ProcessEventP2pGoNegotiationRequest(struct HdfWpaRemoteNode * node,struct P2pGoNegotiationRequestParam * goNegotiationRequestParam,const char * ifName)2174 int32_t ProcessEventP2pGoNegotiationRequest(struct HdfWpaRemoteNode *node,
2175     struct P2pGoNegotiationRequestParam *goNegotiationRequestParam, const char *ifName)
2176 {
2177     struct HdiP2pGoNegotiationRequestParam hdiP2pGoNegotiationRequestParam = {0};
2178     int32_t ret = HDF_FAILURE;
2179     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventGoNegotiationRequest == NULL) {
2180         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2181         return HDF_ERR_INVALID_PARAM;
2182     }
2183     if (WpaFillP2pGoNegotiationRequestParam(goNegotiationRequestParam,
2184         &hdiP2pGoNegotiationRequestParam) != HDF_SUCCESS) {
2185         HDF_LOGE("%{public}s: hdiP2pGoNegotiationRequestParam is NULL or goNegotiationRequestParam fialed!", __func__);
2186     } else {
2187         ret = node->callbackObj->OnEventGoNegotiationRequest(node->callbackObj,
2188             &hdiP2pGoNegotiationRequestParam, ifName);
2189     }
2190     HdiP2pGoNegotiationRequestParamFree(&hdiP2pGoNegotiationRequestParam, false);
2191     return ret;
2192 }
2193 
ProcessEventP2pGoNegotiationCompleted(struct HdfWpaRemoteNode * node,struct P2pGoNegotiationCompletedParam * goNegotiationCompletedParam,const char * ifName)2194 int32_t ProcessEventP2pGoNegotiationCompleted(struct HdfWpaRemoteNode *node, struct P2pGoNegotiationCompletedParam
2195     *goNegotiationCompletedParam, const char *ifName)
2196 {
2197     struct HdiP2pGoNegotiationCompletedParam hdiP2pGoNegotiationCompletedParam = {0};
2198     int32_t ret = HDF_FAILURE;
2199     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventGoNegotiationCompleted == NULL) {
2200         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2201         return HDF_ERR_INVALID_PARAM;
2202     }
2203     if (WpaFillP2pGoNegotiationCompletedParam(goNegotiationCompletedParam,
2204         &hdiP2pGoNegotiationCompletedParam) != HDF_SUCCESS) {
2205         HDF_LOGE("%{public}s: hdiP2pGoNegotiationCompletedParam is NULL or goNegotiationCompletedParam fialed!",
2206             __func__);
2207     } else {
2208         ret = node->callbackObj->OnEventGoNegotiationCompleted(node->callbackObj,
2209             &hdiP2pGoNegotiationCompletedParam, ifName);
2210     }
2211     HdiP2pGoNegotiationCompletedParamFree(&hdiP2pGoNegotiationCompletedParam, false);
2212     return ret;
2213 }
2214 
ProcessEventP2pInvitationReceived(struct HdfWpaRemoteNode * node,struct P2pInvitationReceivedParam * invitationReceivedParam,const char * ifName)2215 int32_t ProcessEventP2pInvitationReceived(struct HdfWpaRemoteNode *node,
2216     struct P2pInvitationReceivedParam *invitationReceivedParam, const char *ifName)
2217 {
2218     struct HdiP2pInvitationReceivedParam hdiP2pInvitationReceivedParam = {0};
2219     int32_t ret = HDF_FAILURE;
2220     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventInvitationReceived == NULL) {
2221         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2222         return HDF_ERR_INVALID_PARAM;
2223     }
2224     if (WpaFillP2pInvitationReceivedParam(invitationReceivedParam, &hdiP2pInvitationReceivedParam) != HDF_SUCCESS) {
2225         HDF_LOGE("%{public}s: hdiP2pInvitationReceivedParam is NULL or invitationReceivedParam fialed!", __func__);
2226         return ret;
2227     } else {
2228         ret = node->callbackObj->OnEventInvitationReceived(node->callbackObj, &hdiP2pInvitationReceivedParam, ifName);
2229     }
2230     HdiP2pInvitationReceivedParamFree(&hdiP2pInvitationReceivedParam, false);
2231     return ret;
2232 }
2233 
ProcessEventP2pInvitationResult(struct HdfWpaRemoteNode * node,struct P2pInvitationResultParam * invitationResultParam,const char * ifName)2234 int32_t ProcessEventP2pInvitationResult(struct HdfWpaRemoteNode *node,
2235     struct P2pInvitationResultParam *invitationResultParam, const char *ifName)
2236 {
2237     struct HdiP2pInvitationResultParam hdiP2pInvitationResultParam = {0};
2238     int32_t ret = HDF_FAILURE;
2239     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventInvitationResult == NULL) {
2240         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2241         return HDF_ERR_INVALID_PARAM;
2242     }
2243     if (WpaFillP2pInvitationResultParam(invitationResultParam, &hdiP2pInvitationResultParam) != HDF_SUCCESS) {
2244         HDF_LOGE("%{public}s: hdiP2pInvitationResultParam is NULL or invitationResultParam fialed!", __func__);
2245         return ret;
2246     } else {
2247         ret = node->callbackObj->OnEventInvitationResult(node->callbackObj, &hdiP2pInvitationResultParam, ifName);
2248     }
2249     HdiP2pInvitationResultParamFree(&hdiP2pInvitationResultParam, false);
2250     return ret;
2251 }
2252 
ProcessEventP2pGroupFormationSuccess(struct HdfWpaRemoteNode * node,const char * ifName)2253 int32_t ProcessEventP2pGroupFormationSuccess(struct HdfWpaRemoteNode *node,
2254     const char *ifName)
2255 {
2256     int32_t ret = HDF_FAILURE;
2257     if (node == NULL || node->callbackObj == NULL) {
2258         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2259         return HDF_ERR_INVALID_PARAM;
2260     }
2261     ret = node->callbackObj->OnEventGroupFormationSuccess(node->callbackObj, ifName);
2262     return ret;
2263 }
2264 
ProcessEventP2pGroupFormationFailure(struct HdfWpaRemoteNode * node,char * reason,const char * ifName)2265 int32_t ProcessEventP2pGroupFormationFailure(struct HdfWpaRemoteNode *node, char *reason,
2266     const char *ifName)
2267 {
2268     char *hdiReason = NULL;
2269     int32_t ret = HDF_FAILURE;
2270     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventGroupFormationFailure == NULL ||
2271         reason == NULL) {
2272         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2273         return HDF_ERR_INVALID_PARAM;
2274     }
2275     hdiReason = (char *)OsalMemCalloc(WIFI_REASON_LENGTH);
2276     if ((hdiReason == NULL) || (strncpy_s(hdiReason, WIFI_REASON_LENGTH, reason, strlen(reason)) != HDF_SUCCESS)) {
2277         HDF_LOGE("%{public}s: hdiReason is NULL or reason fialed!", __func__);
2278     } else {
2279         ret = node->callbackObj->OnEventGroupFormationFailure(node->callbackObj, hdiReason, ifName);
2280     }
2281     if (hdiReason) {
2282         OsalMemFree(hdiReason);
2283         hdiReason = NULL;
2284     }
2285     return ret;
2286 }
2287 
ProcessEventP2pGroupStarted(struct HdfWpaRemoteNode * node,struct P2pGroupStartedParam * groupStartedParam,const char * ifName)2288 int32_t ProcessEventP2pGroupStarted(struct HdfWpaRemoteNode *node,
2289     struct P2pGroupStartedParam *groupStartedParam, const char *ifName)
2290 {
2291     struct HdiP2pGroupInfoStartedParam hdiP2pGroupInfoStartedParam = {0};
2292     int32_t ret = HDF_FAILURE;
2293     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventGroupInfoStarted == NULL) {
2294         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2295         return HDF_ERR_INVALID_PARAM;
2296     }
2297     if (WpaFillP2pGroupInfoStartedParam(groupStartedParam, &hdiP2pGroupInfoStartedParam) != HDF_SUCCESS) {
2298         HDF_LOGE("%{public}s: hdiP2pGroupStartedParam is NULL or groupStartedParam fialed!", __func__);
2299     } else {
2300         ret = node->callbackObj->OnEventGroupInfoStarted(node->callbackObj, &hdiP2pGroupInfoStartedParam, ifName);
2301     }
2302     HdiP2pGroupInfoStartedParamFree(&hdiP2pGroupInfoStartedParam, false);
2303     return ret;
2304 }
2305 
ProcessEventP2pGroupRemoved(struct HdfWpaRemoteNode * node,struct P2pGroupRemovedParam * groupRemovedParam,const char * ifName)2306 int32_t ProcessEventP2pGroupRemoved(struct HdfWpaRemoteNode *node,
2307     struct P2pGroupRemovedParam *groupRemovedParam, const char *ifName)
2308 {
2309     struct HdiP2pGroupRemovedParam hdiP2pGroupRemovedParam = {0};
2310     int32_t ret = HDF_FAILURE;
2311     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventGroupRemoved == NULL) {
2312         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2313         return HDF_ERR_INVALID_PARAM;
2314     }
2315     if (WpaFillP2pGroupRemovedParam(groupRemovedParam, &hdiP2pGroupRemovedParam) != HDF_SUCCESS) {
2316         HDF_LOGE("%{public}s: hdiP2pGroupRemovedParam is NULL or groupRemovedParam fialed!", __func__);
2317     } else {
2318         ret = node->callbackObj->OnEventGroupRemoved(node->callbackObj, &hdiP2pGroupRemovedParam, ifName);
2319     }
2320     HdiP2pGroupRemovedParamFree(&hdiP2pGroupRemovedParam, false);
2321     return ret;
2322 }
2323 
ProcessEventP2pProvisionDiscoveryCompleted(struct HdfWpaRemoteNode * node,struct P2pProvisionDiscoveryCompletedParam * provisionDiscoveryCompletedParam,const char * ifName)2324 int32_t ProcessEventP2pProvisionDiscoveryCompleted(struct HdfWpaRemoteNode *node,
2325     struct P2pProvisionDiscoveryCompletedParam *provisionDiscoveryCompletedParam, const char *ifName)
2326 {
2327     struct HdiP2pProvisionDiscoveryCompletedParam hdiP2pProvisionDiscoveryCompletedParam = {0};
2328     int32_t ret = HDF_FAILURE;
2329     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventProvisionDiscoveryCompleted == NULL) {
2330         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2331         return HDF_ERR_INVALID_PARAM;
2332     }
2333     if (WpaFillP2pProvisionDiscoveryCompletedParam(provisionDiscoveryCompletedParam,
2334         &hdiP2pProvisionDiscoveryCompletedParam) != HDF_SUCCESS) {
2335         HDF_LOGE("%{public}s: Param is NULL or provisionDiscoveryCompletedParam fialed!", __func__);
2336     } else {
2337         ret = node->callbackObj->OnEventProvisionDiscoveryCompleted(node->callbackObj,
2338             &hdiP2pProvisionDiscoveryCompletedParam, ifName);
2339     }
2340     HdiP2pProvisionDiscoveryCompletedParamFree(&hdiP2pProvisionDiscoveryCompletedParam, false);
2341     return ret;
2342 }
2343 
ProcessEventP2pFindStopped(struct HdfWpaRemoteNode * node,const char * ifName)2344 int32_t ProcessEventP2pFindStopped(struct HdfWpaRemoteNode *node,
2345      const char *ifName)
2346 {
2347     int32_t ret = HDF_FAILURE;
2348     if (node == NULL || node->callbackObj == NULL) {
2349         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2350         return HDF_ERR_INVALID_PARAM;
2351     }
2352     ret = node->callbackObj->OnEventFindStopped(node->callbackObj, ifName);
2353     return ret;
2354 }
2355 
ProcessEventP2pServDiscReq(struct HdfWpaRemoteNode * node,struct P2pServDiscReqInfoParam * servDiscReqInfo,const char * ifName)2356 int32_t ProcessEventP2pServDiscReq(struct HdfWpaRemoteNode *node,
2357     struct P2pServDiscReqInfoParam *servDiscReqInfo, const char *ifName)
2358 {
2359     struct HdiP2pServDiscReqInfoParam hdiP2pServDiscReqInfo = {0};
2360     int32_t ret = HDF_FAILURE;
2361     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventServDiscReq == NULL) {
2362         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2363         return HDF_ERR_INVALID_PARAM;
2364     }
2365     if (WpaFillP2pServDiscReqParam(servDiscReqInfo, &hdiP2pServDiscReqInfo) != HDF_SUCCESS) {
2366         HDF_LOGE("%{public}s: hdiP2pServDiscReqInfo is NULL or servDiscReqInfo fialed!", __func__);
2367     } else {
2368         ret = node->callbackObj->OnEventServDiscReq(node->callbackObj, &hdiP2pServDiscReqInfo, ifName);
2369     }
2370     HdiP2pServDiscReqInfoParamFree(&hdiP2pServDiscReqInfo, false);
2371     return ret;
2372 }
2373 
ProcessEventP2pServDiscResp(struct HdfWpaRemoteNode * node,struct P2pServDiscRespParam * servDiscRespParam,const char * ifName)2374 int32_t ProcessEventP2pServDiscResp(struct HdfWpaRemoteNode *node,
2375     struct P2pServDiscRespParam *servDiscRespParam, const char *ifName)
2376 {
2377     struct HdiP2pServDiscRespParam hdiP2pServDiscRespParam = {0};
2378     int32_t ret = HDF_FAILURE;
2379     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventServDiscResp == NULL) {
2380         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2381         return HDF_ERR_INVALID_PARAM;
2382     }
2383     if (WpaFillP2pServDiscRespParam(servDiscRespParam, &hdiP2pServDiscRespParam) != HDF_SUCCESS) {
2384         HDF_LOGE("%{public}s: hdiP2pServDiscRespParam is NULL or servDiscRespParam fialed!", __func__);
2385     } else {
2386         ret = node->callbackObj->OnEventServDiscResp(node->callbackObj, &hdiP2pServDiscRespParam, ifName);
2387     }
2388     HdiP2pServDiscRespParamFree(&hdiP2pServDiscRespParam, false);
2389     return ret;
2390 }
2391 
ProcessEventP2pStaConnectState(struct HdfWpaRemoteNode * node,struct P2pStaConnectStateParam * staConnectStateParam,const char * ifName)2392 int32_t ProcessEventP2pStaConnectState(struct HdfWpaRemoteNode *node,
2393     struct P2pStaConnectStateParam *staConnectStateParam, const char *ifName)
2394 {
2395     struct HdiP2pStaConnectStateParam hdiP2pStaConnectStateParam = {0};
2396     int32_t ret = HDF_FAILURE;
2397     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventStaConnectState == NULL) {
2398         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2399         return HDF_ERR_INVALID_PARAM;
2400     }
2401     if (WpaFillP2pStaConnectStateParam(staConnectStateParam, &hdiP2pStaConnectStateParam) != HDF_SUCCESS) {
2402         HDF_LOGE("%{public}s: hdiP2pStaConnectStateParam is NULL or staConnectStateParam fialed!", __func__);
2403     } else {
2404         ret = node->callbackObj->OnEventStaConnectState(node->callbackObj, &hdiP2pStaConnectStateParam, ifName);
2405     }
2406     HdiP2pStaConnectStateParamFree(&hdiP2pStaConnectStateParam, false);
2407     return ret;
2408 }
2409 
ProcessEventP2pIfaceCreated(struct HdfWpaRemoteNode * node,struct P2pIfaceCreatedParam * ifaceCreatedParam,const char * ifName)2410 int32_t ProcessEventP2pIfaceCreated(struct HdfWpaRemoteNode *node, struct P2pIfaceCreatedParam *ifaceCreatedParam,
2411     const char *ifName)
2412 {
2413     struct HdiP2pIfaceCreatedParam hdiP2pIfaceCreatedParam = {0};
2414     int32_t ret = HDF_FAILURE;
2415     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventIfaceCreated == NULL) {
2416         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
2417         return HDF_ERR_INVALID_PARAM;
2418     }
2419     if (WpaFillP2pIfaceCreatedParam(ifaceCreatedParam, &hdiP2pIfaceCreatedParam) != HDF_SUCCESS) {
2420         HDF_LOGE("%{public}s: hdiP2pIfaceCreatedParam is NULL or ifaceCreatedParam fialed!", __func__);
2421     } else {
2422         ret = node->callbackObj->OnEventIfaceCreated(node->callbackObj, &hdiP2pIfaceCreatedParam, ifName);
2423     }
2424     HdiP2pIfaceCreatedParamFree(&hdiP2pIfaceCreatedParam, false);
2425     return ret;
2426 }
2427