wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1 /* SPWFSA04 Device
JMF 0:24d3eb812fd4 2 * Copyright (c) 2015 ARM Limited
JMF 0:24d3eb812fd4 3 *
JMF 0:24d3eb812fd4 4 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:24d3eb812fd4 5 * you may not use this file except in compliance with the License.
JMF 0:24d3eb812fd4 6 * You may obtain a copy of the License at
JMF 0:24d3eb812fd4 7 *
JMF 0:24d3eb812fd4 8 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:24d3eb812fd4 9 *
JMF 0:24d3eb812fd4 10 * Unless required by applicable law or agreed to in writing, software
JMF 0:24d3eb812fd4 11 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:24d3eb812fd4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:24d3eb812fd4 13 * See the License for the specific language governing permissions and
JMF 0:24d3eb812fd4 14 * limitations under the License.
JMF 0:24d3eb812fd4 15 */
JMF 0:24d3eb812fd4 16
JMF 0:24d3eb812fd4 17 #include "SPWFSA04.h"
JMF 0:24d3eb812fd4 18 #include "SpwfSAInterface.h"
JMF 0:24d3eb812fd4 19 #include "mbed_debug.h"
JMF 0:24d3eb812fd4 20
JMF 0:24d3eb812fd4 21 #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1
JMF 0:24d3eb812fd4 22
JMF 0:24d3eb812fd4 23 SPWFSA04::SPWFSA04(PinName tx, PinName rx,
JMF 0:24d3eb812fd4 24 PinName rts, PinName cts,
JMF 0:24d3eb812fd4 25 SpwfSAInterface &ifce, bool debug,
JMF 0:24d3eb812fd4 26 PinName wakeup, PinName reset)
JMF 0:24d3eb812fd4 27 : SPWFSAxx(tx, rx, rts, cts, ifce, debug, wakeup, reset) {
JMF 0:24d3eb812fd4 28 }
JMF 0:24d3eb812fd4 29
JMF 0:24d3eb812fd4 30 bool SPWFSA04::open(const char *type, int* spwf_id, const char* addr, int port)
JMF 0:24d3eb812fd4 31 {
JMF 0:24d3eb812fd4 32 int socket_id;
JMF 0:24d3eb812fd4 33 int value;
JMF 0:24d3eb812fd4 34 int trials;
JMF 0:24d3eb812fd4 35
JMF 0:24d3eb812fd4 36 if(!_parser.send("AT+S.SOCKON=%s,%d,NULL,%s", addr, port, type))
JMF 0:24d3eb812fd4 37 {
JMF 0:24d3eb812fd4 38 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 39 return false;
JMF 0:24d3eb812fd4 40 }
JMF 0:24d3eb812fd4 41
JMF 0:24d3eb812fd4 42 /* handle both response possibilities here before returning
JMF 0:24d3eb812fd4 43 * otherwise module seems to remain in inconsistent state.
JMF 0:24d3eb812fd4 44 */
JMF 0:24d3eb812fd4 45
JMF 0:24d3eb812fd4 46 if(!_parser.recv("AT-S.")) { // get prefix
JMF 0:24d3eb812fd4 47 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 48 empty_rx_buffer();
JMF 0:24d3eb812fd4 49 return false;
JMF 0:24d3eb812fd4 50 }
JMF 0:24d3eb812fd4 51
JMF 0:24d3eb812fd4 52 /* wait for next character */
JMF 0:24d3eb812fd4 53 trials = 0;
JMF 0:24d3eb812fd4 54 while((value = _parser.getc()) < 0) {
JMF 0:24d3eb812fd4 55 if(++trials >= SPWFXX_MAX_TRIALS) {
JMF 0:24d3eb812fd4 56 debug("\r\nSPWF> `SPWFSA04::open`: error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 57 empty_rx_buffer();
JMF 0:24d3eb812fd4 58 return false;
JMF 0:24d3eb812fd4 59 }
JMF 0:24d3eb812fd4 60 }
JMF 0:24d3eb812fd4 61
JMF 0:24d3eb812fd4 62 switch(value) {
JMF 0:24d3eb812fd4 63 case 'O':
JMF 0:24d3eb812fd4 64 /* get next character */
JMF 0:24d3eb812fd4 65 value = _parser.getc();
JMF 0:24d3eb812fd4 66 if(value != 'n') {
JMF 0:24d3eb812fd4 67 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d) (%d, \'%c\')\r\n",
JMF 0:24d3eb812fd4 68 __LINE__, value, value);
JMF 0:24d3eb812fd4 69 empty_rx_buffer();
JMF 0:24d3eb812fd4 70 return false;
JMF 0:24d3eb812fd4 71 }
JMF 0:24d3eb812fd4 72
JMF 0:24d3eb812fd4 73 /* get socket id */
JMF 0:24d3eb812fd4 74 if(!(_parser.recv(":%*u.%*u.%*u.%*u:%d\n", &socket_id)
JMF 0:24d3eb812fd4 75 && _recv_delim_lf()
JMF 0:24d3eb812fd4 76 && _recv_ok())) {
JMF 0:24d3eb812fd4 77 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 78 empty_rx_buffer();
JMF 0:24d3eb812fd4 79 return false;
JMF 0:24d3eb812fd4 80 }
JMF 0:24d3eb812fd4 81 debug_if(_dbg_on, "AT^ AT-S.On:%s:%d\r\n", addr, socket_id);
JMF 0:24d3eb812fd4 82
JMF 0:24d3eb812fd4 83 *spwf_id = socket_id;
JMF 0:24d3eb812fd4 84 return true;
JMF 0:24d3eb812fd4 85 case 'E':
JMF 0:24d3eb812fd4 86 int err_nr;
JMF 0:24d3eb812fd4 87 if(_parser.recv("RROR:%d:%255[^\n]\n", &err_nr, _msg_buffer) && _recv_delim_lf()) {
JMF 0:24d3eb812fd4 88 debug_if(_dbg_on, "AT^ AT-S.ERROR:%d:%s (%d)\r\n", err_nr, _msg_buffer, __LINE__);
JMF 0:24d3eb812fd4 89 } else {
JMF 0:24d3eb812fd4 90 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 91 empty_rx_buffer();
JMF 0:24d3eb812fd4 92 }
JMF 0:24d3eb812fd4 93 break;
JMF 0:24d3eb812fd4 94 default:
JMF 0:24d3eb812fd4 95 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA04::open`: error opening socket (%d) (%d, \'%c\')\r\n",
JMF 0:24d3eb812fd4 96 __LINE__, value, value);
JMF 0:24d3eb812fd4 97 empty_rx_buffer();
JMF 0:24d3eb812fd4 98 break;
JMF 0:24d3eb812fd4 99 }
JMF 0:24d3eb812fd4 100
JMF 0:24d3eb812fd4 101 return false;
JMF 0:24d3eb812fd4 102 }
JMF 0:24d3eb812fd4 103
JMF 0:24d3eb812fd4 104 int SPWFSA04::_read_in(char* buffer, int spwf_id, uint32_t amount) {
JMF 0:24d3eb812fd4 105 int ret = -1;
JMF 0:24d3eb812fd4 106 int received, cumulative;
JMF 0:24d3eb812fd4 107
JMF 0:24d3eb812fd4 108 MBED_ASSERT(buffer != NULL);
JMF 0:24d3eb812fd4 109
JMF 0:24d3eb812fd4 110 /* block asynchronous indications */
JMF 0:24d3eb812fd4 111 if(!_winds_off()) {
JMF 0:24d3eb812fd4 112 return -1;
JMF 0:24d3eb812fd4 113 }
JMF 0:24d3eb812fd4 114
JMF 0:24d3eb812fd4 115 /* read in data */
JMF 0:24d3eb812fd4 116 if(_parser.send("AT+S.SOCKR=%d,%d", spwf_id, (unsigned int)amount)) {
JMF 0:24d3eb812fd4 117 if(!(_parser.recv("AT-S.Reading:%d:%d\n", &received, &cumulative) &&
JMF 0:24d3eb812fd4 118 _recv_delim_lf())) {
JMF 0:24d3eb812fd4 119 debug_if(_dbg_on, "\r\nSPWF> failed to receive AT-S.Reading (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 120 empty_rx_buffer();
JMF 0:24d3eb812fd4 121 } else {
JMF 0:24d3eb812fd4 122 /* set high timeout */
JMF 0:24d3eb812fd4 123 _parser.set_timeout(SPWF_READ_BIN_TIMEOUT);
JMF 0:24d3eb812fd4 124 /* read in binary data */
JMF 0:24d3eb812fd4 125 int read = _parser.read(buffer, amount);
JMF 0:24d3eb812fd4 126 /* reset timeout value */
JMF 0:24d3eb812fd4 127 _parser.set_timeout(_timeout);
JMF 0:24d3eb812fd4 128 if(read > 0) {
JMF 0:24d3eb812fd4 129 if(_recv_ok()) {
JMF 0:24d3eb812fd4 130 ret = amount;
JMF 0:24d3eb812fd4 131
JMF 0:24d3eb812fd4 132 /* remove from pending sizes
JMF 0:24d3eb812fd4 133 * (MUST be done before next async indications handling (e.g. `_winds_on()`)) */
JMF 0:24d3eb812fd4 134 _remove_pending_pkt_size(spwf_id, amount);
JMF 0:24d3eb812fd4 135 } else {
JMF 0:24d3eb812fd4 136 debug_if(_dbg_on, "\r\nSPWF> failed to receive OK (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 137 empty_rx_buffer();
JMF 0:24d3eb812fd4 138 }
JMF 0:24d3eb812fd4 139 } else {
JMF 0:24d3eb812fd4 140 debug_if(_dbg_on, "\r\nSPWF> failed to read binary data (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 141 empty_rx_buffer();
JMF 0:24d3eb812fd4 142 }
JMF 0:24d3eb812fd4 143 }
JMF 0:24d3eb812fd4 144 } else {
JMF 0:24d3eb812fd4 145 debug_if(_dbg_on, "%s(%d): failed to send SOCKR\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 146 }
JMF 0:24d3eb812fd4 147
JMF 0:24d3eb812fd4 148 debug_if(_dbg_on, "\r\nSPWF> %s():\t%d:%d\r\n", __func__, spwf_id, amount);
JMF 0:24d3eb812fd4 149
JMF 0:24d3eb812fd4 150 /* unblock asynchronous indications */
JMF 0:24d3eb812fd4 151 _winds_on();
JMF 0:24d3eb812fd4 152
JMF 0:24d3eb812fd4 153 return ret;
JMF 0:24d3eb812fd4 154 }
JMF 0:24d3eb812fd4 155
JMF 0:24d3eb812fd4 156 /* betzw - TODO: improve performance! */
JMF 0:24d3eb812fd4 157 bool SPWFSA04::_recv_ap(nsapi_wifi_ap_t *ap)
JMF 0:24d3eb812fd4 158 {
JMF 0:24d3eb812fd4 159 bool ret;
JMF 0:24d3eb812fd4 160 int curr;
JMF 0:24d3eb812fd4 161 unsigned int channel;
JMF 0:24d3eb812fd4 162 int trials;
JMF 0:24d3eb812fd4 163
JMF 0:24d3eb812fd4 164 ap->security = NSAPI_SECURITY_UNKNOWN;
JMF 0:24d3eb812fd4 165
JMF 0:24d3eb812fd4 166 /* determine list end */
JMF 0:24d3eb812fd4 167 curr = _parser.getc();
JMF 0:24d3eb812fd4 168 if(curr == 'A') { // assume end of list ("AT-S.OK")
JMF 0:24d3eb812fd4 169 if(!(_parser.recv("T-S.OK\n") && _recv_delim_lf())) {
JMF 0:24d3eb812fd4 170 empty_rx_buffer();
JMF 0:24d3eb812fd4 171 }
JMF 0:24d3eb812fd4 172 return false;
JMF 0:24d3eb812fd4 173 }
JMF 0:24d3eb812fd4 174
JMF 0:24d3eb812fd4 175 /* run to 'horizontal tab' */
JMF 0:24d3eb812fd4 176 trials = 0;
JMF 0:24d3eb812fd4 177 while(_parser.getc() != '\x09') {
JMF 0:24d3eb812fd4 178 if(trials++ > SPWFXX_MAX_TRIALS) {
JMF 0:24d3eb812fd4 179 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 180 empty_rx_buffer();
JMF 0:24d3eb812fd4 181 return false;
JMF 0:24d3eb812fd4 182 }
JMF 0:24d3eb812fd4 183 }
JMF 0:24d3eb812fd4 184
JMF 0:24d3eb812fd4 185 /* read in next line */
JMF 0:24d3eb812fd4 186 ret = _parser.recv("%255[^\n]\n", _msg_buffer) && _recv_delim_lf();
JMF 0:24d3eb812fd4 187
JMF 0:24d3eb812fd4 188 /* parse line - first phase */
JMF 0:24d3eb812fd4 189 if(ret) {
JMF 0:24d3eb812fd4 190 int val = sscanf(_msg_buffer,
JMF 0:24d3eb812fd4 191 " %*s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx CHAN: %u RSSI: %hhd SSID: \'%*255[^\']\'",
JMF 0:24d3eb812fd4 192 &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
JMF 0:24d3eb812fd4 193 &channel, &ap->rssi);
JMF 0:24d3eb812fd4 194 if(val < 8) {
JMF 0:24d3eb812fd4 195 ret = false;
JMF 0:24d3eb812fd4 196 }
JMF 0:24d3eb812fd4 197 }
JMF 0:24d3eb812fd4 198
JMF 0:24d3eb812fd4 199 /* parse line - second phase */
JMF 0:24d3eb812fd4 200 if(ret) { // ret == true
JMF 0:24d3eb812fd4 201 char value;
JMF 0:24d3eb812fd4 202 char *rest, *first, *last;
JMF 0:24d3eb812fd4 203
JMF 0:24d3eb812fd4 204 /* decide about position of `CAPS:` */
JMF 0:24d3eb812fd4 205 first = strchr(_msg_buffer, '\'');
JMF 0:24d3eb812fd4 206 if(first == NULL) {
JMF 0:24d3eb812fd4 207 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 208 empty_rx_buffer();
JMF 0:24d3eb812fd4 209 return false;
JMF 0:24d3eb812fd4 210 }
JMF 0:24d3eb812fd4 211 last = strrchr(_msg_buffer, '\'');
JMF 0:24d3eb812fd4 212 if((last == NULL) || (last < (first+1))) {
JMF 0:24d3eb812fd4 213 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 214 empty_rx_buffer();
JMF 0:24d3eb812fd4 215 return false;
JMF 0:24d3eb812fd4 216 }
JMF 0:24d3eb812fd4 217 rest = strstr(last, "CAPS:");
JMF 0:24d3eb812fd4 218 if(rest == NULL) {
JMF 0:24d3eb812fd4 219 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 220 empty_rx_buffer();
JMF 0:24d3eb812fd4 221 return false;
JMF 0:24d3eb812fd4 222 }
JMF 0:24d3eb812fd4 223
JMF 0:24d3eb812fd4 224 /* substitute '\'' with '\0' */
JMF 0:24d3eb812fd4 225 *last = '\0';
JMF 0:24d3eb812fd4 226
JMF 0:24d3eb812fd4 227 /* copy values */
JMF 0:24d3eb812fd4 228 memcpy(&ap->ssid, first+1, sizeof(ap->ssid)-1);
JMF 0:24d3eb812fd4 229 ap->ssid[sizeof(ap->ssid)-1] = '\0';
JMF 0:24d3eb812fd4 230 ap->channel = channel;
JMF 0:24d3eb812fd4 231
JMF 0:24d3eb812fd4 232 /* skip `CAPS: 0421 ` */
JMF 0:24d3eb812fd4 233 if(strlen(rest) < 11) {
JMF 0:24d3eb812fd4 234 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 235 empty_rx_buffer();
JMF 0:24d3eb812fd4 236 return false;
JMF 0:24d3eb812fd4 237 }
JMF 0:24d3eb812fd4 238 rest += 11;
JMF 0:24d3eb812fd4 239
JMF 0:24d3eb812fd4 240 /* get next character */
JMF 0:24d3eb812fd4 241 value = *rest++;
JMF 0:24d3eb812fd4 242 if(value != 'W') { // no security
JMF 0:24d3eb812fd4 243 ap->security = NSAPI_SECURITY_NONE;
JMF 0:24d3eb812fd4 244 return true;
JMF 0:24d3eb812fd4 245 }
JMF 0:24d3eb812fd4 246
JMF 0:24d3eb812fd4 247 /* determine security */
JMF 0:24d3eb812fd4 248 {
JMF 0:24d3eb812fd4 249 char buffer[10];
JMF 0:24d3eb812fd4 250
JMF 0:24d3eb812fd4 251 if(!(sscanf(rest, "%s%*[\x20]", (char*)&buffer) > 0)) { // '\0x20' == <space>
JMF 0:24d3eb812fd4 252 return true;
JMF 0:24d3eb812fd4 253 } else if(strncmp("EP", buffer, 10) == 0) {
JMF 0:24d3eb812fd4 254 ap->security = NSAPI_SECURITY_WEP;
JMF 0:24d3eb812fd4 255 return true;
JMF 0:24d3eb812fd4 256 } else if(strncmp("PA2", buffer, 10) == 0) {
JMF 0:24d3eb812fd4 257 ap->security = NSAPI_SECURITY_WPA2;
JMF 0:24d3eb812fd4 258 return true;
JMF 0:24d3eb812fd4 259 } else if(strncmp("PA", buffer, 10) != 0) {
JMF 0:24d3eb812fd4 260 return true;
JMF 0:24d3eb812fd4 261 }
JMF 0:24d3eb812fd4 262
JMF 0:24d3eb812fd4 263 /* got a "WPA", check for "WPA2" */
JMF 0:24d3eb812fd4 264 rest += strlen(buffer);
JMF 0:24d3eb812fd4 265 value = *rest++;
JMF 0:24d3eb812fd4 266 if(value == '\0') { // no further protocol
JMF 0:24d3eb812fd4 267 ap->security = NSAPI_SECURITY_WPA;
JMF 0:24d3eb812fd4 268 return true;
JMF 0:24d3eb812fd4 269 } else { // assume "WPA2"
JMF 0:24d3eb812fd4 270 ap->security = NSAPI_SECURITY_WPA_WPA2;
JMF 0:24d3eb812fd4 271 return true;
JMF 0:24d3eb812fd4 272 }
JMF 0:24d3eb812fd4 273 }
JMF 0:24d3eb812fd4 274 } else { // ret == false
JMF 0:24d3eb812fd4 275 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 276 empty_rx_buffer();
JMF 0:24d3eb812fd4 277 }
JMF 0:24d3eb812fd4 278
JMF 0:24d3eb812fd4 279 return ret;
JMF 0:24d3eb812fd4 280 }
JMF 0:24d3eb812fd4 281
JMF 0:24d3eb812fd4 282 nsapi_size_or_error_t SPWFSA04::scan(WiFiAccessPoint *res, unsigned limit)
JMF 0:24d3eb812fd4 283 {
JMF 0:24d3eb812fd4 284 unsigned int cnt = 0, found;
JMF 0:24d3eb812fd4 285 nsapi_wifi_ap_t ap;
JMF 0:24d3eb812fd4 286
JMF 0:24d3eb812fd4 287 if (!_parser.send("AT+S.SCAN=s,")) {
JMF 0:24d3eb812fd4 288 return NSAPI_ERROR_DEVICE_ERROR;
JMF 0:24d3eb812fd4 289 }
JMF 0:24d3eb812fd4 290
JMF 0:24d3eb812fd4 291 if(!(_parser.recv("AT-S.Parsing Networks:%u\n", &found) && _recv_delim_lf())) {
JMF 0:24d3eb812fd4 292 debug_if(_dbg_on, "SPWF> error start network scanning\r\n");
JMF 0:24d3eb812fd4 293 empty_rx_buffer();
JMF 0:24d3eb812fd4 294 return NSAPI_ERROR_DEVICE_ERROR;
JMF 0:24d3eb812fd4 295 }
JMF 0:24d3eb812fd4 296
JMF 0:24d3eb812fd4 297 debug_if(_dbg_on, "AT^ AT-S.Parsing Networks:%u\r\n", found);
JMF 0:24d3eb812fd4 298
JMF 0:24d3eb812fd4 299 if(found > 0) {
JMF 0:24d3eb812fd4 300 while (_recv_ap(&ap)) {
JMF 0:24d3eb812fd4 301 if (cnt < limit) {
JMF 0:24d3eb812fd4 302 res[cnt] = WiFiAccessPoint(ap);
JMF 0:24d3eb812fd4 303 }
JMF 0:24d3eb812fd4 304
JMF 0:24d3eb812fd4 305 if (!((limit != 0) && ((cnt + 1) > limit))) {
JMF 0:24d3eb812fd4 306 cnt++;
JMF 0:24d3eb812fd4 307 }
JMF 0:24d3eb812fd4 308 }
JMF 0:24d3eb812fd4 309 } else {
JMF 0:24d3eb812fd4 310 if(!_recv_ok()) {
JMF 0:24d3eb812fd4 311 empty_rx_buffer();
JMF 0:24d3eb812fd4 312 }
JMF 0:24d3eb812fd4 313 }
JMF 0:24d3eb812fd4 314
JMF 0:24d3eb812fd4 315 return cnt;
JMF 0:24d3eb812fd4 316 }
JMF 0:24d3eb812fd4 317
JMF 0:24d3eb812fd4 318 #endif // MBED_CONF_IDW0XX1_EXPANSION_BOARD