1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-25 20:02:37 +03:00

Implement most WiFi functions

Need to update to SDK 0.9.3 because DHCP client functions are missing
This commit is contained in:
Ivan Grokhotkov 2014-12-05 22:39:40 +03:00
parent 31a8539763
commit b4f21fc6b0
5 changed files with 401 additions and 321 deletions

View File

@ -0,0 +1,334 @@
/*
WiFi.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
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
*/
#include "ESP8266WiFi.h"
extern "C" {
#include "c_types.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "lwip/opt.h"
#include "lwip/err.h"
#include "lwip/dns.h"
}
extern "C" void esp_schedule();
extern "C" void esp_yield();
ESP8266WiFiClass::ESP8266WiFiClass()
{
}
void ESP8266WiFiClass::mode(WiFiMode m)
{
ETS_UART_INTR_DISABLE();
wifi_set_opmode(m);
ETS_UART_INTR_ENABLE();
}
int ESP8266WiFiClass::begin(const char* ssid)
{
return begin(ssid, 0);
}
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase)
{
if (wifi_get_opmode() == WIFI_AP)
{
// turn on AP+STA mode
mode(WIFI_AP_STA);
}
struct station_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
if (passphrase)
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
else
*conf.password = 0;
ETS_UART_INTR_DISABLE();
wifi_station_set_config(&conf);
wifi_station_connect();
ETS_UART_INTR_ENABLE();
wifi_station_dhcp_start();
return 0;
}
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
{
struct ip_info info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
wifi_station_dhcpc_stop();
wifi_set_ip_info(STATION_IF, &info);
}
int ESP8266WiFiClass::disconnect()
{
struct station_config conf;
*conf.ssid = 0;
*conf.password = 0;
ETS_UART_INTR_DISABLE();
wifi_station_set_config(&conf);
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();
return 0;
}
void ESP8266WiFiClass::softAP(const char* ssid)
{
softAP(ssid, 0);
}
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase)
{
if (wifi_get_opmode() == WIFI_STA)
{
// turn on AP+STA mode
mode(WIFI_AP_STA);
}
struct softap_config conf;
wifi_softap_get_config(&conf);
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
conf.channel = 1;
if (!passphrase || strlen(passphrase) == 0)
{
conf.authmode = AUTH_OPEN;
*conf.password = 0;
}
else
{
conf.authmode = AUTH_WPA2_PSK;
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
}
ETS_UART_INTR_DISABLE();
wifi_softap_set_config(&conf);
ETS_UART_INTR_ENABLE();
}
uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac)
{
wifi_get_macaddr(STATION_IF, mac);
return mac;
}
uint8_t* softAPmacAddress(uint8_t* mac)
{
wifi_get_macaddr(SOFTAP_IF, mac);
return mac;
}
IPAddress ESP8266WiFiClass::localIP()
{
struct ip_info ip;
wifi_get_ip_info(STATION_IF, &ip);
return IPAddress(ip.ip.addr);
}
IPAddress softAPIP()
{
struct ip_info ip;
wifi_get_ip_info(SOFTAP_IF, &ip);
return IPAddress(ip.ip.addr);
}
IPAddress ESP8266WiFiClass::subnetMask()
{
struct ip_info ip;
wifi_get_ip_info(STATION_IF, &ip);
return IPAddress(ip.netmask.addr);
}
IPAddress ESP8266WiFiClass::gatewayIP()
{
struct ip_info ip;
wifi_get_ip_info(STATION_IF, &ip);
return IPAddress(ip.gw.addr);
}
char* ESP8266WiFiClass::SSID()
{
static struct station_config conf;
wifi_station_get_config(&conf);
return reinterpret_cast<char*>(conf.ssid);
}
// uint8_t* ESP8266WiFiClass::BSSID(uint8_t* bssid)
// {
// uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
// memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
// return bssid;
// }
// int32_t ESP8266WiFiClass::RSSI()
// {
// return WiFiDrv::getCurrentRSSI();
// }
// uint8_t ESP8266WiFiClass::encryptionType()
// {
// return WiFiDrv::getCurrentEncryptionType();
// }
void ESP8266WiFiClass::_scanDone(void* result, int status)
{
if (status != OK)
{
ESP8266WiFiClass::_scanCount = 0;
ESP8266WiFiClass::_scanResult = 0;
}
else
{
ESP8266WiFiClass::_scanResult = result;
int i = 0;
for (bss_info* it = reinterpret_cast<bss_info*>(result); it; it = STAILQ_NEXT(it, next), ++i);
ESP8266WiFiClass::_scanCount = i;
}
esp_schedule();
}
int8_t ESP8266WiFiClass::scanNetworks()
{
if (wifi_get_opmode() == WIFI_AP)
{
mode(WIFI_AP_STA);
}
int status = wifi_station_get_connect_status();
if (status != STATION_GOT_IP && status != STATION_IDLE)
{
disconnect();
}
struct scan_config config;
config.ssid = 0;
config.bssid = 0;
config.channel = 0;
wifi_station_scan(&config, reinterpret_cast<scan_done_cb_t>(&ESP8266WiFiClass::_scanDone));
esp_yield();
return ESP8266WiFiClass::_scanCount;
}
void * ESP8266WiFiClass::_getScanInfoByIndex(int i)
{
if (!ESP8266WiFiClass::_scanResult || i > ESP8266WiFiClass::_scanCount)
return 0;
struct bss_info* it = reinterpret_cast<struct bss_info*>(ESP8266WiFiClass::_scanResult);
for (; i; it = STAILQ_NEXT(it, next), --i);
if (!it)
return 0;
return it;
}
char* ESP8266WiFiClass::SSID(uint8_t i)
{
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
if (!it)
return 0;
return reinterpret_cast<char*>(it->ssid);
}
int32_t ESP8266WiFiClass::RSSI(uint8_t i)
{
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
if (!it)
return 0;
return it->rssi;
}
uint8_t ESP8266WiFiClass::encryptionType(uint8_t i)
{
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
if (!it)
return -1;
int authmode = it->authmode;
if (authmode == AUTH_OPEN)
return ENC_TYPE_NONE;
if (authmode == AUTH_WEP)
return ENC_TYPE_WEP;
if (authmode == AUTH_WPA_PSK || authmode == AUTH_WPA_WPA2_PSK) // fixme: is this correct?
return ENC_TYPE_TKIP;
if (authmode == AUTH_WPA2_PSK)
return ENC_TYPE_CCMP;
return -1;
}
uint8_t ESP8266WiFiClass::status()
{
int status = wifi_station_get_connect_status();
if (status == STATION_GOT_IP)
return WL_CONNECTED;
else if (status == STATION_NO_AP_FOUND)
return WL_NO_SSID_AVAIL;
else if (status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD)
return WL_CONNECT_FAILED;
else if (status == STATION_IDLE)
return WL_IDLE_STATUS;
else
return WL_DISCONNECTED;
}
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg)
{
if (ipaddr)
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
esp_schedule(); // resume the hostByName function
}
int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
{
ip_addr_t addr;
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
if (err == ERR_OK)
{
aResult = addr.addr;
}
else if (err == ERR_INPROGRESS)
{
esp_yield();
// will return here when dns_found_callback fires
}
else // probably an invalid hostname
{
aResult = static_cast<uint32_t>(0);
}
return (aResult != 0) ? 1 : 0;
}
ESP8266WiFiClass WiFi;

View File

@ -30,43 +30,21 @@ extern "C" {
#include "WiFiClient.h"
#include "WiFiServer.h"
class WiFiClass
enum WiFiMode { WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 };
class ESP8266WiFiClass
{
private:
static void init();
public:
static int16_t _state[MAX_SOCK_NUM];
static uint16_t _server_port[MAX_SOCK_NUM];
WiFiClass();
/*
* Get the first socket available
*/
static uint8_t getSocket();
/*
* Get firmware version
*/
static char* firmwareVersion();
ESP8266WiFiClass();
void mode(WiFiMode);
/* Start Wifi connection for OPEN networks
*
* param ssid: Pointer to the SSID string.
*/
int begin(char* ssid);
/* Start Wifi connection with WEP encryption.
* Configure a key into the device. The key type (WEP-40, WEP-104)
* is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
*
* param ssid: Pointer to the SSID string.
* param key_idx: The key index to set. Valid values are 0-3.
* param key: Key input buffer.
*/
int begin(char* ssid, uint8_t key_idx, const char* key);
int begin(const char* ssid);
/* Start Wifi connection with passphrase
* the most secure supported mode will be automatically selected
@ -77,49 +55,29 @@ public:
*/
int begin(const char* ssid, const char *passphrase);
/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
*/
void config(IPAddress local_ip);
/* Set up an open access point
*
* param ssid: Pointer to the SSID string.
*/
void softAP(const char* ssid);
/* Set up a WPA2-secured access point
*
* param ssid: Pointer to the SSID string.
* param ssid: Pointer to passphrase, 8 characters min.
*/
void softAP(const char* ssid, const char* passphrase);
/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
*/
void config(IPAddress local_ip, IPAddress dns_server);
/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
* param gateway : Static gateway configuration
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
* param gateway: Static gateway configuration
* param subnet: Static Subnet mask
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
/* Change DNS Ip configuration
*
* param dns_server1: ip configuration for DNS server 1
*/
void setDNS(IPAddress dns_server1);
/* Change DNS Ip configuration
*
* param dns_server1: ip configuration for DNS server 1
* param dns_server2: ip configuration for DNS server 2
*
*/
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
/*
* Disconnect from the network
@ -129,19 +87,33 @@ public:
int disconnect(void);
/*
* Get the interface MAC address.
* Get the station interface MAC address.
*
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* macAddress(uint8_t* mac);
/*
* Get the interface IP address.
* Get the softAP interface MAC address.
*
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* softAPmacAddress(uint8_t* mac);
/*
* Get the station interface IP address.
*
* return: Ip address value
*/
IPAddress localIP();
/*
* Get the softAP interface IP address.
*
* return: Ip address value
*/
IPAddress softAPIP();
/*
* Get the interface subnet mask address.
*
@ -163,29 +135,6 @@ public:
*/
char* SSID();
/*
* Return the current BSSID associated with the network.
* It is the MAC address of the Access Point
*
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* BSSID(uint8_t* bssid);
/*
* Return the current RSSI /Received Signal Strength in dBm)
* associated with the network
*
* return: signed value
*/
int32_t RSSI();
/*
* Return the Encryption Type associated with the network
*
* return: one value of wl_enc_type enum
*/
uint8_t encryptionType();
/*
* Start scan WiFi networks available
*
@ -238,8 +187,15 @@ public:
friend class WiFiClient;
friend class WiFiServer;
protected:
static void _scanDone(void* result, int status);
void * _getScanInfoByIndex(int i);
static size_t _scanCount;
static void* _scanResult;
};
extern WiFiClass WiFi;
extern ESP8266WiFiClass WiFi;
#endif

View File

@ -1,207 +0,0 @@
/*
WiFi.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
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
*/
#include "ESP8266WiFi.h"
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
}
WiFiClass::WiFiClass()
{
// Driver initialization
init();
}
void WiFiClass::init()
{
}
// int WiFiClass::begin(char* ssid)
// {
// uint8_t status = WL_IDLE_STATUS;
// uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
// if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
// {
// do
// {
// delay(WL_DELAY_START_CONNECTION);
// status = WiFiDrv::getConnectionStatus();
// }
// while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
// }else
// {
// status = WL_CONNECT_FAILED;
// }
// return status;
// }
// int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
// {
// }
int WiFiClass::begin(const char* ssid, const char *passphrase)
{
struct station_config conf;
strcpy((char*) conf.ssid, ssid);
strcpy((char*) conf.password, passphrase);
wifi_station_set_config(&conf);
return 0;
}
// void WiFiClass::config(IPAddress local_ip)
// {
// WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
// }
// void WiFiClass::config(IPAddress local_ip, IPAddress dns_server)
// {
// WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
// WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
// }
// void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
// {
// WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0);
// WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
// }
// void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
// {
// WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet);
// WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
// }
// void WiFiClass::setDNS(IPAddress dns_server1)
// {
// WiFiDrv::setDNS(1, (uint32_t)dns_server1, 0);
// }
// void WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2)
// {
// WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2);
// }
// int WiFiClass::disconnect()
// {
// return WiFiDrv::disconnect();
// }
// uint8_t* WiFiClass::macAddress(uint8_t* mac)
// {
// uint8_t* _mac = WiFiDrv::getMacAddress();
// memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
// return mac;
// }
// IPAddress WiFiClass::localIP()
// {
// IPAddress ret;
// WiFiDrv::getIpAddress(ret);
// return ret;
// }
// IPAddress WiFiClass::subnetMask()
// {
// IPAddress ret;
// WiFiDrv::getSubnetMask(ret);
// return ret;
// }
// IPAddress WiFiClass::gatewayIP()
// {
// IPAddress ret;
// WiFiDrv::getGatewayIP(ret);
// return ret;
// }
// char* WiFiClass::SSID()
// {
// return WiFiDrv::getCurrentSSID();
// }
// uint8_t* WiFiClass::BSSID(uint8_t* bssid)
// {
// uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
// memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
// return bssid;
// }
// int32_t WiFiClass::RSSI()
// {
// return WiFiDrv::getCurrentRSSI();
// }
// uint8_t WiFiClass::encryptionType()
// {
// return WiFiDrv::getCurrentEncryptionType();
// }
// int8_t WiFiClass::scanNetworks()
// {
// uint8_t attempts = 10;
// uint8_t numOfNetworks = 0;
// if (WiFiDrv::startScanNetworks() == WL_FAILURE)
// return WL_FAILURE;
// do
// {
// delay(2000);
// numOfNetworks = WiFiDrv::getScanNetworks();
// }
// while (( numOfNetworks == 0)&&(--attempts>0));
// return numOfNetworks;
// }
// char* WiFiClass::SSID(uint8_t networkItem)
// {
// return WiFiDrv::getSSIDNetoworks(networkItem);
// }
// int32_t WiFiClass::RSSI(uint8_t networkItem)
// {
// return WiFiDrv::getRSSINetoworks(networkItem);
// }
// uint8_t WiFiClass::encryptionType(uint8_t networkItem)
// {
// return WiFiDrv::getEncTypeNetowrks(networkItem);
// }
// uint8_t WiFiClass::status()
// {
// return WiFiDrv::getConnectionStatus();
// }
// int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
// {
// return WiFiDrv::getHostByName(aHostname, aResult);
// }
WiFiClass WiFi;

View File

@ -21,6 +21,7 @@ public:
, _discard_cb_arg(discard_cb_arg)
, _refcnt(0)
, _next(0)
, _send_waiting(false)
{
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
@ -54,19 +55,13 @@ public:
DEBUGV("WC:ur %d\r\n", _refcnt);
if (--_refcnt == 0 && _pcb)
{
// if (_discard_cb)
// (*_discard_cb)(_discard_cb_arg, this);
// else
// {
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_close(_pcb);
_pcb = 0;
delete this;
//tcp_abort(_pcb);
// }
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_close(_pcb);
_pcb = 0;
delete this;
}
}
@ -117,7 +112,9 @@ public:
_size_sent = will_send;
DEBUGV("WC:wr\r\n");
_send_waiting = true;
delay(5000); // max send timeout
_send_waiting = false;
DEBUGV("WC:ww\r\n");
return will_send - _size_sent;
}
@ -153,7 +150,7 @@ private:
{
DEBUGV("WC:er\r\n");
_pcb = 0;
if (_size_sent)
if (_size_sent && _send_waiting)
esp_schedule();
}
@ -167,7 +164,7 @@ private:
{
DEBUGV("WC:sent %d\r\n", len);
_size_sent -= len;
if (_size_sent == 0)
if (_size_sent == 0 && _send_waiting)
esp_schedule();
return ERR_OK;
}
@ -201,6 +198,7 @@ private:
discard_cb_t _discard_cb;
void* _discard_cb_arg;
size_t _size_sent;
bool _send_waiting;
};

View File

@ -48,14 +48,13 @@
#define WL_MAX_ATTEMPT_CONNECTION 10
typedef enum {
WL_NO_SHIELD = 255,
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL,
WL_SCAN_COMPLETED,
WL_CONNECTED,
WL_CONNECT_FAILED,
WL_CONNECTION_LOST,
WL_DISCONNECTED
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL,
WL_SCAN_COMPLETED,
WL_CONNECTED,
WL_CONNECT_FAILED,
WL_CONNECTION_LOST,
WL_DISCONNECTED
} wl_status_t;
/* Encryption modes */