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