Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266Interface by
ESP8266Interface.cpp@55:c0808849cb89, 2016-04-06 (annotated)
- Committer:
- geky
- Date:
- Wed Apr 06 13:49:41 2016 +0000
- Revision:
- 55:c0808849cb89
- Parent:
- 54:e78fad32cfff
Matched changes to NSAPI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geky | 49:750ed1b67483 | 1 | /* ESP8266 implementation of NetworkInterfaceAPI |
sam_grove | 11:288c15b80a26 | 2 | * Copyright (c) 2015 ARM Limited |
sam_grove | 11:288c15b80a26 | 3 | * |
sam_grove | 11:288c15b80a26 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 11:288c15b80a26 | 5 | * you may not use this file except in compliance with the License. |
sam_grove | 11:288c15b80a26 | 6 | * You may obtain a copy of the License at |
sam_grove | 11:288c15b80a26 | 7 | * |
sam_grove | 11:288c15b80a26 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 11:288c15b80a26 | 9 | * |
sam_grove | 11:288c15b80a26 | 10 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 11:288c15b80a26 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 11:288c15b80a26 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 11:288c15b80a26 | 13 | * See the License for the specific language governing permissions and |
sam_grove | 11:288c15b80a26 | 14 | * limitations under the License. |
sam_grove | 11:288c15b80a26 | 15 | */ |
geky | 55:c0808849cb89 | 16 | |
sam_grove | 24:37504440f296 | 17 | |
sarahmarshy | 18:9fc7976c7b27 | 18 | #include "ESP8266Interface.h" |
sam_grove | 11:288c15b80a26 | 19 | |
Christopher Haster |
45:538e5ce2f0d3 | 20 | // Various timeouts for different ESP8266 operations |
Christopher Haster |
45:538e5ce2f0d3 | 21 | #define ESP8266_CONNECT_TIMEOUT 15000 |
Christopher Haster |
45:538e5ce2f0d3 | 22 | #define ESP8266_SEND_TIMEOUT 500 |
Christopher Haster |
45:538e5ce2f0d3 | 23 | #define ESP8266_RECV_TIMEOUT 0 |
Christopher Haster |
47:60a4b49e8b83 | 24 | #define ESP8266_MISC_TIMEOUT 500 |
Christopher Haster |
45:538e5ce2f0d3 | 25 | |
Christopher Haster |
42:4bf09cadf328 | 26 | |
Christopher Haster |
40:83c6b4129468 | 27 | // ESP8266Interface implementation |
Christopher Haster |
34:9c26a3dcdc1f | 28 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
Christopher Haster |
34:9c26a3dcdc1f | 29 | : _esp(tx, rx, debug) |
sam_grove | 24:37504440f296 | 30 | { |
Christopher Haster |
34:9c26a3dcdc1f | 31 | memset(_ids, 0, sizeof(_ids)); |
sam_grove | 11:288c15b80a26 | 32 | } |
sam_grove | 11:288c15b80a26 | 33 | |
geky | 55:c0808849cb89 | 34 | int ESP8266Interface::connect( |
geky | 55:c0808849cb89 | 35 | const char *ssid, |
geky | 55:c0808849cb89 | 36 | const char *pass, |
geky | 55:c0808849cb89 | 37 | nsapi_security_t security) |
sam_grove | 11:288c15b80a26 | 38 | { |
Christopher Haster |
45:538e5ce2f0d3 | 39 | _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); |
Christopher Haster |
45:538e5ce2f0d3 | 40 | |
Christopher Haster |
41:3f4d5f4862d2 | 41 | if (!_esp.startup(3)) { |
geky | 55:c0808849cb89 | 42 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
41:3f4d5f4862d2 | 43 | } |
Christopher Haster |
41:3f4d5f4862d2 | 44 | |
Christopher Haster |
41:3f4d5f4862d2 | 45 | if (!_esp.dhcp(true, 1)) { |
geky | 55:c0808849cb89 | 46 | return NSAPI_ERROR_DHCP_FAILURE; |
Christopher Haster |
41:3f4d5f4862d2 | 47 | } |
Christopher Haster |
41:3f4d5f4862d2 | 48 | |
geky | 55:c0808849cb89 | 49 | if (!_esp.connect(ssid, pass)) { |
geky | 55:c0808849cb89 | 50 | return NSAPI_ERROR_NO_CONNECTION; |
Christopher Haster |
41:3f4d5f4862d2 | 51 | } |
Christopher Haster |
34:9c26a3dcdc1f | 52 | |
Christopher Haster |
46:6b1bd1268074 | 53 | if (!_esp.getIPAddress()) { |
geky | 55:c0808849cb89 | 54 | return NSAPI_ERROR_DHCP_FAILURE; |
Christopher Haster |
41:3f4d5f4862d2 | 55 | } |
Christopher Haster |
34:9c26a3dcdc1f | 56 | |
sam_grove | 24:37504440f296 | 57 | return 0; |
sam_grove | 11:288c15b80a26 | 58 | } |
sam_grove | 11:288c15b80a26 | 59 | |
geky | 55:c0808849cb89 | 60 | int ESP8266Interface::disconnect() |
sam_grove | 11:288c15b80a26 | 61 | { |
Christopher Haster |
45:538e5ce2f0d3 | 62 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
Christopher Haster |
45:538e5ce2f0d3 | 63 | |
Christopher Haster |
41:3f4d5f4862d2 | 64 | if (!_esp.disconnect()) { |
geky | 55:c0808849cb89 | 65 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
41:3f4d5f4862d2 | 66 | } |
Christopher Haster |
34:9c26a3dcdc1f | 67 | |
sarahmarshy | 22:312453862371 | 68 | return 0; |
sam_grove | 11:288c15b80a26 | 69 | } |
sam_grove | 11:288c15b80a26 | 70 | |
geky | 55:c0808849cb89 | 71 | const char *ESP8266Interface::get_ip_address() |
sarahmarshy | 18:9fc7976c7b27 | 72 | { |
Christopher Haster |
46:6b1bd1268074 | 73 | return _esp.getIPAddress(); |
sarahmarshy | 18:9fc7976c7b27 | 74 | } |
sarahmarshy | 18:9fc7976c7b27 | 75 | |
geky | 55:c0808849cb89 | 76 | const char *ESP8266Interface::get_mac_address() |
sam_grove | 11:288c15b80a26 | 77 | { |
Christopher Haster |
46:6b1bd1268074 | 78 | return _esp.getMACAddress(); |
sam_grove | 11:288c15b80a26 | 79 | } |
sam_grove | 11:288c15b80a26 | 80 | |
geky | 55:c0808849cb89 | 81 | struct esp8266_socket { |
geky | 55:c0808849cb89 | 82 | int id; |
geky | 55:c0808849cb89 | 83 | nsapi_protocol_t proto; |
geky | 55:c0808849cb89 | 84 | bool connected; |
geky | 55:c0808849cb89 | 85 | }; |
geky | 55:c0808849cb89 | 86 | |
geky | 55:c0808849cb89 | 87 | void *ESP8266Interface::socket_create(nsapi_protocol_t proto) |
sam_grove | 11:288c15b80a26 | 88 | { |
Christopher Haster |
34:9c26a3dcdc1f | 89 | // Look for an unused socket |
sarahmarshy | 18:9fc7976c7b27 | 90 | int id = -1; |
geky | 55:c0808849cb89 | 91 | |
Christopher Haster |
34:9c26a3dcdc1f | 92 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
Christopher Haster |
34:9c26a3dcdc1f | 93 | if (!_ids[i]) { |
sam_grove | 24:37504440f296 | 94 | id = i; |
Christopher Haster |
34:9c26a3dcdc1f | 95 | _ids[i] = true; |
sarahmarshy | 18:9fc7976c7b27 | 96 | break; |
sarahmarshy | 18:9fc7976c7b27 | 97 | } |
sarahmarshy | 18:9fc7976c7b27 | 98 | } |
geky | 55:c0808849cb89 | 99 | |
sam_grove | 24:37504440f296 | 100 | if (id == -1) { |
Christopher Haster |
34:9c26a3dcdc1f | 101 | return 0; |
sarahmarshy | 22:312453862371 | 102 | } |
geky | 55:c0808849cb89 | 103 | |
geky | 55:c0808849cb89 | 104 | struct esp8266_socket *socket = new struct esp8266_socket; |
geky | 55:c0808849cb89 | 105 | if (!socket) { |
geky | 55:c0808849cb89 | 106 | return 0; |
geky | 55:c0808849cb89 | 107 | } |
geky | 55:c0808849cb89 | 108 | |
geky | 55:c0808849cb89 | 109 | socket->id = id; |
geky | 55:c0808849cb89 | 110 | socket->proto = proto; |
geky | 55:c0808849cb89 | 111 | socket->connected = false; |
geky | 55:c0808849cb89 | 112 | return socket; |
bridadan | 16:b2f781416464 | 113 | } |
bridadan | 16:b2f781416464 | 114 | |
geky | 55:c0808849cb89 | 115 | void ESP8266Interface::socket_destroy(void *handle) |
sarahmarshy | 18:9fc7976c7b27 | 116 | { |
geky | 55:c0808849cb89 | 117 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 118 | _ids[socket->id] = false; |
Christopher Haster |
39:7e85bf8003fa | 119 | delete socket; |
sam_grove | 13:0186e9e35a24 | 120 | } |
sam_grove | 13:0186e9e35a24 | 121 | |
geky | 55:c0808849cb89 | 122 | int ESP8266Interface::socket_set_option(void *handle, int optname, const void *optval, unsigned optlen) |
geky | 55:c0808849cb89 | 123 | { |
geky | 55:c0808849cb89 | 124 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 55:c0808849cb89 | 125 | } |
Christopher Haster |
40:83c6b4129468 | 126 | |
geky | 55:c0808849cb89 | 127 | int ESP8266Interface::socket_get_option(void *handle, int optname, void *optval, unsigned *optlen) |
geky | 55:c0808849cb89 | 128 | { |
geky | 55:c0808849cb89 | 129 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 55:c0808849cb89 | 130 | } |
geky | 55:c0808849cb89 | 131 | |
geky | 55:c0808849cb89 | 132 | int ESP8266Interface::socket_bind(void *handle, int port) |
geky | 55:c0808849cb89 | 133 | { |
geky | 55:c0808849cb89 | 134 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 55:c0808849cb89 | 135 | } |
geky | 55:c0808849cb89 | 136 | |
geky | 55:c0808849cb89 | 137 | int ESP8266Interface::socket_listen(void *handle, int backlog) |
geky | 55:c0808849cb89 | 138 | { |
geky | 55:c0808849cb89 | 139 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 55:c0808849cb89 | 140 | } |
geky | 55:c0808849cb89 | 141 | |
geky | 55:c0808849cb89 | 142 | int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) |
Christopher Haster |
40:83c6b4129468 | 143 | { |
geky | 55:c0808849cb89 | 144 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 145 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
geky | 55:c0808849cb89 | 146 | |
geky | 55:c0808849cb89 | 147 | const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP"; |
geky | 55:c0808849cb89 | 148 | if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
geky | 55:c0808849cb89 | 149 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 55:c0808849cb89 | 150 | } |
geky | 55:c0808849cb89 | 151 | |
geky | 55:c0808849cb89 | 152 | socket->connected = true; |
geky | 55:c0808849cb89 | 153 | return 0; |
geky | 55:c0808849cb89 | 154 | } |
geky | 55:c0808849cb89 | 155 | |
geky | 55:c0808849cb89 | 156 | bool ESP8266Interface::socket_is_connected(void *handle) |
geky | 55:c0808849cb89 | 157 | { |
geky | 55:c0808849cb89 | 158 | return true; |
geky | 55:c0808849cb89 | 159 | } |
geky | 55:c0808849cb89 | 160 | |
geky | 55:c0808849cb89 | 161 | int ESP8266Interface::socket_accept(void *handle, void **connection) |
geky | 55:c0808849cb89 | 162 | { |
geky | 55:c0808849cb89 | 163 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 55:c0808849cb89 | 164 | } |
Christopher Haster |
45:538e5ce2f0d3 | 165 | |
geky | 55:c0808849cb89 | 166 | int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) |
geky | 55:c0808849cb89 | 167 | { |
geky | 55:c0808849cb89 | 168 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 169 | _esp.setTimeout(ESP8266_SEND_TIMEOUT); |
geky | 55:c0808849cb89 | 170 | |
geky | 55:c0808849cb89 | 171 | if (!_esp.send(socket->id, data, size)) { |
geky | 55:c0808849cb89 | 172 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 55:c0808849cb89 | 173 | } |
geky | 55:c0808849cb89 | 174 | |
geky | 55:c0808849cb89 | 175 | return size; |
geky | 55:c0808849cb89 | 176 | } |
geky | 55:c0808849cb89 | 177 | |
geky | 55:c0808849cb89 | 178 | int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) |
geky | 55:c0808849cb89 | 179 | { |
geky | 55:c0808849cb89 | 180 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 181 | _esp.setTimeout(ESP8266_RECV_TIMEOUT); |
geky | 55:c0808849cb89 | 182 | |
geky | 55:c0808849cb89 | 183 | int32_t recv = _esp.recv(socket->id, data, size); |
geky | 55:c0808849cb89 | 184 | if (recv < 0) { |
geky | 55:c0808849cb89 | 185 | return NSAPI_ERROR_WOULD_BLOCK; |
geky | 55:c0808849cb89 | 186 | } |
geky | 55:c0808849cb89 | 187 | |
geky | 55:c0808849cb89 | 188 | return recv; |
geky | 55:c0808849cb89 | 189 | } |
Christopher Haster |
40:83c6b4129468 | 190 | |
geky | 55:c0808849cb89 | 191 | int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
geky | 55:c0808849cb89 | 192 | { |
geky | 55:c0808849cb89 | 193 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 194 | if (!socket->connected) { |
geky | 55:c0808849cb89 | 195 | int err = socket_connect(socket, addr); |
geky | 55:c0808849cb89 | 196 | if (err < 0) { |
geky | 55:c0808849cb89 | 197 | return err; |
geky | 55:c0808849cb89 | 198 | } |
Christopher Haster |
40:83c6b4129468 | 199 | } |
geky | 55:c0808849cb89 | 200 | |
geky | 55:c0808849cb89 | 201 | return socket_send(socket, data, size); |
geky | 55:c0808849cb89 | 202 | } |
Christopher Haster |
40:83c6b4129468 | 203 | |
geky | 55:c0808849cb89 | 204 | int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
geky | 55:c0808849cb89 | 205 | { |
geky | 55:c0808849cb89 | 206 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 207 | return socket_recv(socket, data, size); |
geky | 55:c0808849cb89 | 208 | } |
geky | 55:c0808849cb89 | 209 | |
geky | 55:c0808849cb89 | 210 | int ESP8266Interface::socket_close(void *handle, bool shutdown) |
geky | 55:c0808849cb89 | 211 | { |
geky | 55:c0808849cb89 | 212 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
geky | 55:c0808849cb89 | 213 | _esp.setTimeout(ESP8266_MISC_TIMEOUT); |
geky | 55:c0808849cb89 | 214 | |
geky | 55:c0808849cb89 | 215 | if (!_esp.close(socket->id)) { |
geky | 55:c0808849cb89 | 216 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 55:c0808849cb89 | 217 | } |
geky | 55:c0808849cb89 | 218 | |
Christopher Haster |
40:83c6b4129468 | 219 | return 0; |
Christopher Haster |
40:83c6b4129468 | 220 | } |
Christopher Haster |
40:83c6b4129468 | 221 | |
geky | 55:c0808849cb89 | 222 | void ESP8266Interface::socket_attach_accept(void *handle, void (*callback)(void *), void *id) |
Christopher Haster |
40:83c6b4129468 | 223 | { |
Christopher Haster |
40:83c6b4129468 | 224 | } |
Christopher Haster |
40:83c6b4129468 | 225 | |
geky | 55:c0808849cb89 | 226 | void ESP8266Interface::socket_attach_send(void *handle, void (*callback)(void *), void *id) |
Christopher Haster |
40:83c6b4129468 | 227 | { |
Christopher Haster |
40:83c6b4129468 | 228 | } |
Christopher Haster |
40:83c6b4129468 | 229 | |
geky | 55:c0808849cb89 | 230 | void ESP8266Interface::socket_attach_recv(void *handle, void (*callback)(void *), void *id) |
Christopher Haster |
40:83c6b4129468 | 231 | { |
Christopher Haster |
40:83c6b4129468 | 232 | } |