wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Wed Aug 22 15:52:29 2012 +0000
Revision:
1:8f04181f9ad8
Parent:
0:6ffb0aeb3972
Child:
2:d78b1f66e345
add dnsLookup method

Who changed what in which revision?

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