• 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 "hostapd_common_cmd.h"
16 #include <securec.h>
17 #include <hdf_base.h>
18 #include <errno.h>
19 #include <hdf_log.h>
20 #include <osal_time.h>
21 #include <osal_mem.h>
22 #include "v1_0/ihostapd_callback.h"
23 #include "v1_0/ihostapd_interface.h"
24 #include "ap/ap_config.h"
25 #include "ap/hostapd.h"
26 #include "ap_ctrl_iface.h"
27 #include "ap/ctrl_iface_ap.h"
28 #include "ctrl_iface.h"
29 #include "ap_main.h"
30 #include "hostapd_client.h"
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <dlfcn.h>
34 #include <string.h>
35 #include "eap_server/eap_methods.h"
36 
37 pthread_t g_tid;
38 
HdfHostapdStubDriver(void)39 struct HdfHostapdStubData *HdfHostapdStubDriver(void)
40 {
41     static struct HdfHostapdStubData registerManager;
42     return &registerManager;
43 }
44 
SplitCmdString(const char * startCmd,struct StApMainParam * pParam)45 static void SplitCmdString(const char *startCmd, struct StApMainParam *pParam)
46 {
47     if (pParam == NULL) {
48         return;
49     }
50     if (startCmd == NULL) {
51         pParam->argc = 0;
52         return;
53     }
54     const char *p = startCmd;
55     int i = 0;
56     int j = 0;
57     while (*p != '\0') {
58         if (*p == ' ') {
59             if (j <= MAX_WPA_MAIN_ARGV_LEN - 1) {
60                 pParam->argv[i][j] = '\0';
61             } else {
62                 pParam->argv[i][MAX_WPA_MAIN_ARGV_LEN - 1] = '\0';
63             }
64             ++i;
65             j = 0;
66             if (i >= MAX_WPA_MAIN_ARGC_NUM) {
67                 break;
68             }
69         } else {
70             if (j < MAX_WPA_MAIN_ARGV_LEN - 1) {
71                 pParam->argv[i][j] = *p;
72                 ++j;
73             }
74         }
75         ++p;
76     }
77     if (i >= MAX_WPA_MAIN_ARGC_NUM) {
78         pParam->argc = MAX_WPA_MAIN_ARGC_NUM;
79     } else {
80         pParam->argc = i + 1;
81     }
82     return;
83 }
84 
ApThreadMain(void * p)85 static void *ApThreadMain(void *p)
86 {
87     const char *startCmd;
88     struct StApMainParam param = {0};
89     char *tmpArgv[MAX_WPA_MAIN_ARGC_NUM] = {0};
90 
91     if (p == NULL) {
92         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
93         return NULL;
94     }
95     startCmd = (const char *)p;
96     HDF_LOGE("%{public}s: startCmd: %{public}s", __func__, startCmd);
97     SplitCmdString(startCmd, &param);
98     for (int i = 0; i < param.argc; i++) {
99         tmpArgv[i] = param.argv[i];
100         HDF_LOGE("%{public}s: tmpArgv[%{public}d]: %{public}s", __func__, i, tmpArgv[i]);
101     }
102     int ret = ap_main(param.argc, tmpArgv);
103     HDF_LOGI("%{public}s: run ap_main ret:%{public}d.", __func__, ret);
104     return NULL;
105 }
106 
StartApMain(const char * moduleName,const char * startCmd)107 static int32_t StartApMain(const char *moduleName, const char *startCmd)
108 {
109     int32_t ret;
110 
111     if (moduleName == NULL || startCmd == NULL) {
112         HDF_LOGE("%{public}s input parameter invalid!", __func__);
113         return HDF_ERR_INVALID_PARAM ;
114     }
115     ret = pthread_create(&g_tid, NULL, ApThreadMain, (void *)startCmd);
116     if (ret != HDF_SUCCESS) {
117         HDF_LOGE("%{public}s: Create Ap thread failed, error code: %{public}d", __func__, ret);
118         return HDF_FAILURE;
119     }
120     pthread_setname_np(g_tid, "ApMainThread");
121     HDF_LOGE("%{public}s: pthread_create ID: %{public}p.", __func__, (void*)g_tid);
122     usleep(WPA_SLEEP_TIME);
123     return HDF_SUCCESS;
124 }
125 
HostapdInterfaceEnableAp(struct IHostapdInterface * self,const char * ifName,int32_t id)126 int32_t HostapdInterfaceEnableAp(struct IHostapdInterface *self, const char *ifName,
127     int32_t id)
128 {
129     struct hostapd_data *hostApd;
130     int32_t ret = HDF_FAILURE;
131 
132     (void)self;
133     if (ifName == NULL) {
134         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
135         return HDF_ERR_INVALID_PARAM;
136     }
137     hostApd = getHostapd();
138     if (hostApd == NULL) {
139         HDF_LOGE("%{public}s hostApd is null.", __func__);
140         return HDF_FAILURE;
141     }
142     ret = hostapd_ctrl_iface_enable(hostApd->iface);
143     if (ret != HDF_SUCCESS) {
144         HDF_LOGE("%{public}s: Enable Ap failed!", __func__);
145         return HDF_FAILURE;
146     }
147     return HDF_SUCCESS;
148 }
149 
HostapdInterfaceDisableAp(struct IHostapdInterface * self,const char * ifName,int32_t id)150 int32_t HostapdInterfaceDisableAp(struct IHostapdInterface *self, const char *ifName,
151     int32_t id)
152 {
153     struct hostapd_data *hostApd;
154     int32_t ret = HDF_FAILURE;
155 
156     (void)self;
157     if (ifName == NULL) {
158         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
159         return HDF_ERR_INVALID_PARAM;
160     }
161     hostApd = getHostapd();
162     if (hostApd == NULL) {
163         HDF_LOGE("%{public}s hostApd is null.", __func__);
164         return HDF_FAILURE;
165     }
166     ret = hostapd_ctrl_iface_disable(hostApd->iface);
167     if (ret != HDF_SUCCESS) {
168         HDF_LOGE("%{public}s: Disable Ap failed!", __func__);
169         return HDF_FAILURE;
170     }
171     return HDF_SUCCESS;
172 }
173 
HostapdInterfaceStartAp(struct IHostapdInterface * self)174 int32_t HostapdInterfaceStartAp(struct IHostapdInterface *self)
175 {
176     int32_t ret;
177 
178     (void)self;
179     ret = StartApMain(WPA_HOSTAPD_NAME, START_CMD);
180     if (ret != HDF_SUCCESS) {
181         HDF_LOGE("%{public}s: StartHostapd failed, error code: %{public}d", __func__, ret);
182         return HDF_FAILURE;
183     }
184     HDF_LOGE("%{public}s: hostapd start successfully!", __func__);
185     return HDF_SUCCESS;
186 }
187 
HostapdInterfaceStopAp(struct IHostapdInterface * self)188 int32_t HostapdInterfaceStopAp(struct IHostapdInterface *self)
189 {
190     HDF_LOGI("%{public}s: enter HostapdInterfaceStopAp stop...", __func__);
191     struct hostapd_data *hostApd;
192 
193     (void)self;
194     hostApd = getHostapd();
195     if (hostApd == NULL) {
196         HDF_LOGE("%{public}s hostApd is null.", __func__);
197         return HDF_FAILURE;
198     }
199     eap_server_unregister_methods();
200     hostapd_ctrl_iface_deinit(hostApd);
201     HDF_LOGI("%{public}s: hostapd stop successfully!", __func__);
202     return HDF_SUCCESS;
203 }
204 
HostapdInterfaceSetApPasswd(struct IHostapdInterface * self,const char * ifName,const char * pass,int32_t id)205 int32_t HostapdInterfaceSetApPasswd(struct IHostapdInterface *self, const char *ifName,
206     const char *pass, int32_t id)
207 {
208     struct hostapd_data *hostApd;
209     char cmd[CMD_SIZE] = {0};
210     int32_t ret = HDF_FAILURE;
211 
212     (void)self;
213     if (ifName == NULL || pass == NULL) {
214         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
215         return HDF_ERR_INVALID_PARAM;
216     }
217     hostApd = getHostapd();
218     if (hostApd == NULL) {
219         HDF_LOGE("%{public}s hostApd is null.", __func__);
220         return HDF_FAILURE;
221     }
222     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "wpa_passphrase %s", pass);
223     if (ret < EOK) {
224         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
225         return HDF_FAILURE;
226     }
227     ret = hostapd_ctrl_iface_set(hostApd, cmd);
228     if (ret != HDF_SUCCESS) {
229         HDF_LOGE("%{public}s: Hostapd failed to set password!", __func__);
230         return HDF_FAILURE;
231     }
232     return HDF_SUCCESS;
233 }
234 
HostapdInterfaceSetApName(struct IHostapdInterface * self,const char * ifName,const char * name,int32_t id)235 int32_t HostapdInterfaceSetApName(struct IHostapdInterface *self, const char *ifName,
236     const char *name, int32_t id)
237 {
238     struct hostapd_data *hostApd;
239     char cmd[CMD_SIZE] = {0};
240     int32_t ret = HDF_FAILURE;
241 
242     (void)self;
243     if (ifName == NULL || name == NULL) {
244         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
245         return HDF_ERR_INVALID_PARAM;
246     }
247     hostApd = getHostapd();
248     if (hostApd == NULL) {
249         HDF_LOGE("%{public}s hostApd is null.", __func__);
250         return HDF_FAILURE;
251     }
252     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "ssid %s", name);
253     if (ret < EOK) {
254         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
255         return HDF_FAILURE;
256     }
257     ret = hostapd_ctrl_iface_set(hostApd, cmd);
258     if (ret != HDF_SUCCESS) {
259         HDF_LOGE("%{public}s: Hostapd failed to set name!", __func__);
260         return HDF_FAILURE;
261     }
262     return HDF_SUCCESS;
263 }
264 
HostapdInterfaceSetApWpaValue(struct IHostapdInterface * self,const char * ifName,int32_t securityType,int32_t id)265 int32_t HostapdInterfaceSetApWpaValue(struct IHostapdInterface *self, const char *ifName,
266     int32_t securityType, int32_t id)
267 {
268     struct hostapd_data *hostApd;
269     char cmd[CMD_SIZE] = {0};
270     int32_t ret = HDF_FAILURE;
271 
272     (void)self;
273     if (ifName == NULL) {
274         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
275         return HDF_ERR_INVALID_PARAM;
276     }
277     hostApd = getHostapd();
278     if (hostApd == NULL) {
279         HDF_LOGE("%{public}s hostApd == NULL", __func__);
280         return HDF_FAILURE;
281     }
282     switch (securityType) {
283         case NONE:
284             // The authentication mode is NONE.
285             ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "wpa 0");
286             break;
287         case WPA_PSK:
288             // The authentication mode is WPA-PSK.
289             ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "wpa 1");
290             break;
291         case WPA2_PSK:
292             // The authentication mode is WPA2-PSK.
293             ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "wpa 2");
294             break;
295         default:
296             HDF_LOGE("Unknown encryption type!");
297             return ret;
298     }
299     if (ret < EOK) {
300         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, Type = %{public}d", __func__, cmd, securityType);
301         return HDF_FAILURE;
302     }
303     ret = hostapd_ctrl_iface_set(hostApd, cmd);
304     if (ret == 0 && securityType != NONE) {
305         /*
306          * If the value of wpa is switched between 0, 1, and 2, the wpa_key_mgmt,
307          * wpa_pairwise, and rsn_pairwise attributes must be set. Otherwise, the
308          * enable or STA cannot be connected.
309          */
310         strcpy_s(cmd, sizeof(cmd), "wpa_key_mgmt WPA-PSK");
311         ret = hostapd_ctrl_iface_set(hostApd, cmd);
312     }
313     if (ret == 0 && securityType == WPA_PSK) {
314         strcpy_s(cmd, sizeof(cmd), "wpa_pairwise CCMP");
315         ret = hostapd_ctrl_iface_set(hostApd, cmd);
316     }
317     if (ret == 0 && securityType == WPA2_PSK) {
318         strcpy_s(cmd, sizeof(cmd), "rsn_pairwise CCMP");
319         ret = hostapd_ctrl_iface_set(hostApd, cmd);
320     }
321     if (ret != HDF_SUCCESS) {
322         HDF_LOGE("%{public}s: Hostapd failed to set securityType, Type = %{public}d.", __func__, securityType);
323         return HDF_FAILURE;
324     }
325     HDF_LOGI("%{public}s:set securityType successfully, Type = %{public}d.", __func__, securityType);
326     return HDF_SUCCESS;
327 }
328 
HostapdInterfaceSetApBand(struct IHostapdInterface * self,const char * ifName,int32_t band,int32_t id)329 int32_t HostapdInterfaceSetApBand(struct IHostapdInterface *self, const char *ifName,
330     int32_t band, int32_t id)
331 {
332     struct hostapd_data *hostApd;
333     char cmd[CMD_SIZE] = {0};
334     const char *hwMode = NULL;
335     int32_t ret = HDF_FAILURE;
336 
337     (void)self;
338     if (ifName == NULL) {
339         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
340         return HDF_ERR_INVALID_PARAM;
341     }
342     hostApd = getHostapd();
343     if (hostApd == NULL) {
344         HDF_LOGE("%{public}s hostApd is null.", __func__);
345         return HDF_FAILURE;
346     }
347     switch (band) {
348         case AP_NONE_BAND:
349             /* Unknown frequency band. */
350             hwMode = "any";
351             break;
352         case AP_2GHZ_BAND:
353             /* BAND_2_4_GHZ. */
354             hwMode = "g";
355             break;
356         case AP_5GHZ_BAND:
357             /* BAND_5_GHZ. */
358             hwMode = "a";
359             break;
360         default:
361             HDF_LOGE("Invalid band!");
362             return HDF_FAILURE;
363         }
364     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "hw_mode %s", hwMode);
365     if (ret < EOK) {
366         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
367         return HDF_FAILURE;
368     }
369     ret = hostapd_ctrl_iface_set(hostApd, cmd);
370     if (ret != HDF_SUCCESS) {
371         HDF_LOGE("%{public}s: Hostapd failed to set AP bandwith!", __func__);
372         return HDF_FAILURE;
373     }
374     return HDF_SUCCESS;
375 }
376 
HostapdInterfaceSetAp80211n(struct IHostapdInterface * self,const char * ifName,int32_t value,int32_t id)377 int32_t HostapdInterfaceSetAp80211n(struct IHostapdInterface *self, const char *ifName,
378     int32_t value, int32_t id)
379 {
380     struct hostapd_data *hostApd;
381     char cmd[CMD_SIZE] = {0};
382     int32_t ret = HDF_FAILURE;
383 
384     (void)self;
385     if (ifName == NULL) {
386         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
387         return HDF_ERR_INVALID_PARAM;
388     }
389     hostApd = getHostapd();
390     if (hostApd == NULL) {
391         HDF_LOGE("%{public}s hostApd is null.", __func__);
392         return HDF_FAILURE;
393     }
394     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "ieee80211n %d", value);
395     if (ret < EOK) {
396         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
397         return HDF_FAILURE;
398     }
399     ret = hostapd_ctrl_iface_set(hostApd, cmd);
400     if (ret != HDF_SUCCESS) {
401         HDF_LOGE("%{public}s: Hostapd failed to set Ap80211n!", __func__);
402         return HDF_FAILURE;
403     }
404     return HDF_SUCCESS;
405 }
406 
HostapdInterfaceSetApWmm(struct IHostapdInterface * self,const char * ifName,int32_t value,int32_t id)407 int32_t HostapdInterfaceSetApWmm(struct IHostapdInterface *self, const char *ifName,
408     int32_t value, int32_t id)
409 {
410     struct hostapd_data *hostApd;
411     char cmd[CMD_SIZE] = {0};
412     int32_t ret = HDF_FAILURE;
413 
414     (void)self;
415     if (ifName == NULL) {
416         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
417         return HDF_ERR_INVALID_PARAM;
418     }
419     hostApd = getHostapd();
420     if (hostApd == NULL) {
421         HDF_LOGE("%{public}s hostApd is null.", __func__);
422         return HDF_FAILURE;
423     }
424     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "wmm_enabled %d", value);
425     if (ret < EOK) {
426         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
427         return HDF_FAILURE;
428     }
429     ret = hostapd_ctrl_iface_set(hostApd, cmd);
430     if (ret != HDF_SUCCESS) {
431         HDF_LOGE("%{public}s: Hostapd failed to set ApWmm!", __func__);
432         return HDF_FAILURE;
433     }
434     return HDF_SUCCESS;
435 }
436 
HostapdInterfaceSetApChannel(struct IHostapdInterface * self,const char * ifName,int32_t channel,int32_t id)437 int32_t HostapdInterfaceSetApChannel(struct IHostapdInterface *self, const char *ifName,
438     int32_t channel, int32_t id)
439 {
440     struct hostapd_data *hostApd;
441     char cmd[CMD_SIZE] = {0};
442     int32_t ret = HDF_FAILURE;
443 
444     (void)self;
445     if (ifName == NULL) {
446         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
447         return HDF_ERR_INVALID_PARAM;
448     }
449     hostApd = getHostapd();
450     if (hostApd == NULL) {
451         HDF_LOGE("%{public}s hostApd is null.", __func__);
452         return HDF_FAILURE;
453     }
454     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "channel %d", channel);
455     if (ret < EOK) {
456         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
457         return HDF_FAILURE;
458     }
459     ret = hostapd_ctrl_iface_set(hostApd, cmd);
460     if (ret != HDF_SUCCESS) {
461         HDF_LOGE("%{public}s: Hostapd failed to set ApWmm!", __func__);
462         return HDF_FAILURE;
463     }
464     return HDF_SUCCESS;
465 }
466 
HostapdInterfaceSetApMaxConn(struct IHostapdInterface * self,const char * ifName,int32_t maxConn,int32_t id)467 int32_t HostapdInterfaceSetApMaxConn(struct IHostapdInterface *self, const char *ifName,
468     int32_t maxConn, int32_t id)
469 {
470     struct hostapd_data *hostApd;
471     char cmd[CMD_SIZE] = {0};
472     int32_t ret = HDF_FAILURE;
473 
474     (void)self;
475     if (ifName == NULL) {
476         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
477         return HDF_ERR_INVALID_PARAM;
478     }
479     hostApd = getHostapd();
480     if (hostApd == NULL) {
481         HDF_LOGE("%{public}s hostApd is null.", __func__);
482         return HDF_FAILURE;
483     }
484     ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "max_num_sta %d", maxConn);
485     if (ret < EOK) {
486         HDF_LOGE("%{public}s snprintf_s failed, cmd: %{public}s, ret = %{public}d", __func__, cmd, ret);
487         return HDF_FAILURE;
488     }
489     ret = hostapd_ctrl_iface_set(hostApd, cmd);
490     if (ret != HDF_SUCCESS) {
491         HDF_LOGE("%{public}s: Hostapd failed to set ApWmm!", __func__);
492         return HDF_FAILURE;
493     }
494     return HDF_SUCCESS;
495 }
496 
HostapdInterfaceSetMacFilter(struct IHostapdInterface * self,const char * ifName,const char * mac,int32_t id)497 int32_t HostapdInterfaceSetMacFilter(struct IHostapdInterface *self, const char *ifName,
498     const char *mac, int32_t id)
499 {
500     struct hostapd_data *hostApd;
501 
502     (void)self;
503     if (ifName == NULL || mac == NULL) {
504         HDF_LOGE("%{public}s: SetMacFilter or ifName is NULL!", __func__);
505         return HDF_ERR_INVALID_PARAM;
506     }
507     hostApd = getHostapd();
508     if (hostApd == NULL) {
509         HDF_LOGE("%{public}s hostApd is null.", __func__);
510         return HDF_FAILURE;
511     }
512     if (!hostapd_ctrl_iface_acl_add_mac(
513         &hostApd->conf->deny_mac, &hostApd->conf->num_deny_mac, mac)) {
514         hostapd_disassoc_deny_mac(hostApd);
515         HDF_LOGE("%{public}s: Hostapd add mac success!", __func__);
516     } else {
517         HDF_LOGE("%{public}s: Hostapd failed to add mac!", __func__);
518         return HDF_FAILURE;
519     }
520     return HDF_SUCCESS;
521 }
522 
HostapdInterfaceDelMacFilter(struct IHostapdInterface * self,const char * ifName,const char * mac,int32_t id)523 int32_t HostapdInterfaceDelMacFilter(struct IHostapdInterface *self, const char *ifName,
524     const char *mac, int32_t id)
525 {
526     struct hostapd_data *hostApd;
527 
528     (void)self;
529     if (ifName == NULL || mac == NULL) {
530         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
531         return HDF_ERR_INVALID_PARAM;
532     }
533     hostApd = getHostapd();
534     if (hostApd == NULL) {
535         HDF_LOGE("%{public}s hostApd is null.", __func__);
536         return HDF_FAILURE;
537     }
538     if (hostapd_ctrl_iface_acl_del_mac(
539         &hostApd->conf->deny_mac, &hostApd->conf->num_deny_mac, mac)) {
540         HDF_LOGE("%{public}s: Hostapd failed to delete the mac!", __func__);
541         return HDF_FAILURE;
542     }
543     return HDF_SUCCESS;
544 }
545 
HostapdInterfaceGetStaInfos(struct IHostapdInterface * self,const char * ifName,char * buf,uint32_t bufLen,int32_t size,int32_t id)546 int32_t HostapdInterfaceGetStaInfos(struct IHostapdInterface *self, const char *ifName,
547     char *buf, uint32_t bufLen, int32_t size, int32_t id)
548 {
549     struct hostapd_data *hostApd;
550     char cmd[CMD_SIZE] = {0};
551     int32_t ret = HDF_FAILURE;
552     char *reqBuf = (char *)calloc(BUFFSIZE_REQUEST, sizeof(char));
553 
554     (void)self;
555     if (ifName == NULL || buf == NULL || reqBuf == NULL) {
556         HDF_LOGE("%{public}s: Input parameter invalid or calloc failed!", __func__);
557         free(reqBuf);
558         return HDF_ERR_INVALID_PARAM;
559     }
560     hostApd = getHostapd();
561     if (hostApd == NULL) {
562         HDF_LOGE("%{public}s hostapd is null.", __func__);
563         free(reqBuf);
564         return HDF_FAILURE;
565     }
566     ret = hostapd_ctrl_iface_sta_first(hostApd, reqBuf, BUFFSIZE_REQUEST);
567     if (ret != HDF_SUCCESS) {
568         HDF_LOGE("%{public}s: Hostapd failed to get first sta!", __func__);
569         free(reqBuf);
570         return HDF_FAILURE;
571     }
572     do {
573         char *pos = reqBuf;
574         while (*pos != '\0' && *pos != '\n') {
575             /* return station info, first line is mac address */
576             pos++;
577         }
578         *pos = '\0';
579         if (strcmp(reqBuf, "") != 0) {
580             int bufLen = strlen(buf);
581             int staLen = strlen(reqBuf);
582             if (bufLen + staLen + 1 >= size) {
583                 free(reqBuf);
584                 reqBuf = NULL;
585                 return HDF_SUCCESS;
586             }
587             buf[bufLen++] = ',';
588             for (int i = 0; i < staLen; ++i) {
589                 buf[bufLen + i] = reqBuf[i];
590             }
591             buf[bufLen + staLen] = '\0';
592         }
593         if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "%s", reqBuf) < 0) {
594             HDF_LOGE("%{public}s: Hostapd failed to get sta infos!", __func__);
595             free(reqBuf);
596             return HDF_FAILURE;
597         }
598     } while (hostapd_ctrl_iface_sta_next(hostApd, cmd, reqBuf, BUFFSIZE_REQUEST));
599     free(reqBuf);
600     reqBuf = NULL;
601     return HDF_SUCCESS;
602 }
603 
HostapdInterfaceDisassociateSta(struct IHostapdInterface * self,const char * ifName,const char * mac,int32_t id)604 int32_t HostapdInterfaceDisassociateSta(struct IHostapdInterface *self, const char *ifName,
605     const char *mac, int32_t id)
606 {
607     struct hostapd_data *hostApd;
608     int32_t ret = HDF_FAILURE;
609 
610     (void)self;
611     if (ifName == NULL || mac == NULL) {
612         HDF_LOGE("%{public}s: Input parameter invalid!", __func__);
613         return HDF_ERR_INVALID_PARAM;
614     }
615     hostApd = getHostapd();
616     if (hostApd == NULL) {
617         HDF_LOGE("%{public}s hostApd is null.", __func__);
618         return HDF_FAILURE;
619     }
620     ret = hostapd_ctrl_iface_disassociate(hostApd, mac);
621     if (ret != HDF_SUCCESS) {
622         HDF_LOGE("%{public}s: Hostapd failed to disassociate with sta!", __func__);
623         return HDF_FAILURE;
624     }
625     return HDF_SUCCESS;
626 }
627 
ProcessEventStaJoin(struct HdfHostapdRemoteNode * node,struct HostapdApCbParm * apCbParm,const char * ifName)628 static int32_t ProcessEventStaJoin(struct HdfHostapdRemoteNode *node,
629     struct HostapdApCbParm *apCbParm, const char *ifName)
630 {
631     struct HdiApCbParm *hdiApCbParm = NULL;
632     int32_t ret = HDF_FAILURE;
633 
634     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventStaJoin == NULL) {
635         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
636         return HDF_ERR_INVALID_PARAM;
637     }
638     hdiApCbParm = (struct HdiApCbParm *)OsalMemCalloc(sizeof(struct HdiApCbParm));
639     if (hdiApCbParm == NULL) {
640         HDF_LOGE("%{public}s: hdiApCbParm is NULL!", __func__);
641         return HDF_FAILURE;
642     } else {
643         hdiApCbParm->content = OsalMemCalloc(WIFI_HOSTAPD_CB_CONTENT_LENGTH);
644         if (hdiApCbParm->content == NULL) {
645             HDF_LOGE("%{public}s: hdiApCbParm->content is NULL!", __func__);
646         } else {
647             memcpy_s(hdiApCbParm->content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
648                 apCbParm->content, WIFI_HOSTAPD_CB_CONTENT_LENGTH);
649             hdiApCbParm->id = apCbParm->id;
650             ret = node->callbackObj->OnEventStaJoin(node->callbackObj, hdiApCbParm, ifName);
651         }
652     }
653     HdiApCbParmFree(hdiApCbParm, true);
654     return ret;
655 }
656 
ProcessEventApState(struct HdfHostapdRemoteNode * node,struct HostapdApCbParm * apCbParm,const char * ifName)657 static int32_t ProcessEventApState(struct HdfHostapdRemoteNode *node,
658     struct HostapdApCbParm *apCbParm, const char *ifName)
659 {
660     struct HdiApCbParm *hdiApCbParm  = NULL;
661     int32_t ret = HDF_FAILURE;
662 
663     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventApState == NULL) {
664         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
665         return HDF_ERR_INVALID_PARAM;
666     }
667     hdiApCbParm = (struct HdiApCbParm *)OsalMemCalloc(sizeof(struct HdiApCbParm));
668     if (hdiApCbParm == NULL) {
669         HDF_LOGE("%{public}s: hdiApCbParm is NULL!", __func__);
670         return HDF_FAILURE;
671     } else {
672         hdiApCbParm->content = OsalMemCalloc(WIFI_HOSTAPD_CB_CONTENT_LENGTH);
673         if (hdiApCbParm->content == NULL) {
674             HDF_LOGE("%{public}s: hdiApCbParm is NULL!", __func__);
675         } else {
676             memcpy_s(hdiApCbParm->content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
677                 apCbParm->content, WIFI_HOSTAPD_CB_CONTENT_LENGTH);
678             hdiApCbParm->id = apCbParm->id;
679             ret = node->callbackObj->OnEventApState(node->callbackObj, hdiApCbParm, ifName);
680         }
681     }
682     HdiApCbParmFree(hdiApCbParm, true);
683     return ret;
684 }
685 
ProcessEventHostapdNotify(struct HdfHostapdRemoteNode * node,char * notifyParam,const char * ifName)686 int32_t ProcessEventHostapdNotify(struct HdfHostapdRemoteNode *node, char *notifyParam, const char *ifName)
687 {
688     int32_t ret = HDF_FAILURE;
689 
690     if (node == NULL || node->callbackObj == NULL || node->callbackObj->OnEventHostApdNotify == NULL) {
691         HDF_LOGE("%{public}s: hdf wlan remote node or callbackObj is NULL!", __func__);
692         return HDF_ERR_INVALID_PARAM;
693     }
694     if (strlen(notifyParam) == 0) {
695         ret = HDF_FAILURE;
696     }
697     return ret;
698 }
699 
HdfHostapdCallbackFun(uint32_t event,void * data,const char * ifName)700 static int32_t HdfHostapdCallbackFun(uint32_t event, void *data, const char *ifName)
701 {
702     struct HdfHostapdRemoteNode *pos = NULL;
703     struct DListHead *head = NULL;
704     int32_t ret = HDF_FAILURE;
705 
706     (void)OsalMutexLock(&HdfHostapdStubDriver()->mutex);
707     head = &HdfHostapdStubDriver()->remoteListHead;
708     HDF_LOGD("%s: enter HdfHostapdCallbackFun event =%d ", __FUNCTION__, event);
709     if (data == NULL || ifName == NULL) {
710         HDF_LOGE("%{public}s: data or ifName is NULL!", __func__);
711         (void)OsalMutexUnlock(&HdfHostapdStubDriver()->mutex);
712         return HDF_ERR_INVALID_PARAM;
713     }
714     DLIST_FOR_EACH_ENTRY(pos, head, struct HdfHostapdRemoteNode, node) {
715         if (pos == NULL) {
716             HDF_LOGE("%{public}s: pos is NULL", __func__);
717             break;
718         }
719         if (pos->service == NULL || pos->callbackObj == NULL) {
720             HDF_LOGW("%{public}s: pos->service or pos->callbackObj NULL", __func__);
721             continue;
722         }
723         switch (event) {
724             case HOSTAPD_EVENT_STA_JOIN:
725                 ret = ProcessEventStaJoin(pos, (struct HostapdApCbParm *)data, ifName);
726                 break;
727             case HOSTAPD_EVENT_AP_STATE:
728                 ret = ProcessEventApState(pos, (struct HostapdApCbParm *)data, ifName);
729                 break;
730             case HOSTAPD_EVENT_HOSTAPD_NOTIFY:
731                 ret = ProcessEventHostapdNotify(pos, (char *)data, ifName);
732                 break;
733             default:
734                 HDF_LOGE("%{public}s: unknown eventId:%{public}d", __func__, event);
735                 break;
736         }
737         if (ret != HDF_SUCCESS) {
738             HDF_LOGE("%{public}s: dispatch code fialed, error code: %{public}d", __func__, ret);
739         }
740     }
741     (void)OsalMutexUnlock(&HdfHostapdStubDriver()->mutex);
742     return ret;
743 }
744 
HdfHostapdAddRemoteObj(struct IHostapdCallback * self)745 static int32_t HdfHostapdAddRemoteObj(struct IHostapdCallback *self)
746 {
747     struct HdfHostapdRemoteNode *pos = NULL;
748     struct DListHead *head = &HdfHostapdStubDriver()->remoteListHead;
749 
750     if (self == NULL) {
751         HDF_LOGE("%{public}s:self is null.", __func__);
752         return HDF_ERR_INVALID_PARAM;
753     }
754     if (!DListIsEmpty(head)) {
755             DLIST_FOR_EACH_ENTRY(pos, head, struct HdfHostapdRemoteNode, node) {
756             if (pos->service == self->AsObject(self)) {
757                 HDF_LOGE("%{public}s: pos->service == self", __func__);
758                 return HDF_FAILURE;
759             }
760         }
761     }
762     struct HdfHostapdRemoteNode *newRemoteNode =
763         (struct HdfHostapdRemoteNode *)OsalMemCalloc(sizeof(struct HdfHostapdRemoteNode));
764     if (newRemoteNode == NULL) {
765         HDF_LOGE("%{public}s:newRemoteNode is NULL", __func__);
766         return HDF_FAILURE;
767     }
768     newRemoteNode->callbackObj = self;
769     newRemoteNode->service = self->AsObject(self);
770     DListInsertTail(&newRemoteNode->node, head);
771     return HDF_SUCCESS;
772 }
773 
HdfHostapdDelRemoteObj(struct IHostapdCallback * self)774 static void HdfHostapdDelRemoteObj(struct IHostapdCallback *self)
775 {
776     struct HdfHostapdRemoteNode *pos = NULL;
777     struct HdfHostapdRemoteNode *tmp = NULL;
778     struct DListHead *head = &HdfHostapdStubDriver()->remoteListHead;
779 
780     DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, head, struct HdfHostapdRemoteNode, node) {
781         if (pos->service->index == self->AsObject(self)->index) {
782             DListRemove(&(pos->node));
783             IHostapdCallbackRelease(pos->callbackObj);
784             OsalMemFree(pos);
785             break;
786         }
787     }
788     IHostapdCallbackRelease(self);
789 }
790 
HostapdInterfaceRegisterEventCallback(struct IHostapdInterface * self,struct IHostapdCallback * cbFunc,const char * ifName)791 int32_t HostapdInterfaceRegisterEventCallback(struct IHostapdInterface *self,
792     struct IHostapdCallback *cbFunc, const char *ifName)
793 {
794     int32_t ret = HDF_FAILURE;
795 
796     (void)self;
797     if (cbFunc == NULL || ifName == NULL) {
798         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
799         return HDF_ERR_INVALID_PARAM;
800     }
801     (void)OsalMutexLock(&HdfHostapdStubDriver()->mutex);
802     do {
803         ret = HdfHostapdAddRemoteObj(cbFunc);
804         if (ret != HDF_SUCCESS) {
805             HDF_LOGE("%{public}s: HdfSensorAddRemoteObj false", __func__);
806             break;
807         }
808         ret = HostapdRegisterEventCallback(HdfHostapdCallbackFun, WIFI_HOSTAPD_TO_HAL_CLIENT, ifName);
809         if (ret != HDF_SUCCESS) {
810             HDF_LOGE("%{public}s: Register failed!, error code: %{public}d", __func__, ret);
811             HdfHostapdDelRemoteObj(cbFunc);
812             break;
813         }
814     } while (0);
815     (void)OsalMutexUnlock(&HdfHostapdStubDriver()->mutex);
816     return ret;
817 }
818 
HostapdInterfaceUnregisterEventCallback(struct IHostapdInterface * self,struct IHostapdCallback * cbFunc,const char * ifName)819 int32_t HostapdInterfaceUnregisterEventCallback(struct IHostapdInterface *self,
820     struct IHostapdCallback *cbFunc, const char *ifName)
821 {
822     int32_t ret = HDF_FAILURE;
823 
824     (void)self;
825     if (cbFunc == NULL || ifName == NULL) {
826         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
827         return HDF_ERR_INVALID_PARAM;
828     }
829     (void)OsalMutexLock(&HdfHostapdStubDriver()->mutex);
830     HdfHostapdDelRemoteObj(cbFunc);
831     if (DListIsEmpty(&HdfHostapdStubDriver()->remoteListHead)) {
832         ret = HostapdUnregisterEventCallback(HdfHostapdCallbackFun, WIFI_HOSTAPD_TO_HAL_CLIENT, ifName);
833         if (ret != HDF_SUCCESS) {
834             HDF_LOGE("%{public}s: Unregister failed!, error code: %{public}d", __func__, ret);
835         }
836     }
837     (void)OsalMutexUnlock(&HdfHostapdStubDriver()->mutex);
838     return HDF_SUCCESS;
839 }
840 
HostApdInterfaceShellCmd(struct IHostapdInterface * self,const char * ifName,const char * cmd)841 int32_t HostApdInterfaceShellCmd(struct IHostapdInterface *self, const char *ifName, const char *cmd)
842 {
843     struct hostapd_data *hostApd;
844 
845     (void)self;
846     if (ifName == NULL || cmd == NULL) {
847         HDF_LOGE("%{public}s: input parameter invalid!", __func__);
848         return HDF_ERR_INVALID_PARAM;
849     }
850     hostApd = getHostapd();
851     if (hostApd == NULL) {
852         HDF_LOGE("%{public}s wpaSupp == NULL", __func__);
853         return HDF_FAILURE;
854     }
855     return HDF_SUCCESS;
856 }