1 /*
2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/common.h"
10 #include "wpa_supplicant/ctrl_iface.h"
11 #include "utils/eloop.h"
12 #include "wpa_supplicant_i.h"
13 #include "ap/hostapd.h"
14 #include "wifi_api.h"
15 /* Per-interface ctrl_iface */
16
17 struct ctrl_iface_priv *g_ctrl_iface_data = NULL;
18
wpa_supplicant_ctrl_iface_receive(void * eloop_ctx,void * sock_ctx)19 static void wpa_supplicant_ctrl_iface_receive(void *eloop_ctx, void *sock_ctx)
20 {
21 struct ctrl_iface_event_buf *event_buf = NULL;
22 struct wpa_supplicant *wpa_s = NULL;
23 char *buf = NULL;
24 char *reply_buf = NULL;
25 size_t reply_len = 0;
26 void *packet = NULL;
27 (void)eloop_ctx;
28 (void)sock_ctx;
29
30 while (1) {
31 /*
32 * priv->event_queue is from eloop.events[i].user_data,
33 * while eloop.events is malloc in eloop_init and free
34 * in eloop_destroy
35 */
36 if (g_ctrl_iface_data == NULL)
37 break;
38 packet = eloop_read_event(g_ctrl_iface_data->event_queue, 0);
39 if (packet == NULL) {
40 break;
41 }
42 #ifdef LOS_CONFIG_SUPPORT_CPUP
43 uapi_cpup_load_check_proc(uapi_task_get_current_id(), LOAD_SLEEP_TIME_DEFAULT);
44 #endif /* LOS_CONFIG_SUPPORT_CPUP */
45 event_buf = (struct ctrl_iface_event_buf *)packet;
46 buf = event_buf->buf;
47 wpa_s = event_buf->wpa_s;
48 /* reply_buf malloc */
49 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf, &reply_len);
50 /* reply_buf free */
51 if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
52 wpa_error_log0(MSG_ERROR, "os_event_read,wait for WPA_EVENT_SCAN_RESULT_FREE_OK");
53 unsigned int ret = 0;
54 (void)os_event_read(g_wpa_event, WPA_EVENT_SCAN_RESULT_FREE_OK, &ret,
55 WIFI_WAITMODE_OR | WIFI_WAITMODE_CLR, WIFI_EVENT_DELAY_5S);
56 }
57 os_free(reply_buf);
58 /* buf free */
59 os_memset(buf, 0, WPA_CTRL_CMD_LEN);
60 os_free(buf);
61 /* packet free */
62 os_free(packet);
63 }
64 }
65
los_ctrl_iface_init(void * data)66 struct ctrl_iface_priv * los_ctrl_iface_init(void *data)
67 {
68 struct ctrl_iface_priv *priv = os_zalloc(sizeof(struct ctrl_iface_priv));
69 if (priv == NULL) {
70 wpa_error_log0(MSG_ERROR, "los_ctrl_iface_init malloc fail.");
71 return NULL;
72 }
73 priv->wpa_s = data;
74 priv->sock = -1;
75
76 if (eloop_register_event(&priv->event_queue, sizeof(void *), wpa_supplicant_ctrl_iface_receive,
77 data, priv) != EXT_WIFI_OK) {
78 os_free(priv);
79 return NULL;
80 }
81 g_ctrl_iface_data = priv;
82 return priv;
83 }
84
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant * wpa_s)85 struct ctrl_iface_priv * wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
86 {
87 struct ctrl_iface_priv *priv = los_ctrl_iface_init(wpa_s);
88 if (priv == NULL)
89 return NULL;
90
91 return priv;
92 }
93
wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant * wpa_s,struct ctrl_iface_priv * priv)94 void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s, struct ctrl_iface_priv *priv)
95 {
96 struct ext_wifi_dev *softap_wifi_dev = los_get_wifi_dev_by_iftype(EXT_WIFI_IFTYPE_AP);
97 struct ext_wifi_dev *wifi_dev = NULL;
98 if ((priv == NULL) || (priv->wpa_s == NULL) || (priv->event_queue == NULL))
99 return;
100
101 wifi_dev = wpa_get_other_existed_wpa_wifi_dev(priv->wpa_s);
102 eloop_unregister_cli_event(priv->event_queue, sizeof(void *));
103 if ((wifi_dev != NULL) && (wifi_dev->priv != NULL))
104 g_ctrl_iface_data = ((struct wpa_supplicant *)(wifi_dev->priv))->ctrl_iface;
105 else if ((softap_wifi_dev != NULL) && (softap_wifi_dev->priv != NULL))
106 g_ctrl_iface_data = ((struct hostapd_data *)(softap_wifi_dev->priv))->ctrl_iface;
107 else
108 g_ctrl_iface_data = NULL;
109
110 os_free(priv);
111 wpa_error_log0(MSG_ERROR, "wpa_supplicant_ctrl_iface_deinit exit");
112 }
113