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