Skip to content

Commit

Permalink
Made changes Suggested by TerryE
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dnc40085 committed Mar 5, 2016
1 parent 819a22e commit 160c5bc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 113 deletions.
10 changes: 4 additions & 6 deletions app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
87 changes: 33 additions & 54 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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; i<len; i++)
{
return 2;
//characters in the middle of the host name can be alphanumeric or a hyphen(-) only
if (!(isalnum(hostname[i]) || hostname[i]=='-'))
{
return false;
}
}
return status;
return wifi_station_set_hostname((char*)hostname);
}

// Lua: wifi.sta.sethostname()
static int wifi_sta_sethostname_lua( lua_State* L )
{
size_t len;
const char *hostname = luaL_checklstring(L, 1, &len);
uint8 retval = wifi_sta_sethostname(hostname, len);
NODE_DBG("\n\tstring is: \"%s\"\n\tlength is: %u\t wifi_sta_sethostname returned: %u\n", hostname, len, retval);
if(retval==0)
{
lua_pushboolean(L, 0);
return 1;
}
else if (retval==1)
{
lua_pushboolean(L, 1);
return 1;
}
else if (retval==2)
{
return luaL_error(L, "Invalid hostname!");
}
luaL_argcheck(L, wifi_sta_sethostname(hostname, len), 1, "Invalid hostname");
return 0;
}


// Lua: wifi.sta.status()
static int wifi_station_status( lua_State* L )
{
Expand Down Expand Up @@ -1178,7 +1154,7 @@ static int wifi_ap_listclient( lua_State* L )
while (station != NULL)
{
c_sprintf(temp, MACSTR, MAC2STR(station->bssid));
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ) },
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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;
Expand Down
23 changes: 2 additions & 21 deletions app/modules/wifi_common.c
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
31 changes: 24 additions & 7 deletions app/modules/wifi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 160c5bc

Please sign in to comment.