Damien Frost / WiflyInterface

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Fri Aug 05 13:31:37 2016 +0000
Revision:
35:ce3afc021ec2
Parent:
34:a22a6343e3d4
Child:
36:8774837f5a78
- Added baudrate variable to Wifly so that after a reset, the baud rate is updated to the previously baud rate; - Debug messages on

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