ACTUALIZED LIBRARY 0 Open (Default) 1 WEP-128 2 WPA1 3 Mixed WPA1 & WPA2-PSK 4 WPA2-PSK 5 Not Used 6 Adhoc, Join any Adhoc network

Dependents:   mbed_Arduino_sensor

Fork of WiflyInterface by Samuel Mokrani

Committer:
sherckuith
Date:
Fri Aug 24 12:29:57 2012 +0000
Revision:
11:002225507f44
Parent:
7:a92dbed32890
WLAN Parameters FULL ACTUALIZED!; 0 Open (Default); 1 WEP-128; 2 WPA1; 3 Mixed WPA1 & WPA2-PSK; 4 WPA2-PSK; 5 Not Used; 6 Adhoc, Join any Adhoc network

Who changed what in which revision?

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