Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
Fork of WiflyInterface by
Wifly/Wifly.cpp@7:e42b7fa7ef70, 2013-06-02 (annotated)
- Committer:
- leihen
- Date:
- Sun Jun 02 00:20:45 2013 +0000
- Revision:
- 7:e42b7fa7ef70
- Parent:
- 5:48d55083d2ff
- Child:
- 8:ac134ae11893
Added possibility to control baud rate.
; Increased internal buffer sizes.
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 |
leihen | 7:e42b7fa7ef70 | 25 | #if (0 && !defined(TARGET_LPC11U24)) |
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 | 2:8e54830d0df7 | 35 | #if !defined(TARGET_LPC11U24) |
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 | |
leihen | 7:e42b7fa7ef70 | 43 | const int std_baudrates[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 }; |
leihen | 7:e42b7fa7ef70 | 44 | |
leihen | 7:e42b7fa7ef70 | 45 | |
samux | 1:fb4494783863 | 46 | Wifly * Wifly::inst; |
samux | 1:fb4494783863 | 47 | |
leihen | 7:e42b7fa7ef70 | 48 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec, WiflyBaudrate_t baud): |
leihen | 7:e42b7fa7ef70 | 49 | wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512), m_baudrate(baud) |
samux | 1:fb4494783863 | 50 | { |
samux | 1:fb4494783863 | 51 | memset(&state, 0, sizeof(state)); |
samux | 1:fb4494783863 | 52 | state.sec = sec; |
samux | 1:fb4494783863 | 53 | |
samux | 1:fb4494783863 | 54 | // change all ' ' in '$' in the ssid and the passphrase |
samux | 1:fb4494783863 | 55 | strcpy(this->ssid, ssid); |
samux | 1:fb4494783863 | 56 | for (int i = 0; i < strlen(ssid); i++) { |
samux | 1:fb4494783863 | 57 | if (this->ssid[i] == ' ') |
samux | 1:fb4494783863 | 58 | this->ssid[i] = '$'; |
samux | 1:fb4494783863 | 59 | } |
samux | 1:fb4494783863 | 60 | strcpy(this->phrase, phrase); |
samux | 1:fb4494783863 | 61 | for (int i = 0; i < strlen(phrase); i++) { |
samux | 1:fb4494783863 | 62 | if (this->phrase[i] == ' ') |
samux | 1:fb4494783863 | 63 | this->phrase[i] = '$'; |
samux | 1:fb4494783863 | 64 | } |
samux | 1:fb4494783863 | 65 | |
samux | 1:fb4494783863 | 66 | inst = this; |
samux | 1:fb4494783863 | 67 | attach_rx(false); |
samux | 1:fb4494783863 | 68 | state.cmd_mode = false; |
leihen | 7:e42b7fa7ef70 | 69 | |
leihen | 7:e42b7fa7ef70 | 70 | reset(); |
samux | 1:fb4494783863 | 71 | } |
samux | 1:fb4494783863 | 72 | |
samux | 1:fb4494783863 | 73 | bool Wifly::join() |
samux | 1:fb4494783863 | 74 | { |
leihen | 7:e42b7fa7ef70 | 75 | char cmd[50]; |
leihen | 7:e42b7fa7ef70 | 76 | |
leihen | 7:e42b7fa7ef70 | 77 | setBaudRate(std_baudrates[m_baudrate]); |
samux | 1:fb4494783863 | 78 | |
samux | 1:fb4494783863 | 79 | for (int i= 0; i < MAX_TRY_JOIN; i++) { |
samux | 2:8e54830d0df7 | 80 | |
samux | 2:8e54830d0df7 | 81 | // no auto join |
samux | 2:8e54830d0df7 | 82 | if (!sendCommand("set w j 0\r", "AOK")) |
samux | 2:8e54830d0df7 | 83 | continue; |
samux | 2:8e54830d0df7 | 84 | |
samux | 2:8e54830d0df7 | 85 | //no echo |
samux | 2:8e54830d0df7 | 86 | if (!sendCommand("set u m 1\r", "AOK")) |
samux | 2:8e54830d0df7 | 87 | continue; |
samux | 2:8e54830d0df7 | 88 | |
samux | 1:fb4494783863 | 89 | // set time |
samux | 4:0bcec6272784 | 90 | if (!sendCommand("set c t 30\r", "AOK")) |
samux | 1:fb4494783863 | 91 | continue; |
samux | 1:fb4494783863 | 92 | |
samux | 1:fb4494783863 | 93 | // set size |
samux | 4:0bcec6272784 | 94 | if (!sendCommand("set c s 1024\r", "AOK")) |
samux | 1:fb4494783863 | 95 | continue; |
samux | 1:fb4494783863 | 96 | |
samux | 1:fb4494783863 | 97 | // red led on when tcp connection active |
samux | 1:fb4494783863 | 98 | if (!sendCommand("set s i 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 99 | continue; |
samux | 1:fb4494783863 | 100 | |
samux | 1:fb4494783863 | 101 | // no string sent to the tcp client |
samux | 1:fb4494783863 | 102 | if (!sendCommand("set c r 0\r", "AOK")) |
samux | 1:fb4494783863 | 103 | continue; |
samux | 1:fb4494783863 | 104 | |
samux | 1:fb4494783863 | 105 | // tcp protocol |
samux | 1:fb4494783863 | 106 | if (!sendCommand("set i p 2\r", "AOK")) |
samux | 1:fb4494783863 | 107 | continue; |
samux | 1:fb4494783863 | 108 | |
samux | 1:fb4494783863 | 109 | // tcp retry |
samux | 1:fb4494783863 | 110 | if (!sendCommand("set i f 0x7\r", "AOK")) |
samux | 1:fb4494783863 | 111 | continue; |
samux | 2:8e54830d0df7 | 112 | |
samux | 2:8e54830d0df7 | 113 | // set dns server |
samux | 2:8e54830d0df7 | 114 | if (!sendCommand("set d n rn.microchip.com\r", "AOK")) |
samux | 1:fb4494783863 | 115 | continue; |
samux | 1:fb4494783863 | 116 | |
samux | 1:fb4494783863 | 117 | //dhcp |
samux | 1:fb4494783863 | 118 | sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0); |
samux | 1:fb4494783863 | 119 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 120 | continue; |
samux | 1:fb4494783863 | 121 | |
samux | 1:fb4494783863 | 122 | // ssid |
leihen | 5:48d55083d2ff | 123 | sprintf(cmd, "set wlan ssid %s\r", ssid); |
samux | 1:fb4494783863 | 124 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 125 | continue; |
samux | 1:fb4494783863 | 126 | |
samux | 1:fb4494783863 | 127 | //auth |
samux | 1:fb4494783863 | 128 | sprintf(cmd, "set w a %d\r", state.sec); |
samux | 1:fb4494783863 | 129 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 130 | continue; |
samux | 1:fb4494783863 | 131 | |
samux | 1:fb4494783863 | 132 | // if no dhcp, set ip, netmask and gateway |
samux | 1:fb4494783863 | 133 | if (!state.dhcp) { |
samux | 1:fb4494783863 | 134 | DBG("not dhcp\r"); |
samux | 1:fb4494783863 | 135 | |
samux | 1:fb4494783863 | 136 | sprintf(cmd, "set i a %s\r\n", ip); |
samux | 1:fb4494783863 | 137 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 138 | continue; |
samux | 1:fb4494783863 | 139 | |
samux | 1:fb4494783863 | 140 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 1:fb4494783863 | 141 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 142 | continue; |
samux | 1:fb4494783863 | 143 | |
samux | 1:fb4494783863 | 144 | sprintf(cmd, "set i g %s\r", gateway); |
samux | 1:fb4494783863 | 145 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 146 | continue; |
samux | 1:fb4494783863 | 147 | } |
samux | 1:fb4494783863 | 148 | |
samux | 1:fb4494783863 | 149 | //key step |
samux | 1:fb4494783863 | 150 | if (state.sec != NONE) { |
samux | 1:fb4494783863 | 151 | if (state.sec == WPA) |
samux | 1:fb4494783863 | 152 | sprintf(cmd, "set w p %s\r", phrase); |
samux | 1:fb4494783863 | 153 | else if (state.sec == WEP_128) |
samux | 1:fb4494783863 | 154 | sprintf(cmd, "set w k %s\r", phrase); |
samux | 1:fb4494783863 | 155 | |
samux | 1:fb4494783863 | 156 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 157 | continue; |
samux | 1:fb4494783863 | 158 | } |
samux | 1:fb4494783863 | 159 | |
samux | 2:8e54830d0df7 | 160 | //join the network (10s timeout) |
samux | 1:fb4494783863 | 161 | if (state.dhcp) { |
samux | 2:8e54830d0df7 | 162 | if (!sendCommand("join\r", "DHCP=ON", NULL, 10000)) |
samux | 2:8e54830d0df7 | 163 | continue; |
samux | 2:8e54830d0df7 | 164 | } else { |
samux | 2:8e54830d0df7 | 165 | if (!sendCommand("join\r", "Associated", NULL, 10000)) |
samux | 1:fb4494783863 | 166 | continue; |
samux | 1:fb4494783863 | 167 | } |
leihen | 5:48d55083d2ff | 168 | |
leihen | 7:e42b7fa7ef70 | 169 | enableTime(1); |
leihen | 7:e42b7fa7ef70 | 170 | |
samux | 2:8e54830d0df7 | 171 | if (!sendCommand("save\r", "Stor")) |
samux | 2:8e54830d0df7 | 172 | continue; |
samux | 2:8e54830d0df7 | 173 | |
samux | 1:fb4494783863 | 174 | exit(); |
samux | 1:fb4494783863 | 175 | |
samux | 1:fb4494783863 | 176 | state.associated = true; |
samux | 1:fb4494783863 | 177 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, getStringSecurity()); |
samux | 1:fb4494783863 | 178 | return true; |
samux | 1:fb4494783863 | 179 | } |
samux | 1:fb4494783863 | 180 | return false; |
samux | 1:fb4494783863 | 181 | } |
samux | 1:fb4494783863 | 182 | |
leihen | 7:e42b7fa7ef70 | 183 | int Wifly::enableTime(int minutes, const char* ntp_address) |
leihen | 7:e42b7fa7ef70 | 184 | { |
leihen | 7:e42b7fa7ef70 | 185 | char cmd[30]; |
leihen | 7:e42b7fa7ef70 | 186 | |
leihen | 7:e42b7fa7ef70 | 187 | //let module automatically conntect to timeserver and get the actual time |
leihen | 7:e42b7fa7ef70 | 188 | sprintf(cmd, "set t e %d\r", minutes); |
leihen | 7:e42b7fa7ef70 | 189 | if (!sendCommand(cmd, "AOK")) { |
leihen | 7:e42b7fa7ef70 | 190 | ERR("Failed to modify time function !"); |
leihen | 7:e42b7fa7ef70 | 191 | return -1; |
leihen | 7:e42b7fa7ef70 | 192 | } |
leihen | 7:e42b7fa7ef70 | 193 | |
leihen | 7:e42b7fa7ef70 | 194 | //set the NTP server address |
leihen | 7:e42b7fa7ef70 | 195 | sprintf(cmd, "set t a %s\r", ntp_address); |
leihen | 7:e42b7fa7ef70 | 196 | if (!sendCommand(cmd, "AOK")) { |
leihen | 7:e42b7fa7ef70 | 197 | ERR("Failed to modify time server address !"); |
leihen | 7:e42b7fa7ef70 | 198 | } |
leihen | 7:e42b7fa7ef70 | 199 | |
leihen | 7:e42b7fa7ef70 | 200 | if (!sendCommand("set option format 1\r", "AOK")) { |
leihen | 7:e42b7fa7ef70 | 201 | ERR("Failed to set option format to ASCII !"); |
leihen | 7:e42b7fa7ef70 | 202 | } |
leihen | 7:e42b7fa7ef70 | 203 | |
leihen | 7:e42b7fa7ef70 | 204 | if (!sendCommand("set time zone 0\r", "AOK")) { |
leihen | 7:e42b7fa7ef70 | 205 | ERR("Failed to set time zone !"); |
leihen | 7:e42b7fa7ef70 | 206 | } |
leihen | 7:e42b7fa7ef70 | 207 | |
leihen | 7:e42b7fa7ef70 | 208 | sendCommand("time\r", NULL, NULL); |
leihen | 7:e42b7fa7ef70 | 209 | |
leihen | 7:e42b7fa7ef70 | 210 | flush(); |
leihen | 7:e42b7fa7ef70 | 211 | |
leihen | 7:e42b7fa7ef70 | 212 | exit(); |
leihen | 7:e42b7fa7ef70 | 213 | |
leihen | 7:e42b7fa7ef70 | 214 | return 0; |
leihen | 7:e42b7fa7ef70 | 215 | } |
leihen | 7:e42b7fa7ef70 | 216 | |
leihen | 7:e42b7fa7ef70 | 217 | string Wifly::getTime(bool uptime) |
leihen | 7:e42b7fa7ef70 | 218 | { |
leihen | 7:e42b7fa7ef70 | 219 | char buf[100]; |
leihen | 7:e42b7fa7ef70 | 220 | |
leihen | 7:e42b7fa7ef70 | 221 | sendCommand("time\r", NULL, NULL, 10000); |
leihen | 7:e42b7fa7ef70 | 222 | if (!sendCommand("show time\r", NULL, buf, 10000)) |
leihen | 7:e42b7fa7ef70 | 223 | return ""; |
leihen | 7:e42b7fa7ef70 | 224 | INFO("\r\nReceived Time : %s\r\n", buf); |
leihen | 7:e42b7fa7ef70 | 225 | |
leihen | 7:e42b7fa7ef70 | 226 | exit(); |
leihen | 7:e42b7fa7ef70 | 227 | return buf; |
leihen | 7:e42b7fa7ef70 | 228 | } |
leihen | 7:e42b7fa7ef70 | 229 | |
leihen | 7:e42b7fa7ef70 | 230 | bool Wifly::setBaudRate(int baud) |
leihen | 7:e42b7fa7ef70 | 231 | { |
leihen | 7:e42b7fa7ef70 | 232 | char str[35]; |
leihen | 7:e42b7fa7ef70 | 233 | bool bfound = false; |
leihen | 7:e42b7fa7ef70 | 234 | |
leihen | 7:e42b7fa7ef70 | 235 | INFO("Trying baudrates.\n"); |
leihen | 7:e42b7fa7ef70 | 236 | sprintf(str, "set uart raw %d\r", baud); |
leihen | 7:e42b7fa7ef70 | 237 | for( int i = 0 ; i < sizeof(std_baudrates)/sizeof(int) ; i++) { |
leihen | 7:e42b7fa7ef70 | 238 | // try all standard baudrates until the correct one is found |
leihen | 7:e42b7fa7ef70 | 239 | INFO("Trying at %d baud\n", std_baudrates[i]); |
leihen | 7:e42b7fa7ef70 | 240 | wifi.baud(std_baudrates[i]); |
leihen | 7:e42b7fa7ef70 | 241 | if (sendCommand(str, "AOK")) { |
leihen | 7:e42b7fa7ef70 | 242 | bfound = true; |
leihen | 7:e42b7fa7ef70 | 243 | break; |
leihen | 7:e42b7fa7ef70 | 244 | } |
leihen | 7:e42b7fa7ef70 | 245 | } |
leihen | 7:e42b7fa7ef70 | 246 | |
leihen | 7:e42b7fa7ef70 | 247 | if (bfound) { |
leihen | 7:e42b7fa7ef70 | 248 | wait(0.05); |
leihen | 7:e42b7fa7ef70 | 249 | if (!sendCommand("save\r", "STOR")) |
leihen | 7:e42b7fa7ef70 | 250 | return false; |
leihen | 7:e42b7fa7ef70 | 251 | |
leihen | 7:e42b7fa7ef70 | 252 | reboot(); |
leihen | 7:e42b7fa7ef70 | 253 | |
leihen | 7:e42b7fa7ef70 | 254 | wifi.baud(baud); |
leihen | 7:e42b7fa7ef70 | 255 | } |
leihen | 7:e42b7fa7ef70 | 256 | return true; |
leihen | 7:e42b7fa7ef70 | 257 | } |
samux | 1:fb4494783863 | 258 | |
samux | 1:fb4494783863 | 259 | bool Wifly::setProtocol(Protocol p) |
samux | 1:fb4494783863 | 260 | { |
samux | 1:fb4494783863 | 261 | // use udp auto pairing |
samux | 1:fb4494783863 | 262 | char cmd[20]; |
samux | 1:fb4494783863 | 263 | sprintf(cmd, "set i p %d\r", p); |
samux | 1:fb4494783863 | 264 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 265 | return false; |
samux | 1:fb4494783863 | 266 | |
samux | 1:fb4494783863 | 267 | switch(p) { |
samux | 1:fb4494783863 | 268 | case TCP: |
samux | 1:fb4494783863 | 269 | // set ip flags: tcp retry enabled |
samux | 1:fb4494783863 | 270 | if (!sendCommand("set i f 0x07\r", "AOK")) |
samux | 1:fb4494783863 | 271 | return false; |
samux | 1:fb4494783863 | 272 | break; |
samux | 1:fb4494783863 | 273 | case UDP: |
samux | 1:fb4494783863 | 274 | // set ip flags: udp auto pairing enabled |
samux | 1:fb4494783863 | 275 | if (!sendCommand("set i h 0.0.0.0\r", "AOK")) |
samux | 1:fb4494783863 | 276 | return false; |
samux | 4:0bcec6272784 | 277 | if (!sendCommand("set i f 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 278 | return false; |
samux | 1:fb4494783863 | 279 | break; |
samux | 1:fb4494783863 | 280 | } |
samux | 1:fb4494783863 | 281 | state.proto = p; |
samux | 1:fb4494783863 | 282 | return true; |
samux | 1:fb4494783863 | 283 | } |
samux | 1:fb4494783863 | 284 | |
samux | 1:fb4494783863 | 285 | char * Wifly::getStringSecurity() |
samux | 1:fb4494783863 | 286 | { |
samux | 1:fb4494783863 | 287 | switch(state.sec) { |
samux | 1:fb4494783863 | 288 | case NONE: |
samux | 1:fb4494783863 | 289 | return "NONE"; |
samux | 1:fb4494783863 | 290 | case WEP_128: |
samux | 1:fb4494783863 | 291 | return "WEP_128"; |
samux | 1:fb4494783863 | 292 | case WPA: |
samux | 1:fb4494783863 | 293 | return "WPA"; |
samux | 1:fb4494783863 | 294 | } |
samux | 1:fb4494783863 | 295 | return "UNKNOWN"; |
samux | 1:fb4494783863 | 296 | } |
samux | 1:fb4494783863 | 297 | |
samux | 1:fb4494783863 | 298 | bool Wifly::connect(const char * host, int port) |
samux | 1:fb4494783863 | 299 | { |
samux | 1:fb4494783863 | 300 | char rcv[20]; |
leihen | 7:e42b7fa7ef70 | 301 | char cmd[50]; |
samux | 1:fb4494783863 | 302 | |
samux | 2:8e54830d0df7 | 303 | // try to open |
samux | 2:8e54830d0df7 | 304 | sprintf(cmd, "open %s %d\r", host, port); |
samux | 2:8e54830d0df7 | 305 | if (sendCommand(cmd, "OPEN", NULL, 10000)) { |
samux | 2:8e54830d0df7 | 306 | state.tcp = true; |
samux | 2:8e54830d0df7 | 307 | state.cmd_mode = false; |
samux | 2:8e54830d0df7 | 308 | return true; |
samux | 1:fb4494783863 | 309 | } |
samux | 1:fb4494783863 | 310 | |
samux | 2:8e54830d0df7 | 311 | // if failed, retry and parse the response |
samux | 2:8e54830d0df7 | 312 | if (sendCommand(cmd, NULL, rcv, 5000)) { |
samux | 1:fb4494783863 | 313 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 1:fb4494783863 | 314 | if (strstr(rcv, "Connected") != NULL) { |
samux | 2:8e54830d0df7 | 315 | wait(0.25); |
samux | 1:fb4494783863 | 316 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 317 | return false; |
samux | 2:8e54830d0df7 | 318 | wait(0.25); |
samux | 2:8e54830d0df7 | 319 | if (!sendCommand(cmd, "OPEN", NULL, 10000)) |
samux | 1:fb4494783863 | 320 | return false; |
samux | 1:fb4494783863 | 321 | } else { |
samux | 1:fb4494783863 | 322 | return false; |
samux | 1:fb4494783863 | 323 | } |
samux | 1:fb4494783863 | 324 | } |
samux | 1:fb4494783863 | 325 | } else { |
samux | 1:fb4494783863 | 326 | return false; |
samux | 1:fb4494783863 | 327 | } |
samux | 2:8e54830d0df7 | 328 | |
samux | 1:fb4494783863 | 329 | state.tcp = true; |
samux | 1:fb4494783863 | 330 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 331 | |
samux | 1:fb4494783863 | 332 | return true; |
samux | 1:fb4494783863 | 333 | } |
samux | 1:fb4494783863 | 334 | |
samux | 1:fb4494783863 | 335 | |
samux | 1:fb4494783863 | 336 | bool Wifly::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 337 | { |
samux | 1:fb4494783863 | 338 | string h = host; |
samux | 1:fb4494783863 | 339 | char cmd[30], rcv[100]; |
samux | 1:fb4494783863 | 340 | int l = 0; |
samux | 1:fb4494783863 | 341 | char * point; |
samux | 1:fb4494783863 | 342 | int nb_digits = 0; |
samux | 1:fb4494783863 | 343 | |
samux | 1:fb4494783863 | 344 | // no dns needed |
samux | 1:fb4494783863 | 345 | int pos = h.find("."); |
samux | 1:fb4494783863 | 346 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 347 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 348 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 349 | } |
samux | 1:fb4494783863 | 350 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 1:fb4494783863 | 351 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 352 | strcpy(ip, host); |
samux | 1:fb4494783863 | 353 | } |
samux | 1:fb4494783863 | 354 | // dns needed |
samux | 1:fb4494783863 | 355 | else { |
samux | 1:fb4494783863 | 356 | nb_digits = 0; |
samux | 1:fb4494783863 | 357 | sprintf(cmd, "lookup %s\r", host); |
samux | 1:fb4494783863 | 358 | if (!sendCommand(cmd, NULL, rcv)) |
samux | 1:fb4494783863 | 359 | return false; |
samux | 1:fb4494783863 | 360 | |
samux | 1:fb4494783863 | 361 | // look for the ip address |
samux | 1:fb4494783863 | 362 | char * begin = strstr(rcv, "=") + 1; |
samux | 1:fb4494783863 | 363 | for (int i = 0; i < 3; i++) { |
samux | 1:fb4494783863 | 364 | point = strstr(begin + l, "."); |
samux | 1:fb4494783863 | 365 | DBG("str: %s", begin + l); |
samux | 1:fb4494783863 | 366 | l += point - (begin + l) + 1; |
samux | 1:fb4494783863 | 367 | } |
samux | 1:fb4494783863 | 368 | DBG("str: %s", begin + l); |
samux | 1:fb4494783863 | 369 | while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') { |
samux | 1:fb4494783863 | 370 | DBG("digit: %c", *(begin + l + nb_digits)); |
samux | 1:fb4494783863 | 371 | nb_digits++; |
samux | 1:fb4494783863 | 372 | } |
samux | 1:fb4494783863 | 373 | memcpy(ip, begin, l + nb_digits); |
samux | 1:fb4494783863 | 374 | ip[l+nb_digits] = 0; |
samux | 1:fb4494783863 | 375 | DBG("ip from dns: %s", ip); |
samux | 1:fb4494783863 | 376 | } |
samux | 1:fb4494783863 | 377 | return true; |
samux | 1:fb4494783863 | 378 | } |
samux | 1:fb4494783863 | 379 | |
samux | 1:fb4494783863 | 380 | |
samux | 1:fb4494783863 | 381 | void Wifly::flush() |
samux | 1:fb4494783863 | 382 | { |
samux | 1:fb4494783863 | 383 | buf_wifly.flush(); |
samux | 1:fb4494783863 | 384 | } |
samux | 1:fb4494783863 | 385 | |
samux | 1:fb4494783863 | 386 | bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) |
samux | 1:fb4494783863 | 387 | { |
samux | 1:fb4494783863 | 388 | if (!state.cmd_mode) { |
samux | 1:fb4494783863 | 389 | cmdMode(); |
samux | 1:fb4494783863 | 390 | } |
samux | 1:fb4494783863 | 391 | if (send(cmd, strlen(cmd), ack, res, timeout) == -1) { |
samux | 1:fb4494783863 | 392 | ERR("sendCommand: cannot %s\r\n", cmd); |
samux | 1:fb4494783863 | 393 | exit(); |
samux | 1:fb4494783863 | 394 | return false; |
samux | 1:fb4494783863 | 395 | } |
samux | 1:fb4494783863 | 396 | return true; |
samux | 1:fb4494783863 | 397 | } |
samux | 1:fb4494783863 | 398 | |
samux | 1:fb4494783863 | 399 | bool Wifly::cmdMode() |
samux | 1:fb4494783863 | 400 | { |
samux | 1:fb4494783863 | 401 | // if already in cmd mode, return |
samux | 1:fb4494783863 | 402 | if (state.cmd_mode) |
samux | 1:fb4494783863 | 403 | return true; |
samux | 2:8e54830d0df7 | 404 | |
samux | 1:fb4494783863 | 405 | if (send("$$$", 3, "CMD") == -1) { |
samux | 1:fb4494783863 | 406 | ERR("cannot enter in cmd mode\r\n"); |
samux | 2:8e54830d0df7 | 407 | exit(); |
samux | 1:fb4494783863 | 408 | return false; |
samux | 1:fb4494783863 | 409 | } |
samux | 1:fb4494783863 | 410 | state.cmd_mode = true; |
samux | 1:fb4494783863 | 411 | return true; |
samux | 1:fb4494783863 | 412 | } |
samux | 1:fb4494783863 | 413 | |
samux | 1:fb4494783863 | 414 | bool Wifly::disconnect() |
samux | 1:fb4494783863 | 415 | { |
samux | 1:fb4494783863 | 416 | // if already disconnected, return |
samux | 1:fb4494783863 | 417 | if (!state.associated) |
samux | 1:fb4494783863 | 418 | return true; |
samux | 2:8e54830d0df7 | 419 | |
samux | 1:fb4494783863 | 420 | if (!sendCommand("leave\r", "DeAuth")) |
samux | 1:fb4494783863 | 421 | return false; |
samux | 1:fb4494783863 | 422 | exit(); |
samux | 2:8e54830d0df7 | 423 | |
samux | 1:fb4494783863 | 424 | state.associated = false; |
samux | 1:fb4494783863 | 425 | return true; |
samux | 1:fb4494783863 | 426 | |
samux | 1:fb4494783863 | 427 | } |
samux | 1:fb4494783863 | 428 | |
samux | 1:fb4494783863 | 429 | bool Wifly::is_connected() |
samux | 1:fb4494783863 | 430 | { |
samux | 1:fb4494783863 | 431 | return (tcp_status.read() == 1) ? true : false; |
samux | 1:fb4494783863 | 432 | } |
samux | 1:fb4494783863 | 433 | |
samux | 1:fb4494783863 | 434 | |
samux | 1:fb4494783863 | 435 | void Wifly::reset() |
samux | 1:fb4494783863 | 436 | { |
samux | 1:fb4494783863 | 437 | reset_pin = 0; |
samux | 1:fb4494783863 | 438 | wait(0.2); |
samux | 1:fb4494783863 | 439 | reset_pin = 1; |
samux | 1:fb4494783863 | 440 | wait(0.2); |
samux | 1:fb4494783863 | 441 | } |
samux | 1:fb4494783863 | 442 | |
samux | 3:9aa05e19c62e | 443 | bool Wifly::reboot() |
samux | 3:9aa05e19c62e | 444 | { |
samux | 3:9aa05e19c62e | 445 | // if already in cmd mode, return |
samux | 3:9aa05e19c62e | 446 | if (!sendCommand("reboot\r")) |
samux | 3:9aa05e19c62e | 447 | return false; |
samux | 3:9aa05e19c62e | 448 | |
samux | 3:9aa05e19c62e | 449 | wait(0.3); |
samux | 3:9aa05e19c62e | 450 | |
samux | 3:9aa05e19c62e | 451 | state.cmd_mode = false; |
samux | 3:9aa05e19c62e | 452 | return true; |
samux | 3:9aa05e19c62e | 453 | } |
samux | 3:9aa05e19c62e | 454 | |
samux | 1:fb4494783863 | 455 | bool Wifly::close() |
samux | 1:fb4494783863 | 456 | { |
samux | 1:fb4494783863 | 457 | // if not connected, return |
samux | 1:fb4494783863 | 458 | if (!state.tcp) |
samux | 1:fb4494783863 | 459 | return true; |
samux | 2:8e54830d0df7 | 460 | |
samux | 1:fb4494783863 | 461 | wait(0.25); |
samux | 1:fb4494783863 | 462 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 463 | return false; |
samux | 1:fb4494783863 | 464 | exit(); |
samux | 2:8e54830d0df7 | 465 | |
samux | 1:fb4494783863 | 466 | state.tcp = false; |
samux | 1:fb4494783863 | 467 | return true; |
samux | 1:fb4494783863 | 468 | } |
samux | 1:fb4494783863 | 469 | |
samux | 1:fb4494783863 | 470 | |
samux | 1:fb4494783863 | 471 | int Wifly::putc(char c) |
samux | 1:fb4494783863 | 472 | { |
samux | 1:fb4494783863 | 473 | while (!wifi.writeable()); |
samux | 1:fb4494783863 | 474 | return wifi.putc(c); |
samux | 1:fb4494783863 | 475 | } |
samux | 1:fb4494783863 | 476 | |
samux | 1:fb4494783863 | 477 | |
samux | 1:fb4494783863 | 478 | bool Wifly::exit() |
samux | 1:fb4494783863 | 479 | { |
samux | 1:fb4494783863 | 480 | flush(); |
samux | 1:fb4494783863 | 481 | if (!state.cmd_mode) |
samux | 1:fb4494783863 | 482 | return true; |
samux | 1:fb4494783863 | 483 | if (!sendCommand("exit\r", "EXIT")) |
samux | 1:fb4494783863 | 484 | return false; |
samux | 1:fb4494783863 | 485 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 486 | flush(); |
samux | 1:fb4494783863 | 487 | return true; |
samux | 1:fb4494783863 | 488 | } |
samux | 1:fb4494783863 | 489 | |
samux | 1:fb4494783863 | 490 | |
samux | 1:fb4494783863 | 491 | int Wifly::readable() |
samux | 1:fb4494783863 | 492 | { |
samux | 1:fb4494783863 | 493 | return buf_wifly.available(); |
samux | 1:fb4494783863 | 494 | } |
samux | 1:fb4494783863 | 495 | |
samux | 1:fb4494783863 | 496 | int Wifly::writeable() |
samux | 1:fb4494783863 | 497 | { |
samux | 1:fb4494783863 | 498 | return wifi.writeable(); |
samux | 1:fb4494783863 | 499 | } |
samux | 1:fb4494783863 | 500 | |
samux | 1:fb4494783863 | 501 | char Wifly::getc() |
samux | 1:fb4494783863 | 502 | { |
samux | 1:fb4494783863 | 503 | char c; |
samux | 1:fb4494783863 | 504 | while (!buf_wifly.available()); |
samux | 1:fb4494783863 | 505 | buf_wifly.dequeue(&c); |
samux | 1:fb4494783863 | 506 | return c; |
samux | 1:fb4494783863 | 507 | } |
samux | 1:fb4494783863 | 508 | |
samux | 1:fb4494783863 | 509 | void Wifly::handler_rx(void) |
samux | 1:fb4494783863 | 510 | { |
samux | 1:fb4494783863 | 511 | //read characters |
samux | 1:fb4494783863 | 512 | while (wifi.readable()) |
samux | 1:fb4494783863 | 513 | buf_wifly.queue(wifi.getc()); |
samux | 1:fb4494783863 | 514 | } |
samux | 1:fb4494783863 | 515 | |
samux | 1:fb4494783863 | 516 | void Wifly::attach_rx(bool callback) |
samux | 1:fb4494783863 | 517 | { |
samux | 1:fb4494783863 | 518 | if (!callback) |
samux | 1:fb4494783863 | 519 | wifi.attach(NULL); |
samux | 1:fb4494783863 | 520 | else |
samux | 1:fb4494783863 | 521 | wifi.attach(this, &Wifly::handler_rx); |
samux | 1:fb4494783863 | 522 | } |
samux | 1:fb4494783863 | 523 | |
samux | 1:fb4494783863 | 524 | |
samux | 1:fb4494783863 | 525 | int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 526 | { |
samux | 1:fb4494783863 | 527 | char read; |
samux | 1:fb4494783863 | 528 | size_t found = string::npos; |
samux | 1:fb4494783863 | 529 | string checking; |
samux | 1:fb4494783863 | 530 | Timer tmr; |
samux | 1:fb4494783863 | 531 | int result = 0; |
samux | 1:fb4494783863 | 532 | |
samux | 1:fb4494783863 | 533 | DBG("will send: %s\r\n",str); |
samux | 1:fb4494783863 | 534 | |
samux | 1:fb4494783863 | 535 | attach_rx(false); |
samux | 1:fb4494783863 | 536 | |
samux | 1:fb4494783863 | 537 | //We flush the buffer |
samux | 1:fb4494783863 | 538 | while (wifi.readable()) |
samux | 1:fb4494783863 | 539 | wifi.getc(); |
samux | 1:fb4494783863 | 540 | |
samux | 1:fb4494783863 | 541 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 1:fb4494783863 | 542 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 543 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 544 | } else { |
samux | 1:fb4494783863 | 545 | //We flush the buffer |
samux | 1:fb4494783863 | 546 | while (wifi.readable()) |
samux | 1:fb4494783863 | 547 | wifi.getc(); |
samux | 1:fb4494783863 | 548 | |
samux | 1:fb4494783863 | 549 | tmr.start(); |
samux | 1:fb4494783863 | 550 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 551 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 552 | |
samux | 1:fb4494783863 | 553 | while (1) { |
samux | 1:fb4494783863 | 554 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 555 | //We flush the buffer |
samux | 1:fb4494783863 | 556 | while (wifi.readable()) |
samux | 1:fb4494783863 | 557 | wifi.getc(); |
samux | 1:fb4494783863 | 558 | |
samux | 1:fb4494783863 | 559 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 560 | |
samux | 1:fb4494783863 | 561 | attach_rx(true); |
samux | 1:fb4494783863 | 562 | return -1; |
samux | 1:fb4494783863 | 563 | } else if (wifi.readable()) { |
samux | 1:fb4494783863 | 564 | read = wifi.getc(); |
samux | 1:fb4494783863 | 565 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 566 | checking += read; |
samux | 1:fb4494783863 | 567 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 568 | if (found != string::npos) { |
samux | 1:fb4494783863 | 569 | wait(0.01); |
samux | 1:fb4494783863 | 570 | //We flush the buffer |
samux | 1:fb4494783863 | 571 | while (wifi.readable()) |
samux | 1:fb4494783863 | 572 | wifi.getc(); |
samux | 1:fb4494783863 | 573 | |
samux | 1:fb4494783863 | 574 | break; |
samux | 1:fb4494783863 | 575 | } |
samux | 1:fb4494783863 | 576 | } |
samux | 1:fb4494783863 | 577 | } |
samux | 1:fb4494783863 | 578 | } |
samux | 1:fb4494783863 | 579 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 580 | |
samux | 1:fb4494783863 | 581 | attach_rx(true); |
samux | 1:fb4494783863 | 582 | return result; |
samux | 1:fb4494783863 | 583 | } |
samux | 1:fb4494783863 | 584 | |
samux | 1:fb4494783863 | 585 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 586 | if ( res != NULL) { |
samux | 1:fb4494783863 | 587 | int i = 0; |
samux | 1:fb4494783863 | 588 | Timer timeout; |
samux | 1:fb4494783863 | 589 | timeout.start(); |
samux | 1:fb4494783863 | 590 | tmr.reset(); |
samux | 1:fb4494783863 | 591 | while (1) { |
samux | 1:fb4494783863 | 592 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 593 | if (i == 0) { |
samux | 1:fb4494783863 | 594 | res = NULL; |
samux | 1:fb4494783863 | 595 | break; |
samux | 1:fb4494783863 | 596 | } |
samux | 1:fb4494783863 | 597 | res[i] = '\0'; |
samux | 1:fb4494783863 | 598 | DBG("user str 1: %s\r\n", res); |
samux | 1:fb4494783863 | 599 | |
samux | 1:fb4494783863 | 600 | break; |
samux | 1:fb4494783863 | 601 | } else { |
samux | 1:fb4494783863 | 602 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 603 | res[i] = '\0'; |
samux | 1:fb4494783863 | 604 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 605 | |
samux | 1:fb4494783863 | 606 | break; |
samux | 1:fb4494783863 | 607 | } |
samux | 1:fb4494783863 | 608 | if (wifi.readable()) { |
samux | 1:fb4494783863 | 609 | tmr.start(); |
samux | 1:fb4494783863 | 610 | read = wifi.getc(); |
samux | 1:fb4494783863 | 611 | |
samux | 1:fb4494783863 | 612 | // we drop \r and \n |
samux | 1:fb4494783863 | 613 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 614 | res[i++] = read; |
samux | 1:fb4494783863 | 615 | } |
samux | 1:fb4494783863 | 616 | } |
samux | 1:fb4494783863 | 617 | } |
samux | 1:fb4494783863 | 618 | } |
samux | 1:fb4494783863 | 619 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 620 | } |
samux | 1:fb4494783863 | 621 | |
samux | 1:fb4494783863 | 622 | //We flush the buffer |
samux | 1:fb4494783863 | 623 | while (wifi.readable()) |
samux | 1:fb4494783863 | 624 | wifi.getc(); |
samux | 1:fb4494783863 | 625 | |
samux | 1:fb4494783863 | 626 | attach_rx(true); |
samux | 1:fb4494783863 | 627 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 628 | return result; |
samux | 1:fb4494783863 | 629 | } |