Advantech / Mbed OS pelion-example-common
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP32Interface.cpp Source File

ESP32Interface.cpp

00001 /* ESP32 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2017 Renesas Electronics Corporation
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 <string.h>
00018 #include "ESP32Interface.h"
00019 
00020 // ESP32Interface implementation
00021 ESP32Interface::ESP32Interface() :
00022     ESP32Stack(MBED_CONF_ESP32_WIFI_EN, MBED_CONF_ESP32_WIFI_IO0, MBED_CONF_ESP32_WIFI_TX, MBED_CONF_ESP32_WIFI_RX, MBED_CONF_ESP32_WIFI_DEBUG, MBED_CONF_ESP32_WIFI_RTS, MBED_CONF_ESP32_WIFI_CTS, MBED_CONF_ESP32_WIFI_BAUDRATE),
00023      _dhcp(true),
00024     _ap_ssid(),
00025     _ap_pass(),
00026     _ap_sec(NSAPI_SECURITY_NONE),
00027     _ip_address(),
00028     _netmask(),
00029     _gateway(),
00030     _connection_status(NSAPI_STATUS_DISCONNECTED),
00031     _connection_status_cb(NULL)
00032 {
00033     _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb));
00034 }
00035 
00036 ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
00037     PinName rts, PinName cts, int baudrate) :
00038     ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate),
00039      _dhcp(true),
00040     _ap_ssid(),
00041     _ap_pass(),
00042     _ap_sec(NSAPI_SECURITY_NONE),
00043     _ip_address(),
00044     _netmask(),
00045     _gateway(),
00046     _connection_status(NSAPI_STATUS_DISCONNECTED),
00047     _connection_status_cb(NULL)
00048 {
00049     _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb));
00050 }
00051 
00052 ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) :
00053     ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400),
00054      _dhcp(true),
00055     _ap_ssid(),
00056     _ap_pass(),
00057     _ap_sec(NSAPI_SECURITY_NONE),
00058     _ip_address(),
00059     _netmask(),
00060     _gateway(),
00061     _connection_status(NSAPI_STATUS_DISCONNECTED),
00062     _connection_status_cb(NULL)
00063 {
00064     _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb));
00065 }
00066 
00067 nsapi_error_t ESP32Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00068 {
00069     _dhcp = false;
00070 
00071     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00072     _ip_address[sizeof(_ip_address) - 1] = '\0';
00073     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00074     _netmask[sizeof(_netmask) - 1] = '\0';
00075     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00076     _gateway[sizeof(_gateway) - 1] = '\0';
00077 
00078     return NSAPI_ERROR_OK;
00079 }
00080 
00081 nsapi_error_t ESP32Interface::set_dhcp(bool dhcp)
00082 {
00083     _dhcp = dhcp;
00084 
00085     return NSAPI_ERROR_OK;
00086 }
00087 
00088 int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
00089                                         uint8_t channel)
00090 {
00091     if (channel != 0) {
00092         return NSAPI_ERROR_UNSUPPORTED;
00093     }
00094 
00095     set_credentials(ssid, pass, security);
00096     return connect();
00097 }
00098 
00099 int ESP32Interface::connect()
00100 {
00101     if (!_esp->dhcp(_dhcp, 1)) {
00102         return NSAPI_ERROR_DHCP_FAILURE;
00103     }
00104 
00105     if (!_dhcp) {
00106         if (!_esp->set_network(_ip_address, _netmask, _gateway)) {
00107             return NSAPI_ERROR_DEVICE_ERROR;
00108         }
00109     }
00110 
00111     set_connection_status(NSAPI_STATUS_CONNECTING);
00112     if (!_esp->connect(_ap_ssid, _ap_pass)) {
00113         set_connection_status(NSAPI_STATUS_DISCONNECTED);
00114         return NSAPI_ERROR_NO_CONNECTION;
00115     }
00116 
00117     return NSAPI_ERROR_OK;
00118 }
00119 
00120 int ESP32Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
00121 {
00122     memset(_ap_ssid, 0, sizeof(_ap_ssid));
00123     strncpy(_ap_ssid, ssid, sizeof(_ap_ssid));
00124 
00125     memset(_ap_pass, 0, sizeof(_ap_pass));
00126     strncpy(_ap_pass, pass, sizeof(_ap_pass));
00127 
00128     _ap_sec = security;
00129 
00130     return 0;
00131 }
00132 
00133 int ESP32Interface::set_channel(uint8_t channel)
00134 {
00135     return NSAPI_ERROR_UNSUPPORTED;
00136 }
00137 
00138 int ESP32Interface::disconnect()
00139 {
00140     if (!_esp->disconnect()) {
00141         return NSAPI_ERROR_DEVICE_ERROR;
00142     }
00143 
00144     return NSAPI_ERROR_OK;
00145 }
00146 
00147 const char *ESP32Interface::get_ip_address()
00148 {
00149     return _esp->getIPAddress();
00150 }
00151 
00152 const char *ESP32Interface::get_mac_address()
00153 {
00154     return _esp->getMACAddress();
00155 }
00156 
00157 const char *ESP32Interface::get_gateway()
00158 {
00159     return _esp->getGateway();
00160 }
00161 
00162 const char *ESP32Interface::get_netmask()
00163 {
00164     return _esp->getNetmask();
00165 }
00166 
00167 int8_t ESP32Interface::get_rssi()
00168 {
00169     return _esp->getRSSI();
00170 }
00171 
00172 int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count)
00173 {
00174     return _esp->scan(res, count);
00175 }
00176 
00177 void ESP32Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00178 {
00179     _connection_status_cb = status_cb;
00180 }
00181 
00182 nsapi_connection_status_t ESP32Interface::get_connection_status() const
00183 {
00184     return _connection_status;
00185 }
00186 
00187 void ESP32Interface::set_connection_status(nsapi_connection_status_t connection_status)
00188 {
00189     if (_connection_status != connection_status) {
00190         _connection_status = connection_status;
00191         if (_connection_status_cb) {
00192             _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
00193         }
00194     }
00195 }
00196 
00197 void ESP32Interface::wifi_status_cb(int8_t wifi_status)
00198 {
00199     switch (wifi_status) {
00200         case ESP32::STATUS_DISCONNECTED:
00201             set_connection_status(NSAPI_STATUS_DISCONNECTED);
00202             break;
00203         case ESP32::STATUS_GOT_IP:
00204             set_connection_status(NSAPI_STATUS_GLOBAL_UP);
00205             break;
00206         case ESP32::STATUS_CONNECTED:
00207         default:
00208             // do nothing
00209             break;
00210     }
00211 }
00212 
00213 #if MBED_CONF_ESP32_PROVIDE_DEFAULT
00214 
00215 WiFiInterface *WiFiInterface::get_default_instance() {
00216     static ESP32Interface esp32;
00217     return &esp32;
00218 }
00219 
00220 #endif
00221