Skip to content

Commit

Permalink
Add functionality requested in issue nodemcu#1131
Browse files Browse the repository at this point in the history
  • Loading branch information
dnc40085 committed Apr 5, 2017
1 parent b2cbf52 commit 765cef4
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 8 deletions.
192 changes: 192 additions & 0 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@
#include "wifi_common.h"


void stackDump (lua_State *L) {
int i=lua_gettop(L);
c_printf("\n\t---------------- Stack Dump ----------------\n" );
while( i ) {
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING:
c_printf("\t%d:`%s'\n", i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
c_printf("\t%d: %s\n",i,lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
c_printf("\t%d: %g\n", i, lua_tonumber(L, i));
break;
default: c_printf("\t%d: %s\n", i, lua_typename(L, t)); break;
}
i--;
}
c_printf("\t--------------- Stack Dump Finished ---------------\n" );
}


#ifdef WIFI_SMART_ENABLE
#include "smart.h"
#include "smartconfig.h"
Expand Down Expand Up @@ -833,6 +856,102 @@ static int wifi_station_config( lua_State* L )
}
lua_pop(L, 1);

#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE

lua_State* L_temp = NULL;

lua_getfield(L, 1, "connect_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_STAMODE_CONNECTED);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "connect_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "disconnect_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_STAMODE_DISCONNECTED);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "disconnect_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "authmode_change_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_STAMODE_AUTHMODE_CHANGE);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "authmode_change_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "got_ip_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_STAMODE_GOT_IP);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "gotip_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "dhcp_timeout_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_STAMODE_DHCP_TIMEOUT);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "dhcp_timeout_cb:not function");
}
}
lua_pop(L, 1);

#endif

}
else //to be deprecated
{
Expand Down Expand Up @@ -928,13 +1047,27 @@ static int wifi_station_config( lua_State* L )
// Lua: wifi.sta.connect()
static int wifi_station_connect4lua( lua_State* L )
{
#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
if(lua_isfunction(L, 1)){
lua_pushnumber(L, EVENT_STAMODE_CONNECTED);
lua_pushvalue(L, 1);
wifi_event_monitor_register(L);
}
#endif
wifi_station_connect();
return 0;
}

// Lua: wifi.sta.disconnect()
static int wifi_station_disconnect4lua( lua_State* L )
{
#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
if(lua_isfunction(L, 1)){
lua_pushnumber(L, EVENT_STAMODE_DISCONNECTED);
lua_pushvalue(L, 1);
wifi_event_monitor_register(L);
}
#endif
wifi_station_disconnect();
return 0;
}
Expand Down Expand Up @@ -1507,6 +1640,65 @@ static int wifi_ap_config( lua_State* L )
}
lua_pop(L, 1);

#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE

lua_State* L_temp = NULL;

lua_getfield(L, 1, "staconnected_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_SOFTAPMODE_STACONNECTED);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "staconnected_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "stadisconnected_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_SOFTAPMODE_STADISCONNECTED);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "stadisconnected_cb:not function");
}
}
lua_pop(L, 1);

lua_getfield(L, 1, "probereq_cb");
if (!lua_isnil(L, -1))
{
if (lua_isfunction(L, -1))
{
L_temp = lua_newthread(L);
lua_pushnumber(L, EVENT_SOFTAPMODE_PROBEREQRECVED);
lua_pushvalue(L, -3);
lua_xmove(L, L_temp, 2);
wifi_event_monitor_register(L_temp);
}
else
{
return luaL_argerror(L, 1, "probereq_cb:not function");
}
}
lua_pop(L, 1);

#endif

#if defined(WIFI_DEBUG)
char debug_temp[sizeof(config.password)+1];
Expand Down
3 changes: 2 additions & 1 deletion app/modules/wifi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "c_stdio.h"
#include "task/task.h"

//#define WIFI_DEBUG
#define WIFI_DEBUG
//#define EVENT_DEBUG

void wifi_add_sprintf_field(lua_State* L, char* name, char* string, ...);
Expand Down Expand Up @@ -64,6 +64,7 @@ enum wifi_suspension_state
#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
extern const LUA_REG_TYPE wifi_event_monitor_map[];
void wifi_eventmon_init();
int wifi_event_monitor_register(lua_State* L);
#endif
#ifdef WIFI_STATION_STATUS_MONITOR_ENABLE
int wifi_station_event_mon_start(lua_State* L);
Expand Down
2 changes: 1 addition & 1 deletion app/modules/wifi_eventmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static evt_queue_t *wifi_event_queue_tail; //pointer to end of queue
static int wifi_event_cb_ref[EVENT_MAX+1] = { [0 ... EVENT_MAX] = LUA_NOREF}; //holds references to registered Lua callbacks

// wifi.eventmon.register()
static int wifi_event_monitor_register(lua_State* L)
int wifi_event_monitor_register(lua_State* L)
{
uint8 id = (uint8)luaL_checknumber(L, 1);
if ( id > EVENT_MAX ) //Check if user is trying to register a callback for a valid event.
Expand Down
54 changes: 48 additions & 6 deletions docs/en/modules/wifi.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,29 @@ Sets the WiFi station configuration.
- "DE AD BE EF 7A C0"
- `save` Save station configuration to flash.
- `true` configuration **will** be retained through power cycle.
- `false` configuration **will not** be retained through power cycle. (Default)
- `false` configuration **will not** be retained through power cycle. (Default).
- Event callbacks will only be available if `WIFI_SDK_EVENT_MONITOR_ENABLE` is enabled in `user_config.h`
- `connected_cb`: Callback to execute when station is connected to an access point.
- Items returned in table :
- `SSID`: SSID of access point. (format: string)
- `BSSID`: BSSID of access point. (format: string)
- `channel`: The channel the access point is on. (format: number)
- `disconnected_cb`: Callback to execute when station is disconnected from an access point.
- Items returned in table :
- `SSID`: SSID of access point. (format: string)
- `BSSID`: BSSID of access point. (format: string)
- `REASON`: See [wifi.eventmon.reason](#wifieventmonreason) below. (format: number)
- `authmode_change_cb`: Callback to execute when the access point has changed authorization mode.
- Items returned in table :
- `old_auth_mode`: Old wifi authorization mode. (format: number)
- `new_auth_mode`: New wifi authorization mode. (format: number)
- `got_ip_cb`: Callback to execute when the station received an IP address from the access point.
- Items returned in table :
- `IP`: The IP address assigned to the station. (format: string)
- `netmask`: Subnet mask. (format: string)
- `gateway`: The IP address of the access point the station is connected to. (format: string)
- `dhcp_timeout_cb`: Station DHCP request has timed out.
- Blank table is returned.

#### Returns
- `true` Success
Expand Down Expand Up @@ -449,10 +471,14 @@ wifi.sta.config(station_cfg)
Connects to the configured AP in station mode. You only ever need to call this if auto-connect was disabled in [`wifi.sta.config()`](#wifistaconfig).

#### Syntax
`wifi.sta.connect()`
`wifi.sta.connect([connected_cb])`

#### Parameters
none
- `connected_cb`: Callback to execute when station is connected to an access point.
- Items returned in table :
- `SSID`: SSID of access point. (format: string)
- `BSSID`: BSSID of access point. (format: string)
- `channel`: The channel the access point is on. (format: number)

#### Returns
`nil`
Expand All @@ -469,10 +495,14 @@ Disconnects from AP in station mode.
Please note that disconnecting from Access Point does not reduce power consumption. If power saving is your goal, please refer to the description for `wifi.NULLMODE` in the function [`wifi.setmode()`](#wifisetmode) for more details.

#### Syntax
`wifi.sta.disconnect()`
`wifi.sta.disconnect([disconnected_cb])`

#### Parameters
none
- `disconnected_cb`: Callback to execute when station is disconnected from an access point.
- Items returned in table :
- `SSID`: SSID of access point. (format: string)
- `BSSID`: BSSID of access point. (format: string)
- `REASON`: See [wifi.eventmon.reason](#wifieventmonreason) below. (format: number)

#### Returns
`nil`
Expand Down Expand Up @@ -1113,7 +1143,19 @@ Sets SSID and password in AP mode. Be sure to make the password at least 8 chara
- `save` save configuration to flash.
- `true` configuration **will** be retained through power cycle. (Default)
- `false` configuration **will not** be retained through power cycle.

- Event callbacks will only be available if `WIFI_SDK_EVENT_MONITOR_ENABLE` is enabled in `user_config.h`
- `staconnected_cb`: Callback executed when a new client has connected to the access point.
- Items returned in table :
- `MAC`: MAC address of client that has connected.
- `AID`: SDK provides no details concerning this return value.
- `stadisconnected_cb`: Callback executed when a client has disconnected from the access point.
- Items returned in table :
- `MAC`: MAC address of client that has disconnected.
- `AID`: SDK provides no details concerning this return value.
- `probereq_cb`: Callback executed when a probe request was received.
- Items returned in table :
- `MAC`: MAC address of the client that is probing the access point.
- `RSSI`: Received Signal Strength Indicator of client.

#### Returns
- `true` Success
Expand Down

0 comments on commit 765cef4

Please sign in to comment.