- Added messages
Fork of WiflyInterface by
Wifly/Wifly.cpp@43:b14d4ba33255, 2017-05-24 (annotated)
- Committer:
- defrost
- Date:
- Wed May 24 18:34:11 2017 +0000
- Revision:
- 43:b14d4ba33255
- Parent:
- 41:598257b3e319
- Not sure what changed here, probably something minor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:fb4494783863 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:fb4494783863 | 2 | * |
samux | 1:fb4494783863 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:fb4494783863 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:fb4494783863 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:fb4494783863 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:fb4494783863 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:fb4494783863 | 8 | * |
samux | 1:fb4494783863 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:fb4494783863 | 10 | * substantial portions of the Software. |
samux | 1:fb4494783863 | 11 | * |
samux | 1:fb4494783863 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:fb4494783863 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:fb4494783863 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:fb4494783863 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:fb4494783863 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:fb4494783863 | 17 | */ |
samux | 1:fb4494783863 | 18 | |
samux | 1:fb4494783863 | 19 | #include "mbed.h" |
samux | 1:fb4494783863 | 20 | #include "Wifly.h" |
samux | 1:fb4494783863 | 21 | #include <string> |
samux | 1:fb4494783863 | 22 | #include <algorithm> |
defrost | 22:6c5bcfa33922 | 23 | |
defrost | 43:b14d4ba33255 | 24 | //#define DEBUG |
defrost | 26:eaaedb036df1 | 25 | #define INFOMESSAGES |
defrost | 34:a22a6343e3d4 | 26 | #define WARNMESSAGES |
defrost | 34:a22a6343e3d4 | 27 | #define ERRMESSAGES |
defrost | 34:a22a6343e3d4 | 28 | #define FUNCNAME "Wifly" |
defrost | 34:a22a6343e3d4 | 29 | #include "messages.h" |
samux | 1:fb4494783863 | 30 | |
samux | 1:fb4494783863 | 31 | #define MAX_TRY_JOIN 3 |
samux | 1:fb4494783863 | 32 | |
samux | 1:fb4494783863 | 33 | Wifly * Wifly::inst; |
defrost | 39:2b8018d66b68 | 34 | Serial * Wifly::SerialCommPort; |
samux | 1:fb4494783863 | 35 | |
defrost | 40:2bbc4753b23f | 36 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid_input, const char * phrase_input, Security sec, Serial* messagesPort): |
defrost | 25:36b2d76ca8d9 | 37 | wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(1024) |
samux | 1:fb4494783863 | 38 | { |
defrost | 35:ce3afc021ec2 | 39 | // Set the default baud rate: |
defrost | 35:ce3afc021ec2 | 40 | serial_baudrate = 9600; |
defrost | 35:ce3afc021ec2 | 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 |
defrost | 40:2bbc4753b23f | 46 | strcpy(this->ssid, ssid_input); |
defrost | 40:2bbc4753b23f | 47 | for (int i = 0; i < strlen(ssid_input); i++) { |
samux | 1:fb4494783863 | 48 | if (this->ssid[i] == ' ') |
samux | 1:fb4494783863 | 49 | this->ssid[i] = '$'; |
samux | 1:fb4494783863 | 50 | } |
defrost | 40:2bbc4753b23f | 51 | strcpy(this->phrase, phrase_input); |
defrost | 40:2bbc4753b23f | 52 | for (int i = 0; i < strlen(phrase_input); 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; |
samux | 1:fb4494783863 | 58 | attach_rx(false); |
samux | 1:fb4494783863 | 59 | state.cmd_mode = false; |
defrost | 39:2b8018d66b68 | 60 | SerialCommPort = messagesPort; |
defrost | 41:598257b3e319 | 61 | } |
defrost | 41:598257b3e319 | 62 | |
defrost | 41:598257b3e319 | 63 | void Wifly::setSerialPort(Serial * scp){ |
defrost | 41:598257b3e319 | 64 | SerialCommPort = scp; |
defrost | 41:598257b3e319 | 65 | return; |
samux | 1:fb4494783863 | 66 | } |
samux | 1:fb4494783863 | 67 | |
defrost | 40:2bbc4753b23f | 68 | void Wifly::setSsid(char * ssid_input){ |
defrost | 40:2bbc4753b23f | 69 | strcpy(this->ssid, ssid_input); |
defrost | 40:2bbc4753b23f | 70 | for (int i = 0; i < strlen(ssid_input); i++) { |
defrost | 40:2bbc4753b23f | 71 | if (this->ssid[i] == ' ') |
defrost | 40:2bbc4753b23f | 72 | this->ssid[i] = '$'; |
defrost | 40:2bbc4753b23f | 73 | } |
defrost | 40:2bbc4753b23f | 74 | return; |
defrost | 40:2bbc4753b23f | 75 | } |
defrost | 40:2bbc4753b23f | 76 | |
defrost | 40:2bbc4753b23f | 77 | void Wifly::setPhrase(char * phrase_input){ |
defrost | 40:2bbc4753b23f | 78 | strcpy(this->phrase, phrase_input); |
defrost | 40:2bbc4753b23f | 79 | for (int i = 0; i < strlen(phrase_input); i++) { |
defrost | 40:2bbc4753b23f | 80 | if (this->phrase[i] == ' ') |
defrost | 40:2bbc4753b23f | 81 | this->phrase[i] = '$'; |
defrost | 40:2bbc4753b23f | 82 | } |
defrost | 40:2bbc4753b23f | 83 | return; |
defrost | 40:2bbc4753b23f | 84 | } |
defrost | 40:2bbc4753b23f | 85 | |
defrost | 15:eaa1ec0e26bf | 86 | void Wifly::setBaud(int baudrate) |
defrost | 15:eaa1ec0e26bf | 87 | { |
defrost | 18:cf3bd54703a5 | 88 | char cmd[28]; |
defrost | 35:ce3afc021ec2 | 89 | // Save the baudrate: |
defrost | 35:ce3afc021ec2 | 90 | if(baudrate > 0){ |
defrost | 35:ce3afc021ec2 | 91 | serial_baudrate = baudrate; |
defrost | 35:ce3afc021ec2 | 92 | } |
defrost | 39:2b8018d66b68 | 93 | DBG(SerialCommPort, "Setting wifi shield baudrate to: %d.", serial_baudrate); |
defrost | 15:eaa1ec0e26bf | 94 | // This sets the baud rate 'instantly' |
defrost | 35:ce3afc021ec2 | 95 | sprintf(cmd, "set u i %d\r", serial_baudrate); |
defrost | 15:eaa1ec0e26bf | 96 | // This one sets it some other way that does not work |
defrost | 15:eaa1ec0e26bf | 97 | //sprintf(cmd, "set u b %d\r", baudrate); |
defrost | 15:eaa1ec0e26bf | 98 | // Set baud rate of wifly: |
defrost | 16:de16e8b077c1 | 99 | sendCommand(cmd, NULL); |
defrost | 16:de16e8b077c1 | 100 | wait(0.5); |
defrost | 15:eaa1ec0e26bf | 101 | // Set baud rate of UART: |
defrost | 35:ce3afc021ec2 | 102 | wifi.baud(serial_baudrate); |
defrost | 16:de16e8b077c1 | 103 | wait(0.5); |
defrost | 15:eaa1ec0e26bf | 104 | exit(); |
defrost | 15:eaa1ec0e26bf | 105 | } |
defrost | 15:eaa1ec0e26bf | 106 | |
samux | 1:fb4494783863 | 107 | bool Wifly::join() |
samux | 1:fb4494783863 | 108 | { |
defrost | 18:cf3bd54703a5 | 109 | char cmd[28]; |
samux | 1:fb4494783863 | 110 | |
samux | 1:fb4494783863 | 111 | for (int i= 0; i < MAX_TRY_JOIN; i++) { |
samux | 2:8e54830d0df7 | 112 | |
samux | 2:8e54830d0df7 | 113 | // no auto join |
samux | 2:8e54830d0df7 | 114 | if (!sendCommand("set w j 0\r", "AOK")) |
samux | 2:8e54830d0df7 | 115 | continue; |
samux | 2:8e54830d0df7 | 116 | |
samux | 2:8e54830d0df7 | 117 | //no echo |
samux | 2:8e54830d0df7 | 118 | if (!sendCommand("set u m 1\r", "AOK")) |
samux | 2:8e54830d0df7 | 119 | continue; |
samux | 2:8e54830d0df7 | 120 | |
samux | 1:fb4494783863 | 121 | // set time |
samux | 4:0bcec6272784 | 122 | if (!sendCommand("set c t 30\r", "AOK")) |
samux | 1:fb4494783863 | 123 | continue; |
samux | 1:fb4494783863 | 124 | |
samux | 1:fb4494783863 | 125 | // set size |
samux | 4:0bcec6272784 | 126 | if (!sendCommand("set c s 1024\r", "AOK")) |
samux | 1:fb4494783863 | 127 | continue; |
samux | 1:fb4494783863 | 128 | |
samux | 1:fb4494783863 | 129 | // red led on when tcp connection active |
samux | 1:fb4494783863 | 130 | if (!sendCommand("set s i 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 131 | continue; |
samux | 1:fb4494783863 | 132 | |
samux | 1:fb4494783863 | 133 | // no string sent to the tcp client |
samux | 1:fb4494783863 | 134 | if (!sendCommand("set c r 0\r", "AOK")) |
samux | 1:fb4494783863 | 135 | continue; |
samux | 1:fb4494783863 | 136 | |
samux | 1:fb4494783863 | 137 | // tcp protocol |
samux | 1:fb4494783863 | 138 | if (!sendCommand("set i p 2\r", "AOK")) |
samux | 1:fb4494783863 | 139 | continue; |
samux | 1:fb4494783863 | 140 | |
samux | 1:fb4494783863 | 141 | // tcp retry |
samux | 1:fb4494783863 | 142 | if (!sendCommand("set i f 0x7\r", "AOK")) |
samux | 1:fb4494783863 | 143 | continue; |
samux | 2:8e54830d0df7 | 144 | |
samux | 2:8e54830d0df7 | 145 | // set dns server |
samux | 2:8e54830d0df7 | 146 | if (!sendCommand("set d n rn.microchip.com\r", "AOK")) |
samux | 1:fb4494783863 | 147 | continue; |
samux | 1:fb4494783863 | 148 | |
samux | 1:fb4494783863 | 149 | //dhcp |
samux | 1:fb4494783863 | 150 | sprintf(cmd, "set i d %d\r", (state.dhcp) ? 1 : 0); |
samux | 1:fb4494783863 | 151 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 152 | continue; |
samux | 1:fb4494783863 | 153 | |
samux | 1:fb4494783863 | 154 | // ssid |
samux | 1:fb4494783863 | 155 | sprintf(cmd, "set w s %s\r", ssid); |
lz307 | 7:3152fcc74390 | 156 | if (!sendCommand(cmd, "AOK", NULL, 1000)) |
samux | 1:fb4494783863 | 157 | continue; |
samux | 1:fb4494783863 | 158 | |
samux | 1:fb4494783863 | 159 | //auth |
samux | 1:fb4494783863 | 160 | sprintf(cmd, "set w a %d\r", state.sec); |
samux | 1:fb4494783863 | 161 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 162 | continue; |
samux | 1:fb4494783863 | 163 | |
samux | 1:fb4494783863 | 164 | // if no dhcp, set ip, netmask and gateway |
samux | 1:fb4494783863 | 165 | if (!state.dhcp) { |
defrost | 39:2b8018d66b68 | 166 | DBG(SerialCommPort, "not dhcp\r"); |
samux | 1:fb4494783863 | 167 | |
samux | 1:fb4494783863 | 168 | sprintf(cmd, "set i a %s\r\n", ip); |
samux | 1:fb4494783863 | 169 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 170 | continue; |
samux | 1:fb4494783863 | 171 | |
samux | 1:fb4494783863 | 172 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 1:fb4494783863 | 173 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 174 | continue; |
samux | 1:fb4494783863 | 175 | |
samux | 1:fb4494783863 | 176 | sprintf(cmd, "set i g %s\r", gateway); |
samux | 1:fb4494783863 | 177 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 178 | continue; |
samux | 1:fb4494783863 | 179 | } |
samux | 1:fb4494783863 | 180 | |
samux | 1:fb4494783863 | 181 | //key step |
samux | 1:fb4494783863 | 182 | if (state.sec != NONE) { |
defrost | 12:1aaea7f302e6 | 183 | if ((state.sec == WPA)||(state.sec == WPA2)) |
samux | 1:fb4494783863 | 184 | sprintf(cmd, "set w p %s\r", phrase); |
samux | 1:fb4494783863 | 185 | else if (state.sec == WEP_128) |
samux | 1:fb4494783863 | 186 | sprintf(cmd, "set w k %s\r", phrase); |
samux | 1:fb4494783863 | 187 | |
lz307 | 7:3152fcc74390 | 188 | if (!sendCommand(cmd, "AOK", NULL, 1000)) |
samux | 1:fb4494783863 | 189 | continue; |
samux | 1:fb4494783863 | 190 | } |
samux | 1:fb4494783863 | 191 | |
samux | 1:fb4494783863 | 192 | //join the network |
samux | 1:fb4494783863 | 193 | sprintf(cmd, "join\r"); |
samux | 1:fb4494783863 | 194 | if (!sendCommand(cmd, "Associated", NULL, 3000)) |
samux | 1:fb4494783863 | 195 | continue; |
lz307 | 7:3152fcc74390 | 196 | |
lz307 | 7:3152fcc74390 | 197 | if (!sendCommand("", "IP=", NULL, 10000)) |
lz307 | 7:3152fcc74390 | 198 | continue; |
samux | 1:fb4494783863 | 199 | |
samux | 1:fb4494783863 | 200 | if (state.dhcp) { |
lz307 | 7:3152fcc74390 | 201 | if (!sendCommand("get i\r", "DHCP=ON", NULL, 3000)) |
samux | 1:fb4494783863 | 202 | continue; |
samux | 1:fb4494783863 | 203 | } |
samux | 1:fb4494783863 | 204 | |
samux | 2:8e54830d0df7 | 205 | if (!sendCommand("save\r", "Stor")) |
samux | 2:8e54830d0df7 | 206 | continue; |
samux | 2:8e54830d0df7 | 207 | |
samux | 1:fb4494783863 | 208 | exit(); |
samux | 1:fb4494783863 | 209 | |
samux | 1:fb4494783863 | 210 | state.associated = true; |
defrost | 39:2b8018d66b68 | 211 | INFO(SerialCommPort, "ssid: %s", this->ssid); |
defrost | 39:2b8018d66b68 | 212 | INFO(SerialCommPort, "phrase: %s", this->phrase); |
defrost | 39:2b8018d66b68 | 213 | INFO(SerialCommPort, "security: %s", getStringSecurity()); |
defrost | 13:8846f12fa277 | 214 | |
samux | 1:fb4494783863 | 215 | return true; |
samux | 1:fb4494783863 | 216 | } |
samux | 1:fb4494783863 | 217 | return false; |
samux | 1:fb4494783863 | 218 | } |
samux | 1:fb4494783863 | 219 | |
samux | 1:fb4494783863 | 220 | |
samux | 1:fb4494783863 | 221 | bool Wifly::setProtocol(Protocol p) |
samux | 1:fb4494783863 | 222 | { |
samux | 1:fb4494783863 | 223 | // use udp auto pairing |
samux | 1:fb4494783863 | 224 | char cmd[20]; |
samux | 1:fb4494783863 | 225 | sprintf(cmd, "set i p %d\r", p); |
samux | 1:fb4494783863 | 226 | if (!sendCommand(cmd, "AOK")) |
samux | 1:fb4494783863 | 227 | return false; |
samux | 1:fb4494783863 | 228 | |
samux | 1:fb4494783863 | 229 | switch(p) { |
samux | 1:fb4494783863 | 230 | case TCP: |
samux | 1:fb4494783863 | 231 | // set ip flags: tcp retry enabled |
samux | 1:fb4494783863 | 232 | if (!sendCommand("set i f 0x07\r", "AOK")) |
samux | 1:fb4494783863 | 233 | return false; |
samux | 1:fb4494783863 | 234 | break; |
samux | 1:fb4494783863 | 235 | case UDP: |
samux | 1:fb4494783863 | 236 | // set ip flags: udp auto pairing enabled |
samux | 1:fb4494783863 | 237 | if (!sendCommand("set i h 0.0.0.0\r", "AOK")) |
samux | 1:fb4494783863 | 238 | return false; |
samux | 4:0bcec6272784 | 239 | if (!sendCommand("set i f 0x40\r", "AOK")) |
samux | 1:fb4494783863 | 240 | return false; |
samux | 1:fb4494783863 | 241 | break; |
samux | 1:fb4494783863 | 242 | } |
samux | 1:fb4494783863 | 243 | state.proto = p; |
samux | 1:fb4494783863 | 244 | return true; |
samux | 1:fb4494783863 | 245 | } |
samux | 1:fb4494783863 | 246 | |
samux | 1:fb4494783863 | 247 | char * Wifly::getStringSecurity() |
samux | 1:fb4494783863 | 248 | { |
samux | 1:fb4494783863 | 249 | switch(state.sec) { |
samux | 1:fb4494783863 | 250 | case NONE: |
samux | 1:fb4494783863 | 251 | return "NONE"; |
samux | 1:fb4494783863 | 252 | case WEP_128: |
samux | 1:fb4494783863 | 253 | return "WEP_128"; |
samux | 1:fb4494783863 | 254 | case WPA: |
samux | 1:fb4494783863 | 255 | return "WPA"; |
defrost | 13:8846f12fa277 | 256 | case WPA2: |
defrost | 13:8846f12fa277 | 257 | return "WPA2"; |
samux | 1:fb4494783863 | 258 | } |
samux | 1:fb4494783863 | 259 | return "UNKNOWN"; |
samux | 1:fb4494783863 | 260 | } |
samux | 1:fb4494783863 | 261 | |
samux | 1:fb4494783863 | 262 | bool Wifly::connect(const char * host, int port) |
samux | 1:fb4494783863 | 263 | { |
defrost | 18:cf3bd54703a5 | 264 | char rcv[28]; |
defrost | 18:cf3bd54703a5 | 265 | char cmd[28]; |
defrost | 12:1aaea7f302e6 | 266 | |
defrost | 12:1aaea7f302e6 | 267 | |
samux | 1:fb4494783863 | 268 | |
samux | 2:8e54830d0df7 | 269 | // try to open |
samux | 2:8e54830d0df7 | 270 | sprintf(cmd, "open %s %d\r", host, port); |
defrost | 39:2b8018d66b68 | 271 | DBG(SerialCommPort, "CMD1: %s",cmd); |
defrost | 18:cf3bd54703a5 | 272 | if (sendCommand(cmd, "OPEN", rcv, 10000)) { |
samux | 2:8e54830d0df7 | 273 | state.tcp = true; |
samux | 2:8e54830d0df7 | 274 | state.cmd_mode = false; |
samux | 2:8e54830d0df7 | 275 | return true; |
samux | 1:fb4494783863 | 276 | } |
defrost | 39:2b8018d66b68 | 277 | DBG(SerialCommPort, "CMD2: %s",cmd); |
defrost | 39:2b8018d66b68 | 278 | DBG(SerialCommPort, "CMD4: %s",rcv); |
samux | 2:8e54830d0df7 | 279 | // if failed, retry and parse the response |
samux | 2:8e54830d0df7 | 280 | if (sendCommand(cmd, NULL, rcv, 5000)) { |
defrost | 39:2b8018d66b68 | 281 | DBG(SerialCommPort, "CMD5: %s",rcv); |
samux | 1:fb4494783863 | 282 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 1:fb4494783863 | 283 | if (strstr(rcv, "Connected") != NULL) { |
samux | 2:8e54830d0df7 | 284 | wait(0.25); |
samux | 1:fb4494783863 | 285 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 286 | return false; |
samux | 2:8e54830d0df7 | 287 | wait(0.25); |
samux | 2:8e54830d0df7 | 288 | if (!sendCommand(cmd, "OPEN", NULL, 10000)) |
samux | 1:fb4494783863 | 289 | return false; |
samux | 1:fb4494783863 | 290 | } else { |
samux | 1:fb4494783863 | 291 | return false; |
samux | 1:fb4494783863 | 292 | } |
samux | 1:fb4494783863 | 293 | } |
samux | 1:fb4494783863 | 294 | } else { |
defrost | 39:2b8018d66b68 | 295 | DBG(SerialCommPort, "CMD5: %s",rcv); |
samux | 1:fb4494783863 | 296 | return false; |
samux | 1:fb4494783863 | 297 | } |
defrost | 12:1aaea7f302e6 | 298 | |
samux | 2:8e54830d0df7 | 299 | |
samux | 1:fb4494783863 | 300 | state.tcp = true; |
samux | 1:fb4494783863 | 301 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 302 | |
samux | 1:fb4494783863 | 303 | return true; |
samux | 1:fb4494783863 | 304 | } |
samux | 1:fb4494783863 | 305 | |
samux | 1:fb4494783863 | 306 | |
samux | 1:fb4494783863 | 307 | bool Wifly::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 308 | { |
samux | 1:fb4494783863 | 309 | string h = host; |
samux | 1:fb4494783863 | 310 | char cmd[30], rcv[100]; |
samux | 1:fb4494783863 | 311 | int l = 0; |
samux | 1:fb4494783863 | 312 | char * point; |
samux | 1:fb4494783863 | 313 | int nb_digits = 0; |
samux | 1:fb4494783863 | 314 | |
samux | 1:fb4494783863 | 315 | // no dns needed |
samux | 1:fb4494783863 | 316 | int pos = h.find("."); |
samux | 1:fb4494783863 | 317 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 318 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 319 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 320 | } |
samux | 1:fb4494783863 | 321 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 322 | strcpy(ip, host); |
samux | 1:fb4494783863 | 323 | } |
samux | 1:fb4494783863 | 324 | // dns needed |
samux | 1:fb4494783863 | 325 | else { |
samux | 1:fb4494783863 | 326 | nb_digits = 0; |
samux | 1:fb4494783863 | 327 | sprintf(cmd, "lookup %s\r", host); |
samux | 1:fb4494783863 | 328 | if (!sendCommand(cmd, NULL, rcv)) |
samux | 1:fb4494783863 | 329 | return false; |
samux | 1:fb4494783863 | 330 | |
samux | 1:fb4494783863 | 331 | // look for the ip address |
samux | 1:fb4494783863 | 332 | char * begin = strstr(rcv, "=") + 1; |
samux | 1:fb4494783863 | 333 | for (int i = 0; i < 3; i++) { |
samux | 1:fb4494783863 | 334 | point = strstr(begin + l, "."); |
defrost | 39:2b8018d66b68 | 335 | DBG(SerialCommPort, "str: %s", begin + l); |
samux | 1:fb4494783863 | 336 | l += point - (begin + l) + 1; |
samux | 1:fb4494783863 | 337 | } |
defrost | 39:2b8018d66b68 | 338 | DBG(SerialCommPort, "str: %s", begin + l); |
samux | 1:fb4494783863 | 339 | while(*(begin + l + nb_digits) >= '0' && *(begin + l + nb_digits) <= '9') { |
defrost | 39:2b8018d66b68 | 340 | DBG(SerialCommPort, "digit: %c", *(begin + l + nb_digits)); |
samux | 1:fb4494783863 | 341 | nb_digits++; |
samux | 1:fb4494783863 | 342 | } |
samux | 1:fb4494783863 | 343 | memcpy(ip, begin, l + nb_digits); |
samux | 1:fb4494783863 | 344 | ip[l+nb_digits] = 0; |
defrost | 39:2b8018d66b68 | 345 | DBG(SerialCommPort, "ip from dns: %s", ip); |
samux | 1:fb4494783863 | 346 | } |
samux | 1:fb4494783863 | 347 | return true; |
samux | 1:fb4494783863 | 348 | } |
samux | 1:fb4494783863 | 349 | |
samux | 1:fb4494783863 | 350 | |
samux | 1:fb4494783863 | 351 | void Wifly::flush() |
samux | 1:fb4494783863 | 352 | { |
samux | 1:fb4494783863 | 353 | buf_wifly.flush(); |
samux | 1:fb4494783863 | 354 | } |
samux | 1:fb4494783863 | 355 | |
samux | 1:fb4494783863 | 356 | bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) |
samux | 1:fb4494783863 | 357 | { |
samux | 1:fb4494783863 | 358 | if (!state.cmd_mode) { |
samux | 1:fb4494783863 | 359 | cmdMode(); |
samux | 1:fb4494783863 | 360 | } |
samux | 1:fb4494783863 | 361 | if (send(cmd, strlen(cmd), ack, res, timeout) == -1) { |
defrost | 39:2b8018d66b68 | 362 | ERR(SerialCommPort, "sendCommand: cannot (%s)", cmd); |
defrost | 36:8774837f5a78 | 363 | if(strcmp(cmd, "exit") != 0){ |
defrost | 36:8774837f5a78 | 364 | exit(); |
defrost | 36:8774837f5a78 | 365 | }else{ |
defrost | 39:2b8018d66b68 | 366 | WARN(SerialCommPort, "Manually setting cmd_mode to false."); |
defrost | 36:8774837f5a78 | 367 | state.cmd_mode = false; |
defrost | 36:8774837f5a78 | 368 | } |
samux | 1:fb4494783863 | 369 | return false; |
samux | 1:fb4494783863 | 370 | } |
samux | 1:fb4494783863 | 371 | return true; |
samux | 1:fb4494783863 | 372 | } |
samux | 1:fb4494783863 | 373 | |
samux | 1:fb4494783863 | 374 | bool Wifly::cmdMode() |
samux | 1:fb4494783863 | 375 | { |
samux | 1:fb4494783863 | 376 | // if already in cmd mode, return |
samux | 1:fb4494783863 | 377 | if (state.cmd_mode) |
samux | 1:fb4494783863 | 378 | return true; |
samux | 1:fb4494783863 | 379 | |
lz307 | 7:3152fcc74390 | 380 | if (send("$$$", 3, "CMD") == -1 && send("\r",1,">") != true) { |
defrost | 39:2b8018d66b68 | 381 | ERR(SerialCommPort, "cannot enter in cmd mode"); |
samux | 2:8e54830d0df7 | 382 | exit(); |
samux | 1:fb4494783863 | 383 | return false; |
samux | 1:fb4494783863 | 384 | } |
samux | 1:fb4494783863 | 385 | state.cmd_mode = true; |
samux | 1:fb4494783863 | 386 | return true; |
samux | 1:fb4494783863 | 387 | } |
samux | 1:fb4494783863 | 388 | |
samux | 1:fb4494783863 | 389 | bool Wifly::disconnect() |
samux | 1:fb4494783863 | 390 | { |
samux | 1:fb4494783863 | 391 | // if already disconnected, return |
samux | 1:fb4494783863 | 392 | if (!state.associated) |
samux | 1:fb4494783863 | 393 | return true; |
samux | 2:8e54830d0df7 | 394 | |
samux | 1:fb4494783863 | 395 | if (!sendCommand("leave\r", "DeAuth")) |
samux | 1:fb4494783863 | 396 | return false; |
samux | 1:fb4494783863 | 397 | exit(); |
samux | 2:8e54830d0df7 | 398 | |
samux | 1:fb4494783863 | 399 | state.associated = false; |
samux | 1:fb4494783863 | 400 | return true; |
samux | 1:fb4494783863 | 401 | |
samux | 1:fb4494783863 | 402 | } |
samux | 1:fb4494783863 | 403 | |
samux | 1:fb4494783863 | 404 | bool Wifly::is_connected() |
samux | 1:fb4494783863 | 405 | { |
samux | 1:fb4494783863 | 406 | return (tcp_status.read() == 1) ? true : false; |
samux | 1:fb4494783863 | 407 | } |
samux | 1:fb4494783863 | 408 | |
samux | 1:fb4494783863 | 409 | |
samux | 1:fb4494783863 | 410 | void Wifly::reset() |
samux | 1:fb4494783863 | 411 | { |
defrost | 36:8774837f5a78 | 412 | |
samux | 1:fb4494783863 | 413 | reset_pin = 0; |
defrost | 36:8774837f5a78 | 414 | wifi.baud(9600); |
defrost | 36:8774837f5a78 | 415 | flush(); |
defrost | 36:8774837f5a78 | 416 | state.cmd_mode = false; |
defrost | 35:ce3afc021ec2 | 417 | wait(0.5); |
samux | 1:fb4494783863 | 418 | reset_pin = 1; |
defrost | 36:8774837f5a78 | 419 | // Set the serial port baud rate back to default: |
defrost | 35:ce3afc021ec2 | 420 | wait(0.5); |
defrost | 39:2b8018d66b68 | 421 | INFO(SerialCommPort, "Wifi Shield Reset"); |
defrost | 35:ce3afc021ec2 | 422 | setBaud(-1); |
defrost | 39:2b8018d66b68 | 423 | DBG(SerialCommPort, "Baud Rate updated from Reset."); |
samux | 1:fb4494783863 | 424 | } |
samux | 1:fb4494783863 | 425 | |
samux | 3:9aa05e19c62e | 426 | bool Wifly::reboot() |
samux | 3:9aa05e19c62e | 427 | { |
samux | 3:9aa05e19c62e | 428 | // if already in cmd mode, return |
samux | 3:9aa05e19c62e | 429 | if (!sendCommand("reboot\r")) |
samux | 3:9aa05e19c62e | 430 | return false; |
samux | 3:9aa05e19c62e | 431 | |
samux | 3:9aa05e19c62e | 432 | wait(0.3); |
samux | 3:9aa05e19c62e | 433 | |
samux | 3:9aa05e19c62e | 434 | state.cmd_mode = false; |
samux | 3:9aa05e19c62e | 435 | return true; |
samux | 3:9aa05e19c62e | 436 | } |
samux | 3:9aa05e19c62e | 437 | |
samux | 1:fb4494783863 | 438 | bool Wifly::close() |
samux | 1:fb4494783863 | 439 | { |
samux | 1:fb4494783863 | 440 | // if not connected, return |
samux | 1:fb4494783863 | 441 | if (!state.tcp) |
samux | 1:fb4494783863 | 442 | return true; |
samux | 2:8e54830d0df7 | 443 | |
samux | 1:fb4494783863 | 444 | wait(0.25); |
samux | 1:fb4494783863 | 445 | if (!sendCommand("close\r", "CLOS")) |
samux | 1:fb4494783863 | 446 | return false; |
samux | 1:fb4494783863 | 447 | exit(); |
samux | 2:8e54830d0df7 | 448 | |
samux | 1:fb4494783863 | 449 | state.tcp = false; |
samux | 1:fb4494783863 | 450 | return true; |
samux | 1:fb4494783863 | 451 | } |
samux | 1:fb4494783863 | 452 | |
samux | 1:fb4494783863 | 453 | |
samux | 1:fb4494783863 | 454 | int Wifly::putc(char c) |
samux | 1:fb4494783863 | 455 | { |
samux | 1:fb4494783863 | 456 | while (!wifi.writeable()); |
samux | 1:fb4494783863 | 457 | return wifi.putc(c); |
samux | 1:fb4494783863 | 458 | } |
samux | 1:fb4494783863 | 459 | |
samux | 1:fb4494783863 | 460 | |
samux | 1:fb4494783863 | 461 | bool Wifly::exit() |
samux | 1:fb4494783863 | 462 | { |
samux | 1:fb4494783863 | 463 | flush(); |
samux | 1:fb4494783863 | 464 | if (!state.cmd_mode) |
samux | 1:fb4494783863 | 465 | return true; |
samux | 1:fb4494783863 | 466 | if (!sendCommand("exit\r", "EXIT")) |
samux | 1:fb4494783863 | 467 | return false; |
samux | 1:fb4494783863 | 468 | state.cmd_mode = false; |
samux | 1:fb4494783863 | 469 | flush(); |
samux | 1:fb4494783863 | 470 | return true; |
samux | 1:fb4494783863 | 471 | } |
samux | 1:fb4494783863 | 472 | |
samux | 1:fb4494783863 | 473 | |
samux | 1:fb4494783863 | 474 | int Wifly::readable() |
samux | 1:fb4494783863 | 475 | { |
samux | 1:fb4494783863 | 476 | return buf_wifly.available(); |
samux | 1:fb4494783863 | 477 | } |
samux | 1:fb4494783863 | 478 | |
samux | 1:fb4494783863 | 479 | int Wifly::writeable() |
samux | 1:fb4494783863 | 480 | { |
samux | 1:fb4494783863 | 481 | return wifi.writeable(); |
samux | 1:fb4494783863 | 482 | } |
samux | 1:fb4494783863 | 483 | |
samux | 1:fb4494783863 | 484 | char Wifly::getc() |
samux | 1:fb4494783863 | 485 | { |
samux | 1:fb4494783863 | 486 | char c; |
defrost | 39:2b8018d66b68 | 487 | //DBG(SerialCommPort, "Waiting for buf_wifly.available() to return true..."); |
samux | 1:fb4494783863 | 488 | while (!buf_wifly.available()); |
defrost | 39:2b8018d66b68 | 489 | //DBG(SerialCommPort, "Dequeue-ing c..."); |
samux | 1:fb4494783863 | 490 | buf_wifly.dequeue(&c); |
defrost | 39:2b8018d66b68 | 491 | //DBG(SerialCommPort, "Return c:(%c)",c); |
samux | 1:fb4494783863 | 492 | return c; |
samux | 1:fb4494783863 | 493 | } |
samux | 1:fb4494783863 | 494 | |
samux | 1:fb4494783863 | 495 | void Wifly::handler_rx(void) |
samux | 1:fb4494783863 | 496 | { |
samux | 1:fb4494783863 | 497 | //read characters |
samux | 1:fb4494783863 | 498 | while (wifi.readable()) |
samux | 1:fb4494783863 | 499 | buf_wifly.queue(wifi.getc()); |
samux | 1:fb4494783863 | 500 | } |
samux | 1:fb4494783863 | 501 | |
samux | 1:fb4494783863 | 502 | void Wifly::attach_rx(bool callback) |
samux | 1:fb4494783863 | 503 | { |
samux | 1:fb4494783863 | 504 | if (!callback) |
samux | 1:fb4494783863 | 505 | wifi.attach(NULL); |
samux | 1:fb4494783863 | 506 | else |
defrost | 40:2bbc4753b23f | 507 | wifi.attach(this, &Wifly::handler_rx, Serial::RxIrq); |
defrost | 41:598257b3e319 | 508 | // If the function was static, could do: |
defrost | 41:598257b3e319 | 509 | //wifi.attach(&handler_rx); |
defrost | 40:2bbc4753b23f | 510 | //wifi.attach(NULL); |
samux | 1:fb4494783863 | 511 | } |
samux | 1:fb4494783863 | 512 | |
samux | 1:fb4494783863 | 513 | |
samux | 1:fb4494783863 | 514 | int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 515 | { |
samux | 1:fb4494783863 | 516 | char read; |
samux | 1:fb4494783863 | 517 | size_t found = string::npos; |
samux | 1:fb4494783863 | 518 | string checking; |
samux | 1:fb4494783863 | 519 | Timer tmr; |
samux | 1:fb4494783863 | 520 | int result = 0; |
defrost | 30:2c704a72d88a | 521 | |
defrost | 33:26a14bc6d90e | 522 | // Don't display the string that we will be sending because it is encoded, thus may screw up printf() |
defrost | 30:2c704a72d88a | 523 | if(ACK == NULL){ |
defrost | 39:2b8018d66b68 | 524 | DBG(SerialCommPort, "Will send a string, no ACK requested."); |
defrost | 30:2c704a72d88a | 525 | }else{ |
defrost | 39:2b8018d66b68 | 526 | DBG(SerialCommPort, "Will send a string, looking for ACK: %s", ACK); |
defrost | 30:2c704a72d88a | 527 | } |
samux | 1:fb4494783863 | 528 | |
samux | 1:fb4494783863 | 529 | attach_rx(false); |
samux | 1:fb4494783863 | 530 | |
samux | 1:fb4494783863 | 531 | //We flush the buffer |
samux | 1:fb4494783863 | 532 | while (wifi.readable()) |
samux | 1:fb4494783863 | 533 | wifi.getc(); |
samux | 1:fb4494783863 | 534 | |
samux | 1:fb4494783863 | 535 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 1:fb4494783863 | 536 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 537 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 538 | } else { |
samux | 1:fb4494783863 | 539 | //We flush the buffer |
samux | 1:fb4494783863 | 540 | while (wifi.readable()) |
samux | 1:fb4494783863 | 541 | wifi.getc(); |
samux | 1:fb4494783863 | 542 | |
samux | 1:fb4494783863 | 543 | tmr.start(); |
samux | 1:fb4494783863 | 544 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 545 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 546 | |
samux | 1:fb4494783863 | 547 | while (1) { |
samux | 1:fb4494783863 | 548 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 549 | //We flush the buffer |
samux | 1:fb4494783863 | 550 | while (wifi.readable()) |
samux | 1:fb4494783863 | 551 | wifi.getc(); |
samux | 1:fb4494783863 | 552 | |
defrost | 39:2b8018d66b68 | 553 | DBG(SerialCommPort, "check: %s", checking.c_str()); |
defrost | 39:2b8018d66b68 | 554 | DBG(SerialCommPort, "result ignored: %d", result) |
samux | 1:fb4494783863 | 555 | attach_rx(true); |
samux | 1:fb4494783863 | 556 | return -1; |
samux | 1:fb4494783863 | 557 | } else if (wifi.readable()) { |
samux | 1:fb4494783863 | 558 | read = wifi.getc(); |
samux | 1:fb4494783863 | 559 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 560 | checking += read; |
samux | 1:fb4494783863 | 561 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 562 | if (found != string::npos) { |
samux | 1:fb4494783863 | 563 | wait(0.01); |
samux | 1:fb4494783863 | 564 | |
samux | 1:fb4494783863 | 565 | //We flush the buffer |
samux | 1:fb4494783863 | 566 | while (wifi.readable()) |
samux | 1:fb4494783863 | 567 | wifi.getc(); |
samux | 1:fb4494783863 | 568 | |
samux | 1:fb4494783863 | 569 | break; |
samux | 1:fb4494783863 | 570 | } |
defrost | 24:705480e2e482 | 571 | } |
samux | 1:fb4494783863 | 572 | } |
samux | 1:fb4494783863 | 573 | } |
defrost | 39:2b8018d66b68 | 574 | DBG(SerialCommPort, "check: %s", checking.c_str()); |
defrost | 39:2b8018d66b68 | 575 | DBG(SerialCommPort, "result: %d", result) |
samux | 1:fb4494783863 | 576 | attach_rx(true); |
samux | 1:fb4494783863 | 577 | return result; |
samux | 1:fb4494783863 | 578 | } |
samux | 1:fb4494783863 | 579 | |
samux | 1:fb4494783863 | 580 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 581 | if ( res != NULL) { |
samux | 1:fb4494783863 | 582 | int i = 0; |
samux | 1:fb4494783863 | 583 | Timer timeout; |
samux | 1:fb4494783863 | 584 | timeout.start(); |
samux | 1:fb4494783863 | 585 | tmr.reset(); |
samux | 1:fb4494783863 | 586 | while (1) { |
samux | 1:fb4494783863 | 587 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 588 | if (i == 0) { |
samux | 1:fb4494783863 | 589 | res = NULL; |
samux | 1:fb4494783863 | 590 | break; |
samux | 1:fb4494783863 | 591 | } |
samux | 1:fb4494783863 | 592 | res[i] = '\0'; |
defrost | 39:2b8018d66b68 | 593 | DBG(SerialCommPort, "user str 1: %s", res); |
samux | 1:fb4494783863 | 594 | |
samux | 1:fb4494783863 | 595 | break; |
samux | 1:fb4494783863 | 596 | } else { |
samux | 1:fb4494783863 | 597 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 598 | res[i] = '\0'; |
defrost | 39:2b8018d66b68 | 599 | DBG(SerialCommPort, "user str2: %s", res); |
samux | 1:fb4494783863 | 600 | |
samux | 1:fb4494783863 | 601 | break; |
samux | 1:fb4494783863 | 602 | } |
samux | 1:fb4494783863 | 603 | if (wifi.readable()) { |
samux | 1:fb4494783863 | 604 | tmr.start(); |
samux | 1:fb4494783863 | 605 | read = wifi.getc(); |
samux | 1:fb4494783863 | 606 | |
samux | 1:fb4494783863 | 607 | // we drop \r and \n |
samux | 1:fb4494783863 | 608 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 609 | res[i++] = read; |
samux | 1:fb4494783863 | 610 | } |
samux | 1:fb4494783863 | 611 | } |
samux | 1:fb4494783863 | 612 | } |
samux | 1:fb4494783863 | 613 | } |
defrost | 39:2b8018d66b68 | 614 | DBG(SerialCommPort, "user str3: %s", res); |
samux | 1:fb4494783863 | 615 | } |
samux | 1:fb4494783863 | 616 | |
samux | 1:fb4494783863 | 617 | //We flush the buffer |
samux | 1:fb4494783863 | 618 | while (wifi.readable()) |
samux | 1:fb4494783863 | 619 | wifi.getc(); |
samux | 1:fb4494783863 | 620 | |
samux | 1:fb4494783863 | 621 | attach_rx(true); |
defrost | 39:2b8018d66b68 | 622 | DBG(SerialCommPort, "result: %d", result) |
samux | 1:fb4494783863 | 623 | return result; |
defrost | 30:2c704a72d88a | 624 | } |
defrost | 30:2c704a72d88a | 625 | |
defrost | 30:2c704a72d88a | 626 | int Wifly::checkNetworkStatus(void){ |
defrost | 30:2c704a72d88a | 627 | int status = 0; |
defrost | 30:2c704a72d88a | 628 | char rcv[128]; |
defrost | 31:2846355deb7e | 629 | unsigned int ConnectionReg; |
defrost | 30:2c704a72d88a | 630 | |
defrost | 31:2846355deb7e | 631 | if(!sendCommand("show c\r", NULL, rcv, 5000)){ |
defrost | 30:2c704a72d88a | 632 | status = 0; |
defrost | 30:2c704a72d88a | 633 | // the sendCommand function quits command mode if there is an error |
defrost | 30:2c704a72d88a | 634 | }else{ |
defrost | 30:2c704a72d88a | 635 | // check the response: |
defrost | 31:2846355deb7e | 636 | sscanf(rcv, "%x%*s", &ConnectionReg); |
defrost | 31:2846355deb7e | 637 | if(((ConnectionReg & CC_ASSOCIATION)>0) && ((ConnectionReg & CC_AUTHENTICATION)>0)){ |
defrost | 31:2846355deb7e | 638 | // Associated and Authenticated: |
defrost | 31:2846355deb7e | 639 | status = 3; |
defrost | 31:2846355deb7e | 640 | }else if((ConnectionReg & CC_ASSOCIATION)>0){ |
defrost | 31:2846355deb7e | 641 | // Associated: |
defrost | 31:2846355deb7e | 642 | status = 1; |
defrost | 31:2846355deb7e | 643 | }else if((ConnectionReg & CC_AUTHENTICATION)>0){ |
defrost | 31:2846355deb7e | 644 | // Athenticated: |
defrost | 31:2846355deb7e | 645 | status = 2; |
defrost | 31:2846355deb7e | 646 | }else{ |
defrost | 31:2846355deb7e | 647 | // Disconnected: |
defrost | 31:2846355deb7e | 648 | status = 0; |
defrost | 31:2846355deb7e | 649 | } |
defrost | 39:2b8018d66b68 | 650 | INFO(SerialCommPort, "Wifly Check network response: 0x%x, Status: %d", ConnectionReg, status); |
defrost | 30:2c704a72d88a | 651 | exit(); // Quit command mode |
defrost | 30:2c704a72d88a | 652 | } |
defrost | 30:2c704a72d88a | 653 | |
defrost | 30:2c704a72d88a | 654 | return status; |
defrost | 32:ab206f7f5090 | 655 | } |
defrost | 32:ab206f7f5090 | 656 | |
defrost | 32:ab206f7f5090 | 657 | void Wifly::sleep(void){ |
defrost | 32:ab206f7f5090 | 658 | // This function puts the module to sleep: |
defrost | 32:ab206f7f5090 | 659 | sendCommand("sleep\r", NULL); |
defrost | 32:ab206f7f5090 | 660 | // It will wake up from sleep if any characters are sent to it. |
defrost | 32:ab206f7f5090 | 661 | return; |
samux | 1:fb4494783863 | 662 | } |