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