wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Fri Aug 24 13:25:55 2012 +0000
Revision:
13:108340829acc
Parent:
12:5ffaed1456c8
reduce buffer size

Who changed what in which revision?

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