2016-10-06 13:21:30 +02:00
|
|
|
/*
|
|
|
|
ESP8266WiFiGeneric.cpp - WiFi library for esp8266
|
|
|
|
|
|
|
|
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
|
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Reworked on 28 Dec 2015 by Markus Sattler
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "WiFi.h"
|
|
|
|
#include "WiFiGeneric.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
2016-11-13 16:30:21 +01:00
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
#include <esp_err.h>
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
#include <esp_event_loop.h>
|
2016-11-13 16:30:21 +01:00
|
|
|
#include "lwip/ip_addr.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
#include "lwip/opt.h"
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/dns.h"
|
2016-11-27 23:50:21 +01:00
|
|
|
#include "esp_ipc.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
|
2016-11-27 23:50:21 +01:00
|
|
|
} //extern "C"
|
|
|
|
|
2017-02-23 00:11:57 +01:00
|
|
|
#include "esp32-hal-log.h"
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#include <vector>
|
|
|
|
|
2017-10-21 08:50:43 +02:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#if CONFIG_FREERTOS_UNICORE
|
|
|
|
#define ARDUINO_RUNNING_CORE 0
|
|
|
|
#else
|
|
|
|
#define ARDUINO_RUNNING_CORE 1
|
|
|
|
#endif
|
2017-09-28 05:41:35 +02:00
|
|
|
|
|
|
|
static xQueueHandle _network_event_queue;
|
|
|
|
static TaskHandle_t _network_event_task_handle = NULL;
|
|
|
|
|
|
|
|
static void _network_event_task(void * arg){
|
|
|
|
system_event_t *event = NULL;
|
|
|
|
for (;;) {
|
2018-01-17 23:56:58 +01:00
|
|
|
if(xQueueReceive(_network_event_queue, &event, portMAX_DELAY) == pdTRUE){
|
|
|
|
WiFiGenericClass::_eventCallback(arg, event);
|
2017-09-28 05:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
_network_event_task_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t _network_event_cb(void *arg, system_event_t *event){
|
|
|
|
if (xQueueSend(_network_event_queue, &event, portMAX_DELAY) != pdPASS) {
|
|
|
|
log_w("Network Event Queue Send Failed!");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _start_network_event_task(){
|
|
|
|
if(!_network_event_queue){
|
|
|
|
_network_event_queue = xQueueCreate(32, sizeof(system_event_t *));
|
|
|
|
if(!_network_event_queue){
|
|
|
|
log_e("Network Event Queue Create Failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!_network_event_task_handle){
|
2017-10-21 08:47:24 +02:00
|
|
|
xTaskCreatePinnedToCore(_network_event_task, "network_event", 4096, NULL, 2, &_network_event_task_handle, ARDUINO_RUNNING_CORE);
|
2017-09-28 05:41:35 +02:00
|
|
|
if(!_network_event_task_handle){
|
|
|
|
log_e("Network Event Task Start Failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
esp_event_loop_init(&_network_event_cb, NULL);
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:31:23 +02:00
|
|
|
void tcpipInit(){
|
|
|
|
static bool initialized = false;
|
|
|
|
if(!initialized){
|
|
|
|
initialized = true;
|
2017-09-28 05:41:35 +02:00
|
|
|
_start_network_event_task();
|
2017-05-22 14:31:23 +02:00
|
|
|
tcpip_adapter_init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:11:57 +01:00
|
|
|
static bool wifiLowLevelInit(){
|
|
|
|
static bool lowLevelInitDone = false;
|
|
|
|
if(!lowLevelInitDone){
|
2017-05-22 14:31:23 +02:00
|
|
|
tcpipInit();
|
2017-02-23 00:11:57 +01:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
esp_err_t err = esp_wifi_init(&cfg);
|
|
|
|
if(err){
|
|
|
|
log_e("esp_wifi_init %d", err);
|
2016-11-27 23:50:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
|
|
|
|
esp_wifi_set_mode(WIFI_MODE_NULL);
|
|
|
|
lowLevelInitDone = true;
|
2016-11-27 23:50:21 +01:00
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wifiLowLevelDeinit(){
|
|
|
|
//deinit not working yet!
|
|
|
|
//esp_wifi_deinit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _esp_wifi_started = false;
|
|
|
|
|
|
|
|
static bool espWiFiStart(){
|
|
|
|
if(_esp_wifi_started){
|
2016-11-27 23:50:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
if(!wifiLowLevelInit()){
|
2016-11-27 23:50:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
esp_err_t err = esp_wifi_start();
|
2016-11-27 23:50:21 +01:00
|
|
|
if (err != ESP_OK) {
|
2017-02-23 00:11:57 +01:00
|
|
|
log_e("esp_wifi_start %d", err);
|
|
|
|
wifiLowLevelDeinit();
|
2016-11-27 23:50:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
_esp_wifi_started = true;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-27 23:50:21 +01:00
|
|
|
|
2017-02-23 00:11:57 +01:00
|
|
|
static bool espWiFiStop(){
|
|
|
|
esp_err_t err;
|
|
|
|
if(!_esp_wifi_started){
|
|
|
|
return true;
|
2016-11-27 23:50:21 +01:00
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
err = esp_wifi_stop();
|
|
|
|
if(err){
|
|
|
|
log_e("Could not stop WiFi! %u", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_esp_wifi_started = false;
|
|
|
|
return wifiLowLevelDeinit();
|
2016-11-27 23:50:21 +01:00
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
// -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2017-09-18 11:54:57 +02:00
|
|
|
typedef struct {
|
|
|
|
WiFiEventCb cb;
|
|
|
|
WiFiEventFullCb fcb;
|
2018-01-17 23:56:58 +01:00
|
|
|
WiFiEventSysCb scb;
|
2017-09-18 11:54:57 +02:00
|
|
|
system_event_id_t event;
|
|
|
|
} WiFiEventCbList_t;
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
// arduino dont like std::vectors move static here
|
|
|
|
static std::vector<WiFiEventCbList_t> cbEventList;
|
|
|
|
|
|
|
|
bool WiFiGenericClass::_persistent = true;
|
|
|
|
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;
|
|
|
|
|
|
|
|
WiFiGenericClass::WiFiGenericClass()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set callback function
|
|
|
|
* @param cbEvent WiFiEventCb
|
|
|
|
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
|
|
|
*/
|
|
|
|
void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WiFiEventCbList_t newEventHandler;
|
|
|
|
newEventHandler.cb = cbEvent;
|
2017-09-18 11:54:57 +02:00
|
|
|
newEventHandler.fcb = NULL;
|
2018-01-17 23:56:58 +01:00
|
|
|
newEventHandler.scb = NULL;
|
2017-09-18 11:54:57 +02:00
|
|
|
newEventHandler.event = event;
|
|
|
|
cbEventList.push_back(newEventHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiGenericClass::onEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WiFiEventCbList_t newEventHandler;
|
|
|
|
newEventHandler.cb = NULL;
|
|
|
|
newEventHandler.fcb = cbEvent;
|
2018-01-17 23:56:58 +01:00
|
|
|
newEventHandler.scb = NULL;
|
|
|
|
newEventHandler.event = event;
|
|
|
|
cbEventList.push_back(newEventHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WiFiEventCbList_t newEventHandler;
|
|
|
|
newEventHandler.cb = NULL;
|
|
|
|
newEventHandler.fcb = NULL;
|
|
|
|
newEventHandler.scb = cbEvent;
|
2016-10-06 13:21:30 +02:00
|
|
|
newEventHandler.event = event;
|
|
|
|
cbEventList.push_back(newEventHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes a callback form event handler
|
|
|
|
* @param cbEvent WiFiEventCb
|
|
|
|
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
|
|
|
*/
|
|
|
|
void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
|
|
|
WiFiEventCbList_t entry = cbEventList[i];
|
|
|
|
if(entry.cb == cbEvent && entry.event == event) {
|
|
|
|
cbEventList.erase(cbEventList.begin() + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 11:54:57 +02:00
|
|
|
void WiFiGenericClass::removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
|
|
|
WiFiEventCbList_t entry = cbEventList[i];
|
|
|
|
if(entry.fcb == cbEvent && entry.event == event) {
|
|
|
|
cbEventList.erase(cbEventList.begin() + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
|
|
|
|
{
|
|
|
|
if(!cbEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
|
|
|
WiFiEventCbList_t entry = cbEventList[i];
|
|
|
|
if(entry.scb == cbEvent && entry.event == event) {
|
|
|
|
cbEventList.erase(cbEventList.begin() + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:21:30 +02:00
|
|
|
/**
|
|
|
|
* callback for WiFi events
|
|
|
|
* @param arg
|
|
|
|
*/
|
2017-02-06 14:17:11 +01:00
|
|
|
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
|
2018-01-17 23:56:58 +01:00
|
|
|
const char * system_event_names[] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONNECTED", "STA_DISCONNECTED", "STA_AUTHMODE_CHANGE", "STA_GOT_IP", "STA_LOST_IP", "STA_WPS_ER_SUCCESS", "STA_WPS_ER_FAILED", "STA_WPS_ER_TIMEOUT", "STA_WPS_ER_PIN", "AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_PROBEREQRECVED", "GOT_IP6", "ETH_START", "ETH_STOP", "ETH_CONNECTED", "ETH_DISCONNECTED", "ETH_GOT_IP", "MAX"};
|
2017-02-10 03:27:48 +01:00
|
|
|
#endif
|
|
|
|
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
|
2017-02-05 22:59:44 +01:00
|
|
|
const char * system_event_reasons[] = { "UNSPECIFIED", "AUTH_EXPIRE", "AUTH_LEAVE", "ASSOC_EXPIRE", "ASSOC_TOOMANY", "NOT_AUTHED", "NOT_ASSOCED", "ASSOC_LEAVE", "ASSOC_NOT_AUTHED", "DISASSOC_PWRCAP_BAD", "DISASSOC_SUPCHAN_BAD", "IE_INVALID", "MIC_FAILURE", "4WAY_HANDSHAKE_TIMEOUT", "GROUP_KEY_UPDATE_TIMEOUT", "IE_IN_4WAY_DIFFERS", "GROUP_CIPHER_INVALID", "PAIRWISE_CIPHER_INVALID", "AKMP_INVALID", "UNSUPP_RSN_IE_VERSION", "INVALID_RSN_IE_CAP", "802_1X_AUTH_FAILED", "CIPHER_SUITE_REJECTED", "BEACON_TIMEOUT", "NO_AP_FOUND", "AUTH_FAIL", "ASSOC_FAIL", "HANDSHAKE_TIMEOUT" };
|
2018-03-04 21:05:54 +01:00
|
|
|
#define reason2str(r) ((r>176)?system_event_reasons[r-177]:system_event_reasons[r-1])
|
2017-02-05 22:59:44 +01:00
|
|
|
#endif
|
2016-10-06 13:21:30 +02:00
|
|
|
esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
|
|
|
|
{
|
2017-02-05 22:59:44 +01:00
|
|
|
log_d("Event: %d - %s", event->event_id, system_event_names[event->event_id]);
|
2016-10-06 13:21:30 +02:00
|
|
|
if(event->event_id == SYSTEM_EVENT_SCAN_DONE) {
|
|
|
|
WiFiScanClass::_scanDone();
|
|
|
|
} else if(event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
|
|
|
|
uint8_t reason = event->event_info.disconnected.reason;
|
2017-02-05 22:59:44 +01:00
|
|
|
log_w("Reason: %u - %s", reason, reason2str(reason));
|
2016-10-06 13:21:30 +02:00
|
|
|
if(reason == WIFI_REASON_NO_AP_FOUND) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_NO_SSID_AVAIL);
|
|
|
|
} else if(reason == WIFI_REASON_AUTH_FAIL || reason == WIFI_REASON_ASSOC_FAIL) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_CONNECT_FAILED);
|
|
|
|
} else if(reason == WIFI_REASON_BEACON_TIMEOUT || reason == WIFI_REASON_HANDSHAKE_TIMEOUT) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_CONNECTION_LOST);
|
2017-03-25 23:28:45 +01:00
|
|
|
} else if(reason == WIFI_REASON_AUTH_EXPIRE) {
|
2017-05-22 16:50:57 +02:00
|
|
|
if(WiFi.getAutoReconnect()){
|
|
|
|
WiFi.begin();
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
} else {
|
|
|
|
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
|
|
|
|
}
|
|
|
|
} else if(event->event_id == SYSTEM_EVENT_STA_START) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
|
|
|
|
} else if(event->event_id == SYSTEM_EVENT_STA_STOP) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_NO_SHIELD);
|
2018-01-18 12:54:26 +01:00
|
|
|
} else if(event->event_id == SYSTEM_EVENT_STA_CONNECTED) {
|
|
|
|
WiFiSTAClass::_setStatus(WL_IDLE_STATUS);
|
2016-10-06 13:21:30 +02:00
|
|
|
} else if(event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
|
2018-03-04 21:05:24 +01:00
|
|
|
//#1081 https://github.com/espressif/arduino-esp32/issues/1081
|
|
|
|
// if(WiFiSTAClass::status() == WL_IDLE_STATUS)
|
|
|
|
{
|
2018-01-18 12:54:26 +01:00
|
|
|
WiFiSTAClass::_setStatus(WL_CONNECTED);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
|
|
|
WiFiEventCbList_t entry = cbEventList[i];
|
2018-01-17 23:56:58 +01:00
|
|
|
if(entry.cb || entry.fcb || entry.scb) {
|
2016-10-06 13:21:30 +02:00
|
|
|
if(entry.event == (system_event_id_t) event->event_id || entry.event == SYSTEM_EVENT_MAX) {
|
2017-09-18 11:54:57 +02:00
|
|
|
if(entry.cb){
|
|
|
|
entry.cb((system_event_id_t) event->event_id);
|
2018-01-17 23:56:58 +01:00
|
|
|
} else if(entry.fcb){
|
2017-09-18 11:54:57 +02:00
|
|
|
entry.fcb((system_event_id_t) event->event_id, (system_event_info_t) event->event_info);
|
2018-01-17 23:56:58 +01:00
|
|
|
} else {
|
|
|
|
entry.scb(event);
|
2017-09-18 11:54:57 +02:00
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current channel associated with the network
|
|
|
|
* @return channel (1-13)
|
|
|
|
*/
|
|
|
|
int32_t WiFiGenericClass::channel(void)
|
|
|
|
{
|
|
|
|
uint8_t primaryChan;
|
|
|
|
wifi_second_chan_t secondChan;
|
|
|
|
esp_wifi_get_channel(&primaryChan, &secondChan);
|
|
|
|
return primaryChan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store WiFi config in SDK flash area
|
|
|
|
* @param persistent
|
|
|
|
*/
|
|
|
|
void WiFiGenericClass::persistent(bool persistent)
|
|
|
|
{
|
|
|
|
_persistent = persistent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set new mode
|
|
|
|
* @param m WiFiMode_t
|
|
|
|
*/
|
|
|
|
bool WiFiGenericClass::mode(wifi_mode_t m)
|
|
|
|
{
|
2016-11-27 23:50:21 +01:00
|
|
|
wifi_mode_t cm = getMode();
|
|
|
|
if(cm == WIFI_MODE_MAX){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(cm == m) {
|
2016-10-06 13:21:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
esp_err_t err;
|
|
|
|
err = esp_wifi_set_mode(m);
|
|
|
|
if(err){
|
|
|
|
log_e("Could not set mode! %u", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(m){
|
|
|
|
return espWiFiStart();
|
|
|
|
}
|
|
|
|
return espWiFiStop();
|
2016-10-06 13:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get WiFi mode
|
|
|
|
* @return WiFiMode
|
|
|
|
*/
|
|
|
|
wifi_mode_t WiFiGenericClass::getMode()
|
|
|
|
{
|
2017-02-23 00:11:57 +01:00
|
|
|
if(!wifiLowLevelInit()){
|
2016-11-27 23:50:21 +01:00
|
|
|
return WIFI_MODE_MAX;
|
|
|
|
}
|
2017-02-23 00:11:57 +01:00
|
|
|
uint8_t mode;
|
2016-10-06 13:21:30 +02:00
|
|
|
esp_wifi_get_mode((wifi_mode_t*)&mode);
|
|
|
|
return (wifi_mode_t)mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* control STA mode
|
|
|
|
* @param enable bool
|
|
|
|
* @return ok
|
|
|
|
*/
|
|
|
|
bool WiFiGenericClass::enableSTA(bool enable)
|
|
|
|
{
|
|
|
|
|
|
|
|
wifi_mode_t currentMode = getMode();
|
|
|
|
bool isEnabled = ((currentMode & WIFI_MODE_STA) != 0);
|
|
|
|
|
|
|
|
if(isEnabled != enable) {
|
|
|
|
if(enable) {
|
|
|
|
return mode((wifi_mode_t)(currentMode | WIFI_MODE_STA));
|
|
|
|
} else {
|
|
|
|
return mode((wifi_mode_t)(currentMode & (~WIFI_MODE_STA)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* control AP mode
|
|
|
|
* @param enable bool
|
|
|
|
* @return ok
|
|
|
|
*/
|
|
|
|
bool WiFiGenericClass::enableAP(bool enable)
|
|
|
|
{
|
|
|
|
|
|
|
|
wifi_mode_t currentMode = getMode();
|
|
|
|
bool isEnabled = ((currentMode & WIFI_MODE_AP) != 0);
|
|
|
|
|
|
|
|
if(isEnabled != enable) {
|
|
|
|
if(enable) {
|
|
|
|
return mode((wifi_mode_t)(currentMode | WIFI_MODE_AP));
|
|
|
|
} else {
|
|
|
|
return mode((wifi_mode_t)(currentMode & (~WIFI_MODE_AP)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
// ------------------------------------------------ Generic Network function ---------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2016-12-30 01:44:45 +01:00
|
|
|
static bool _dns_busy = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DNS callback
|
|
|
|
* @param name
|
|
|
|
* @param ipaddr
|
|
|
|
* @param callback_arg
|
|
|
|
*/
|
|
|
|
static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
|
|
|
|
{
|
|
|
|
if(ipaddr) {
|
|
|
|
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->u_addr.ip4.addr;
|
|
|
|
}
|
|
|
|
_dns_busy = false;
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the given hostname to an IP address.
|
|
|
|
* @param aHostname Name to be resolved
|
|
|
|
* @param aResult IPAddress structure to store the returned IP address
|
|
|
|
* @return 1 if aIPAddrString was successfully converted to an IP address,
|
|
|
|
* else error code
|
|
|
|
*/
|
|
|
|
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
|
|
|
|
{
|
|
|
|
ip_addr_t addr;
|
|
|
|
aResult = static_cast<uint32_t>(0);
|
2016-12-30 01:44:45 +01:00
|
|
|
|
|
|
|
_dns_busy = true;
|
2016-10-06 13:21:30 +02:00
|
|
|
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
|
2016-12-30 01:44:45 +01:00
|
|
|
if(err == ERR_OK && addr.u_addr.ip4.addr) {
|
2016-10-06 13:21:30 +02:00
|
|
|
aResult = addr.u_addr.ip4.addr;
|
2016-12-30 01:44:45 +01:00
|
|
|
_dns_busy = false;
|
|
|
|
} else if(err == ERR_INPROGRESS) {
|
|
|
|
while(_dns_busy){
|
|
|
|
delay(1);
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
} else {
|
2016-12-30 01:44:45 +01:00
|
|
|
_dns_busy = false;
|
2016-10-06 13:21:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|