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