Skip to content

Commit

Permalink
Merge pull request #725 from dnc40085/dev_wifi_toggle
Browse files Browse the repository at this point in the history
Add function wifi.sleep
  • Loading branch information
vowstar committed Nov 5, 2015
2 parents 9e48269 + 91ba22a commit 3da57dc
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
static int wifi_smart_succeed = LUA_NOREF;
static uint8 getap_output_format=0;

//wifi.sleep variables
#define FPM_SLEEP_MAX_TIME 0xFFFFFFF
static bool FLAG_wifi_force_sleep_enabled=0;


//variables for wifi event monitor
static sint32_t wifi_status_cb_ref[6] = {LUA_NOREF,LUA_NOREF,LUA_NOREF,LUA_NOREF,LUA_NOREF,LUA_NOREF};
static volatile os_timer_t wifi_sta_status_timer;
Expand Down Expand Up @@ -319,6 +324,47 @@ static int wifi_getphymode( lua_State* L )
return 1;
}

//wifi.sleep()
static int wifi_sleep(lua_State* L)
{
uint8 desired_sleep_state=2;
if(lua_isnumber(L, 1))
{
if(luaL_checknumber(L, 1)==0)
{
desired_sleep_state=FALSE;
}
else if(luaL_checknumber(L, 1)==1)
{
desired_sleep_state=TRUE;
}
}
if (!FLAG_wifi_force_sleep_enabled && desired_sleep_state==1)
{
FLAG_wifi_force_sleep_enabled=TRUE;
uint8 wifi_current_opmode=wifi_get_opmode();
if (wifi_current_opmode==1||wifi_current_opmode==3)
{
wifi_station_disconnect();
}
// set WiFi mode to null mode
wifi_set_opmode(NULL_MODE);
// set force sleep type
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
}
else if(FLAG_wifi_force_sleep_enabled && desired_sleep_state==0)
{
FLAG_wifi_force_sleep_enabled=FALSE;
// wake up to use WiFi again
wifi_fpm_do_wakeup();
wifi_fpm_close();
}
lua_pushnumber(L, FLAG_wifi_force_sleep_enabled);
return 1;
}

// Lua: mac = wifi.xx.getmac()
static int wifi_getmac( lua_State* L, uint8_t mode )
{
Expand Down Expand Up @@ -1329,14 +1375,15 @@ const LUA_REG_TYPE wifi_map[] =
{ LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) },
{ LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) },
{ LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) },
{ LSTRKEY( "sleep" ), LFUNCVAL( wifi_sleep ) },
{ LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) },
{ LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) },
{ LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "sta" ), LROVAL( wifi_station_map ) },
{ LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) },

// { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) },
{ LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) },
{ LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) },
{ LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) },
{ LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) },
Expand Down

0 comments on commit 3da57dc

Please sign in to comment.