1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: eloop API
15 */
16
17 #include "common.h"
18 #include "eloop_ltos.h"
19 #include "wpa_supplicant_if.h"
20 #include "hostapd_if.h"
21 #include "wifi_api.h"
22 #include "wpa_supplicant/ctrl_iface.h"
23 #include "securec.h"
24
25 static int g_eloop_task_flag[(int)ELOOP_MAX_TASK_TYPE_NUM] = {0};
26 static int g_eloop_softap_terminate_flag = WPA_FLAG_OFF;
27 static int g_eloop_wpa_terminate_flag = WPA_FLAG_OFF;
28
wpa_supplicant_main_task(void * param)29 int wpa_supplicant_main_task(void *param)
30 {
31 struct ext_wifi_dev *wifi_dev = (struct ext_wifi_dev *)param;
32 int ret = EXT_WIFI_OK;
33 if (wifi_dev == NULL) {
34 (void)os_event_write(g_wpa_event, WPA_EVENT_WPA_START_ERROR);
35 ret = EXT_WIFI_FAIL;
36 goto exit;
37 }
38 if (wifi_dev->iftype == EXT_WIFI_IFTYPE_AP) {
39 hostapd_main(wifi_dev->ifname);
40 ret = EXT_WIFI_OK;
41 goto exit;
42 }
43 wpa_supplicant_main(wifi_dev->ifname);
44 exit:
45 wpa_event_task_free();
46 os_task_delete(g_wpataskid);
47 return ret;
48 }
49
wpa_supplicant_exit(void)50 void wpa_supplicant_exit(void)
51 {
52 struct wpa_global *wpa_global = wpa_supplicant_get_wpa_global();
53 if (wpa_global == NULL)
54 return;
55 wpa_supplicant_deinit(wpa_global);
56 wpa_supplicant_global_deinit();
57 }
58
59
hostapd_exit(void)60 void hostapd_exit(void)
61 {
62 struct hapd_interfaces *interfaces = hostapd_get_interfaces();
63 if (interfaces == NULL)
64 return;
65 hostapd_pre_quit(interfaces);
66 los_free_wifi_dev(los_get_wifi_dev_by_priv(g_hapd));
67 hostapd_global_deinit();
68 hostapd_global_interfaces_deinit();
69 }
70
71
eloop_is_running(eloop_task_type eloop_task)72 int eloop_is_running(eloop_task_type eloop_task)
73 {
74 if (eloop_task >= ELOOP_MAX_TASK_TYPE_NUM)
75 return EXT_WIFI_FAIL;
76
77 unsigned int int_save;
78 os_task_lock(&int_save);
79 if (g_eloop_task_flag[eloop_task] != WPA_FLAG_ON) {
80 os_task_unlock(int_save);
81 return EXT_WIFI_FAIL;
82 }
83 os_task_unlock(int_save);
84 return EXT_WIFI_OK;
85 }
86
global_eloop_is_running(void)87 int global_eloop_is_running(void)
88 {
89 return ((g_eloop_task_flag[ELOOP_TASK_WPA] == WPA_FLAG_ON) ||
90 (g_eloop_task_flag[ELOOP_TASK_HOSTAPD] == WPA_FLAG_ON));
91 }
92
eloop_terminate_wpa_process(void)93 int eloop_terminate_wpa_process(void)
94 {
95 int count = 0;
96 if (g_eloop_wpa_terminate_flag == WPA_FLAG_ON) {
97 g_eloop_wpa_terminate_flag = WPA_FLAG_OFF;
98 count++;
99 wpa_supplicant_exit();
100 (void)os_event_write(g_wpa_event, WPA_EVENT_STA_STOP_OK);
101 }
102 if (g_eloop_softap_terminate_flag == WPA_FLAG_ON) {
103 g_eloop_softap_terminate_flag = WPA_FLAG_OFF;
104 count++;
105 hostapd_exit();
106 // send softap stop event
107 (void)os_event_write(g_softap_event, WPA_EVENT_AP_STOP_OK);
108 }
109 return count ? EXT_WIFI_OK : EXT_WIFI_FAIL;
110 }
111
eloop_run_process_normal_event(unsigned int * ret_flags,struct eloop_data * eloop)112 void eloop_run_process_normal_event(unsigned int *ret_flags, struct eloop_data *eloop)
113 {
114 if ((ret_flags == NULL) || (eloop == NULL))
115 return;
116 for (int i = 0; i < ELOOP_MAX_EVENTS; i++) {
117 if (eloop->events[i].flag & *ret_flags) {
118 if (eloop->events[i].handler)
119 eloop->events[i].handler(eloop->events[i].eloop_data, eloop->events[i].user_data);
120 *ret_flags &= ~eloop->events[i].flag;
121 if (eloop->events[i].handler == (eloop_event_handler)eloop_terminate)
122 break;
123 }
124 }
125 }
126
eloop_start_running(eloop_task_type eloop_task)127 int eloop_start_running(eloop_task_type eloop_task)
128 {
129 if (global_eloop_is_running()) {
130 g_eloop_task_flag[eloop_task] = WPA_FLAG_ON;
131 (void)os_event_write(g_wpa_event, WPA_EVENT_WPA_START_OK);
132 return ELOOP_ALREADY_RUNNING;
133 }
134
135 g_eloop_task_flag[eloop_task] = WPA_FLAG_ON;
136 return ELOOP_NOT_RUNNING;
137 }
138
eloop_terminate(eloop_task_type eloop_task)139 void eloop_terminate(eloop_task_type eloop_task)
140 {
141 if ((eloop_task != ELOOP_TASK_WPA) && (eloop_task != ELOOP_TASK_HOSTAPD))
142 return;
143 if (g_eloop_task_flag[eloop_task] != WPA_FLAG_ON)
144 return;
145 g_eloop_task_flag[eloop_task] = WPA_FLAG_OFF;
146
147 if (eloop_task == ELOOP_TASK_WPA)
148 g_eloop_wpa_terminate_flag = WPA_FLAG_ON;
149 else
150 g_eloop_softap_terminate_flag = WPA_FLAG_ON;
151 }
152
eloop_terminated(eloop_task_type eloop_task)153 int eloop_terminated(eloop_task_type eloop_task)
154 {
155 if ((eloop_task != ELOOP_TASK_WPA) && (eloop_task != ELOOP_TASK_HOSTAPD))
156 return EXT_WIFI_FAIL;
157 return !g_eloop_task_flag[eloop_task];
158 }
159