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