Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event callbacks to wifi.sta.config() and wifi.ap.config() and more #1903

Merged
merged 3 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,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 +1024,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 +1617,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
1 change: 1 addition & 0 deletions app/modules/wifi_common.h
Original file line number Diff line number Diff line change
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
59 changes: 53 additions & 6 deletions docs/en/modules/wifi.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ none

Sets the WiFi station configuration.

!!! note
It is not advised to assume that the WiFi is connected at any time during initialization start-up. WiFi connection status should be validated either by using a WiFi event callback or by polling the status on a timer.

#### Syntax
`wifi.sta.config(station_config)`

Expand All @@ -400,7 +403,30 @@ 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 uncommented in `user_config.h`
- Please note: To ensure all station events are handled at boot time, all relevant callbacks must be registered as early as possible in `init.lua` with either `wifi.sta.config()` or `wifi.eventmon.register()`.
- `connected_cb`: Callback to execute when station is connected to an access point. (Optional)
- 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. (Optional)
- 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. (Optional)
- 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. (Optional)
- 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. (Optional)
- Blank table is returned.

#### Returns
- `true` Success
Expand Down Expand Up @@ -449,10 +475,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. (Optional)
- 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 +499,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. (Optional)
- 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 +1147,20 @@ 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 uncommented in `user_config.h`
- Please note: To ensure all SoftAP events are handled at boot time, all relevant callbacks must be registered as early as possible in `init.lua` with either `wifi.ap.config()` or `wifi.eventmon.register()`.
- `staconnected_cb`: Callback executed when a new client has connected to the access point. (Optional)
- 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. (Optional)
- 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. (Optional)
- 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