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