Reem M
/
http-server-example1
http
easy-connect/esp8266-driver/ESP8266/ESP8266.cpp@0:a49e37a83a7a, 2019-04-15 (annotated)
- Committer:
- DuaaAbusharkh
- Date:
- Mon Apr 15 08:30:22 2019 +0000
- Revision:
- 0:a49e37a83a7a
blink
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuaaAbusharkh | 0:a49e37a83a7a | 1 | /* ESP8266 Example |
DuaaAbusharkh | 0:a49e37a83a7a | 2 | * Copyright (c) 2015 ARM Limited |
DuaaAbusharkh | 0:a49e37a83a7a | 3 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DuaaAbusharkh | 0:a49e37a83a7a | 5 | * you may not use this file except in compliance with the License. |
DuaaAbusharkh | 0:a49e37a83a7a | 6 | * You may obtain a copy of the License at |
DuaaAbusharkh | 0:a49e37a83a7a | 7 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DuaaAbusharkh | 0:a49e37a83a7a | 9 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 10 | * Unless required by applicable law or agreed to in writing, software |
DuaaAbusharkh | 0:a49e37a83a7a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DuaaAbusharkh | 0:a49e37a83a7a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DuaaAbusharkh | 0:a49e37a83a7a | 13 | * See the License for the specific language governing permissions and |
DuaaAbusharkh | 0:a49e37a83a7a | 14 | * limitations under the License. |
DuaaAbusharkh | 0:a49e37a83a7a | 15 | */ |
DuaaAbusharkh | 0:a49e37a83a7a | 16 | |
DuaaAbusharkh | 0:a49e37a83a7a | 17 | #include "ESP8266.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 18 | |
DuaaAbusharkh | 0:a49e37a83a7a | 19 | ESP8266::ESP8266(PinName tx, PinName rx, bool debug) |
DuaaAbusharkh | 0:a49e37a83a7a | 20 | : _serial(tx, rx, 1024), _parser(_serial) |
DuaaAbusharkh | 0:a49e37a83a7a | 21 | , _packets(0), _packets_end(&_packets) |
DuaaAbusharkh | 0:a49e37a83a7a | 22 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 23 | _serial.baud(115200); |
DuaaAbusharkh | 0:a49e37a83a7a | 24 | _parser.debugOn(debug); |
DuaaAbusharkh | 0:a49e37a83a7a | 25 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 26 | |
DuaaAbusharkh | 0:a49e37a83a7a | 27 | int ESP8266::get_firmware_version() |
DuaaAbusharkh | 0:a49e37a83a7a | 28 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 29 | _parser.send("AT+GMR"); |
DuaaAbusharkh | 0:a49e37a83a7a | 30 | int version; |
DuaaAbusharkh | 0:a49e37a83a7a | 31 | if(_parser.recv("SDK version:%d", &version) && _parser.recv("OK")) { |
DuaaAbusharkh | 0:a49e37a83a7a | 32 | return version; |
DuaaAbusharkh | 0:a49e37a83a7a | 33 | } else { |
DuaaAbusharkh | 0:a49e37a83a7a | 34 | // Older firmware versions do not prefix the version with "SDK version: " |
DuaaAbusharkh | 0:a49e37a83a7a | 35 | return -1; |
DuaaAbusharkh | 0:a49e37a83a7a | 36 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 37 | |
DuaaAbusharkh | 0:a49e37a83a7a | 38 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 39 | |
DuaaAbusharkh | 0:a49e37a83a7a | 40 | bool ESP8266::startup(int mode) |
DuaaAbusharkh | 0:a49e37a83a7a | 41 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 42 | //only 3 valid modes |
DuaaAbusharkh | 0:a49e37a83a7a | 43 | if(mode < 1 || mode > 3) { |
DuaaAbusharkh | 0:a49e37a83a7a | 44 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 45 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 46 | |
DuaaAbusharkh | 0:a49e37a83a7a | 47 | bool success = _parser.send("AT+CWMODE_CUR=%d", mode) |
DuaaAbusharkh | 0:a49e37a83a7a | 48 | && _parser.recv("OK") |
DuaaAbusharkh | 0:a49e37a83a7a | 49 | && _parser.send("AT+CIPMUX=1") |
DuaaAbusharkh | 0:a49e37a83a7a | 50 | && _parser.recv("OK"); |
DuaaAbusharkh | 0:a49e37a83a7a | 51 | |
DuaaAbusharkh | 0:a49e37a83a7a | 52 | _parser.oob("+IPD", this, &ESP8266::_packet_handler); |
DuaaAbusharkh | 0:a49e37a83a7a | 53 | |
DuaaAbusharkh | 0:a49e37a83a7a | 54 | return success; |
DuaaAbusharkh | 0:a49e37a83a7a | 55 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 56 | |
DuaaAbusharkh | 0:a49e37a83a7a | 57 | bool ESP8266::reset(void) |
DuaaAbusharkh | 0:a49e37a83a7a | 58 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 59 | for (int i = 0; i < 2; i++) { |
DuaaAbusharkh | 0:a49e37a83a7a | 60 | if (_parser.send("AT+RST") |
DuaaAbusharkh | 0:a49e37a83a7a | 61 | && _parser.recv("OK\r\nready")) { |
DuaaAbusharkh | 0:a49e37a83a7a | 62 | return true; |
DuaaAbusharkh | 0:a49e37a83a7a | 63 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 64 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 65 | |
DuaaAbusharkh | 0:a49e37a83a7a | 66 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 67 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 68 | |
DuaaAbusharkh | 0:a49e37a83a7a | 69 | bool ESP8266::dhcp(bool enabled, int mode) |
DuaaAbusharkh | 0:a49e37a83a7a | 70 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 71 | //only 3 valid modes |
DuaaAbusharkh | 0:a49e37a83a7a | 72 | if(mode < 0 || mode > 2) { |
DuaaAbusharkh | 0:a49e37a83a7a | 73 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 74 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 75 | |
DuaaAbusharkh | 0:a49e37a83a7a | 76 | return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode) |
DuaaAbusharkh | 0:a49e37a83a7a | 77 | && _parser.recv("OK"); |
DuaaAbusharkh | 0:a49e37a83a7a | 78 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 79 | |
DuaaAbusharkh | 0:a49e37a83a7a | 80 | bool ESP8266::connect(const char *ap, const char *passPhrase) |
DuaaAbusharkh | 0:a49e37a83a7a | 81 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 82 | return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase) |
DuaaAbusharkh | 0:a49e37a83a7a | 83 | && _parser.recv("OK"); |
DuaaAbusharkh | 0:a49e37a83a7a | 84 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 85 | |
DuaaAbusharkh | 0:a49e37a83a7a | 86 | bool ESP8266::disconnect(void) |
DuaaAbusharkh | 0:a49e37a83a7a | 87 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 88 | return _parser.send("AT+CWQAP") && _parser.recv("OK"); |
DuaaAbusharkh | 0:a49e37a83a7a | 89 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 90 | |
DuaaAbusharkh | 0:a49e37a83a7a | 91 | const char *ESP8266::getIPAddress(void) |
DuaaAbusharkh | 0:a49e37a83a7a | 92 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 93 | if (!(_parser.send("AT+CIFSR") |
DuaaAbusharkh | 0:a49e37a83a7a | 94 | && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer) |
DuaaAbusharkh | 0:a49e37a83a7a | 95 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 96 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 97 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 98 | |
DuaaAbusharkh | 0:a49e37a83a7a | 99 | return _ip_buffer; |
DuaaAbusharkh | 0:a49e37a83a7a | 100 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 101 | |
DuaaAbusharkh | 0:a49e37a83a7a | 102 | const char *ESP8266::getMACAddress(void) |
DuaaAbusharkh | 0:a49e37a83a7a | 103 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 104 | if (!(_parser.send("AT+CIFSR") |
DuaaAbusharkh | 0:a49e37a83a7a | 105 | && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer) |
DuaaAbusharkh | 0:a49e37a83a7a | 106 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 107 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 108 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 109 | |
DuaaAbusharkh | 0:a49e37a83a7a | 110 | return _mac_buffer; |
DuaaAbusharkh | 0:a49e37a83a7a | 111 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 112 | |
DuaaAbusharkh | 0:a49e37a83a7a | 113 | const char *ESP8266::getGateway() |
DuaaAbusharkh | 0:a49e37a83a7a | 114 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 115 | if (!(_parser.send("AT+CIPSTA?") |
DuaaAbusharkh | 0:a49e37a83a7a | 116 | && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer) |
DuaaAbusharkh | 0:a49e37a83a7a | 117 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 118 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 119 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 120 | |
DuaaAbusharkh | 0:a49e37a83a7a | 121 | return _gateway_buffer; |
DuaaAbusharkh | 0:a49e37a83a7a | 122 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 123 | |
DuaaAbusharkh | 0:a49e37a83a7a | 124 | const char *ESP8266::getNetmask() |
DuaaAbusharkh | 0:a49e37a83a7a | 125 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 126 | if (!(_parser.send("AT+CIPSTA?") |
DuaaAbusharkh | 0:a49e37a83a7a | 127 | && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer) |
DuaaAbusharkh | 0:a49e37a83a7a | 128 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 129 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 130 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 131 | |
DuaaAbusharkh | 0:a49e37a83a7a | 132 | return _netmask_buffer; |
DuaaAbusharkh | 0:a49e37a83a7a | 133 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 134 | |
DuaaAbusharkh | 0:a49e37a83a7a | 135 | int8_t ESP8266::getRSSI() |
DuaaAbusharkh | 0:a49e37a83a7a | 136 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 137 | int8_t rssi; |
DuaaAbusharkh | 0:a49e37a83a7a | 138 | char bssid[18]; |
DuaaAbusharkh | 0:a49e37a83a7a | 139 | |
DuaaAbusharkh | 0:a49e37a83a7a | 140 | if (!(_parser.send("AT+CWJAP?") |
DuaaAbusharkh | 0:a49e37a83a7a | 141 | && _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid) |
DuaaAbusharkh | 0:a49e37a83a7a | 142 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 143 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 144 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 145 | |
DuaaAbusharkh | 0:a49e37a83a7a | 146 | if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid) |
DuaaAbusharkh | 0:a49e37a83a7a | 147 | && _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi) |
DuaaAbusharkh | 0:a49e37a83a7a | 148 | && _parser.recv("OK"))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 149 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 150 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 151 | |
DuaaAbusharkh | 0:a49e37a83a7a | 152 | return rssi; |
DuaaAbusharkh | 0:a49e37a83a7a | 153 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 154 | |
DuaaAbusharkh | 0:a49e37a83a7a | 155 | bool ESP8266::isConnected(void) |
DuaaAbusharkh | 0:a49e37a83a7a | 156 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 157 | return getIPAddress() != 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 158 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 159 | |
DuaaAbusharkh | 0:a49e37a83a7a | 160 | int ESP8266::scan(WiFiAccessPoint *res, unsigned limit) |
DuaaAbusharkh | 0:a49e37a83a7a | 161 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 162 | unsigned cnt = 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 163 | nsapi_wifi_ap_t ap; |
DuaaAbusharkh | 0:a49e37a83a7a | 164 | |
DuaaAbusharkh | 0:a49e37a83a7a | 165 | if (!_parser.send("AT+CWLAP")) { |
DuaaAbusharkh | 0:a49e37a83a7a | 166 | return NSAPI_ERROR_DEVICE_ERROR; |
DuaaAbusharkh | 0:a49e37a83a7a | 167 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 168 | |
DuaaAbusharkh | 0:a49e37a83a7a | 169 | while (recv_ap(&ap)) { |
DuaaAbusharkh | 0:a49e37a83a7a | 170 | if (cnt < limit) { |
DuaaAbusharkh | 0:a49e37a83a7a | 171 | res[cnt] = WiFiAccessPoint(ap); |
DuaaAbusharkh | 0:a49e37a83a7a | 172 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 173 | |
DuaaAbusharkh | 0:a49e37a83a7a | 174 | cnt++; |
DuaaAbusharkh | 0:a49e37a83a7a | 175 | if (limit != 0 && cnt >= limit) { |
DuaaAbusharkh | 0:a49e37a83a7a | 176 | break; |
DuaaAbusharkh | 0:a49e37a83a7a | 177 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 178 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 179 | |
DuaaAbusharkh | 0:a49e37a83a7a | 180 | return cnt; |
DuaaAbusharkh | 0:a49e37a83a7a | 181 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 182 | |
DuaaAbusharkh | 0:a49e37a83a7a | 183 | bool ESP8266::open(const char *type, int id, const char* addr, int port) |
DuaaAbusharkh | 0:a49e37a83a7a | 184 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 185 | //IDs only 0-4 |
DuaaAbusharkh | 0:a49e37a83a7a | 186 | if(id > 4) { |
DuaaAbusharkh | 0:a49e37a83a7a | 187 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 188 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 189 | return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port) |
DuaaAbusharkh | 0:a49e37a83a7a | 190 | && _parser.recv("OK"); |
DuaaAbusharkh | 0:a49e37a83a7a | 191 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 192 | |
DuaaAbusharkh | 0:a49e37a83a7a | 193 | bool ESP8266::dns_lookup(const char* name, char* ip) |
DuaaAbusharkh | 0:a49e37a83a7a | 194 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 195 | return _parser.send("AT+CIPDOMAIN=\"%s\"", name) && _parser.recv("+CIPDOMAIN:%s%*[\r]%*[\n]", ip); |
DuaaAbusharkh | 0:a49e37a83a7a | 196 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 197 | |
DuaaAbusharkh | 0:a49e37a83a7a | 198 | bool ESP8266::send(int id, const void *data, uint32_t amount) |
DuaaAbusharkh | 0:a49e37a83a7a | 199 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 200 | //May take a second try if device is busy |
DuaaAbusharkh | 0:a49e37a83a7a | 201 | for (unsigned i = 0; i < 2; i++) { |
DuaaAbusharkh | 0:a49e37a83a7a | 202 | if (_parser.send("AT+CIPSEND=%d,%d", id, amount) |
DuaaAbusharkh | 0:a49e37a83a7a | 203 | && _parser.recv(">") |
DuaaAbusharkh | 0:a49e37a83a7a | 204 | && _parser.write((char*)data, (int)amount) >= 0) { |
DuaaAbusharkh | 0:a49e37a83a7a | 205 | return true; |
DuaaAbusharkh | 0:a49e37a83a7a | 206 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 207 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 208 | |
DuaaAbusharkh | 0:a49e37a83a7a | 209 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 210 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 211 | |
DuaaAbusharkh | 0:a49e37a83a7a | 212 | void ESP8266::_packet_handler() |
DuaaAbusharkh | 0:a49e37a83a7a | 213 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 214 | int id; |
DuaaAbusharkh | 0:a49e37a83a7a | 215 | uint32_t amount; |
DuaaAbusharkh | 0:a49e37a83a7a | 216 | |
DuaaAbusharkh | 0:a49e37a83a7a | 217 | // parse out the packet |
DuaaAbusharkh | 0:a49e37a83a7a | 218 | if (!_parser.recv(",%d,%d:", &id, &amount)) { |
DuaaAbusharkh | 0:a49e37a83a7a | 219 | return; |
DuaaAbusharkh | 0:a49e37a83a7a | 220 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 221 | |
DuaaAbusharkh | 0:a49e37a83a7a | 222 | struct packet *packet = (struct packet*)malloc( |
DuaaAbusharkh | 0:a49e37a83a7a | 223 | sizeof(struct packet) + amount); |
DuaaAbusharkh | 0:a49e37a83a7a | 224 | if (!packet) { |
DuaaAbusharkh | 0:a49e37a83a7a | 225 | return; |
DuaaAbusharkh | 0:a49e37a83a7a | 226 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 227 | |
DuaaAbusharkh | 0:a49e37a83a7a | 228 | packet->id = id; |
DuaaAbusharkh | 0:a49e37a83a7a | 229 | packet->len = amount; |
DuaaAbusharkh | 0:a49e37a83a7a | 230 | packet->next = 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 231 | |
DuaaAbusharkh | 0:a49e37a83a7a | 232 | if (!(_parser.read((char*)(packet + 1), amount))) { |
DuaaAbusharkh | 0:a49e37a83a7a | 233 | free(packet); |
DuaaAbusharkh | 0:a49e37a83a7a | 234 | return; |
DuaaAbusharkh | 0:a49e37a83a7a | 235 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 236 | |
DuaaAbusharkh | 0:a49e37a83a7a | 237 | // append to packet list |
DuaaAbusharkh | 0:a49e37a83a7a | 238 | *_packets_end = packet; |
DuaaAbusharkh | 0:a49e37a83a7a | 239 | _packets_end = &packet->next; |
DuaaAbusharkh | 0:a49e37a83a7a | 240 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 241 | |
DuaaAbusharkh | 0:a49e37a83a7a | 242 | int32_t ESP8266::recv(int id, void *data, uint32_t amount) |
DuaaAbusharkh | 0:a49e37a83a7a | 243 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 244 | while (true) { |
DuaaAbusharkh | 0:a49e37a83a7a | 245 | // check if any packets are ready for us |
DuaaAbusharkh | 0:a49e37a83a7a | 246 | for (struct packet **p = &_packets; *p; p = &(*p)->next) { |
DuaaAbusharkh | 0:a49e37a83a7a | 247 | if ((*p)->id == id) { |
DuaaAbusharkh | 0:a49e37a83a7a | 248 | struct packet *q = *p; |
DuaaAbusharkh | 0:a49e37a83a7a | 249 | |
DuaaAbusharkh | 0:a49e37a83a7a | 250 | if (q->len <= amount) { // Return and remove full packet |
DuaaAbusharkh | 0:a49e37a83a7a | 251 | memcpy(data, q+1, q->len); |
DuaaAbusharkh | 0:a49e37a83a7a | 252 | |
DuaaAbusharkh | 0:a49e37a83a7a | 253 | if (_packets_end == &(*p)->next) { |
DuaaAbusharkh | 0:a49e37a83a7a | 254 | _packets_end = p; |
DuaaAbusharkh | 0:a49e37a83a7a | 255 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 256 | *p = (*p)->next; |
DuaaAbusharkh | 0:a49e37a83a7a | 257 | |
DuaaAbusharkh | 0:a49e37a83a7a | 258 | uint32_t len = q->len; |
DuaaAbusharkh | 0:a49e37a83a7a | 259 | free(q); |
DuaaAbusharkh | 0:a49e37a83a7a | 260 | return len; |
DuaaAbusharkh | 0:a49e37a83a7a | 261 | } else { // return only partial packet |
DuaaAbusharkh | 0:a49e37a83a7a | 262 | memcpy(data, q+1, amount); |
DuaaAbusharkh | 0:a49e37a83a7a | 263 | |
DuaaAbusharkh | 0:a49e37a83a7a | 264 | q->len -= amount; |
DuaaAbusharkh | 0:a49e37a83a7a | 265 | memmove(q+1, (uint8_t*)(q+1) + amount, q->len); |
DuaaAbusharkh | 0:a49e37a83a7a | 266 | |
DuaaAbusharkh | 0:a49e37a83a7a | 267 | return amount; |
DuaaAbusharkh | 0:a49e37a83a7a | 268 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 269 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 270 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 271 | |
DuaaAbusharkh | 0:a49e37a83a7a | 272 | // Wait for inbound packet |
DuaaAbusharkh | 0:a49e37a83a7a | 273 | if (!_parser.recv("OK")) { |
DuaaAbusharkh | 0:a49e37a83a7a | 274 | return -1; |
DuaaAbusharkh | 0:a49e37a83a7a | 275 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 276 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 277 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 278 | |
DuaaAbusharkh | 0:a49e37a83a7a | 279 | bool ESP8266::close(int id) |
DuaaAbusharkh | 0:a49e37a83a7a | 280 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 281 | //May take a second try if device is busy |
DuaaAbusharkh | 0:a49e37a83a7a | 282 | for (unsigned i = 0; i < 2; i++) { |
DuaaAbusharkh | 0:a49e37a83a7a | 283 | if (_parser.send("AT+CIPCLOSE=%d", id) |
DuaaAbusharkh | 0:a49e37a83a7a | 284 | && _parser.recv("OK")) { |
DuaaAbusharkh | 0:a49e37a83a7a | 285 | return true; |
DuaaAbusharkh | 0:a49e37a83a7a | 286 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 287 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 288 | |
DuaaAbusharkh | 0:a49e37a83a7a | 289 | return false; |
DuaaAbusharkh | 0:a49e37a83a7a | 290 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 291 | |
DuaaAbusharkh | 0:a49e37a83a7a | 292 | void ESP8266::setTimeout(uint32_t timeout_ms) |
DuaaAbusharkh | 0:a49e37a83a7a | 293 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 294 | _parser.setTimeout(timeout_ms); |
DuaaAbusharkh | 0:a49e37a83a7a | 295 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 296 | |
DuaaAbusharkh | 0:a49e37a83a7a | 297 | bool ESP8266::readable() |
DuaaAbusharkh | 0:a49e37a83a7a | 298 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 299 | return _serial.readable(); |
DuaaAbusharkh | 0:a49e37a83a7a | 300 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 301 | |
DuaaAbusharkh | 0:a49e37a83a7a | 302 | bool ESP8266::writeable() |
DuaaAbusharkh | 0:a49e37a83a7a | 303 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 304 | return _serial.writeable(); |
DuaaAbusharkh | 0:a49e37a83a7a | 305 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 306 | |
DuaaAbusharkh | 0:a49e37a83a7a | 307 | void ESP8266::attach(Callback<void()> func) |
DuaaAbusharkh | 0:a49e37a83a7a | 308 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 309 | _serial.attach(func); |
DuaaAbusharkh | 0:a49e37a83a7a | 310 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 311 | |
DuaaAbusharkh | 0:a49e37a83a7a | 312 | bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap) |
DuaaAbusharkh | 0:a49e37a83a7a | 313 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 314 | int sec; |
DuaaAbusharkh | 0:a49e37a83a7a | 315 | bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid, |
DuaaAbusharkh | 0:a49e37a83a7a | 316 | &ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], |
DuaaAbusharkh | 0:a49e37a83a7a | 317 | &ap->bssid[5], &ap->channel); |
DuaaAbusharkh | 0:a49e37a83a7a | 318 | |
DuaaAbusharkh | 0:a49e37a83a7a | 319 | ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN; |
DuaaAbusharkh | 0:a49e37a83a7a | 320 | |
DuaaAbusharkh | 0:a49e37a83a7a | 321 | return ret; |
DuaaAbusharkh | 0:a49e37a83a7a | 322 | } |