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:
Wed Jun 26 21:12:21 2013 +0000
Revision:
11:63a7dc9e45dd
Parent:
10:d84defb718ab
Child:
12:4f95e6a365db
Working Multithreaded HTTP Server Version

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
leihen 11:63a7dc9e45dd 24 DigitalOut ledrx(LED3);
leihen 11:63a7dc9e45dd 25
samux 1:fb4494783863 26 //Debug is disabled by default
leihen 11:63a7dc9e45dd 27 #if (1 && !defined(TARGET_LPC11U24))
samux 1:fb4494783863 28 #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 29 #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 30 #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__);
leihen 11:63a7dc9e45dd 31 #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 32 #else
samux 1:fb4494783863 33 #define DBG(x, ...)
samux 1:fb4494783863 34 #define WARN(x, ...)
samux 1:fb4494783863 35 #define ERR(x, ...)
samux 1:fb4494783863 36 #define INFO(x, ...)
samux 1:fb4494783863 37 #endif
samux 1:fb4494783863 38
samux 1:fb4494783863 39 #define MAX_TRY_JOIN 3
samux 1:fb4494783863 40
leihen 7:e42b7fa7ef70 41 const int std_baudrates[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
leihen 7:e42b7fa7ef70 42
leihen 7:e42b7fa7ef70 43
samux 1:fb4494783863 44 Wifly * Wifly::inst;
samux 1:fb4494783863 45
leihen 7:e42b7fa7ef70 46 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec, WiflyBaudrate_t baud):
leihen 7:e42b7fa7ef70 47 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512), m_baudrate(baud)
samux 1:fb4494783863 48 {
samux 1:fb4494783863 49 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 50 state.sec = sec;
samux 1:fb4494783863 51
samux 1:fb4494783863 52 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 53 strcpy(this->ssid, ssid);
samux 1:fb4494783863 54 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 55 if (this->ssid[i] == ' ')
samux 1:fb4494783863 56 this->ssid[i] = '$';
samux 1:fb4494783863 57 }
samux 1:fb4494783863 58 strcpy(this->phrase, phrase);
samux 1:fb4494783863 59 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 60 if (this->phrase[i] == ' ')
samux 1:fb4494783863 61 this->phrase[i] = '$';
samux 1:fb4494783863 62 }
samux 1:fb4494783863 63
samux 1:fb4494783863 64 inst = this;
leihen 9:4f6f2f35a21a 65 attach_rx(false);
samux 1:fb4494783863 66 state.cmd_mode = false;
leihen 7:e42b7fa7ef70 67
leihen 7:e42b7fa7ef70 68 reset();
samux 1:fb4494783863 69 }
samux 1:fb4494783863 70
samux 1:fb4494783863 71 bool Wifly::join()
samux 1:fb4494783863 72 {
leihen 7:e42b7fa7ef70 73 char cmd[50];
leihen 7:e42b7fa7ef70 74
leihen 7:e42b7fa7ef70 75 setBaudRate(std_baudrates[m_baudrate]);
samux 1:fb4494783863 76
samux 1:fb4494783863 77 for (int i= 0; i < MAX_TRY_JOIN; i++) {
samux 2:8e54830d0df7 78
samux 2:8e54830d0df7 79 // no auto join
samux 2:8e54830d0df7 80 if (!sendCommand("set w j 0\r", "AOK"))
samux 2:8e54830d0df7 81 continue;
samux 2:8e54830d0df7 82
samux 2:8e54830d0df7 83 //no echo
leihen 11:63a7dc9e45dd 84 if (!sendCommand("set u m 17\r", "AOK"))
samux 2:8e54830d0df7 85 continue;
samux 2:8e54830d0df7 86
samux 1:fb4494783863 87 // set time
samux 4:0bcec6272784 88 if (!sendCommand("set c t 30\r", "AOK"))
samux 1:fb4494783863 89 continue;
samux 1:fb4494783863 90
samux 1:fb4494783863 91 // set size
samux 4:0bcec6272784 92 if (!sendCommand("set c s 1024\r", "AOK"))
samux 1:fb4494783863 93 continue;
samux 1:fb4494783863 94
samux 1:fb4494783863 95 // red led on when tcp connection active
samux 1:fb4494783863 96 if (!sendCommand("set s i 0x40\r", "AOK"))
samux 1:fb4494783863 97 continue;
samux 1:fb4494783863 98
samux 1:fb4494783863 99 // no string sent to the tcp client
samux 1:fb4494783863 100 if (!sendCommand("set c r 0\r", "AOK"))
samux 1:fb4494783863 101 continue;
samux 1:fb4494783863 102
samux 1:fb4494783863 103 // tcp protocol
samux 1:fb4494783863 104 if (!sendCommand("set i p 2\r", "AOK"))
samux 1:fb4494783863 105 continue;
samux 1:fb4494783863 106
samux 1:fb4494783863 107 // tcp retry
samux 1:fb4494783863 108 if (!sendCommand("set i f 0x7\r", "AOK"))
samux 1:fb4494783863 109 continue;
samux 2:8e54830d0df7 110
samux 2:8e54830d0df7 111 // set dns server
samux 2:8e54830d0df7 112 if (!sendCommand("set d n rn.microchip.com\r", "AOK"))
samux 1:fb4494783863 113 continue;
samux 1:fb4494783863 114
samux 1:fb4494783863 115 //dhcp
samux 1:fb4494783863 116 sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0);
samux 1:fb4494783863 117 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 118 continue;
samux 1:fb4494783863 119
samux 1:fb4494783863 120 // ssid
leihen 5:48d55083d2ff 121 sprintf(cmd, "set wlan ssid %s\r", ssid);
samux 1:fb4494783863 122 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 123 continue;
samux 1:fb4494783863 124
samux 1:fb4494783863 125 //auth
samux 1:fb4494783863 126 sprintf(cmd, "set w a %d\r", state.sec);
samux 1:fb4494783863 127 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 128 continue;
samux 1:fb4494783863 129
samux 1:fb4494783863 130 // if no dhcp, set ip, netmask and gateway
samux 1:fb4494783863 131 if (!state.dhcp) {
samux 1:fb4494783863 132 DBG("not dhcp\r");
samux 1:fb4494783863 133
samux 1:fb4494783863 134 sprintf(cmd, "set i a %s\r\n", ip);
samux 1:fb4494783863 135 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 136 continue;
samux 1:fb4494783863 137
samux 1:fb4494783863 138 sprintf(cmd, "set i n %s\r", netmask);
samux 1:fb4494783863 139 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 140 continue;
samux 1:fb4494783863 141
samux 1:fb4494783863 142 sprintf(cmd, "set i g %s\r", gateway);
samux 1:fb4494783863 143 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 144 continue;
samux 1:fb4494783863 145 }
samux 1:fb4494783863 146
samux 1:fb4494783863 147 //key step
samux 1:fb4494783863 148 if (state.sec != NONE) {
samux 1:fb4494783863 149 if (state.sec == WPA)
samux 1:fb4494783863 150 sprintf(cmd, "set w p %s\r", phrase);
samux 1:fb4494783863 151 else if (state.sec == WEP_128)
samux 1:fb4494783863 152 sprintf(cmd, "set w k %s\r", phrase);
samux 1:fb4494783863 153
samux 1:fb4494783863 154 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 155 continue;
samux 1:fb4494783863 156 }
samux 1:fb4494783863 157
samux 2:8e54830d0df7 158 //join the network (10s timeout)
samux 1:fb4494783863 159 if (state.dhcp) {
samux 2:8e54830d0df7 160 if (!sendCommand("join\r", "DHCP=ON", NULL, 10000))
samux 2:8e54830d0df7 161 continue;
samux 2:8e54830d0df7 162 } else {
samux 2:8e54830d0df7 163 if (!sendCommand("join\r", "Associated", NULL, 10000))
samux 1:fb4494783863 164 continue;
samux 1:fb4494783863 165 }
leihen 5:48d55083d2ff 166
leihen 7:e42b7fa7ef70 167 enableTime(1);
leihen 7:e42b7fa7ef70 168
samux 2:8e54830d0df7 169 if (!sendCommand("save\r", "Stor"))
samux 2:8e54830d0df7 170 continue;
samux 2:8e54830d0df7 171
samux 1:fb4494783863 172 exit();
samux 1:fb4494783863 173
samux 1:fb4494783863 174 state.associated = true;
samux 1:fb4494783863 175 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
samux 1:fb4494783863 176 return true;
samux 1:fb4494783863 177 }
samux 1:fb4494783863 178 return false;
samux 1:fb4494783863 179 }
samux 1:fb4494783863 180
leihen 7:e42b7fa7ef70 181 int Wifly::enableTime(int minutes, const char* ntp_address)
leihen 7:e42b7fa7ef70 182 {
leihen 7:e42b7fa7ef70 183 char cmd[30];
leihen 7:e42b7fa7ef70 184
leihen 7:e42b7fa7ef70 185 //let module automatically conntect to timeserver and get the actual time
leihen 7:e42b7fa7ef70 186 sprintf(cmd, "set t e %d\r", minutes);
leihen 7:e42b7fa7ef70 187 if (!sendCommand(cmd, "AOK")) {
leihen 7:e42b7fa7ef70 188 ERR("Failed to modify time function !");
leihen 7:e42b7fa7ef70 189 return -1;
leihen 7:e42b7fa7ef70 190 }
leihen 7:e42b7fa7ef70 191
leihen 7:e42b7fa7ef70 192 //set the NTP server address
leihen 7:e42b7fa7ef70 193 sprintf(cmd, "set t a %s\r", ntp_address);
leihen 7:e42b7fa7ef70 194 if (!sendCommand(cmd, "AOK")) {
leihen 7:e42b7fa7ef70 195 ERR("Failed to modify time server address !");
leihen 7:e42b7fa7ef70 196 }
leihen 7:e42b7fa7ef70 197
leihen 7:e42b7fa7ef70 198 if (!sendCommand("set option format 1\r", "AOK")) {
leihen 7:e42b7fa7ef70 199 ERR("Failed to set option format to ASCII !");
leihen 7:e42b7fa7ef70 200 }
leihen 7:e42b7fa7ef70 201
leihen 7:e42b7fa7ef70 202 if (!sendCommand("set time zone 0\r", "AOK")) {
leihen 7:e42b7fa7ef70 203 ERR("Failed to set time zone !");
leihen 7:e42b7fa7ef70 204 }
leihen 7:e42b7fa7ef70 205
leihen 9:4f6f2f35a21a 206 sendCommand("time\r", NULL, NULL, 1000);
leihen 7:e42b7fa7ef70 207
leihen 7:e42b7fa7ef70 208 flush();
leihen 7:e42b7fa7ef70 209
leihen 7:e42b7fa7ef70 210 exit();
leihen 7:e42b7fa7ef70 211
leihen 7:e42b7fa7ef70 212 return 0;
leihen 7:e42b7fa7ef70 213 }
leihen 7:e42b7fa7ef70 214
leihen 7:e42b7fa7ef70 215 string Wifly::getTime(bool uptime)
leihen 7:e42b7fa7ef70 216 {
leihen 7:e42b7fa7ef70 217 char buf[100];
leihen 7:e42b7fa7ef70 218
leihen 7:e42b7fa7ef70 219 sendCommand("time\r", NULL, NULL, 10000);
leihen 7:e42b7fa7ef70 220 if (!sendCommand("show time\r", NULL, buf, 10000))
leihen 7:e42b7fa7ef70 221 return "";
leihen 7:e42b7fa7ef70 222 INFO("\r\nReceived Time : %s\r\n", buf);
leihen 7:e42b7fa7ef70 223
leihen 7:e42b7fa7ef70 224 exit();
leihen 7:e42b7fa7ef70 225 return buf;
leihen 7:e42b7fa7ef70 226 }
leihen 7:e42b7fa7ef70 227
leihen 7:e42b7fa7ef70 228 bool Wifly::setBaudRate(int baud)
leihen 7:e42b7fa7ef70 229 {
leihen 7:e42b7fa7ef70 230 char str[35];
leihen 9:4f6f2f35a21a 231 bool bfound = true;
leihen 7:e42b7fa7ef70 232
leihen 7:e42b7fa7ef70 233 INFO("Trying baudrates.\n");
leihen 7:e42b7fa7ef70 234 sprintf(str, "set uart raw %d\r", baud);
leihen 9:4f6f2f35a21a 235
leihen 9:4f6f2f35a21a 236 wifi.baud(baud);
leihen 9:4f6f2f35a21a 237 if (!sendCommand(str, "AOK")) {
leihen 9:4f6f2f35a21a 238 for( int i = 0 ; i < sizeof(std_baudrates)/sizeof(int) ; i++) {
leihen 9:4f6f2f35a21a 239 // try all standard baudrates until the correct one is found
leihen 9:4f6f2f35a21a 240 INFO("Trying at %d baud\n", std_baudrates[i]);
leihen 9:4f6f2f35a21a 241 wifi.baud(std_baudrates[i]);
leihen 9:4f6f2f35a21a 242 if (sendCommand(str, "AOK")) {
leihen 9:4f6f2f35a21a 243 bfound = true;
leihen 9:4f6f2f35a21a 244 break;
leihen 9:4f6f2f35a21a 245 }
leihen 7:e42b7fa7ef70 246 }
leihen 7:e42b7fa7ef70 247 }
leihen 7:e42b7fa7ef70 248 if (bfound) {
leihen 7:e42b7fa7ef70 249 wait(0.05);
leihen 7:e42b7fa7ef70 250 if (!sendCommand("save\r", "STOR"))
leihen 7:e42b7fa7ef70 251 return false;
leihen 7:e42b7fa7ef70 252
leihen 7:e42b7fa7ef70 253 reboot();
leihen 7:e42b7fa7ef70 254
leihen 7:e42b7fa7ef70 255 wifi.baud(baud);
leihen 7:e42b7fa7ef70 256 }
leihen 7:e42b7fa7ef70 257 return true;
leihen 7:e42b7fa7ef70 258 }
samux 1:fb4494783863 259
samux 1:fb4494783863 260 bool Wifly::setProtocol(Protocol p)
samux 1:fb4494783863 261 {
samux 1:fb4494783863 262 // use udp auto pairing
samux 1:fb4494783863 263 char cmd[20];
samux 1:fb4494783863 264 sprintf(cmd, "set i p %d\r", p);
samux 1:fb4494783863 265 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 266 return false;
samux 1:fb4494783863 267
samux 1:fb4494783863 268 switch(p) {
samux 1:fb4494783863 269 case TCP:
samux 1:fb4494783863 270 // set ip flags: tcp retry enabled
samux 1:fb4494783863 271 if (!sendCommand("set i f 0x07\r", "AOK"))
samux 1:fb4494783863 272 return false;
samux 1:fb4494783863 273 break;
samux 1:fb4494783863 274 case UDP:
samux 1:fb4494783863 275 // set ip flags: udp auto pairing enabled
samux 1:fb4494783863 276 if (!sendCommand("set i h 0.0.0.0\r", "AOK"))
samux 1:fb4494783863 277 return false;
samux 4:0bcec6272784 278 if (!sendCommand("set i f 0x40\r", "AOK"))
samux 1:fb4494783863 279 return false;
samux 1:fb4494783863 280 break;
samux 1:fb4494783863 281 }
samux 1:fb4494783863 282 state.proto = p;
samux 1:fb4494783863 283 return true;
samux 1:fb4494783863 284 }
samux 1:fb4494783863 285
samux 1:fb4494783863 286 char * Wifly::getStringSecurity()
samux 1:fb4494783863 287 {
samux 1:fb4494783863 288 switch(state.sec) {
samux 1:fb4494783863 289 case NONE:
samux 1:fb4494783863 290 return "NONE";
samux 1:fb4494783863 291 case WEP_128:
samux 1:fb4494783863 292 return "WEP_128";
samux 1:fb4494783863 293 case WPA:
samux 1:fb4494783863 294 return "WPA";
samux 1:fb4494783863 295 }
samux 1:fb4494783863 296 return "UNKNOWN";
samux 1:fb4494783863 297 }
samux 1:fb4494783863 298
samux 1:fb4494783863 299 bool Wifly::connect(const char * host, int port)
samux 1:fb4494783863 300 {
samux 1:fb4494783863 301 char rcv[20];
leihen 7:e42b7fa7ef70 302 char cmd[50];
samux 1:fb4494783863 303
samux 2:8e54830d0df7 304 // try to open
samux 2:8e54830d0df7 305 sprintf(cmd, "open %s %d\r", host, port);
samux 2:8e54830d0df7 306 if (sendCommand(cmd, "OPEN", NULL, 10000)) {
samux 2:8e54830d0df7 307 state.tcp = true;
samux 2:8e54830d0df7 308 state.cmd_mode = false;
samux 2:8e54830d0df7 309 return true;
samux 1:fb4494783863 310 }
samux 1:fb4494783863 311
samux 2:8e54830d0df7 312 // if failed, retry and parse the response
samux 2:8e54830d0df7 313 if (sendCommand(cmd, NULL, rcv, 5000)) {
samux 1:fb4494783863 314 if (strstr(rcv, "OPEN") == NULL) {
samux 1:fb4494783863 315 if (strstr(rcv, "Connected") != NULL) {
samux 2:8e54830d0df7 316 wait(0.25);
samux 1:fb4494783863 317 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 318 return false;
samux 2:8e54830d0df7 319 wait(0.25);
samux 2:8e54830d0df7 320 if (!sendCommand(cmd, "OPEN", NULL, 10000))
samux 1:fb4494783863 321 return false;
samux 1:fb4494783863 322 } else {
samux 1:fb4494783863 323 return false;
samux 1:fb4494783863 324 }
samux 1:fb4494783863 325 }
samux 1:fb4494783863 326 } else {
samux 1:fb4494783863 327 return false;
samux 1:fb4494783863 328 }
samux 2:8e54830d0df7 329
samux 1:fb4494783863 330 state.tcp = true;
samux 1:fb4494783863 331 state.cmd_mode = false;
samux 1:fb4494783863 332
samux 1:fb4494783863 333 return true;
samux 1:fb4494783863 334 }
samux 1:fb4494783863 335
samux 1:fb4494783863 336
samux 1:fb4494783863 337 bool Wifly::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 338 {
samux 1:fb4494783863 339 string h = host;
samux 1:fb4494783863 340 char cmd[30], rcv[100];
samux 1:fb4494783863 341 int l = 0;
samux 1:fb4494783863 342 char * point;
samux 1:fb4494783863 343 int nb_digits = 0;
samux 1:fb4494783863 344
samux 1:fb4494783863 345 // no dns needed
samux 1:fb4494783863 346 int pos = h.find(".");
samux 1:fb4494783863 347 if (pos != string::npos) {
samux 1:fb4494783863 348 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 349 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 350 }
samux 1:fb4494783863 351 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 352 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 353 strcpy(ip, host);
samux 1:fb4494783863 354 }
samux 1:fb4494783863 355 // dns needed
samux 1:fb4494783863 356 else {
samux 1:fb4494783863 357 nb_digits = 0;
samux 1:fb4494783863 358 sprintf(cmd, "lookup %s\r", host);
samux 1:fb4494783863 359 if (!sendCommand(cmd, NULL, rcv))
samux 1:fb4494783863 360 return false;
samux 1:fb4494783863 361
samux 1:fb4494783863 362 // look for the ip address
samux 1:fb4494783863 363 char * begin = strstr(rcv, "=") + 1;
samux 1:fb4494783863 364 for (int i = 0; i < 3; i++) {
samux 1:fb4494783863 365 point = strstr(begin + l, ".");
samux 1:fb4494783863 366 DBG("str: %s", begin + l);
samux 1:fb4494783863 367 l += point - (begin + l) + 1;
samux 1:fb4494783863 368 }
samux 1:fb4494783863 369 DBG("str: %s", begin + l);
samux 1:fb4494783863 370 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
samux 1:fb4494783863 371 DBG("digit: %c", *(begin + l + nb_digits));
samux 1:fb4494783863 372 nb_digits++;
samux 1:fb4494783863 373 }
samux 1:fb4494783863 374 memcpy(ip, begin, l + nb_digits);
samux 1:fb4494783863 375 ip[l+nb_digits] = 0;
samux 1:fb4494783863 376 DBG("ip from dns: %s", ip);
samux 1:fb4494783863 377 }
samux 1:fb4494783863 378 return true;
samux 1:fb4494783863 379 }
samux 1:fb4494783863 380
samux 1:fb4494783863 381
samux 1:fb4494783863 382 void Wifly::flush()
samux 1:fb4494783863 383 {
samux 1:fb4494783863 384 buf_wifly.flush();
samux 1:fb4494783863 385 }
samux 1:fb4494783863 386
samux 1:fb4494783863 387 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
samux 1:fb4494783863 388 {
samux 1:fb4494783863 389 if (!state.cmd_mode) {
samux 1:fb4494783863 390 cmdMode();
samux 1:fb4494783863 391 }
samux 1:fb4494783863 392 if (send(cmd, strlen(cmd), ack, res, timeout) == -1) {
samux 1:fb4494783863 393 ERR("sendCommand: cannot %s\r\n", cmd);
samux 1:fb4494783863 394 exit();
samux 1:fb4494783863 395 return false;
samux 1:fb4494783863 396 }
samux 1:fb4494783863 397 return true;
samux 1:fb4494783863 398 }
samux 1:fb4494783863 399
samux 1:fb4494783863 400 bool Wifly::cmdMode()
samux 1:fb4494783863 401 {
samux 1:fb4494783863 402 // if already in cmd mode, return
samux 1:fb4494783863 403 if (state.cmd_mode)
samux 1:fb4494783863 404 return true;
samux 2:8e54830d0df7 405
samux 1:fb4494783863 406 if (send("$$$", 3, "CMD") == -1) {
samux 1:fb4494783863 407 ERR("cannot enter in cmd mode\r\n");
samux 2:8e54830d0df7 408 exit();
samux 1:fb4494783863 409 return false;
samux 1:fb4494783863 410 }
samux 1:fb4494783863 411 state.cmd_mode = true;
samux 1:fb4494783863 412 return true;
samux 1:fb4494783863 413 }
samux 1:fb4494783863 414
samux 1:fb4494783863 415 bool Wifly::disconnect()
samux 1:fb4494783863 416 {
samux 1:fb4494783863 417 // if already disconnected, return
samux 1:fb4494783863 418 if (!state.associated)
samux 1:fb4494783863 419 return true;
samux 2:8e54830d0df7 420
samux 1:fb4494783863 421 if (!sendCommand("leave\r", "DeAuth"))
samux 1:fb4494783863 422 return false;
samux 1:fb4494783863 423 exit();
samux 2:8e54830d0df7 424
samux 1:fb4494783863 425 state.associated = false;
samux 1:fb4494783863 426 return true;
samux 1:fb4494783863 427
samux 1:fb4494783863 428 }
samux 1:fb4494783863 429
samux 1:fb4494783863 430 bool Wifly::is_connected()
samux 1:fb4494783863 431 {
samux 1:fb4494783863 432 return (tcp_status.read() == 1) ? true : false;
samux 1:fb4494783863 433 }
samux 1:fb4494783863 434
samux 1:fb4494783863 435
samux 1:fb4494783863 436 void Wifly::reset()
samux 1:fb4494783863 437 {
samux 1:fb4494783863 438 reset_pin = 0;
samux 1:fb4494783863 439 wait(0.2);
samux 1:fb4494783863 440 reset_pin = 1;
samux 1:fb4494783863 441 wait(0.2);
samux 1:fb4494783863 442 }
samux 1:fb4494783863 443
samux 3:9aa05e19c62e 444 bool Wifly::reboot()
samux 3:9aa05e19c62e 445 {
samux 3:9aa05e19c62e 446 // if already in cmd mode, return
samux 3:9aa05e19c62e 447 if (!sendCommand("reboot\r"))
samux 3:9aa05e19c62e 448 return false;
samux 3:9aa05e19c62e 449
samux 3:9aa05e19c62e 450 wait(0.3);
samux 3:9aa05e19c62e 451
samux 3:9aa05e19c62e 452 state.cmd_mode = false;
samux 3:9aa05e19c62e 453 return true;
samux 3:9aa05e19c62e 454 }
samux 3:9aa05e19c62e 455
samux 1:fb4494783863 456 bool Wifly::close()
samux 1:fb4494783863 457 {
samux 1:fb4494783863 458 // if not connected, return
leihen 11:63a7dc9e45dd 459 // if (!state.tcp)
leihen 11:63a7dc9e45dd 460 // return true;
samux 2:8e54830d0df7 461
leihen 11:63a7dc9e45dd 462 // Thread::wait(250);
samux 1:fb4494783863 463 if (!sendCommand("close\r", "CLOS"))
samux 1:fb4494783863 464 return false;
leihen 9:4f6f2f35a21a 465 exit(false);
samux 2:8e54830d0df7 466
samux 1:fb4494783863 467 state.tcp = false;
samux 1:fb4494783863 468 return true;
samux 1:fb4494783863 469 }
samux 1:fb4494783863 470
samux 1:fb4494783863 471
samux 1:fb4494783863 472 int Wifly::putc(char c)
samux 1:fb4494783863 473 {
samux 1:fb4494783863 474 while (!wifi.writeable());
samux 1:fb4494783863 475 return wifi.putc(c);
samux 1:fb4494783863 476 }
samux 1:fb4494783863 477
samux 1:fb4494783863 478
leihen 9:4f6f2f35a21a 479 bool Wifly::exit(bool bflush)
samux 1:fb4494783863 480 {
leihen 9:4f6f2f35a21a 481 if (bflush)
leihen 9:4f6f2f35a21a 482 flush();
samux 1:fb4494783863 483 if (!state.cmd_mode)
samux 1:fb4494783863 484 return true;
samux 1:fb4494783863 485 if (!sendCommand("exit\r", "EXIT"))
samux 1:fb4494783863 486 return false;
samux 1:fb4494783863 487 state.cmd_mode = false;
leihen 9:4f6f2f35a21a 488 if (bflush)
leihen 9:4f6f2f35a21a 489 flush();
samux 1:fb4494783863 490 return true;
samux 1:fb4494783863 491 }
samux 1:fb4494783863 492
samux 1:fb4494783863 493
samux 1:fb4494783863 494 int Wifly::readable()
samux 1:fb4494783863 495 {
samux 1:fb4494783863 496 return buf_wifly.available();
samux 1:fb4494783863 497 }
samux 1:fb4494783863 498
samux 1:fb4494783863 499 int Wifly::writeable()
samux 1:fb4494783863 500 {
samux 1:fb4494783863 501 return wifi.writeable();
samux 1:fb4494783863 502 }
samux 1:fb4494783863 503
samux 1:fb4494783863 504 char Wifly::getc()
samux 1:fb4494783863 505 {
samux 1:fb4494783863 506 char c;
samux 1:fb4494783863 507 while (!buf_wifly.available());
samux 1:fb4494783863 508 buf_wifly.dequeue(&c);
samux 1:fb4494783863 509 return c;
samux 1:fb4494783863 510 }
samux 1:fb4494783863 511
leihen 9:4f6f2f35a21a 512
leihen 9:4f6f2f35a21a 513 char Wifly::peek()
leihen 9:4f6f2f35a21a 514 {
leihen 9:4f6f2f35a21a 515 char c;
leihen 9:4f6f2f35a21a 516 while (!buf_wifly.available())
leihen 9:4f6f2f35a21a 517 ;
leihen 9:4f6f2f35a21a 518 buf_wifly.peek(&c);
leihen 9:4f6f2f35a21a 519 return c;
leihen 9:4f6f2f35a21a 520 }
leihen 9:4f6f2f35a21a 521
samux 1:fb4494783863 522 void Wifly::handler_rx(void)
samux 1:fb4494783863 523 {
samux 1:fb4494783863 524 //read characters
leihen 10:d84defb718ab 525 while (wifi.readable()) {
leihen 10:d84defb718ab 526 char c = LPC_UART3->RBR;
leihen 10:d84defb718ab 527 buf_wifly.queue(c);
leihen 11:63a7dc9e45dd 528 ledrx = !ledrx;
leihen 10:d84defb718ab 529 }
samux 1:fb4494783863 530 }
samux 1:fb4494783863 531
samux 1:fb4494783863 532 void Wifly::attach_rx(bool callback)
samux 1:fb4494783863 533 {
samux 1:fb4494783863 534 if (!callback)
samux 1:fb4494783863 535 wifi.attach(NULL);
samux 1:fb4494783863 536 else
samux 1:fb4494783863 537 wifi.attach(this, &Wifly::handler_rx);
samux 1:fb4494783863 538 }
samux 1:fb4494783863 539
samux 1:fb4494783863 540 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 541 {
samux 1:fb4494783863 542 char read;
samux 1:fb4494783863 543 size_t found = string::npos;
samux 1:fb4494783863 544 string checking;
samux 1:fb4494783863 545 Timer tmr;
samux 1:fb4494783863 546 int result = 0;
samux 1:fb4494783863 547
samux 1:fb4494783863 548 DBG("will send: %s\r\n",str);
samux 1:fb4494783863 549
samux 1:fb4494783863 550 attach_rx(false);
samux 1:fb4494783863 551
samux 1:fb4494783863 552 //We flush the buffer
samux 1:fb4494783863 553 while (wifi.readable())
leihen 9:4f6f2f35a21a 554 {
leihen 9:4f6f2f35a21a 555 char c = wifi.getc();
leihen 9:4f6f2f35a21a 556 INFO("Flushing : %c",c);
leihen 9:4f6f2f35a21a 557 }
samux 1:fb4494783863 558
samux 1:fb4494783863 559 if (!ACK || !strcmp(ACK, "NO")) {
samux 1:fb4494783863 560 for (int i = 0; i < len; i++)
samux 1:fb4494783863 561 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 562 } else {
samux 1:fb4494783863 563 //We flush the buffer
samux 1:fb4494783863 564 while (wifi.readable())
leihen 9:4f6f2f35a21a 565 {
leihen 9:4f6f2f35a21a 566 char c = wifi.getc();
leihen 9:4f6f2f35a21a 567 INFO("Flushing : %c",c);
leihen 9:4f6f2f35a21a 568 }
samux 1:fb4494783863 569
samux 1:fb4494783863 570 tmr.start();
samux 1:fb4494783863 571 for (int i = 0; i < len; i++)
samux 1:fb4494783863 572 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 573
samux 1:fb4494783863 574 while (1) {
samux 1:fb4494783863 575 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 576 //We flush the buffer
leihen 11:63a7dc9e45dd 577 while (wifi.readable()) {
leihen 11:63a7dc9e45dd 578 char c = wifi.getc();
leihen 11:63a7dc9e45dd 579 INFO("Flushing : %c",c);
leihen 11:63a7dc9e45dd 580 }
samux 1:fb4494783863 581
samux 1:fb4494783863 582 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 583
samux 1:fb4494783863 584 attach_rx(true);
samux 1:fb4494783863 585 return -1;
samux 1:fb4494783863 586 } else if (wifi.readable()) {
samux 1:fb4494783863 587 read = wifi.getc();
samux 1:fb4494783863 588 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 589 checking += read;
samux 1:fb4494783863 590 found = checking.find(ACK);
samux 1:fb4494783863 591 if (found != string::npos) {
samux 1:fb4494783863 592 wait(0.01);
samux 1:fb4494783863 593 //We flush the buffer
leihen 11:63a7dc9e45dd 594 while (wifi.readable()) {
leihen 11:63a7dc9e45dd 595 char c = wifi.getc();
leihen 11:63a7dc9e45dd 596 INFO("Flushing : %c",c);
leihen 11:63a7dc9e45dd 597 }
samux 1:fb4494783863 598
samux 1:fb4494783863 599 break;
samux 1:fb4494783863 600 }
samux 1:fb4494783863 601 }
samux 1:fb4494783863 602 }
samux 1:fb4494783863 603 }
samux 1:fb4494783863 604 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 605
samux 1:fb4494783863 606 attach_rx(true);
samux 1:fb4494783863 607 return result;
samux 1:fb4494783863 608 }
samux 1:fb4494783863 609
samux 1:fb4494783863 610 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 611 if ( res != NULL) {
samux 1:fb4494783863 612 int i = 0;
samux 1:fb4494783863 613 Timer timeout;
samux 1:fb4494783863 614 timeout.start();
samux 1:fb4494783863 615 tmr.reset();
samux 1:fb4494783863 616 while (1) {
samux 1:fb4494783863 617 if (timeout.read() > 2) {
samux 1:fb4494783863 618 if (i == 0) {
samux 1:fb4494783863 619 res = NULL;
samux 1:fb4494783863 620 break;
samux 1:fb4494783863 621 }
samux 1:fb4494783863 622 res[i] = '\0';
samux 1:fb4494783863 623 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 624
samux 1:fb4494783863 625 break;
samux 1:fb4494783863 626 } else {
samux 1:fb4494783863 627 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 628 res[i] = '\0';
samux 1:fb4494783863 629 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 630
samux 1:fb4494783863 631 break;
samux 1:fb4494783863 632 }
samux 1:fb4494783863 633 if (wifi.readable()) {
samux 1:fb4494783863 634 tmr.start();
samux 1:fb4494783863 635 read = wifi.getc();
samux 1:fb4494783863 636
samux 1:fb4494783863 637 // we drop \r and \n
samux 1:fb4494783863 638 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 639 res[i++] = read;
samux 1:fb4494783863 640 }
samux 1:fb4494783863 641 }
samux 1:fb4494783863 642 }
samux 1:fb4494783863 643 }
samux 1:fb4494783863 644 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 645 }
samux 1:fb4494783863 646
samux 1:fb4494783863 647 //We flush the buffer
leihen 11:63a7dc9e45dd 648 while (wifi.readable()) {
leihen 11:63a7dc9e45dd 649 char c = wifi.getc();
leihen 11:63a7dc9e45dd 650 INFO("Flushing : %c",c);
leihen 11:63a7dc9e45dd 651 }
samux 1:fb4494783863 652
samux 1:fb4494783863 653 attach_rx(true);
samux 1:fb4494783863 654 DBG("result: %d\r\n", result)
samux 1:fb4494783863 655 return result;
leihen 11:63a7dc9e45dd 656 }
leihen 11:63a7dc9e45dd 657
leihen 11:63a7dc9e45dd 658
leihen 11:63a7dc9e45dd 659 int Wifly::sendData(const char* data, int len, int _timeout)
leihen 11:63a7dc9e45dd 660 {
leihen 11:63a7dc9e45dd 661 int result = 0;
leihen 11:63a7dc9e45dd 662 Timer tmr;
leihen 11:63a7dc9e45dd 663
leihen 11:63a7dc9e45dd 664 tmr.start();
leihen 11:63a7dc9e45dd 665 while (tmr.read_ms() < _timeout) {
leihen 11:63a7dc9e45dd 666 if (wifi.writeable())
leihen 11:63a7dc9e45dd 667 break;
leihen 11:63a7dc9e45dd 668 }
leihen 11:63a7dc9e45dd 669 if (tmr.read_ms() >= _timeout) {
leihen 11:63a7dc9e45dd 670 return -1;
leihen 11:63a7dc9e45dd 671 }
leihen 11:63a7dc9e45dd 672 for (int i = 0; i < len; i++)
leihen 11:63a7dc9e45dd 673 result = (putc(data[i]) == data[i]) ? result + 1 : result;
leihen 11:63a7dc9e45dd 674
leihen 11:63a7dc9e45dd 675 return result;
samux 1:fb4494783863 676 }