From 160c5bc87ffa59773d40edf1d8974674870eca82 Mon Sep 17 00:00:00 2001 From: dnc40085 Date: Sat, 27 Feb 2016 05:08:55 -0800 Subject: [PATCH] Made changes Suggested by TerryE Also, moved code that sets the default host name out of luaopen_wifi_init and into a separate function and added a post_task_low entry in it's place. Replaced some if test then return error lines with luaL_argcheck --- app/include/user_config.h | 10 ++--- app/modules/wifi.c | 87 ++++++++++++++----------------------- app/modules/wifi_common.c | 23 +--------- app/modules/wifi_common.h | 31 ++++++++++--- app/modules/wifi_eventmon.c | 47 ++++++++++---------- docs/en/modules/wifi.md | 2 +- 6 files changed, 87 insertions(+), 113 deletions(-) diff --git a/app/include/user_config.h b/app/include/user_config.h index 9d17a37709..6600f71345 100644 --- a/app/include/user_config.h +++ b/app/include/user_config.h @@ -93,13 +93,11 @@ //#define WIFI_STA_HOSTNAME "NodeMCU" //#define WIFI_STA_HOSTNAME_APPEND_MAC -<<<<<<< Upstream, based on upstream/dev //#define WIFI_SMART_ENABLE -======= -#define USE_WIFI_STATION_STATUS_MONITOR -#define USE_WIFI_SDK_EVENT_MONITOR -#define INCLUDE_WIFI_EVENT_DISCONNECT_REASON_LIST ->>>>>>> c1a0f0b Modified wifi module + +#define WIFI_STATION_STATUS_MONITOR_ENABLE +#define WIFI_SDK_EVENT_MONITOR_ENABLE +#define WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE #define STRBUF_DEFAULT_INCREMENT 32 diff --git a/app/modules/wifi.c b/app/modules/wifi.c index 2e5c945dc4..55c490ff21 100644 --- a/app/modules/wifi.c +++ b/app/modules/wifi.c @@ -113,13 +113,13 @@ static void wifi_scan_done(void *arg, STATUS status) if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel) { c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid)); - table_add_string(L, temp, "%s,%d,%d,%d", + wifi_add_sprintf_field(L, temp, "%s,%d,%d,%d", ssid, bss_link->rssi, bss_link->authmode, bss_link->channel); NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp);//00 00 00 00 00 00 } else//use old format(SSID : Authmode, RSSI, BSSID, Channel) { - table_add_string(L, ssid, "%d,%d,"MACSTR",%d", + wifi_add_sprintf_field(L, ssid, "%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel); NODE_DBG("%s : %s\n", ssid, temp); } @@ -708,13 +708,8 @@ static int wifi_station_setauto( lua_State* L ) unsigned a; a = luaL_checkinteger( L, 1 ); - - if ( a != 0 && a != 1 ) - return luaL_error( L, "wrong arg type" ); + luaL_argcheck(L, ( a == 0 || a == 1 ), 1, "0 or 1"); wifi_station_set_auto_connect(a); - if(a){ - // station_check_connect(0); - } return 0; } @@ -905,58 +900,39 @@ static int wifi_station_listap( lua_State* L ) // Lua: wifi.sta.gethostname() static int wifi_sta_gethostname( lua_State* L ) { - char* hostname = wifi_station_get_hostname(); - lua_pushstring(L, hostname); + char* hostname = wifi_station_get_hostname(); + lua_pushstring(L, hostname); return 1; } -static uint8 wifi_sta_sethostname(const char *hostname, size_t len) +static bool wifi_sta_sethostname(const char *hostname, size_t len) { - uint8 status; - if(hostname[0]!='-' && hostname[len-1]!='-' && (len >= 1 && len <= 32)) + //this function follows RFC 952 & RFC 1123 host name standards. + //the hostname must be 32 chars or less and first and last char must be alphanumeric + if (!isalnum(hostname[0]) || !isalnum(hostname[len-1]) || len > 32) { - uint8 i=0; - while(hostname[i]) - { - if(!(isalnum(hostname[i])||hostname[i]=='-')) - { - return 2; - } - i++; - } - status=wifi_station_set_hostname((char*)hostname); + return false; } - else + + for (int i=1; ibssid)); - table_add_string(L, temp, IPSTR, IP2STR(&station->ip)); + wifi_add_sprintf_field(L, temp, IPSTR, IP2STR(&station->ip)); next_station = STAILQ_NEXT(station, next); c_free(station); station = next_station; @@ -1255,7 +1231,7 @@ static const LUA_REG_TYPE wifi_station_map[] = { { LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) }, { LSTRKEY( "getrssi" ), LFUNCVAL( wifi_station_getrssi ) }, { LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) }, -#if defined(USE_WIFI_STATION_STATUS_MONITOR) && defined(LUA_USE_MODULES_WIFI_EVENTMON) +#if defined(WIFI_STATION_STATUS_MONITOR_ENABLE) && defined(LUA_USE_MODULES_WIFI_EVENTMON) { LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) }, //declared in wifi_eventmon.c { LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) }, //declared in wifi_eventmon.c { LSTRKEY( "eventMonStop" ), LFUNCVAL( wifi_station_event_mon_stop ) }, //declared in wifi_eventmon.c @@ -1300,7 +1276,7 @@ static const LUA_REG_TYPE wifi_map[] = { { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) }, { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) }, -#if defined(USE_WIFI_SDK_EVENT_MONITOR) && defined(LUA_USE_MODULES_WIFI_EVENTMON) +#if defined(WIFI_SDK_EVENT_MONITOR_ENABLE) && defined(LUA_USE_MODULES_WIFI_EVENTMON) { LSTRKEY( "eventmon" ), LROVAL( wifi_event_monitor_map ) }, //declared in wifi_eventmon.c #endif { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) }, @@ -1333,7 +1309,7 @@ static const LUA_REG_TYPE wifi_map[] = { { LNILKEY, LNILVAL } }; -int luaopen_wifi( lua_State *L ) +static void wifi_change_default_host_name(task_param_t param, uint8 priority) { #ifndef WIFI_STA_HOSTNAME char temp[32]; @@ -1343,9 +1319,7 @@ int luaopen_wifi( lua_State *L ) wifi_sta_sethostname((const char*)temp, strlen(temp)); #elif defined(WIFI_STA_HOSTNAME) && !defined(WIFI_STA_HOSTNAME_APPEND_MAC) - const char* hostname=WIFI_STA_HOSTNAME; - uint8 retval=wifi_sta_sethostname(hostname, strlen(hostname)); - if(retval!=1) + if(!wifi_sta_sethostname(WIFI_STA_HOSTNAME, strlen(WIFI_STA_HOSTNAME))) { char temp[32]; uint8_t mac[6]; @@ -1359,14 +1333,19 @@ int luaopen_wifi( lua_State *L ) uint8_t mac[6]; wifi_get_macaddr(STATION_IF, mac); c_sprintf(temp, "%s%X%X%X", WIFI_STA_HOSTNAME, (mac)[3], (mac)[4], (mac)[5]); - uint8 retval=wifi_sta_sethostname(temp, strlen(temp)); - if(retval!=1) + if(!wifi_sta_sethostname(temp, strlen(temp))) { c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]); wifi_sta_sethostname((const char*)temp, strlen(temp)); } #endif -#if defined(USE_WIFI_SDK_EVENT_MONITOR) && defined(LUA_USE_MODULES_WIFI_EVENTMON) + +} + +int luaopen_wifi( lua_State *L ) +{ + task_post_low(task_get_id(wifi_change_default_host_name), FALSE); +#if defined(WIFI_SDK_EVENT_MONITOR_ENABLE) && defined(LUA_USE_MODULES_WIFI_EVENTMON) wifi_eventmon_init(); #endif return 0; diff --git a/app/modules/wifi_common.c b/app/modules/wifi_common.c index 93dcf2253f..cba000ebba 100644 --- a/app/modules/wifi_common.c +++ b/app/modules/wifi_common.c @@ -1,11 +1,11 @@ #include "wifi_common.h" -void table_add_int(lua_State* L, char* name, lua_Integer integer) +void wifi_add_int_field(lua_State* L, char* name, lua_Integer integer) { lua_pushinteger(L, integer); lua_setfield(L, -2, name); } -void table_add_string(lua_State* L, char* name, char* string, ...) +void wifi_add_sprintf_field(lua_State* L, char* name, char* string, ...) { char buffer[256]; va_list arglist; @@ -15,22 +15,3 @@ void table_add_string(lua_State* L, char* name, char* string, ...) lua_pushstring(L, buffer); lua_setfield(L, -2, name); } - -void register_lua_cb(lua_State* L,int* cb_ref) -{ - int ref=luaL_ref(L, LUA_REGISTRYINDEX); - if( *cb_ref != LUA_NOREF && *cb_ref != ref) - { - luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref); - } - *cb_ref = ref; -} - -void unregister_lua_cb(lua_State* L, int* cb_ref) -{ - if(*cb_ref != LUA_NOREF) - { - luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref); - *cb_ref = LUA_NOREF; - } -} diff --git a/app/modules/wifi_common.h b/app/modules/wifi_common.h index b09c1c8e2c..720d411da9 100644 --- a/app/modules/wifi_common.h +++ b/app/modules/wifi_common.h @@ -11,12 +11,29 @@ #include "user_interface.h" #include "user_config.h" #include "c_stdio.h" -#include "rom.h" +#include "task/task.h" -void table_add_string(lua_State* L, char* name, char* string, ...); -void table_add_int(lua_State* L, char* name, lua_Integer integer); -void register_lua_cb(lua_State* L,int* cb_ref); -void unregister_lua_cb(lua_State* L, int* cb_ref); +void wifi_add_sprintf_field(lua_State* L, char* name, char* string, ...); +void wifi_add_int_field(lua_State* L, char* name, lua_Integer integer); + +static inline void register_lua_cb(lua_State* L,int* cb_ref) +{ + int ref=luaL_ref(L, LUA_REGISTRYINDEX); + if( *cb_ref != LUA_NOREF) + { + luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref); + } + *cb_ref = ref; +} + +static inline void unregister_lua_cb(lua_State* L, int* cb_ref) +{ + if(*cb_ref != LUA_NOREF) + { + luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref); + *cb_ref = LUA_NOREF; + } +} #ifdef NODE_DEBUG #define EVENT_DBG(...) c_printf(__VA_ARGS__) @@ -25,11 +42,11 @@ void unregister_lua_cb(lua_State* L, int* cb_ref); #endif #ifdef LUA_USE_MODULES_WIFI_EVENTMON - #ifdef USE_WIFI_SDK_EVENT_MONITOR + #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE extern const LUA_REG_TYPE wifi_event_monitor_map[]; void wifi_eventmon_init(); #endif - #ifdef USE_WIFI_STATION_STATUS_MONITOR + #ifdef WIFI_STATION_STATUS_MONITOR_ENABLE int wifi_station_event_mon_start(lua_State* L); int wifi_station_event_mon_reg(lua_State* L); void wifi_station_event_mon_stop(lua_State* L); diff --git a/app/modules/wifi_eventmon.c b/app/modules/wifi_eventmon.c index 119d88d3f4..fab4640b9c 100644 --- a/app/modules/wifi_eventmon.c +++ b/app/modules/wifi_eventmon.c @@ -12,13 +12,12 @@ #include "smart.h" #include "smartconfig.h" #include "user_config.h" -#include "task/task.h" #include "wifi_common.h" #if defined(LUA_USE_MODULES_WIFI) && defined(LUA_USE_MODULES_WIFI_EVENTMON) -#ifdef USE_WIFI_STATION_STATUS_MONITOR +#ifdef WIFI_STATION_STATUS_MONITOR_ENABLE //variables for wifi event monitor static int wifi_station_status_cb_ref[6] = {[0 ... 6-1] = LUA_NOREF}; @@ -110,7 +109,7 @@ int wifi_station_event_mon_start(lua_State* L) #endif -#ifdef USE_WIFI_SDK_EVENT_MONITOR +#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE //variables for wifi event monitor static task_handle_t wifi_event_monitor_task_id; //variable to hold task id for task handler(process_event_queue) @@ -150,7 +149,7 @@ static int wifi_event_monitor_register(lua_State* L) static void wifi_event_monitor_handle_event_cb(System_Event_t *evt) { - EVENT_DBG("\n\wifi_event_monitor_handle_event_cb is called\n"); + EVENT_DBG("\n\twifi_event_monitor_handle_event_cb is called\n"); if((wifi_event_cb_ref[evt->event] != LUA_NOREF) || ((wifi_event_cb_ref[EVENT_MAX] != LUA_NOREF) && !(evt->event == EVENT_STAMODE_CONNECTED || evt->event == EVENT_STAMODE_DISCONNECTED || @@ -205,9 +204,9 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri { case EVENT_STAMODE_CONNECTED: EVENT_DBG("\n\tSTAMODE_CONNECTED\n"); - table_add_string(L, "SSID", (char*)evt->event_info.connected.ssid); - table_add_string(L, "BSSID", MACSTR, MAC2STR(evt->event_info.connected.bssid)); - table_add_int(L, "channel", evt->event_info.connected.channel); + wifi_add_sprintf_field(L, "SSID", (char*)evt->event_info.connected.ssid); + wifi_add_sprintf_field(L, "BSSID", MACSTR, MAC2STR(evt->event_info.connected.bssid)); + wifi_add_int_field(L, "channel", evt->event_info.connected.channel); EVENT_DBG("\tConnected to SSID %s, Channel %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel); @@ -215,9 +214,9 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_STAMODE_DISCONNECTED: EVENT_DBG("\n\tSTAMODE_DISCONNECTED\n"); - table_add_string(L, "SSID", (char*)evt->event_info.disconnected.ssid); - table_add_int(L, "reason", evt->event_info.disconnected.reason); - table_add_string(L, "BSSID", MACSTR, MAC2STR(evt->event_info.disconnected.bssid)); + wifi_add_sprintf_field(L, "SSID", (char*)evt->event_info.disconnected.ssid); + wifi_add_int_field(L, "reason", evt->event_info.disconnected.reason); + wifi_add_sprintf_field(L, "BSSID", MACSTR, MAC2STR(evt->event_info.disconnected.bssid)); EVENT_DBG("\tDisconnect from SSID %s, reason %d\n", evt->event_info.disconnected.ssid, evt->event_info.disconnected.reason); @@ -225,8 +224,8 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_STAMODE_AUTHMODE_CHANGE: EVENT_DBG("\n\tSTAMODE_AUTHMODE_CHANGE\n"); - table_add_int(L, "old_auth_mode", evt->event_info.auth_change.old_mode); - table_add_int(L, "new_auth_mode", evt->event_info.auth_change.new_mode); + wifi_add_int_field(L, "old_auth_mode", evt->event_info.auth_change.old_mode); + wifi_add_int_field(L, "new_auth_mode", evt->event_info.auth_change.new_mode); EVENT_DBG("\tAuthmode: %u -> %u\n", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); @@ -234,9 +233,9 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_STAMODE_GOT_IP: EVENT_DBG("\n\tGOT_IP\n"); - table_add_string(L, "IP", IPSTR, IP2STR(&evt->event_info.got_ip.ip)); - table_add_string(L, "netmask", IPSTR, IP2STR(&evt->event_info.got_ip.mask)); - table_add_string(L, "gateway", IPSTR, IP2STR(&evt->event_info.got_ip.gw)); + wifi_add_sprintf_field(L, "IP", IPSTR, IP2STR(&evt->event_info.got_ip.ip)); + wifi_add_sprintf_field(L, "netmask", IPSTR, IP2STR(&evt->event_info.got_ip.mask)); + wifi_add_sprintf_field(L, "gateway", IPSTR, IP2STR(&evt->event_info.got_ip.gw)); EVENT_DBG("\tIP:" IPSTR ",Mask:" IPSTR ",GW:" IPSTR "\n", IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), @@ -249,8 +248,8 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_SOFTAPMODE_STACONNECTED: EVENT_DBG("\n\tSOFTAPMODE_STACONNECTED\n"); - table_add_string(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_connected.mac)); - table_add_int(L, "AID", evt->event_info.sta_connected.aid); + wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_connected.mac)); + wifi_add_int_field(L, "AID", evt->event_info.sta_connected.aid); EVENT_DBG("\tStation: " MACSTR "join, AID = %d\n", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); @@ -258,8 +257,8 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_SOFTAPMODE_STADISCONNECTED: EVENT_DBG("\n\tSOFTAPMODE_STADISCONNECTED\n"); - table_add_string(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_disconnected.mac)); - table_add_int(L, "AID", evt->event_info.sta_disconnected.aid); + wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.sta_disconnected.mac)); + wifi_add_int_field(L, "AID", evt->event_info.sta_disconnected.aid); EVENT_DBG("\tstation: " MACSTR "leave, AID = %d\n", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); @@ -267,8 +266,8 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri case EVENT_SOFTAPMODE_PROBEREQRECVED: EVENT_DBG("\n\tSOFTAPMODE_PROBEREQRECVED\n"); - table_add_string(L, "MAC", MACSTR, MAC2STR(evt->event_info.ap_probereqrecved.mac)); - table_add_int(L, "RSSI", evt->event_info.ap_probereqrecved.rssi); + wifi_add_sprintf_field(L, "MAC", MACSTR, MAC2STR(evt->event_info.ap_probereqrecved.mac)); + wifi_add_int_field(L, "RSSI", evt->event_info.ap_probereqrecved.rssi); EVENT_DBG("Station PROBEREQ: " MACSTR " RSSI = %d\n", MAC2STR(evt->event_info.ap_probereqrecved.mac), evt->event_info.ap_probereqrecved.rssi); @@ -276,7 +275,7 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri default://if event is not implemented, push table with userdata containing event data EVENT_DBG("\n\tswitch/case default\n"); - table_add_string(L, "info", "event %u not implemented", evt->event); + wifi_add_sprintf_field(L, "info", "event %u not implemented", evt->event); break; } @@ -298,7 +297,7 @@ static void wifi_event_monitor_process_event_queue(task_param_t param, uint8 pri c_free(temp); //free memory used by queue structure } -#ifdef INCLUDE_WIFI_EVENT_DISCONNECT_REASON_LIST +#ifdef WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE static const LUA_REG_TYPE wifi_event_monitor_reason_map[] = { { LSTRKEY( "UNSPECIFIED" ), LNUMVAL( REASON_UNSPECIFIED ) }, @@ -346,7 +345,7 @@ const LUA_REG_TYPE wifi_event_monitor_map[] = { LSTRKEY( "AP_STADISCONNECTED" ), LNUMVAL( EVENT_SOFTAPMODE_STADISCONNECTED ) }, { LSTRKEY( "AP_PROBEREQRECVED" ), LNUMVAL( EVENT_SOFTAPMODE_PROBEREQRECVED ) }, { LSTRKEY( "EVENT_MAX" ), LNUMVAL( EVENT_MAX ) }, -#ifdef INCLUDE_WIFI_EVENT_DISCONNECT_REASON_LIST +#ifdef WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE { LSTRKEY( "reason" ), LROVAL( wifi_event_monitor_reason_map ) }, #endif { LNILKEY, LNILVAL } diff --git a/docs/en/modules/wifi.md b/docs/en/modules/wifi.md index a9b2286944..5a32cbecac 100644 --- a/docs/en/modules/wifi.md +++ b/docs/en/modules/wifi.md @@ -665,7 +665,7 @@ Sets station hostname. `hostname` must only contain letters, numbers and hyphens('-') and be 32 characters or less with first and last character being alphanumeric #### Returns -true if hostname was successfully set, false otherwise +`nil` #### Example ```lua