wifly/socket interface for wifly modules

Dependents:   PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019

Committer:
samux
Date:
Thu Aug 23 12:59:19 2012 +0000
Revision:
7:a92dbed32890
Parent:
6:f281180726e8
Child:
11:b912f91e3212
add security enum

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 7:a92dbed32890 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 7:a92dbed32890 2 *
samux 7:a92dbed32890 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 7:a92dbed32890 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 7:a92dbed32890 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 7:a92dbed32890 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 7:a92dbed32890 7 * furnished to do so, subject to the following conditions:
samux 7:a92dbed32890 8 *
samux 7:a92dbed32890 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 7:a92dbed32890 10 * substantial portions of the Software.
samux 7:a92dbed32890 11 *
samux 7:a92dbed32890 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 7:a92dbed32890 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 7:a92dbed32890 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 7:a92dbed32890 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 7:a92dbed32890 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 7:a92dbed32890 17 */
samux 7:a92dbed32890 18
samux 7:a92dbed32890 19 #include "mbed.h"
samux 7:a92dbed32890 20 #include "Wifly.h"
samux 7:a92dbed32890 21 #include <string>
samux 7:a92dbed32890 22 #include <algorithm>
samux 7:a92dbed32890 23
samux 7:a92dbed32890 24 //Debug is disabled by default
samux 7:a92dbed32890 25 #if 0
samux 7:a92dbed32890 26 #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
samux 7:a92dbed32890 27 #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
samux 7:a92dbed32890 28 #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
samux 7:a92dbed32890 29 #else
samux 7:a92dbed32890 30 #define DBG(x, ...)
samux 7:a92dbed32890 31 #define WARN(x, ...)
samux 7:a92dbed32890 32 #define ERR(x, ...)
samux 7:a92dbed32890 33 #endif
samux 7:a92dbed32890 34
samux 7:a92dbed32890 35 #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
samux 7:a92dbed32890 36
samux 7:a92dbed32890 37 #define MAX_TRY_JOIN 3
samux 7:a92dbed32890 38
samux 7:a92dbed32890 39 Wifly * Wifly::inst;
samux 7:a92dbed32890 40
samux 7:a92dbed32890 41 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
samux 7:a92dbed32890 42 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512)
samux 7:a92dbed32890 43 {
samux 7:a92dbed32890 44 this->wpa = wpa;
samux 7:a92dbed32890 45 security = sec;
samux 7:a92dbed32890 46
samux 7:a92dbed32890 47 // change all ' ' in '$' in the ssid and the passphrase
samux 7:a92dbed32890 48 strcpy(this->ssid, ssid);
samux 7:a92dbed32890 49 for (int i = 0; i < strlen(ssid); i++) {
samux 7:a92dbed32890 50 if (this->ssid[i] == ' ')
samux 7:a92dbed32890 51 this->ssid[i] = '$';
samux 7:a92dbed32890 52 }
samux 7:a92dbed32890 53 strcpy(this->phrase, phrase);
samux 7:a92dbed32890 54 for (int i = 0; i < strlen(phrase); i++) {
samux 7:a92dbed32890 55 if (this->phrase[i] == ' ')
samux 7:a92dbed32890 56 this->phrase[i] = '$';
samux 7:a92dbed32890 57 }
samux 7:a92dbed32890 58
samux 7:a92dbed32890 59 inst = this;
samux 7:a92dbed32890 60 attach_rx(false);
samux 7:a92dbed32890 61 cmd_mode = false;
samux 7:a92dbed32890 62 }
samux 7:a92dbed32890 63
samux 7:a92dbed32890 64 bool Wifly::join()
samux 7:a92dbed32890 65 {
samux 7:a92dbed32890 66 char cmd[100];
samux 7:a92dbed32890 67
samux 7:a92dbed32890 68 for (int i= 0; i < MAX_TRY_JOIN; i++) {
samux 7:a92dbed32890 69 if (!sendCommand("set comm time 20\r", "AOK"))
samux 7:a92dbed32890 70 continue;
samux 7:a92dbed32890 71
samux 7:a92dbed32890 72 if (!sendCommand("set comm size 128\r", "AOK"))
samux 7:a92dbed32890 73 continue;
samux 7:a92dbed32890 74
samux 7:a92dbed32890 75 if (!sendCommand("set sys iofunc 0x40\r", "AOK"))
samux 7:a92dbed32890 76 continue;
samux 7:a92dbed32890 77
samux 7:a92dbed32890 78 // no string sent to the tcp client
samux 7:a92dbed32890 79 if (!sendCommand("set comm remote 0\r", "AOK"))
samux 7:a92dbed32890 80 continue;
samux 7:a92dbed32890 81
samux 7:a92dbed32890 82 // tcp protocol
samux 7:a92dbed32890 83 if (!sendCommand("set ip proto 2\r", "AOK"))
samux 7:a92dbed32890 84 continue;
samux 7:a92dbed32890 85
samux 7:a92dbed32890 86 // tcp retry
samux 7:a92dbed32890 87 if (!sendCommand("set ip flags 0x7\r", "AOK"))
samux 7:a92dbed32890 88 continue;
samux 7:a92dbed32890 89
samux 7:a92dbed32890 90 //no echo
samux 7:a92dbed32890 91 if (!sendCommand("set u m 1\r", "AOK"))
samux 7:a92dbed32890 92 continue;
samux 7:a92dbed32890 93
samux 7:a92dbed32890 94 // no auto join
samux 7:a92dbed32890 95 if (!sendCommand("set w j 0\r", "AOK"))
samux 7:a92dbed32890 96 continue;
samux 7:a92dbed32890 97
samux 7:a92dbed32890 98 //dhcp
samux 7:a92dbed32890 99 sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0);
samux 7:a92dbed32890 100 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 101 continue;
samux 7:a92dbed32890 102
samux 7:a92dbed32890 103 // ssid
samux 7:a92dbed32890 104 sprintf(cmd, "set w s %s\r", ssid);
samux 7:a92dbed32890 105 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 106 continue;
samux 7:a92dbed32890 107
samux 7:a92dbed32890 108 //auth
samux 7:a92dbed32890 109 sprintf(cmd, "set w a %d\r", security);
samux 7:a92dbed32890 110 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 111 continue;
samux 7:a92dbed32890 112
samux 7:a92dbed32890 113 // if no dhcp, set ip, netmask and gateway
samux 7:a92dbed32890 114 if (!dhcp) {
samux 7:a92dbed32890 115 DBG("not dhcp\r");
samux 7:a92dbed32890 116
samux 7:a92dbed32890 117 sprintf(cmd, "set i a %s\r\n", ip);
samux 7:a92dbed32890 118 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 119 continue;
samux 7:a92dbed32890 120
samux 7:a92dbed32890 121 sprintf(cmd, "set i n %s\r", netmask);
samux 7:a92dbed32890 122 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 123 continue;
samux 7:a92dbed32890 124
samux 7:a92dbed32890 125 sprintf(cmd, "set i g %s\r", gateway);
samux 7:a92dbed32890 126 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 127 continue;
samux 7:a92dbed32890 128 }
samux 7:a92dbed32890 129
samux 7:a92dbed32890 130 //key step
samux 7:a92dbed32890 131 if (security != NONE) {
samux 7:a92dbed32890 132 if (security == WPA)
samux 7:a92dbed32890 133 sprintf(cmd, "set w p %s\r", phrase);
samux 7:a92dbed32890 134 else if (security == WEP_128)
samux 7:a92dbed32890 135 sprintf(cmd, "set w k %s\r", phrase);
samux 7:a92dbed32890 136
samux 7:a92dbed32890 137 if (!sendCommand(cmd, "AOK"))
samux 7:a92dbed32890 138 continue;
samux 7:a92dbed32890 139 }
samux 7:a92dbed32890 140
samux 7:a92dbed32890 141 // save
samux 7:a92dbed32890 142 if (!sendCommand("save\r", "Stor"))
samux 7:a92dbed32890 143 return false;
samux 7:a92dbed32890 144
samux 7:a92dbed32890 145 //join the network
samux 7:a92dbed32890 146 sprintf(cmd, "join\r");
samux 7:a92dbed32890 147 if (!sendCommand(cmd, "Associated", NULL, 3000))
samux 7:a92dbed32890 148 continue;
samux 7:a92dbed32890 149
samux 7:a92dbed32890 150 if (dhcp) {
samux 7:a92dbed32890 151 if (!sendCommand("", "DHCP=ON", NULL, 3000))
samux 7:a92dbed32890 152 continue;
samux 7:a92dbed32890 153 }
samux 7:a92dbed32890 154
samux 7:a92dbed32890 155 exit();
samux 7:a92dbed32890 156
samux 7:a92dbed32890 157 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
samux 7:a92dbed32890 158 return true;
samux 7:a92dbed32890 159 }
samux 7:a92dbed32890 160 return false;
samux 7:a92dbed32890 161 }
samux 7:a92dbed32890 162
samux 7:a92dbed32890 163 char * Wifly::getStringSecurity() {
samux 7:a92dbed32890 164 switch(security) {
samux 7:a92dbed32890 165 case NONE:
samux 7:a92dbed32890 166 return "NONE";
samux 7:a92dbed32890 167 case WEP_128:
samux 7:a92dbed32890 168 return "WEP_128";
samux 7:a92dbed32890 169 case WPA:
samux 7:a92dbed32890 170 return "WPA";
samux 7:a92dbed32890 171 }
samux 7:a92dbed32890 172 return "UNKNOWN";
samux 7:a92dbed32890 173 }
samux 7:a92dbed32890 174
samux 7:a92dbed32890 175
samux 7:a92dbed32890 176 bool Wifly::dnsLookup(const char * host, char * ip)
samux 7:a92dbed32890 177 {
samux 7:a92dbed32890 178 string h = host;
samux 7:a92dbed32890 179 char cmd[30], rcv[100];
samux 7:a92dbed32890 180 int l = 0;
samux 7:a92dbed32890 181 char * point;
samux 7:a92dbed32890 182 int nb_digits = 0;
samux 7:a92dbed32890 183
samux 7:a92dbed32890 184 // no dns needed
samux 7:a92dbed32890 185 int pos = h.find(".");
samux 7:a92dbed32890 186 if (pos != string::npos) {
samux 7:a92dbed32890 187 string sub = h.substr(0, h.find("."));
samux 7:a92dbed32890 188 nb_digits = atoi(sub.c_str());
samux 7:a92dbed32890 189 }
samux 7:a92dbed32890 190 //printf("substrL %s\r\n", sub.c_str());
samux 7:a92dbed32890 191 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 7:a92dbed32890 192 strcpy(ip, host);
samux 7:a92dbed32890 193 }
samux 7:a92dbed32890 194 // dns needed
samux 7:a92dbed32890 195 else {
samux 7:a92dbed32890 196 nb_digits = 0;
samux 7:a92dbed32890 197 sprintf(cmd, "lookup %s\r", host);
samux 7:a92dbed32890 198 if (!sendCommand(cmd, NULL, rcv))
samux 7:a92dbed32890 199 return false;
samux 7:a92dbed32890 200
samux 7:a92dbed32890 201 // look for the ip address
samux 7:a92dbed32890 202 char * begin = strstr(rcv, "=") + 1;
samux 7:a92dbed32890 203 for (int i = 0; i < 3; i++) {
samux 7:a92dbed32890 204 point = strstr(begin + l, ".");
samux 7:a92dbed32890 205 DBG("str: %s", begin + l);
samux 7:a92dbed32890 206 l += point - (begin + l) + 1;
samux 7:a92dbed32890 207 }
samux 7:a92dbed32890 208 DBG("str: %s", begin + l);
samux 7:a92dbed32890 209 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
samux 7:a92dbed32890 210 DBG("digit: %c", *(begin + l + nb_digits));
samux 7:a92dbed32890 211 nb_digits++;
samux 7:a92dbed32890 212 }
samux 7:a92dbed32890 213 memcpy(ip, begin, l + nb_digits);
samux 7:a92dbed32890 214 ip[l+nb_digits] = 0;
samux 7:a92dbed32890 215 DBG("ip from dns: %s", ip);
samux 7:a92dbed32890 216 }
samux 7:a92dbed32890 217 return true;
samux 7:a92dbed32890 218 }
samux 7:a92dbed32890 219
samux 7:a92dbed32890 220
samux 7:a92dbed32890 221 void Wifly::flush()
samux 7:a92dbed32890 222 {
samux 7:a92dbed32890 223 buf_wifly.flush();
samux 7:a92dbed32890 224 }
samux 7:a92dbed32890 225
samux 7:a92dbed32890 226 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
samux 7:a92dbed32890 227 {
samux 7:a92dbed32890 228 if (!cmd_mode) {
samux 7:a92dbed32890 229 cmdMode();
samux 7:a92dbed32890 230 }
samux 7:a92dbed32890 231 if (send(cmd, strlen(cmd), ack, res, timeout) == -1) {
samux 7:a92dbed32890 232 ERR("sendCommand: cannot %s\r\n", cmd);
samux 7:a92dbed32890 233 exit();
samux 7:a92dbed32890 234 return false;
samux 7:a92dbed32890 235 }
samux 7:a92dbed32890 236 return true;
samux 7:a92dbed32890 237 }
samux 7:a92dbed32890 238
samux 7:a92dbed32890 239 bool Wifly::cmdMode()
samux 7:a92dbed32890 240 {
samux 7:a92dbed32890 241 if (send("$$$", 3, "CMD") == -1) {
samux 7:a92dbed32890 242 ERR("cannot enter in cmd mode\r\n");
samux 7:a92dbed32890 243 return false;
samux 7:a92dbed32890 244 }
samux 7:a92dbed32890 245 cmd_mode = true;
samux 7:a92dbed32890 246 return true;
samux 7:a92dbed32890 247 }
samux 7:a92dbed32890 248
samux 7:a92dbed32890 249 bool Wifly::leave()
samux 7:a92dbed32890 250 {
samux 7:a92dbed32890 251 if (!sendCommand("leave\r", "DeAuth"))
samux 7:a92dbed32890 252 return false;
samux 7:a92dbed32890 253 exit();
samux 7:a92dbed32890 254 return true;
samux 7:a92dbed32890 255
samux 7:a92dbed32890 256 }
samux 7:a92dbed32890 257
samux 7:a92dbed32890 258 bool Wifly::is_connected()
samux 7:a92dbed32890 259 {
samux 7:a92dbed32890 260 return (tcp_status.read() == 1) ? true : false;
samux 7:a92dbed32890 261 }
samux 7:a92dbed32890 262
samux 7:a92dbed32890 263
samux 7:a92dbed32890 264 void Wifly::reset()
samux 7:a92dbed32890 265 {
samux 7:a92dbed32890 266 reset_pin = 0;
samux 7:a92dbed32890 267 wait(0.2);
samux 7:a92dbed32890 268 reset_pin = 1;
samux 7:a92dbed32890 269 wait(0.2);
samux 7:a92dbed32890 270 }
samux 7:a92dbed32890 271
samux 7:a92dbed32890 272 bool Wifly::close()
samux 7:a92dbed32890 273 {
samux 7:a92dbed32890 274 wait(0.25);
samux 7:a92dbed32890 275 if (!cmdMode())
samux 7:a92dbed32890 276 return false;
samux 7:a92dbed32890 277 if (send("close\r", 6, "CLOS") == -1)
samux 7:a92dbed32890 278 return false;
samux 7:a92dbed32890 279 exit();
samux 7:a92dbed32890 280 return true;
samux 7:a92dbed32890 281 }
samux 7:a92dbed32890 282
samux 7:a92dbed32890 283
samux 7:a92dbed32890 284 int Wifly::putc(char c)
samux 7:a92dbed32890 285 {
samux 7:a92dbed32890 286 while (!wifi.writeable());
samux 7:a92dbed32890 287 return wifi.putc(c);
samux 7:a92dbed32890 288 }
samux 7:a92dbed32890 289
samux 7:a92dbed32890 290
samux 7:a92dbed32890 291 bool Wifly::read(char * str)
samux 7:a92dbed32890 292 {
samux 7:a92dbed32890 293 int length = buf_wifly.available();
samux 7:a92dbed32890 294 if (length == 0)
samux 7:a92dbed32890 295 return false;
samux 7:a92dbed32890 296 for (int i = 0; i < length; i++)
samux 7:a92dbed32890 297 buf_wifly.dequeue(&str[i]);
samux 7:a92dbed32890 298 str[length] = 0;
samux 7:a92dbed32890 299 return true;
samux 7:a92dbed32890 300 }
samux 7:a92dbed32890 301
samux 7:a92dbed32890 302
samux 7:a92dbed32890 303 bool Wifly::exit()
samux 7:a92dbed32890 304 {
samux 7:a92dbed32890 305 flush();
samux 7:a92dbed32890 306 if (!cmd_mode)
samux 7:a92dbed32890 307 return true;
samux 7:a92dbed32890 308 if (send("exit\r", 5, "EXIT") == -1)
samux 7:a92dbed32890 309 return false;
samux 7:a92dbed32890 310 cmd_mode = false;
samux 7:a92dbed32890 311 flush();
samux 7:a92dbed32890 312 return true;
samux 7:a92dbed32890 313 }
samux 7:a92dbed32890 314
samux 7:a92dbed32890 315
samux 7:a92dbed32890 316 int Wifly::readable()
samux 7:a92dbed32890 317 {
samux 7:a92dbed32890 318 return buf_wifly.available();
samux 7:a92dbed32890 319 }
samux 7:a92dbed32890 320
samux 7:a92dbed32890 321 int Wifly::writeable()
samux 7:a92dbed32890 322 {
samux 7:a92dbed32890 323 return wifi.writeable();
samux 7:a92dbed32890 324 }
samux 7:a92dbed32890 325
samux 7:a92dbed32890 326 char Wifly::getc()
samux 7:a92dbed32890 327 {
samux 7:a92dbed32890 328 char c;
samux 7:a92dbed32890 329 while (!buf_wifly.available());
samux 7:a92dbed32890 330 buf_wifly.dequeue(&c);
samux 7:a92dbed32890 331 return c;
samux 7:a92dbed32890 332 }
samux 7:a92dbed32890 333
samux 7:a92dbed32890 334 void Wifly::handler_rx(void)
samux 7:a92dbed32890 335 {
samux 7:a92dbed32890 336 //read characters
samux 7:a92dbed32890 337 while (wifi.readable())
samux 7:a92dbed32890 338 buf_wifly.queue(wifi.getc());
samux 7:a92dbed32890 339 }
samux 7:a92dbed32890 340
samux 7:a92dbed32890 341 void Wifly::attach_rx(bool callback)
samux 7:a92dbed32890 342 {
samux 7:a92dbed32890 343 if (!callback)
samux 7:a92dbed32890 344 wifi.attach(NULL);
samux 7:a92dbed32890 345 else
samux 7:a92dbed32890 346 wifi.attach(this, &Wifly::handler_rx);
samux 7:a92dbed32890 347 }
samux 7:a92dbed32890 348
samux 7:a92dbed32890 349
samux 7:a92dbed32890 350 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 7:a92dbed32890 351 {
samux 7:a92dbed32890 352 char read;
samux 7:a92dbed32890 353 size_t found = string::npos;
samux 7:a92dbed32890 354 string checking;
samux 7:a92dbed32890 355 Timer tmr;
samux 7:a92dbed32890 356 int result = 0;
samux 7:a92dbed32890 357
samux 7:a92dbed32890 358 DBG("will send: %s\r\n",str);
samux 7:a92dbed32890 359
samux 7:a92dbed32890 360 attach_rx(false);
samux 7:a92dbed32890 361
samux 7:a92dbed32890 362 //We flush the buffer
samux 7:a92dbed32890 363 while (wifi.readable())
samux 7:a92dbed32890 364 wifi.getc();
samux 7:a92dbed32890 365
samux 7:a92dbed32890 366 if (!ACK || !strcmp(ACK, "NO")) {
samux 7:a92dbed32890 367 for (int i = 0; i < len; i++)
samux 7:a92dbed32890 368 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 7:a92dbed32890 369 } else {
samux 7:a92dbed32890 370 //We flush the buffer
samux 7:a92dbed32890 371 while (wifi.readable())
samux 7:a92dbed32890 372 wifi.getc();
samux 7:a92dbed32890 373
samux 7:a92dbed32890 374 tmr.start();
samux 7:a92dbed32890 375 for (int i = 0; i < len; i++)
samux 7:a92dbed32890 376 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 7:a92dbed32890 377
samux 7:a92dbed32890 378 while (1) {
samux 7:a92dbed32890 379 if (tmr.read_ms() > timeout) {
samux 7:a92dbed32890 380 //We flush the buffer
samux 7:a92dbed32890 381 while (wifi.readable())
samux 7:a92dbed32890 382 wifi.getc();
samux 7:a92dbed32890 383
samux 7:a92dbed32890 384 DBG("check: %s\r\n", checking.c_str());
samux 7:a92dbed32890 385
samux 7:a92dbed32890 386 attach_rx(true);
samux 7:a92dbed32890 387 return -1;
samux 7:a92dbed32890 388 } else if (wifi.readable()) {
samux 7:a92dbed32890 389 read = wifi.getc();
samux 7:a92dbed32890 390 if ( read != '\r' && read != '\n') {
samux 7:a92dbed32890 391 checking += read;
samux 7:a92dbed32890 392 found = checking.find(ACK);
samux 7:a92dbed32890 393 if (found != string::npos) {
samux 7:a92dbed32890 394 wait(0.01);
samux 7:a92dbed32890 395
samux 7:a92dbed32890 396 //We flush the buffer
samux 7:a92dbed32890 397 while (wifi.readable())
samux 7:a92dbed32890 398 wifi.getc();
samux 7:a92dbed32890 399
samux 7:a92dbed32890 400 break;
samux 7:a92dbed32890 401 }
samux 7:a92dbed32890 402 }
samux 7:a92dbed32890 403 }
samux 7:a92dbed32890 404 }
samux 7:a92dbed32890 405 DBG("check: %s\r\n", checking.c_str());
samux 7:a92dbed32890 406
samux 7:a92dbed32890 407 attach_rx(true);
samux 7:a92dbed32890 408 return result;
samux 7:a92dbed32890 409 }
samux 7:a92dbed32890 410
samux 7:a92dbed32890 411 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 7:a92dbed32890 412 if ( res != NULL) {
samux 7:a92dbed32890 413 int i = 0;
samux 7:a92dbed32890 414 Timer timeout;
samux 7:a92dbed32890 415 timeout.start();
samux 7:a92dbed32890 416 tmr.reset();
samux 7:a92dbed32890 417 while (1) {
samux 7:a92dbed32890 418 if (timeout.read() > 2) {
samux 7:a92dbed32890 419 if (i == 0) {
samux 7:a92dbed32890 420 res = NULL;
samux 7:a92dbed32890 421 break;
samux 7:a92dbed32890 422 }
samux 7:a92dbed32890 423 res[i] = '\0';
samux 7:a92dbed32890 424 DBG("user str 1: %s\r\n", res);
samux 7:a92dbed32890 425
samux 7:a92dbed32890 426 break;
samux 7:a92dbed32890 427 } else {
samux 7:a92dbed32890 428 if (tmr.read_ms() > 300) {
samux 7:a92dbed32890 429 res[i] = '\0';
samux 7:a92dbed32890 430 DBG("user str: %s\r\n", res);
samux 7:a92dbed32890 431
samux 7:a92dbed32890 432 break;
samux 7:a92dbed32890 433 }
samux 7:a92dbed32890 434 if (wifi.readable()) {
samux 7:a92dbed32890 435 tmr.start();
samux 7:a92dbed32890 436 read = wifi.getc();
samux 7:a92dbed32890 437
samux 7:a92dbed32890 438 // we drop \r and \n
samux 7:a92dbed32890 439 if ( read != '\r' && read != '\n') {
samux 7:a92dbed32890 440 res[i++] = read;
samux 7:a92dbed32890 441 }
samux 7:a92dbed32890 442 }
samux 7:a92dbed32890 443 }
samux 7:a92dbed32890 444 }
samux 7:a92dbed32890 445 DBG("user str: %s\r\n", res);
samux 7:a92dbed32890 446 }
samux 7:a92dbed32890 447
samux 7:a92dbed32890 448 //We flush the buffer
samux 7:a92dbed32890 449 while (wifi.readable())
samux 7:a92dbed32890 450 wifi.getc();
samux 7:a92dbed32890 451
samux 7:a92dbed32890 452 attach_rx(true);
samux 7:a92dbed32890 453 DBG("result: %d\r\n", result)
samux 7:a92dbed32890 454 return result;
samux 0:6ffb0aeb3972 455 }