test public

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP32InterfaceAP.cpp Source File

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