Toyomasa Watarai
/
Mbed-example-WS-W27
Mbed Cloud example program for workshop in W27 2018.
Embed:
(wiki syntax)
Show/hide line numbers
ESP8266Interface.cpp
00001 /* ESP8266 implementation of NetworkInterfaceAPI 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <cstring> 00018 #include "ESP8266.h" 00019 #include "ESP8266Interface.h" 00020 #include "mbed_debug.h" 00021 #include "nsapi_types.h" 00022 00023 00024 // Firmware version 00025 #define ESP8266_VERSION 2 00026 00027 // ESP8266Interface implementation 00028 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) 00029 : _esp(tx, rx, debug), 00030 _initialized(false), 00031 _started(false) 00032 { 00033 memset(_ids, 0, sizeof(_ids)); 00034 memset(_cbs, 0, sizeof(_cbs)); 00035 memset(ap_ssid, 0, sizeof(ap_ssid)); 00036 memset(ap_pass, 0, sizeof(ap_pass)); 00037 memset(_local_ports, 0, sizeof(_local_ports)); 00038 ap_sec = NSAPI_SECURITY_UNKNOWN; 00039 00040 _esp.attach(this, &ESP8266Interface::event); 00041 _esp.setTimeout(); 00042 } 00043 00044 int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, 00045 uint8_t channel) 00046 { 00047 if (channel != 0) { 00048 return NSAPI_ERROR_UNSUPPORTED; 00049 } 00050 00051 int err = set_credentials(ssid, pass, security); 00052 if(err) { 00053 return err; 00054 } 00055 00056 return connect(); 00057 } 00058 00059 int ESP8266Interface::connect() 00060 { 00061 nsapi_error_t status; 00062 00063 if (strlen(ap_ssid) == 0) { 00064 return NSAPI_ERROR_NO_SSID; 00065 } 00066 00067 if (ap_sec != NSAPI_SECURITY_NONE) { 00068 if (strlen(ap_pass) < ESP8266_PASSPHRASE_MIN_LENGTH) { 00069 return NSAPI_ERROR_PARAMETER; 00070 } 00071 } 00072 00073 status = _init(); 00074 if(status != NSAPI_ERROR_OK) { 00075 return status; 00076 } 00077 00078 if(get_ip_address()) { 00079 return NSAPI_ERROR_IS_CONNECTED; 00080 } 00081 00082 status = _startup(ESP8266::WIFIMODE_STATION); 00083 if(status != NSAPI_ERROR_OK) { 00084 return status; 00085 } 00086 _started = true; 00087 00088 if (!_esp.dhcp(true, 1)) { 00089 return NSAPI_ERROR_DHCP_FAILURE; 00090 } 00091 00092 int connect_error = _esp.connect(ap_ssid, ap_pass); 00093 if (connect_error) { 00094 return connect_error; 00095 } 00096 00097 if (!get_ip_address()) { 00098 return NSAPI_ERROR_DHCP_FAILURE; 00099 } 00100 00101 return NSAPI_ERROR_OK; 00102 } 00103 00104 int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) 00105 { 00106 ap_sec = security; 00107 00108 if (!ssid) { 00109 return NSAPI_ERROR_PARAMETER; 00110 } 00111 00112 int ssid_length = strlen(ssid); 00113 00114 if (ssid_length > 0 00115 && ssid_length <= ESP8266_SSID_MAX_LENGTH) { 00116 memset(ap_ssid, 0, sizeof(ap_ssid)); 00117 strncpy(ap_ssid, ssid, sizeof(ap_ssid)); 00118 } else { 00119 return NSAPI_ERROR_PARAMETER; 00120 } 00121 00122 if (ap_sec != NSAPI_SECURITY_NONE) { 00123 00124 if (!pass) { 00125 return NSAPI_ERROR_PARAMETER; 00126 } 00127 00128 int pass_length = strlen(pass); 00129 if (pass_length >= ESP8266_PASSPHRASE_MIN_LENGTH 00130 && pass_length <= ESP8266_PASSPHRASE_MAX_LENGTH ) { 00131 memset(ap_pass, 0, sizeof(ap_pass)); 00132 strncpy(ap_pass, pass, sizeof(ap_pass)); 00133 } else { 00134 return NSAPI_ERROR_PARAMETER; 00135 } 00136 } else { 00137 memset(ap_pass, 0, sizeof(ap_pass)); 00138 } 00139 00140 return NSAPI_ERROR_OK; 00141 } 00142 00143 int ESP8266Interface::set_channel(uint8_t channel) 00144 { 00145 return NSAPI_ERROR_UNSUPPORTED; 00146 } 00147 00148 00149 int ESP8266Interface::disconnect() 00150 { 00151 _started = false; 00152 _initialized = false; 00153 00154 return _esp.disconnect() ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR; 00155 } 00156 00157 const char *ESP8266Interface::get_ip_address() 00158 { 00159 if(!_started) { 00160 return NULL; 00161 } 00162 00163 const char *ip_buff = _esp.getIPAddress(); 00164 if(!ip_buff || std::strcmp(ip_buff, "0.0.0.0") == 0) { 00165 return NULL; 00166 } 00167 00168 return ip_buff; 00169 } 00170 00171 const char *ESP8266Interface::get_mac_address() 00172 { 00173 return _esp.getMACAddress(); 00174 } 00175 00176 const char *ESP8266Interface::get_gateway() 00177 { 00178 return _started ? _esp.getGateway() : NULL; 00179 } 00180 00181 const char *ESP8266Interface::get_netmask() 00182 { 00183 return _started ? _esp.getNetmask() : NULL; 00184 } 00185 00186 int8_t ESP8266Interface::get_rssi() 00187 { 00188 return _started ? _esp.getRSSI() : 0; 00189 } 00190 00191 int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count) 00192 { 00193 nsapi_error_t status; 00194 00195 status = _init(); 00196 if(status != NSAPI_ERROR_OK) { 00197 return status; 00198 } 00199 00200 status = _startup(ESP8266::WIFIMODE_STATION); 00201 if(status != NSAPI_ERROR_OK) { 00202 return status; 00203 } 00204 00205 return _esp.scan(res, count); 00206 } 00207 00208 bool ESP8266Interface::_get_firmware_ok() 00209 { 00210 if (_esp.get_firmware_version() != ESP8266_VERSION) { 00211 debug("ESP8266: ERROR: Firmware incompatible with this driver.\ 00212 \r\nUpdate to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n",ESP8266_VERSION); 00213 return false; 00214 } 00215 00216 return true; 00217 } 00218 00219 bool ESP8266Interface::_disable_default_softap() 00220 { 00221 static int disabled = false; 00222 00223 if (disabled || _esp.get_default_wifi_mode() == ESP8266::WIFIMODE_STATION) { 00224 disabled = true; 00225 return true; 00226 } 00227 if (_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) { 00228 disabled = true; 00229 return true; 00230 } 00231 00232 return false; 00233 } 00234 00235 nsapi_error_t ESP8266Interface::_init(void) 00236 { 00237 if (!_initialized) { 00238 if (!_esp.reset()) { 00239 return NSAPI_ERROR_DEVICE_ERROR; 00240 } 00241 if (!_get_firmware_ok()) { 00242 return NSAPI_ERROR_DEVICE_ERROR; 00243 } 00244 if (_disable_default_softap() == false) { 00245 return NSAPI_ERROR_DEVICE_ERROR; 00246 } 00247 _initialized = true; 00248 } 00249 return NSAPI_ERROR_OK; 00250 } 00251 00252 nsapi_error_t ESP8266Interface::_startup(const int8_t wifi_mode) 00253 { 00254 if (!_started) { 00255 if (!_esp.startup(wifi_mode)) { 00256 return NSAPI_ERROR_DEVICE_ERROR; 00257 } 00258 } 00259 return NSAPI_ERROR_OK; 00260 } 00261 00262 struct esp8266_socket { 00263 int id; 00264 nsapi_protocol_t proto; 00265 bool connected; 00266 SocketAddress addr; 00267 int keepalive; // TCP 00268 }; 00269 00270 int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) 00271 { 00272 // Look for an unused socket 00273 int id = -1; 00274 00275 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { 00276 if (!_ids[i]) { 00277 id = i; 00278 _ids[i] = true; 00279 break; 00280 } 00281 } 00282 00283 if (id == -1) { 00284 return NSAPI_ERROR_NO_SOCKET; 00285 } 00286 00287 struct esp8266_socket *socket = new struct esp8266_socket; 00288 if (!socket) { 00289 return NSAPI_ERROR_NO_SOCKET; 00290 } 00291 00292 socket->id = id; 00293 socket->proto = proto; 00294 socket->connected = false; 00295 socket->keepalive = 0; 00296 *handle = socket; 00297 return 0; 00298 } 00299 00300 int ESP8266Interface::socket_close(void *handle) 00301 { 00302 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00303 int err = 0; 00304 00305 if (socket->connected && !_esp.close(socket->id)) { 00306 err = NSAPI_ERROR_DEVICE_ERROR; 00307 } 00308 00309 socket->connected = false; 00310 _ids[socket->id] = false; 00311 _local_ports[socket->id] = 0; 00312 delete socket; 00313 return err; 00314 } 00315 00316 int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) 00317 { 00318 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00319 00320 if (socket->proto == NSAPI_UDP) { 00321 if(address.get_addr().version != NSAPI_UNSPEC) { 00322 return NSAPI_ERROR_UNSUPPORTED; 00323 } 00324 00325 for(int id = 0; id < ESP8266_SOCKET_COUNT; id++) { 00326 if(_local_ports[id] == address.get_port() && id != socket->id) { // Port already reserved by another socket 00327 return NSAPI_ERROR_PARAMETER; 00328 } else if (id == socket->id && socket->connected) { 00329 return NSAPI_ERROR_PARAMETER; 00330 } 00331 } 00332 _local_ports[socket->id] = address.get_port(); 00333 return 0; 00334 } 00335 00336 return NSAPI_ERROR_UNSUPPORTED; 00337 } 00338 00339 int ESP8266Interface::socket_listen(void *handle, int backlog) 00340 { 00341 return NSAPI_ERROR_UNSUPPORTED; 00342 } 00343 00344 int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) 00345 { 00346 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00347 00348 if (socket->proto == NSAPI_UDP) { 00349 if (!_esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _local_ports[socket->id])) { 00350 return NSAPI_ERROR_DEVICE_ERROR; 00351 } 00352 } else { 00353 if (!_esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive)) { 00354 return NSAPI_ERROR_DEVICE_ERROR; 00355 } 00356 } 00357 00358 socket->connected = true; 00359 return 0; 00360 } 00361 00362 int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr) 00363 { 00364 return NSAPI_ERROR_UNSUPPORTED; 00365 } 00366 00367 int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) 00368 { 00369 nsapi_error_t status; 00370 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00371 00372 status = _esp.send(socket->id, data, size); 00373 00374 return status != NSAPI_ERROR_OK ? status : size; 00375 } 00376 00377 int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) 00378 { 00379 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00380 00381 int32_t recv; 00382 if (socket->proto == NSAPI_TCP) { 00383 recv = _esp.recv_tcp(socket->id, data, size); 00384 if (recv <= 0 && recv != NSAPI_ERROR_WOULD_BLOCK) { 00385 socket->connected = false; 00386 } 00387 } else { 00388 recv = _esp.recv_udp(socket->id, data, size); 00389 } 00390 00391 return recv; 00392 } 00393 00394 int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) 00395 { 00396 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00397 00398 if((strcmp(addr.get_ip_address(), "0.0.0.0") == 0) || !addr.get_port()) { 00399 return NSAPI_ERROR_DNS_FAILURE; 00400 } 00401 00402 if (socket->connected && socket->addr != addr) { 00403 if (!_esp.close(socket->id)) { 00404 return NSAPI_ERROR_DEVICE_ERROR; 00405 } 00406 socket->connected = false; 00407 } 00408 00409 if (!socket->connected) { 00410 int err = socket_connect(socket, addr); 00411 if (err < 0) { 00412 return err; 00413 } 00414 socket->addr = addr; 00415 } 00416 00417 return socket_send(socket, data, size); 00418 } 00419 00420 int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) 00421 { 00422 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00423 int ret = socket_recv(socket, data, size); 00424 if (ret >= 0 && addr) { 00425 *addr = socket->addr; 00426 } 00427 00428 return ret; 00429 } 00430 00431 void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) 00432 { 00433 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00434 _cbs[socket->id].callback = callback; 00435 _cbs[socket->id].data = data; 00436 } 00437 00438 nsapi_error_t ESP8266Interface::setsockopt(nsapi_socket_t handle, int level, 00439 int optname, const void *optval, unsigned optlen) 00440 { 00441 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00442 00443 if (!optlen || !socket) { 00444 return NSAPI_ERROR_PARAMETER; 00445 } 00446 00447 if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) { 00448 switch (optname) { 00449 case NSAPI_KEEPALIVE: { 00450 if(socket->connected) {// ESP8266 limitation, keepalive needs to be given before connecting 00451 return NSAPI_ERROR_UNSUPPORTED; 00452 } 00453 00454 if (optlen == sizeof(int)) { 00455 int secs = *(int *)optval; 00456 if (secs >= 0 && secs <= 7200) { 00457 socket->keepalive = secs; 00458 return NSAPI_ERROR_OK; 00459 } 00460 } 00461 return NSAPI_ERROR_PARAMETER; 00462 } 00463 } 00464 } 00465 00466 return NSAPI_ERROR_UNSUPPORTED; 00467 } 00468 00469 nsapi_error_t ESP8266Interface::getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen) 00470 { 00471 struct esp8266_socket *socket = (struct esp8266_socket *)handle; 00472 00473 if (!optval || !optlen || !socket) { 00474 return NSAPI_ERROR_PARAMETER; 00475 } 00476 00477 if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) { 00478 switch (optname) { 00479 case NSAPI_KEEPALIVE: { 00480 if(*optlen > sizeof(int)) { 00481 *optlen = sizeof(int); 00482 } 00483 memcpy(optval, &(socket->keepalive), *optlen); 00484 return NSAPI_ERROR_OK; 00485 } 00486 } 00487 } 00488 00489 return NSAPI_ERROR_UNSUPPORTED; 00490 } 00491 00492 00493 void ESP8266Interface::event() { 00494 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { 00495 if (_cbs[i].callback) { 00496 _cbs[i].callback(_cbs[i].data); 00497 } 00498 } 00499 }
Generated on Tue Jul 12 2022 16:22:05 by 1.7.2