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 /* SPWFSA01 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 "SPWFSA01.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 == IDW01M1
JMF 0:24d3eb812fd4 22
JMF 0:24d3eb812fd4 23 SPWFSA01::SPWFSA01(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 SPWFSA01::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,%s,ind", addr, port, type))
JMF 0:24d3eb812fd4 37 {
JMF 0:24d3eb812fd4 38 debug_if(_dbg_on, "\r\nSPWF> `SPWFSA01::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 /* wait for first character */
JMF 0:24d3eb812fd4 47 trials = 0;
JMF 0:24d3eb812fd4 48 while((value = _parser.getc()) < 0) {
JMF 0:24d3eb812fd4 49 if(trials++ > SPWFXX_MAX_TRIALS) {
JMF 0:24d3eb812fd4 50 debug("\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 51 empty_rx_buffer();
JMF 0:24d3eb812fd4 52 return false;
JMF 0:24d3eb812fd4 53 }
JMF 0:24d3eb812fd4 54 }
JMF 0:24d3eb812fd4 55
JMF 0:24d3eb812fd4 56 if(value != _cr_) { // Note: this is different to what the spec exactly says
JMF 0:24d3eb812fd4 57 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 58 empty_rx_buffer();
JMF 0:24d3eb812fd4 59 return false;
JMF 0:24d3eb812fd4 60 }
JMF 0:24d3eb812fd4 61
JMF 0:24d3eb812fd4 62 if(!_recv_delim_lf()) { // Note: this is different to what the spec exactly says
JMF 0:24d3eb812fd4 63 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 64 empty_rx_buffer();
JMF 0:24d3eb812fd4 65 return false;
JMF 0:24d3eb812fd4 66 }
JMF 0:24d3eb812fd4 67
JMF 0:24d3eb812fd4 68 value = _parser.getc();
JMF 0:24d3eb812fd4 69 switch(value) {
JMF 0:24d3eb812fd4 70 case ' ':
JMF 0:24d3eb812fd4 71 if(_parser.recv("ID: %d\n", &socket_id)
JMF 0:24d3eb812fd4 72 && _recv_ok()) {
JMF 0:24d3eb812fd4 73 debug_if(_dbg_on, "AT^ ID: %d\r\n", socket_id);
JMF 0:24d3eb812fd4 74
JMF 0:24d3eb812fd4 75 *spwf_id = socket_id;
JMF 0:24d3eb812fd4 76 return true;
JMF 0:24d3eb812fd4 77 } else {
JMF 0:24d3eb812fd4 78 empty_rx_buffer();
JMF 0:24d3eb812fd4 79 }
JMF 0:24d3eb812fd4 80 break;
JMF 0:24d3eb812fd4 81 case 'E':
JMF 0:24d3eb812fd4 82 if(_parser.recv("RROR: %255[^\n]\n", _msg_buffer) && _recv_delim_lf()) {
JMF 0:24d3eb812fd4 83 debug_if(_dbg_on, "AT^ ERROR: %s (%d)\r\n", _msg_buffer, __LINE__);
JMF 0:24d3eb812fd4 84 } else {
JMF 0:24d3eb812fd4 85 debug_if(_dbg_on, "\r\nSPWF> error opening socket (%d)\r\n", __LINE__);
JMF 0:24d3eb812fd4 86 empty_rx_buffer();
JMF 0:24d3eb812fd4 87 }
JMF 0:24d3eb812fd4 88 break;
JMF 0:24d3eb812fd4 89 default:
JMF 0:24d3eb812fd4 90 debug_if(_dbg_on, "\r\nSPWF> error opening socket (value=%d, %d)\r\n", value, __LINE__);
JMF 0:24d3eb812fd4 91 break;
JMF 0:24d3eb812fd4 92 }
JMF 0:24d3eb812fd4 93
JMF 0:24d3eb812fd4 94 return false;
JMF 0:24d3eb812fd4 95 }
JMF 0:24d3eb812fd4 96
JMF 0:24d3eb812fd4 97 int SPWFSA01::_read_in(char* buffer, int spwf_id, uint32_t amount) {
JMF 0:24d3eb812fd4 98 int ret = -1;
JMF 0:24d3eb812fd4 99
JMF 0:24d3eb812fd4 100 MBED_ASSERT(buffer != NULL);
JMF 0:24d3eb812fd4 101
JMF 0:24d3eb812fd4 102 /* block asynchronous indications */
JMF 0:24d3eb812fd4 103 if(!_winds_off()) {
JMF 0:24d3eb812fd4 104 return -1;
JMF 0:24d3eb812fd4 105 }
JMF 0:24d3eb812fd4 106
JMF 0:24d3eb812fd4 107 /* read in data */
JMF 0:24d3eb812fd4 108 if(_parser.send("AT+S.SOCKR=%d,%u", spwf_id, (unsigned int)amount)) {
JMF 0:24d3eb812fd4 109 /* set high timeout */
JMF 0:24d3eb812fd4 110 _parser.set_timeout(SPWF_READ_BIN_TIMEOUT);
JMF 0:24d3eb812fd4 111 /* read in binary data */
JMF 0:24d3eb812fd4 112 int read = _parser.read(buffer, amount);
JMF 0:24d3eb812fd4 113 /* reset timeout value */
JMF 0:24d3eb812fd4 114 _parser.set_timeout(_timeout);
JMF 0:24d3eb812fd4 115 if(read > 0) {
JMF 0:24d3eb812fd4 116 if(_recv_ok()) {
JMF 0:24d3eb812fd4 117 ret = amount;
JMF 0:24d3eb812fd4 118
JMF 0:24d3eb812fd4 119 /* remove from pending sizes
JMF 0:24d3eb812fd4 120 * (MUST be done before next async indications handling (e.g. `_winds_on()`)) */
JMF 0:24d3eb812fd4 121 _remove_pending_pkt_size(spwf_id, amount);
JMF 0:24d3eb812fd4 122 } else {
JMF 0:24d3eb812fd4 123 debug_if(_dbg_on, "\r\nSPWF> failed to receive OK (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 124 empty_rx_buffer();
JMF 0:24d3eb812fd4 125 }
JMF 0:24d3eb812fd4 126 } else {
JMF 0:24d3eb812fd4 127 debug_if(_dbg_on, "\r\nSPWF> failed to read binary data (%u:%d), (%s, %d)\r\n", amount, read, __func__, __LINE__);
JMF 0:24d3eb812fd4 128 empty_rx_buffer();
JMF 0:24d3eb812fd4 129 }
JMF 0:24d3eb812fd4 130 } else {
JMF 0:24d3eb812fd4 131 debug_if(_dbg_on, "\r\nSPWF> failed to send SOCKR (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 132 }
JMF 0:24d3eb812fd4 133
JMF 0:24d3eb812fd4 134 debug_if(_dbg_on, "\r\nSPWF> %s():\t%d:%d\r\n", __func__, spwf_id, amount);
JMF 0:24d3eb812fd4 135
JMF 0:24d3eb812fd4 136 /* unblock asynchronous indications */
JMF 0:24d3eb812fd4 137 _winds_on();
JMF 0:24d3eb812fd4 138
JMF 0:24d3eb812fd4 139 return ret;
JMF 0:24d3eb812fd4 140 }
JMF 0:24d3eb812fd4 141
JMF 0:24d3eb812fd4 142 /* betzw - TODO: improve performance! */
JMF 0:24d3eb812fd4 143 bool SPWFSA01::_recv_ap(nsapi_wifi_ap_t *ap)
JMF 0:24d3eb812fd4 144 {
JMF 0:24d3eb812fd4 145 bool ret;
JMF 0:24d3eb812fd4 146 unsigned int channel;
JMF 0:24d3eb812fd4 147 int trials;
JMF 0:24d3eb812fd4 148
JMF 0:24d3eb812fd4 149 ap->security = NSAPI_SECURITY_UNKNOWN;
JMF 0:24d3eb812fd4 150
JMF 0:24d3eb812fd4 151 /* check for end of list */
JMF 0:24d3eb812fd4 152 if(_recv_delim_cr_lf()) {
JMF 0:24d3eb812fd4 153 return false;
JMF 0:24d3eb812fd4 154 }
JMF 0:24d3eb812fd4 155
JMF 0:24d3eb812fd4 156 /* run to 'horizontal tab' */
JMF 0:24d3eb812fd4 157 trials = 0;
JMF 0:24d3eb812fd4 158 while(_parser.getc() != '\x09') {
JMF 0:24d3eb812fd4 159 if(trials++ > SPWFXX_MAX_TRIALS) {
JMF 0:24d3eb812fd4 160 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 161 return false;
JMF 0:24d3eb812fd4 162 }
JMF 0:24d3eb812fd4 163 }
JMF 0:24d3eb812fd4 164
JMF 0:24d3eb812fd4 165 /* read in next line */
JMF 0:24d3eb812fd4 166 ret = _parser.recv("%255[^\n]\n", _msg_buffer) && _recv_delim_lf();
JMF 0:24d3eb812fd4 167
JMF 0:24d3eb812fd4 168 /* parse line - first phase */
JMF 0:24d3eb812fd4 169 if(ret) {
JMF 0:24d3eb812fd4 170 int val = sscanf(_msg_buffer,
JMF 0:24d3eb812fd4 171 " %*s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx CHAN: %u RSSI: %hhd SSID: \'%*255[^\']\'",
JMF 0:24d3eb812fd4 172 &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
JMF 0:24d3eb812fd4 173 &channel, &ap->rssi);
JMF 0:24d3eb812fd4 174 if(val < 8) {
JMF 0:24d3eb812fd4 175 ret = false;
JMF 0:24d3eb812fd4 176 }
JMF 0:24d3eb812fd4 177 }
JMF 0:24d3eb812fd4 178
JMF 0:24d3eb812fd4 179 /* parse line - second phase */
JMF 0:24d3eb812fd4 180 if(ret) { // ret == true
JMF 0:24d3eb812fd4 181 char value;
JMF 0:24d3eb812fd4 182 char *rest, *first, *last;
JMF 0:24d3eb812fd4 183
JMF 0:24d3eb812fd4 184 /* decide about position of `CAPS:` */
JMF 0:24d3eb812fd4 185 first = strchr(_msg_buffer, '\'');
JMF 0:24d3eb812fd4 186 if(first == NULL) {
JMF 0:24d3eb812fd4 187 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 188 return false;
JMF 0:24d3eb812fd4 189 }
JMF 0:24d3eb812fd4 190 last = strrchr(_msg_buffer, '\'');
JMF 0:24d3eb812fd4 191 if((last == NULL) || (last < (first+1))) {
JMF 0:24d3eb812fd4 192 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 193 return false;
JMF 0:24d3eb812fd4 194 }
JMF 0:24d3eb812fd4 195 rest = strstr(last, "CAPS:");
JMF 0:24d3eb812fd4 196 if(rest == NULL) {
JMF 0:24d3eb812fd4 197 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 198 return false;
JMF 0:24d3eb812fd4 199 }
JMF 0:24d3eb812fd4 200
JMF 0:24d3eb812fd4 201 /* substitute '\'' with '\0' */
JMF 0:24d3eb812fd4 202 *last = '\0';
JMF 0:24d3eb812fd4 203
JMF 0:24d3eb812fd4 204 /* copy values */
JMF 0:24d3eb812fd4 205 memcpy(&ap->ssid, first+1, sizeof(ap->ssid)-1);
JMF 0:24d3eb812fd4 206 ap->ssid[sizeof(ap->ssid)-1] = '\0';
JMF 0:24d3eb812fd4 207 ap->channel = channel;
JMF 0:24d3eb812fd4 208
JMF 0:24d3eb812fd4 209 /* skip `CAPS: 0421 ` */
JMF 0:24d3eb812fd4 210 if(strlen(rest) < 11) {
JMF 0:24d3eb812fd4 211 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 212 return false;
JMF 0:24d3eb812fd4 213 }
JMF 0:24d3eb812fd4 214 rest += 11;
JMF 0:24d3eb812fd4 215
JMF 0:24d3eb812fd4 216 /* get next character */
JMF 0:24d3eb812fd4 217 value = *rest++;
JMF 0:24d3eb812fd4 218 if(value != 'W') { // no security
JMF 0:24d3eb812fd4 219 ap->security = NSAPI_SECURITY_NONE;
JMF 0:24d3eb812fd4 220 return true;
JMF 0:24d3eb812fd4 221 }
JMF 0:24d3eb812fd4 222
JMF 0:24d3eb812fd4 223 /* determine security */
JMF 0:24d3eb812fd4 224 {
JMF 0:24d3eb812fd4 225 char buffer[10];
JMF 0:24d3eb812fd4 226
JMF 0:24d3eb812fd4 227 if(!(sscanf(rest, "%s%*[\x20]", (char*)&buffer) > 0)) { // '\0x20' == <space>
JMF 0:24d3eb812fd4 228 return true;
JMF 0:24d3eb812fd4 229 } else if(strncmp("EP", buffer, 10) == 0) {
JMF 0:24d3eb812fd4 230 ap->security = NSAPI_SECURITY_WEP;
JMF 0:24d3eb812fd4 231 return true;
JMF 0:24d3eb812fd4 232 } else if(strncmp("PA2", buffer, 10) == 0) {
JMF 0:24d3eb812fd4 233 ap->security = NSAPI_SECURITY_WPA2;
JMF 0:24d3eb812fd4 234 return true;
JMF 0:24d3eb812fd4 235 } else if(strncmp("PA", buffer, 10) != 0) {
JMF 0:24d3eb812fd4 236 return true;
JMF 0:24d3eb812fd4 237 }
JMF 0:24d3eb812fd4 238
JMF 0:24d3eb812fd4 239 /* got a "WPA", check for "WPA2" */
JMF 0:24d3eb812fd4 240 rest += strlen(buffer);
JMF 0:24d3eb812fd4 241 value = *rest++;
JMF 0:24d3eb812fd4 242 if(value == '\0') { // no further protocol
JMF 0:24d3eb812fd4 243 ap->security = NSAPI_SECURITY_WPA;
JMF 0:24d3eb812fd4 244 return true;
JMF 0:24d3eb812fd4 245 } else { // assume "WPA2"
JMF 0:24d3eb812fd4 246 ap->security = NSAPI_SECURITY_WPA_WPA2;
JMF 0:24d3eb812fd4 247 return true;
JMF 0:24d3eb812fd4 248 }
JMF 0:24d3eb812fd4 249 }
JMF 0:24d3eb812fd4 250 } else { // ret == false
JMF 0:24d3eb812fd4 251 debug("\r\nSPWF> WARNING: might happen in case of RX buffer overflow! (%s, %d)\r\n", __func__, __LINE__);
JMF 0:24d3eb812fd4 252 }
JMF 0:24d3eb812fd4 253
JMF 0:24d3eb812fd4 254 return ret;
JMF 0:24d3eb812fd4 255 }
JMF 0:24d3eb812fd4 256
JMF 0:24d3eb812fd4 257 int SPWFSA01::scan(WiFiAccessPoint *res, unsigned limit)
JMF 0:24d3eb812fd4 258 {
JMF 0:24d3eb812fd4 259 unsigned cnt = 0;
JMF 0:24d3eb812fd4 260 nsapi_wifi_ap_t ap;
JMF 0:24d3eb812fd4 261
JMF 0:24d3eb812fd4 262 if (!_parser.send("AT+S.SCAN=a,s")) {
JMF 0:24d3eb812fd4 263 return NSAPI_ERROR_DEVICE_ERROR;
JMF 0:24d3eb812fd4 264 }
JMF 0:24d3eb812fd4 265
JMF 0:24d3eb812fd4 266 while (_recv_ap(&ap)) {
JMF 0:24d3eb812fd4 267 if (cnt < limit) {
JMF 0:24d3eb812fd4 268 res[cnt] = WiFiAccessPoint(ap);
JMF 0:24d3eb812fd4 269 }
JMF 0:24d3eb812fd4 270
JMF 0:24d3eb812fd4 271 cnt++;
JMF 0:24d3eb812fd4 272 if (limit != 0 && cnt >= limit) {
JMF 0:24d3eb812fd4 273 break;
JMF 0:24d3eb812fd4 274 }
JMF 0:24d3eb812fd4 275 }
JMF 0:24d3eb812fd4 276
JMF 0:24d3eb812fd4 277 if(!_recv_ok()) {
JMF 0:24d3eb812fd4 278 empty_rx_buffer();
JMF 0:24d3eb812fd4 279 }
JMF 0:24d3eb812fd4 280
JMF 0:24d3eb812fd4 281 return cnt;
JMF 0:24d3eb812fd4 282 }
JMF 0:24d3eb812fd4 283
JMF 0:24d3eb812fd4 284 #endif // MBED_CONF_IDW0XX1_EXPANSION_BOARD