Christopher Foley
/
ESE519_WIFI_CONTROLLER
a
Fork of ESE519_Lab6_part1_skeleton by
esp8266-driver/ESP8266Interface.cpp@7:ca226305d28b, 2018-10-24 (annotated)
- Committer:
- hydroguy45
- Date:
- Wed Oct 24 20:54:43 2018 +0000
- Revision:
- 7:ca226305d28b
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hydroguy45 | 7:ca226305d28b | 1 | /* ESP8266 implementation of NetworkInterfaceAPI |
hydroguy45 | 7:ca226305d28b | 2 | * Copyright (c) 2015 ARM Limited |
hydroguy45 | 7:ca226305d28b | 3 | * |
hydroguy45 | 7:ca226305d28b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hydroguy45 | 7:ca226305d28b | 5 | * you may not use this file except in compliance with the License. |
hydroguy45 | 7:ca226305d28b | 6 | * You may obtain a copy of the License at |
hydroguy45 | 7:ca226305d28b | 7 | * |
hydroguy45 | 7:ca226305d28b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hydroguy45 | 7:ca226305d28b | 9 | * |
hydroguy45 | 7:ca226305d28b | 10 | * Unless required by applicable law or agreed to in writing, software |
hydroguy45 | 7:ca226305d28b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hydroguy45 | 7:ca226305d28b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hydroguy45 | 7:ca226305d28b | 13 | * See the License for the specific language governing permissions and |
hydroguy45 | 7:ca226305d28b | 14 | * limitations under the License. |
hydroguy45 | 7:ca226305d28b | 15 | */ |
hydroguy45 | 7:ca226305d28b | 16 | |
hydroguy45 | 7:ca226305d28b | 17 | #include <string.h> |
hydroguy45 | 7:ca226305d28b | 18 | #include "ESP8266Interface.h" |
hydroguy45 | 7:ca226305d28b | 19 | |
hydroguy45 | 7:ca226305d28b | 20 | // Various timeouts for different ESP8266 operations |
hydroguy45 | 7:ca226305d28b | 21 | #define ESP8266_CONNECT_TIMEOUT 15000 |
hydroguy45 | 7:ca226305d28b | 22 | #define ESP8266_SEND_TIMEOUT 500 |
hydroguy45 | 7:ca226305d28b | 23 | #define ESP8266_RECV_TIMEOUT 0 |
hydroguy45 | 7:ca226305d28b | 24 | #define ESP8266_MISC_TIMEOUT 500 |
hydroguy45 | 7:ca226305d28b | 25 | |
hydroguy45 | 7:ca226305d28b | 26 | // ESP8266Interface implementation |
hydroguy45 | 7:ca226305d28b | 27 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
hydroguy45 | 7:ca226305d28b | 28 | : _esp(tx, rx, debug) |
hydroguy45 | 7:ca226305d28b | 29 | { |
hydroguy45 | 7:ca226305d28b | 30 | memset(_ids, 0, sizeof(_ids)); |
hydroguy45 | 7:ca226305d28b | 31 | memset(_cbs, 0, sizeof(_cbs)); |
hydroguy45 | 7:ca226305d28b | 32 | |
hydroguy45 | 7:ca226305d28b | 33 | _esp.attach(this, &ESP8266Interface::event); |
hydroguy45 | 7:ca226305d28b | 34 | } |
hydroguy45 | 7:ca226305d28b | 35 | |
hydroguy45 | 7:ca226305d28b | 36 | int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
hydroguy45 | 7:ca226305d28b | 37 | uint8_t channel) |
hydroguy45 | 7:ca226305d28b | 38 | { |
hydroguy45 | 7:ca226305d28b | 39 | if (channel != 0) { |
hydroguy45 | 7:ca226305d28b | 40 | return NSAPI_ERROR_UNSUPPORTED; |
hydroguy45 | 7:ca226305d28b | 41 | } |
hydroguy45 | 7:ca226305d28b | 42 | |
hydroguy45 | 7:ca226305d28b | 43 | set_credentials(ssid, pass, security); |
hydroguy45 | 7:ca226305d28b | 44 | return connect(); |
hydroguy45 | 7:ca226305d28b | 45 | } |
hydroguy45 | 7:ca226305d28b | 46 | |
hydroguy45 | 7:ca226305d28b | 47 | int ESP8266Interface::connect() |
hydroguy45 | 7:ca226305d28b | 48 | { |
hydroguy45 | 7:ca226305d28b | 49 | _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 50 | |
hydroguy45 | 7:ca226305d28b | 51 | if (!_esp.startup(3)) { |
hydroguy45 | 7:ca226305d28b | 52 | return NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 53 | } |
hydroguy45 | 7:ca226305d28b | 54 | |
hydroguy45 | 7:ca226305d28b | 55 | if (!_esp.dhcp(true, 1)) { |
hydroguy45 | 7:ca226305d28b | 56 | return NSAPI_ERROR_DHCP_FAILURE; |
hydroguy45 | 7:ca226305d28b | 57 | } |
hydroguy45 | 7:ca226305d28b | 58 | |
hydroguy45 | 7:ca226305d28b | 59 | if (!_esp.connect(ap_ssid, ap_pass)) { |
hydroguy45 | 7:ca226305d28b | 60 | return NSAPI_ERROR_NO_CONNECTION; |
hydroguy45 | 7:ca226305d28b | 61 | } |
hydroguy45 | 7:ca226305d28b | 62 | |
hydroguy45 | 7:ca226305d28b | 63 | if (!_esp.getIPAddress()) { |
hydroguy45 | 7:ca226305d28b | 64 | return NSAPI_ERROR_DHCP_FAILURE; |
hydroguy45 | 7:ca226305d28b | 65 | } |
hydroguy45 | 7:ca226305d28b | 66 | |
hydroguy45 | 7:ca226305d28b | 67 | return NSAPI_ERROR_OK; |
hydroguy45 | 7:ca226305d28b | 68 | } |
hydroguy45 | 7:ca226305d28b | 69 | |
hydroguy45 | 7:ca226305d28b | 70 | int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
hydroguy45 | 7:ca226305d28b | 71 | { |
hydroguy45 | 7:ca226305d28b | 72 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
hydroguy45 | 7:ca226305d28b | 73 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
hydroguy45 | 7:ca226305d28b | 74 | |
hydroguy45 | 7:ca226305d28b | 75 | memset(ap_pass, 0, sizeof(ap_pass)); |
hydroguy45 | 7:ca226305d28b | 76 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
hydroguy45 | 7:ca226305d28b | 77 | |
hydroguy45 | 7:ca226305d28b | 78 | ap_sec = security; |
hydroguy45 | 7:ca226305d28b | 79 | |
hydroguy45 | 7:ca226305d28b | 80 | return 0; |
hydroguy45 | 7:ca226305d28b | 81 | } |
hydroguy45 | 7:ca226305d28b | 82 | |
hydroguy45 | 7:ca226305d28b | 83 | int ESP8266Interface::set_channel(uint8_t channel) |
hydroguy45 | 7:ca226305d28b | 84 | { |
hydroguy45 | 7:ca226305d28b | 85 | return NSAPI_ERROR_UNSUPPORTED; |
hydroguy45 | 7:ca226305d28b | 86 | } |
hydroguy45 | 7:ca226305d28b | 87 | |
hydroguy45 | 7:ca226305d28b | 88 | |
hydroguy45 | 7:ca226305d28b | 89 | int ESP8266Interface::disconnect() |
hydroguy45 | 7:ca226305d28b | 90 | { |
hydroguy45 | 7:ca226305d28b | 91 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 92 | |
hydroguy45 | 7:ca226305d28b | 93 | if (!_esp.disconnect()) { |
hydroguy45 | 7:ca226305d28b | 94 | return NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 95 | } |
hydroguy45 | 7:ca226305d28b | 96 | |
hydroguy45 | 7:ca226305d28b | 97 | return NSAPI_ERROR_OK; |
hydroguy45 | 7:ca226305d28b | 98 | } |
hydroguy45 | 7:ca226305d28b | 99 | |
hydroguy45 | 7:ca226305d28b | 100 | const char *ESP8266Interface::get_ip_address() |
hydroguy45 | 7:ca226305d28b | 101 | { |
hydroguy45 | 7:ca226305d28b | 102 | return _esp.getIPAddress(); |
hydroguy45 | 7:ca226305d28b | 103 | } |
hydroguy45 | 7:ca226305d28b | 104 | |
hydroguy45 | 7:ca226305d28b | 105 | const char *ESP8266Interface::get_mac_address() |
hydroguy45 | 7:ca226305d28b | 106 | { |
hydroguy45 | 7:ca226305d28b | 107 | return _esp.getMACAddress(); |
hydroguy45 | 7:ca226305d28b | 108 | } |
hydroguy45 | 7:ca226305d28b | 109 | |
hydroguy45 | 7:ca226305d28b | 110 | const char *ESP8266Interface::get_gateway() |
hydroguy45 | 7:ca226305d28b | 111 | { |
hydroguy45 | 7:ca226305d28b | 112 | return _esp.getGateway(); |
hydroguy45 | 7:ca226305d28b | 113 | } |
hydroguy45 | 7:ca226305d28b | 114 | |
hydroguy45 | 7:ca226305d28b | 115 | const char *ESP8266Interface::get_netmask() |
hydroguy45 | 7:ca226305d28b | 116 | { |
hydroguy45 | 7:ca226305d28b | 117 | return _esp.getNetmask(); |
hydroguy45 | 7:ca226305d28b | 118 | } |
hydroguy45 | 7:ca226305d28b | 119 | |
hydroguy45 | 7:ca226305d28b | 120 | int8_t ESP8266Interface::get_rssi() |
hydroguy45 | 7:ca226305d28b | 121 | { |
hydroguy45 | 7:ca226305d28b | 122 | return _esp.getRSSI(); |
hydroguy45 | 7:ca226305d28b | 123 | } |
hydroguy45 | 7:ca226305d28b | 124 | |
hydroguy45 | 7:ca226305d28b | 125 | int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count) |
hydroguy45 | 7:ca226305d28b | 126 | { |
hydroguy45 | 7:ca226305d28b | 127 | return _esp.scan(res, count); |
hydroguy45 | 7:ca226305d28b | 128 | } |
hydroguy45 | 7:ca226305d28b | 129 | |
hydroguy45 | 7:ca226305d28b | 130 | struct esp8266_socket { |
hydroguy45 | 7:ca226305d28b | 131 | int id; |
hydroguy45 | 7:ca226305d28b | 132 | nsapi_protocol_t proto; |
hydroguy45 | 7:ca226305d28b | 133 | bool connected; |
hydroguy45 | 7:ca226305d28b | 134 | SocketAddress addr; |
hydroguy45 | 7:ca226305d28b | 135 | }; |
hydroguy45 | 7:ca226305d28b | 136 | |
hydroguy45 | 7:ca226305d28b | 137 | int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) |
hydroguy45 | 7:ca226305d28b | 138 | { |
hydroguy45 | 7:ca226305d28b | 139 | // Look for an unused socket |
hydroguy45 | 7:ca226305d28b | 140 | int id = -1; |
hydroguy45 | 7:ca226305d28b | 141 | |
hydroguy45 | 7:ca226305d28b | 142 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
hydroguy45 | 7:ca226305d28b | 143 | if (!_ids[i]) { |
hydroguy45 | 7:ca226305d28b | 144 | id = i; |
hydroguy45 | 7:ca226305d28b | 145 | _ids[i] = true; |
hydroguy45 | 7:ca226305d28b | 146 | break; |
hydroguy45 | 7:ca226305d28b | 147 | } |
hydroguy45 | 7:ca226305d28b | 148 | } |
hydroguy45 | 7:ca226305d28b | 149 | |
hydroguy45 | 7:ca226305d28b | 150 | if (id == -1) { |
hydroguy45 | 7:ca226305d28b | 151 | return NSAPI_ERROR_NO_SOCKET; |
hydroguy45 | 7:ca226305d28b | 152 | } |
hydroguy45 | 7:ca226305d28b | 153 | |
hydroguy45 | 7:ca226305d28b | 154 | struct esp8266_socket *socket = new struct esp8266_socket; |
hydroguy45 | 7:ca226305d28b | 155 | if (!socket) { |
hydroguy45 | 7:ca226305d28b | 156 | return NSAPI_ERROR_NO_SOCKET; |
hydroguy45 | 7:ca226305d28b | 157 | } |
hydroguy45 | 7:ca226305d28b | 158 | |
hydroguy45 | 7:ca226305d28b | 159 | socket->id = id; |
hydroguy45 | 7:ca226305d28b | 160 | socket->proto = proto; |
hydroguy45 | 7:ca226305d28b | 161 | socket->connected = false; |
hydroguy45 | 7:ca226305d28b | 162 | *handle = socket; |
hydroguy45 | 7:ca226305d28b | 163 | return 0; |
hydroguy45 | 7:ca226305d28b | 164 | } |
hydroguy45 | 7:ca226305d28b | 165 | |
hydroguy45 | 7:ca226305d28b | 166 | int ESP8266Interface::socket_close(void *handle) |
hydroguy45 | 7:ca226305d28b | 167 | { |
hydroguy45 | 7:ca226305d28b | 168 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 169 | int err = 0; |
hydroguy45 | 7:ca226305d28b | 170 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 171 | |
hydroguy45 | 7:ca226305d28b | 172 | if (!_esp.close(socket->id)) { |
hydroguy45 | 7:ca226305d28b | 173 | err = NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 174 | } |
hydroguy45 | 7:ca226305d28b | 175 | |
hydroguy45 | 7:ca226305d28b | 176 | _ids[socket->id] = false; |
hydroguy45 | 7:ca226305d28b | 177 | delete socket; |
hydroguy45 | 7:ca226305d28b | 178 | return err; |
hydroguy45 | 7:ca226305d28b | 179 | } |
hydroguy45 | 7:ca226305d28b | 180 | |
hydroguy45 | 7:ca226305d28b | 181 | int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) |
hydroguy45 | 7:ca226305d28b | 182 | { |
hydroguy45 | 7:ca226305d28b | 183 | return NSAPI_ERROR_UNSUPPORTED; |
hydroguy45 | 7:ca226305d28b | 184 | } |
hydroguy45 | 7:ca226305d28b | 185 | |
hydroguy45 | 7:ca226305d28b | 186 | int ESP8266Interface::socket_listen(void *handle, int backlog) |
hydroguy45 | 7:ca226305d28b | 187 | { |
hydroguy45 | 7:ca226305d28b | 188 | return NSAPI_ERROR_UNSUPPORTED; |
hydroguy45 | 7:ca226305d28b | 189 | } |
hydroguy45 | 7:ca226305d28b | 190 | |
hydroguy45 | 7:ca226305d28b | 191 | int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) |
hydroguy45 | 7:ca226305d28b | 192 | { |
hydroguy45 | 7:ca226305d28b | 193 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 194 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 195 | |
hydroguy45 | 7:ca226305d28b | 196 | const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP"; |
hydroguy45 | 7:ca226305d28b | 197 | if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
hydroguy45 | 7:ca226305d28b | 198 | return NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 199 | } |
hydroguy45 | 7:ca226305d28b | 200 | |
hydroguy45 | 7:ca226305d28b | 201 | socket->connected = true; |
hydroguy45 | 7:ca226305d28b | 202 | return 0; |
hydroguy45 | 7:ca226305d28b | 203 | } |
hydroguy45 | 7:ca226305d28b | 204 | |
hydroguy45 | 7:ca226305d28b | 205 | int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
hydroguy45 | 7:ca226305d28b | 206 | { |
hydroguy45 | 7:ca226305d28b | 207 | return NSAPI_ERROR_UNSUPPORTED; |
hydroguy45 | 7:ca226305d28b | 208 | } |
hydroguy45 | 7:ca226305d28b | 209 | |
hydroguy45 | 7:ca226305d28b | 210 | int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) |
hydroguy45 | 7:ca226305d28b | 211 | { |
hydroguy45 | 7:ca226305d28b | 212 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 213 | _esp.setTimeout(ESP8266_SEND_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 214 | |
hydroguy45 | 7:ca226305d28b | 215 | if (!_esp.send(socket->id, data, size)) { |
hydroguy45 | 7:ca226305d28b | 216 | return NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 217 | } |
hydroguy45 | 7:ca226305d28b | 218 | |
hydroguy45 | 7:ca226305d28b | 219 | return size; |
hydroguy45 | 7:ca226305d28b | 220 | } |
hydroguy45 | 7:ca226305d28b | 221 | |
hydroguy45 | 7:ca226305d28b | 222 | int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) |
hydroguy45 | 7:ca226305d28b | 223 | { |
hydroguy45 | 7:ca226305d28b | 224 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 225 | _esp.setTimeout(ESP8266_RECV_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 226 | |
hydroguy45 | 7:ca226305d28b | 227 | int32_t recv = _esp.recv(socket->id, data, size); |
hydroguy45 | 7:ca226305d28b | 228 | if (recv < 0) { |
hydroguy45 | 7:ca226305d28b | 229 | return NSAPI_ERROR_WOULD_BLOCK; |
hydroguy45 | 7:ca226305d28b | 230 | } |
hydroguy45 | 7:ca226305d28b | 231 | |
hydroguy45 | 7:ca226305d28b | 232 | return recv; |
hydroguy45 | 7:ca226305d28b | 233 | } |
hydroguy45 | 7:ca226305d28b | 234 | |
hydroguy45 | 7:ca226305d28b | 235 | int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
hydroguy45 | 7:ca226305d28b | 236 | { |
hydroguy45 | 7:ca226305d28b | 237 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 238 | |
hydroguy45 | 7:ca226305d28b | 239 | if (socket->connected && socket->addr != addr) { |
hydroguy45 | 7:ca226305d28b | 240 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
hydroguy45 | 7:ca226305d28b | 241 | if (!_esp.close(socket->id)) { |
hydroguy45 | 7:ca226305d28b | 242 | return NSAPI_ERROR_DEVICE_ERROR; |
hydroguy45 | 7:ca226305d28b | 243 | } |
hydroguy45 | 7:ca226305d28b | 244 | socket->connected = false; |
hydroguy45 | 7:ca226305d28b | 245 | } |
hydroguy45 | 7:ca226305d28b | 246 | |
hydroguy45 | 7:ca226305d28b | 247 | if (!socket->connected) { |
hydroguy45 | 7:ca226305d28b | 248 | int err = socket_connect(socket, addr); |
hydroguy45 | 7:ca226305d28b | 249 | if (err < 0) { |
hydroguy45 | 7:ca226305d28b | 250 | return err; |
hydroguy45 | 7:ca226305d28b | 251 | } |
hydroguy45 | 7:ca226305d28b | 252 | socket->addr = addr; |
hydroguy45 | 7:ca226305d28b | 253 | } |
hydroguy45 | 7:ca226305d28b | 254 | |
hydroguy45 | 7:ca226305d28b | 255 | return socket_send(socket, data, size); |
hydroguy45 | 7:ca226305d28b | 256 | } |
hydroguy45 | 7:ca226305d28b | 257 | |
hydroguy45 | 7:ca226305d28b | 258 | int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
hydroguy45 | 7:ca226305d28b | 259 | { |
hydroguy45 | 7:ca226305d28b | 260 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 261 | int ret = socket_recv(socket, data, size); |
hydroguy45 | 7:ca226305d28b | 262 | if (ret >= 0 && addr) { |
hydroguy45 | 7:ca226305d28b | 263 | *addr = socket->addr; |
hydroguy45 | 7:ca226305d28b | 264 | } |
hydroguy45 | 7:ca226305d28b | 265 | |
hydroguy45 | 7:ca226305d28b | 266 | return ret; |
hydroguy45 | 7:ca226305d28b | 267 | } |
hydroguy45 | 7:ca226305d28b | 268 | |
hydroguy45 | 7:ca226305d28b | 269 | void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
hydroguy45 | 7:ca226305d28b | 270 | { |
hydroguy45 | 7:ca226305d28b | 271 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
hydroguy45 | 7:ca226305d28b | 272 | _cbs[socket->id].callback = callback; |
hydroguy45 | 7:ca226305d28b | 273 | _cbs[socket->id].data = data; |
hydroguy45 | 7:ca226305d28b | 274 | } |
hydroguy45 | 7:ca226305d28b | 275 | |
hydroguy45 | 7:ca226305d28b | 276 | void ESP8266Interface::event() { |
hydroguy45 | 7:ca226305d28b | 277 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
hydroguy45 | 7:ca226305d28b | 278 | if (_cbs[i].callback) { |
hydroguy45 | 7:ca226305d28b | 279 | _cbs[i].callback(_cbs[i].data); |
hydroguy45 | 7:ca226305d28b | 280 | } |
hydroguy45 | 7:ca226305d28b | 281 | } |
hydroguy45 | 7:ca226305d28b | 282 | } |