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