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