WizFi310Interface
Fork of WizFi310Interface_Legacynew by
WizFi310/WizFi310_msg.cpp@0:774ff1e8b26b, 2017-04-19 (annotated)
- Committer:
- kaizen
- Date:
- Wed Apr 19 00:46:44 2017 +0000
- Revision:
- 0:774ff1e8b26b
First release WizFi310Interface for mbed OS2 ( Legacy version )
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:774ff1e8b26b | 1 | /* |
kaizen | 0:774ff1e8b26b | 2 | * Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:774ff1e8b26b | 3 | * |
kaizen | 0:774ff1e8b26b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:774ff1e8b26b | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:774ff1e8b26b | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:774ff1e8b26b | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:774ff1e8b26b | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:774ff1e8b26b | 9 | * |
kaizen | 0:774ff1e8b26b | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:774ff1e8b26b | 11 | * substantial portions of the Software. |
kaizen | 0:774ff1e8b26b | 12 | * |
kaizen | 0:774ff1e8b26b | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:774ff1e8b26b | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:774ff1e8b26b | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:774ff1e8b26b | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:774ff1e8b26b | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:774ff1e8b26b | 18 | */ |
kaizen | 0:774ff1e8b26b | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:774ff1e8b26b | 21 | */ |
kaizen | 0:774ff1e8b26b | 22 | /* Copyright (C) 2017 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 23 | * port to the Wiznet Module WizFi310 |
kaizen | 0:774ff1e8b26b | 24 | */ |
kaizen | 0:774ff1e8b26b | 25 | |
kaizen | 0:774ff1e8b26b | 26 | #include "WizFi310.h" |
kaizen | 0:774ff1e8b26b | 27 | |
kaizen | 0:774ff1e8b26b | 28 | #ifdef CFG_ENABLE_RTOS |
kaizen | 0:774ff1e8b26b | 29 | #undef WIZ_DBG |
kaizen | 0:774ff1e8b26b | 30 | #define WIZ_DBG(x, ...) |
kaizen | 0:774ff1e8b26b | 31 | #endif |
kaizen | 0:774ff1e8b26b | 32 | |
kaizen | 0:774ff1e8b26b | 33 | //daniel |
kaizen | 0:774ff1e8b26b | 34 | char g_asyncbuf[256]; |
kaizen | 0:774ff1e8b26b | 35 | |
kaizen | 0:774ff1e8b26b | 36 | // This function is operating in ISR. So you can't use debug message. |
kaizen | 0:774ff1e8b26b | 37 | void WizFi310::recvData ( char c ) |
kaizen | 0:774ff1e8b26b | 38 | { |
kaizen | 0:774ff1e8b26b | 39 | static int cid, sub, len, count; |
kaizen | 0:774ff1e8b26b | 40 | static int is_mqtt_data = 0; |
kaizen | 0:774ff1e8b26b | 41 | char tbf[10]; |
kaizen | 0:774ff1e8b26b | 42 | |
kaizen | 0:774ff1e8b26b | 43 | switch(_state.mode) |
kaizen | 0:774ff1e8b26b | 44 | { |
kaizen | 0:774ff1e8b26b | 45 | case MODE_COMMAND: |
kaizen | 0:774ff1e8b26b | 46 | switch(c) |
kaizen | 0:774ff1e8b26b | 47 | { |
kaizen | 0:774ff1e8b26b | 48 | case 0: |
kaizen | 0:774ff1e8b26b | 49 | case 0x0a: // LF |
kaizen | 0:774ff1e8b26b | 50 | case 0x0d: // CR |
kaizen | 0:774ff1e8b26b | 51 | break; |
kaizen | 0:774ff1e8b26b | 52 | |
kaizen | 0:774ff1e8b26b | 53 | case '{': |
kaizen | 0:774ff1e8b26b | 54 | _state.buf->flush(); |
kaizen | 0:774ff1e8b26b | 55 | _state.mode = MODE_DATA_RX; |
kaizen | 0:774ff1e8b26b | 56 | sub = 0; |
kaizen | 0:774ff1e8b26b | 57 | break; |
kaizen | 0:774ff1e8b26b | 58 | |
kaizen | 0:774ff1e8b26b | 59 | default: |
kaizen | 0:774ff1e8b26b | 60 | _state.buf->flush(); |
kaizen | 0:774ff1e8b26b | 61 | _state.buf->queue(c); |
kaizen | 0:774ff1e8b26b | 62 | _state.mode = MODE_CMDRESP; |
kaizen | 0:774ff1e8b26b | 63 | break; |
kaizen | 0:774ff1e8b26b | 64 | } |
kaizen | 0:774ff1e8b26b | 65 | break; |
kaizen | 0:774ff1e8b26b | 66 | |
kaizen | 0:774ff1e8b26b | 67 | case MODE_CMDRESP: |
kaizen | 0:774ff1e8b26b | 68 | switch(c) |
kaizen | 0:774ff1e8b26b | 69 | { |
kaizen | 0:774ff1e8b26b | 70 | case 0: |
kaizen | 0:774ff1e8b26b | 71 | break; |
kaizen | 0:774ff1e8b26b | 72 | case 0x0a: // LF |
kaizen | 0:774ff1e8b26b | 73 | break; |
kaizen | 0:774ff1e8b26b | 74 | case 0x0d: // CR |
kaizen | 0:774ff1e8b26b | 75 | if (_flow == 2) setRts(false); // block |
kaizen | 0:774ff1e8b26b | 76 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 77 | parseMessage(); |
kaizen | 0:774ff1e8b26b | 78 | if (_flow == 2) setRts(true); // release |
kaizen | 0:774ff1e8b26b | 79 | break; |
kaizen | 0:774ff1e8b26b | 80 | default: |
kaizen | 0:774ff1e8b26b | 81 | _state.buf->queue(c); |
kaizen | 0:774ff1e8b26b | 82 | break; |
kaizen | 0:774ff1e8b26b | 83 | } |
kaizen | 0:774ff1e8b26b | 84 | break; |
kaizen | 0:774ff1e8b26b | 85 | |
kaizen | 0:774ff1e8b26b | 86 | case MODE_DATA_RX: |
kaizen | 0:774ff1e8b26b | 87 | |
kaizen | 0:774ff1e8b26b | 88 | switch(sub) |
kaizen | 0:774ff1e8b26b | 89 | { |
kaizen | 0:774ff1e8b26b | 90 | case 0: |
kaizen | 0:774ff1e8b26b | 91 | // cid |
kaizen | 0:774ff1e8b26b | 92 | if( (c >= '0') && (c <= '9') ) |
kaizen | 0:774ff1e8b26b | 93 | { |
kaizen | 0:774ff1e8b26b | 94 | cid = x2i(c); |
kaizen | 0:774ff1e8b26b | 95 | } |
kaizen | 0:774ff1e8b26b | 96 | else if ( c == ',' ) |
kaizen | 0:774ff1e8b26b | 97 | { |
kaizen | 0:774ff1e8b26b | 98 | sub++; |
kaizen | 0:774ff1e8b26b | 99 | count = 0; |
kaizen | 0:774ff1e8b26b | 100 | len = 0; |
kaizen | 0:774ff1e8b26b | 101 | } |
kaizen | 0:774ff1e8b26b | 102 | //daniel add for mqtt |
kaizen | 0:774ff1e8b26b | 103 | else if ( c == 'Q' ) |
kaizen | 0:774ff1e8b26b | 104 | { |
kaizen | 0:774ff1e8b26b | 105 | cid = 0; |
kaizen | 0:774ff1e8b26b | 106 | is_mqtt_data = 1; |
kaizen | 0:774ff1e8b26b | 107 | } |
kaizen | 0:774ff1e8b26b | 108 | // |
kaizen | 0:774ff1e8b26b | 109 | else |
kaizen | 0:774ff1e8b26b | 110 | { |
kaizen | 0:774ff1e8b26b | 111 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 112 | } |
kaizen | 0:774ff1e8b26b | 113 | break; |
kaizen | 0:774ff1e8b26b | 114 | |
kaizen | 0:774ff1e8b26b | 115 | case 1: |
kaizen | 0:774ff1e8b26b | 116 | // ip |
kaizen | 0:774ff1e8b26b | 117 | // if ((c >= '0' && c <= '9') || c == '.') |
kaizen | 0:774ff1e8b26b | 118 | if (((c >= '0' && c <= '9') || c == '.') && is_mqtt_data == 0 ) |
kaizen | 0:774ff1e8b26b | 119 | { |
kaizen | 0:774ff1e8b26b | 120 | _con[cid].ip[count] = c; |
kaizen | 0:774ff1e8b26b | 121 | count++; |
kaizen | 0:774ff1e8b26b | 122 | } |
kaizen | 0:774ff1e8b26b | 123 | else if( c == ',' ) |
kaizen | 0:774ff1e8b26b | 124 | { |
kaizen | 0:774ff1e8b26b | 125 | _con[cid].ip[count] = '\0'; |
kaizen | 0:774ff1e8b26b | 126 | _con[cid].port = 0; |
kaizen | 0:774ff1e8b26b | 127 | sub++; |
kaizen | 0:774ff1e8b26b | 128 | } |
kaizen | 0:774ff1e8b26b | 129 | //daniel for mqtt |
kaizen | 0:774ff1e8b26b | 130 | else if( is_mqtt_data == 1) |
kaizen | 0:774ff1e8b26b | 131 | { |
kaizen | 0:774ff1e8b26b | 132 | rcvd_mqtt_topic[count] = c; |
kaizen | 0:774ff1e8b26b | 133 | count++; |
kaizen | 0:774ff1e8b26b | 134 | } |
kaizen | 0:774ff1e8b26b | 135 | // else |
kaizen | 0:774ff1e8b26b | 136 | else if( is_mqtt_data == 0 ) |
kaizen | 0:774ff1e8b26b | 137 | { |
kaizen | 0:774ff1e8b26b | 138 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 139 | } |
kaizen | 0:774ff1e8b26b | 140 | break; |
kaizen | 0:774ff1e8b26b | 141 | |
kaizen | 0:774ff1e8b26b | 142 | case 2: |
kaizen | 0:774ff1e8b26b | 143 | // port |
kaizen | 0:774ff1e8b26b | 144 | if ( c >= '0' && c <= '9' ) |
kaizen | 0:774ff1e8b26b | 145 | { |
kaizen | 0:774ff1e8b26b | 146 | _con[cid].port = (_con[cid].port * 10) + ( c - '0' ); |
kaizen | 0:774ff1e8b26b | 147 | } |
kaizen | 0:774ff1e8b26b | 148 | else if( c == ',') |
kaizen | 0:774ff1e8b26b | 149 | { |
kaizen | 0:774ff1e8b26b | 150 | sub++; |
kaizen | 0:774ff1e8b26b | 151 | count = 0; |
kaizen | 0:774ff1e8b26b | 152 | } |
kaizen | 0:774ff1e8b26b | 153 | else |
kaizen | 0:774ff1e8b26b | 154 | { |
kaizen | 0:774ff1e8b26b | 155 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 156 | } |
kaizen | 0:774ff1e8b26b | 157 | break; |
kaizen | 0:774ff1e8b26b | 158 | |
kaizen | 0:774ff1e8b26b | 159 | case 3: |
kaizen | 0:774ff1e8b26b | 160 | // data length |
kaizen | 0:774ff1e8b26b | 161 | if ( c >= '0' && c <= '9' ) |
kaizen | 0:774ff1e8b26b | 162 | { |
kaizen | 0:774ff1e8b26b | 163 | //_con[cid].recv_length = (_con[cid].recv_length * 10) + (c - '0'); |
kaizen | 0:774ff1e8b26b | 164 | len = (len * 10) + (c - '0'); |
kaizen | 0:774ff1e8b26b | 165 | } |
kaizen | 0:774ff1e8b26b | 166 | else if( c == '}' ) |
kaizen | 0:774ff1e8b26b | 167 | { |
kaizen | 0:774ff1e8b26b | 168 | sub++; |
kaizen | 0:774ff1e8b26b | 169 | count = 0; |
kaizen | 0:774ff1e8b26b | 170 | _con[cid].recv_length = len; |
kaizen | 0:774ff1e8b26b | 171 | } |
kaizen | 0:774ff1e8b26b | 172 | else |
kaizen | 0:774ff1e8b26b | 173 | { |
kaizen | 0:774ff1e8b26b | 174 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 175 | } |
kaizen | 0:774ff1e8b26b | 176 | break; |
kaizen | 0:774ff1e8b26b | 177 | |
kaizen | 0:774ff1e8b26b | 178 | default: |
kaizen | 0:774ff1e8b26b | 179 | |
kaizen | 0:774ff1e8b26b | 180 | if(_con[cid].buf != NULL) |
kaizen | 0:774ff1e8b26b | 181 | { |
kaizen | 0:774ff1e8b26b | 182 | _con[cid].buf->queue(c); |
kaizen | 0:774ff1e8b26b | 183 | if(_con[cid].buf->available() > CFG_DATA_SIZE - 16 ) |
kaizen | 0:774ff1e8b26b | 184 | { |
kaizen | 0:774ff1e8b26b | 185 | setRts(false); // blcok |
kaizen | 0:774ff1e8b26b | 186 | _con[cid].received = true; |
kaizen | 0:774ff1e8b26b | 187 | WIZ_WARN("buf full"); |
kaizen | 0:774ff1e8b26b | 188 | } |
kaizen | 0:774ff1e8b26b | 189 | } |
kaizen | 0:774ff1e8b26b | 190 | _con[cid].recv_length--; |
kaizen | 0:774ff1e8b26b | 191 | if(_con[cid].recv_length == 0) |
kaizen | 0:774ff1e8b26b | 192 | { |
kaizen | 0:774ff1e8b26b | 193 | //WIZ_DBG("recv cid: %d, count : %d, len : %d",cid, count, len); |
kaizen | 0:774ff1e8b26b | 194 | //sprintf(tbf, "recv cid: %d, count : %d, len : %d",cid, count, len); |
kaizen | 0:774ff1e8b26b | 195 | //strcat(g_asyncbuf, tbf); |
kaizen | 0:774ff1e8b26b | 196 | _con[cid].received = true; |
kaizen | 0:774ff1e8b26b | 197 | _state.mode = MODE_COMMAND; |
kaizen | 0:774ff1e8b26b | 198 | } |
kaizen | 0:774ff1e8b26b | 199 | break; |
kaizen | 0:774ff1e8b26b | 200 | } |
kaizen | 0:774ff1e8b26b | 201 | break; |
kaizen | 0:774ff1e8b26b | 202 | } |
kaizen | 0:774ff1e8b26b | 203 | } |
kaizen | 0:774ff1e8b26b | 204 | |
kaizen | 0:774ff1e8b26b | 205 | |
kaizen | 0:774ff1e8b26b | 206 | //#define MSG_TABLE_NUM 6 |
kaizen | 0:774ff1e8b26b | 207 | //daniel |
kaizen | 0:774ff1e8b26b | 208 | #define MSG_TABLE_NUM 8 |
kaizen | 0:774ff1e8b26b | 209 | #define RES_TABLE_NUM 7 |
kaizen | 0:774ff1e8b26b | 210 | int WizFi310::parseMessage () { |
kaizen | 0:774ff1e8b26b | 211 | int i; |
kaizen | 0:774ff1e8b26b | 212 | char buf[128]; |
kaizen | 0:774ff1e8b26b | 213 | |
kaizen | 0:774ff1e8b26b | 214 | static const struct MSG_TABLE { |
kaizen | 0:774ff1e8b26b | 215 | const char msg[24]; |
kaizen | 0:774ff1e8b26b | 216 | void (WizFi310::*func)(const char *); |
kaizen | 0:774ff1e8b26b | 217 | } msg_table[MSG_TABLE_NUM] = { |
kaizen | 0:774ff1e8b26b | 218 | {"[OK]", &WizFi310::msgOk}, |
kaizen | 0:774ff1e8b26b | 219 | {"[ERROR]", &WizFi310::msgError}, |
kaizen | 0:774ff1e8b26b | 220 | {"[ERROR:INVALIDINPUT]", &WizFi310::msgError}, |
kaizen | 0:774ff1e8b26b | 221 | {"[CONNECT ", &WizFi310::msgConnect}, |
kaizen | 0:774ff1e8b26b | 222 | {"[DISCONNECT ", &WizFi310::msgDisconnect}, |
kaizen | 0:774ff1e8b26b | 223 | {"[LISTEN ", &WizFi310::msgListen}, |
kaizen | 0:774ff1e8b26b | 224 | //daniel |
kaizen | 0:774ff1e8b26b | 225 | {"[MQTT CONNECT]", &WizFi310::msgMQTTConnect}, |
kaizen | 0:774ff1e8b26b | 226 | {"[MQTT DISCONNECT]", &WizFi310::msgMQTTDisconnect}, |
kaizen | 0:774ff1e8b26b | 227 | }; |
kaizen | 0:774ff1e8b26b | 228 | static const struct RES_TABLE{ |
kaizen | 0:774ff1e8b26b | 229 | const Response res; |
kaizen | 0:774ff1e8b26b | 230 | void (WizFi310::*func)(const char *); |
kaizen | 0:774ff1e8b26b | 231 | }res_table[RES_TABLE_NUM]={ |
kaizen | 0:774ff1e8b26b | 232 | {RES_NULL, NULL}, |
kaizen | 0:774ff1e8b26b | 233 | {RES_MACADDRESS, &WizFi310::resMacAddress}, |
kaizen | 0:774ff1e8b26b | 234 | // {RES_WJOIN, &WizFi310::resWJOIN}, |
kaizen | 0:774ff1e8b26b | 235 | {RES_CONNECT, &WizFi310::resConnect}, |
kaizen | 0:774ff1e8b26b | 236 | {RES_SSEND, &WizFi310::resSSEND}, |
kaizen | 0:774ff1e8b26b | 237 | {RES_FDNS, &WizFi310::resFDNS}, |
kaizen | 0:774ff1e8b26b | 238 | {RES_SMGMT, &WizFi310::resSMGMT}, |
kaizen | 0:774ff1e8b26b | 239 | {RES_WSTATUS, &WizFi310::resWSTATUS}, |
kaizen | 0:774ff1e8b26b | 240 | }; |
kaizen | 0:774ff1e8b26b | 241 | |
kaizen | 0:774ff1e8b26b | 242 | |
kaizen | 0:774ff1e8b26b | 243 | for( i=0; i<sizeof(buf); i++ ) |
kaizen | 0:774ff1e8b26b | 244 | { |
kaizen | 0:774ff1e8b26b | 245 | if( _state.buf->dequeue(&buf[i]) == false ) break; |
kaizen | 0:774ff1e8b26b | 246 | } |
kaizen | 0:774ff1e8b26b | 247 | |
kaizen | 0:774ff1e8b26b | 248 | buf[i] = '\0'; |
kaizen | 0:774ff1e8b26b | 249 | //strncpy(_state.dbgRespBuf, buf, sizeof(buf) ); |
kaizen | 0:774ff1e8b26b | 250 | //WIZ_DBG("%s\r\n",_state.dbgRespBuf); |
kaizen | 0:774ff1e8b26b | 251 | |
kaizen | 0:774ff1e8b26b | 252 | if(_state.res != RES_NULL) |
kaizen | 0:774ff1e8b26b | 253 | { |
kaizen | 0:774ff1e8b26b | 254 | for( i=0; i<RES_TABLE_NUM; i++) |
kaizen | 0:774ff1e8b26b | 255 | { |
kaizen | 0:774ff1e8b26b | 256 | if(res_table[i].res == _state.res) |
kaizen | 0:774ff1e8b26b | 257 | { |
kaizen | 0:774ff1e8b26b | 258 | //WIZ_DBG("parse res %d '%s'\r\n", i, buf); |
kaizen | 0:774ff1e8b26b | 259 | if(res_table[i].func != NULL) |
kaizen | 0:774ff1e8b26b | 260 | { |
kaizen | 0:774ff1e8b26b | 261 | (this->*(res_table[i].func))(buf); |
kaizen | 0:774ff1e8b26b | 262 | } |
kaizen | 0:774ff1e8b26b | 263 | |
kaizen | 0:774ff1e8b26b | 264 | if(res_table[i].res == RES_CONNECT && _state.n < 2) |
kaizen | 0:774ff1e8b26b | 265 | return -1; |
kaizen | 0:774ff1e8b26b | 266 | } |
kaizen | 0:774ff1e8b26b | 267 | } |
kaizen | 0:774ff1e8b26b | 268 | } |
kaizen | 0:774ff1e8b26b | 269 | |
kaizen | 0:774ff1e8b26b | 270 | for( i=0; i<MSG_TABLE_NUM; i++) |
kaizen | 0:774ff1e8b26b | 271 | { |
kaizen | 0:774ff1e8b26b | 272 | if( strncmp(buf, msg_table[i].msg, strlen(msg_table[i].msg)) == 0 ) |
kaizen | 0:774ff1e8b26b | 273 | { |
kaizen | 0:774ff1e8b26b | 274 | //WIZ_DBG("parse msg '%s'\r\n", buf); |
kaizen | 0:774ff1e8b26b | 275 | if(msg_table[i].func != NULL) |
kaizen | 0:774ff1e8b26b | 276 | { |
kaizen | 0:774ff1e8b26b | 277 | (this->*(msg_table[i].func))(buf); |
kaizen | 0:774ff1e8b26b | 278 | } |
kaizen | 0:774ff1e8b26b | 279 | return 0; |
kaizen | 0:774ff1e8b26b | 280 | } |
kaizen | 0:774ff1e8b26b | 281 | } |
kaizen | 0:774ff1e8b26b | 282 | |
kaizen | 0:774ff1e8b26b | 283 | return -1; |
kaizen | 0:774ff1e8b26b | 284 | } |
kaizen | 0:774ff1e8b26b | 285 | |
kaizen | 0:774ff1e8b26b | 286 | |
kaizen | 0:774ff1e8b26b | 287 | void WizFi310::msgOk (const char *buf) |
kaizen | 0:774ff1e8b26b | 288 | { |
kaizen | 0:774ff1e8b26b | 289 | _state.ok = true; |
kaizen | 0:774ff1e8b26b | 290 | } |
kaizen | 0:774ff1e8b26b | 291 | |
kaizen | 0:774ff1e8b26b | 292 | void WizFi310::msgError (const char *buf) |
kaizen | 0:774ff1e8b26b | 293 | { |
kaizen | 0:774ff1e8b26b | 294 | _state.failure = true; |
kaizen | 0:774ff1e8b26b | 295 | } |
kaizen | 0:774ff1e8b26b | 296 | |
kaizen | 0:774ff1e8b26b | 297 | void WizFi310::msgConnect (const char *buf) |
kaizen | 0:774ff1e8b26b | 298 | { |
kaizen | 0:774ff1e8b26b | 299 | int cid; |
kaizen | 0:774ff1e8b26b | 300 | |
kaizen | 0:774ff1e8b26b | 301 | if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; |
kaizen | 0:774ff1e8b26b | 302 | |
kaizen | 0:774ff1e8b26b | 303 | cid = x2i(buf[9]); |
kaizen | 0:774ff1e8b26b | 304 | |
kaizen | 0:774ff1e8b26b | 305 | initCon(cid, true); |
kaizen | 0:774ff1e8b26b | 306 | _state.cid = cid; |
kaizen | 0:774ff1e8b26b | 307 | _con[cid].accept = true; |
kaizen | 0:774ff1e8b26b | 308 | _con[cid].parent = cid; |
kaizen | 0:774ff1e8b26b | 309 | } |
kaizen | 0:774ff1e8b26b | 310 | |
kaizen | 0:774ff1e8b26b | 311 | void WizFi310::msgDisconnect (const char *buf) |
kaizen | 0:774ff1e8b26b | 312 | { |
kaizen | 0:774ff1e8b26b | 313 | int cid; |
kaizen | 0:774ff1e8b26b | 314 | |
kaizen | 0:774ff1e8b26b | 315 | if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; |
kaizen | 0:774ff1e8b26b | 316 | |
kaizen | 0:774ff1e8b26b | 317 | cid = x2i(buf[12]); |
kaizen | 0:774ff1e8b26b | 318 | _con[cid].connected = false; |
kaizen | 0:774ff1e8b26b | 319 | } |
kaizen | 0:774ff1e8b26b | 320 | |
kaizen | 0:774ff1e8b26b | 321 | |
kaizen | 0:774ff1e8b26b | 322 | void WizFi310::msgMQTTConnect (const char *buf) |
kaizen | 0:774ff1e8b26b | 323 | { |
kaizen | 0:774ff1e8b26b | 324 | int cid = 0; |
kaizen | 0:774ff1e8b26b | 325 | |
kaizen | 0:774ff1e8b26b | 326 | //if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; |
kaizen | 0:774ff1e8b26b | 327 | |
kaizen | 0:774ff1e8b26b | 328 | //cid = x2i(buf[9]); |
kaizen | 0:774ff1e8b26b | 329 | initCon(cid, true); |
kaizen | 0:774ff1e8b26b | 330 | _state.cid = cid; |
kaizen | 0:774ff1e8b26b | 331 | _con[cid].accept = true; |
kaizen | 0:774ff1e8b26b | 332 | _con[cid].parent = cid; |
kaizen | 0:774ff1e8b26b | 333 | |
kaizen | 0:774ff1e8b26b | 334 | _con[cid].connected = true; |
kaizen | 0:774ff1e8b26b | 335 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 336 | _state.ok = true; |
kaizen | 0:774ff1e8b26b | 337 | } |
kaizen | 0:774ff1e8b26b | 338 | |
kaizen | 0:774ff1e8b26b | 339 | void WizFi310::msgMQTTDisconnect (const char *buf) |
kaizen | 0:774ff1e8b26b | 340 | { |
kaizen | 0:774ff1e8b26b | 341 | int cid = 0; |
kaizen | 0:774ff1e8b26b | 342 | |
kaizen | 0:774ff1e8b26b | 343 | //if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; |
kaizen | 0:774ff1e8b26b | 344 | |
kaizen | 0:774ff1e8b26b | 345 | //cid = x2i(buf[12]); |
kaizen | 0:774ff1e8b26b | 346 | _con[cid].connected = false; |
kaizen | 0:774ff1e8b26b | 347 | } |
kaizen | 0:774ff1e8b26b | 348 | |
kaizen | 0:774ff1e8b26b | 349 | |
kaizen | 0:774ff1e8b26b | 350 | void WizFi310::msgListen (const char *buf) |
kaizen | 0:774ff1e8b26b | 351 | { |
kaizen | 0:774ff1e8b26b | 352 | int cid; |
kaizen | 0:774ff1e8b26b | 353 | |
kaizen | 0:774ff1e8b26b | 354 | if(buf[8] < '0' || buf[8] > '8' || buf[9] != ']') return; |
kaizen | 0:774ff1e8b26b | 355 | |
kaizen | 0:774ff1e8b26b | 356 | cid = x2i(buf[8]); |
kaizen | 0:774ff1e8b26b | 357 | _state.cid = cid; |
kaizen | 0:774ff1e8b26b | 358 | } |
kaizen | 0:774ff1e8b26b | 359 | |
kaizen | 0:774ff1e8b26b | 360 | void WizFi310::resMacAddress (const char *buf) |
kaizen | 0:774ff1e8b26b | 361 | { |
kaizen | 0:774ff1e8b26b | 362 | if( buf[2] == ':' && buf[5] == ':') |
kaizen | 0:774ff1e8b26b | 363 | { |
kaizen | 0:774ff1e8b26b | 364 | strncpy(_state.mac, buf, sizeof(_state.mac)); |
kaizen | 0:774ff1e8b26b | 365 | _state.mac[17] = 0; |
kaizen | 0:774ff1e8b26b | 366 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 367 | |
kaizen | 0:774ff1e8b26b | 368 | if(strncmp(_state.mac,CFG_DEFAULT_MAC,sizeof(CFG_DEFAULT_MAC)) == 0) |
kaizen | 0:774ff1e8b26b | 369 | _state.ok = false; |
kaizen | 0:774ff1e8b26b | 370 | _state.ok = true; |
kaizen | 0:774ff1e8b26b | 371 | } |
kaizen | 0:774ff1e8b26b | 372 | } |
kaizen | 0:774ff1e8b26b | 373 | |
kaizen | 0:774ff1e8b26b | 374 | void WizFi310::resConnect (const char *buf) |
kaizen | 0:774ff1e8b26b | 375 | { |
kaizen | 0:774ff1e8b26b | 376 | int cid; |
kaizen | 0:774ff1e8b26b | 377 | |
kaizen | 0:774ff1e8b26b | 378 | if (buf[0] == '[' && buf[1] == 'O' && buf[2] == 'K' && buf[3] == ']') |
kaizen | 0:774ff1e8b26b | 379 | { |
kaizen | 0:774ff1e8b26b | 380 | _state.n++; |
kaizen | 0:774ff1e8b26b | 381 | } |
kaizen | 0:774ff1e8b26b | 382 | else if( buf[0] == '[' && buf[1] == 'C' && buf[2] == 'O' && buf[3] == 'N' && |
kaizen | 0:774ff1e8b26b | 383 | buf[4] == 'N' && buf[5] == 'E' && buf[6] == 'C' && buf[7] == 'T') |
kaizen | 0:774ff1e8b26b | 384 | { |
kaizen | 0:774ff1e8b26b | 385 | cid = x2i(buf[9]); |
kaizen | 0:774ff1e8b26b | 386 | _state.cid = cid; |
kaizen | 0:774ff1e8b26b | 387 | _state.n++; |
kaizen | 0:774ff1e8b26b | 388 | } |
kaizen | 0:774ff1e8b26b | 389 | |
kaizen | 0:774ff1e8b26b | 390 | if(_state.n >= 2) |
kaizen | 0:774ff1e8b26b | 391 | { |
kaizen | 0:774ff1e8b26b | 392 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 393 | _state.ok = true; |
kaizen | 0:774ff1e8b26b | 394 | } |
kaizen | 0:774ff1e8b26b | 395 | } |
kaizen | 0:774ff1e8b26b | 396 | |
kaizen | 0:774ff1e8b26b | 397 | void WizFi310::resSSEND (const char *buf) |
kaizen | 0:774ff1e8b26b | 398 | { |
kaizen | 0:774ff1e8b26b | 399 | if(_state.cid != -1) |
kaizen | 0:774ff1e8b26b | 400 | { |
kaizen | 0:774ff1e8b26b | 401 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 402 | _state.ok = true; |
kaizen | 0:774ff1e8b26b | 403 | } |
kaizen | 0:774ff1e8b26b | 404 | } |
kaizen | 0:774ff1e8b26b | 405 | |
kaizen | 0:774ff1e8b26b | 406 | void WizFi310::resFDNS (const char *buf) |
kaizen | 0:774ff1e8b26b | 407 | { |
kaizen | 0:774ff1e8b26b | 408 | int i; |
kaizen | 0:774ff1e8b26b | 409 | |
kaizen | 0:774ff1e8b26b | 410 | for(i=0; i<strlen(buf); i++) |
kaizen | 0:774ff1e8b26b | 411 | { |
kaizen | 0:774ff1e8b26b | 412 | if( (buf[i] < '0' || buf[i] > '9') && buf[i] != '.' ) |
kaizen | 0:774ff1e8b26b | 413 | { |
kaizen | 0:774ff1e8b26b | 414 | return; |
kaizen | 0:774ff1e8b26b | 415 | } |
kaizen | 0:774ff1e8b26b | 416 | } |
kaizen | 0:774ff1e8b26b | 417 | |
kaizen | 0:774ff1e8b26b | 418 | strncpy(_state.resolv, buf, sizeof(_state.resolv)); |
kaizen | 0:774ff1e8b26b | 419 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 420 | } |
kaizen | 0:774ff1e8b26b | 421 | |
kaizen | 0:774ff1e8b26b | 422 | void WizFi310::resSMGMT (const char *buf) |
kaizen | 0:774ff1e8b26b | 423 | { |
kaizen | 0:774ff1e8b26b | 424 | int cid, i; |
kaizen | 0:774ff1e8b26b | 425 | char *c; |
kaizen | 0:774ff1e8b26b | 426 | |
kaizen | 0:774ff1e8b26b | 427 | if( (buf[0] < '0' || buf[0] > '8') ) return; |
kaizen | 0:774ff1e8b26b | 428 | |
kaizen | 0:774ff1e8b26b | 429 | cid = x2i(buf[0]); |
kaizen | 0:774ff1e8b26b | 430 | if( cid != _state.cid ) return; |
kaizen | 0:774ff1e8b26b | 431 | |
kaizen | 0:774ff1e8b26b | 432 | // IP |
kaizen | 0:774ff1e8b26b | 433 | c = (char*)(buf+6); |
kaizen | 0:774ff1e8b26b | 434 | for( i=0; i<16; i++ ) |
kaizen | 0:774ff1e8b26b | 435 | { |
kaizen | 0:774ff1e8b26b | 436 | if( *(c+i) == ':') |
kaizen | 0:774ff1e8b26b | 437 | { |
kaizen | 0:774ff1e8b26b | 438 | _con[cid].ip[i] = '\0'; |
kaizen | 0:774ff1e8b26b | 439 | i++; |
kaizen | 0:774ff1e8b26b | 440 | break; |
kaizen | 0:774ff1e8b26b | 441 | } |
kaizen | 0:774ff1e8b26b | 442 | if( ( *(c+i) < '0' || *(c+i) > '9') && *(c+i) != '.' ) return; |
kaizen | 0:774ff1e8b26b | 443 | _con[cid].ip[i] = *(c+i); |
kaizen | 0:774ff1e8b26b | 444 | } |
kaizen | 0:774ff1e8b26b | 445 | |
kaizen | 0:774ff1e8b26b | 446 | // Port |
kaizen | 0:774ff1e8b26b | 447 | c = (c+i); |
kaizen | 0:774ff1e8b26b | 448 | _con[cid].port = 0; |
kaizen | 0:774ff1e8b26b | 449 | for( i=0; i<5; i++ ) |
kaizen | 0:774ff1e8b26b | 450 | { |
kaizen | 0:774ff1e8b26b | 451 | if( *(c+i) == '/') break; |
kaizen | 0:774ff1e8b26b | 452 | if( *(c+i) < '0' || *(c+i) > '9' ) return; |
kaizen | 0:774ff1e8b26b | 453 | |
kaizen | 0:774ff1e8b26b | 454 | _con[cid].port = (_con[cid].port * 10) + ( *(c+i) - '0' ); |
kaizen | 0:774ff1e8b26b | 455 | } |
kaizen | 0:774ff1e8b26b | 456 | |
kaizen | 0:774ff1e8b26b | 457 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 458 | } |
kaizen | 0:774ff1e8b26b | 459 | |
kaizen | 0:774ff1e8b26b | 460 | void WizFi310::resWSTATUS (const char *buf) |
kaizen | 0:774ff1e8b26b | 461 | { |
kaizen | 0:774ff1e8b26b | 462 | int idx=0,sep_cnt=0; |
kaizen | 0:774ff1e8b26b | 463 | int ip_idx=0,gw_idx=0; |
kaizen | 0:774ff1e8b26b | 464 | |
kaizen | 0:774ff1e8b26b | 465 | if(_state.n == 0) |
kaizen | 0:774ff1e8b26b | 466 | { |
kaizen | 0:774ff1e8b26b | 467 | _state.n++; |
kaizen | 0:774ff1e8b26b | 468 | } |
kaizen | 0:774ff1e8b26b | 469 | else if(_state.n == 1) |
kaizen | 0:774ff1e8b26b | 470 | { |
kaizen | 0:774ff1e8b26b | 471 | for(idx=0;buf[idx]!='\r';idx++) |
kaizen | 0:774ff1e8b26b | 472 | { |
kaizen | 0:774ff1e8b26b | 473 | if(buf[idx] =='/') |
kaizen | 0:774ff1e8b26b | 474 | { |
kaizen | 0:774ff1e8b26b | 475 | sep_cnt++; |
kaizen | 0:774ff1e8b26b | 476 | continue; |
kaizen | 0:774ff1e8b26b | 477 | } |
kaizen | 0:774ff1e8b26b | 478 | |
kaizen | 0:774ff1e8b26b | 479 | if( sep_cnt == 2) // IP Address |
kaizen | 0:774ff1e8b26b | 480 | { |
kaizen | 0:774ff1e8b26b | 481 | _state.ip[ip_idx++] = buf[idx]; |
kaizen | 0:774ff1e8b26b | 482 | } |
kaizen | 0:774ff1e8b26b | 483 | else if(sep_cnt == 3) |
kaizen | 0:774ff1e8b26b | 484 | { |
kaizen | 0:774ff1e8b26b | 485 | _state.gateway[gw_idx++] = buf[idx]; |
kaizen | 0:774ff1e8b26b | 486 | } |
kaizen | 0:774ff1e8b26b | 487 | } |
kaizen | 0:774ff1e8b26b | 488 | _state.ip[ip_idx] = '\0'; |
kaizen | 0:774ff1e8b26b | 489 | _state.gateway[gw_idx] = '\0'; |
kaizen | 0:774ff1e8b26b | 490 | _state.res = RES_NULL; |
kaizen | 0:774ff1e8b26b | 491 | } |
kaizen | 0:774ff1e8b26b | 492 | } |