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