Skip to content

Commit

Permalink
Exposed Forced sleep API
Browse files Browse the repository at this point in the history
Added functions `wifi.suspend()`, `wifi.resume()` and `node.sleep()`
  • Loading branch information
dnc40085 committed Jul 27, 2016
1 parent 3eccf50 commit 57fd447
Show file tree
Hide file tree
Showing 12 changed files with 759 additions and 68 deletions.
6 changes: 4 additions & 2 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ SUBDIRS= \
crypto \
dhtlib \
tsl2561 \
net \
http
net \
http \
pm

endif # } PDIR

Expand Down Expand Up @@ -87,6 +88,7 @@ COMPONENTS_eagle.app.v6 = \
dhtlib/libdhtlib.a \
tsl2561/tsl2561lib.a \
http/libhttp.a \
pm/libpm.a \
net/libnodemcu_net.a \
modules/libmodules.a \

Expand Down
3 changes: 3 additions & 0 deletions app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ extern void luaL_assertfail(const char *file, int line, const char *message);
#define WIFI_SDK_EVENT_MONITOR_ENABLE
#define WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE

#define PMSLEEP_ENABLE


#define STRBUF_DEFAULT_INCREMENT 32

#endif /* __USER_CONFIG_H__ */
1 change: 1 addition & 0 deletions app/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ INCLUDES += -I ../smart
INCLUDES += -I ../cjson
INCLUDES += -I ../dhtlib
INCLUDES += -I ../http
INCLUDES += -I ../pm
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile

46 changes: 33 additions & 13 deletions app/modules/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,35 @@ static int node_deepsleep( lua_State* L )
return 0;
}

// Lua: dsleep_set_options
// Combined to dsleep( us, option )
// static int node_deepsleep_setoption( lua_State* L )
// {
// s32 option;
// option = luaL_checkinteger( L, 1 );
// if ( option < 0 || option > 4)
// return luaL_error( L, "wrong arg range" );
// else
// deep_sleep_set_option( option );
// return 0;
// }
// Lua: info()

#ifdef PMSLEEP_ENABLE
#include "pmSleep.h"

int node_sleep_resume_cb_ref= LUA_NOREF;
void node_sleep_resume_cb(void)
{
PMSLEEP_DBG("\n\tDBG: %s START\n", __func__);
pmSleep_execute_lua_cb(&node_sleep_resume_cb_ref);
}

// Lua: node.sleep(table)
static int node_sleep( lua_State* L )
{
pmSleep_INIT_CFG(cfg);
cfg.sleep_mode=LIGHT_SLEEP_T;
if(lua_istable(L, 1))
{
pmSleep_parse_table_lua(L, 1, &cfg, NULL, &node_sleep_resume_cb_ref);
}
else
{
return luaL_argerror(L, 1, "must be table");
}
cfg.resume_cb_ptr=&node_sleep_resume_cb;
pmSleep_suspend(&cfg);
return 0;
}
#endif //PMSLEEP_ENABLE

static int node_info( lua_State* L )
{
Expand Down Expand Up @@ -638,6 +654,10 @@ static const LUA_REG_TYPE node_map[] =
{
{ LSTRKEY( "restart" ), LFUNCVAL( node_restart ) },
{ LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) },
#ifdef PMSLEEP_ENABLE
{ LSTRKEY( "sleep" ), LFUNCVAL( node_sleep ) },
PMSLEEP_INT_MAP,
#endif
{ LSTRKEY( "info" ), LFUNCVAL( node_info ) },
{ LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) },
{ LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) },
Expand Down
131 changes: 80 additions & 51 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ static uint8 getap_output_format=0;

#define INVALID_MAC_STR "MAC:FF:FF:FF:FF:FF:FF"

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

#ifdef WIFI_SMART_ENABLE
static void wifi_smart_succeed_cb(sc_status status, void *pdata){
NODE_DBG("wifi_smart_succeed_cb is called.\n");
Expand Down Expand Up @@ -311,67 +307,97 @@ static int wifi_getphymode( lua_State* L )
return 1;
}

//wifi.sleep()
static int wifi_sleep(lua_State* L)
#ifdef PMSLEEP_ENABLE
/* Begin WiFi suspend functions*/
#include "pmSleep.h"

static int wifi_resume_cb_ref = LUA_NOREF; // Holds resume callback reference
static int wifi_suspend_cb_ref = LUA_NOREF; // Holds suspend callback reference

void wifi_pmSleep_suspend_CB(void)
{
uint8 desired_sleep_state = 2;
sint8 wifi_fpm_do_sleep_return_value = 1;
if(lua_isnumber(L, 1))
PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
if (wifi_suspend_cb_ref != LUA_NOREF)
{
if(luaL_checknumber(L, 1) == 0)
{
desired_sleep_state = 0;
}
else if(luaL_checknumber(L, 1) == 1)
{
desired_sleep_state = 1;
}
lua_State* L = lua_getstate(); // Get main Lua thread pointer
lua_rawgeti(L, LUA_REGISTRYINDEX, wifi_suspend_cb_ref); // Push suspend callback onto stack
lua_unref(L, wifi_suspend_cb_ref); // remove suspend callback from LUA_REGISTRY
wifi_suspend_cb_ref = LUA_NOREF; // Update variable since reference is no longer valid
lua_call(L, 0, 0); // Execute suspend callback
}
if (!FLAG_wifi_force_sleep_enabled && desired_sleep_state == 1 )
else
{
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_return_value = wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
if (wifi_fpm_do_sleep_return_value == 0)
{
FLAG_wifi_force_sleep_enabled = TRUE;
}
else
{
wifi_fpm_close();
FLAG_wifi_force_sleep_enabled = FALSE;
}

PMSLEEP_DBG("\n\tDBG: lua cb unavailable\n");
}
else if(FLAG_wifi_force_sleep_enabled && desired_sleep_state == 0)
PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
return;
}

void wifi_pmSleep_resume_CB(void)
{
PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
// If resume callback was defined
pmSleep_execute_lua_cb(&wifi_resume_cb_ref);
PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
return;
}

// Lua: wifi.suspend({duration, suspend_cb, resume_cb, preserve_mode})
static int wifi_suspend(lua_State* L)
{
// If no parameters were provided
if (lua_isnone(L, 1))
{
FLAG_wifi_force_sleep_enabled = FALSE;
// wake up to use WiFi again
wifi_fpm_do_wakeup();
wifi_fpm_close();
// Return current WiFi suspension state
lua_pushnumber(L, pmSleep_get_state());
return 1; // Return WiFi suspension state
}

if (desired_sleep_state == 1 && FLAG_wifi_force_sleep_enabled == FALSE)
pmSleep_INIT_CFG(cfg);
cfg.sleep_mode=MODEM_SLEEP_T;
if(lua_istable(L, 1))
{
lua_pushnil(L);
lua_pushnumber(L, wifi_fpm_do_sleep_return_value);
pmSleep_parse_table_lua(L, 1, &cfg, &wifi_suspend_cb_ref, &wifi_resume_cb_ref);
}
else
return luaL_argerror(L, 1, "must be table");
cfg.resume_cb_ptr = &wifi_pmSleep_resume_CB;
cfg.suspend_cb_ptr = &wifi_pmSleep_suspend_CB;
pmSleep_suspend(&cfg);
return 0;
}
// Lua: wifi.resume([Resume_CB])
static int wifi_resume(lua_State* L)
{
PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
uint8 fpm_state=pmSleep_get_state();
// If forced sleep api is not enabled, return error
if (fpm_state==0)
{
lua_pushnumber(L, FLAG_wifi_force_sleep_enabled);
lua_pushnil(L);
return luaL_error(L, "WIFi not suspended");
}
return 2;

// If a resume callback was provided
if (lua_isfunction(L, 1))
{
// If there is already a resume callback reference
lua_pushvalue(L, 1); //Push resume callback to the top of the stack
register_lua_cb(L, &wifi_resume_cb_ref);
// if (wifi_resume_cb_ref != LUA_NOREF)
// {
// luaL_unref(L, LUA_REGISTRYINDEX, wifi_resume_cb_ref); // Discard old callback
// }
// wifi_resume_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); // Register resume callback in LUA_REGISTRY and store it's reference
PMSLEEP_DBG("\n\tDBG: Resume CB registered\n");
}
pmSleep_resume(NULL);
PMSLEEP_DBG("\n\tDBG: %s end\n", __func__);
return 0;
}

/* End WiFi suspend functions*/
#endif

// Lua: mac = wifi.xx.getmac()
static int wifi_getmac( lua_State* L, uint8_t mode )
{
Expand Down Expand Up @@ -895,6 +921,7 @@ static int wifi_station_listap( lua_State* L )
{
unregister_lua_cb(L, &wifi_scan_succeed);
}
return 0;
}

// Lua: wifi.sta.gethostname()
Expand Down Expand Up @@ -1267,7 +1294,8 @@ static 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( "suspend" ), LFUNCVAL( wifi_suspend ) },
{ LSTRKEY( "resume" ), LFUNCVAL( wifi_resume ) },
#ifdef WIFI_SMART_ENABLE
{ LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) },
{ LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) },
Expand Down Expand Up @@ -1349,6 +1377,7 @@ void wifi_change_default_host_name(void)

int luaopen_wifi( lua_State *L )
{
// wifi_fpm_auto_sleep_set_in_null_mode(1);
#if defined(WIFI_SDK_EVENT_MONITOR_ENABLE)
wifi_eventmon_init();
#endif
Expand Down
14 changes: 13 additions & 1 deletion app/modules/wifi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "c_stdio.h"
#include "task/task.h"

//#define WIFI_SLEEP_DEBUG
//#define WIFI_EVENTMON_DEBUG

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);

Expand All @@ -37,12 +40,21 @@ static inline void unregister_lua_cb(lua_State* L, int* cb_ref)

void wifi_change_default_host_name(void);

#ifdef NODE_DEBUG
#if defined(WIFI_EVENTMON_DEBUG) || defined(NODE_DEBUG)
#define EVENT_DBG(...) c_printf(__VA_ARGS__)
#else
#define EVENT_DBG(...) //c_printf(__VA_ARGS__)
#endif

enum wifi_suspension_state
{
WIFI_AWAKE = 0,
WIFI_SUSPENSION_PENDING = 1,
WIFI_SUSPENDED = 2
};



#ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
extern const LUA_REG_TYPE wifi_event_monitor_map[];
void wifi_eventmon_init();
Expand Down
52 changes: 52 additions & 0 deletions app/pm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = libpm.a
endif

STD_CFLAGS=-std=gnu11 -Wimplicit

#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=

#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#

INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ./include
INCLUDES += -I ../include
INCLUDES += -I ../../include
INCLUDES += -I ../lua
INCLUDES += -I ../platform
INCLUDES += -I ../libc

PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile

Loading

0 comments on commit 57fd447

Please sign in to comment.