cc3200: Fix bug in telnet that caused the rx buffer to overflow.

This commit is contained in:
danicampora 2015-03-22 11:15:01 +01:00
parent 55b74d1ff5
commit 104a867447
3 changed files with 25 additions and 20 deletions

View File

@ -33,7 +33,7 @@
#define SIMPLELINK_SPAWN_TASK_PRIORITY 3 #define SIMPLELINK_SPAWN_TASK_PRIORITY 3
#define SIMPLELINK_TASK_STACK_SIZE 2048 #define SIMPLELINK_TASK_STACK_SIZE 2048
#define SL_STOP_TIMEOUT 35 #define SL_STOP_TIMEOUT 35
#define SL_STOP_TIMEOUT_LONG 205 #define SL_STOP_TIMEOUT_LONG 575
/****************************************************************************** /******************************************************************************
DEFINE TYPES DEFINE TYPES

View File

@ -135,6 +135,7 @@ void TASK_Servers (void *pvParameters) {
void servers_start (void) { void servers_start (void) {
servers_data.do_disable = false; servers_data.do_disable = false;
servers_data.do_enable = true; servers_data.do_enable = true;
servers_data.do_reset = true;
} }
void servers_reset (void) { void servers_reset (void) {

View File

@ -38,15 +38,15 @@
#include "serverstask.h" #include "serverstask.h"
#include "genhdr/py-version.h" #include "genhdr/py-version.h"
/****************************************************************************** /******************************************************************************
DEFINE PRIVATE CONSTANTS DEFINE PRIVATE CONSTANTS
******************************************************************************/ ******************************************************************************/
#define TELNET_PORT 23 #define TELNET_PORT 23
// rxRindex and rxWindex must be uint8_t and TELNET_RX_BUFFER_SIZE == 256
#define TELNET_RX_BUFFER_SIZE 256 #define TELNET_RX_BUFFER_SIZE 256
#define TELNET_MAX_CLIENTS 1 #define TELNET_MAX_CLIENTS 1
#define TELNET_TX_RETRIES_MAX 25 #define TELNET_TX_RETRIES_MAX 25
#define TELNET_WAIT_TIME_MS 7 #define TELNET_WAIT_TIME_MS 5
#define TELNET_LOGIN_RETRIES_MAX 3 #define TELNET_LOGIN_RETRIES_MAX 3
#define TELNET_TIMEOUT_MS 1800000 // 30 minutes #define TELNET_TIMEOUT_MS 1800000 // 30 minutes
#define TELNET_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2) #define TELNET_CYCLE_TIME_MS (SERVERS_CYCLE_TIME_MS * 2)
@ -88,12 +88,15 @@ typedef union {
typedef struct { typedef struct {
uint8_t *rxBuffer; uint8_t *rxBuffer;
uint32_t timeout; uint32_t timeout;
int16_t sd;
int16_t n_sd;
int16_t rxWindex;
int16_t rxRindex;
telnet_state_t state; telnet_state_t state;
telnet_substate_t substate; telnet_substate_t substate;
int16_t sd;
int16_t n_sd;
// rxRindex and rxWindex must be uint8_t and TELNET_RX_BUFFER_SIZE == 256
uint8_t rxWindex;
uint8_t rxRindex;
uint8_t txRetries; uint8_t txRetries;
uint8_t logginRetries; uint8_t logginRetries;
bool enabled; bool enabled;
@ -108,7 +111,7 @@ static const char* telnet_welcome_msg = "Micro Python " MICROPY_GIT_TAG "
static const char* telnet_request_user = "Login as:"; static const char* telnet_request_user = "Login as:";
static const char* telnet_request_password = "Password:"; static const char* telnet_request_password = "Password:";
static const char* telnet_invalid_loggin = "\r\nInvalid credentials, try again\r\n"; static const char* telnet_invalid_loggin = "\r\nInvalid credentials, try again\r\n";
static char telnet_loggin_success[] = "\r\nLogin succeeded!\r\nType \"help()\" for more information.\r\n"; static const char* telnet_loggin_success = "\r\nLogin succeeded!\r\nType \"help()\" for more information.\r\n";
static const uint8_t telnet_options_user[] = // IAC WONT ECHO IAC WONT SUPPRESS_GO_AHEAD IAC WILL LINEMODE static const uint8_t telnet_options_user[] = // IAC WONT ECHO IAC WONT SUPPRESS_GO_AHEAD IAC WILL LINEMODE
{ 255, 252, 1, 255, 252, 3, 255, 251, 34 }; { 255, 252, 1, 255, 252, 3, 255, 251, 34 };
static const uint8_t telnet_options_pass[] = // IAC WILL ECHO IAC WONT SUPPRESS_GO_AHEAD IAC WILL LINEMODE static const uint8_t telnet_options_pass[] = // IAC WILL ECHO IAC WONT SUPPRESS_GO_AHEAD IAC WILL LINEMODE
@ -264,13 +267,14 @@ void telnet_tx_strn_cooked (const char *str, uint len) {
} }
bool telnet_rx_any (void) { bool telnet_rx_any (void) {
return (telnet_data.n_sd > 0) ? ((telnet_data.rxRindex < telnet_data.rxWindex) && return (telnet_data.n_sd > 0) ? ((telnet_data.rxRindex != telnet_data.rxWindex) &&
(telnet_data.state == E_TELNET_STE_LOGGED_IN)) : false; (telnet_data.state == E_TELNET_STE_LOGGED_IN)) : false;
} }
int telnet_rx_char (void) { int telnet_rx_char (void) {
int rx_char = -1; int rx_char = -1;
if (telnet_data.rxRindex < telnet_data.rxWindex) { if (telnet_data.rxRindex != telnet_data.rxWindex) {
// rxRindex must be uint8_t and TELNET_RX_BUFFER_SIZE == 256 so that it wraps around automatically
rx_char = (int)telnet_data.rxBuffer[telnet_data.rxRindex++]; rx_char = (int)telnet_data.rxBuffer[telnet_data.rxRindex++];
} }
return rx_char; return rx_char;
@ -417,16 +421,16 @@ static telnet_result_t telnet_recv_text_non_blocking (void *buff, _i16 Maxlen, _
} }
static void telnet_process (void) { static void telnet_process (void) {
if (!telnet_rx_any()) { _i16 rxLen;
telnet_data.rxWindex = 0; _i16 maxLen = (telnet_data.rxWindex >= telnet_data.rxRindex) ? (TELNET_RX_BUFFER_SIZE - telnet_data.rxWindex) :
telnet_data.rxRindex = 0; ((telnet_data.rxRindex - telnet_data.rxWindex) - 1);
} // to avoid an overrrun
maxLen = (telnet_data.rxRindex == 0) ? (maxLen - 1) : maxLen;
if (telnet_data.rxWindex < TELNET_RX_BUFFER_SIZE) { if (maxLen > 0) {
_i16 rxLen; if (E_TELNET_RESULT_OK == telnet_recv_text_non_blocking(&telnet_data.rxBuffer[telnet_data.rxWindex], maxLen, &rxLen)) {
if (E_TELNET_RESULT_OK == telnet_recv_text_non_blocking(&telnet_data.rxBuffer[telnet_data.rxWindex], // rxWindex must be uint8_t and TELNET_RX_BUFFER_SIZE == 256 so that it wraps around automatically
TELNET_RX_BUFFER_SIZE - telnet_data.rxWindex, &rxLen)) { telnet_data.rxWindex = telnet_data.rxWindex + rxLen;
telnet_data.rxWindex += rxLen;
} }
} }
} }