Damien Frost / WiflyInterface

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Thu Mar 31 16:22:37 2016 +0000
Revision:
25:36b2d76ca8d9
Parent:
24:705480e2e482
Child:
26:eaaedb036df1
- Removed all printfs; - changed printing of messages to pc.printf, from std::printf

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