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 05 23:40:17 2013 +0000
Revision:
9:4f6f2f35a21a
Parent:
8:ac134ae11893
Child:
10:d84defb718ab
Improved 'Accept' handling.
; Added 'peek' functionality for CBuffer

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