wifi_eventmon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // WiFi Event Monitor
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "c_string.h"
  6. #include "c_stdlib.h"
  7. #include "c_types.h"
  8. #include "user_interface.h"
  9. #include "smart.h"
  10. #include "smartconfig.h"
  11. #include "user_config.h"
  12. #include "wifi_common.h"
  13. #if defined(LUA_USE_MODULES_WIFI)
  14. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  15. //variables for wifi event monitor
  16. static task_handle_t wifi_event_monitor_task_id; //variable to hold task id for task handler(process_event_queue)
  17. typedef struct evt_queue{
  18. System_Event_t *evt;
  19. struct evt_queue * next;
  20. }evt_queue_t; //structure to hold pointers to event info and next item in queue
  21. static evt_queue_t *wifi_event_queue_head; //pointer to beginning of queue
  22. static evt_queue_t *wifi_event_queue_tail; //pointer to end of queue
  23. static int wifi_event_cb_ref[EVENT_MAX+1] = { [0 ... EVENT_MAX] = LUA_NOREF}; //holds references to registered Lua callbacks
  24. #ifdef LUA_USE_MODULES_WIFI_MONITOR
  25. static int (*hook_fn)(System_Event_t *);
  26. void wifi_event_monitor_register_hook(int (*fn)(System_Event_t*)) {
  27. hook_fn = fn;
  28. }
  29. #endif
  30. // wifi.eventmon.register()
  31. int wifi_event_monitor_register(lua_State* L)
  32. {
  33. uint8 id = (uint8)luaL_checknumber(L, 1);
  34. if ( id > EVENT_MAX ) //Check if user is trying to register a callback for a valid event.
  35. {
  36. return luaL_error( L, "valid wifi events:0-%d", EVENT_MAX );
  37. }
  38. else
  39. {
  40. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) //check if 2nd item on stack is a function
  41. {
  42. lua_pushvalue(L, 2); // copy argument (func) to the top of stack
  43. register_lua_cb(L, &wifi_event_cb_ref[id]); //pop function from top of the stack, register it in the LUA_REGISTRY, then assign lua_ref to wifi_event_cb_ref[id]
  44. }
  45. else // unregister user's callback
  46. {
  47. unregister_lua_cb(L, &wifi_event_cb_ref[id]);
  48. }
  49. return 0;
  50. }
  51. }
  52. static void wifi_event_monitor_handle_event_cb(System_Event_t *evt)
  53. {
  54. EVENT_DBG("\n\twifi_event_monitor_handle_event_cb is called\n");
  55. #ifdef LUA_USE_MODULES_WIFI_MONITOR
  56. if (hook_fn && hook_fn(evt)) {
  57. return;
  58. }
  59. #endif
  60. if((wifi_event_cb_ref[evt->event] != LUA_NOREF) || ((wifi_event_cb_ref[EVENT_MAX] != LUA_NOREF) &&
  61. !(evt->event == EVENT_STAMODE_CONNECTED || evt->event == EVENT_STAMODE_DISCONNECTED ||
  62. evt->event == EVENT_STAMODE_AUTHMODE_CHANGE || evt->event == EVENT_STAMODE_GOT_IP ||
  63. evt->event == EVENT_STAMODE_DHCP_TIMEOUT || evt->event == EVENT_SOFTAPMODE_STACONNECTED ||
  64. evt->event == EVENT_SOFTAPMODE_STADISCONNECTED || evt->event == EVENT_SOFTAPMODE_PROBEREQRECVED ||
  65. evt->event == EVENT_OPMODE_CHANGED)))
  66. {
  67. evt_queue_t *temp = (evt_queue_t*)c_malloc(sizeof(evt_queue_t)); //allocate memory for new queue item
  68. temp->evt = (System_Event_t*)c_malloc(sizeof(System_Event_t)); //allocate memory to hold event structure
  69. if(!temp || !temp->evt)
  70. {
  71. luaL_error(lua_getstate(), "wifi.eventmon malloc: out of memory");
  72. return;
  73. }
  74. c_memcpy(temp->evt, evt, sizeof(System_Event_t)); //copy event data to new struct
  75. if(wifi_event_queue_head == NULL && wifi_event_queue_tail == NULL)// if queue is empty add item to queue
  76. {
  77. wifi_event_queue_head = wifi_event_queue_tail = temp;
  78. EVENT_DBG("\n\tqueue empty, adding event and posting task\n");
  79. task_post_low(wifi_event_monitor_task_id, false);
  80. }
  81. else //if queue is not empty append item to end of queue
  82. {
  83. wifi_event_queue_tail->next=temp;
  84. wifi_event_queue_tail=temp;
  85. EVENT_DBG("\n\tqueue not empty, appending queue\n");
  86. }
  87. }
  88. }
  89. static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 priority)
  90. {
  91. lua_State* L = lua_getstate();
  92. evt_queue_t *temp = wifi_event_queue_head; //copy event_queue_head pointer to temporary pointer
  93. System_Event_t *evt = temp->evt; //copy event data pointer to temporary pointer
  94. EVENT_DBG("\t\tevent %u\n", evt->event);
  95. if(wifi_event_cb_ref[evt->event] != LUA_NOREF) // check if user has registered a callback
  96. {
  97. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_event_cb_ref[evt->event]); //get user's callback
  98. }
  99. else if((wifi_event_cb_ref[EVENT_MAX]!=LUA_NOREF) &&
  100. !(evt->event==EVENT_STAMODE_CONNECTED||evt->event==EVENT_STAMODE_DISCONNECTED||
  101. evt->event==EVENT_STAMODE_AUTHMODE_CHANGE||evt->event==EVENT_STAMODE_GOT_IP||
  102. evt->event==EVENT_STAMODE_DHCP_TIMEOUT||evt->event==EVENT_SOFTAPMODE_STACONNECTED||
  103. evt->event==EVENT_SOFTAPMODE_STADISCONNECTED||evt->event==EVENT_SOFTAPMODE_PROBEREQRECVED))
  104. { //if user has registered an EVENT_MAX(default) callback and event is not implemented...
  105. lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_event_cb_ref[EVENT_MAX]); //get user's callback
  106. }
  107. lua_newtable( L );
  108. switch (evt->event)
  109. {
  110. case EVENT_STAMODE_CONNECTED:
  111. EVENT_DBG("\n\tSTAMODE_CONNECTED\n");
  112. wifi_add_sprintf_field(L, "SSID", (char*)evt->event_info.connected.ssid);
  113. wifi_add_sprintf_field(L, "BSSID", MACSTR, MAC2STR(evt->event_info.connected.bssid));
  114. wifi_add_int_field(L, "channel", evt->event_info.connected.channel);
  115. EVENT_DBG("\tConnected to SSID %s, Channel %d\n",
  116. evt->event_info.connected.ssid,
  117. evt->event_info.connected.channel);
  118. break;
  119. case EVENT_STAMODE_DISCONNECTED:
  120. EVENT_DBG("\n\tSTAMODE_DISCONNECTED\n");
  121. wifi_add_sprintf_field(L, "SSID", (char*)evt->event_info.disconnected.ssid);
  122. wifi_add_int_field(L, "reason", evt->event_info.disconnected.reason);
  123. wifi_add_sprintf_field(L, "BSSID", MACSTR, MAC2STR(evt->event_info.disconnected.bssid));
  124. EVENT_DBG("\tDisconnect from SSID %s, reason %d\n",
  125. evt->event_info.disconnected.ssid,
  126. evt->event_info.disconnected.reason);
  127. break;
  128. case EVENT_STAMODE_AUTHMODE_CHANGE:
  129. EVENT_DBG("\n\tSTAMODE_AUTHMODE_CHANGE\n");
  130. wifi_add_int_field(L, "old_auth_mode", evt->event_info.auth_change.old_mode);
  131. wifi_add_int_field(L, "new_auth_mode", evt->event_info.auth_change.new_mode);
  132. EVENT_DBG("\tAuthmode: %u -> %u\n",
  133. evt->event_info.auth_change.old_mode,
  134. evt->event_info.auth_change.new_mode);
  135. break;
  136. case EVENT_STAMODE_GOT_IP:
  137. EVENT_DBG("\n\tGOT_IP\n");
  138. wifi_add_sprintf_field(L, "IP", IPSTR, IP2STR(&evt->event_info.got_ip.ip));
  139. wifi_add_sprintf_field(L, "netmask", IPSTR, IP2STR(&evt->event_info.got_ip.mask));
  140. wifi_add_sprintf_field(L, "gateway", IPSTR, IP2STR(&evt->event_info.got_ip.gw));
  141. EVENT_DBG("\tIP:" IPSTR ",Mask:" IPSTR ",GW:" IPSTR "\n",
  142. IP2STR(&evt->event_info.got_ip.ip),
  143. IP2STR(&evt->event_info.got_ip.mask),
  144. IP2STR(&evt->event_info.got_ip.gw));
  145. break;
  146. case EVENT_STAMODE_DHCP_TIMEOUT:
  147. EVENT_DBG("\n\tSTAMODE_DHCP_TIMEOUT\n");
  148. break;
  149. case EVENT_SOFTAPMODE_STACONNECTED:
  150. EVENT_DBG("\n\tSOFTAPMODE_STACONNECTED\n");
  151. wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_connected.mac));
  152. wifi_add_int_field(L, "AID", evt->event_info.sta_connected.aid);
  153. EVENT_DBG("\tStation: " MACSTR "join, AID = %d\n",
  154. MAC2STR(evt->event_info.sta_connected.mac),
  155. evt->event_info.sta_connected.aid);
  156. break;
  157. case EVENT_SOFTAPMODE_STADISCONNECTED:
  158. EVENT_DBG("\n\tSOFTAPMODE_STADISCONNECTED\n");
  159. wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_disconnected.mac));
  160. wifi_add_int_field(L, "AID", evt->event_info.sta_disconnected.aid);
  161. EVENT_DBG("\tstation: " MACSTR "leave, AID = %d\n",
  162. MAC2STR(evt->event_info.sta_disconnected.mac),
  163. evt->event_info.sta_disconnected.aid);
  164. break;
  165. case EVENT_SOFTAPMODE_PROBEREQRECVED:
  166. EVENT_DBG("\n\tSOFTAPMODE_PROBEREQRECVED\n");
  167. wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.ap_probereqrecved.mac));
  168. wifi_add_int_field(L, "RSSI", evt->event_info.ap_probereqrecved.rssi);
  169. EVENT_DBG("Station PROBEREQ: " MACSTR " RSSI = %d\n",
  170. MAC2STR(evt->event_info.ap_probereqrecved.mac),
  171. evt->event_info.ap_probereqrecved.rssi);
  172. break;
  173. case EVENT_OPMODE_CHANGED:
  174. EVENT_DBG("\n\tOPMODE_CHANGED\n");
  175. wifi_add_int_field(L, "old_mode", evt->event_info.opmode_changed.old_opmode);
  176. wifi_add_int_field(L, "new_mode", evt->event_info.opmode_changed.new_opmode);
  177. EVENT_DBG("\topmode: %u -> %u\n",
  178. evt->event_info.opmode_changed.old_opmode,
  179. evt->event_info.opmode_changed.new_opmode);
  180. break;
  181. default://if event is not implemented, return event id
  182. EVENT_DBG("\n\tswitch/case default\n");
  183. wifi_add_sprintf_field(L, "info", "event %u not implemented", evt->event);
  184. break;
  185. }
  186. lua_call(L, 1, 0); //execute user's callback and pass Lua table
  187. if (wifi_event_queue_head == wifi_event_queue_tail) //if queue is empty..
  188. {
  189. wifi_event_queue_head = wifi_event_queue_tail = NULL; //set queue pointers to NULL
  190. EVENT_DBG("\n\tQueue empty\n");
  191. }
  192. else //if queue is not empty...
  193. {
  194. wifi_event_queue_head = wifi_event_queue_head->next; //append item to end of queue
  195. EVENT_DBG("\n\tmore in queue, posting task...\n");
  196. task_post_low(wifi_event_monitor_task_id, false); //post task to process next item in queue
  197. }
  198. c_free(evt); //free memory used by event structure
  199. c_free(temp); //free memory used by queue structure
  200. }
  201. #ifdef WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE
  202. static const LUA_REG_TYPE wifi_event_monitor_reason_map[] =
  203. {
  204. { LSTRKEY( "UNSPECIFIED" ), LNUMVAL( REASON_UNSPECIFIED ) },
  205. { LSTRKEY( "AUTH_EXPIRE" ), LNUMVAL( REASON_AUTH_EXPIRE ) },
  206. { LSTRKEY( "AUTH_LEAVE" ), LNUMVAL( REASON_AUTH_LEAVE ) },
  207. { LSTRKEY( "ASSOC_EXPIRE" ), LNUMVAL( REASON_ASSOC_EXPIRE ) },
  208. { LSTRKEY( "ASSOC_TOOMANY" ), LNUMVAL( REASON_ASSOC_TOOMANY ) },
  209. { LSTRKEY( "NOT_AUTHED" ), LNUMVAL( REASON_NOT_AUTHED ) },
  210. { LSTRKEY( "NOT_ASSOCED" ), LNUMVAL( REASON_NOT_ASSOCED ) },
  211. { LSTRKEY( "ASSOC_LEAVE" ), LNUMVAL( REASON_ASSOC_LEAVE ) },
  212. { LSTRKEY( "ASSOC_NOT_AUTHED" ), LNUMVAL( REASON_ASSOC_NOT_AUTHED ) },
  213. { LSTRKEY( "DISASSOC_PWRCAP_BAD" ), LNUMVAL( REASON_DISASSOC_PWRCAP_BAD ) },
  214. { LSTRKEY( "DISASSOC_SUPCHAN_BAD" ), LNUMVAL( REASON_DISASSOC_SUPCHAN_BAD ) },
  215. { LSTRKEY( "IE_INVALID" ), LNUMVAL( REASON_IE_INVALID ) },
  216. { LSTRKEY( "MIC_FAILURE" ), LNUMVAL( REASON_MIC_FAILURE ) },
  217. { LSTRKEY( "4WAY_HANDSHAKE_TIMEOUT" ), LNUMVAL( REASON_4WAY_HANDSHAKE_TIMEOUT ) },
  218. { LSTRKEY( "GROUP_KEY_UPDATE_TIMEOUT" ), LNUMVAL( REASON_GROUP_KEY_UPDATE_TIMEOUT ) },
  219. { LSTRKEY( "IE_IN_4WAY_DIFFERS" ), LNUMVAL( REASON_IE_IN_4WAY_DIFFERS ) },
  220. { LSTRKEY( "GROUP_CIPHER_INVALID" ), LNUMVAL( REASON_GROUP_CIPHER_INVALID ) },
  221. { LSTRKEY( "PAIRWISE_CIPHER_INVALID" ), LNUMVAL( REASON_PAIRWISE_CIPHER_INVALID ) },
  222. { LSTRKEY( "AKMP_INVALID" ), LNUMVAL( REASON_AKMP_INVALID ) },
  223. { LSTRKEY( "UNSUPP_RSN_IE_VERSION" ), LNUMVAL( REASON_UNSUPP_RSN_IE_VERSION ) },
  224. { LSTRKEY( "INVALID_RSN_IE_CAP" ), LNUMVAL( REASON_INVALID_RSN_IE_CAP ) },
  225. { LSTRKEY( "802_1X_AUTH_FAILED" ), LNUMVAL( REASON_802_1X_AUTH_FAILED ) },
  226. { LSTRKEY( "CIPHER_SUITE_REJECTED" ), LNUMVAL( REASON_CIPHER_SUITE_REJECTED ) },
  227. { LSTRKEY( "BEACON_TIMEOUT" ), LNUMVAL( REASON_BEACON_TIMEOUT ) },
  228. { LSTRKEY( "NO_AP_FOUND" ), LNUMVAL( REASON_NO_AP_FOUND ) },
  229. { LSTRKEY( "AUTH_FAIL" ), LNUMVAL( REASON_AUTH_FAIL ) },
  230. { LSTRKEY( "ASSOC_FAIL" ), LNUMVAL( REASON_ASSOC_FAIL ) },
  231. { LSTRKEY( "HANDSHAKE_TIMEOUT" ), LNUMVAL( REASON_HANDSHAKE_TIMEOUT ) },
  232. { LNILKEY, LNILVAL }
  233. };
  234. #endif
  235. const LUA_REG_TYPE wifi_event_monitor_map[] =
  236. {
  237. { LSTRKEY( "register" ), LFUNCVAL( wifi_event_monitor_register ) },
  238. { LSTRKEY( "unregister" ), LFUNCVAL( wifi_event_monitor_register ) },
  239. { LSTRKEY( "STA_CONNECTED" ), LNUMVAL( EVENT_STAMODE_CONNECTED ) },
  240. { LSTRKEY( "STA_DISCONNECTED" ), LNUMVAL( EVENT_STAMODE_DISCONNECTED ) },
  241. { LSTRKEY( "STA_AUTHMODE_CHANGE" ), LNUMVAL( EVENT_STAMODE_AUTHMODE_CHANGE ) },
  242. { LSTRKEY( "STA_GOT_IP" ), LNUMVAL( EVENT_STAMODE_GOT_IP ) },
  243. { LSTRKEY( "STA_DHCP_TIMEOUT" ), LNUMVAL( EVENT_STAMODE_DHCP_TIMEOUT ) },
  244. { LSTRKEY( "AP_STACONNECTED" ), LNUMVAL( EVENT_SOFTAPMODE_STACONNECTED ) },
  245. { LSTRKEY( "AP_STADISCONNECTED" ), LNUMVAL( EVENT_SOFTAPMODE_STADISCONNECTED ) },
  246. { LSTRKEY( "AP_PROBEREQRECVED" ), LNUMVAL( EVENT_SOFTAPMODE_PROBEREQRECVED ) },
  247. { LSTRKEY( "WIFI_MODE_CHANGED" ), LNUMVAL( EVENT_OPMODE_CHANGED ) },
  248. { LSTRKEY( "EVENT_MAX" ), LNUMVAL( EVENT_MAX ) },
  249. #ifdef WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE
  250. { LSTRKEY( "reason" ), LROVAL( wifi_event_monitor_reason_map ) },
  251. #endif
  252. { LNILKEY, LNILVAL }
  253. };
  254. void wifi_eventmon_init()
  255. {
  256. wifi_event_monitor_task_id = task_get_id(wifi_event_monitor_process_event_queue);//get task id from task interface
  257. wifi_set_event_handler_cb(wifi_event_monitor_handle_event_cb);
  258. return;
  259. }
  260. #endif
  261. #endif