wifly/socket interface for wifly modules
Dependents: PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019
Wifly/Wifly.cpp@12:5ffaed1456c8, 2012-08-24 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 24 13:11:20 2012 +0000
- Revision:
- 12:5ffaed1456c8
- Parent:
- 11:b912f91e3212
- Child:
- 13:108340829acc
update state.associated
Who changed what in which revision?
User | Revision | Line number | New 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 | 12:5ffaed1456c8 | 46 | wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512) |
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 | 12:5ffaed1456c8 | 70 | char cmd[100]; |
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 | // save |
samux | 12:5ffaed1456c8 | 149 | if (!sendCommand("save\r", "Stor")) |
samux | 12:5ffaed1456c8 | 150 | return false; |
samux | 12:5ffaed1456c8 | 151 | |
samux | 12:5ffaed1456c8 | 152 | //join the network |
samux | 12:5ffaed1456c8 | 153 | sprintf(cmd, "join\r"); |
samux | 12:5ffaed1456c8 | 154 | if (!sendCommand(cmd, "Associated", NULL, 3000)) |
samux | 12:5ffaed1456c8 | 155 | continue; |
samux | 12:5ffaed1456c8 | 156 | |
samux | 12:5ffaed1456c8 | 157 | if (state.dhcp) { |
samux | 12:5ffaed1456c8 | 158 | if (!sendCommand("", "DHCP=ON", NULL, 3000)) |
samux | 12:5ffaed1456c8 | 159 | continue; |
samux | 12:5ffaed1456c8 | 160 | } |
samux | 12:5ffaed1456c8 | 161 | |
samux | 12:5ffaed1456c8 | 162 | exit(); |
samux | 12:5ffaed1456c8 | 163 | |
samux | 12:5ffaed1456c8 | 164 | state.associated = true; |
samux | 12:5ffaed1456c8 | 165 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity()); |
samux | 12:5ffaed1456c8 | 166 | return true; |
samux | 12:5ffaed1456c8 | 167 | } |
samux | 12:5ffaed1456c8 | 168 | return false; |
samux | 12:5ffaed1456c8 | 169 | } |
samux | 12:5ffaed1456c8 | 170 | |
samux | 12:5ffaed1456c8 | 171 | |
samux | 12:5ffaed1456c8 | 172 | bool Wifly::setProtocol(Protocol p) |
samux | 12:5ffaed1456c8 | 173 | { |
samux | 12:5ffaed1456c8 | 174 | // use udp auto pairing |
samux | 12:5ffaed1456c8 | 175 | char cmd[20]; |
samux | 12:5ffaed1456c8 | 176 | sprintf(cmd, "set i p %d\r", p); |
samux | 12:5ffaed1456c8 | 177 | if (!sendCommand(cmd, "AOK")) |
samux | 12:5ffaed1456c8 | 178 | return false; |
samux | 12:5ffaed1456c8 | 179 | |
samux | 12:5ffaed1456c8 | 180 | switch(p) { |
samux | 12:5ffaed1456c8 | 181 | case TCP: |
samux | 12:5ffaed1456c8 | 182 | // set ip flags: tcp retry enabled |
samux | 12:5ffaed1456c8 | 183 | if (!sendCommand("set i f 0x07\r", "AOK")) |
samux | 12:5ffaed1456c8 | 184 | return false; |
samux | 12:5ffaed1456c8 | 185 | break; |
samux | 12:5ffaed1456c8 | 186 | case UDP: |
samux | 12:5ffaed1456c8 | 187 | // set ip flags: udp auto pairing enabled |
samux | 12:5ffaed1456c8 | 188 | if (!sendCommand("set i f 0x40\r", "AOK")) |
samux | 12:5ffaed1456c8 | 189 | return false; |
samux | 12:5ffaed1456c8 | 190 | if (!sendCommand("set i h 0.0.0.0\r", "AOK")) |
samux | 12:5ffaed1456c8 | 191 | return false; |
samux | 12:5ffaed1456c8 | 192 | if (!sendCommand("set i r 0\r", "AOK")) |
samux | 12:5ffaed1456c8 | 193 | return false; |
samux | 12:5ffaed1456c8 | 194 | break; |
samux | 12:5ffaed1456c8 | 195 | } |
samux | 12:5ffaed1456c8 | 196 | state.proto = p; |
samux | 12:5ffaed1456c8 | 197 | return true; |
samux | 12:5ffaed1456c8 | 198 | } |
samux | 12:5ffaed1456c8 | 199 | |
samux | 12:5ffaed1456c8 | 200 | char * Wifly::getStringSecurity() |
samux | 12:5ffaed1456c8 | 201 | { |
samux | 12:5ffaed1456c8 | 202 | switch(state.sec) { |
samux | 12:5ffaed1456c8 | 203 | case NONE: |
samux | 12:5ffaed1456c8 | 204 | return "NONE"; |
samux | 12:5ffaed1456c8 | 205 | case WEP_128: |
samux | 12:5ffaed1456c8 | 206 | return "WEP_128"; |
samux | 12:5ffaed1456c8 | 207 | case WPA: |
samux | 12:5ffaed1456c8 | 208 | return "WPA"; |
samux | 12:5ffaed1456c8 | 209 | } |
samux | 12:5ffaed1456c8 | 210 | return "UNKNOWN"; |
samux | 12:5ffaed1456c8 | 211 | } |
samux | 12:5ffaed1456c8 | 212 | |
samux | 12:5ffaed1456c8 | 213 | bool Wifly::connect(const char * host, int port) |
samux | 12:5ffaed1456c8 | 214 | { |
samux | 12:5ffaed1456c8 | 215 | char rcv[20]; |
samux | 12:5ffaed1456c8 | 216 | char cmd[20]; |
samux | 12:5ffaed1456c8 | 217 | |
samux | 12:5ffaed1456c8 | 218 | // get ip from host and set host |
samux | 12:5ffaed1456c8 | 219 | if (gethostbyname(host, rcv)) { |
samux | 12:5ffaed1456c8 | 220 | sprintf(cmd, "set i h %s\r", rcv); |
samux | 12:5ffaed1456c8 | 221 | if (!sendCommand(cmd, "AOK")) |
samux | 12:5ffaed1456c8 | 222 | return false; |
samux | 12:5ffaed1456c8 | 223 | } else { |
samux | 12:5ffaed1456c8 | 224 | return false; |
samux | 12:5ffaed1456c8 | 225 | } |
samux | 12:5ffaed1456c8 | 226 | |
samux | 12:5ffaed1456c8 | 227 | // set port |
samux | 12:5ffaed1456c8 | 228 | sprintf(cmd, "set i r %d\r", port); |
samux | 12:5ffaed1456c8 | 229 | if (!sendCommand(cmd, "AOK")) |
samux | 12:5ffaed1456c8 | 230 | return false; |
samux | 12:5ffaed1456c8 | 231 | |
samux | 12:5ffaed1456c8 | 232 | // open |
samux | 12:5ffaed1456c8 | 233 | if (sendCommand("open\r", NULL, rcv)) { |
samux | 12:5ffaed1456c8 | 234 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 12:5ffaed1456c8 | 235 | if (strstr(rcv, "Connected") != NULL) { |
samux | 12:5ffaed1456c8 | 236 | if (!sendCommand("close\r", "CLOS")) |
samux | 12:5ffaed1456c8 | 237 | return false; |
samux | 12:5ffaed1456c8 | 238 | if (!sendCommand("open\r", "OPEN")) |
samux | 12:5ffaed1456c8 | 239 | return false; |
samux | 12:5ffaed1456c8 | 240 | } else { |
samux | 12:5ffaed1456c8 | 241 | return false; |
samux | 12:5ffaed1456c8 | 242 | } |
samux | 12:5ffaed1456c8 | 243 | } |
samux | 12:5ffaed1456c8 | 244 | } else { |
samux | 12:5ffaed1456c8 | 245 | return false; |
samux | 12:5ffaed1456c8 | 246 | } |
samux | 12:5ffaed1456c8 | 247 | |
samux | 12:5ffaed1456c8 | 248 | state.tcp = true; |
samux | 12:5ffaed1456c8 | 249 | state.cmd_mode = false; |
samux | 12:5ffaed1456c8 | 250 | |
samux | 12:5ffaed1456c8 | 251 | return true; |
samux | 12:5ffaed1456c8 | 252 | } |
samux | 12:5ffaed1456c8 | 253 | |
samux | 12:5ffaed1456c8 | 254 | |
samux | 12:5ffaed1456c8 | 255 | bool Wifly::gethostbyname(const char * host, char * ip) |
samux | 12:5ffaed1456c8 | 256 | { |
samux | 12:5ffaed1456c8 | 257 | string h = host; |
samux | 12:5ffaed1456c8 | 258 | char cmd[30], rcv[100]; |
samux | 12:5ffaed1456c8 | 259 | int l = 0; |
samux | 12:5ffaed1456c8 | 260 | char * point; |
samux | 12:5ffaed1456c8 | 261 | int nb_digits = 0; |
samux | 12:5ffaed1456c8 | 262 | |
samux | 12:5ffaed1456c8 | 263 | // no dns needed |
samux | 12:5ffaed1456c8 | 264 | int pos = h.find("."); |
samux | 12:5ffaed1456c8 | 265 | if (pos != string::npos) { |
samux | 12:5ffaed1456c8 | 266 | string sub = h.substr(0, h.find(".")); |
samux | 12:5ffaed1456c8 | 267 | nb_digits = atoi(sub.c_str()); |
samux | 12:5ffaed1456c8 | 268 | } |
samux | 12:5ffaed1456c8 | 269 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 12:5ffaed1456c8 | 270 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 12:5ffaed1456c8 | 271 | strcpy(ip, host); |
samux | 12:5ffaed1456c8 | 272 | } |
samux | 12:5ffaed1456c8 | 273 | // dns needed |
samux | 12:5ffaed1456c8 | 274 | else { |
samux | 12:5ffaed1456c8 | 275 | nb_digits = 0; |
samux | 12:5ffaed1456c8 | 276 | sprintf(cmd, "lookup %s\r", host); |
samux | 12:5ffaed1456c8 | 277 | if (!sendCommand(cmd, NULL, rcv)) |
samux | 12:5ffaed1456c8 | 278 | return false; |
samux | 12:5ffaed1456c8 | 279 | |
samux | 12:5ffaed1456c8 | 280 | // look for the ip address |
samux | 12:5ffaed1456c8 | 281 | char * begin = strstr(rcv, "=") + 1; |
samux | 12:5ffaed1456c8 | 282 | for (int i = 0; i < 3; i++) { |
samux | 12:5ffaed1456c8 | 283 | point = strstr(begin + l, "."); |
samux | 12:5ffaed1456c8 | 284 | DBG("str: %s", begin + l); |
samux | 12:5ffaed1456c8 | 285 | l += point - (begin + l) + 1; |
samux | 12:5ffaed1456c8 | 286 | } |
samux | 12:5ffaed1456c8 | 287 | DBG("str: %s", begin + l); |
samux | 12:5ffaed1456c8 | 288 | while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') { |
samux | 12:5ffaed1456c8 | 289 | DBG("digit: %c", *(begin + l + nb_digits)); |
samux | 12:5ffaed1456c8 | 290 | nb_digits++; |
samux | 12:5ffaed1456c8 | 291 | } |
samux | 12:5ffaed1456c8 | 292 | memcpy(ip, begin, l + nb_digits); |
samux | 12:5ffaed1456c8 | 293 | ip[l+nb_digits] = 0; |
samux | 12:5ffaed1456c8 | 294 | DBG("ip from dns: %s", ip); |
samux | 12:5ffaed1456c8 | 295 | } |
samux | 12:5ffaed1456c8 | 296 | return true; |
samux | 12:5ffaed1456c8 | 297 | } |
samux | 12:5ffaed1456c8 | 298 | |
samux | 12:5ffaed1456c8 | 299 | |
samux | 12:5ffaed1456c8 | 300 | void Wifly::flush() |
samux | 12:5ffaed1456c8 | 301 | { |
samux | 12:5ffaed1456c8 | 302 | buf_wifly.flush(); |
samux | 12:5ffaed1456c8 | 303 | } |
samux | 12:5ffaed1456c8 | 304 | |
samux | 12:5ffaed1456c8 | 305 | bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) |
samux | 12:5ffaed1456c8 | 306 | { |
samux | 12:5ffaed1456c8 | 307 | if (!state.cmd_mode) { |
samux | 12:5ffaed1456c8 | 308 | cmdMode(); |
samux | 12:5ffaed1456c8 | 309 | } |
samux | 12:5ffaed1456c8 | 310 | if (send(cmd, strlen(cmd), ack, res, timeout) == -1) { |
samux | 12:5ffaed1456c8 | 311 | ERR("sendCommand: cannot %s\r\n", cmd); |
samux | 12:5ffaed1456c8 | 312 | exit(); |
samux | 12:5ffaed1456c8 | 313 | return false; |
samux | 12:5ffaed1456c8 | 314 | } |
samux | 12:5ffaed1456c8 | 315 | return true; |
samux | 12:5ffaed1456c8 | 316 | } |
samux | 12:5ffaed1456c8 | 317 | |
samux | 12:5ffaed1456c8 | 318 | bool Wifly::cmdMode() |
samux | 12:5ffaed1456c8 | 319 | { |
samux | 12:5ffaed1456c8 | 320 | // if already in cmd mode, return |
samux | 12:5ffaed1456c8 | 321 | if (state.cmd_mode) |
samux | 12:5ffaed1456c8 | 322 | return true; |
samux | 12:5ffaed1456c8 | 323 | |
samux | 12:5ffaed1456c8 | 324 | if (send("$$$", 3, "CMD") == -1) { |
samux | 12:5ffaed1456c8 | 325 | ERR("cannot enter in cmd mode\r\n"); |
samux | 12:5ffaed1456c8 | 326 | return false; |
samux | 12:5ffaed1456c8 | 327 | } |
samux | 12:5ffaed1456c8 | 328 | state.cmd_mode = true; |
samux | 12:5ffaed1456c8 | 329 | return true; |
samux | 12:5ffaed1456c8 | 330 | } |
samux | 12:5ffaed1456c8 | 331 | |
samux | 12:5ffaed1456c8 | 332 | bool Wifly::disconnect() |
samux | 12:5ffaed1456c8 | 333 | { |
samux | 12:5ffaed1456c8 | 334 | // if already disconnected, return |
samux | 12:5ffaed1456c8 | 335 | if (!state.associated) |
samux | 12:5ffaed1456c8 | 336 | return true; |
samux | 12:5ffaed1456c8 | 337 | |
samux | 12:5ffaed1456c8 | 338 | if (!sendCommand("leave\r", "DeAuth")) |
samux | 12:5ffaed1456c8 | 339 | return false; |
samux | 12:5ffaed1456c8 | 340 | exit(); |
samux | 12:5ffaed1456c8 | 341 | |
samux | 12:5ffaed1456c8 | 342 | state.associated = false; |
samux | 12:5ffaed1456c8 | 343 | return true; |
samux | 12:5ffaed1456c8 | 344 | |
samux | 12:5ffaed1456c8 | 345 | } |
samux | 12:5ffaed1456c8 | 346 | |
samux | 12:5ffaed1456c8 | 347 | bool Wifly::is_connected() |
samux | 12:5ffaed1456c8 | 348 | { |
samux | 12:5ffaed1456c8 | 349 | return (tcp_status.read() == 1) ? true : false; |
samux | 12:5ffaed1456c8 | 350 | } |
samux | 12:5ffaed1456c8 | 351 | |
samux | 12:5ffaed1456c8 | 352 | |
samux | 12:5ffaed1456c8 | 353 | void Wifly::reset() |
samux | 12:5ffaed1456c8 | 354 | { |
samux | 12:5ffaed1456c8 | 355 | reset_pin = 0; |
samux | 12:5ffaed1456c8 | 356 | wait(0.2); |
samux | 12:5ffaed1456c8 | 357 | reset_pin = 1; |
samux | 12:5ffaed1456c8 | 358 | wait(0.2); |
samux | 12:5ffaed1456c8 | 359 | } |
samux | 12:5ffaed1456c8 | 360 | |
samux | 12:5ffaed1456c8 | 361 | bool Wifly::close() |
samux | 12:5ffaed1456c8 | 362 | { |
samux | 12:5ffaed1456c8 | 363 | // if not connected, return |
samux | 12:5ffaed1456c8 | 364 | if (!state.tcp) |
samux | 12:5ffaed1456c8 | 365 | return true; |
samux | 12:5ffaed1456c8 | 366 | |
samux | 12:5ffaed1456c8 | 367 | wait(0.25); |
samux | 12:5ffaed1456c8 | 368 | if (!sendCommand("close\r", "CLOS")) |
samux | 12:5ffaed1456c8 | 369 | return false; |
samux | 12:5ffaed1456c8 | 370 | exit(); |
samux | 12:5ffaed1456c8 | 371 | |
samux | 12:5ffaed1456c8 | 372 | state.tcp = false; |
samux | 12:5ffaed1456c8 | 373 | return true; |
samux | 12:5ffaed1456c8 | 374 | } |
samux | 12:5ffaed1456c8 | 375 | |
samux | 12:5ffaed1456c8 | 376 | |
samux | 12:5ffaed1456c8 | 377 | int Wifly::putc(char c) |
samux | 12:5ffaed1456c8 | 378 | { |
samux | 12:5ffaed1456c8 | 379 | while (!wifi.writeable()); |
samux | 12:5ffaed1456c8 | 380 | return wifi.putc(c); |
samux | 12:5ffaed1456c8 | 381 | } |
samux | 12:5ffaed1456c8 | 382 | |
samux | 12:5ffaed1456c8 | 383 | |
samux | 12:5ffaed1456c8 | 384 | bool Wifly::exit() |
samux | 12:5ffaed1456c8 | 385 | { |
samux | 12:5ffaed1456c8 | 386 | flush(); |
samux | 12:5ffaed1456c8 | 387 | if (!state.cmd_mode) |
samux | 12:5ffaed1456c8 | 388 | return true; |
samux | 12:5ffaed1456c8 | 389 | if (!sendCommand("exit\r", "EXIT")) |
samux | 12:5ffaed1456c8 | 390 | return false; |
samux | 12:5ffaed1456c8 | 391 | state.cmd_mode = false; |
samux | 12:5ffaed1456c8 | 392 | flush(); |
samux | 12:5ffaed1456c8 | 393 | return true; |
samux | 12:5ffaed1456c8 | 394 | } |
samux | 12:5ffaed1456c8 | 395 | |
samux | 12:5ffaed1456c8 | 396 | |
samux | 12:5ffaed1456c8 | 397 | int Wifly::readable() |
samux | 12:5ffaed1456c8 | 398 | { |
samux | 12:5ffaed1456c8 | 399 | return buf_wifly.available(); |
samux | 12:5ffaed1456c8 | 400 | } |
samux | 12:5ffaed1456c8 | 401 | |
samux | 12:5ffaed1456c8 | 402 | int Wifly::writeable() |
samux | 12:5ffaed1456c8 | 403 | { |
samux | 12:5ffaed1456c8 | 404 | return wifi.writeable(); |
samux | 12:5ffaed1456c8 | 405 | } |
samux | 12:5ffaed1456c8 | 406 | |
samux | 12:5ffaed1456c8 | 407 | char Wifly::getc() |
samux | 12:5ffaed1456c8 | 408 | { |
samux | 12:5ffaed1456c8 | 409 | char c; |
samux | 12:5ffaed1456c8 | 410 | while (!buf_wifly.available()); |
samux | 12:5ffaed1456c8 | 411 | buf_wifly.dequeue(&c); |
samux | 12:5ffaed1456c8 | 412 | return c; |
samux | 12:5ffaed1456c8 | 413 | } |
samux | 12:5ffaed1456c8 | 414 | |
samux | 12:5ffaed1456c8 | 415 | void Wifly::handler_rx(void) |
samux | 12:5ffaed1456c8 | 416 | { |
samux | 12:5ffaed1456c8 | 417 | //read characters |
samux | 12:5ffaed1456c8 | 418 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 419 | buf_wifly.queue(wifi.getc()); |
samux | 12:5ffaed1456c8 | 420 | } |
samux | 12:5ffaed1456c8 | 421 | |
samux | 12:5ffaed1456c8 | 422 | void Wifly::attach_rx(bool callback) |
samux | 12:5ffaed1456c8 | 423 | { |
samux | 12:5ffaed1456c8 | 424 | if (!callback) |
samux | 12:5ffaed1456c8 | 425 | wifi.attach(NULL); |
samux | 12:5ffaed1456c8 | 426 | else |
samux | 12:5ffaed1456c8 | 427 | wifi.attach(this, &Wifly::handler_rx); |
samux | 12:5ffaed1456c8 | 428 | } |
samux | 12:5ffaed1456c8 | 429 | |
samux | 12:5ffaed1456c8 | 430 | |
samux | 12:5ffaed1456c8 | 431 | int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) |
samux | 12:5ffaed1456c8 | 432 | { |
samux | 12:5ffaed1456c8 | 433 | char read; |
samux | 12:5ffaed1456c8 | 434 | size_t found = string::npos; |
samux | 12:5ffaed1456c8 | 435 | string checking; |
samux | 12:5ffaed1456c8 | 436 | Timer tmr; |
samux | 12:5ffaed1456c8 | 437 | int result = 0; |
samux | 12:5ffaed1456c8 | 438 | |
samux | 12:5ffaed1456c8 | 439 | DBG("will send: %s\r\n",str); |
samux | 12:5ffaed1456c8 | 440 | |
samux | 12:5ffaed1456c8 | 441 | attach_rx(false); |
samux | 12:5ffaed1456c8 | 442 | |
samux | 12:5ffaed1456c8 | 443 | //We flush the buffer |
samux | 12:5ffaed1456c8 | 444 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 445 | wifi.getc(); |
samux | 12:5ffaed1456c8 | 446 | |
samux | 12:5ffaed1456c8 | 447 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 12:5ffaed1456c8 | 448 | for (int i = 0; i < len; i++) |
samux | 12:5ffaed1456c8 | 449 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 12:5ffaed1456c8 | 450 | } else { |
samux | 12:5ffaed1456c8 | 451 | //We flush the buffer |
samux | 12:5ffaed1456c8 | 452 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 453 | wifi.getc(); |
samux | 12:5ffaed1456c8 | 454 | |
samux | 12:5ffaed1456c8 | 455 | tmr.start(); |
samux | 12:5ffaed1456c8 | 456 | for (int i = 0; i < len; i++) |
samux | 12:5ffaed1456c8 | 457 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 12:5ffaed1456c8 | 458 | |
samux | 12:5ffaed1456c8 | 459 | while (1) { |
samux | 12:5ffaed1456c8 | 460 | if (tmr.read_ms() > timeout) { |
samux | 12:5ffaed1456c8 | 461 | //We flush the buffer |
samux | 12:5ffaed1456c8 | 462 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 463 | wifi.getc(); |
samux | 12:5ffaed1456c8 | 464 | |
samux | 12:5ffaed1456c8 | 465 | DBG("check: %s\r\n", checking.c_str()); |
samux | 12:5ffaed1456c8 | 466 | |
samux | 12:5ffaed1456c8 | 467 | attach_rx(true); |
samux | 12:5ffaed1456c8 | 468 | return -1; |
samux | 12:5ffaed1456c8 | 469 | } else if (wifi.readable()) { |
samux | 12:5ffaed1456c8 | 470 | read = wifi.getc(); |
samux | 12:5ffaed1456c8 | 471 | if ( read != '\r' && read != '\n') { |
samux | 12:5ffaed1456c8 | 472 | checking += read; |
samux | 12:5ffaed1456c8 | 473 | found = checking.find(ACK); |
samux | 12:5ffaed1456c8 | 474 | if (found != string::npos) { |
samux | 12:5ffaed1456c8 | 475 | wait(0.01); |
samux | 12:5ffaed1456c8 | 476 | |
samux | 12:5ffaed1456c8 | 477 | //We flush the buffer |
samux | 12:5ffaed1456c8 | 478 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 479 | wifi.getc(); |
samux | 12:5ffaed1456c8 | 480 | |
samux | 12:5ffaed1456c8 | 481 | break; |
samux | 12:5ffaed1456c8 | 482 | } |
samux | 12:5ffaed1456c8 | 483 | } |
samux | 12:5ffaed1456c8 | 484 | } |
samux | 12:5ffaed1456c8 | 485 | } |
samux | 12:5ffaed1456c8 | 486 | DBG("check: %s\r\n", checking.c_str()); |
samux | 12:5ffaed1456c8 | 487 | |
samux | 12:5ffaed1456c8 | 488 | attach_rx(true); |
samux | 12:5ffaed1456c8 | 489 | return result; |
samux | 12:5ffaed1456c8 | 490 | } |
samux | 12:5ffaed1456c8 | 491 | |
samux | 12:5ffaed1456c8 | 492 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 12:5ffaed1456c8 | 493 | if ( res != NULL) { |
samux | 12:5ffaed1456c8 | 494 | int i = 0; |
samux | 12:5ffaed1456c8 | 495 | Timer timeout; |
samux | 12:5ffaed1456c8 | 496 | timeout.start(); |
samux | 12:5ffaed1456c8 | 497 | tmr.reset(); |
samux | 12:5ffaed1456c8 | 498 | while (1) { |
samux | 12:5ffaed1456c8 | 499 | if (timeout.read() > 2) { |
samux | 12:5ffaed1456c8 | 500 | if (i == 0) { |
samux | 12:5ffaed1456c8 | 501 | res = NULL; |
samux | 12:5ffaed1456c8 | 502 | break; |
samux | 12:5ffaed1456c8 | 503 | } |
samux | 12:5ffaed1456c8 | 504 | res[i] = '\0'; |
samux | 12:5ffaed1456c8 | 505 | DBG("user str 1: %s\r\n", res); |
samux | 12:5ffaed1456c8 | 506 | |
samux | 12:5ffaed1456c8 | 507 | break; |
samux | 12:5ffaed1456c8 | 508 | } else { |
samux | 12:5ffaed1456c8 | 509 | if (tmr.read_ms() > 300) { |
samux | 12:5ffaed1456c8 | 510 | res[i] = '\0'; |
samux | 12:5ffaed1456c8 | 511 | DBG("user str: %s\r\n", res); |
samux | 12:5ffaed1456c8 | 512 | |
samux | 12:5ffaed1456c8 | 513 | break; |
samux | 12:5ffaed1456c8 | 514 | } |
samux | 12:5ffaed1456c8 | 515 | if (wifi.readable()) { |
samux | 12:5ffaed1456c8 | 516 | tmr.start(); |
samux | 12:5ffaed1456c8 | 517 | read = wifi.getc(); |
samux | 12:5ffaed1456c8 | 518 | |
samux | 12:5ffaed1456c8 | 519 | // we drop \r and \n |
samux | 12:5ffaed1456c8 | 520 | if ( read != '\r' && read != '\n') { |
samux | 12:5ffaed1456c8 | 521 | res[i++] = read; |
samux | 12:5ffaed1456c8 | 522 | } |
samux | 12:5ffaed1456c8 | 523 | } |
samux | 12:5ffaed1456c8 | 524 | } |
samux | 12:5ffaed1456c8 | 525 | } |
samux | 12:5ffaed1456c8 | 526 | DBG("user str: %s\r\n", res); |
samux | 12:5ffaed1456c8 | 527 | } |
samux | 12:5ffaed1456c8 | 528 | |
samux | 12:5ffaed1456c8 | 529 | //We flush the buffer |
samux | 12:5ffaed1456c8 | 530 | while (wifi.readable()) |
samux | 12:5ffaed1456c8 | 531 | wifi.getc(); |
samux | 12:5ffaed1456c8 | 532 | |
samux | 12:5ffaed1456c8 | 533 | attach_rx(true); |
samux | 12:5ffaed1456c8 | 534 | DBG("result: %d\r\n", result) |
samux | 12:5ffaed1456c8 | 535 | return result; |
samux | 0:6ffb0aeb3972 | 536 | } |