Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IoT_Ex BatteryModelTester BatteryModelTester
Fork of WiflyInterface by
Wifly/Wifly.cpp@18:cf3bd54703a5, 2016-03-25 (annotated)
- Committer:
- defrost
- Date:
- Fri Mar 25 11:14:33 2016 +0000
- Revision:
- 18:cf3bd54703a5
- Parent:
- 16:de16e8b077c1
- Child:
- 20:3616a765c1b8
- Added more debug statements; - increased the size of the rcv and cmd buffers in some commands to accomodate the string lengths that can be expected. For example: 'open 192.168.111.123 12345' contains 26 chars
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> |
defrost | 18:cf3bd54703a5 | 23 | #define DEBUG |
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 | |
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 | |
defrost | 15:eaa1ec0e26bf | 68 | void Wifly::setBaud(int baudrate) |
defrost | 15:eaa1ec0e26bf | 69 | { |
defrost | 18:cf3bd54703a5 | 70 | char cmd[28]; |
defrost | 15:eaa1ec0e26bf | 71 | // This sets the baud rate 'instantly' |
defrost | 15:eaa1ec0e26bf | 72 | sprintf(cmd, "set u i %d\r", baudrate); |
defrost | 15:eaa1ec0e26bf | 73 | // This one sets it some other way that does not work |
defrost | 15:eaa1ec0e26bf | 74 | //sprintf(cmd, "set u b %d\r", baudrate); |
defrost | 15:eaa1ec0e26bf | 75 | // Set baud rate of wifly: |
defrost | 16:de16e8b077c1 | 76 | sendCommand(cmd, NULL); |
defrost | 16:de16e8b077c1 | 77 | wait(0.5); |
defrost | 15:eaa1ec0e26bf | 78 | // Set baud rate of UART: |
defrost | 15:eaa1ec0e26bf | 79 | wifi.baud(baudrate); |
defrost | 16:de16e8b077c1 | 80 | wait(0.5); |
defrost | 15:eaa1ec0e26bf | 81 | exit(); |
defrost | 15:eaa1ec0e26bf | 82 | } |
defrost | 15:eaa1ec0e26bf | 83 | |
samux | 1:fb4494783863 | 84 | bool Wifly::join() |
samux | 1:fb4494783863 | 85 | { |
defrost | 18:cf3bd54703a5 | 86 | char cmd[28]; |
samux | 1:fb4494783863 | 87 | |
samux | 1:fb4494783863 | 88 | for (int i= 0; i < MAX_TRY_JOIN; i++) { |
samux | 2:8e54830d0df7 | 89 | |
samux | 2:8e54830d0df7 | 90 | // no auto join |
samux | 2:8e54830d0df7 | 91 | if (!sendCommand("set w j 0\r", "AOK")) |
samux | 2:8e54830d0df7 | 92 | continue; |
samux | 2:8e54830d0df7 | 93 | |
samux | 2:8e54830d0df7 | 94 | //no echo |
samux | 2:8e54830d0df7 | 95 | if (!sendCommand("set u m 1\r", "AOK")) |
samux | 2:8e54830d0df7 | 96 | continue; |
samux | 2:8e54830d0df7 | 97 | |
samux | 1:fb4494783863 | 98 | // set time |
samux | 4:0bcec6272784 | 99 | if (!sendCommand("set c t 30\r", "AOK")) |
samux | 1:fb4494783863 | 100 | continue; |
samux | 1:fb4494783863 | 101 | |
samux | 1:fb4494783863 | 102 | // set size |
samux | 4:0bcec6272784 | 103 | if (!sendCommand("set c s 1024\r", "AOK")) |
samux | 1:fb4494783863 | 104 | continue; |
samux | 1:fb4494783863 | 105 | |
samux | 1:fb4494783863 | 106 | // red led on when tcp connection active |
samux | 1:fb4494783863 | 107 | if (!sendCommand("set s i 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 108 | continue; |
samux | 1:fb4494783863 | 109 | |
samux | 1:fb4494783863 | 110 | // no string sent to the tcp client |
samux | 1:fb4494783863 | 111 | if (!sendCommand("set c r 0\r", "AOK")) |
samux | 1:fb4494783863 | 112 | continue; |
samux | 1:fb4494783863 | 113 | |
samux | 1:fb4494783863 | 114 | // tcp protocol |
samux | 1:fb4494783863 | 115 | if (!sendCommand("set i p 2\r", "AOK")) |
samux | 1:fb4494783863 | 116 | continue; |
samux | 1:fb4494783863 | 117 | |
samux | 1:fb4494783863 | 118 | // tcp retry |
samux | 1:fb4494783863 | 119 | if (!sendCommand("set i f 0x7\r", "AOK")) |
samux | 1:fb4494783863 | 120 | continue; |
samux | 2:8e54830d0df7 | 121 | |
samux | 2:8e54830d0df7 | 122 | // set dns server |
samux | 2:8e54830d0df7 | 123 | if (!sendCommand("set d n rn.microchip.com\r", "AOK")) |
samux | 1:fb4494783863 | 124 | continue; |
samux | 1:fb4494783863 | 125 | |
samux | 1:fb4494783863 | 126 | //dhcp |
samux | 1:fb4494783863 | 127 | sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0); |
samux | 1:fb4494783863 | 128 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 129 | continue; |
samux | 1:fb4494783863 | 130 | |
samux | 1:fb4494783863 | 131 | // ssid |
samux | 1:fb4494783863 | 132 | sprintf(cmd, "set w s %s\r", ssid); |
lz307 | 7:3152fcc74390 | 133 | if (!sendCommand(cmd, "AOK", NULL, 1000)) |
samux | 1:fb4494783863 | 134 | continue; |
samux | 1:fb4494783863 | 135 | |
samux | 1:fb4494783863 | 136 | //auth |
samux | 1:fb4494783863 | 137 | sprintf(cmd, "set w a %d\r", state.sec); |
samux | 1:fb4494783863 | 138 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 139 | continue; |
samux | 1:fb4494783863 | 140 | |
samux | 1:fb4494783863 | 141 | // if no dhcp, set ip, netmask and gateway |
samux | 1:fb4494783863 | 142 | if (!state.dhcp) { |
samux | 1:fb4494783863 | 143 | DBG("not dhcp\r"); |
samux | 1:fb4494783863 | 144 | |
samux | 1:fb4494783863 | 145 | sprintf(cmd, "set i a %s\r\n", ip); |
samux | 1:fb4494783863 | 146 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 147 | continue; |
samux | 1:fb4494783863 | 148 | |
samux | 1:fb4494783863 | 149 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 1:fb4494783863 | 150 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 151 | continue; |
samux | 1:fb4494783863 | 152 | |
samux | 1:fb4494783863 | 153 | sprintf(cmd, "set i g %s\r", gateway); |
samux | 1:fb4494783863 | 154 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 155 | continue; |
samux | 1:fb4494783863 | 156 | } |
samux | 1:fb4494783863 | 157 | |
samux | 1:fb4494783863 | 158 | //key step |
samux | 1:fb4494783863 | 159 | if (state.sec != NONE) { |
defrost | 12:1aaea7f302e6 | 160 | if ((state.sec == WPA)||(state.sec == WPA2)) |
samux | 1:fb4494783863 | 161 | sprintf(cmd, "set w p %s\r", phrase); |
samux | 1:fb4494783863 | 162 | else if (state.sec == WEP_128) |
samux | 1:fb4494783863 | 163 | sprintf(cmd, "set w k %s\r", phrase); |
samux | 1:fb4494783863 | 164 | |
lz307 | 7:3152fcc74390 | 165 | if (!sendCommand(cmd, "AOK", NULL, 1000)) |
samux | 1:fb4494783863 | 166 | continue; |
samux | 1:fb4494783863 | 167 | } |
samux | 1:fb4494783863 | 168 | |
samux | 1:fb4494783863 | 169 | //join the network |
samux | 1:fb4494783863 | 170 | sprintf(cmd, "join\r"); |
samux | 1:fb4494783863 | 171 | if (!sendCommand(cmd, "Associated", NULL, 3000)) |
samux | 1:fb4494783863 | 172 | continue; |
lz307 | 7:3152fcc74390 | 173 | |
lz307 | 7:3152fcc74390 | 174 | if (!sendCommand("", "IP=", NULL, 10000)) |
lz307 | 7:3152fcc74390 | 175 | continue; |
samux | 1:fb4494783863 | 176 | |
samux | 1:fb4494783863 | 177 | if (state.dhcp) { |
lz307 | 7:3152fcc74390 | 178 | if (!sendCommand("get i\r", "DHCP=ON", NULL, 3000)) |
samux | 1:fb4494783863 | 179 | continue; |
samux | 1:fb4494783863 | 180 | } |
samux | 1:fb4494783863 | 181 | |
samux | 2:8e54830d0df7 | 182 | if (!sendCommand("save\r", "Stor")) |
samux | 2:8e54830d0df7 | 183 | continue; |
samux | 2:8e54830d0df7 | 184 | |
samux | 1:fb4494783863 | 185 | exit(); |
samux | 1:fb4494783863 | 186 | |
samux | 1:fb4494783863 | 187 | state.associated = true; |
samux | 1:fb4494783863 | 188 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity()); |
defrost | 13:8846f12fa277 | 189 | |
samux | 1:fb4494783863 | 190 | return true; |
samux | 1:fb4494783863 | 191 | } |
samux | 1:fb4494783863 | 192 | return false; |
samux | 1:fb4494783863 | 193 | } |
samux | 1:fb4494783863 | 194 | |
samux | 1:fb4494783863 | 195 | |
samux | 1:fb4494783863 | 196 | bool Wifly::setProtocol(Protocol p) |
samux | 1:fb4494783863 | 197 | { |
samux | 1:fb4494783863 | 198 | // use udp auto pairing |
samux | 1:fb4494783863 | 199 | char cmd[20]; |
samux | 1:fb4494783863 | 200 | sprintf(cmd, "set i p %d\r", p); |
samux | 1:fb4494783863 | 201 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 202 | return false; |
samux | 1:fb4494783863 | 203 | |
samux | 1:fb4494783863 | 204 | switch(p) { |
samux | 1:fb4494783863 | 205 | case TCP: |
samux | 1:fb4494783863 | 206 | // set ip flags: tcp retry enabled |
samux | 1:fb4494783863 | 207 | if (!sendCommand("set i f 0x07\r", "AOK")) |
samux | 1:fb4494783863 | 208 | return false; |
samux | 1:fb4494783863 | 209 | break; |
samux | 1:fb4494783863 | 210 | case UDP: |
samux | 1:fb4494783863 | 211 | // set ip flags: udp auto pairing enabled |
samux | 1:fb4494783863 | 212 | if (!sendCommand("set i h 0.0.0.0\r", "AOK")) |
samux | 1:fb4494783863 | 213 | return false; |
samux | 4:0bcec6272784 | 214 | if (!sendCommand("set i f 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 215 | return false; |
samux | 1:fb4494783863 | 216 | break; |
samux | 1:fb4494783863 | 217 | } |
samux | 1:fb4494783863 | 218 | state.proto = p; |
samux | 1:fb4494783863 | 219 | return true; |
samux | 1:fb4494783863 | 220 | } |
samux | 1:fb4494783863 | 221 | |
samux | 1:fb4494783863 | 222 | char * Wifly::getStringSecurity() |
samux | 1:fb4494783863 | 223 | { |
samux | 1:fb4494783863 | 224 | switch(state.sec) { |
samux | 1:fb4494783863 | 225 | case NONE: |
samux | 1:fb4494783863 | 226 | return "NONE"; |
samux | 1:fb4494783863 | 227 | case WEP_128: |
samux | 1:fb4494783863 | 228 | return "WEP_128"; |
samux | 1:fb4494783863 | 229 | case WPA: |
samux | 1:fb4494783863 | 230 | return "WPA"; |
defrost | 13:8846f12fa277 | 231 | case WPA2: |
defrost | 13:8846f12fa277 | 232 | return "WPA2"; |
samux | 1:fb4494783863 | 233 | } |
samux | 1:fb4494783863 | 234 | return "UNKNOWN"; |
samux | 1:fb4494783863 | 235 | } |
samux | 1:fb4494783863 | 236 | |
samux | 1:fb4494783863 | 237 | bool Wifly::connect(const char * host, int port) |
samux | 1:fb4494783863 | 238 | { |
defrost | 18:cf3bd54703a5 | 239 | char rcv[28]; |
defrost | 18:cf3bd54703a5 | 240 | char cmd[28]; |
defrost | 12:1aaea7f302e6 | 241 | |
defrost | 12:1aaea7f302e6 | 242 | |
samux | 1:fb4494783863 | 243 | |
samux | 2:8e54830d0df7 | 244 | // try to open |
samux | 2:8e54830d0df7 | 245 | sprintf(cmd, "open %s %d\r", host, port); |
defrost | 18:cf3bd54703a5 | 246 | DBG("CMD1: %s\n\r",cmd); |
defrost | 18:cf3bd54703a5 | 247 | if (sendCommand(cmd, "OPEN", rcv, 10000)) { |
samux | 2:8e54830d0df7 | 248 | state.tcp = true; |
samux | 2:8e54830d0df7 | 249 | state.cmd_mode = false; |
samux | 2:8e54830d0df7 | 250 | return true; |
samux | 1:fb4494783863 | 251 | } |
defrost | 18:cf3bd54703a5 | 252 | DBG("CMD2: %s\n\r",cmd); |
defrost | 18:cf3bd54703a5 | 253 | DBG("CMD4: %s\n\r",rcv); |
samux | 2:8e54830d0df7 | 254 | // if failed, retry and parse the response |
samux | 2:8e54830d0df7 | 255 | if (sendCommand(cmd, NULL, rcv, 5000)) { |
samux | 1:fb4494783863 | 256 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 1:fb4494783863 | 257 | if (strstr(rcv, "Connected") != NULL) { |
samux | 2:8e54830d0df7 | 258 | wait(0.25); |
samux | 1:fb4494783863 | 259 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 260 | return false; |
samux | 2:8e54830d0df7 | 261 | wait(0.25); |
samux | 2:8e54830d0df7 | 262 | if (!sendCommand(cmd, "OPEN", NULL, 10000)) |
samux | 1:fb4494783863 | 263 | return false; |
samux | 1:fb4494783863 | 264 | } else { |
samux | 1:fb4494783863 | 265 | return false; |
samux | 1:fb4494783863 | 266 | } |
samux | 1:fb4494783863 | 267 | } |
samux | 1:fb4494783863 | 268 | } else { |
samux | 1:fb4494783863 | 269 | return false; |
samux | 1:fb4494783863 | 270 | } |
defrost | 12:1aaea7f302e6 | 271 | |
samux | 2:8e54830d0df7 | 272 | |
samux | 1:fb4494783863 | 273 | state.tcp = true; |
samux | 1:fb4494783863 | 274 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 275 | |
samux | 1:fb4494783863 | 276 | return true; |
samux | 1:fb4494783863 | 277 | } |
samux | 1:fb4494783863 | 278 | |
samux | 1:fb4494783863 | 279 | |
samux | 1:fb4494783863 | 280 | bool Wifly::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 281 | { |
samux | 1:fb4494783863 | 282 | string h = host; |
samux | 1:fb4494783863 | 283 | char cmd[30], rcv[100]; |
samux | 1:fb4494783863 | 284 | int l = 0; |
samux | 1:fb4494783863 | 285 | char * point; |
samux | 1:fb4494783863 | 286 | int nb_digits = 0; |
samux | 1:fb4494783863 | 287 | |
samux | 1:fb4494783863 | 288 | // no dns needed |
samux | 1:fb4494783863 | 289 | int pos = h.find("."); |
samux | 1:fb4494783863 | 290 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 291 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 292 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 293 | } |
samux | 1:fb4494783863 | 294 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 1:fb4494783863 | 295 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 296 | strcpy(ip, host); |
samux | 1:fb4494783863 | 297 | } |
samux | 1:fb4494783863 | 298 | // dns needed |
samux | 1:fb4494783863 | 299 | else { |
samux | 1:fb4494783863 | 300 | nb_digits = 0; |
samux | 1:fb4494783863 | 301 | sprintf(cmd, "lookup %s\r", host); |
samux | 1:fb4494783863 | 302 | if (!sendCommand(cmd, NULL, rcv)) |
samux | 1:fb4494783863 | 303 | return false; |
samux | 1:fb4494783863 | 304 | |
samux | 1:fb4494783863 | 305 | // look for the ip address |
samux | 1:fb4494783863 | 306 | char * begin = strstr(rcv, "=") + 1; |
samux | 1:fb4494783863 | 307 | for (int i = 0; i < 3; i++) { |
samux | 1:fb4494783863 | 308 | point = strstr(begin + l, "."); |
samux | 1:fb4494783863 | 309 | DBG("str: %s", begin + l); |
samux | 1:fb4494783863 | 310 | l += point - (begin + l) + 1; |
samux | 1:fb4494783863 | 311 | } |
samux | 1:fb4494783863 | 312 | DBG("str: %s", begin + l); |
samux | 1:fb4494783863 | 313 | while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') { |
samux | 1:fb4494783863 | 314 | DBG("digit: %c", *(begin + l + nb_digits)); |
samux | 1:fb4494783863 | 315 | nb_digits++; |
samux | 1:fb4494783863 | 316 | } |
samux | 1:fb4494783863 | 317 | memcpy(ip, begin, l + nb_digits); |
samux | 1:fb4494783863 | 318 | ip[l+nb_digits] = 0; |
samux | 1:fb4494783863 | 319 | DBG("ip from dns: %s", ip); |
samux | 1:fb4494783863 | 320 | } |
samux | 1:fb4494783863 | 321 | return true; |
samux | 1:fb4494783863 | 322 | } |
samux | 1:fb4494783863 | 323 | |
samux | 1:fb4494783863 | 324 | |
samux | 1:fb4494783863 | 325 | void Wifly::flush() |
samux | 1:fb4494783863 | 326 | { |
samux | 1:fb4494783863 | 327 | buf_wifly.flush(); |
samux | 1:fb4494783863 | 328 | } |
samux | 1:fb4494783863 | 329 | |
samux | 1:fb4494783863 | 330 | bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) |
samux | 1:fb4494783863 | 331 | { |
samux | 1:fb4494783863 | 332 | if (!state.cmd_mode) { |
samux | 1:fb4494783863 | 333 | cmdMode(); |
samux | 1:fb4494783863 | 334 | } |
samux | 1:fb4494783863 | 335 | if (send(cmd, strlen(cmd), ack, res, timeout) == -1) { |
samux | 1:fb4494783863 | 336 | ERR("sendCommand: cannot %s\r\n", cmd); |
samux | 1:fb4494783863 | 337 | exit(); |
samux | 1:fb4494783863 | 338 | return false; |
samux | 1:fb4494783863 | 339 | } |
samux | 1:fb4494783863 | 340 | return true; |
samux | 1:fb4494783863 | 341 | } |
samux | 1:fb4494783863 | 342 | |
samux | 1:fb4494783863 | 343 | bool Wifly::cmdMode() |
samux | 1:fb4494783863 | 344 | { |
samux | 1:fb4494783863 | 345 | // if already in cmd mode, return |
samux | 1:fb4494783863 | 346 | if (state.cmd_mode) |
samux | 1:fb4494783863 | 347 | return true; |
samux | 1:fb4494783863 | 348 | |
lz307 | 7:3152fcc74390 | 349 | if (send("$$$", 3, "CMD") == -1 && send("\r",1,">") != true) { |
samux | 1:fb4494783863 | 350 | ERR("cannot enter in cmd mode\r\n"); |
samux | 2:8e54830d0df7 | 351 | exit(); |
samux | 1:fb4494783863 | 352 | return false; |
samux | 1:fb4494783863 | 353 | } |
samux | 1:fb4494783863 | 354 | state.cmd_mode = true; |
samux | 1:fb4494783863 | 355 | return true; |
samux | 1:fb4494783863 | 356 | } |
samux | 1:fb4494783863 | 357 | |
samux | 1:fb4494783863 | 358 | bool Wifly::disconnect() |
samux | 1:fb4494783863 | 359 | { |
samux | 1:fb4494783863 | 360 | // if already disconnected, return |
samux | 1:fb4494783863 | 361 | if (!state.associated) |
samux | 1:fb4494783863 | 362 | return true; |
samux | 2:8e54830d0df7 | 363 | |
samux | 1:fb4494783863 | 364 | if (!sendCommand("leave\r", "DeAuth")) |
samux | 1:fb4494783863 | 365 | return false; |
samux | 1:fb4494783863 | 366 | exit(); |
samux | 2:8e54830d0df7 | 367 | |
samux | 1:fb4494783863 | 368 | state.associated = false; |
samux | 1:fb4494783863 | 369 | return true; |
samux | 1:fb4494783863 | 370 | |
samux | 1:fb4494783863 | 371 | } |
samux | 1:fb4494783863 | 372 | |
samux | 1:fb4494783863 | 373 | bool Wifly::is_connected() |
samux | 1:fb4494783863 | 374 | { |
samux | 1:fb4494783863 | 375 | return (tcp_status.read() == 1) ? true : false; |
samux | 1:fb4494783863 | 376 | } |
samux | 1:fb4494783863 | 377 | |
samux | 1:fb4494783863 | 378 | |
samux | 1:fb4494783863 | 379 | void Wifly::reset() |
samux | 1:fb4494783863 | 380 | { |
samux | 1:fb4494783863 | 381 | reset_pin = 0; |
samux | 1:fb4494783863 | 382 | wait(0.2); |
samux | 1:fb4494783863 | 383 | reset_pin = 1; |
samux | 1:fb4494783863 | 384 | wait(0.2); |
samux | 1:fb4494783863 | 385 | } |
samux | 1:fb4494783863 | 386 | |
samux | 3:9aa05e19c62e | 387 | bool Wifly::reboot() |
samux | 3:9aa05e19c62e | 388 | { |
samux | 3:9aa05e19c62e | 389 | // if already in cmd mode, return |
samux | 3:9aa05e19c62e | 390 | if (!sendCommand("reboot\r")) |
samux | 3:9aa05e19c62e | 391 | return false; |
samux | 3:9aa05e19c62e | 392 | |
samux | 3:9aa05e19c62e | 393 | wait(0.3); |
samux | 3:9aa05e19c62e | 394 | |
samux | 3:9aa05e19c62e | 395 | state.cmd_mode = false; |
samux | 3:9aa05e19c62e | 396 | return true; |
samux | 3:9aa05e19c62e | 397 | } |
samux | 3:9aa05e19c62e | 398 | |
samux | 1:fb4494783863 | 399 | bool Wifly::close() |
samux | 1:fb4494783863 | 400 | { |
samux | 1:fb4494783863 | 401 | // if not connected, return |
samux | 1:fb4494783863 | 402 | if (!state.tcp) |
samux | 1:fb4494783863 | 403 | return true; |
samux | 2:8e54830d0df7 | 404 | |
samux | 1:fb4494783863 | 405 | wait(0.25); |
samux | 1:fb4494783863 | 406 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 407 | return false; |
samux | 1:fb4494783863 | 408 | exit(); |
samux | 2:8e54830d0df7 | 409 | |
samux | 1:fb4494783863 | 410 | state.tcp = false; |
samux | 1:fb4494783863 | 411 | return true; |
samux | 1:fb4494783863 | 412 | } |
samux | 1:fb4494783863 | 413 | |
samux | 1:fb4494783863 | 414 | |
samux | 1:fb4494783863 | 415 | int Wifly::putc(char c) |
samux | 1:fb4494783863 | 416 | { |
samux | 1:fb4494783863 | 417 | while (!wifi.writeable()); |
samux | 1:fb4494783863 | 418 | return wifi.putc(c); |
samux | 1:fb4494783863 | 419 | } |
samux | 1:fb4494783863 | 420 | |
samux | 1:fb4494783863 | 421 | |
samux | 1:fb4494783863 | 422 | bool Wifly::exit() |
samux | 1:fb4494783863 | 423 | { |
samux | 1:fb4494783863 | 424 | flush(); |
samux | 1:fb4494783863 | 425 | if (!state.cmd_mode) |
samux | 1:fb4494783863 | 426 | return true; |
samux | 1:fb4494783863 | 427 | if (!sendCommand("exit\r", "EXIT")) |
samux | 1:fb4494783863 | 428 | return false; |
samux | 1:fb4494783863 | 429 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 430 | flush(); |
samux | 1:fb4494783863 | 431 | return true; |
samux | 1:fb4494783863 | 432 | } |
samux | 1:fb4494783863 | 433 | |
samux | 1:fb4494783863 | 434 | |
samux | 1:fb4494783863 | 435 | int Wifly::readable() |
samux | 1:fb4494783863 | 436 | { |
samux | 1:fb4494783863 | 437 | return buf_wifly.available(); |
samux | 1:fb4494783863 | 438 | } |
samux | 1:fb4494783863 | 439 | |
samux | 1:fb4494783863 | 440 | int Wifly::writeable() |
samux | 1:fb4494783863 | 441 | { |
samux | 1:fb4494783863 | 442 | return wifi.writeable(); |
samux | 1:fb4494783863 | 443 | } |
samux | 1:fb4494783863 | 444 | |
samux | 1:fb4494783863 | 445 | char Wifly::getc() |
samux | 1:fb4494783863 | 446 | { |
samux | 1:fb4494783863 | 447 | char c; |
samux | 1:fb4494783863 | 448 | while (!buf_wifly.available()); |
samux | 1:fb4494783863 | 449 | buf_wifly.dequeue(&c); |
samux | 1:fb4494783863 | 450 | return c; |
samux | 1:fb4494783863 | 451 | } |
samux | 1:fb4494783863 | 452 | |
samux | 1:fb4494783863 | 453 | void Wifly::handler_rx(void) |
samux | 1:fb4494783863 | 454 | { |
samux | 1:fb4494783863 | 455 | //read characters |
samux | 1:fb4494783863 | 456 | while (wifi.readable()) |
samux | 1:fb4494783863 | 457 | buf_wifly.queue(wifi.getc()); |
samux | 1:fb4494783863 | 458 | } |
samux | 1:fb4494783863 | 459 | |
samux | 1:fb4494783863 | 460 | void Wifly::attach_rx(bool callback) |
samux | 1:fb4494783863 | 461 | { |
samux | 1:fb4494783863 | 462 | if (!callback) |
samux | 1:fb4494783863 | 463 | wifi.attach(NULL); |
samux | 1:fb4494783863 | 464 | else |
samux | 1:fb4494783863 | 465 | wifi.attach(this, &Wifly::handler_rx); |
samux | 1:fb4494783863 | 466 | } |
samux | 1:fb4494783863 | 467 | |
samux | 1:fb4494783863 | 468 | |
samux | 1:fb4494783863 | 469 | int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 470 | { |
samux | 1:fb4494783863 | 471 | char read; |
samux | 1:fb4494783863 | 472 | size_t found = string::npos; |
samux | 1:fb4494783863 | 473 | string checking; |
samux | 1:fb4494783863 | 474 | Timer tmr; |
samux | 1:fb4494783863 | 475 | int result = 0; |
samux | 1:fb4494783863 | 476 | |
samux | 1:fb4494783863 | 477 | DBG("will send: %s\r\n",str); |
samux | 1:fb4494783863 | 478 | |
samux | 1:fb4494783863 | 479 | attach_rx(false); |
samux | 1:fb4494783863 | 480 | |
samux | 1:fb4494783863 | 481 | //We flush the buffer |
samux | 1:fb4494783863 | 482 | while (wifi.readable()) |
samux | 1:fb4494783863 | 483 | wifi.getc(); |
samux | 1:fb4494783863 | 484 | |
samux | 1:fb4494783863 | 485 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 1:fb4494783863 | 486 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 487 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 488 | } else { |
samux | 1:fb4494783863 | 489 | //We flush the buffer |
samux | 1:fb4494783863 | 490 | while (wifi.readable()) |
samux | 1:fb4494783863 | 491 | wifi.getc(); |
samux | 1:fb4494783863 | 492 | |
samux | 1:fb4494783863 | 493 | tmr.start(); |
samux | 1:fb4494783863 | 494 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 495 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 496 | |
samux | 1:fb4494783863 | 497 | while (1) { |
samux | 1:fb4494783863 | 498 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 499 | //We flush the buffer |
samux | 1:fb4494783863 | 500 | while (wifi.readable()) |
samux | 1:fb4494783863 | 501 | wifi.getc(); |
samux | 1:fb4494783863 | 502 | |
samux | 1:fb4494783863 | 503 | DBG("check: %s\r\n", checking.c_str()); |
defrost | 18:cf3bd54703a5 | 504 | DBG("result ignored: %d\r\n", result) |
samux | 1:fb4494783863 | 505 | attach_rx(true); |
samux | 1:fb4494783863 | 506 | return -1; |
samux | 1:fb4494783863 | 507 | } else if (wifi.readable()) { |
samux | 1:fb4494783863 | 508 | read = wifi.getc(); |
samux | 1:fb4494783863 | 509 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 510 | checking += read; |
samux | 1:fb4494783863 | 511 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 512 | if (found != string::npos) { |
samux | 1:fb4494783863 | 513 | wait(0.01); |
samux | 1:fb4494783863 | 514 | |
samux | 1:fb4494783863 | 515 | //We flush the buffer |
samux | 1:fb4494783863 | 516 | while (wifi.readable()) |
samux | 1:fb4494783863 | 517 | wifi.getc(); |
samux | 1:fb4494783863 | 518 | |
samux | 1:fb4494783863 | 519 | break; |
samux | 1:fb4494783863 | 520 | } |
samux | 1:fb4494783863 | 521 | } |
samux | 1:fb4494783863 | 522 | } |
samux | 1:fb4494783863 | 523 | } |
samux | 1:fb4494783863 | 524 | DBG("check: %s\r\n", checking.c_str()); |
defrost | 18:cf3bd54703a5 | 525 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 526 | attach_rx(true); |
samux | 1:fb4494783863 | 527 | return result; |
samux | 1:fb4494783863 | 528 | } |
samux | 1:fb4494783863 | 529 | |
samux | 1:fb4494783863 | 530 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 531 | if ( res != NULL) { |
samux | 1:fb4494783863 | 532 | int i = 0; |
samux | 1:fb4494783863 | 533 | Timer timeout; |
samux | 1:fb4494783863 | 534 | timeout.start(); |
samux | 1:fb4494783863 | 535 | tmr.reset(); |
samux | 1:fb4494783863 | 536 | while (1) { |
samux | 1:fb4494783863 | 537 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 538 | if (i == 0) { |
samux | 1:fb4494783863 | 539 | res = NULL; |
samux | 1:fb4494783863 | 540 | break; |
samux | 1:fb4494783863 | 541 | } |
samux | 1:fb4494783863 | 542 | res[i] = '\0'; |
samux | 1:fb4494783863 | 543 | DBG("user str 1: %s\r\n", res); |
samux | 1:fb4494783863 | 544 | |
samux | 1:fb4494783863 | 545 | break; |
samux | 1:fb4494783863 | 546 | } else { |
samux | 1:fb4494783863 | 547 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 548 | res[i] = '\0'; |
defrost | 18:cf3bd54703a5 | 549 | DBG("user str2: %s\r\n", res); |
samux | 1:fb4494783863 | 550 | |
samux | 1:fb4494783863 | 551 | break; |
samux | 1:fb4494783863 | 552 | } |
samux | 1:fb4494783863 | 553 | if (wifi.readable()) { |
samux | 1:fb4494783863 | 554 | tmr.start(); |
samux | 1:fb4494783863 | 555 | read = wifi.getc(); |
samux | 1:fb4494783863 | 556 | |
samux | 1:fb4494783863 | 557 | // we drop \r and \n |
samux | 1:fb4494783863 | 558 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 559 | res[i++] = read; |
samux | 1:fb4494783863 | 560 | } |
samux | 1:fb4494783863 | 561 | } |
samux | 1:fb4494783863 | 562 | } |
samux | 1:fb4494783863 | 563 | } |
defrost | 18:cf3bd54703a5 | 564 | DBG("user str3: %s\r\n", res); |
samux | 1:fb4494783863 | 565 | } |
samux | 1:fb4494783863 | 566 | |
samux | 1:fb4494783863 | 567 | //We flush the buffer |
samux | 1:fb4494783863 | 568 | while (wifi.readable()) |
samux | 1:fb4494783863 | 569 | wifi.getc(); |
samux | 1:fb4494783863 | 570 | |
samux | 1:fb4494783863 | 571 | attach_rx(true); |
samux | 1:fb4494783863 | 572 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 573 | return result; |
samux | 1:fb4494783863 | 574 | } |