Damien Frost / WiflyInterface

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Thu Feb 18 12:05:53 2016 +0000
Revision:
12:1aaea7f302e6
Parent:
11:fc3d86645d23
Child:
13:8846f12fa277
- Adding lots of random code to Wifly Interface

Who changed what in which revision?

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