ThingPlug Test
Dependents: WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P
Fork of WizFi310Interface by
Diff: WizFi310/WizFi310_msg.cpp
- Revision:
- 5:72212beb817c
- Parent:
- 1:16e57103a7dd
- Child:
- 6:007cec5e96c0
diff -r 176b6f3addd6 -r 72212beb817c WizFi310/WizFi310_msg.cpp --- a/WizFi310/WizFi310_msg.cpp Mon Apr 17 06:44:26 2017 +0000 +++ b/WizFi310/WizFi310_msg.cpp Mon Jun 26 00:17:10 2017 +0000 @@ -27,18 +27,23 @@ #define WIZ_DBG(x, ...) #endif -//daniel -char g_asyncbuf[256]; +#define RESP_MSG_OK "[OK]" +#define RESP_MSG_ERROR "[ERROR" +#define RESP_MSG_CONNECT "[CONNECT " +#define RESP_MSG_DISCONNECT "[DISCONNECT " +#define RESP_MSG_LISTEN "[LISTEN " +#define RESP_MSG_MQTTCON "[MQTT CONNECT]" +#define RESP_MSG_MQTTDISCON "[MQTT DISCONNECT]" + // This function is operating in ISR. So you can't use debug message. void WizFi310::recvData ( char c ) { static int cid, sub, len, count; - static int is_mqtt_data = 0; - char tbf[10]; + static int is_mqtt_data = 0; switch(_state.mode) - { + { case MODE_COMMAND: switch(c) { @@ -85,124 +90,118 @@ switch(sub) { case 0: - // cid - if( (c >= '0') && (c <= '9') ) - { - cid = x2i(c); - } - else if ( c == ',' ) - { - sub++; - count = 0; - len = 0; - } - //daniel add for mqtt - else if ( c == 'Q' ) - { - cid = 0; - is_mqtt_data = 1; - } - // - else - { - _state.mode = MODE_COMMAND; - } - break; + // cid + if( (c >= '0') && (c <= '9') ) + { + cid = x2i(c); + } + else if ( c == ',' ) + { + sub++; + count = 0; + len = 0; + } + //daniel add for mqtt + else if ( c == 'Q' ) + { + cid = 0; + is_mqtt_data = 1; + } + // + else + { + _state.mode = MODE_COMMAND; + } + break; - case 1: - // ip -// if ((c >= '0' && c <= '9') || c == '.') - if (((c >= '0' && c <= '9') || c == '.') && is_mqtt_data == 0 ) - { - _con[cid].ip[count] = c; - count++; - } - else if( c == ',' ) - { - _con[cid].ip[count] = '\0'; - _con[cid].port = 0; - sub++; - } - //daniel for mqtt - else if( is_mqtt_data == 1) - { - rcvd_mqtt_topic[count] = c; - count++; - } -// else - else if( is_mqtt_data == 0 ) - { - _state.mode = MODE_COMMAND; + case 1: + // ip + // if ((c >= '0' && c <= '9') || c == '.') + if (((c >= '0' && c <= '9') || c == '.') && is_mqtt_data == 0 ) + { + _con[cid].ip[count] = c; + count++; + } + else if( c == ',' ) + { + _con[cid].ip[count] = '\0'; + _con[cid].port = 0; + sub++; + } + //daniel for mqtt + else if( is_mqtt_data == 1) + { + rcvd_mqtt_topic[count] = c; + count++; + } + // else + else if( is_mqtt_data == 0 ) + { + _state.mode = MODE_COMMAND; + } + break; + + case 2: + // port + if ( c >= '0' && c <= '9' ) + { + _con[cid].port = (_con[cid].port * 10) + ( c - '0' ); + } + else if( c == ',') + { + sub++; + count = 0; + } + else + { + _state.mode = MODE_COMMAND; + } + break; + + case 3: + // data length + if ( c >= '0' && c <= '9' ) + { + //_con[cid].recv_length = (_con[cid].recv_length * 10) + (c - '0'); + len = (len * 10) + (c - '0'); + } + else if( c == '}' ) + { + sub++; + count = 0; + _con[cid].recv_length = len; + } + else + { + _state.mode = MODE_COMMAND; + } + break; + + default: + + if(_con[cid].buf != NULL) + { + _con[cid].buf->queue(c); + if(_con[cid].buf->available() > CFG_DATA_SIZE - 16 ) + { + setRts(false); // blcok + _con[cid].received = true; + WIZ_WARN("buf full"); + } + } + _con[cid].recv_length--; + if(_con[cid].recv_length == 0) + { + _con[cid].received = true; + _state.mode = MODE_COMMAND; + } + break; } break; - - case 2: - // port - if ( c >= '0' && c <= '9' ) - { - _con[cid].port = (_con[cid].port * 10) + ( c - '0' ); - } - else if( c == ',') - { - sub++; - count = 0; - } - else - { - _state.mode = MODE_COMMAND; - } - break; - - case 3: - // data length - if ( c >= '0' && c <= '9' ) - { - //_con[cid].recv_length = (_con[cid].recv_length * 10) + (c - '0'); - len = (len * 10) + (c - '0'); - } - else if( c == '}' ) - { - sub++; - count = 0; - _con[cid].recv_length = len; - } - else - { - _state.mode = MODE_COMMAND; - } - break; - - default: - - if(_con[cid].buf != NULL) - { - _con[cid].buf->queue(c); - if(_con[cid].buf->available() > CFG_DATA_SIZE - 16 ) - { - setRts(false); // blcok - _con[cid].received = true; - WIZ_WARN("buf full"); - } - } - _con[cid].recv_length--; - if(_con[cid].recv_length == 0) - { - //WIZ_DBG("recv cid: %d, count : %d, len : %d",cid, count, len); - //sprintf(tbf, "recv cid: %d, count : %d, len : %d",cid, count, len); - //strcat(g_asyncbuf, tbf); - _con[cid].received = true; - _state.mode = MODE_COMMAND; - } - break; - } - break; } } - -//#define MSG_TABLE_NUM 6 -//daniel -#define MSG_TABLE_NUM 8 +#define MSG_TABLE_NUM 7 #define RES_TABLE_NUM 7 int WizFi310::parseMessage () { int i; @@ -212,15 +211,14 @@ const char msg[24]; void (WizFi310::*func)(const char *); } msg_table[MSG_TABLE_NUM] = { - {"[OK]", &WizFi310::msgOk}, - {"[ERROR]", &WizFi310::msgError}, - {"[ERROR:INVALIDINPUT]", &WizFi310::msgError}, - {"[CONNECT ", &WizFi310::msgConnect}, - {"[DISCONNECT ", &WizFi310::msgDisconnect}, - {"[LISTEN ", &WizFi310::msgListen}, - //daniel - {"[MQTT CONNECT]", &WizFi310::msgMQTTConnect}, - {"[MQTT DISCONNECT]", &WizFi310::msgMQTTDisconnect}, + {RESP_MSG_OK, &WizFi310::msgOk}, + {RESP_MSG_ERROR, &WizFi310::msgError}, +// {"[ERROR:INVALIDINPUT]", &WizFi310::msgError}, + {RESP_MSG_CONNECT, &WizFi310::msgConnect}, + {RESP_MSG_DISCONNECT, &WizFi310::msgDisconnect}, + {RESP_MSG_LISTEN, &WizFi310::msgListen}, + {RESP_MSG_MQTTCON, &WizFi310::msgMQTTConnect}, + {RESP_MSG_MQTTDISCON, &WizFi310::msgMQTTDisconnect}, }; static const struct RES_TABLE{ const Response res; @@ -243,16 +241,13 @@ } buf[i] = '\0'; - //strncpy(_state.dbgRespBuf, buf, sizeof(buf) ); - //WIZ_DBG("%s\r\n",_state.dbgRespBuf); - + if(_state.res != RES_NULL) { for( i=0; i<RES_TABLE_NUM; i++) { if(res_table[i].res == _state.res) { - //WIZ_DBG("parse res %d '%s'\r\n", i, buf); if(res_table[i].func != NULL) { (this->*(res_table[i].func))(buf); @@ -268,7 +263,6 @@ { if( strncmp(buf, msg_table[i].msg, strlen(msg_table[i].msg)) == 0 ) { - //WIZ_DBG("parse msg '%s'\r\n", buf); if(msg_table[i].func != NULL) { (this->*(msg_table[i].func))(buf); @@ -295,10 +289,11 @@ { int cid; - if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; + //if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; + if( isdigit (buf[9])) return; cid = x2i(buf[9]); - + initCon(cid, true); _state.cid = cid; _con[cid].accept = true; @@ -309,7 +304,8 @@ { int cid; - if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; + //if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; + if( isdigit (buf[9])) return; cid = x2i(buf[12]); _con[cid].connected = false; @@ -320,9 +316,6 @@ { int cid = 0; - //if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; - - //cid = x2i(buf[9]); initCon(cid, true); _state.cid = cid; _con[cid].accept = true; @@ -336,10 +329,6 @@ void WizFi310::msgMQTTDisconnect (const char *buf) { int cid = 0; - - //if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; - - //cid = x2i(buf[12]); _con[cid].connected = false; } @@ -348,7 +337,8 @@ { int cid; - if(buf[8] < '0' || buf[8] > '8' || buf[9] != ']') return; + //if(buf[8] < '0' || buf[8] > '8' || buf[9] != ']') return; + if( isdigit (buf[9])) return; cid = x2i(buf[8]); _state.cid = cid; @@ -362,8 +352,9 @@ _state.mac[17] = 0; _state.res = RES_NULL; - if(strncmp(_state.mac,CFG_DEFAULT_MAC,sizeof(CFG_DEFAULT_MAC)) == 0) + if(strncmp(_state.mac,CFG_DEFAULT_MAC,sizeof(CFG_DEFAULT_MAC)) == 0){ _state.ok = false; + } _state.ok = true; } } @@ -372,13 +363,10 @@ { int cid; - if (buf[0] == '[' && buf[1] == 'O' && buf[2] == 'K' && buf[3] == ']') - { + if( strstr((char*)buf, RESP_MSG_OK) != NULL){ _state.n++; } - else if( buf[0] == '[' && buf[1] == 'C' && buf[2] == 'O' && buf[3] == 'N' && - buf[4] == 'N' && buf[5] == 'E' && buf[6] == 'C' && buf[7] == 'T') - { + else if( strstr((char*)buf, RESP_MSG_CONNECT) != NULL){ cid = x2i(buf[9]); _state.cid = cid; _state.n++; @@ -421,7 +409,8 @@ int cid, i; char *c; - if( (buf[0] < '0' || buf[0] > '8') ) return; +// if( (buf[0] < '0' || buf[0] > '8') ) return; + if( isdigit (buf[9])) return; cid = x2i(buf[0]); if( cid != _state.cid ) return; @@ -456,34 +445,25 @@ void WizFi310::resWSTATUS (const char *buf) { - int idx=0,sep_cnt=0; - int ip_idx=0,gw_idx=0; - + char* idx_ptr; + if(_state.n == 0) { _state.n++; } else if(_state.n == 1) { - for(idx=0;buf[idx]!='\r';idx++) - { - if(buf[idx] =='/') - { - sep_cnt++; - continue; - } + idx_ptr = strtok((char*)buf, "/"); // Interface STA or AP + idx_ptr = strtok( NULL, "/"); // SSID + idx_ptr = strtok( NULL, "/"); // IP Addr + memset(_state.ip, 0, sizeof(_state.ip)); + memcpy(_state.ip, idx_ptr, strlen(idx_ptr)+1); + + idx_ptr = strtok( NULL, "/"); // Gateway Addr + memset(_state.gateway, 0, sizeof(_state.gateway)); + memcpy(_state.gateway, idx_ptr, strlen(idx_ptr)+1); - if( sep_cnt == 2) // IP Address - { - _state.ip[ip_idx++] = buf[idx]; - } - else if(sep_cnt == 3) - { - _state.gateway[gw_idx++] = buf[idx]; - } - } - _state.ip[ip_idx] = '\0'; - _state.gateway[gw_idx] = '\0'; _state.res = RES_NULL; } -} \ No newline at end of file + +}