wifly/socket interface for wifly modules
Dependents: PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019
Wifly/Wifly.cpp@0:6ffb0aeb3972, 2012-08-17 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 17 08:28:04 2012 +0000
- Revision:
- 0:6ffb0aeb3972
- Child:
- 1:8f04181f9ad8
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:6ffb0aeb3972 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 0:6ffb0aeb3972 | 2 | * |
samux | 0:6ffb0aeb3972 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 0:6ffb0aeb3972 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 0:6ffb0aeb3972 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 0:6ffb0aeb3972 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 0:6ffb0aeb3972 | 7 | * furnished to do so, subject to the following conditions: |
samux | 0:6ffb0aeb3972 | 8 | * |
samux | 0:6ffb0aeb3972 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 0:6ffb0aeb3972 | 10 | * substantial portions of the Software. |
samux | 0:6ffb0aeb3972 | 11 | * |
samux | 0:6ffb0aeb3972 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 0:6ffb0aeb3972 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 0:6ffb0aeb3972 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 0:6ffb0aeb3972 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:6ffb0aeb3972 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 0:6ffb0aeb3972 | 17 | */ |
samux | 0:6ffb0aeb3972 | 18 | |
samux | 0:6ffb0aeb3972 | 19 | #include "mbed.h" |
samux | 0:6ffb0aeb3972 | 20 | #include "Wifly.h" |
samux | 0:6ffb0aeb3972 | 21 | #include <string> |
samux | 0:6ffb0aeb3972 | 22 | |
samux | 0:6ffb0aeb3972 | 23 | //Debug is disabled by default |
samux | 0:6ffb0aeb3972 | 24 | #if 0 |
samux | 0:6ffb0aeb3972 | 25 | #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__); |
samux | 0:6ffb0aeb3972 | 26 | #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__); |
samux | 0:6ffb0aeb3972 | 27 | #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__); |
samux | 0:6ffb0aeb3972 | 28 | #else |
samux | 0:6ffb0aeb3972 | 29 | #define DBG(x, ...) |
samux | 0:6ffb0aeb3972 | 30 | #define WARN(x, ...) |
samux | 0:6ffb0aeb3972 | 31 | #define ERR(x, ...) |
samux | 0:6ffb0aeb3972 | 32 | #endif |
samux | 0:6ffb0aeb3972 | 33 | |
samux | 0:6ffb0aeb3972 | 34 | #define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__); |
samux | 0:6ffb0aeb3972 | 35 | |
samux | 0:6ffb0aeb3972 | 36 | Wifly * Wifly::inst; |
samux | 0:6ffb0aeb3972 | 37 | |
samux | 0:6ffb0aeb3972 | 38 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa): |
samux | 0:6ffb0aeb3972 | 39 | wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256) { |
samux | 0:6ffb0aeb3972 | 40 | this->wpa = wpa; |
samux | 0:6ffb0aeb3972 | 41 | this->phrase = phrase; |
samux | 0:6ffb0aeb3972 | 42 | this->ssid = ssid; |
samux | 0:6ffb0aeb3972 | 43 | inst = this; |
samux | 0:6ffb0aeb3972 | 44 | attach_rx(false); |
samux | 0:6ffb0aeb3972 | 45 | cmd_mode = false; |
samux | 0:6ffb0aeb3972 | 46 | } |
samux | 0:6ffb0aeb3972 | 47 | |
samux | 0:6ffb0aeb3972 | 48 | bool Wifly::join() { |
samux | 0:6ffb0aeb3972 | 49 | char cmd[100]; |
samux | 0:6ffb0aeb3972 | 50 | |
samux | 0:6ffb0aeb3972 | 51 | if (!sendCommand("set comm time 20\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 52 | return false; |
samux | 0:6ffb0aeb3972 | 53 | |
samux | 0:6ffb0aeb3972 | 54 | if (!sendCommand("set comm size 128\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 55 | return false; |
samux | 0:6ffb0aeb3972 | 56 | |
samux | 0:6ffb0aeb3972 | 57 | if (!sendCommand("set sys iofunc 0x40\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 58 | return false; |
samux | 0:6ffb0aeb3972 | 59 | |
samux | 0:6ffb0aeb3972 | 60 | // no string sent to the tcp client |
samux | 0:6ffb0aeb3972 | 61 | if (!sendCommand("set comm remote 0\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 62 | return false; |
samux | 0:6ffb0aeb3972 | 63 | |
samux | 0:6ffb0aeb3972 | 64 | // tcp protocol |
samux | 0:6ffb0aeb3972 | 65 | if (!sendCommand("set ip proto 2\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 66 | return false; |
samux | 0:6ffb0aeb3972 | 67 | |
samux | 0:6ffb0aeb3972 | 68 | // tcp retry |
samux | 0:6ffb0aeb3972 | 69 | if (!sendCommand("set ip flags 0x7\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 70 | return false; |
samux | 0:6ffb0aeb3972 | 71 | |
samux | 0:6ffb0aeb3972 | 72 | //no echo |
samux | 0:6ffb0aeb3972 | 73 | if (!sendCommand("set u m 1\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 74 | return false; |
samux | 0:6ffb0aeb3972 | 75 | |
samux | 0:6ffb0aeb3972 | 76 | //dhcp |
samux | 0:6ffb0aeb3972 | 77 | sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0); |
samux | 0:6ffb0aeb3972 | 78 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 79 | return false; |
samux | 0:6ffb0aeb3972 | 80 | |
samux | 0:6ffb0aeb3972 | 81 | if (dhcp) { |
samux | 0:6ffb0aeb3972 | 82 | if (!sendCommand("get w\r", NULL, cmd)) |
samux | 0:6ffb0aeb3972 | 83 | return false; |
samux | 0:6ffb0aeb3972 | 84 | |
samux | 0:6ffb0aeb3972 | 85 | // if the wlan parameters are already known, we just connect without resending all commands |
samux | 0:6ffb0aeb3972 | 86 | if ((string(cmd).find(ssid) != string::npos) && (string(cmd).find(phrase) != string::npos) ) { |
samux | 0:6ffb0aeb3972 | 87 | DBG("ssid found && phrase found\r\n"); |
samux | 0:6ffb0aeb3972 | 88 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 89 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 90 | while (tmr.read() < 2) { |
samux | 0:6ffb0aeb3972 | 91 | send("show c\r", 7, NULL, cmd); |
samux | 0:6ffb0aeb3972 | 92 | DBG("join: state of conn: %s\r\n", cmd); |
samux | 0:6ffb0aeb3972 | 93 | if ((cmd[2] - '0') & 0x01) { |
samux | 0:6ffb0aeb3972 | 94 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); |
samux | 0:6ffb0aeb3972 | 95 | // save |
samux | 0:6ffb0aeb3972 | 96 | if (!sendCommand("save\r", "Stor")) |
samux | 0:6ffb0aeb3972 | 97 | return false; |
samux | 0:6ffb0aeb3972 | 98 | reboot(); |
samux | 0:6ffb0aeb3972 | 99 | return true; |
samux | 0:6ffb0aeb3972 | 100 | } |
samux | 0:6ffb0aeb3972 | 101 | wait(0.2); |
samux | 0:6ffb0aeb3972 | 102 | } |
samux | 0:6ffb0aeb3972 | 103 | } |
samux | 0:6ffb0aeb3972 | 104 | |
samux | 0:6ffb0aeb3972 | 105 | DBG("getw: %s\r\n", cmd); |
samux | 0:6ffb0aeb3972 | 106 | } |
samux | 0:6ffb0aeb3972 | 107 | |
samux | 0:6ffb0aeb3972 | 108 | // ssid |
samux | 0:6ffb0aeb3972 | 109 | sprintf(cmd, "set w s %s\r", ssid); |
samux | 0:6ffb0aeb3972 | 110 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 111 | return false; |
samux | 0:6ffb0aeb3972 | 112 | |
samux | 0:6ffb0aeb3972 | 113 | //auth |
samux | 0:6ffb0aeb3972 | 114 | sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1); |
samux | 0:6ffb0aeb3972 | 115 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 116 | return false; |
samux | 0:6ffb0aeb3972 | 117 | |
samux | 0:6ffb0aeb3972 | 118 | // if no dhcp, set ip, netmask and gateway |
samux | 0:6ffb0aeb3972 | 119 | if (!dhcp) { |
samux | 0:6ffb0aeb3972 | 120 | DBG("not dhcp\r"); |
samux | 0:6ffb0aeb3972 | 121 | |
samux | 0:6ffb0aeb3972 | 122 | sprintf(cmd, "set i a %s\r\n", ip); |
samux | 0:6ffb0aeb3972 | 123 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 124 | return false; |
samux | 0:6ffb0aeb3972 | 125 | |
samux | 0:6ffb0aeb3972 | 126 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 0:6ffb0aeb3972 | 127 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 128 | return false; |
samux | 0:6ffb0aeb3972 | 129 | |
samux | 0:6ffb0aeb3972 | 130 | sprintf(cmd, "set i g %s\r", gateway); |
samux | 0:6ffb0aeb3972 | 131 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 132 | return false; |
samux | 0:6ffb0aeb3972 | 133 | } |
samux | 0:6ffb0aeb3972 | 134 | |
samux | 0:6ffb0aeb3972 | 135 | //key step |
samux | 0:6ffb0aeb3972 | 136 | if (wpa) |
samux | 0:6ffb0aeb3972 | 137 | sprintf(cmd, "set w p %s\r", phrase); |
samux | 0:6ffb0aeb3972 | 138 | else |
samux | 0:6ffb0aeb3972 | 139 | sprintf(cmd, "set w k %s\r", phrase); |
samux | 0:6ffb0aeb3972 | 140 | |
samux | 0:6ffb0aeb3972 | 141 | if (!sendCommand(cmd, "AOK")) |
samux | 0:6ffb0aeb3972 | 142 | return false; |
samux | 0:6ffb0aeb3972 | 143 | |
samux | 0:6ffb0aeb3972 | 144 | // auto join |
samux | 0:6ffb0aeb3972 | 145 | if (!sendCommand("set w j 1\r", "AOK")) |
samux | 0:6ffb0aeb3972 | 146 | return false; |
samux | 0:6ffb0aeb3972 | 147 | |
samux | 0:6ffb0aeb3972 | 148 | // save |
samux | 0:6ffb0aeb3972 | 149 | if (!sendCommand("save\r", "Stor")) |
samux | 0:6ffb0aeb3972 | 150 | return false; |
samux | 0:6ffb0aeb3972 | 151 | |
samux | 0:6ffb0aeb3972 | 152 | //join the network |
samux | 0:6ffb0aeb3972 | 153 | sprintf(cmd, "join %s\r", ssid); |
samux | 0:6ffb0aeb3972 | 154 | if (!sendCommand(cmd, "Associated")) |
samux | 0:6ffb0aeb3972 | 155 | return false; |
samux | 0:6ffb0aeb3972 | 156 | |
samux | 0:6ffb0aeb3972 | 157 | reboot(); |
samux | 0:6ffb0aeb3972 | 158 | |
samux | 0:6ffb0aeb3972 | 159 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); |
samux | 0:6ffb0aeb3972 | 160 | return true; |
samux | 0:6ffb0aeb3972 | 161 | } |
samux | 0:6ffb0aeb3972 | 162 | |
samux | 0:6ffb0aeb3972 | 163 | bool Wifly::reboot() { |
samux | 0:6ffb0aeb3972 | 164 | if (!sendCommand("reboot\r", "boot")) |
samux | 0:6ffb0aeb3972 | 165 | return false; |
samux | 0:6ffb0aeb3972 | 166 | wait(0.2); |
samux | 0:6ffb0aeb3972 | 167 | // wait that the module fetches an IP |
samux | 0:6ffb0aeb3972 | 168 | if (dhcp) { |
samux | 0:6ffb0aeb3972 | 169 | while(send("", 0, "DHCP=ON") == -1); |
samux | 0:6ffb0aeb3972 | 170 | } else { |
samux | 0:6ffb0aeb3972 | 171 | while(send("", 0, "Static") == -1); |
samux | 0:6ffb0aeb3972 | 172 | } |
samux | 0:6ffb0aeb3972 | 173 | flush(); |
samux | 0:6ffb0aeb3972 | 174 | cmd_mode = false; |
samux | 0:6ffb0aeb3972 | 175 | return true; |
samux | 0:6ffb0aeb3972 | 176 | } |
samux | 0:6ffb0aeb3972 | 177 | |
samux | 0:6ffb0aeb3972 | 178 | |
samux | 0:6ffb0aeb3972 | 179 | void Wifly::flush() { |
samux | 0:6ffb0aeb3972 | 180 | buf_wifly.flush(); |
samux | 0:6ffb0aeb3972 | 181 | } |
samux | 0:6ffb0aeb3972 | 182 | |
samux | 0:6ffb0aeb3972 | 183 | bool Wifly::sendCommand(const char * cmd, const char * ack, char * res) { |
samux | 0:6ffb0aeb3972 | 184 | if (!cmd_mode) { |
samux | 0:6ffb0aeb3972 | 185 | cmdMode(); |
samux | 0:6ffb0aeb3972 | 186 | } |
samux | 0:6ffb0aeb3972 | 187 | if (send(cmd, strlen(cmd), ack, res) == -1) { |
samux | 0:6ffb0aeb3972 | 188 | ERR("sendCommand: cannot %s\r\n", cmd); |
samux | 0:6ffb0aeb3972 | 189 | exit(); |
samux | 0:6ffb0aeb3972 | 190 | return false; |
samux | 0:6ffb0aeb3972 | 191 | } |
samux | 0:6ffb0aeb3972 | 192 | return true; |
samux | 0:6ffb0aeb3972 | 193 | } |
samux | 0:6ffb0aeb3972 | 194 | |
samux | 0:6ffb0aeb3972 | 195 | bool Wifly::cmdMode() { |
samux | 0:6ffb0aeb3972 | 196 | if (send("$$$", 3, "CMD") == -1) { |
samux | 0:6ffb0aeb3972 | 197 | ERR("cannot enter in cmd mode\r\n"); |
samux | 0:6ffb0aeb3972 | 198 | return false; |
samux | 0:6ffb0aeb3972 | 199 | } |
samux | 0:6ffb0aeb3972 | 200 | cmd_mode = true; |
samux | 0:6ffb0aeb3972 | 201 | return true; |
samux | 0:6ffb0aeb3972 | 202 | } |
samux | 0:6ffb0aeb3972 | 203 | |
samux | 0:6ffb0aeb3972 | 204 | bool Wifly::leave() { |
samux | 0:6ffb0aeb3972 | 205 | if (!sendCommand("leave\r", "DeAuth")) |
samux | 0:6ffb0aeb3972 | 206 | return false; |
samux | 0:6ffb0aeb3972 | 207 | exit(); |
samux | 0:6ffb0aeb3972 | 208 | return true; |
samux | 0:6ffb0aeb3972 | 209 | |
samux | 0:6ffb0aeb3972 | 210 | } |
samux | 0:6ffb0aeb3972 | 211 | |
samux | 0:6ffb0aeb3972 | 212 | bool Wifly::is_connected() { |
samux | 0:6ffb0aeb3972 | 213 | return (tcp_status.read() == 1) ? true : false; |
samux | 0:6ffb0aeb3972 | 214 | } |
samux | 0:6ffb0aeb3972 | 215 | |
samux | 0:6ffb0aeb3972 | 216 | |
samux | 0:6ffb0aeb3972 | 217 | void Wifly::reset() { |
samux | 0:6ffb0aeb3972 | 218 | reset_pin = 0; |
samux | 0:6ffb0aeb3972 | 219 | wait(0.2); |
samux | 0:6ffb0aeb3972 | 220 | reset_pin = 1; |
samux | 0:6ffb0aeb3972 | 221 | wait(0.2); |
samux | 0:6ffb0aeb3972 | 222 | } |
samux | 0:6ffb0aeb3972 | 223 | |
samux | 0:6ffb0aeb3972 | 224 | bool Wifly::close() { |
samux | 0:6ffb0aeb3972 | 225 | wait(0.25); |
samux | 0:6ffb0aeb3972 | 226 | if (!cmdMode()) |
samux | 0:6ffb0aeb3972 | 227 | return false; |
samux | 0:6ffb0aeb3972 | 228 | if (send("close\r", 6, "CLOS") == -1) |
samux | 0:6ffb0aeb3972 | 229 | return false; |
samux | 0:6ffb0aeb3972 | 230 | exit(); |
samux | 0:6ffb0aeb3972 | 231 | return true; |
samux | 0:6ffb0aeb3972 | 232 | } |
samux | 0:6ffb0aeb3972 | 233 | |
samux | 0:6ffb0aeb3972 | 234 | |
samux | 0:6ffb0aeb3972 | 235 | int Wifly::putc(char c) { |
samux | 0:6ffb0aeb3972 | 236 | while (!wifi.writeable()); |
samux | 0:6ffb0aeb3972 | 237 | return wifi.putc(c); |
samux | 0:6ffb0aeb3972 | 238 | } |
samux | 0:6ffb0aeb3972 | 239 | |
samux | 0:6ffb0aeb3972 | 240 | |
samux | 0:6ffb0aeb3972 | 241 | bool Wifly::read(char * str) { |
samux | 0:6ffb0aeb3972 | 242 | int length = buf_wifly.available(); |
samux | 0:6ffb0aeb3972 | 243 | if (length == 0) |
samux | 0:6ffb0aeb3972 | 244 | return false; |
samux | 0:6ffb0aeb3972 | 245 | for (int i = 0; i < length; i++) |
samux | 0:6ffb0aeb3972 | 246 | buf_wifly.dequeue(&str[i]); |
samux | 0:6ffb0aeb3972 | 247 | str[length] = 0; |
samux | 0:6ffb0aeb3972 | 248 | return true; |
samux | 0:6ffb0aeb3972 | 249 | } |
samux | 0:6ffb0aeb3972 | 250 | |
samux | 0:6ffb0aeb3972 | 251 | |
samux | 0:6ffb0aeb3972 | 252 | bool Wifly::exit() { |
samux | 0:6ffb0aeb3972 | 253 | flush(); |
samux | 0:6ffb0aeb3972 | 254 | if (send("exit\r", 5, "EXIT") == -1) |
samux | 0:6ffb0aeb3972 | 255 | return false; |
samux | 0:6ffb0aeb3972 | 256 | cmd_mode = false; |
samux | 0:6ffb0aeb3972 | 257 | return true; |
samux | 0:6ffb0aeb3972 | 258 | } |
samux | 0:6ffb0aeb3972 | 259 | |
samux | 0:6ffb0aeb3972 | 260 | |
samux | 0:6ffb0aeb3972 | 261 | int Wifly::readable() { |
samux | 0:6ffb0aeb3972 | 262 | return buf_wifly.available(); |
samux | 0:6ffb0aeb3972 | 263 | } |
samux | 0:6ffb0aeb3972 | 264 | |
samux | 0:6ffb0aeb3972 | 265 | int Wifly::writeable() { |
samux | 0:6ffb0aeb3972 | 266 | return wifi.writeable(); |
samux | 0:6ffb0aeb3972 | 267 | } |
samux | 0:6ffb0aeb3972 | 268 | |
samux | 0:6ffb0aeb3972 | 269 | char Wifly::getc() { |
samux | 0:6ffb0aeb3972 | 270 | char c; |
samux | 0:6ffb0aeb3972 | 271 | while (!buf_wifly.available()); |
samux | 0:6ffb0aeb3972 | 272 | buf_wifly.dequeue(&c); |
samux | 0:6ffb0aeb3972 | 273 | return c; |
samux | 0:6ffb0aeb3972 | 274 | } |
samux | 0:6ffb0aeb3972 | 275 | |
samux | 0:6ffb0aeb3972 | 276 | void Wifly::handler_rx(void) { |
samux | 0:6ffb0aeb3972 | 277 | //read characters |
samux | 0:6ffb0aeb3972 | 278 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 279 | buf_wifly.queue(wifi.getc()); |
samux | 0:6ffb0aeb3972 | 280 | } |
samux | 0:6ffb0aeb3972 | 281 | |
samux | 0:6ffb0aeb3972 | 282 | void Wifly::attach_rx(bool callback) { |
samux | 0:6ffb0aeb3972 | 283 | if (!callback) |
samux | 0:6ffb0aeb3972 | 284 | wifi.attach(NULL); |
samux | 0:6ffb0aeb3972 | 285 | else |
samux | 0:6ffb0aeb3972 | 286 | wifi.attach(this, &Wifly::handler_rx); |
samux | 0:6ffb0aeb3972 | 287 | } |
samux | 0:6ffb0aeb3972 | 288 | |
samux | 0:6ffb0aeb3972 | 289 | |
samux | 0:6ffb0aeb3972 | 290 | int Wifly::send(const char * str, int len, const char * ACK, char * res) { |
samux | 0:6ffb0aeb3972 | 291 | char read; |
samux | 0:6ffb0aeb3972 | 292 | size_t found = string::npos; |
samux | 0:6ffb0aeb3972 | 293 | string checking; |
samux | 0:6ffb0aeb3972 | 294 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 295 | int result = 0; |
samux | 0:6ffb0aeb3972 | 296 | |
samux | 0:6ffb0aeb3972 | 297 | DBG("will send: %s\r\n",str); |
samux | 0:6ffb0aeb3972 | 298 | |
samux | 0:6ffb0aeb3972 | 299 | attach_rx(false); |
samux | 0:6ffb0aeb3972 | 300 | |
samux | 0:6ffb0aeb3972 | 301 | //We flush the buffer |
samux | 0:6ffb0aeb3972 | 302 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 303 | wifi.getc(); |
samux | 0:6ffb0aeb3972 | 304 | |
samux | 0:6ffb0aeb3972 | 305 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 0:6ffb0aeb3972 | 306 | for (int i = 0; i < len; i++) |
samux | 0:6ffb0aeb3972 | 307 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 0:6ffb0aeb3972 | 308 | } else { |
samux | 0:6ffb0aeb3972 | 309 | //We flush the buffer |
samux | 0:6ffb0aeb3972 | 310 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 311 | wifi.getc(); |
samux | 0:6ffb0aeb3972 | 312 | |
samux | 0:6ffb0aeb3972 | 313 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 314 | for (int i = 0; i < len; i++) |
samux | 0:6ffb0aeb3972 | 315 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 0:6ffb0aeb3972 | 316 | |
samux | 0:6ffb0aeb3972 | 317 | while (1) { |
samux | 0:6ffb0aeb3972 | 318 | if (tmr.read() > 3) { |
samux | 0:6ffb0aeb3972 | 319 | //We flush the buffer |
samux | 0:6ffb0aeb3972 | 320 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 321 | wifi.getc(); |
samux | 0:6ffb0aeb3972 | 322 | |
samux | 0:6ffb0aeb3972 | 323 | DBG("check: %s\r\n", checking.c_str()); |
samux | 0:6ffb0aeb3972 | 324 | |
samux | 0:6ffb0aeb3972 | 325 | attach_rx(true); |
samux | 0:6ffb0aeb3972 | 326 | return -1; |
samux | 0:6ffb0aeb3972 | 327 | } else if (wifi.readable()) { |
samux | 0:6ffb0aeb3972 | 328 | read = wifi.getc(); |
samux | 0:6ffb0aeb3972 | 329 | if ( read != '\r' && read != '\n') { |
samux | 0:6ffb0aeb3972 | 330 | checking += read; |
samux | 0:6ffb0aeb3972 | 331 | found = checking.find(ACK); |
samux | 0:6ffb0aeb3972 | 332 | if (found != string::npos) { |
samux | 0:6ffb0aeb3972 | 333 | wait(0.01); |
samux | 0:6ffb0aeb3972 | 334 | |
samux | 0:6ffb0aeb3972 | 335 | //We flush the buffer |
samux | 0:6ffb0aeb3972 | 336 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 337 | wifi.getc(); |
samux | 0:6ffb0aeb3972 | 338 | |
samux | 0:6ffb0aeb3972 | 339 | break; |
samux | 0:6ffb0aeb3972 | 340 | } |
samux | 0:6ffb0aeb3972 | 341 | } |
samux | 0:6ffb0aeb3972 | 342 | } |
samux | 0:6ffb0aeb3972 | 343 | } |
samux | 0:6ffb0aeb3972 | 344 | DBG("check: %s\r\n", checking.c_str()); |
samux | 0:6ffb0aeb3972 | 345 | |
samux | 0:6ffb0aeb3972 | 346 | attach_rx(true); |
samux | 0:6ffb0aeb3972 | 347 | return result; |
samux | 0:6ffb0aeb3972 | 348 | } |
samux | 0:6ffb0aeb3972 | 349 | |
samux | 0:6ffb0aeb3972 | 350 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 0:6ffb0aeb3972 | 351 | if ( res != NULL) { |
samux | 0:6ffb0aeb3972 | 352 | int i = 0; |
samux | 0:6ffb0aeb3972 | 353 | Timer timeout; |
samux | 0:6ffb0aeb3972 | 354 | timeout.start(); |
samux | 0:6ffb0aeb3972 | 355 | tmr.reset(); |
samux | 0:6ffb0aeb3972 | 356 | while (1) { |
samux | 0:6ffb0aeb3972 | 357 | if (timeout.read() > 3) { |
samux | 0:6ffb0aeb3972 | 358 | if (i == 0) { |
samux | 0:6ffb0aeb3972 | 359 | res = NULL; |
samux | 0:6ffb0aeb3972 | 360 | break; |
samux | 0:6ffb0aeb3972 | 361 | } |
samux | 0:6ffb0aeb3972 | 362 | res[i] = '\0'; |
samux | 0:6ffb0aeb3972 | 363 | DBG("user str 1: %s\r\n", res); |
samux | 0:6ffb0aeb3972 | 364 | |
samux | 0:6ffb0aeb3972 | 365 | break; |
samux | 0:6ffb0aeb3972 | 366 | } else { |
samux | 0:6ffb0aeb3972 | 367 | if (tmr.read_ms() > 1000) { |
samux | 0:6ffb0aeb3972 | 368 | res[i] = '\0'; |
samux | 0:6ffb0aeb3972 | 369 | DBG("user str: %s\r\n", res); |
samux | 0:6ffb0aeb3972 | 370 | |
samux | 0:6ffb0aeb3972 | 371 | break; |
samux | 0:6ffb0aeb3972 | 372 | } |
samux | 0:6ffb0aeb3972 | 373 | if (wifi.readable()) { |
samux | 0:6ffb0aeb3972 | 374 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 375 | read = wifi.getc(); |
samux | 0:6ffb0aeb3972 | 376 | |
samux | 0:6ffb0aeb3972 | 377 | // we drop \r and \n |
samux | 0:6ffb0aeb3972 | 378 | if ( read != '\r' && read != '\n') { |
samux | 0:6ffb0aeb3972 | 379 | res[i++] = read; |
samux | 0:6ffb0aeb3972 | 380 | } |
samux | 0:6ffb0aeb3972 | 381 | } |
samux | 0:6ffb0aeb3972 | 382 | } |
samux | 0:6ffb0aeb3972 | 383 | } |
samux | 0:6ffb0aeb3972 | 384 | DBG("user str: %s\r\n", res); |
samux | 0:6ffb0aeb3972 | 385 | } |
samux | 0:6ffb0aeb3972 | 386 | |
samux | 0:6ffb0aeb3972 | 387 | //We flush the buffer |
samux | 0:6ffb0aeb3972 | 388 | while (wifi.readable()) |
samux | 0:6ffb0aeb3972 | 389 | wifi.getc(); |
samux | 0:6ffb0aeb3972 | 390 | |
samux | 0:6ffb0aeb3972 | 391 | attach_rx(true); |
samux | 0:6ffb0aeb3972 | 392 | DBG("result: %d\r\n", result) |
samux | 0:6ffb0aeb3972 | 393 | return result; |
samux | 0:6ffb0aeb3972 | 394 | } |