ESP8266 Socket Library. AT Thinker firmware.

Dependents:   ESP8266_MQTT_HelloWorld ESP8266_IFTTT_Test ECE_4180_Lab_4 websocketmbed ... more

Fork of ESP8266Interface by ESP8266

This repository has been superceded

This project has moved to https://developer.mbed.org/teams/ESP8266/code/esp8266-driver/

This library works with the AT Thinker firmware.

Note

This library is currently in Beta. It is not feature complete and has some bugs, proceed with caution! Fixes and patches are welcome and appreciated!

Currently the ESP8266Interface Library has the following Abilities:

Working

  • TCP Client
  • UDP Client
  • Transparent mode (single connection of 1 type at a time)
  • Station Mode (connects to AP)

To be implemented

  • TCP Server
  • UDP Server
  • Multi Connection Mode (able to have up to 5 sockets at a time)
  • AP Mode (Make ESP Chip act like access point)
  • DNS Support (currently websites must be looked up by IP)
  • Error Recovery

Nice but not necessary

  • colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
Committer:
michaeljkoster
Date:
Wed Oct 08 19:57:58 2014 +0000
Revision:
12:c5f0eac67a8a
Parent:
11:fc3d86645d23
Debug

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
screamer 10:131675c17372 25 #if (defined(DEBUG))
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
screamer 10:131675c17372 35 #if defined(DEBUG)
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
michaeljkoster 12:c5f0eac67a8a 43 extern Serial pc;
michaeljkoster 12:c5f0eac67a8a 44
samux 1:fb4494783863 45 Wifly * Wifly::inst;
samux 1:fb4494783863 46
samux 1:fb4494783863 47 Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
samux 1:fb4494783863 48 wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256)
samux 1:fb4494783863 49 {
samux 1:fb4494783863 50 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 51 state.sec = sec;
samux 1:fb4494783863 52
samux 1:fb4494783863 53 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 54 strcpy(this->ssid, ssid);
samux 1:fb4494783863 55 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 56 if (this->ssid[i] == ' ')
samux 1:fb4494783863 57 this->ssid[i] = '$';
samux 1:fb4494783863 58 }
samux 1:fb4494783863 59 strcpy(this->phrase, phrase);
samux 1:fb4494783863 60 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 61 if (this->phrase[i] == ' ')
samux 1:fb4494783863 62 this->phrase[i] = '$';
samux 1:fb4494783863 63 }
samux 1:fb4494783863 64
samux 1:fb4494783863 65 inst = this;
samux 1:fb4494783863 66 attach_rx(false);
samux 1:fb4494783863 67 state.cmd_mode = false;
samux 1:fb4494783863 68 }
samux 1:fb4494783863 69
samux 1:fb4494783863 70 bool Wifly::join()
samux 1:fb4494783863 71 {
samux 1:fb4494783863 72 char cmd[20];
samux 1:fb4494783863 73
samux 1:fb4494783863 74 for (int i= 0; i < MAX_TRY_JOIN; i++) {
samux 2:8e54830d0df7 75
samux 2:8e54830d0df7 76 // no auto join
michaeljkoster 12:c5f0eac67a8a 77 if (!sendCommand("set w j 0\r\n", "AOK"))
samux 2:8e54830d0df7 78 continue;
samux 2:8e54830d0df7 79
samux 2:8e54830d0df7 80 //no echo
michaeljkoster 12:c5f0eac67a8a 81 if (!sendCommand("set u m 1\r\n", "AOK"))
samux 2:8e54830d0df7 82 continue;
samux 2:8e54830d0df7 83
samux 1:fb4494783863 84 // set time
michaeljkoster 12:c5f0eac67a8a 85 if (!sendCommand("set c t 30\r\n", "AOK"))
samux 1:fb4494783863 86 continue;
samux 1:fb4494783863 87
samux 1:fb4494783863 88 // set size
michaeljkoster 12:c5f0eac67a8a 89 if (!sendCommand("set c s 1024\r\n", "AOK"))
samux 1:fb4494783863 90 continue;
samux 1:fb4494783863 91
samux 1:fb4494783863 92 // red led on when tcp connection active
michaeljkoster 12:c5f0eac67a8a 93 if (!sendCommand("set s i 0x40\r\n", "AOK"))
samux 1:fb4494783863 94 continue;
samux 1:fb4494783863 95
samux 1:fb4494783863 96 // no string sent to the tcp client
michaeljkoster 12:c5f0eac67a8a 97 if (!sendCommand("set c r 0\r\n", "AOK"))
samux 1:fb4494783863 98 continue;
samux 1:fb4494783863 99
samux 1:fb4494783863 100 // tcp protocol
michaeljkoster 12:c5f0eac67a8a 101 if (!sendCommand("set i p 2\r\n", "AOK"))
samux 1:fb4494783863 102 continue;
samux 1:fb4494783863 103
samux 1:fb4494783863 104 // tcp retry
michaeljkoster 12:c5f0eac67a8a 105 if (!sendCommand("set i f 0x7\r\n", "AOK"))
samux 1:fb4494783863 106 continue;
samux 2:8e54830d0df7 107
samux 2:8e54830d0df7 108 // set dns server
michaeljkoster 12:c5f0eac67a8a 109 if (!sendCommand("set d n rn.microchip.com\r\n", "AOK"))
samux 1:fb4494783863 110 continue;
samux 1:fb4494783863 111
samux 1:fb4494783863 112 //dhcp
michaeljkoster 12:c5f0eac67a8a 113 sprintf(cmd, "set i d %d\r\n", (state.dhcp) ? 1 : 0);
samux 1:fb4494783863 114 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 115 continue;
samux 1:fb4494783863 116
samux 1:fb4494783863 117 // ssid
michaeljkoster 12:c5f0eac67a8a 118 sprintf(cmd, "set w s %s\r\n", ssid);
lz307 7:3152fcc74390 119 if (!sendCommand(cmd, "AOK", NULL, 1000))
samux 1:fb4494783863 120 continue;
samux 1:fb4494783863 121
samux 1:fb4494783863 122 //auth
michaeljkoster 12:c5f0eac67a8a 123 sprintf(cmd, "set w a %d\r\n", state.sec);
samux 1:fb4494783863 124 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 125 continue;
samux 1:fb4494783863 126
samux 1:fb4494783863 127 // if no dhcp, set ip, netmask and gateway
samux 1:fb4494783863 128 if (!state.dhcp) {
michaeljkoster 12:c5f0eac67a8a 129 DBG("not dhcp\r\n");
samux 1:fb4494783863 130
samux 1:fb4494783863 131 sprintf(cmd, "set i a %s\r\n", ip);
samux 1:fb4494783863 132 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 133 continue;
samux 1:fb4494783863 134
michaeljkoster 12:c5f0eac67a8a 135 sprintf(cmd, "set i n %s\r\n", netmask);
samux 1:fb4494783863 136 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 137 continue;
samux 1:fb4494783863 138
samux 1:fb4494783863 139 sprintf(cmd, "set i g %s\r", gateway);
samux 1:fb4494783863 140 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 141 continue;
samux 1:fb4494783863 142 }
samux 1:fb4494783863 143
samux 1:fb4494783863 144 //key step
samux 1:fb4494783863 145 if (state.sec != NONE) {
samux 1:fb4494783863 146 if (state.sec == WPA)
michaeljkoster 12:c5f0eac67a8a 147 sprintf(cmd, "set w p %s\r\n", phrase);
samux 1:fb4494783863 148 else if (state.sec == WEP_128)
michaeljkoster 12:c5f0eac67a8a 149 sprintf(cmd, "set w k %s\r\n", phrase);
samux 1:fb4494783863 150
lz307 7:3152fcc74390 151 if (!sendCommand(cmd, "AOK", NULL, 1000))
samux 1:fb4494783863 152 continue;
samux 1:fb4494783863 153 }
samux 1:fb4494783863 154
samux 1:fb4494783863 155 //join the network
michaeljkoster 12:c5f0eac67a8a 156 sprintf(cmd, "join\r\n");
samux 1:fb4494783863 157 if (!sendCommand(cmd, "Associated", NULL, 3000))
samux 1:fb4494783863 158 continue;
lz307 7:3152fcc74390 159
lz307 7:3152fcc74390 160 if (!sendCommand("", "IP=", NULL, 10000))
lz307 7:3152fcc74390 161 continue;
samux 1:fb4494783863 162
samux 1:fb4494783863 163 if (state.dhcp) {
michaeljkoster 12:c5f0eac67a8a 164 if (!sendCommand("get i\r\n", "DHCP=ON", NULL, 3000))
samux 1:fb4494783863 165 continue;
samux 1:fb4494783863 166 }
samux 1:fb4494783863 167
michaeljkoster 12:c5f0eac67a8a 168 if (!sendCommand("save\r\n", "Stor"))
samux 2:8e54830d0df7 169 continue;
samux 2:8e54830d0df7 170
samux 1:fb4494783863 171 exit();
samux 1:fb4494783863 172
samux 1:fb4494783863 173 state.associated = true;
samux 1:fb4494783863 174 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity());
samux 1:fb4494783863 175 return true;
samux 1:fb4494783863 176 }
samux 1:fb4494783863 177 return false;
samux 1:fb4494783863 178 }
samux 1:fb4494783863 179
samux 1:fb4494783863 180
samux 1:fb4494783863 181 bool Wifly::setProtocol(Protocol p)
samux 1:fb4494783863 182 {
samux 1:fb4494783863 183 // use udp auto pairing
samux 1:fb4494783863 184 char cmd[20];
michaeljkoster 12:c5f0eac67a8a 185 sprintf(cmd, "set i p %d\r\n", p);
samux 1:fb4494783863 186 if (!sendCommand(cmd, "AOK"))
samux 1:fb4494783863 187 return false;
samux 1:fb4494783863 188
samux 1:fb4494783863 189 switch(p) {
samux 1:fb4494783863 190 case TCP:
samux 1:fb4494783863 191 // set ip flags: tcp retry enabled
michaeljkoster 12:c5f0eac67a8a 192 if (!sendCommand("set i f 0x07\r\n", "AOK"))
samux 1:fb4494783863 193 return false;
samux 1:fb4494783863 194 break;
samux 1:fb4494783863 195 case UDP:
samux 1:fb4494783863 196 // set ip flags: udp auto pairing enabled
michaeljkoster 12:c5f0eac67a8a 197 if (!sendCommand("set i h 0.0.0.0\r\n", "AOK"))
samux 1:fb4494783863 198 return false;
michaeljkoster 12:c5f0eac67a8a 199 if (!sendCommand("set i f 0x40\r\n", "AOK"))
michaeljkoster 12:c5f0eac67a8a 200 // if (!sendCommand("set i f 0x00\r\n", "AOK")) // TEST turn off autopairing
samux 1:fb4494783863 201 return false;
samux 1:fb4494783863 202 break;
samux 1:fb4494783863 203 }
samux 1:fb4494783863 204 state.proto = p;
samux 1:fb4494783863 205 return true;
samux 1:fb4494783863 206 }
samux 1:fb4494783863 207
samux 1:fb4494783863 208 char * Wifly::getStringSecurity()
samux 1:fb4494783863 209 {
samux 1:fb4494783863 210 switch(state.sec) {
samux 1:fb4494783863 211 case NONE:
samux 1:fb4494783863 212 return "NONE";
samux 1:fb4494783863 213 case WEP_128:
samux 1:fb4494783863 214 return "WEP_128";
samux 1:fb4494783863 215 case WPA:
samux 1:fb4494783863 216 return "WPA";
samux 1:fb4494783863 217 }
samux 1:fb4494783863 218 return "UNKNOWN";
samux 1:fb4494783863 219 }
samux 1:fb4494783863 220
samux 1:fb4494783863 221 bool Wifly::connect(const char * host, int port)
samux 1:fb4494783863 222 {
samux 1:fb4494783863 223 char rcv[20];
samux 1:fb4494783863 224 char cmd[20];
samux 1:fb4494783863 225
samux 2:8e54830d0df7 226 // try to open
michaeljkoster 12:c5f0eac67a8a 227 sprintf(cmd, "open %s %d\r\n", host, port);
samux 2:8e54830d0df7 228 if (sendCommand(cmd, "OPEN", NULL, 10000)) {
samux 2:8e54830d0df7 229 state.tcp = true;
samux 2:8e54830d0df7 230 state.cmd_mode = false;
samux 2:8e54830d0df7 231 return true;
samux 1:fb4494783863 232 }
samux 1:fb4494783863 233
samux 2:8e54830d0df7 234 // if failed, retry and parse the response
samux 2:8e54830d0df7 235 if (sendCommand(cmd, NULL, rcv, 5000)) {
samux 1:fb4494783863 236 if (strstr(rcv, "OPEN") == NULL) {
samux 1:fb4494783863 237 if (strstr(rcv, "Connected") != NULL) {
samux 2:8e54830d0df7 238 wait(0.25);
michaeljkoster 12:c5f0eac67a8a 239 if (!sendCommand("close\r\n", "CLOS"))
samux 1:fb4494783863 240 return false;
samux 2:8e54830d0df7 241 wait(0.25);
samux 2:8e54830d0df7 242 if (!sendCommand(cmd, "OPEN", NULL, 10000))
samux 1:fb4494783863 243 return false;
samux 1:fb4494783863 244 } else {
samux 1:fb4494783863 245 return false;
samux 1:fb4494783863 246 }
samux 1:fb4494783863 247 }
samux 1:fb4494783863 248 } else {
samux 1:fb4494783863 249 return false;
samux 1:fb4494783863 250 }
samux 2:8e54830d0df7 251
samux 1:fb4494783863 252 state.tcp = true;
samux 1:fb4494783863 253 state.cmd_mode = false;
samux 1:fb4494783863 254
samux 1:fb4494783863 255 return true;
samux 1:fb4494783863 256 }
samux 1:fb4494783863 257
samux 1:fb4494783863 258
samux 1:fb4494783863 259 bool Wifly::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 260 {
samux 1:fb4494783863 261 string h = host;
samux 1:fb4494783863 262 char cmd[30], rcv[100];
samux 1:fb4494783863 263 int l = 0;
samux 1:fb4494783863 264 char * point;
samux 1:fb4494783863 265 int nb_digits = 0;
samux 1:fb4494783863 266
samux 1:fb4494783863 267 // no dns needed
samux 1:fb4494783863 268 int pos = h.find(".");
samux 1:fb4494783863 269 if (pos != string::npos) {
samux 1:fb4494783863 270 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 271 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 272 }
samux 1:fb4494783863 273 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 274 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 275 strcpy(ip, host);
samux 1:fb4494783863 276 }
samux 1:fb4494783863 277 // dns needed
samux 1:fb4494783863 278 else {
samux 1:fb4494783863 279 nb_digits = 0;
michaeljkoster 12:c5f0eac67a8a 280 sprintf(cmd, "lookup %s\r\n", host);
samux 1:fb4494783863 281 if (!sendCommand(cmd, NULL, rcv))
samux 1:fb4494783863 282 return false;
samux 1:fb4494783863 283
samux 1:fb4494783863 284 // look for the ip address
samux 1:fb4494783863 285 char * begin = strstr(rcv, "=") + 1;
samux 1:fb4494783863 286 for (int i = 0; i < 3; i++) {
samux 1:fb4494783863 287 point = strstr(begin + l, ".");
samux 1:fb4494783863 288 DBG("str: %s", begin + l);
samux 1:fb4494783863 289 l += point - (begin + l) + 1;
samux 1:fb4494783863 290 }
samux 1:fb4494783863 291 DBG("str: %s", begin + l);
samux 1:fb4494783863 292 while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') {
samux 1:fb4494783863 293 DBG("digit: %c", *(begin + l + nb_digits));
samux 1:fb4494783863 294 nb_digits++;
samux 1:fb4494783863 295 }
samux 1:fb4494783863 296 memcpy(ip, begin, l + nb_digits);
samux 1:fb4494783863 297 ip[l+nb_digits] = 0;
samux 1:fb4494783863 298 DBG("ip from dns: %s", ip);
samux 1:fb4494783863 299 }
samux 1:fb4494783863 300 return true;
samux 1:fb4494783863 301 }
samux 1:fb4494783863 302
samux 1:fb4494783863 303
samux 1:fb4494783863 304 void Wifly::flush()
samux 1:fb4494783863 305 {
samux 1:fb4494783863 306 buf_wifly.flush();
samux 1:fb4494783863 307 }
samux 1:fb4494783863 308
samux 1:fb4494783863 309 bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
samux 1:fb4494783863 310 {
samux 1:fb4494783863 311 if (!state.cmd_mode) {
samux 1:fb4494783863 312 cmdMode();
samux 1:fb4494783863 313 }
samux 1:fb4494783863 314 if (send(cmd, strlen(cmd), ack, res, timeout) == -1) {
samux 1:fb4494783863 315 ERR("sendCommand: cannot %s\r\n", cmd);
samux 1:fb4494783863 316 exit();
samux 1:fb4494783863 317 return false;
samux 1:fb4494783863 318 }
samux 1:fb4494783863 319 return true;
samux 1:fb4494783863 320 }
samux 1:fb4494783863 321
samux 1:fb4494783863 322 bool Wifly::cmdMode()
samux 1:fb4494783863 323 {
samux 1:fb4494783863 324 // if already in cmd mode, return
samux 1:fb4494783863 325 if (state.cmd_mode)
samux 1:fb4494783863 326 return true;
samux 1:fb4494783863 327
lz307 7:3152fcc74390 328 if (send("$$$", 3, "CMD") == -1 && send("\r",1,">") != true) {
samux 1:fb4494783863 329 ERR("cannot enter in cmd mode\r\n");
samux 2:8e54830d0df7 330 exit();
samux 1:fb4494783863 331 return false;
samux 1:fb4494783863 332 }
samux 1:fb4494783863 333 state.cmd_mode = true;
samux 1:fb4494783863 334 return true;
samux 1:fb4494783863 335 }
samux 1:fb4494783863 336
samux 1:fb4494783863 337 bool Wifly::disconnect()
samux 1:fb4494783863 338 {
samux 1:fb4494783863 339 // if already disconnected, return
samux 1:fb4494783863 340 if (!state.associated)
samux 1:fb4494783863 341 return true;
samux 2:8e54830d0df7 342
samux 1:fb4494783863 343 if (!sendCommand("leave\r", "DeAuth"))
samux 1:fb4494783863 344 return false;
samux 1:fb4494783863 345 exit();
samux 2:8e54830d0df7 346
samux 1:fb4494783863 347 state.associated = false;
samux 1:fb4494783863 348 return true;
samux 1:fb4494783863 349
samux 1:fb4494783863 350 }
samux 1:fb4494783863 351
samux 1:fb4494783863 352 bool Wifly::is_connected()
samux 1:fb4494783863 353 {
samux 1:fb4494783863 354 return (tcp_status.read() == 1) ? true : false;
samux 1:fb4494783863 355 }
samux 1:fb4494783863 356
samux 1:fb4494783863 357
samux 1:fb4494783863 358 void Wifly::reset()
samux 1:fb4494783863 359 {
samux 1:fb4494783863 360 reset_pin = 0;
samux 1:fb4494783863 361 wait(0.2);
samux 1:fb4494783863 362 reset_pin = 1;
samux 1:fb4494783863 363 wait(0.2);
samux 1:fb4494783863 364 }
samux 1:fb4494783863 365
samux 3:9aa05e19c62e 366 bool Wifly::reboot()
samux 3:9aa05e19c62e 367 {
samux 3:9aa05e19c62e 368 // if already in cmd mode, return
michaeljkoster 12:c5f0eac67a8a 369 if (!sendCommand("reboot\r\n"))
samux 3:9aa05e19c62e 370 return false;
samux 3:9aa05e19c62e 371
samux 3:9aa05e19c62e 372 wait(0.3);
samux 3:9aa05e19c62e 373
samux 3:9aa05e19c62e 374 state.cmd_mode = false;
samux 3:9aa05e19c62e 375 return true;
samux 3:9aa05e19c62e 376 }
samux 3:9aa05e19c62e 377
samux 1:fb4494783863 378 bool Wifly::close()
samux 1:fb4494783863 379 {
samux 1:fb4494783863 380 // if not connected, return
samux 1:fb4494783863 381 if (!state.tcp)
samux 1:fb4494783863 382 return true;
samux 2:8e54830d0df7 383
samux 1:fb4494783863 384 wait(0.25);
michaeljkoster 12:c5f0eac67a8a 385 if (!sendCommand("close\r\n", "CLOS"))
samux 1:fb4494783863 386 return false;
samux 1:fb4494783863 387 exit();
samux 2:8e54830d0df7 388
samux 1:fb4494783863 389 state.tcp = false;
samux 1:fb4494783863 390 return true;
samux 1:fb4494783863 391 }
samux 1:fb4494783863 392
samux 1:fb4494783863 393
samux 1:fb4494783863 394 int Wifly::putc(char c)
samux 1:fb4494783863 395 {
samux 1:fb4494783863 396 while (!wifi.writeable());
samux 1:fb4494783863 397 return wifi.putc(c);
samux 1:fb4494783863 398 }
samux 1:fb4494783863 399
samux 1:fb4494783863 400
samux 1:fb4494783863 401 bool Wifly::exit()
samux 1:fb4494783863 402 {
samux 1:fb4494783863 403 flush();
samux 1:fb4494783863 404 if (!state.cmd_mode)
samux 1:fb4494783863 405 return true;
michaeljkoster 12:c5f0eac67a8a 406 if (!sendCommand("exit\r\n", "EXIT"))
samux 1:fb4494783863 407 return false;
samux 1:fb4494783863 408 state.cmd_mode = false;
samux 1:fb4494783863 409 flush();
samux 1:fb4494783863 410 return true;
samux 1:fb4494783863 411 }
samux 1:fb4494783863 412
samux 1:fb4494783863 413
samux 1:fb4494783863 414 int Wifly::readable()
samux 1:fb4494783863 415 {
samux 1:fb4494783863 416 return buf_wifly.available();
samux 1:fb4494783863 417 }
samux 1:fb4494783863 418
samux 1:fb4494783863 419 int Wifly::writeable()
samux 1:fb4494783863 420 {
samux 1:fb4494783863 421 return wifi.writeable();
samux 1:fb4494783863 422 }
samux 1:fb4494783863 423
samux 1:fb4494783863 424 char Wifly::getc()
samux 1:fb4494783863 425 {
samux 1:fb4494783863 426 char c;
samux 1:fb4494783863 427 while (!buf_wifly.available());
samux 1:fb4494783863 428 buf_wifly.dequeue(&c);
samux 1:fb4494783863 429 return c;
samux 1:fb4494783863 430 }
samux 1:fb4494783863 431
samux 1:fb4494783863 432 void Wifly::handler_rx(void)
samux 1:fb4494783863 433 {
samux 1:fb4494783863 434 //read characters
samux 1:fb4494783863 435 while (wifi.readable())
samux 1:fb4494783863 436 buf_wifly.queue(wifi.getc());
samux 1:fb4494783863 437 }
samux 1:fb4494783863 438
samux 1:fb4494783863 439 void Wifly::attach_rx(bool callback)
samux 1:fb4494783863 440 {
samux 1:fb4494783863 441 if (!callback)
samux 1:fb4494783863 442 wifi.attach(NULL);
samux 1:fb4494783863 443 else
samux 1:fb4494783863 444 wifi.attach(this, &Wifly::handler_rx);
samux 1:fb4494783863 445 }
samux 1:fb4494783863 446
samux 1:fb4494783863 447
samux 1:fb4494783863 448 int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 449 {
samux 1:fb4494783863 450 char read;
samux 1:fb4494783863 451 size_t found = string::npos;
samux 1:fb4494783863 452 string checking;
samux 1:fb4494783863 453 Timer tmr;
samux 1:fb4494783863 454 int result = 0;
samux 1:fb4494783863 455
samux 1:fb4494783863 456 DBG("will send: %s\r\n",str);
samux 1:fb4494783863 457
samux 1:fb4494783863 458 attach_rx(false);
samux 1:fb4494783863 459
samux 1:fb4494783863 460 //We flush the buffer
samux 1:fb4494783863 461 while (wifi.readable())
samux 1:fb4494783863 462 wifi.getc();
samux 1:fb4494783863 463
samux 1:fb4494783863 464 if (!ACK || !strcmp(ACK, "NO")) {
samux 1:fb4494783863 465 for (int i = 0; i < len; i++)
samux 1:fb4494783863 466 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 467 } else {
samux 1:fb4494783863 468 //We flush the buffer
samux 1:fb4494783863 469 while (wifi.readable())
samux 1:fb4494783863 470 wifi.getc();
samux 1:fb4494783863 471
samux 1:fb4494783863 472 tmr.start();
samux 1:fb4494783863 473 for (int i = 0; i < len; i++)
samux 1:fb4494783863 474 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 475
samux 1:fb4494783863 476 while (1) {
samux 1:fb4494783863 477 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 478 //We flush the buffer
samux 1:fb4494783863 479 while (wifi.readable())
samux 1:fb4494783863 480 wifi.getc();
samux 1:fb4494783863 481
samux 1:fb4494783863 482 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 483
samux 1:fb4494783863 484 attach_rx(true);
samux 1:fb4494783863 485 return -1;
samux 1:fb4494783863 486 } else if (wifi.readable()) {
samux 1:fb4494783863 487 read = wifi.getc();
samux 1:fb4494783863 488 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 489 checking += read;
samux 1:fb4494783863 490 found = checking.find(ACK);
samux 1:fb4494783863 491 if (found != string::npos) {
samux 1:fb4494783863 492 wait(0.01);
samux 1:fb4494783863 493
samux 1:fb4494783863 494 //We flush the buffer
samux 1:fb4494783863 495 while (wifi.readable())
samux 1:fb4494783863 496 wifi.getc();
samux 1:fb4494783863 497
samux 1:fb4494783863 498 break;
samux 1:fb4494783863 499 }
samux 1:fb4494783863 500 }
samux 1:fb4494783863 501 }
samux 1:fb4494783863 502 }
samux 1:fb4494783863 503 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 504
samux 1:fb4494783863 505 attach_rx(true);
samux 1:fb4494783863 506 return result;
samux 1:fb4494783863 507 }
samux 1:fb4494783863 508
samux 1:fb4494783863 509 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 510 if ( res != NULL) {
samux 1:fb4494783863 511 int i = 0;
samux 1:fb4494783863 512 Timer timeout;
samux 1:fb4494783863 513 timeout.start();
samux 1:fb4494783863 514 tmr.reset();
samux 1:fb4494783863 515 while (1) {
samux 1:fb4494783863 516 if (timeout.read() > 2) {
samux 1:fb4494783863 517 if (i == 0) {
samux 1:fb4494783863 518 res = NULL;
samux 1:fb4494783863 519 break;
samux 1:fb4494783863 520 }
samux 1:fb4494783863 521 res[i] = '\0';
samux 1:fb4494783863 522 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 523
samux 1:fb4494783863 524 break;
samux 1:fb4494783863 525 } else {
samux 1:fb4494783863 526 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 527 res[i] = '\0';
samux 1:fb4494783863 528 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 529
samux 1:fb4494783863 530 break;
samux 1:fb4494783863 531 }
samux 1:fb4494783863 532 if (wifi.readable()) {
samux 1:fb4494783863 533 tmr.start();
samux 1:fb4494783863 534 read = wifi.getc();
samux 1:fb4494783863 535
samux 1:fb4494783863 536 // we drop \r and \n
samux 1:fb4494783863 537 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 538 res[i++] = read;
samux 1:fb4494783863 539 }
samux 1:fb4494783863 540 }
samux 1:fb4494783863 541 }
samux 1:fb4494783863 542 }
samux 1:fb4494783863 543 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 544 }
samux 1:fb4494783863 545
samux 1:fb4494783863 546 //We flush the buffer
samux 1:fb4494783863 547 while (wifi.readable())
samux 1:fb4494783863 548 wifi.getc();
samux 1:fb4494783863 549
samux 1:fb4494783863 550 attach_rx(true);
samux 1:fb4494783863 551 DBG("result: %d\r\n", result)
samux 1:fb4494783863 552 return result;
samux 1:fb4494783863 553 }