Skip to content

Commit

Permalink
Made some modifications to the error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
dnc40085 committed Feb 18, 2017
1 parent da001dd commit 3b9e679
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 66 deletions.
25 changes: 13 additions & 12 deletions app/include/swTimer/swTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
//#define SWTMR_DEBUG
#define USE_SWTMR_ERROR_STRINGS

#if defined(SWTMR_DEBUG) || defined(NODE_DEBUG)
#ifndef SWTMR_DEBUG
#define SWTMR_DEBUG
#endif
#if defined(DEVELOP_VERSION)
#define SWTMR_DEBUG
#endif

#define SWTMR_DBG(fmt, ...) c_printf("\n SWTMR_DBG(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#if defined(SWTMR_DEBUG)
#define SWTMR_DBG(fmt, ...) dbg_printf("\tSWTIMER(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define SWTMR_DBG(...)
#endif

#if (defined(SWTMR_ERROR) || defined(NODE_ERROR))
#define SWTMR_ERR(fmt, ...) c_printf("\n SWTMR:"fmt"\n", ##__VA_ARGS__)
#if defined(NODE_ERROR)
#define SWTMR_ERR(fmt, ...) NODE_ERR("%s"fmt"\n", "SWTIMER:", ##__VA_ARGS__)
#else
#define SWTMR_ERR(...)
#endif

enum SWTMR_STATUS{
SWTMR_OK = 0,
SWTMR_FAIL = 0,
SWTMR_OK = 1,

SWTMR_MALLOC_FAIL = 10,
SWTMR_TIMER_NOT_ARMED,
SWTMR_NULL_PTR,
// SWTMR_NULL_PTR,

SWTMR_REGISTRY_NO_REGISTERED_TIMERS,

SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED,
SWTMR_SUSPEND_ARRAY_ADD_FAILED,
SWTMR_SUSPEND_ARRAY_REMOVE_FAILED,
// SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED,
// SWTMR_SUSPEND_ARRAY_ADD_FAILED,
// SWTMR_SUSPEND_ARRAY_REMOVE_FAILED,
SWTMR_SUSPEND_TIMER_ALREADY_SUSPENDED,
SWTMR_SUSPEND_TIMER_ALREADY_REARMED,
SWTMR_SUSPEND_NO_SUSPENDED_TIMERS,
Expand Down
11 changes: 3 additions & 8 deletions app/modules/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static int wifi_suspend(lua_State* L)
}

pmSleep_INIT_CFG(cfg);
cfg.sleep_mode=MODEM_SLEEP_T;
cfg.sleep_mode = MODEM_SLEEP_T;
if(lua_istable(L, 1))
{
pmSleep_parse_table_lua(L, 1, &cfg, &wifi_suspend_cb_ref, &wifi_resume_cb_ref);
Expand All @@ -361,9 +361,9 @@ static int wifi_suspend(lua_State* L)
static int wifi_resume(lua_State* L)
{
PMSLEEP_DBG("\n\tDBG: %s start\n", __func__);
uint8 fpm_state=pmSleep_get_state();
uint8 fpm_state = pmSleep_get_state();
// If forced sleep api is not enabled, return error
if (fpm_state==0)
if (fpm_state == 0)
{
return luaL_error(L, "WIFi not suspended");
}
Expand All @@ -374,11 +374,6 @@ static int wifi_resume(lua_State* L)
// 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);
Expand Down
52 changes: 27 additions & 25 deletions app/pm/pmSleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,46 @@ static pmSleep_param_t current_config;


/* INTERNAL FUNCTION DECLARATIONS */
static void disarm_all_timers(void);
static void suspend_all_timers(void);
static void null_mode_check_timer_cb(void* arg);
static void rearm_all_sw_timers(void);
static void resume_all_timers(void);
static inline void register_lua_cb(lua_State* L,int* cb_ref);
static void resume_cb(void);
static void wifi_suspended_timer_cb(int arg);

/* INTERNAL FUNCTIONS */

#include "swTimer/swTimer.h"
static void disarm_all_timers(void){
static void suspend_all_timers(void){
#ifdef ENABLE_TIMER_SUSPEND
swtmr_suspend(NULL);
#endif
return;
}

static void rearm_all_sw_timers(void){
static void resume_all_timers(void){
#ifdef ENABLE_TIMER_SUSPEND
swtmr_resume(NULL);
#endif
return;
}

static void null_mode_check_timer_cb(void* arg){

// if current opmode is NULL and uart 0 tx buffer is empty and uart 1 tx buffer is empty
if (wifi_get_opmode() == NULL_MODE && (READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0 &&
(READ_PERI_REG(UART_STATUS(1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0){
os_timer_disarm(&null_mode_check_timer);
disarm_all_timers();
if (wifi_get_opmode() == NULL_MODE){
//check if uart 0 tx buffer is empty and uart 1 tx buffer is empty
if(current_config.sleep_mode == LIGHT_SLEEP_T){
wifi_fpm_do_sleep(current_config.sleep_duration);
return;
if((READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0 &&
(READ_PERI_REG(UART_STATUS(1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0){
ets_timer_disarm(&null_mode_check_timer);
suspend_all_timers();
wifi_fpm_do_sleep(current_config.sleep_duration);
return;
}
else{
return;
}
}
else{
else{ //MODEM_SLEEP_T
sint8 retval_wifi_fpm_do_sleep = wifi_fpm_do_sleep(current_config.sleep_duration); // Request WiFi suspension and store return value

// If wifi_fpm_do_sleep success
Expand All @@ -65,16 +69,12 @@ static void null_mode_check_timer_cb(void* arg){
os_timer_arm(&wifi_suspended_test_timer, 1, 1);
}
else{ // This should never happen. if it does, return the value for error reporting

wifi_fpm_close();
luaL_error(lua_getstate(), "wifi_fpm_do_sleep returned %d", retval_wifi_fpm_do_sleep);
return;
PMSLEEP_ERR("wifi_fpm_do_sleep returned %d", retval_wifi_fpm_do_sleep);
}
}

}
else{
// PMSLEEP_DBG("wifi_get_opmode()!=NULL_MODE");
ets_timer_disarm(&null_mode_check_timer);
return;
}
}

Expand All @@ -93,7 +93,7 @@ static void resume_cb(void){
wifi_fpm_close(); // Disable force sleep API
PMSLEEP_DBG("WiFi resume");

rearm_all_sw_timers();
resume_all_timers();

//this section restores null mode auto sleep setting
if(autosleep_setting_temp == 1) {
Expand Down Expand Up @@ -360,12 +360,14 @@ void pmSleep_suspend(pmSleep_param_t *cfg){

c_memcpy(&current_config, cfg, sizeof(pmSleep_param_t));
PMSLEEP_DBG("sleep duration is %d", current_config.sleep_duration);
os_timer_disarm(&null_mode_check_timer);
os_timer_setfn(&null_mode_check_timer, null_mode_check_timer_cb, false);
os_timer_arm(&null_mode_check_timer, 1, 1);

//this timer intentionally bypasses the swtimer timer registration process
ets_timer_disarm(&null_mode_check_timer);
ets_timer_setfn(&null_mode_check_timer, null_mode_check_timer_cb, false);
ets_timer_arm_new(&null_mode_check_timer, 1, 1, 1);
}
else{
luaL_error(L, "ERR, mode change fail");
PMSLEEP_ERR("opmode change fail");
}
PMSLEEP_DBG("END");
return;
Expand Down
25 changes: 19 additions & 6 deletions app/pm/pmSleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@
#include "task/task.h"
#include "c_string.h"

//#define PMSLEEP_DEBUG
#if defined(DEVELOP_VERSION)
#define PMSLEEP_DEBUG
#endif

#if defined(PMSLEEP_DEBUG)
#define PMSLEEP_DBG(fmt, ...) dbg_printf("\tPMSLEEP(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define PMSLEEP_DBG(...) //c_printf(__VA_ARGS__)
#endif

#if defined(NODE_ERROR)
#define PMSLEEP_ERR(fmt, ...) NODE_ERR("%s"fmt"\n", "PMSLEEP:", ##__VA_ARGS__)
#else
#define PMSLEEP_ERR(...)
#endif





#define PMSLEEP_SLEEP_MIN_TIME 50000
#define PMSLEEP_SLEEP_MAX_TIME 268435454 //FPM_MAX_SLEEP_TIME-1
Expand All @@ -23,11 +41,6 @@
{ LSTRKEY( "INT_LOW" ), LNUMVAL( GPIO_PIN_INTR_LOLEVEL ) }


#if defined(PMSLEEP_DEBUG) || defined(NODE_DEBUG)
#define PMSLEEP_DBG(fmt, ...) c_printf("\tPMSLEEP_DBG(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define PMSLEEP_DBG(...) //c_printf(__VA_ARGS__)
#endif

typedef struct pmSleep_param{
uint32 sleep_duration;
Expand Down
38 changes: 23 additions & 15 deletions app/swTimer/swTimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
static const char* SWTMR_ERROR_STRINGS[]={
[SWTMR_MALLOC_FAIL] = "Out of memory!",
[SWTMR_TIMER_NOT_ARMED] = "Timer is not armed",
[SWTMR_NULL_PTR] = "A NULL pointer was passed to timer suspend api",
// [SWTMR_NULL_PTR] = "A NULL pointer was passed to timer suspend api",
[SWTMR_REGISTRY_NO_REGISTERED_TIMERS] = "No timers in registry",
[SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED] = "Suspend array init fail",
[SWTMR_SUSPEND_ARRAY_ADD_FAILED] = "Unable to add suspended timer to array",
[SWTMR_SUSPEND_ARRAY_REMOVE_FAILED] = "Unable to remove suspended timer from array",
// [SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED] = "Suspend array init fail",
// [SWTMR_SUSPEND_ARRAY_ADD_FAILED] = "Unable to add suspended timer to array",
// [SWTMR_SUSPEND_ARRAY_REMOVE_FAILED] = "Unable to remove suspended timer from array",
[SWTMR_SUSPEND_TIMER_ALREADY_SUSPENDED] = "Already suspended",
[SWTMR_SUSPEND_TIMER_ALREADY_REARMED] = "Already been re-armed",
[SWTMR_SUSPEND_NO_SUSPENDED_TIMERS] = "No suspended timers",
Expand Down Expand Up @@ -125,8 +125,8 @@ static inline bool timer_armed_check(os_timer_t* timer_ptr){

static inline int timer_do_suspend(os_timer_t* timer_ptr){
if(timer_ptr == NULL){
SWTMR_DBG("timer_ptr is NULL");
return SWTMR_NULL_PTR;
SWTMR_ERR("timer_ptr is invalid");
return SWTMR_FAIL;
}

volatile uint32 frc2_count = RTC_REG_READ(FRC2_COUNT_ADDRESS);
Expand Down Expand Up @@ -155,14 +155,16 @@ static inline int timer_do_suspend(os_timer_t* timer_ptr){

if(suspended_timers.data_ptr == NULL){
if(!dynarr_init(&suspended_timers, 10, sizeof(os_timer_t*))){
return SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED;
SWTMR_ERR("Suspend array init fail");
return SWTMR_FAIL;
}
}

if(suspended_timer_ptr == NULL){
// return SWTMR_SUSPEND_TIMER_ALREADY_SUSPENDED;
if(!dynarr_add(&suspended_timers, &timer_ptr, sizeof(timer_ptr))){
return SWTMR_SUSPEND_ARRAY_ADD_FAILED;
SWTMR_ERR("Unable to add suspended timer to array");
return SWTMR_FAIL;
}
}

Expand All @@ -172,8 +174,8 @@ static inline int timer_do_suspend(os_timer_t* timer_ptr){
//NOTE: Interrupts are temporarily blocked during the execution of this function
static inline int timer_do_resume_single(os_timer_t** suspended_timer_ptr){
if(suspended_timer_ptr == NULL){
SWTMR_DBG("suspended_timer_ptr is NULL");
return SWTMR_NULL_PTR;
SWTMR_ERR("suspended_timer_ptr is invalid");
return SWTMR_FAIL;
}

os_timer_t* timer_list_ptr = NULL;
Expand All @@ -183,7 +185,10 @@ static inline int timer_do_resume_single(os_timer_t** suspended_timer_ptr){
//verify timer has not been rearmed
if(timer_armed_check(resume_timer_ptr) == TRUE){
SWTMR_DBG("Timer(%p) already rearmed, removing from array", resume_timer_ptr);
dynarr_remove(&suspended_timers, suspended_timer_ptr);
if(!dynarr_remove(&suspended_timers, suspended_timer_ptr)){
SWTMR_ERR("Failed to remove timer from suspend array");
return SWTMR_FAIL;
}
return SWTMR_OK;
}

Expand All @@ -202,7 +207,7 @@ static inline int timer_do_resume_single(os_timer_t** suspended_timer_ptr){

while(timer_list_ptr != NULL){
if(resume_timer_ptr->timer_expire > timer_list_ptr->timer_expire){
if(timer_list_ptr->timer_next!=NULL){
if(timer_list_ptr->timer_next != NULL){
if(resume_timer_ptr->timer_expire < timer_list_ptr->timer_next->timer_expire){
resume_timer_ptr->timer_next = timer_list_ptr->timer_next;
timer_list_ptr->timer_next = resume_timer_ptr;
Expand Down Expand Up @@ -352,7 +357,7 @@ static void timer_unregister_task(task_param_t param, uint8 priority){
else{

if(unregister_queue == NULL) {
SWTMR_ERR("ERROR: REGISTER QUEUE EMPTY");
SWTMR_ERR("ERROR register queue empty");
return;
}
registry_queue_t* queue_temp = unregister_queue;
Expand All @@ -365,15 +370,18 @@ static void timer_unregister_task(task_param_t param, uint8 priority){
}
}
if(timer_armed_check(timer_ptr) == TRUE){
SWTMR_DBG("%p still armed, can't remove from registry", timer_ptr);
return;
}


os_timer_t** registry_ptr = timer_registry_check(timer_ptr);
if(registry_ptr != NULL){
if(!dynarr_remove(&timer_registry, registry_ptr)){
/**/SWTMR_ERR("Failed to remove timer(%p) from registry", timer_ptr);
//timer remove FAIL

/**/SWTMR_ERR("Failed to remove timer from registry");
/**/SWTMR_DBG("registry_ptr = %p", registry_ptr);
return;
}
}
else{
Expand Down

0 comments on commit 3b9e679

Please sign in to comment.