IP Configuration

This commit is contained in:
Gašper Dobrovoljc
2023-03-11 15:11:03 +01:00
commit ec125f27db
662 changed files with 103738 additions and 0 deletions

233
lib/WiFiEsp/src/WiFiEsp.cpp Normal file
View File

@@ -0,0 +1,233 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include "WiFiEsp.h"
int16_t WiFiEspClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE };
uint16_t WiFiEspClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
uint8_t WiFiEspClass::espMode = 0;
WiFiEspClass::WiFiEspClass()
{
}
void WiFiEspClass::init(Stream* espSerial)
{
LOGINFO(F("Initializing ESP module"));
EspDrv::wifiDriverInit(espSerial);
}
char* WiFiEspClass::firmwareVersion()
{
return EspDrv::getFwVersion();
}
int WiFiEspClass::begin(const char* ssid, const char* passphrase)
{
espMode = 1;
if (EspDrv::wifiConnect(ssid, passphrase))
return WL_CONNECTED;
return WL_CONNECT_FAILED;
}
int WiFiEspClass::beginAP(const char* ssid, uint8_t channel, const char* pwd, uint8_t enc, bool apOnly)
{
if(apOnly)
espMode = 2;
else
espMode = 3;
if (EspDrv::wifiStartAP(ssid, pwd, channel, enc, espMode))
return WL_CONNECTED;
return WL_CONNECT_FAILED;
}
int WiFiEspClass::beginAP(const char* ssid)
{
return beginAP(ssid, 10, "", 0);
}
int WiFiEspClass::beginAP(const char* ssid, uint8_t channel)
{
return beginAP(ssid, channel, "", 0);
}
void WiFiEspClass::config(IPAddress ip)
{
EspDrv::config(ip);
}
void WiFiEspClass::configAP(IPAddress ip)
{
EspDrv::configAP(ip);
}
int WiFiEspClass::disconnect()
{
return EspDrv::disconnect();
}
uint8_t* WiFiEspClass::macAddress(uint8_t* mac)
{
// TODO we don't need _mac variable
uint8_t* _mac = EspDrv::getMacAddress();
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
return mac;
}
IPAddress WiFiEspClass::localIP()
{
IPAddress ret;
if(espMode==1)
EspDrv::getIpAddress(ret);
else
EspDrv::getIpAddressAP(ret);
return ret;
}
IPAddress WiFiEspClass::subnetMask()
{
IPAddress mask;
if(espMode==1)
EspDrv::getNetmask(mask);
return mask;
}
IPAddress WiFiEspClass::gatewayIP()
{
IPAddress gw;
if(espMode==1)
EspDrv::getGateway(gw);
return gw;
}
char* WiFiEspClass::SSID()
{
return EspDrv::getCurrentSSID();
}
uint8_t* WiFiEspClass::BSSID(uint8_t* bssid)
{
// TODO we don't need _bssid
uint8_t* _bssid = EspDrv::getCurrentBSSID();
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
return bssid;
}
int32_t WiFiEspClass::RSSI()
{
return EspDrv::getCurrentRSSI();
}
int8_t WiFiEspClass::scanNetworks()
{
return EspDrv::getScanNetworks();
}
char* WiFiEspClass::SSID(uint8_t networkItem)
{
return EspDrv::getSSIDNetoworks(networkItem);
}
int32_t WiFiEspClass::RSSI(uint8_t networkItem)
{
return EspDrv::getRSSINetoworks(networkItem);
}
uint8_t WiFiEspClass::encryptionType(uint8_t networkItem)
{
return EspDrv::getEncTypeNetowrks(networkItem);
}
uint8_t WiFiEspClass::status()
{
return EspDrv::getConnectionStatus();
}
////////////////////////////////////////////////////////////////////////////
// Non standard methods
////////////////////////////////////////////////////////////////////////////
void WiFiEspClass::reset(void)
{
EspDrv::reset();
}
/*
void ESP8266::hardReset(void)
{
connected = false;
strcpy(ip, "");
digitalWrite(ESP8266_RST, LOW);
delay(ESP8266_HARD_RESET_DURATION);
digitalWrite(ESP8266_RST, HIGH);
delay(ESP8266_HARD_RESET_DURATION);
}
*/
bool WiFiEspClass::ping(const char *host)
{
return EspDrv::ping(host);
}
uint8_t WiFiEspClass::getFreeSocket()
{
// ESP Module assigns socket numbers in ascending order, so we will assign them in descending order
for (int i = MAX_SOCK_NUM - 1; i >= 0; i--)
{
if (_state[i] == NA_STATE)
{
return i;
}
}
return SOCK_NOT_AVAIL;
}
void WiFiEspClass::allocateSocket(uint8_t sock)
{
_state[sock] = sock;
}
void WiFiEspClass::releaseSocket(uint8_t sock)
{
_state[sock] = NA_STATE;
}
WiFiEspClass WiFi;

274
lib/WiFiEsp/src/WiFiEsp.h Normal file
View File

@@ -0,0 +1,274 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef WiFiEsp_h
#define WiFiEsp_h
#include <Arduino.h>
#include <Stream.h>
#include <IPAddress.h>
#include <inttypes.h>
#include "WiFiEspClient.h"
#include "WiFiEspServer.h"
#include "utility/EspDrv.h"
#include "utility/RingBuffer.h"
#include "utility/debug.h"
class WiFiEspClass
{
public:
static int16_t _state[MAX_SOCK_NUM];
static uint16_t _server_port[MAX_SOCK_NUM];
WiFiEspClass();
/**
* Initialize the ESP module.
*
* param espSerial: the serial interface (HW or SW) used to communicate with the ESP module
*/
static void init(Stream* espSerial);
/**
* Get firmware version
*/
static char* firmwareVersion();
// NOT IMPLEMENTED
//int begin(char* ssid);
// NOT IMPLEMENTED
//int begin(char* ssid, uint8_t key_idx, const char* key);
/**
* Start Wifi connection with passphrase
* the most secure supported mode will be automatically selected
*
* param ssid: Pointer to the SSID string.
* param passphrase: Passphrase. Valid characters in a passphrase
* must be between ASCII 32-126 (decimal).
*/
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);
// NOT IMPLEMENTED
//void config(IPAddress local_ip, IPAddress dns_server);
// NOT IMPLEMENTED
//void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
// NOT IMPLEMENTED
//void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
// NOT IMPLEMENTED
//void setDNS(IPAddress dns_server1);
// NOT IMPLEMENTED
//void setDNS(IPAddress dns_server1, IPAddress dns_server2);
/**
* Disconnect from the network
*
* return: one value of wl_status_t enum
*/
int disconnect(void);
/**
* Get the 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.
*
* return: Ip address value
*/
IPAddress localIP();
/**
* Get the interface subnet mask address.
*
* return: subnet mask address value
*/
IPAddress subnetMask();
/**
* Get the gateway ip address.
*
* return: gateway ip address value
*/
IPAddress gatewayIP();
/**
* Return the current SSID associated with the network
*
* return: ssid string
*/
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 Connection status.
*
* return: one of the value defined in wl_status_t
* see https://www.arduino.cc/en/Reference/WiFiStatus
*/
uint8_t status();
/*
* Return the Encryption Type associated with the network
*
* return: one value of wl_enc_type enum
*/
//uint8_t encryptionType();
/*
* Start scan WiFi networks available
*
* return: Number of discovered networks
*/
int8_t scanNetworks();
/*
* Return the SSID discovered during the network scan.
*
* param networkItem: specify from which network item want to get the information
*
* return: ssid string of the specified item on the networks scanned list
*/
char* SSID(uint8_t networkItem);
/*
* Return the encryption type of the networks discovered during the scanNetworks
*
* param networkItem: specify from which network item want to get the information
*
* return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
*/
uint8_t encryptionType(uint8_t networkItem);
/*
* Return the RSSI of the networks discovered during the scanNetworks
*
* param networkItem: specify from which network item want to get the information
*
* return: signed value of RSSI of the specified item on the networks scanned list
*/
int32_t RSSI(uint8_t networkItem);
// NOT IMPLEMENTED
//int hostByName(const char* aHostname, IPAddress& aResult);
////////////////////////////////////////////////////////////////////////////
// Non standard methods
////////////////////////////////////////////////////////////////////////////
/**
* Start the ESP access point.
*
* param ssid: Pointer to the SSID string.
* param channel: WiFi channel (1-14)
* param pwd: Passphrase. Valid characters in a passphrase
* must be between ASCII 32-126 (decimal).
* param enc: encryption type (enum wl_enc_type)
* param apOnly: Set to false if you want to run AP and Station modes simultaneously
*/
int beginAP(const char* ssid, uint8_t channel, const char* pwd, uint8_t enc, bool apOnly=true);
/*
* Start the ESP access point with open security.
*/
int beginAP(const char* ssid);
int beginAP(const char* ssid, uint8_t channel);
/**
* Change IP address of the AP
*
* param ip: Static ip configuration
*/
void configAP(IPAddress ip);
/**
* Restart the ESP module.
*/
void reset();
/**
* Ping a host.
*/
bool ping(const char *host);
friend class WiFiEspClient;
friend class WiFiEspServer;
friend class WiFiEspUDP;
private:
static uint8_t getFreeSocket();
static void allocateSocket(uint8_t sock);
static void releaseSocket(uint8_t sock);
static uint8_t espMode;
};
extern WiFiEspClass WiFi;
#endif

View File

@@ -0,0 +1,290 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include <inttypes.h>
#include "WiFiEsp.h"
#include "WiFiEspClient.h"
#include "WiFiEspServer.h"
#include "utility/EspDrv.h"
#include "utility/debug.h"
WiFiEspClient::WiFiEspClient() : _sock(255)
{
}
WiFiEspClient::WiFiEspClient(uint8_t sock) : _sock(sock)
{
}
////////////////////////////////////////////////////////////////////////////////
// Overrided Print methods
////////////////////////////////////////////////////////////////////////////////
// the standard print method will call write for each character in the buffer
// this is very slow on ESP
size_t WiFiEspClient::print(const __FlashStringHelper *ifsh)
{
printFSH(ifsh, false);
}
// if we do override this, the standard println will call the print
// method twice
size_t WiFiEspClient::println(const __FlashStringHelper *ifsh)
{
printFSH(ifsh, true);
}
////////////////////////////////////////////////////////////////////////////////
// Implementation of Client virtual methods
////////////////////////////////////////////////////////////////////////////////
int WiFiEspClient::connectSSL(const char* host, uint16_t port)
{
return connect(host, port, SSL_MODE);
}
int WiFiEspClient::connectSSL(IPAddress ip, uint16_t port)
{
char s[16];
sprintf_P(s, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
return connect(s, port, SSL_MODE);
}
int WiFiEspClient::connect(const char* host, uint16_t port)
{
return connect(host, port, TCP_MODE);
}
int WiFiEspClient::connect(IPAddress ip, uint16_t port)
{
char s[16];
sprintf_P(s, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
return connect(s, port, TCP_MODE);
}
/* Private method */
int WiFiEspClient::connect(const char* host, uint16_t port, uint8_t protMode)
{
LOGINFO1(F("Connecting to"), host);
_sock = WiFiEspClass::getFreeSocket();
if (_sock != NO_SOCKET_AVAIL)
{
if (!EspDrv::startClient(host, port, _sock, protMode))
return 0;
WiFiEspClass::allocateSocket(_sock);
}
else
{
LOGERROR(F("No socket available"));
return 0;
}
return 1;
}
size_t WiFiEspClient::write(uint8_t b)
{
return write(&b, 1);
}
size_t WiFiEspClient::write(const uint8_t *buf, size_t size)
{
if (_sock >= MAX_SOCK_NUM or size==0)
{
setWriteError();
return 0;
}
bool r = EspDrv::sendData(_sock, buf, size);
if (!r)
{
setWriteError();
LOGERROR1(F("Failed to write to socket"), _sock);
delay(4000);
stop();
return 0;
}
return size;
}
int WiFiEspClient::available()
{
if (_sock != 255)
{
int bytes = EspDrv::availData(_sock);
if (bytes>0)
{
return bytes;
}
}
return 0;
}
int WiFiEspClient::read()
{
uint8_t b;
if (!available())
return -1;
bool connClose = false;
EspDrv::getData(_sock, &b, false, &connClose);
if (connClose)
{
WiFiEspClass::releaseSocket(_sock);
_sock = 255;
}
return b;
}
int WiFiEspClient::read(uint8_t* buf, size_t size)
{
if (!available())
return -1;
return EspDrv::getDataBuf(_sock, buf, size);
}
int WiFiEspClient::peek()
{
uint8_t b;
if (!available())
return -1;
bool connClose = false;
EspDrv::getData(_sock, &b, true, &connClose);
if (connClose)
{
WiFiEspClass::releaseSocket(_sock);
_sock = 255;
}
return b;
}
void WiFiEspClient::flush()
{
while (available())
read();
}
void WiFiEspClient::stop()
{
if (_sock == 255)
return;
LOGINFO1(F("Disconnecting "), _sock);
EspDrv::stopClient(_sock);
WiFiEspClass::releaseSocket(_sock);
_sock = 255;
}
uint8_t WiFiEspClient::connected()
{
return (status() == ESTABLISHED);
}
WiFiEspClient::operator bool()
{
return _sock != 255;
}
////////////////////////////////////////////////////////////////////////////////
// Additional WiFi standard methods
////////////////////////////////////////////////////////////////////////////////
uint8_t WiFiEspClient::status()
{
if (_sock == 255)
{
return CLOSED;
}
if (EspDrv::availData(_sock))
{
return ESTABLISHED;
}
if (EspDrv::getClientState(_sock))
{
return ESTABLISHED;
}
WiFiEspClass::releaseSocket(_sock);
_sock = 255;
return CLOSED;
}
IPAddress WiFiEspClient::remoteIP()
{
IPAddress ret;
EspDrv::getRemoteIpAddress(ret);
return ret;
}
////////////////////////////////////////////////////////////////////////////////
// Private Methods
////////////////////////////////////////////////////////////////////////////////
size_t WiFiEspClient::printFSH(const __FlashStringHelper *ifsh, bool appendCrLf)
{
size_t size = strlen_P((char*)ifsh);
if (_sock >= MAX_SOCK_NUM or size==0)
{
setWriteError();
return 0;
}
bool r = EspDrv::sendData(_sock, ifsh, size, appendCrLf);
if (!r)
{
setWriteError();
LOGERROR1(F("Failed to write to socket"), _sock);
delay(4000);
stop();
return 0;
}
return size;
}

View File

@@ -0,0 +1,144 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef WiFiEspClient_h
#define WiFiEspClient_h
#include "Arduino.h"
#include "Print.h"
#include "Client.h"
#include "IPAddress.h"
class WiFiEspClient : public Client
{
public:
WiFiEspClient();
WiFiEspClient(uint8_t sock);
// override Print.print method
size_t print(const __FlashStringHelper *ifsh);
size_t println(const __FlashStringHelper *ifsh);
/*
* Connect to the specified IP address and port. The return value indicates success or failure.
* Returns true if the connection succeeds, false if not.
*/
virtual int connect(IPAddress ip, uint16_t port);
/*
* Connect to the specified host and port. The return value indicates success or failure.
* Returns true if the connection succeeds, false if not.
*/
virtual int connect(const char *host, uint16_t port);
/*
* Connect to the specified IP address and port using SSL. The return value indicates success or failure.
* Returns true if the connection succeeds, false if not.
*/
int connectSSL(IPAddress ip, uint16_t port);
/*
* Connect to the specified host and port using SSL. The return value indicates success or failure.
* Returns true if the connection succeeds, false if not.
*/
int connectSSL(const char* host, uint16_t port);
/*
* Write a character to the server the client is connected to.
* Returns the number of characters written.
*/
virtual size_t write(uint8_t);
/*
* Write data to the server the client is connected to.
* Returns the number of characters written.
*/
virtual size_t write(const uint8_t *buf, size_t size);
virtual int available();
/*
* Read the next byte received from the server the client is connected to (after the last call to read()).
* Returns the next byte (or character), or -1 if none is available.
*/
virtual int read();
virtual int read(uint8_t *buf, size_t size);
/*
* Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer.
*/
virtual int peek();
/*
* Discard any bytes that have been written to the client but not yet read.
*/
virtual void flush();
/*
* Disconnect from the server.
*/
virtual void stop();
/*
* Whether or not the client is connected.
* Note that a client is considered connected if the connection has been closed but there is still unread data.
* Returns true if the client is connected, false if not.
*/
virtual uint8_t connected();
uint8_t status();
virtual operator bool();
// needed to correctly handle overriding
// see http://stackoverflow.com/questions/888235/overriding-a-bases-overloaded-function-in-c
using Print::write;
using Print::print;
using Print::println;
/*
* Returns the remote IP address.
*/
IPAddress remoteIP();
friend class WiFiEspServer;
private:
uint8_t _sock; // connection id
int connect(const char* host, uint16_t port, uint8_t protMode);
size_t printFSH(const __FlashStringHelper *ifsh, bool appendCrLf);
};
#endif

View File

@@ -0,0 +1,99 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include "WiFiEspServer.h"
#include "utility/EspDrv.h"
#include "utility/debug.h"
WiFiEspServer::WiFiEspServer(uint16_t port)
{
_port = port;
}
void WiFiEspServer::begin()
{
LOGDEBUG(F("Starting server"));
/* The ESP Module only allows socket 1 to be used for the server */
#if 0
_sock = WiFiEspClass::getFreeSocket();
if (_sock == SOCK_NOT_AVAIL)
{
LOGERROR(F("No socket available for server"));
return;
}
#else
_sock = 1; // If this is already in use, the startServer attempt will fail
#endif
WiFiEspClass::allocateSocket(_sock);
_started = EspDrv::startServer(_port, _sock);
if (_started)
{
LOGINFO1(F("Server started on port"), _port);
}
else
{
LOGERROR(F("Server failed to start"));
}
}
WiFiEspClient WiFiEspServer::available(byte* status)
{
// TODO the original method seems to handle automatic server restart
int bytes = EspDrv::availData(0);
if (bytes>0)
{
LOGINFO1(F("New client"), EspDrv::_connId);
WiFiEspClass::allocateSocket(EspDrv::_connId);
WiFiEspClient client(EspDrv::_connId);
return client;
}
return WiFiEspClient(255);
}
uint8_t WiFiEspServer::status()
{
return EspDrv::getServerState(0);
}
size_t WiFiEspServer::write(uint8_t b)
{
return write(&b, 1);
}
size_t WiFiEspServer::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{
if (WiFiEspClass::_state[sock] != 0)
{
WiFiEspClient client(sock);
n += client.write(buffer, size);
}
}
return n;
}

View File

@@ -0,0 +1,63 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef WiFiEspServer_h
#define WiFiEspServer_h
#include <Server.h>
#include "WiFiEsp.h"
class WiFiEspClient;
class WiFiEspServer : public Server
{
public:
WiFiEspServer(uint16_t port);
/*
* Gets a client that is connected to the server and has data available for reading.
* The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().
* Returns a Client object; if no Client has data available for reading, this object will evaluate to false in an if-statement.
*/
WiFiEspClient available(uint8_t* status = NULL);
/*
* Start the TCP server
*/
void begin();
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();
using Print::write;
private:
uint16_t _port;
uint8_t _sock;
bool _started;
};
#endif

View File

@@ -0,0 +1,193 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include "WiFiEsp.h"
#include "WiFiEspUdp.h"
#include "utility/EspDrv.h"
#include "utility/debug.h"
/* Constructor */
WiFiEspUDP::WiFiEspUDP() : _sock(NO_SOCKET_AVAIL) {}
/* Start WiFiUDP socket, listening at local port PORT */
uint8_t WiFiEspUDP::begin(uint16_t port)
{
uint8_t sock = WiFiEspClass::getFreeSocket();
if (sock != NO_SOCKET_AVAIL)
{
EspDrv::startClient("0", port, sock, UDP_MODE);
WiFiEspClass::allocateSocket(sock); // allocating the socket for the listener
WiFiEspClass::_server_port[sock] = port;
_sock = sock;
_port = port;
return 1;
}
return 0;
}
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiEspUDP::available()
{
if (_sock != NO_SOCKET_AVAIL)
{
int bytes = EspDrv::availData(_sock);
if (bytes>0)
{
return bytes;
}
}
return 0;
}
/* Release any resources being used by this WiFiUDP instance */
void WiFiEspUDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;
// Discard data that might be in the incoming buffer
flush();
// Stop the listener and return the socket to the pool
EspDrv::stopClient(_sock);
WiFiEspClass::_state[_sock] = NA_STATE;
WiFiEspClass::_server_port[_sock] = 0;
_sock = NO_SOCKET_AVAIL;
}
int WiFiEspUDP::beginPacket(const char *host, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = WiFiEspClass::getFreeSocket();
if (_sock != NO_SOCKET_AVAIL)
{
//EspDrv::startClient(host, port, _sock, UDP_MODE);
_remotePort = port;
strcpy(_remoteHost, host);
WiFiEspClass::allocateSocket(_sock);
return 1;
}
return 0;
}
int WiFiEspUDP::beginPacket(IPAddress ip, uint16_t port)
{
char s[18];
sprintf_P(s, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
return beginPacket(s, port);
}
int WiFiEspUDP::endPacket()
{
return 1; //ServerDrv::sendUdpData(_sock);
}
size_t WiFiEspUDP::write(uint8_t byte)
{
return write(&byte, 1);
}
size_t WiFiEspUDP::write(const uint8_t *buffer, size_t size)
{
bool r = EspDrv::sendDataUdp(_sock, _remoteHost, _remotePort, buffer, size);
if (!r)
{
return 0;
}
return size;
}
int WiFiEspUDP::parsePacket()
{
return available();
}
int WiFiEspUDP::read()
{
uint8_t b;
if (!available())
return -1;
bool connClose = false;
// Read the data and handle the timeout condition
if (! EspDrv::getData(_sock, &b, false, &connClose))
return -1; // Timeout occured
return b;
}
int WiFiEspUDP::read(uint8_t* buf, size_t size)
{
if (!available())
return -1;
return EspDrv::getDataBuf(_sock, buf, size);
}
int WiFiEspUDP::peek()
{
uint8_t b;
if (!available())
return -1;
return b;
}
void WiFiEspUDP::flush()
{
// Discard all input data
int count = available();
while (count-- > 0)
read();
}
IPAddress WiFiEspUDP::remoteIP()
{
IPAddress ret;
EspDrv::getRemoteIpAddress(ret);
return ret;
}
uint16_t WiFiEspUDP::remotePort()
{
return EspDrv::getRemotePort();
}
////////////////////////////////////////////////////////////////////////////////
// Private Methods
////////////////////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,97 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef WiFiEspUdp_h
#define WiFiEspUdp_h
#include <Udp.h>
#define UDP_TX_PACKET_MAX_SIZE 24
class WiFiEspUDP : public UDP {
private:
uint8_t _sock; // socket ID for Wiz5100
uint16_t _port; // local port to listen on
uint16_t _remotePort;
char _remoteHost[30];
public:
WiFiEspUDP(); // Constructor
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
virtual void stop(); // Finish with the UDP socket
// Sending UDP packets
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port);
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port);
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket();
// Write a single byte into the packet
virtual size_t write(uint8_t);
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket();
// Number of bytes remaining in the current packet
virtual int available();
// Read a single byte from the current packet
virtual int read();
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len);
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
// Return the next byte from the current packet without moving on to the next byte
virtual int peek();
virtual void flush(); // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP();
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort();
friend class WiFiEspServer;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,340 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef EspDrv_h
#define EspDrv_h
#include "Stream.h"
#include "IPAddress.h"
#include "RingBuffer.h"
// Maximum size of a SSID
#define WL_SSID_MAX_LENGTH 32
// Size of a MAC-address or BSSID
#define WL_MAC_ADDR_LENGTH 6
// Size of a MAC-address or BSSID
#define WL_IPV4_LENGTH 4
// Maximum size of a SSID list
#define WL_NETWORKS_LIST_MAXNUM 10
// Maxmium number of socket
#define MAX_SOCK_NUM 4
// Socket not available constant
#define SOCK_NOT_AVAIL 255
// Default state value for Wifi state field
#define NA_STATE -1
#define WL_FW_VER_LENGTH 6
#define NO_SOCKET_AVAIL 255
// maximum size of AT command
#define CMD_BUFFER_SIZE 200
typedef enum eProtMode {TCP_MODE, UDP_MODE, SSL_MODE} tProtMode;
typedef enum {
WL_FAILURE = -1,
WL_SUCCESS = 1,
} wl_error_code_t;
/* Authentication modes */
enum wl_auth_mode {
AUTH_MODE_INVALID,
AUTH_MODE_AUTO,
AUTH_MODE_OPEN_SYSTEM,
AUTH_MODE_SHARED_KEY,
AUTH_MODE_WPA,
AUTH_MODE_WPA2,
AUTH_MODE_WPA_PSK,
AUTH_MODE_WPA2_PSK
};
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_status_t;
/* Encryption modes */
enum wl_enc_type {
ENC_TYPE_NONE = 0,
ENC_TYPE_WEP = 1,
ENC_TYPE_WPA_PSK = 2,
ENC_TYPE_WPA2_PSK = 3,
ENC_TYPE_WPA_WPA2_PSK = 4
};
enum wl_tcp_state {
CLOSED = 0,
LISTEN = 1,
SYN_SENT = 2,
SYN_RCVD = 3,
ESTABLISHED = 4,
FIN_WAIT_1 = 5,
FIN_WAIT_2 = 6,
CLOSE_WAIT = 7,
CLOSING = 8,
LAST_ACK = 9,
TIME_WAIT = 10
};
class EspDrv
{
public:
static void wifiDriverInit(Stream *espSerial);
/* Start Wifi connection with passphrase
*
* param ssid: Pointer to the SSID string.
* param passphrase: Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
*/
static bool wifiConnect(const char* ssid, const char* passphrase);
/*
* Start the Access Point
*/
static bool wifiStartAP(const char* ssid, const char* pwd, uint8_t channel, uint8_t enc, uint8_t espMode);
/*
* Set ip configuration disabling dhcp client
*/
static void config(IPAddress local_ip);
/*
* Set ip configuration disabling dhcp client
*/
static void configAP(IPAddress local_ip);
/*
* Disconnect from the network
*
* return: WL_SUCCESS or WL_FAILURE
*/
static int8_t disconnect();
/*
*
*
* return: one value of wl_status_t enum
*/
static uint8_t getConnectionStatus();
/*
* Get the interface MAC address.
*
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
static uint8_t* getMacAddress();
/*
* Get the interface IP address.
*
* return: copy the ip address value in IPAddress object
*/
static void getIpAddress(IPAddress& ip);
static void getIpAddressAP(IPAddress& ip);
/*
* Get the interface IP netmask.
* This can be used to retrieve settings configured through DHCP.
*
* return: true if successful
*/
static bool getNetmask(IPAddress& mask);
/*
* Get the interface IP gateway.
* This can be used to retrieve settings configured through DHCP.
*
* return: true if successful
*/
static bool getGateway(IPAddress& mask);
/*
* Return the current SSID associated with the network
*
* return: ssid string
*/
static char* getCurrentSSID();
/*
* 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
*/
static uint8_t* getCurrentBSSID();
/*
* Return the current RSSI /Received Signal Strength in dBm)
* associated with the network
*
* return: signed value
*/
static int32_t getCurrentRSSI();
/*
* Get the networks available
*
* return: Number of discovered networks
*/
static uint8_t getScanNetworks();
/*
* Return the SSID discovered during the network scan.
*
* param networkItem: specify from which network item want to get the information
*
* return: ssid string of the specified item on the networks scanned list
*/
static char* getSSIDNetoworks(uint8_t networkItem);
/*
* Return the RSSI of the networks discovered during the scanNetworks
*
* param networkItem: specify from which network item want to get the information
*
* return: signed value of RSSI of the specified item on the networks scanned list
*/
static int32_t getRSSINetoworks(uint8_t networkItem);
/*
* Return the encryption type of the networks discovered during the scanNetworks
*
* param networkItem: specify from which network item want to get the information
*
* return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
*/
static uint8_t getEncTypeNetowrks(uint8_t networkItem);
/*
* Get the firmware version
*/
static char* getFwVersion();
////////////////////////////////////////////////////////////////////////////
// Client/Server methods
////////////////////////////////////////////////////////////////////////////
static bool startServer(uint16_t port, uint8_t sock);
static bool startClient(const char* host, uint16_t port, uint8_t sock, uint8_t protMode);
static void stopClient(uint8_t sock);
static uint8_t getServerState(uint8_t sock);
static uint8_t getClientState(uint8_t sock);
static bool getData(uint8_t connId, uint8_t *data, bool peek, bool* connClose);
static int getDataBuf(uint8_t connId, uint8_t *buf, uint16_t bufSize);
static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len);
static bool sendData(uint8_t sock, const __FlashStringHelper *data, uint16_t len, bool appendCrLf=false);
static bool sendDataUdp(uint8_t sock, const char* host, uint16_t port, const uint8_t *data, uint16_t len);
static uint16_t availData(uint8_t connId);
static bool ping(const char *host);
static void reset();
static void getRemoteIpAddress(IPAddress& ip);
static uint16_t getRemotePort();
////////////////////////////////////////////////////////////////////////////////
private:
static Stream *espSerial;
static long _bufPos;
static uint8_t _connId;
static uint16_t _remotePort;
static uint8_t _remoteIp[WL_IPV4_LENGTH];
// firmware version string
static char fwVersion[WL_FW_VER_LENGTH];
// settings of requested network
static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH];
static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM];
static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM];
// settings of current selected network
static char _ssid[WL_SSID_MAX_LENGTH];
static uint8_t _bssid[WL_MAC_ADDR_LENGTH];
static uint8_t _mac[WL_MAC_ADDR_LENGTH];
static uint8_t _localIp[WL_IPV4_LENGTH];
// the ring buffer is used to search the tags in the stream
static RingBuffer ringBuf;
//static int sendCmd(const char* cmd, int timeout=1000);
static int sendCmd(const __FlashStringHelper* cmd, int timeout=1000);
static int sendCmd(const __FlashStringHelper* cmd, int timeout, ...);
static bool sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, const char* endTag, char* outStr, int outStrLen);
static bool sendCmdGet(const __FlashStringHelper* cmd, const __FlashStringHelper* startTag, const __FlashStringHelper* endTag, char* outStr, int outStrLen);
static int readUntil(int timeout, const char* tag=NULL, bool findTags=true);
static void espEmptyBuf(bool warn=true);
static int timedRead();
friend class WiFiEsp;
friend class WiFiEspServer;
friend class WiFiEspClient;
friend class WiFiEspUdp;
};
extern EspDrv espDrv;
#endif

View File

@@ -0,0 +1,105 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include "RingBuffer.h"
#include <Arduino.h>
RingBuffer::RingBuffer(unsigned int size)
{
_size = size;
// add one char to terminate the string
ringBuf = new char[size+1];
ringBufEnd = &ringBuf[size];
init();
}
RingBuffer::~RingBuffer() {}
void RingBuffer::reset()
{
ringBufP = ringBuf;
}
void RingBuffer::init()
{
ringBufP = ringBuf;
memset(ringBuf, 0, _size+1);
}
void RingBuffer::push(char c)
{
*ringBufP = c;
ringBufP++;
if (ringBufP>=ringBufEnd)
ringBufP = ringBuf;
}
bool RingBuffer::endsWith(const char* str)
{
int findStrLen = strlen(str);
// b is the start position into the ring buffer
char* b = ringBufP-findStrLen;
if(b < ringBuf)
b = b + _size;
char *p1 = (char*)&str[0];
char *p2 = p1 + findStrLen;
for(char *p=p1; p<p2; p++)
{
if(*p != *b)
return false;
b++;
if (b == ringBufEnd)
b=ringBuf;
}
return true;
}
void RingBuffer::getStr(char * destination, unsigned int skipChars)
{
int len = ringBufP-ringBuf-skipChars;
// copy buffer to destination string
strncpy(destination, ringBuf, len);
// terminate output string
//destination[len]=0;
}
void RingBuffer::getStrN(char * destination, unsigned int skipChars, unsigned int num)
{
int len = ringBufP-ringBuf-skipChars;
if (len>num)
len=num;
// copy buffer to destination string
strncpy(destination, ringBuf, len);
// terminate output string
//destination[len]=0;
}

View File

@@ -0,0 +1,47 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef RingBuffer_h
#define RingBuffer_h
class RingBuffer
{
public:
RingBuffer(unsigned int size);
~RingBuffer();
void reset();
void init();
void push(char c);
int getPos();
bool endsWith(const char* str);
void getStr(char * destination, unsigned int skipChars);
void getStrN(char * destination, unsigned int skipChars, unsigned int num);
private:
unsigned int _size;
char* ringBuf;
char* ringBufEnd;
char* ringBufP;
};
#endif

View File

@@ -0,0 +1,49 @@
/*--------------------------------------------------------------------
This file is part of the Arduino WiFiEsp library.
The Arduino WiFiEsp library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino WiFiEsp 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino WiFiEsp library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef EspDebug_H
#define EspDebug_H
#include <stdio.h>
// Change _ESPLOGLEVEL_ to set tracing and logging verbosity
// 0: DISABLED: no logging
// 1: ERROR: errors
// 2: WARN: errors and warnings
// 3: INFO: errors, warnings and informational (default)
// 4: DEBUG: errors, warnings, informational and debug
#ifndef _ESPLOGLEVEL_
#define _ESPLOGLEVEL_ 2
#endif
#define LOGERROR(x) if(_ESPLOGLEVEL_>0) { Serial.print("[WiFiEsp] "); Serial.println(x); }
#define LOGERROR1(x,y) if(_ESPLOGLEVEL_>2) { Serial.print("[WiFiEsp] "); Serial.print(x); Serial.print(" "); Serial.println(y); }
#define LOGWARN(x) if(_ESPLOGLEVEL_>1) { Serial.print("[WiFiEsp] "); Serial.println(x); }
#define LOGWARN1(x,y) if(_ESPLOGLEVEL_>2) { Serial.print("[WiFiEsp] "); Serial.print(x); Serial.print(" "); Serial.println(y); }
#define LOGINFO(x) if(_ESPLOGLEVEL_>2) { Serial.print("[WiFiEsp] "); Serial.println(x); }
#define LOGINFO1(x,y) if(_ESPLOGLEVEL_>2) { Serial.print("[WiFiEsp] "); Serial.print(x); Serial.print(" "); Serial.println(y); }
#define LOGDEBUG(x) if(_ESPLOGLEVEL_>3) { Serial.println(x); }
#define LOGDEBUG0(x) if(_ESPLOGLEVEL_>3) { Serial.print(x); }
#define LOGDEBUG1(x,y) if(_ESPLOGLEVEL_>3) { Serial.print(x); Serial.print(" "); Serial.println(y); }
#define LOGDEBUG2(x,y,z) if(_ESPLOGLEVEL_>3) { Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.println(z); }
#endif