The driver for the ESP32 WiFi module
Embed:
(wiki syntax)
Show/hide line numbers
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(PinName en, PinName io0, PinName tx, PinName rx, bool debug, 00022 PinName rts, PinName cts, int baudrate) : 00023 ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate), 00024 _dhcp(true), 00025 _ap_ssid(), 00026 _ap_pass(), 00027 _ap_sec(NSAPI_SECURITY_NONE), 00028 _ip_address(), 00029 _netmask(), 00030 _gateway(), 00031 _connection_status(NSAPI_STATUS_DISCONNECTED), 00032 _connection_status_cb(NULL) 00033 { 00034 _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb)); 00035 } 00036 00037 ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) : 00038 ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400), 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 nsapi_error_t ESP32Interface::set_network(const char *ip_address, const char *netmask, const char *gateway) 00053 { 00054 _dhcp = false; 00055 00056 strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address)); 00057 _ip_address[sizeof(_ip_address) - 1] = '\0'; 00058 strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask)); 00059 _netmask[sizeof(_netmask) - 1] = '\0'; 00060 strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway)); 00061 _gateway[sizeof(_gateway) - 1] = '\0'; 00062 00063 return NSAPI_ERROR_OK; 00064 } 00065 00066 nsapi_error_t ESP32Interface::set_dhcp(bool dhcp) 00067 { 00068 _dhcp = dhcp; 00069 00070 return NSAPI_ERROR_OK; 00071 } 00072 00073 int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, 00074 uint8_t channel) 00075 { 00076 if (channel != 0) { 00077 return NSAPI_ERROR_UNSUPPORTED; 00078 } 00079 00080 set_credentials(ssid, pass, security); 00081 return connect(); 00082 } 00083 00084 int ESP32Interface::connect() 00085 { 00086 if (!_esp->dhcp(_dhcp, 1)) { 00087 return NSAPI_ERROR_DHCP_FAILURE; 00088 } 00089 00090 if (!_dhcp) { 00091 if (!_esp->set_network(_ip_address, _netmask, _gateway)) { 00092 return NSAPI_ERROR_DEVICE_ERROR; 00093 } 00094 } 00095 00096 set_connection_status(NSAPI_STATUS_CONNECTING); 00097 if (!_esp->connect(_ap_ssid, _ap_pass)) { 00098 set_connection_status(NSAPI_STATUS_DISCONNECTED); 00099 return NSAPI_ERROR_NO_CONNECTION; 00100 } 00101 00102 return NSAPI_ERROR_OK; 00103 } 00104 00105 int ESP32Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) 00106 { 00107 memset(_ap_ssid, 0, sizeof(_ap_ssid)); 00108 strncpy(_ap_ssid, ssid, sizeof(_ap_ssid)); 00109 00110 memset(_ap_pass, 0, sizeof(_ap_pass)); 00111 strncpy(_ap_pass, pass, sizeof(_ap_pass)); 00112 00113 _ap_sec = security; 00114 00115 return 0; 00116 } 00117 00118 int ESP32Interface::set_channel(uint8_t channel) 00119 { 00120 return NSAPI_ERROR_UNSUPPORTED; 00121 } 00122 00123 int ESP32Interface::disconnect() 00124 { 00125 if (!_esp->disconnect()) { 00126 return NSAPI_ERROR_DEVICE_ERROR; 00127 } 00128 00129 return NSAPI_ERROR_OK; 00130 } 00131 00132 const char *ESP32Interface::get_ip_address() 00133 { 00134 return _esp->getIPAddress(); 00135 } 00136 00137 const char *ESP32Interface::get_mac_address() 00138 { 00139 return _esp->getMACAddress(); 00140 } 00141 00142 const char *ESP32Interface::get_gateway() 00143 { 00144 return _esp->getGateway(); 00145 } 00146 00147 const char *ESP32Interface::get_netmask() 00148 { 00149 return _esp->getNetmask(); 00150 } 00151 00152 int8_t ESP32Interface::get_rssi() 00153 { 00154 return _esp->getRSSI(); 00155 } 00156 00157 int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count) 00158 { 00159 return _esp->scan(res, count); 00160 } 00161 00162 void ESP32Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) 00163 { 00164 _connection_status_cb = status_cb; 00165 } 00166 00167 nsapi_connection_status_t ESP32Interface::get_connection_status() const 00168 { 00169 return _connection_status; 00170 } 00171 00172 void ESP32Interface::set_connection_status(nsapi_connection_status_t connection_status) 00173 { 00174 if (_connection_status != connection_status) { 00175 _connection_status = connection_status; 00176 if (_connection_status_cb) { 00177 _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status); 00178 } 00179 } 00180 } 00181 00182 void ESP32Interface::wifi_status_cb(int8_t wifi_status) 00183 { 00184 switch (wifi_status) { 00185 case ESP32::STATUS_DISCONNECTED: 00186 set_connection_status(NSAPI_STATUS_DISCONNECTED); 00187 break; 00188 case ESP32::STATUS_GOT_IP: 00189 set_connection_status(NSAPI_STATUS_GLOBAL_UP); 00190 break; 00191 case ESP32::STATUS_CONNECTED: 00192 default: 00193 // do nothing 00194 break; 00195 } 00196 } 00197
Generated on Wed Jul 13 2022 08:56:28 by 1.7.2