Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

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