- Added setBaud() function - Added CheckNetworkStatus() function - Improved messaging system

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

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