The driver for the ESP8266 WiFi module

Dependents:   Robot Wi-fi

Fork of esp8266-driver by ESP8266

Committer:
group-ESP8266
Date:
Thu Jan 12 21:57:48 2017 +0000
Revision:
0:6946b0b9e323
Child:
2:77a1e2188ed9
Initial commit

Who changed what in which revision?

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