Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

Fork of WiflyInterface by mbed official

Committer:
leihen
Date:
Sat Jun 01 15:03:14 2013 +0000
Revision:
5:48d55083d2ff
Parent:
4:0bcec6272784
Child:
6:120296f9f865
Child:
7:e42b7fa7ef70
Fixed problem with non-blocking accept method.
; - Increase ring-buffer from 256 to 512.

Who changed what in which revision?

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