The driver for the ESP8266 WiFi module - isConnected function added, working and tested with mbed os 5.7.6

Fork of esp8266-driver by ESP8266

Committer:
mareszym
Date:
Sun Nov 04 12:50:49 2018 +0000
Revision:
2:0bc0b654995c
Parent:
0:6946b0b9e323
isConnected function added

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
mareszym 2:0bc0b654995c 70 bool ESP8266Interface::isConnected()
mareszym 2:0bc0b654995c 71 {
mareszym 2:0bc0b654995c 72 return _esp.isConnected();
mareszym 2:0bc0b654995c 73 }
mareszym 2:0bc0b654995c 74
group-ESP8266 0:6946b0b9e323 75 int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
group-ESP8266 0:6946b0b9e323 76 {
group-ESP8266 0:6946b0b9e323 77 memset(ap_ssid, 0, sizeof(ap_ssid));
group-ESP8266 0:6946b0b9e323 78 strncpy(ap_ssid, ssid, sizeof(ap_ssid));
group-ESP8266 0:6946b0b9e323 79
group-ESP8266 0:6946b0b9e323 80 memset(ap_pass, 0, sizeof(ap_pass));
group-ESP8266 0:6946b0b9e323 81 strncpy(ap_pass, pass, sizeof(ap_pass));
group-ESP8266 0:6946b0b9e323 82
group-ESP8266 0:6946b0b9e323 83 ap_sec = security;
group-ESP8266 0:6946b0b9e323 84
group-ESP8266 0:6946b0b9e323 85 return 0;
group-ESP8266 0:6946b0b9e323 86 }
group-ESP8266 0:6946b0b9e323 87
group-ESP8266 0:6946b0b9e323 88 int ESP8266Interface::set_channel(uint8_t channel)
group-ESP8266 0:6946b0b9e323 89 {
group-ESP8266 0:6946b0b9e323 90 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 91 }
group-ESP8266 0:6946b0b9e323 92
group-ESP8266 0:6946b0b9e323 93
group-ESP8266 0:6946b0b9e323 94 int ESP8266Interface::disconnect()
group-ESP8266 0:6946b0b9e323 95 {
group-ESP8266 0:6946b0b9e323 96 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 97
group-ESP8266 0:6946b0b9e323 98 if (!_esp.disconnect()) {
group-ESP8266 0:6946b0b9e323 99 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 100 }
group-ESP8266 0:6946b0b9e323 101
group-ESP8266 0:6946b0b9e323 102 return NSAPI_ERROR_OK;
group-ESP8266 0:6946b0b9e323 103 }
group-ESP8266 0:6946b0b9e323 104
group-ESP8266 0:6946b0b9e323 105 const char *ESP8266Interface::get_ip_address()
group-ESP8266 0:6946b0b9e323 106 {
group-ESP8266 0:6946b0b9e323 107 return _esp.getIPAddress();
group-ESP8266 0:6946b0b9e323 108 }
group-ESP8266 0:6946b0b9e323 109
group-ESP8266 0:6946b0b9e323 110 const char *ESP8266Interface::get_mac_address()
group-ESP8266 0:6946b0b9e323 111 {
group-ESP8266 0:6946b0b9e323 112 return _esp.getMACAddress();
group-ESP8266 0:6946b0b9e323 113 }
group-ESP8266 0:6946b0b9e323 114
group-ESP8266 0:6946b0b9e323 115 const char *ESP8266Interface::get_gateway()
group-ESP8266 0:6946b0b9e323 116 {
group-ESP8266 0:6946b0b9e323 117 return _esp.getGateway();
group-ESP8266 0:6946b0b9e323 118 }
group-ESP8266 0:6946b0b9e323 119
group-ESP8266 0:6946b0b9e323 120 const char *ESP8266Interface::get_netmask()
group-ESP8266 0:6946b0b9e323 121 {
group-ESP8266 0:6946b0b9e323 122 return _esp.getNetmask();
group-ESP8266 0:6946b0b9e323 123 }
group-ESP8266 0:6946b0b9e323 124
group-ESP8266 0:6946b0b9e323 125 int8_t ESP8266Interface::get_rssi()
group-ESP8266 0:6946b0b9e323 126 {
group-ESP8266 0:6946b0b9e323 127 return _esp.getRSSI();
group-ESP8266 0:6946b0b9e323 128 }
group-ESP8266 0:6946b0b9e323 129
group-ESP8266 0:6946b0b9e323 130 int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
group-ESP8266 0:6946b0b9e323 131 {
group-ESP8266 0:6946b0b9e323 132 return _esp.scan(res, count);
group-ESP8266 0:6946b0b9e323 133 }
group-ESP8266 0:6946b0b9e323 134
group-ESP8266 0:6946b0b9e323 135 struct esp8266_socket {
group-ESP8266 0:6946b0b9e323 136 int id;
group-ESP8266 0:6946b0b9e323 137 nsapi_protocol_t proto;
group-ESP8266 0:6946b0b9e323 138 bool connected;
group-ESP8266 0:6946b0b9e323 139 SocketAddress addr;
group-ESP8266 0:6946b0b9e323 140 };
group-ESP8266 0:6946b0b9e323 141
group-ESP8266 0:6946b0b9e323 142 int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
group-ESP8266 0:6946b0b9e323 143 {
group-ESP8266 0:6946b0b9e323 144 // Look for an unused socket
group-ESP8266 0:6946b0b9e323 145 int id = -1;
group-ESP8266 0:6946b0b9e323 146
group-ESP8266 0:6946b0b9e323 147 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-ESP8266 0:6946b0b9e323 148 if (!_ids[i]) {
group-ESP8266 0:6946b0b9e323 149 id = i;
group-ESP8266 0:6946b0b9e323 150 _ids[i] = true;
group-ESP8266 0:6946b0b9e323 151 break;
group-ESP8266 0:6946b0b9e323 152 }
group-ESP8266 0:6946b0b9e323 153 }
group-ESP8266 0:6946b0b9e323 154
group-ESP8266 0:6946b0b9e323 155 if (id == -1) {
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 struct esp8266_socket *socket = new struct esp8266_socket;
group-ESP8266 0:6946b0b9e323 160 if (!socket) {
group-ESP8266 0:6946b0b9e323 161 return NSAPI_ERROR_NO_SOCKET;
group-ESP8266 0:6946b0b9e323 162 }
group-ESP8266 0:6946b0b9e323 163
group-ESP8266 0:6946b0b9e323 164 socket->id = id;
group-ESP8266 0:6946b0b9e323 165 socket->proto = proto;
group-ESP8266 0:6946b0b9e323 166 socket->connected = false;
group-ESP8266 0:6946b0b9e323 167 *handle = socket;
group-ESP8266 0:6946b0b9e323 168 return 0;
group-ESP8266 0:6946b0b9e323 169 }
group-ESP8266 0:6946b0b9e323 170
group-ESP8266 0:6946b0b9e323 171 int ESP8266Interface::socket_close(void *handle)
group-ESP8266 0:6946b0b9e323 172 {
group-ESP8266 0:6946b0b9e323 173 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 174 int err = 0;
group-ESP8266 0:6946b0b9e323 175 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 176
group-ESP8266 0:6946b0b9e323 177 if (!_esp.close(socket->id)) {
group-ESP8266 0:6946b0b9e323 178 err = NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 179 }
group-ESP8266 0:6946b0b9e323 180
group-ESP8266 0:6946b0b9e323 181 _ids[socket->id] = false;
group-ESP8266 0:6946b0b9e323 182 delete socket;
group-ESP8266 0:6946b0b9e323 183 return err;
group-ESP8266 0:6946b0b9e323 184 }
group-ESP8266 0:6946b0b9e323 185
group-ESP8266 0:6946b0b9e323 186 int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
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_listen(void *handle, int backlog)
group-ESP8266 0:6946b0b9e323 192 {
group-ESP8266 0:6946b0b9e323 193 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 194 }
group-ESP8266 0:6946b0b9e323 195
group-ESP8266 0:6946b0b9e323 196 int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
group-ESP8266 0:6946b0b9e323 197 {
group-ESP8266 0:6946b0b9e323 198 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 199 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 200
group-ESP8266 0:6946b0b9e323 201 const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP";
group-ESP8266 0:6946b0b9e323 202 if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
group-ESP8266 0:6946b0b9e323 203 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 204 }
group-ESP8266 0:6946b0b9e323 205
group-ESP8266 0:6946b0b9e323 206 socket->connected = true;
group-ESP8266 0:6946b0b9e323 207 return 0;
group-ESP8266 0:6946b0b9e323 208 }
group-ESP8266 0:6946b0b9e323 209
group-ESP8266 0:6946b0b9e323 210 int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
group-ESP8266 0:6946b0b9e323 211 {
group-ESP8266 0:6946b0b9e323 212 return NSAPI_ERROR_UNSUPPORTED;
group-ESP8266 0:6946b0b9e323 213 }
group-ESP8266 0:6946b0b9e323 214
group-ESP8266 0:6946b0b9e323 215 int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 216 {
group-ESP8266 0:6946b0b9e323 217 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 218 _esp.setTimeout(ESP8266_SEND_TIMEOUT);
group-ESP8266 0:6946b0b9e323 219
group-ESP8266 0:6946b0b9e323 220 if (!_esp.send(socket->id, data, size)) {
group-ESP8266 0:6946b0b9e323 221 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 222 }
group-ESP8266 0:6946b0b9e323 223
group-ESP8266 0:6946b0b9e323 224 return size;
group-ESP8266 0:6946b0b9e323 225 }
group-ESP8266 0:6946b0b9e323 226
group-ESP8266 0:6946b0b9e323 227 int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 228 {
group-ESP8266 0:6946b0b9e323 229 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 230 _esp.setTimeout(ESP8266_RECV_TIMEOUT);
group-ESP8266 0:6946b0b9e323 231
group-ESP8266 0:6946b0b9e323 232 int32_t recv = _esp.recv(socket->id, data, size);
group-ESP8266 0:6946b0b9e323 233 if (recv < 0) {
group-ESP8266 0:6946b0b9e323 234 return NSAPI_ERROR_WOULD_BLOCK;
group-ESP8266 0:6946b0b9e323 235 }
group-ESP8266 0:6946b0b9e323 236
group-ESP8266 0:6946b0b9e323 237 return recv;
group-ESP8266 0:6946b0b9e323 238 }
group-ESP8266 0:6946b0b9e323 239
group-ESP8266 0:6946b0b9e323 240 int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 241 {
group-ESP8266 0:6946b0b9e323 242 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 243
group-ESP8266 0:6946b0b9e323 244 if (socket->connected && socket->addr != addr) {
group-ESP8266 0:6946b0b9e323 245 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-ESP8266 0:6946b0b9e323 246 if (!_esp.close(socket->id)) {
group-ESP8266 0:6946b0b9e323 247 return NSAPI_ERROR_DEVICE_ERROR;
group-ESP8266 0:6946b0b9e323 248 }
group-ESP8266 0:6946b0b9e323 249 socket->connected = false;
group-ESP8266 0:6946b0b9e323 250 }
group-ESP8266 0:6946b0b9e323 251
group-ESP8266 0:6946b0b9e323 252 if (!socket->connected) {
group-ESP8266 0:6946b0b9e323 253 int err = socket_connect(socket, addr);
group-ESP8266 0:6946b0b9e323 254 if (err < 0) {
group-ESP8266 0:6946b0b9e323 255 return err;
group-ESP8266 0:6946b0b9e323 256 }
group-ESP8266 0:6946b0b9e323 257 socket->addr = addr;
group-ESP8266 0:6946b0b9e323 258 }
group-ESP8266 0:6946b0b9e323 259
group-ESP8266 0:6946b0b9e323 260 return socket_send(socket, data, size);
group-ESP8266 0:6946b0b9e323 261 }
group-ESP8266 0:6946b0b9e323 262
group-ESP8266 0:6946b0b9e323 263 int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
group-ESP8266 0:6946b0b9e323 264 {
group-ESP8266 0:6946b0b9e323 265 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 266 int ret = socket_recv(socket, data, size);
group-ESP8266 0:6946b0b9e323 267 if (ret >= 0 && addr) {
group-ESP8266 0:6946b0b9e323 268 *addr = socket->addr;
group-ESP8266 0:6946b0b9e323 269 }
group-ESP8266 0:6946b0b9e323 270
group-ESP8266 0:6946b0b9e323 271 return ret;
group-ESP8266 0:6946b0b9e323 272 }
group-ESP8266 0:6946b0b9e323 273
group-ESP8266 0:6946b0b9e323 274 void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
group-ESP8266 0:6946b0b9e323 275 {
group-ESP8266 0:6946b0b9e323 276 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-ESP8266 0:6946b0b9e323 277 _cbs[socket->id].callback = callback;
group-ESP8266 0:6946b0b9e323 278 _cbs[socket->id].data = data;
group-ESP8266 0:6946b0b9e323 279 }
group-ESP8266 0:6946b0b9e323 280
group-ESP8266 0:6946b0b9e323 281 void ESP8266Interface::event() {
group-ESP8266 0:6946b0b9e323 282 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-ESP8266 0:6946b0b9e323 283 if (_cbs[i].callback) {
group-ESP8266 0:6946b0b9e323 284 _cbs[i].callback(_cbs[i].data);
group-ESP8266 0:6946b0b9e323 285 }
group-ESP8266 0:6946b0b9e323 286 }
group-ESP8266 0:6946b0b9e323 287 }