open-ameba/sdk/component/common/api/wifi/wifi_ind.c

270 lines
7.5 KiB
C
Raw Permalink Normal View History

2016-11-09 00:56:41 +00:00
//----------------------------------------------------------------------------//
#include "wifi/wifi_ind.h"
#include "wifi/wifi_conf.h"
#include "osdep_service.h"
#include "platform_stdlib.h"
/******************************************************
* Constants
******************************************************/
#if CONFIG_DEBUG_LOG > 3
2017-03-02 08:46:28 +00:00
#define WIFI_INDICATE_MSG 1
2016-11-09 00:56:41 +00:00
#else
2017-03-02 08:46:28 +00:00
#define WIFI_INDICATE_MSG 0
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
#define WIFI_MANAGER_STACKSIZE 400 // 1300
2016-11-09 00:56:41 +00:00
#define WIFI_MANAGER_PRIORITY (0) //Actual priority is 4 since calling rtw_create_task
#define WIFI_MANAGER_Q_SZ 8
#define WIFI_EVENT_MAX_ROW 3
/******************************************************
* Globals
******************************************************/
2017-03-02 08:46:28 +00:00
static event_list_elem_t event_callback_list[WIFI_EVENT_MAX][WIFI_EVENT_MAX_ROW];
2016-11-09 00:56:41 +00:00
#if CONFIG_WIFI_IND_USE_THREAD
2017-03-02 08:46:28 +00:00
static rtw_worker_thread_t wifi_worker_thread;
2016-11-09 00:56:41 +00:00
#endif
//----------------------------------------------------------------------------//
#if CONFIG_WIFI_IND_USE_THREAD
2017-03-02 08:46:28 +00:00
static rtw_result_t rtw_send_event_to_worker(int event_cmd, char *buf,
int buf_len, int flags) {
2016-11-09 00:56:41 +00:00
rtw_event_message_t message;
int i;
rtw_result_t ret = RTW_SUCCESS;
char *local_buf = NULL;
2017-03-02 08:46:28 +00:00
if (event_cmd >= WIFI_EVENT_MAX)
2016-11-09 00:56:41 +00:00
return RTW_BADARG;
2017-03-02 08:46:28 +00:00
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmd][i].handler == NULL)
2016-11-09 00:56:41 +00:00
continue;
2017-03-02 08:46:28 +00:00
message.function =
(event_handler_t) event_callback_list[event_cmd][i].handler;
2016-11-09 00:56:41 +00:00
message.buf_len = buf_len;
2017-03-02 08:46:28 +00:00
if (buf_len) {
local_buf = (char*) pvPortMalloc(buf_len);
if (local_buf == NULL)
2016-11-09 00:56:41 +00:00
return RTW_NOMEM;
memcpy(local_buf, buf, buf_len);
2017-03-02 08:46:28 +00:00
//debug_printf("Allocate %p(%d) for evcmd %d\n", local_buf, buf_len, event_cmd);
2016-11-09 00:56:41 +00:00
}
message.buf = local_buf;
message.flags = flags;
message.user_data = event_callback_list[event_cmd][i].handler_user_data;
ret = rtw_push_to_xqueue(&wifi_worker_thread.event_queue, &message, 0);
2017-03-02 08:46:28 +00:00
if (ret != RTW_SUCCESS) {
if (local_buf) {
warning_printf(
"rtw_send_event_to_worker: enqueue cmd %d failed and free %p(%d)\n",
event_cmd, local_buf, buf_len);
2016-11-09 00:56:41 +00:00
vPortFree(local_buf);
}
break;
}
}
return ret;
}
#else
static rtw_result_t rtw_indicate_event_handle(int event_cmd, char *buf, int buf_len, int flags)
{
rtw_event_handler_t handle = NULL;
int i;
2017-03-02 08:46:28 +00:00
2016-11-09 00:56:41 +00:00
if(event_cmd >= WIFI_EVENT_MAX)
2017-03-02 08:46:28 +00:00
return RTW_BADARG;
for(i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
2016-11-09 00:56:41 +00:00
handle = event_callback_list[event_cmd][i].handler;
if(handle == NULL)
2017-03-02 08:46:28 +00:00
continue;
2016-11-09 00:56:41 +00:00
handle(buf, buf_len, flags, event_callback_list[event_cmd][i].handler_user_data);
}
return RTW_SUCCESS;
}
#endif
2017-02-01 11:57:01 +00:00
#if 0 // test beacon
#include "gpio_api.h" // mbed
extern gpio_t gpio_led;
#endif
2016-11-09 00:56:41 +00:00
2017-03-09 08:23:37 +00:00
void wifi_indication(WIFI_EVENT_INDICATE event, unsigned char *buf, int buf_len,
2017-03-02 08:46:28 +00:00
int flags) {
2016-11-09 00:56:41 +00:00
//
// If upper layer application triggers additional operations on receiving of wext_wlan_indicate,
// please strictly check current stack size usage (by using uxTaskGetStackHighWaterMark() )
// , and tries not to share the same stack with wlan driver if remaining stack space is
// not available for the following operations.
// ex: using semaphore to notice another thread.
2017-03-02 08:46:28 +00:00
switch (event) {
case WIFI_EVENT_DISCONNECT:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): Disconnection indication received\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_CONNECT:
// For WPA/WPA2 mode, indication of connection does not mean data can be
// correctly transmitted or received. Data can be correctly transmitted or
// received only when 4-way handshake is done.
// Please check WIFI_EVENT_FOURWAY_HANDSHAKE_DONE event
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-02 08:46:28 +00:00
// Sample: return mac address
if (buf != NULL && buf_len == 6) {
info_printf(
2017-03-09 08:23:37 +00:00
"%s(%d): Connect indication received: %02x:%02x:%02x:%02x:%02x:%02x\n",
__func__, event, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
2017-03-02 08:46:28 +00:00
}
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_FOURWAY_HANDSHAKE_DONE:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-02 08:46:28 +00:00
if (buf != NULL)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): %s\n", __func__, event, buf);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_SCAN_RESULT_REPORT:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_SCAN_RESULT_REPORT\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_SCAN_DONE:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_SCAN_DONE\n", __func__, event);
2017-03-02 08:46:28 +00:00
//#if CONFIG_DEBUG_LOG > 3
// debug_printf("Time at start %d ms.\n", xTaskGetTickCount());
//#endif
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_RECONNECTION_FAIL:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): %s\n", __func__, event, buf);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_NO_NETWORK:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): %s\n", __func__, event, buf);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
#if CONFIG_ENABLE_P2P
2017-03-02 08:46:28 +00:00
case WIFI_EVENT_SEND_ACTION_DONE:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): %s\n", __func__, event, buf);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_RX_MGNT:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_RX_MGNT\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
#endif //CONFIG_ENABLE_P2P
2017-03-02 08:46:28 +00:00
case WIFI_EVENT_STA_ASSOC:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_STA_ASSOC\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
case WIFI_EVENT_STA_DISASSOC:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_STA_DISASSOC\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
#ifdef CONFIG_WPS
case WIFI_EVENT_STA_WPS_START:
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_STA_WPS_START\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
case WIFI_EVENT_WPS_FINISH:
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_WPS_FINISH\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
case WIFI_EVENT_EAPOL_RECVD:
#if(WIFI_INDICATE_MSG>0)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_EAPOL_RECVD\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
#endif
2017-03-02 08:46:28 +00:00
case WIFI_EVENT_BEACON_AFTER_DHCP:
2016-11-09 00:56:41 +00:00
#if(WIFI_INDICATE_MSG>1)
2017-03-09 08:23:37 +00:00
info_printf("%s(%d): WIFI_EVENT_BEACON_AFTER_DHCP\n", __func__, event);
2016-11-09 00:56:41 +00:00
#endif
2017-02-01 11:57:01 +00:00
#if 0 // test beacon
2017-03-02 08:46:28 +00:00
gpio_write(&gpio_led, 1);
gpio_write(&gpio_led, 0);
2017-02-01 11:57:01 +00:00
#endif
2017-03-02 08:46:28 +00:00
break;
2016-11-09 00:56:41 +00:00
}
#if CONFIG_INIC_EN
inic_indicate_event(event, buf, buf_len, flags);
#endif//CONFIG_INIC_EN
#if CONFIG_WIFI_IND_USE_THREAD
rtw_send_event_to_worker(event, buf, buf_len, flags);
#else
rtw_indicate_event_handle(event, buf, buf_len, flags);
#endif
}
2017-03-02 08:46:28 +00:00
void wifi_reg_event_handler(unsigned int event_cmds,
rtw_event_handler_t handler_func, void *handler_user_data) {
2016-11-09 00:56:41 +00:00
int i = 0, j = 0;
2017-03-02 08:46:28 +00:00
if (event_cmds < WIFI_EVENT_MAX) {
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmds][i].handler == NULL) {
for (j = 0; j < WIFI_EVENT_MAX_ROW; j++) {
if (event_callback_list[event_cmds][j].handler
== handler_func) {
return;
}
}
2016-11-09 00:56:41 +00:00
event_callback_list[event_cmds][i].handler = handler_func;
2017-03-02 08:46:28 +00:00
event_callback_list[event_cmds][i].handler_user_data =
handler_user_data;
2016-11-09 00:56:41 +00:00
return;
}
}
}
}
2017-03-02 08:46:28 +00:00
void wifi_unreg_event_handler(unsigned int event_cmds,
rtw_event_handler_t handler_func) {
2016-11-09 00:56:41 +00:00
int i;
2017-03-02 08:46:28 +00:00
if (event_cmds < WIFI_EVENT_MAX) {
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmds][i].handler == handler_func) {
2016-11-09 00:56:41 +00:00
event_callback_list[event_cmds][i].handler = NULL;
event_callback_list[event_cmds][i].handler_user_data = NULL;
return;
}
}
}
}
2017-03-02 08:46:28 +00:00
void init_event_callback_list() {
2016-11-09 00:56:41 +00:00
memset(event_callback_list, 0, sizeof(event_callback_list));
}
2017-03-02 08:46:28 +00:00
int wifi_manager_init() {
2016-11-09 00:56:41 +00:00
#if CONFIG_WIFI_IND_USE_THREAD
2017-03-02 08:46:28 +00:00
rtw_create_worker_thread(&wifi_worker_thread,
WIFI_MANAGER_PRIORITY,
WIFI_MANAGER_STACKSIZE,
WIFI_MANAGER_Q_SZ);
2016-11-09 00:56:41 +00:00
#endif
return 0;
}
2017-03-02 08:46:28 +00:00
void rtw_wifi_manager_deinit() {
2016-11-09 00:56:41 +00:00
#if CONFIG_WIFI_IND_USE_THREAD
rtw_delete_worker_thread(&wifi_worker_thread);
#endif
}