From cc20482aa997af7c20fe4ec9cad85762aa52b458 Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Wed, 3 Jun 2015 17:28:03 +0200 Subject: [PATCH] cc3200: Add method to configure the servers timeout. With network.server_timeout(secs) the timeout can be changed. The default value is 300 secs. Minimmum accpeted is 5 secs. Without params the function returns the current configured timeout. --- cc3200/ftp/ftp.c | 5 ++-- cc3200/mods/modnetwork.c | 53 ++++++++++++++++++++++++++-------------- cc3200/mods/modwlan.c | 26 ++++++++++++++------ cc3200/mods/modwlan.h | 1 + cc3200/qstrdefsport.h | 3 +-- cc3200/serverstask.c | 46 ++++++++++++++++++++++------------ cc3200/serverstask.h | 2 ++ cc3200/telnet/telnet.c | 5 ++-- 8 files changed, 91 insertions(+), 50 deletions(-) diff --git a/cc3200/ftp/ftp.c b/cc3200/ftp/ftp.c index 0d30108e81..df28e78d02 100644 --- a/cc3200/ftp/ftp.c +++ b/cc3200/ftp/ftp.c @@ -68,7 +68,6 @@ #define FTP_UNIX_TIME_20150101 1420070400 #define FTP_UNIX_SECONDS_180_DAYS 15552000 #define FTP_DATA_TIMEOUT_MS 5000 // 5 seconds -#define FTP_CMD_TIMEOUT_MS 120000 // 2 minutes #define FTP_SOCKETFIFO_ELEMENTS_MAX 4 #define FTP_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2) @@ -253,7 +252,7 @@ void ftp_run (void) { ftp_wait_for_enabled(); break; case E_FTP_STE_START: - if (ftp_create_listening_socket(&ftp_data.lc_sd, FTP_CMD_PORT, FTP_CMD_CLIENTS_MAX )) { + if (wlan_is_connected() && ftp_create_listening_socket(&ftp_data.lc_sd, FTP_CMD_PORT, FTP_CMD_CLIENTS_MAX)) { ftp_data.state = E_FTP_STE_READY; } break; @@ -838,7 +837,7 @@ static void ftp_process_cmd (void) { } } else if (result == E_FTP_RESULT_CONTINUE) { - if (ftp_data.ctimeout++ > (FTP_CMD_TIMEOUT_MS / FTP_CYCLE_TIME_MS)) { + if (ftp_data.ctimeout++ > (servers_get_timeout() / FTP_CYCLE_TIME_MS)) { ftp_send_reply(221, NULL); } } diff --git a/cc3200/mods/modnetwork.c b/cc3200/mods/modnetwork.c index dd91427f41..8f9ee2c6c0 100644 --- a/cc3200/mods/modnetwork.c +++ b/cc3200/mods/modnetwork.c @@ -37,36 +37,54 @@ /// \module network - network configuration /// -/// This module provides network drivers and routing configuration. +/// This module provides network drivers and server configuration. void mod_network_init0(void) { } #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP) -STATIC mp_obj_t network_server_start(void) { - servers_start(); - return mp_const_none; +STATIC mp_obj_t network_server_running(mp_uint_t n_args, const mp_obj_t *args) { + if (n_args > 0) { + // set + if (mp_obj_is_true(args[0])) { + servers_start(); + } else { + servers_stop(); + } + return mp_const_none; + } else { + // get + return MP_BOOL(servers_are_enabled()); + } } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_start_obj, network_server_start); - -STATIC mp_obj_t network_server_stop(void) { - servers_stop(); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_stop_obj, network_server_stop); - -STATIC mp_obj_t network_server_running(void) { - return MP_BOOL(servers_are_enabled()); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_server_running_obj, network_server_running); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_running_obj, 0, 1, network_server_running); STATIC mp_obj_t network_server_login(mp_obj_t user, mp_obj_t pass) { const char *_user = mp_obj_str_get_str(user); const char *_pass = mp_obj_str_get_str(pass); + if (strlen(user) > SERVERS_USER_PASS_LEN_MAX || strlen(pass) > SERVERS_USER_PASS_LEN_MAX) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } servers_set_login ((char *)_user, (char *)_pass); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(network_server_login_obj, network_server_login); + +// timeout value given in seconds +STATIC mp_obj_t network_server_timeout(mp_uint_t n_args, const mp_obj_t *args) { + if (n_args > 0) { + uint32_t _timeout = mp_obj_get_int(args[0]); + if (!servers_set_timeout(_timeout * 1000)) { + // timeout is too low + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } + return mp_const_none; + } else { + // get + return mp_obj_new_int(servers_get_timeout() / 1000); + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_server_timeout_obj, 0, 1, network_server_timeout); #endif STATIC const mp_map_elem_t mp_module_network_globals_table[] = { @@ -74,10 +92,9 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&mod_network_nic_type_wlan }, #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP) - { MP_OBJ_NEW_QSTR(MP_QSTR_start_server), (mp_obj_t)&network_server_start_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_stop_server), (mp_obj_t)&network_server_stop_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_server_running), (mp_obj_t)&network_server_running_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_server_login), (mp_obj_t)&network_server_login_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_server_timeout), (mp_obj_t)&network_server_timeout_obj }, #endif }; diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index 19f2a5433d..19d6dcae9d 100644 --- a/cc3200/mods/modwlan.c +++ b/cc3200/mods/modwlan.c @@ -193,7 +193,6 @@ STATIC void wlan_reenable (SlWlanMode_t mode); STATIC void wlan_servers_start (void); STATIC void wlan_servers_stop (void); STATIC void wlan_get_sl_mac (void); -STATIC bool wlan_is_connected (void); STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec, const char* key, uint32_t key_len, uint32_t timeout); STATIC void wlan_lpds_callback_enable (mp_obj_t self_in); @@ -224,6 +223,11 @@ void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) { memcpy(wlan_obj.ssid_o, pEventData->ssid_name, pEventData->ssid_len); wlan_obj.ssid_o[pEventData->ssid_len] = '\0'; SET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION); + #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP) + // we must reset the servers in case that the last connection + // was lost without any notification being received + servers_reset(); + #endif } break; case SL_WLAN_DISCONNECT_EVENT: @@ -241,6 +245,11 @@ void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) { memcpy(wlan_obj.ssid_o, pEventData->go_peer_device_name, pEventData->go_peer_device_name_len); wlan_obj.ssid_o[pEventData->go_peer_device_name_len] = '\0'; wlan_obj.staconnected = true; + #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP) + // we must reset the servers in case that the last connection + // was lost without any notification being received + servers_reset(); + #endif } break; case SL_WLAN_STA_DISCONNECTED_EVENT: @@ -547,12 +556,17 @@ void wlan_get_ip (uint32_t *ip) { } } +bool wlan_is_connected (void) { + return ((GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION) && + GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_IP_ACQUIRED)) || wlan_obj.staconnected); +} + //***************************************************************************** // DEFINE STATIC FUNCTIONS //***************************************************************************** STATIC void wlan_initialize_data (void) { - wlan_obj.status = 0; + CLR_STATUS_BIT_ALL(wlan_obj.status); wlan_obj.dns = 0; wlan_obj.gateway = 0; wlan_obj.ip = 0; @@ -567,7 +581,8 @@ STATIC void wlan_reenable (SlWlanMode_t mode) { // stop and start again sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER); sl_Stop(SL_STOP_TIMEOUT); - wlan_obj.status = 0; + CLR_STATUS_BIT_ALL(wlan_obj.status); + wlan_obj.staconnected = false; wlan_obj.mode = sl_Start(0, 0, 0); sl_LockObjUnlock (&wlan_LockObj); ASSERT (wlan_obj.mode == mode); @@ -620,11 +635,6 @@ STATIC void wlan_get_sl_mac (void) { sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddrLen, wlan_obj.mac); } -STATIC bool wlan_is_connected (void) { - return ((GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION) && - GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_IP_ACQUIRED)) || wlan_obj.staconnected); -} - /// \method init(mode, ssid=None, *, security=wlan.OPEN, key=None, channel=5) /// /// Initialise the WLAN engine with the given parameters: diff --git a/cc3200/mods/modwlan.h b/cc3200/mods/modwlan.h index 3e90258b7f..82fe4f8c6a 100644 --- a/cc3200/mods/modwlan.h +++ b/cc3200/mods/modwlan.h @@ -62,6 +62,7 @@ extern void wlan_stop (uint32_t timeout); extern void wlan_start (void); extern void wlan_get_mac (uint8_t *macAddress); extern void wlan_get_ip (uint32_t *ip); +extern bool wlan_is_connected (void); extern int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family); extern int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno); diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 30337e741f..8219e03bb2 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -237,10 +237,9 @@ Q(IPPROTO_RAW) // for network class Q(network) -Q(start_server) -Q(stop_server) Q(server_running) Q(server_login) +Q(server_timeout) // for WLAN class Q(WLAN) diff --git a/cc3200/serverstask.c b/cc3200/serverstask.c index 94ef8c8ec8..d20f9bcfa5 100644 --- a/cc3200/serverstask.c +++ b/cc3200/serverstask.c @@ -43,23 +43,27 @@ DECLARE PRIVATE DEFINITIONS ******************************************************************************/ -#define SERVERS_DEF_USER "micro" -#define SERVERS_DEF_PASS "python" +#define SERVERS_DEF_USER "micro" +#define SERVERS_DEF_PASS "python" +#define SERVERS_DEF_TIMEOUT_MS 300000 // 5 minutes +#define SERVERS_MIN_TIMEOUT_MS 5000 // 5 seconds /****************************************************************************** DEFINE PRIVATE TYPES ******************************************************************************/ typedef struct { - volatile bool enabled; - volatile bool do_disable; - volatile bool do_enable; - volatile bool do_reset; -}servers_Data_t; + uint32_t timeout; + bool enabled; + bool do_disable; + bool do_enable; + bool do_reset; +} servers_data_t; /****************************************************************************** DECLARE PRIVATE DATA ******************************************************************************/ -static servers_Data_t servers_data = {.enabled = false, .do_disable = false, .do_enable = false, .do_reset = false}; +static servers_data_t servers_data = {.timeout = SERVERS_DEF_TIMEOUT_MS, .enabled = false, .do_disable = false, + .do_enable = false, .do_reset = false}; static volatile bool sleep_sockets = false; /****************************************************************************** @@ -103,8 +107,7 @@ void TASK_Servers (void *pvParameters) { servers_data.do_disable = false; servers_data.enabled = false; } - - if (servers_data.do_reset) { + else if (servers_data.do_reset && servers_data.enabled) { telnet_reset(); ftp_reset(); servers_data.do_reset = false; @@ -112,13 +115,12 @@ void TASK_Servers (void *pvParameters) { // and we should also close all user sockets modusocket_close_all_user_sockets(); } + + if (cycle) { + telnet_run(); + } else { - if (cycle) { - telnet_run(); - } - else { - ftp_run(); - } + ftp_run(); } if (sleep_sockets) { @@ -176,6 +178,18 @@ void servers_set_login (char *user, char *pass) { memcpy(servers_pass, pass, SERVERS_USER_PASS_LEN_MAX); } +bool servers_set_timeout (uint32_t timeout) { + if (timeout < SERVERS_MIN_TIMEOUT_MS) { + return false; + } + servers_data.timeout = timeout; + return true; +} + +uint32_t servers_get_timeout (void) { + return servers_data.timeout; +} + /****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ diff --git a/cc3200/serverstask.h b/cc3200/serverstask.h index cd9930a4e5..06de1275b8 100644 --- a/cc3200/serverstask.h +++ b/cc3200/serverstask.h @@ -61,5 +61,7 @@ extern bool servers_are_enabled (void); extern void servers_close_socket (int16_t *sd); extern void servers_set_login (char *user, char *pass); extern void server_sleep_sockets (void); +extern bool servers_set_timeout (uint32_t timeout); +extern uint32_t servers_get_timeout (void); #endif /* SERVERSTASK_H_ */ diff --git a/cc3200/telnet/telnet.c b/cc3200/telnet/telnet.c index 06e45ef5fe..66968fbd62 100644 --- a/cc3200/telnet/telnet.c +++ b/cc3200/telnet/telnet.c @@ -49,7 +49,6 @@ #define TELNET_TX_RETRIES_MAX 25 #define TELNET_WAIT_TIME_MS 5 #define TELNET_LOGIN_RETRIES_MAX 3 -#define TELNET_TIMEOUT_MS 300000 // 5 minutes #define TELNET_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2) /****************************************************************************** @@ -151,7 +150,7 @@ void telnet_run (void) { telnet_wait_for_enabled(); break; case E_TELNET_STE_START: - if (telnet_create_socket()) { + if (wlan_is_connected() && telnet_create_socket()) { telnet_data.state = E_TELNET_STE_LISTEN; } break; @@ -237,7 +236,7 @@ void telnet_run (void) { } if (telnet_data.state >= E_TELNET_STE_CONNECTED) { - if (telnet_data.timeout++ > (TELNET_TIMEOUT_MS / TELNET_CYCLE_TIME_MS)) { + if (telnet_data.timeout++ > (servers_get_timeout() / TELNET_CYCLE_TIME_MS)) { telnet_reset(); } }