1 //----------------------------------------------------------------------------//
2 #include "wifi/wifi_ind.h"
3 #include "wifi/wifi_conf.h"
4 #include "osdep_service.h"
5 #include "platform_stdlib.h"
6 
7 /******************************************************
8  *                    Constants
9  ******************************************************/
10 
11 #define WIFI_INDICATE_MSG	0
12 #define WIFI_MANAGER_STACKSIZE	1300
13 #define WIFI_MANAGER_PRIORITY		(0) //Actual priority is 4 since calling rtw_create_task
14 #define WIFI_MANAGER_Q_SZ	8
15 
16 #define WIFI_EVENT_MAX_ROW	3
17 /******************************************************
18  *                    Globals
19  ******************************************************/
20 
21 static event_list_elem_t     event_callback_list[WIFI_EVENT_MAX][WIFI_EVENT_MAX_ROW];
22 #if CONFIG_WIFI_IND_USE_THREAD
23 static rtw_worker_thread_t          wifi_worker_thread;
24 #endif
25 
26 //----------------------------------------------------------------------------//
27 #if CONFIG_WIFI_IND_USE_THREAD
rtw_send_event_to_worker(int event_cmd,char * buf,int buf_len,int flags)28 static rtw_result_t rtw_send_event_to_worker(int event_cmd, char *buf, int buf_len, int flags)
29 {
30 	rtw_event_message_t message;
31 	int i;
32 	rtw_result_t ret = RTW_SUCCESS;
33 	char *local_buf = NULL;
34 
35 	if(event_cmd >= WIFI_EVENT_MAX)
36 		return RTW_BADARG;
37 
38 	for(i = 0; i < WIFI_EVENT_MAX_ROW; i++){
39 		if(event_callback_list[event_cmd][i].handler == NULL)
40 			continue;
41 
42 		message.function = (event_handler_t)event_callback_list[event_cmd][i].handler;
43 		message.buf_len = buf_len;
44 		if(buf_len){
45 			local_buf = (char*)pvPortMalloc(buf_len);
46 			if(local_buf == NULL)
47 				return RTW_NOMEM;
48 			memcpy(local_buf, buf, buf_len);
49 			//printf("\n!!!!!Allocate %p(%d) for evcmd %d\n", local_buf, buf_len, event_cmd);
50 		}
51 		message.buf = local_buf;
52 		message.flags = flags;
53 		message.user_data = event_callback_list[event_cmd][i].handler_user_data;
54 
55 		ret = rtw_push_to_xqueue(&wifi_worker_thread.event_queue, &message, 0);
56 		if(ret != RTW_SUCCESS){
57 			if(local_buf){
58 				printf("\r\nrtw_send_event_to_worker: enqueue cmd %d failed and free %p(%d)\n", event_cmd, local_buf, buf_len);
59 				vPortFree(local_buf);
60 			}
61 			break;
62 		}
63 	}
64 	return ret;
65 }
66 #else
rtw_indicate_event_handle(int event_cmd,char * buf,int buf_len,int flags)67 static rtw_result_t rtw_indicate_event_handle(int event_cmd, char *buf, int buf_len, int flags)
68 {
69 	rtw_event_handler_t handle = NULL;
70 	int i;
71 
72 	if(event_cmd >= WIFI_EVENT_MAX)
73 		return (rtw_result_t)RTW_BADARG;
74 
75 	for(i = 0; i < WIFI_EVENT_MAX_ROW; i++){
76 		handle = event_callback_list[event_cmd][i].handler;
77 		if(handle == NULL)
78 			continue;
79 		handle(buf, buf_len, flags, event_callback_list[event_cmd][i].handler_user_data);
80 	}
81 
82 	return RTW_SUCCESS;
83 }
84 #endif
85 
wifi_indication(rtw_event_indicate_t event,char * buf,int buf_len,int flags)86 void wifi_indication( rtw_event_indicate_t event, char *buf, int buf_len, int flags)
87 {
88 	//
89 	// If upper layer application triggers additional operations on receiving of wext_wlan_indicate,
90 	// 		please strictly check current stack size usage (by using uxTaskGetStackHighWaterMark() )
91 	//		, and tries not to share the same stack with wlan driver if remaining stack space is
92 	//		not available for the following operations.
93 	//		ex: using semaphore to notice another thread.
94 	switch(event)
95 	{
96 		case WIFI_EVENT_DISCONNECT:
97 #if(WIFI_INDICATE_MSG==1)
98 			printf("\n\r %s():Disconnection indication received", __FUNCTION__);
99 #endif
100 			break;
101 		case WIFI_EVENT_CONNECT:
102 			// For WPA/WPA2 mode, indication of connection does not mean data can be
103 			// 		correctly transmitted or received. Data can be correctly transmitted or
104 			// 		received only when 4-way handshake is done.
105 			// Please check WIFI_EVENT_FOURWAY_HANDSHAKE_DONE event
106 #if(WIFI_INDICATE_MSG==1)
107 			// Sample: return mac address
108 			if(buf != NULL && buf_len == 6)
109 			{
110 				printf("\n\r%s():Connect indication received: %02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
111 							buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
112 			}
113 #endif
114 			break;
115 		case WIFI_EVENT_FOURWAY_HANDSHAKE_DONE:
116 #if(WIFI_INDICATE_MSG==1)
117 			if(buf != NULL)
118 			{
119 				if(buf_len == strlen(IW_EXT_STR_FOURWAY_DONE))
120 					printf("\n\r%s():%s", __FUNCTION__, buf);
121 			}
122 #endif
123 			break;
124 		case WIFI_EVENT_SCAN_RESULT_REPORT:
125 #if(WIFI_INDICATE_MSG==1)
126 			printf("\n\r%s(): WIFI_EVENT_SCAN_RESULT_REPORT\n", __func__);
127 #endif
128 			break;
129 		case WIFI_EVENT_SCAN_DONE:
130 #if(WIFI_INDICATE_MSG==1)
131 			printf("\n\r%s(): WIFI_EVENT_SCAN_DONE\n", __func__);
132 #endif
133 			break;
134 		case WIFI_EVENT_RECONNECTION_FAIL:
135 #if(WIFI_INDICATE_MSG==1)
136 			if(buf != NULL){
137 				if(buf_len == strlen(IW_EXT_STR_RECONNECTION_FAIL))
138 					printf("\n\r%s", buf);
139 			}
140 #endif
141 			break;
142 		case WIFI_EVENT_NO_NETWORK:
143 #if(WIFI_INDICATE_MSG==1)
144 			printf("\n\r%s(): WIFI_EVENT_NO_NETWORK\n", __func__);
145 #endif
146 			break;
147 		case WIFI_EVENT_RX_MGNT:
148 #if(WIFI_INDICATE_MSG==1)
149 			printf("\n\r%s(): WIFI_EVENT_RX_MGNT\n", __func__);
150 #endif
151 			break;
152 #if CONFIG_ENABLE_P2P
153 		case WIFI_EVENT_SEND_ACTION_DONE:
154 #if(WIFI_INDICATE_MSG==1)
155 			printf("\n\r%s(): WIFI_EVENT_SEND_ACTION_DONE\n", __func__);
156 #endif
157 			break;
158 #endif //CONFIG_ENABLE_P2P
159 		case WIFI_EVENT_STA_ASSOC:
160 #if(WIFI_INDICATE_MSG==1)
161 			printf("\n\r%s(): WIFI_EVENT_STA_ASSOC\n", __func__);
162 #endif
163 			break;
164 		case WIFI_EVENT_STA_DISASSOC:
165 #if(WIFI_INDICATE_MSG==1)
166 			printf("\n\r%s(): WIFI_EVENT_STA_DISASSOC\n", __func__);
167 #endif
168 			break;
169 #ifdef CONFIG_WPS
170 		case WIFI_EVENT_STA_WPS_START:
171 #if(WIFI_INDICATE_MSG==1)
172 			printf("\n\r%s(): WIFI_EVENT_STA_WPS_START\n", __func__);
173 #endif
174 			break;
175 		case WIFI_EVENT_WPS_FINISH:
176 #if(WIFI_INDICATE_MSG==1)
177 			printf("\n\r%s(): WIFI_EVENT_WPS_FINISH\n", __func__);
178 #endif
179 			break;
180 		case WIFI_EVENT_EAPOL_RECVD:
181 #if(WIFI_INDICATE_MSG==1)
182 			printf("\n\r%s(): WIFI_EVENT_EAPOL_RECVD\n", __func__);
183 #endif
184 			break;
185 #endif
186 		case WIFI_EVENT_BEACON_AFTER_DHCP:
187 #if(WIFI_INDICATE_MSG==1)
188 			printf("\n\r%s(): WIFI_EVENT_BEACON_AFTER_DHCP\n", __func__);
189 #endif
190 			break;
191 		case WIFI_EVENT_IP_CHANGED:
192 #if(WIFI_INDICATE_MSG==1)
193 			printf("\n\r%s(): WIFI_EVENT_IP_CHANNGED\n", __func__);
194 #endif
195 			break;
196 		case WIFI_EVENT_ICV_ERROR:
197 #if(WIFI_INDICATE_MSG==1)
198 			printf("\n\r%s(): WIFI_EVENT_ICV_ERROR\n", __func__);
199 #endif
200 		case WIFI_EVENT_CHALLENGE_FAIL:
201 #if(WIFI_INDICATE_MSG==1)
202 			printf("\n\r%s(): WIFI_EVENT_CHALLENGE_FAIL\n", __func__);
203 #endif
204 			break;
205 		case WIFI_EVENT_SOFTAP_START:
206 #if(WIFI_INDICATE_MSG==1)
207 			printf("\n\r%s(): WIFI_EVENT_SOFTAP_START\n", __func__);
208 #endif
209 			break;
210 		case WIFI_EVENT_SOFTAP_STOP:
211 #if(WIFI_INDICATE_MSG==1)
212 			printf("\n\r%s(): WIFI_EVENT_SOFTAP_STOP\n", __func__);
213 #endif
214 			break;
215 	}
216 
217 #if CONFIG_INIC_EN
218 	inic_indicate_event(event, buf, buf_len, flags);
219 #endif//CONFIG_INIC_EN
220 
221 #if CONFIG_WIFI_IND_USE_THREAD
222 	rtw_send_event_to_worker(event, buf, buf_len, flags);
223 #else
224 	rtw_indicate_event_handle(event, buf, buf_len, flags);
225 #endif
226 }
227 
wifi_reg_event_handler(unsigned int event_cmds,rtw_event_handler_t handler_func,void * handler_user_data)228 void wifi_reg_event_handler(unsigned int event_cmds, rtw_event_handler_t handler_func, void *handler_user_data)
229 {
230 	int i = 0, j = 0;
231 	if(event_cmds < WIFI_EVENT_MAX){
232 		for(i=0; i < WIFI_EVENT_MAX_ROW; i++){
233 			if(event_callback_list[event_cmds][i].handler == NULL){
234 			    for(j=0; j<WIFI_EVENT_MAX_ROW; j++){
235 			        if(event_callback_list[event_cmds][j].handler == handler_func){
236 			            return;
237 			        }
238 			    }
239 				event_callback_list[event_cmds][i].handler = handler_func;
240 				event_callback_list[event_cmds][i].handler_user_data = handler_user_data;
241 				return;
242 			}
243 		}
244 	}
245 }
246 
wifi_unreg_event_handler(unsigned int event_cmds,rtw_event_handler_t handler_func)247 void wifi_unreg_event_handler(unsigned int event_cmds, rtw_event_handler_t handler_func)
248 {
249 	int i;
250 	if(event_cmds < WIFI_EVENT_MAX){
251 		for(i = 0; i < WIFI_EVENT_MAX_ROW; i++){
252 			if(event_callback_list[event_cmds][i].handler == handler_func){
253 				event_callback_list[event_cmds][i].handler = NULL;
254 				event_callback_list[event_cmds][i].handler_user_data = NULL;
255 				return;
256 			}
257 		}
258 	}
259 }
260 
init_event_callback_list(void)261 void init_event_callback_list(void){
262 	memset(event_callback_list, 0, sizeof(event_callback_list));
263 }
264 
wifi_manager_init(void)265 int wifi_manager_init(void)
266 {
267 #if CONFIG_WIFI_IND_USE_THREAD
268 	rtw_create_worker_thread(&wifi_worker_thread,
269 							WIFI_MANAGER_PRIORITY,
270 							WIFI_MANAGER_STACKSIZE,
271 							WIFI_MANAGER_Q_SZ);
272 #endif
273 	return 0;
274 }
275 
rtw_wifi_manager_deinit(void)276 void rtw_wifi_manager_deinit(void)
277 {
278 #if CONFIG_WIFI_IND_USE_THREAD
279 	rtw_delete_worker_thread(&wifi_worker_thread);
280 #endif
281 }
282 
283