Damien Frost / WiflyInterface

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

Committer:
defrost
Date:
Sun Jul 10 11:13:33 2016 +0000
Revision:
34:a22a6343e3d4
Parent:
33:26a14bc6d90e
Child:
35:ce3afc021ec2
- updated debug messages

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