wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/esp8266-driver/ESP8266Interface.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* ESP8266 implementation of NetworkInterfaceAPI |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2015 ARM Limited |
JMF | 0:24d3eb812fd4 | 3 | * |
JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:24d3eb812fd4 | 5 | * you may not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 7 | * |
JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JMF | 0:24d3eb812fd4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 15 | */ |
JMF | 0:24d3eb812fd4 | 16 | |
JMF | 0:24d3eb812fd4 | 17 | #include <cstring> |
JMF | 0:24d3eb812fd4 | 18 | #include "ESP8266.h" |
JMF | 0:24d3eb812fd4 | 19 | #include "ESP8266Interface.h" |
JMF | 0:24d3eb812fd4 | 20 | #include "mbed_debug.h" |
JMF | 0:24d3eb812fd4 | 21 | #include "nsapi_types.h" |
JMF | 0:24d3eb812fd4 | 22 | |
JMF | 0:24d3eb812fd4 | 23 | |
JMF | 0:24d3eb812fd4 | 24 | #ifndef MBED_CONF_ESP8266_TX |
JMF | 0:24d3eb812fd4 | 25 | #ifdef TARGET_FF_ARDUINO |
JMF | 0:24d3eb812fd4 | 26 | #define MBED_CONF_ESP8266_TX D1 |
JMF | 0:24d3eb812fd4 | 27 | #else |
JMF | 0:24d3eb812fd4 | 28 | #define MBED_CONF_ESP8266_TX NC |
JMF | 0:24d3eb812fd4 | 29 | #endif |
JMF | 0:24d3eb812fd4 | 30 | #endif |
JMF | 0:24d3eb812fd4 | 31 | |
JMF | 0:24d3eb812fd4 | 32 | #ifndef MBED_CONF_ESP8266_RX |
JMF | 0:24d3eb812fd4 | 33 | #ifdef TARGET_FF_ARDUINO |
JMF | 0:24d3eb812fd4 | 34 | #define MBED_CONF_ESP8266_RX D0 |
JMF | 0:24d3eb812fd4 | 35 | #else |
JMF | 0:24d3eb812fd4 | 36 | #define MBED_CONF_ESP8266_RX NC |
JMF | 0:24d3eb812fd4 | 37 | #endif |
JMF | 0:24d3eb812fd4 | 38 | #endif |
JMF | 0:24d3eb812fd4 | 39 | |
JMF | 0:24d3eb812fd4 | 40 | // Firmware version |
JMF | 0:24d3eb812fd4 | 41 | #define ESP8266_VERSION 2 |
JMF | 0:24d3eb812fd4 | 42 | |
JMF | 0:24d3eb812fd4 | 43 | ESP8266Interface::ESP8266Interface() |
JMF | 0:24d3eb812fd4 | 44 | : _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG), |
JMF | 0:24d3eb812fd4 | 45 | _initialized(false), |
JMF | 0:24d3eb812fd4 | 46 | _started(false) |
JMF | 0:24d3eb812fd4 | 47 | { |
JMF | 0:24d3eb812fd4 | 48 | memset(_ids, 0, sizeof(_ids)); |
JMF | 0:24d3eb812fd4 | 49 | memset(_cbs, 0, sizeof(_cbs)); |
JMF | 0:24d3eb812fd4 | 50 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
JMF | 0:24d3eb812fd4 | 51 | memset(ap_pass, 0, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 52 | memset(_local_ports, 0, sizeof(_local_ports)); |
JMF | 0:24d3eb812fd4 | 53 | ap_sec = NSAPI_SECURITY_UNKNOWN; |
JMF | 0:24d3eb812fd4 | 54 | |
JMF | 0:24d3eb812fd4 | 55 | _esp.sigio(this, &ESP8266Interface::event); |
JMF | 0:24d3eb812fd4 | 56 | _esp.setTimeout(); |
JMF | 0:24d3eb812fd4 | 57 | } |
JMF | 0:24d3eb812fd4 | 58 | |
JMF | 0:24d3eb812fd4 | 59 | // ESP8266Interface implementation |
JMF | 0:24d3eb812fd4 | 60 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
JMF | 0:24d3eb812fd4 | 61 | : _esp(tx, rx, debug), |
JMF | 0:24d3eb812fd4 | 62 | _initialized(false), |
JMF | 0:24d3eb812fd4 | 63 | _started(false) |
JMF | 0:24d3eb812fd4 | 64 | { |
JMF | 0:24d3eb812fd4 | 65 | memset(_ids, 0, sizeof(_ids)); |
JMF | 0:24d3eb812fd4 | 66 | memset(_cbs, 0, sizeof(_cbs)); |
JMF | 0:24d3eb812fd4 | 67 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
JMF | 0:24d3eb812fd4 | 68 | memset(ap_pass, 0, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 69 | memset(_local_ports, 0, sizeof(_local_ports)); |
JMF | 0:24d3eb812fd4 | 70 | ap_sec = NSAPI_SECURITY_UNKNOWN; |
JMF | 0:24d3eb812fd4 | 71 | |
JMF | 0:24d3eb812fd4 | 72 | _esp.sigio(this, &ESP8266Interface::event); |
JMF | 0:24d3eb812fd4 | 73 | _esp.setTimeout(); |
JMF | 0:24d3eb812fd4 | 74 | } |
JMF | 0:24d3eb812fd4 | 75 | |
JMF | 0:24d3eb812fd4 | 76 | int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
JMF | 0:24d3eb812fd4 | 77 | uint8_t channel) |
JMF | 0:24d3eb812fd4 | 78 | { |
JMF | 0:24d3eb812fd4 | 79 | if (channel != 0) { |
JMF | 0:24d3eb812fd4 | 80 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 81 | } |
JMF | 0:24d3eb812fd4 | 82 | |
JMF | 0:24d3eb812fd4 | 83 | int err = set_credentials(ssid, pass, security); |
JMF | 0:24d3eb812fd4 | 84 | if(err) { |
JMF | 0:24d3eb812fd4 | 85 | return err; |
JMF | 0:24d3eb812fd4 | 86 | } |
JMF | 0:24d3eb812fd4 | 87 | |
JMF | 0:24d3eb812fd4 | 88 | return connect(); |
JMF | 0:24d3eb812fd4 | 89 | } |
JMF | 0:24d3eb812fd4 | 90 | |
JMF | 0:24d3eb812fd4 | 91 | int ESP8266Interface::connect() |
JMF | 0:24d3eb812fd4 | 92 | { |
JMF | 0:24d3eb812fd4 | 93 | nsapi_error_t status; |
JMF | 0:24d3eb812fd4 | 94 | |
JMF | 0:24d3eb812fd4 | 95 | if (strlen(ap_ssid) == 0) { |
JMF | 0:24d3eb812fd4 | 96 | return NSAPI_ERROR_NO_SSID; |
JMF | 0:24d3eb812fd4 | 97 | } |
JMF | 0:24d3eb812fd4 | 98 | |
JMF | 0:24d3eb812fd4 | 99 | if (ap_sec != NSAPI_SECURITY_NONE) { |
JMF | 0:24d3eb812fd4 | 100 | if (strlen(ap_pass) < ESP8266_PASSPHRASE_MIN_LENGTH) { |
JMF | 0:24d3eb812fd4 | 101 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 102 | } |
JMF | 0:24d3eb812fd4 | 103 | } |
JMF | 0:24d3eb812fd4 | 104 | |
JMF | 0:24d3eb812fd4 | 105 | status = _init(); |
JMF | 0:24d3eb812fd4 | 106 | if(status != NSAPI_ERROR_OK) { |
JMF | 0:24d3eb812fd4 | 107 | return status; |
JMF | 0:24d3eb812fd4 | 108 | } |
JMF | 0:24d3eb812fd4 | 109 | |
JMF | 0:24d3eb812fd4 | 110 | if(get_ip_address()) { |
JMF | 0:24d3eb812fd4 | 111 | return NSAPI_ERROR_IS_CONNECTED; |
JMF | 0:24d3eb812fd4 | 112 | } |
JMF | 0:24d3eb812fd4 | 113 | |
JMF | 0:24d3eb812fd4 | 114 | status = _startup(ESP8266::WIFIMODE_STATION); |
JMF | 0:24d3eb812fd4 | 115 | if(status != NSAPI_ERROR_OK) { |
JMF | 0:24d3eb812fd4 | 116 | return status; |
JMF | 0:24d3eb812fd4 | 117 | } |
JMF | 0:24d3eb812fd4 | 118 | _started = true; |
JMF | 0:24d3eb812fd4 | 119 | |
JMF | 0:24d3eb812fd4 | 120 | if (!_esp.dhcp(true, 1)) { |
JMF | 0:24d3eb812fd4 | 121 | return NSAPI_ERROR_DHCP_FAILURE; |
JMF | 0:24d3eb812fd4 | 122 | } |
JMF | 0:24d3eb812fd4 | 123 | |
JMF | 0:24d3eb812fd4 | 124 | int connect_error = _esp.connect(ap_ssid, ap_pass); |
JMF | 0:24d3eb812fd4 | 125 | if (connect_error) { |
JMF | 0:24d3eb812fd4 | 126 | return connect_error; |
JMF | 0:24d3eb812fd4 | 127 | } |
JMF | 0:24d3eb812fd4 | 128 | |
JMF | 0:24d3eb812fd4 | 129 | if (!get_ip_address()) { |
JMF | 0:24d3eb812fd4 | 130 | return NSAPI_ERROR_DHCP_FAILURE; |
JMF | 0:24d3eb812fd4 | 131 | } |
JMF | 0:24d3eb812fd4 | 132 | |
JMF | 0:24d3eb812fd4 | 133 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 134 | } |
JMF | 0:24d3eb812fd4 | 135 | |
JMF | 0:24d3eb812fd4 | 136 | int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
JMF | 0:24d3eb812fd4 | 137 | { |
JMF | 0:24d3eb812fd4 | 138 | ap_sec = security; |
JMF | 0:24d3eb812fd4 | 139 | |
JMF | 0:24d3eb812fd4 | 140 | if (!ssid) { |
JMF | 0:24d3eb812fd4 | 141 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 142 | } |
JMF | 0:24d3eb812fd4 | 143 | |
JMF | 0:24d3eb812fd4 | 144 | int ssid_length = strlen(ssid); |
JMF | 0:24d3eb812fd4 | 145 | |
JMF | 0:24d3eb812fd4 | 146 | if (ssid_length > 0 |
JMF | 0:24d3eb812fd4 | 147 | && ssid_length <= ESP8266_SSID_MAX_LENGTH) { |
JMF | 0:24d3eb812fd4 | 148 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
JMF | 0:24d3eb812fd4 | 149 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
JMF | 0:24d3eb812fd4 | 150 | } else { |
JMF | 0:24d3eb812fd4 | 151 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 152 | } |
JMF | 0:24d3eb812fd4 | 153 | |
JMF | 0:24d3eb812fd4 | 154 | if (ap_sec != NSAPI_SECURITY_NONE) { |
JMF | 0:24d3eb812fd4 | 155 | |
JMF | 0:24d3eb812fd4 | 156 | if (!pass) { |
JMF | 0:24d3eb812fd4 | 157 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 158 | } |
JMF | 0:24d3eb812fd4 | 159 | |
JMF | 0:24d3eb812fd4 | 160 | int pass_length = strlen(pass); |
JMF | 0:24d3eb812fd4 | 161 | if (pass_length >= ESP8266_PASSPHRASE_MIN_LENGTH |
JMF | 0:24d3eb812fd4 | 162 | && pass_length <= ESP8266_PASSPHRASE_MAX_LENGTH ) { |
JMF | 0:24d3eb812fd4 | 163 | memset(ap_pass, 0, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 164 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 165 | } else { |
JMF | 0:24d3eb812fd4 | 166 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 167 | } |
JMF | 0:24d3eb812fd4 | 168 | } else { |
JMF | 0:24d3eb812fd4 | 169 | memset(ap_pass, 0, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 170 | } |
JMF | 0:24d3eb812fd4 | 171 | |
JMF | 0:24d3eb812fd4 | 172 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 173 | } |
JMF | 0:24d3eb812fd4 | 174 | |
JMF | 0:24d3eb812fd4 | 175 | int ESP8266Interface::set_channel(uint8_t channel) |
JMF | 0:24d3eb812fd4 | 176 | { |
JMF | 0:24d3eb812fd4 | 177 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 178 | } |
JMF | 0:24d3eb812fd4 | 179 | |
JMF | 0:24d3eb812fd4 | 180 | |
JMF | 0:24d3eb812fd4 | 181 | int ESP8266Interface::disconnect() |
JMF | 0:24d3eb812fd4 | 182 | { |
JMF | 0:24d3eb812fd4 | 183 | _started = false; |
JMF | 0:24d3eb812fd4 | 184 | _initialized = false; |
JMF | 0:24d3eb812fd4 | 185 | |
JMF | 0:24d3eb812fd4 | 186 | return _esp.disconnect() ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 187 | } |
JMF | 0:24d3eb812fd4 | 188 | |
JMF | 0:24d3eb812fd4 | 189 | const char *ESP8266Interface::get_ip_address() |
JMF | 0:24d3eb812fd4 | 190 | { |
JMF | 0:24d3eb812fd4 | 191 | if(!_started) { |
JMF | 0:24d3eb812fd4 | 192 | return NULL; |
JMF | 0:24d3eb812fd4 | 193 | } |
JMF | 0:24d3eb812fd4 | 194 | |
JMF | 0:24d3eb812fd4 | 195 | const char *ip_buff = _esp.getIPAddress(); |
JMF | 0:24d3eb812fd4 | 196 | if(!ip_buff || std::strcmp(ip_buff, "0.0.0.0") == 0) { |
JMF | 0:24d3eb812fd4 | 197 | return NULL; |
JMF | 0:24d3eb812fd4 | 198 | } |
JMF | 0:24d3eb812fd4 | 199 | |
JMF | 0:24d3eb812fd4 | 200 | return ip_buff; |
JMF | 0:24d3eb812fd4 | 201 | } |
JMF | 0:24d3eb812fd4 | 202 | |
JMF | 0:24d3eb812fd4 | 203 | const char *ESP8266Interface::get_mac_address() |
JMF | 0:24d3eb812fd4 | 204 | { |
JMF | 0:24d3eb812fd4 | 205 | return _esp.getMACAddress(); |
JMF | 0:24d3eb812fd4 | 206 | } |
JMF | 0:24d3eb812fd4 | 207 | |
JMF | 0:24d3eb812fd4 | 208 | const char *ESP8266Interface::get_gateway() |
JMF | 0:24d3eb812fd4 | 209 | { |
JMF | 0:24d3eb812fd4 | 210 | return _started ? _esp.getGateway() : NULL; |
JMF | 0:24d3eb812fd4 | 211 | } |
JMF | 0:24d3eb812fd4 | 212 | |
JMF | 0:24d3eb812fd4 | 213 | const char *ESP8266Interface::get_netmask() |
JMF | 0:24d3eb812fd4 | 214 | { |
JMF | 0:24d3eb812fd4 | 215 | return _started ? _esp.getNetmask() : NULL; |
JMF | 0:24d3eb812fd4 | 216 | } |
JMF | 0:24d3eb812fd4 | 217 | |
JMF | 0:24d3eb812fd4 | 218 | int8_t ESP8266Interface::get_rssi() |
JMF | 0:24d3eb812fd4 | 219 | { |
JMF | 0:24d3eb812fd4 | 220 | return _started ? _esp.getRSSI() : 0; |
JMF | 0:24d3eb812fd4 | 221 | } |
JMF | 0:24d3eb812fd4 | 222 | |
JMF | 0:24d3eb812fd4 | 223 | int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count) |
JMF | 0:24d3eb812fd4 | 224 | { |
JMF | 0:24d3eb812fd4 | 225 | nsapi_error_t status; |
JMF | 0:24d3eb812fd4 | 226 | |
JMF | 0:24d3eb812fd4 | 227 | status = _init(); |
JMF | 0:24d3eb812fd4 | 228 | if(status != NSAPI_ERROR_OK) { |
JMF | 0:24d3eb812fd4 | 229 | return status; |
JMF | 0:24d3eb812fd4 | 230 | } |
JMF | 0:24d3eb812fd4 | 231 | |
JMF | 0:24d3eb812fd4 | 232 | status = _startup(ESP8266::WIFIMODE_STATION); |
JMF | 0:24d3eb812fd4 | 233 | if(status != NSAPI_ERROR_OK) { |
JMF | 0:24d3eb812fd4 | 234 | return status; |
JMF | 0:24d3eb812fd4 | 235 | } |
JMF | 0:24d3eb812fd4 | 236 | |
JMF | 0:24d3eb812fd4 | 237 | return _esp.scan(res, count); |
JMF | 0:24d3eb812fd4 | 238 | } |
JMF | 0:24d3eb812fd4 | 239 | |
JMF | 0:24d3eb812fd4 | 240 | bool ESP8266Interface::_get_firmware_ok() |
JMF | 0:24d3eb812fd4 | 241 | { |
JMF | 0:24d3eb812fd4 | 242 | if (_esp.get_firmware_version() != ESP8266_VERSION) { |
JMF | 0:24d3eb812fd4 | 243 | debug("ESP8266: ERROR: Firmware incompatible with this driver.\ |
JMF | 0:24d3eb812fd4 | 244 | \r\nUpdate to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n",ESP8266_VERSION); |
JMF | 0:24d3eb812fd4 | 245 | return false; |
JMF | 0:24d3eb812fd4 | 246 | } |
JMF | 0:24d3eb812fd4 | 247 | |
JMF | 0:24d3eb812fd4 | 248 | return true; |
JMF | 0:24d3eb812fd4 | 249 | } |
JMF | 0:24d3eb812fd4 | 250 | |
JMF | 0:24d3eb812fd4 | 251 | bool ESP8266Interface::_disable_default_softap() |
JMF | 0:24d3eb812fd4 | 252 | { |
JMF | 0:24d3eb812fd4 | 253 | static int disabled = false; |
JMF | 0:24d3eb812fd4 | 254 | |
JMF | 0:24d3eb812fd4 | 255 | if (disabled || _esp.get_default_wifi_mode() == ESP8266::WIFIMODE_STATION) { |
JMF | 0:24d3eb812fd4 | 256 | disabled = true; |
JMF | 0:24d3eb812fd4 | 257 | return true; |
JMF | 0:24d3eb812fd4 | 258 | } |
JMF | 0:24d3eb812fd4 | 259 | if (_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) { |
JMF | 0:24d3eb812fd4 | 260 | disabled = true; |
JMF | 0:24d3eb812fd4 | 261 | return true; |
JMF | 0:24d3eb812fd4 | 262 | } |
JMF | 0:24d3eb812fd4 | 263 | |
JMF | 0:24d3eb812fd4 | 264 | return false; |
JMF | 0:24d3eb812fd4 | 265 | } |
JMF | 0:24d3eb812fd4 | 266 | |
JMF | 0:24d3eb812fd4 | 267 | nsapi_error_t ESP8266Interface::_init(void) |
JMF | 0:24d3eb812fd4 | 268 | { |
JMF | 0:24d3eb812fd4 | 269 | if (!_initialized) { |
JMF | 0:24d3eb812fd4 | 270 | if (!_esp.reset()) { |
JMF | 0:24d3eb812fd4 | 271 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 272 | } |
JMF | 0:24d3eb812fd4 | 273 | if (!_get_firmware_ok()) { |
JMF | 0:24d3eb812fd4 | 274 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 275 | } |
JMF | 0:24d3eb812fd4 | 276 | if (_disable_default_softap() == false) { |
JMF | 0:24d3eb812fd4 | 277 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 278 | } |
JMF | 0:24d3eb812fd4 | 279 | _initialized = true; |
JMF | 0:24d3eb812fd4 | 280 | } |
JMF | 0:24d3eb812fd4 | 281 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 282 | } |
JMF | 0:24d3eb812fd4 | 283 | |
JMF | 0:24d3eb812fd4 | 284 | nsapi_error_t ESP8266Interface::_startup(const int8_t wifi_mode) |
JMF | 0:24d3eb812fd4 | 285 | { |
JMF | 0:24d3eb812fd4 | 286 | if (!_started) { |
JMF | 0:24d3eb812fd4 | 287 | if (!_esp.startup(wifi_mode)) { |
JMF | 0:24d3eb812fd4 | 288 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 289 | } |
JMF | 0:24d3eb812fd4 | 290 | } |
JMF | 0:24d3eb812fd4 | 291 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 292 | } |
JMF | 0:24d3eb812fd4 | 293 | |
JMF | 0:24d3eb812fd4 | 294 | struct esp8266_socket { |
JMF | 0:24d3eb812fd4 | 295 | int id; |
JMF | 0:24d3eb812fd4 | 296 | nsapi_protocol_t proto; |
JMF | 0:24d3eb812fd4 | 297 | bool connected; |
JMF | 0:24d3eb812fd4 | 298 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 299 | int keepalive; // TCP |
JMF | 0:24d3eb812fd4 | 300 | }; |
JMF | 0:24d3eb812fd4 | 301 | |
JMF | 0:24d3eb812fd4 | 302 | int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) |
JMF | 0:24d3eb812fd4 | 303 | { |
JMF | 0:24d3eb812fd4 | 304 | // Look for an unused socket |
JMF | 0:24d3eb812fd4 | 305 | int id = -1; |
JMF | 0:24d3eb812fd4 | 306 | |
JMF | 0:24d3eb812fd4 | 307 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
JMF | 0:24d3eb812fd4 | 308 | if (!_ids[i]) { |
JMF | 0:24d3eb812fd4 | 309 | id = i; |
JMF | 0:24d3eb812fd4 | 310 | _ids[i] = true; |
JMF | 0:24d3eb812fd4 | 311 | break; |
JMF | 0:24d3eb812fd4 | 312 | } |
JMF | 0:24d3eb812fd4 | 313 | } |
JMF | 0:24d3eb812fd4 | 314 | |
JMF | 0:24d3eb812fd4 | 315 | if (id == -1) { |
JMF | 0:24d3eb812fd4 | 316 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 317 | } |
JMF | 0:24d3eb812fd4 | 318 | |
JMF | 0:24d3eb812fd4 | 319 | struct esp8266_socket *socket = new struct esp8266_socket; |
JMF | 0:24d3eb812fd4 | 320 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 321 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 322 | } |
JMF | 0:24d3eb812fd4 | 323 | |
JMF | 0:24d3eb812fd4 | 324 | socket->id = id; |
JMF | 0:24d3eb812fd4 | 325 | socket->proto = proto; |
JMF | 0:24d3eb812fd4 | 326 | socket->connected = false; |
JMF | 0:24d3eb812fd4 | 327 | socket->keepalive = 0; |
JMF | 0:24d3eb812fd4 | 328 | *handle = socket; |
JMF | 0:24d3eb812fd4 | 329 | return 0; |
JMF | 0:24d3eb812fd4 | 330 | } |
JMF | 0:24d3eb812fd4 | 331 | |
JMF | 0:24d3eb812fd4 | 332 | int ESP8266Interface::socket_close(void *handle) |
JMF | 0:24d3eb812fd4 | 333 | { |
JMF | 0:24d3eb812fd4 | 334 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 335 | int err = 0; |
JMF | 0:24d3eb812fd4 | 336 | |
JMF | 0:24d3eb812fd4 | 337 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 338 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 339 | } |
JMF | 0:24d3eb812fd4 | 340 | |
JMF | 0:24d3eb812fd4 | 341 | if (socket->connected && !_esp.close(socket->id)) { |
JMF | 0:24d3eb812fd4 | 342 | err = NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 343 | } |
JMF | 0:24d3eb812fd4 | 344 | |
JMF | 0:24d3eb812fd4 | 345 | socket->connected = false; |
JMF | 0:24d3eb812fd4 | 346 | _ids[socket->id] = false; |
JMF | 0:24d3eb812fd4 | 347 | _local_ports[socket->id] = 0; |
JMF | 0:24d3eb812fd4 | 348 | delete socket; |
JMF | 0:24d3eb812fd4 | 349 | return err; |
JMF | 0:24d3eb812fd4 | 350 | } |
JMF | 0:24d3eb812fd4 | 351 | |
JMF | 0:24d3eb812fd4 | 352 | int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) |
JMF | 0:24d3eb812fd4 | 353 | { |
JMF | 0:24d3eb812fd4 | 354 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 355 | |
JMF | 0:24d3eb812fd4 | 356 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 357 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 358 | } |
JMF | 0:24d3eb812fd4 | 359 | |
JMF | 0:24d3eb812fd4 | 360 | if (socket->proto == NSAPI_UDP) { |
JMF | 0:24d3eb812fd4 | 361 | if(address.get_addr().version != NSAPI_UNSPEC) { |
JMF | 0:24d3eb812fd4 | 362 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 363 | } |
JMF | 0:24d3eb812fd4 | 364 | |
JMF | 0:24d3eb812fd4 | 365 | for(int id = 0; id < ESP8266_SOCKET_COUNT; id++) { |
JMF | 0:24d3eb812fd4 | 366 | if(_local_ports[id] == address.get_port() && id != socket->id) { // Port already reserved by another socket |
JMF | 0:24d3eb812fd4 | 367 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 368 | } else if (id == socket->id && socket->connected) { |
JMF | 0:24d3eb812fd4 | 369 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 370 | } |
JMF | 0:24d3eb812fd4 | 371 | } |
JMF | 0:24d3eb812fd4 | 372 | _local_ports[socket->id] = address.get_port(); |
JMF | 0:24d3eb812fd4 | 373 | return 0; |
JMF | 0:24d3eb812fd4 | 374 | } |
JMF | 0:24d3eb812fd4 | 375 | |
JMF | 0:24d3eb812fd4 | 376 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 377 | } |
JMF | 0:24d3eb812fd4 | 378 | |
JMF | 0:24d3eb812fd4 | 379 | int ESP8266Interface::socket_listen(void *handle, int backlog) |
JMF | 0:24d3eb812fd4 | 380 | { |
JMF | 0:24d3eb812fd4 | 381 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 382 | } |
JMF | 0:24d3eb812fd4 | 383 | |
JMF | 0:24d3eb812fd4 | 384 | int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) |
JMF | 0:24d3eb812fd4 | 385 | { |
JMF | 0:24d3eb812fd4 | 386 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 387 | nsapi_error_t ret; |
JMF | 0:24d3eb812fd4 | 388 | |
JMF | 0:24d3eb812fd4 | 389 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 390 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 391 | } |
JMF | 0:24d3eb812fd4 | 392 | |
JMF | 0:24d3eb812fd4 | 393 | if (socket->proto == NSAPI_UDP) { |
JMF | 0:24d3eb812fd4 | 394 | ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _local_ports[socket->id]); |
JMF | 0:24d3eb812fd4 | 395 | if (ret != NSAPI_ERROR_OK) { |
JMF | 0:24d3eb812fd4 | 396 | return ret; |
JMF | 0:24d3eb812fd4 | 397 | } |
JMF | 0:24d3eb812fd4 | 398 | } else { |
JMF | 0:24d3eb812fd4 | 399 | if (!_esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive)) { |
JMF | 0:24d3eb812fd4 | 400 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 401 | } |
JMF | 0:24d3eb812fd4 | 402 | } |
JMF | 0:24d3eb812fd4 | 403 | |
JMF | 0:24d3eb812fd4 | 404 | socket->connected = true; |
JMF | 0:24d3eb812fd4 | 405 | return 0; |
JMF | 0:24d3eb812fd4 | 406 | } |
JMF | 0:24d3eb812fd4 | 407 | |
JMF | 0:24d3eb812fd4 | 408 | int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
JMF | 0:24d3eb812fd4 | 409 | { |
JMF | 0:24d3eb812fd4 | 410 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 411 | } |
JMF | 0:24d3eb812fd4 | 412 | |
JMF | 0:24d3eb812fd4 | 413 | int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 414 | { |
JMF | 0:24d3eb812fd4 | 415 | nsapi_error_t status; |
JMF | 0:24d3eb812fd4 | 416 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 417 | |
JMF | 0:24d3eb812fd4 | 418 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 419 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 420 | } |
JMF | 0:24d3eb812fd4 | 421 | |
JMF | 0:24d3eb812fd4 | 422 | status = _esp.send(socket->id, data, size); |
JMF | 0:24d3eb812fd4 | 423 | |
JMF | 0:24d3eb812fd4 | 424 | return status != NSAPI_ERROR_OK ? status : size; |
JMF | 0:24d3eb812fd4 | 425 | } |
JMF | 0:24d3eb812fd4 | 426 | |
JMF | 0:24d3eb812fd4 | 427 | int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 428 | { |
JMF | 0:24d3eb812fd4 | 429 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 430 | |
JMF | 0:24d3eb812fd4 | 431 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 432 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 433 | } |
JMF | 0:24d3eb812fd4 | 434 | |
JMF | 0:24d3eb812fd4 | 435 | int32_t recv; |
JMF | 0:24d3eb812fd4 | 436 | if (socket->proto == NSAPI_TCP) { |
JMF | 0:24d3eb812fd4 | 437 | recv = _esp.recv_tcp(socket->id, data, size); |
JMF | 0:24d3eb812fd4 | 438 | if (recv <= 0 && recv != NSAPI_ERROR_WOULD_BLOCK) { |
JMF | 0:24d3eb812fd4 | 439 | socket->connected = false; |
JMF | 0:24d3eb812fd4 | 440 | } |
JMF | 0:24d3eb812fd4 | 441 | } else { |
JMF | 0:24d3eb812fd4 | 442 | recv = _esp.recv_udp(socket->id, data, size); |
JMF | 0:24d3eb812fd4 | 443 | } |
JMF | 0:24d3eb812fd4 | 444 | |
JMF | 0:24d3eb812fd4 | 445 | return recv; |
JMF | 0:24d3eb812fd4 | 446 | } |
JMF | 0:24d3eb812fd4 | 447 | |
JMF | 0:24d3eb812fd4 | 448 | int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 449 | { |
JMF | 0:24d3eb812fd4 | 450 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 451 | |
JMF | 0:24d3eb812fd4 | 452 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 453 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 454 | } |
JMF | 0:24d3eb812fd4 | 455 | |
JMF | 0:24d3eb812fd4 | 456 | if((strcmp(addr.get_ip_address(), "0.0.0.0") == 0) || !addr.get_port()) { |
JMF | 0:24d3eb812fd4 | 457 | return NSAPI_ERROR_DNS_FAILURE; |
JMF | 0:24d3eb812fd4 | 458 | } |
JMF | 0:24d3eb812fd4 | 459 | |
JMF | 0:24d3eb812fd4 | 460 | if (socket->connected && socket->addr != addr) { |
JMF | 0:24d3eb812fd4 | 461 | if (!_esp.close(socket->id)) { |
JMF | 0:24d3eb812fd4 | 462 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 463 | } |
JMF | 0:24d3eb812fd4 | 464 | socket->connected = false; |
JMF | 0:24d3eb812fd4 | 465 | } |
JMF | 0:24d3eb812fd4 | 466 | |
JMF | 0:24d3eb812fd4 | 467 | if (!socket->connected) { |
JMF | 0:24d3eb812fd4 | 468 | int err = socket_connect(socket, addr); |
JMF | 0:24d3eb812fd4 | 469 | if (err < 0) { |
JMF | 0:24d3eb812fd4 | 470 | return err; |
JMF | 0:24d3eb812fd4 | 471 | } |
JMF | 0:24d3eb812fd4 | 472 | socket->addr = addr; |
JMF | 0:24d3eb812fd4 | 473 | } |
JMF | 0:24d3eb812fd4 | 474 | |
JMF | 0:24d3eb812fd4 | 475 | return socket_send(socket, data, size); |
JMF | 0:24d3eb812fd4 | 476 | } |
JMF | 0:24d3eb812fd4 | 477 | |
JMF | 0:24d3eb812fd4 | 478 | int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 479 | { |
JMF | 0:24d3eb812fd4 | 480 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 481 | |
JMF | 0:24d3eb812fd4 | 482 | if (!socket) { |
JMF | 0:24d3eb812fd4 | 483 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 484 | } |
JMF | 0:24d3eb812fd4 | 485 | |
JMF | 0:24d3eb812fd4 | 486 | int ret = socket_recv(socket, data, size); |
JMF | 0:24d3eb812fd4 | 487 | if (ret >= 0 && addr) { |
JMF | 0:24d3eb812fd4 | 488 | *addr = socket->addr; |
JMF | 0:24d3eb812fd4 | 489 | } |
JMF | 0:24d3eb812fd4 | 490 | |
JMF | 0:24d3eb812fd4 | 491 | return ret; |
JMF | 0:24d3eb812fd4 | 492 | } |
JMF | 0:24d3eb812fd4 | 493 | |
JMF | 0:24d3eb812fd4 | 494 | void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
JMF | 0:24d3eb812fd4 | 495 | { |
JMF | 0:24d3eb812fd4 | 496 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 497 | _cbs[socket->id].callback = callback; |
JMF | 0:24d3eb812fd4 | 498 | _cbs[socket->id].data = data; |
JMF | 0:24d3eb812fd4 | 499 | } |
JMF | 0:24d3eb812fd4 | 500 | |
JMF | 0:24d3eb812fd4 | 501 | nsapi_error_t ESP8266Interface::setsockopt(nsapi_socket_t handle, int level, |
JMF | 0:24d3eb812fd4 | 502 | int optname, const void *optval, unsigned optlen) |
JMF | 0:24d3eb812fd4 | 503 | { |
JMF | 0:24d3eb812fd4 | 504 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 505 | |
JMF | 0:24d3eb812fd4 | 506 | if (!optlen) { |
JMF | 0:24d3eb812fd4 | 507 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 508 | } else if (!socket) { |
JMF | 0:24d3eb812fd4 | 509 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 510 | } |
JMF | 0:24d3eb812fd4 | 511 | |
JMF | 0:24d3eb812fd4 | 512 | if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) { |
JMF | 0:24d3eb812fd4 | 513 | switch (optname) { |
JMF | 0:24d3eb812fd4 | 514 | case NSAPI_KEEPALIVE: { |
JMF | 0:24d3eb812fd4 | 515 | if(socket->connected) {// ESP8266 limitation, keepalive needs to be given before connecting |
JMF | 0:24d3eb812fd4 | 516 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 517 | } |
JMF | 0:24d3eb812fd4 | 518 | |
JMF | 0:24d3eb812fd4 | 519 | if (optlen == sizeof(int)) { |
JMF | 0:24d3eb812fd4 | 520 | int secs = *(int *)optval; |
JMF | 0:24d3eb812fd4 | 521 | if (secs >= 0 && secs <= 7200) { |
JMF | 0:24d3eb812fd4 | 522 | socket->keepalive = secs; |
JMF | 0:24d3eb812fd4 | 523 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 524 | } |
JMF | 0:24d3eb812fd4 | 525 | } |
JMF | 0:24d3eb812fd4 | 526 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 527 | } |
JMF | 0:24d3eb812fd4 | 528 | } |
JMF | 0:24d3eb812fd4 | 529 | } |
JMF | 0:24d3eb812fd4 | 530 | |
JMF | 0:24d3eb812fd4 | 531 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 532 | } |
JMF | 0:24d3eb812fd4 | 533 | |
JMF | 0:24d3eb812fd4 | 534 | nsapi_error_t ESP8266Interface::getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen) |
JMF | 0:24d3eb812fd4 | 535 | { |
JMF | 0:24d3eb812fd4 | 536 | struct esp8266_socket *socket = (struct esp8266_socket *)handle; |
JMF | 0:24d3eb812fd4 | 537 | |
JMF | 0:24d3eb812fd4 | 538 | if (!optval || !optlen) { |
JMF | 0:24d3eb812fd4 | 539 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 540 | } else if (!socket) { |
JMF | 0:24d3eb812fd4 | 541 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 542 | } |
JMF | 0:24d3eb812fd4 | 543 | |
JMF | 0:24d3eb812fd4 | 544 | if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) { |
JMF | 0:24d3eb812fd4 | 545 | switch (optname) { |
JMF | 0:24d3eb812fd4 | 546 | case NSAPI_KEEPALIVE: { |
JMF | 0:24d3eb812fd4 | 547 | if(*optlen > sizeof(int)) { |
JMF | 0:24d3eb812fd4 | 548 | *optlen = sizeof(int); |
JMF | 0:24d3eb812fd4 | 549 | } |
JMF | 0:24d3eb812fd4 | 550 | memcpy(optval, &(socket->keepalive), *optlen); |
JMF | 0:24d3eb812fd4 | 551 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 552 | } |
JMF | 0:24d3eb812fd4 | 553 | } |
JMF | 0:24d3eb812fd4 | 554 | } |
JMF | 0:24d3eb812fd4 | 555 | |
JMF | 0:24d3eb812fd4 | 556 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 557 | } |
JMF | 0:24d3eb812fd4 | 558 | |
JMF | 0:24d3eb812fd4 | 559 | |
JMF | 0:24d3eb812fd4 | 560 | void ESP8266Interface::event() |
JMF | 0:24d3eb812fd4 | 561 | { |
JMF | 0:24d3eb812fd4 | 562 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
JMF | 0:24d3eb812fd4 | 563 | if (_cbs[i].callback) { |
JMF | 0:24d3eb812fd4 | 564 | _cbs[i].callback(_cbs[i].data); |
JMF | 0:24d3eb812fd4 | 565 | } |
JMF | 0:24d3eb812fd4 | 566 | } |
JMF | 0:24d3eb812fd4 | 567 | } |
JMF | 0:24d3eb812fd4 | 568 | |
JMF | 0:24d3eb812fd4 | 569 | void ESP8266Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) |
JMF | 0:24d3eb812fd4 | 570 | { |
JMF | 0:24d3eb812fd4 | 571 | _esp.attach(status_cb); |
JMF | 0:24d3eb812fd4 | 572 | } |
JMF | 0:24d3eb812fd4 | 573 | |
JMF | 0:24d3eb812fd4 | 574 | nsapi_connection_status_t ESP8266Interface::get_connection_status() const |
JMF | 0:24d3eb812fd4 | 575 | { |
JMF | 0:24d3eb812fd4 | 576 | return _esp.get_connection_status(); |
JMF | 0:24d3eb812fd4 | 577 | } |