Skip to content

Commit

Permalink
Rename WiFi Server and UDP
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev committed Mar 21, 2024
1 parent 0ec3884 commit a39dcfd
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 202 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ set(ARDUINO_LIBRARY_Networking_SRCS
libraries/Networking/src/NetworkEvents.cpp
libraries/Networking/src/NetworkManager.cpp
libraries/Networking/src/WiFiClient.cpp
libraries/Networking/src/WiFiServer.cpp
libraries/Networking/src/WiFiUdp.cpp)
libraries/Networking/src/NetworkServer.cpp
libraries/Networking/src/NetworkUdp.cpp)

set(ARDUINO_LIBRARY_WiFi_SRCS
libraries/WiFi/src/WiFiAP.cpp
Expand Down
2 changes: 1 addition & 1 deletion libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <NetworkUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
Expand Down
2 changes: 1 addition & 1 deletion libraries/ArduinoOTA/src/ArduinoOTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ArduinoOTAClass
String _hostname;
String _partition_label;
String _nonce;
WiFiUDP _udp_ota;
NetworkUDP _udp_ota;
bool _initialized;
bool _rebootOnSuccess;
bool _mdnsEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const char* ssid = "............";
const char* password = "..............";

// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
NetworkServer server(80);

void setup(void)
{
Expand Down
2 changes: 1 addition & 1 deletion libraries/HTTPUpdate/src/HTTPUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren

WiFiClient * tcp = http.getStreamPtr();

// To do? WiFiUDP::stopAll();
// To do? NetworkUDP::stopAll();
// To do? WiFiClient::stopAllExcept(tcp);

delay(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
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 "WiFiServer.h"
#include "NetworkServer.h"
#include <lwip/sockets.h>
#include <lwip/netdb.h>

#undef write
#undef close

int WiFiServer::setTimeout(uint32_t seconds){
int NetworkServer::setTimeout(uint32_t seconds){
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
Expand All @@ -32,11 +32,11 @@ int WiFiServer::setTimeout(uint32_t seconds){
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
}

WiFiClient WiFiServer::available(){
WiFiClient NetworkServer::available(){
return accept();
}

WiFiClient WiFiServer::accept(){
WiFiClient NetworkServer::accept(){
if(!_listening)
return WiFiClient();
int client_sock;
Expand Down Expand Up @@ -64,11 +64,11 @@ WiFiClient WiFiServer::accept(){
return WiFiClient();
}

void WiFiServer::begin(uint16_t port){
void NetworkServer::begin(uint16_t port){
begin(port, 1);
}

void WiFiServer::begin(uint16_t port, int enable){
void NetworkServer::begin(uint16_t port, int enable){
if(_listening)
return;
if(port){
Expand Down Expand Up @@ -99,15 +99,15 @@ void WiFiServer::begin(uint16_t port, int enable){
_accepted_sockfd = -1;
}

void WiFiServer::setNoDelay(bool nodelay) {
void NetworkServer::setNoDelay(bool nodelay) {
_noDelay = nodelay;
}

bool WiFiServer::getNoDelay() {
bool NetworkServer::getNoDelay() {
return _noDelay;
}

bool WiFiServer::hasClient() {
bool NetworkServer::hasClient() {
if (_accepted_sockfd >= 0) {
return true;
}
Expand All @@ -124,7 +124,7 @@ bool WiFiServer::hasClient() {
return false;
}

void WiFiServer::end(){
void NetworkServer::end(){
#ifdef ESP_IDF_VERSION_MAJOR
lwip_close(sockfd);
#else
Expand All @@ -134,10 +134,10 @@ void WiFiServer::end(){
_listening = false;
}

void WiFiServer::close(){
void NetworkServer::close(){
end();
}

void WiFiServer::stop(){
void NetworkServer::stop(){
end();
}
62 changes: 62 additions & 0 deletions libraries/Networking/src/NetworkServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Server.h - Server class for Raspberry Pi
Copyright (c) 2016 Hristo Gochkov 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
*/
#ifndef _WIFISERVER_H_
#define _WIFISERVER_H_

#include "Arduino.h"
#include "Server.h"
#include "WiFiClient.h"
#include "IPAddress.h"

class NetworkServer {
private:
int sockfd;
int _accepted_sockfd = -1;
IPAddress _addr;
uint16_t _port;
uint8_t _max_clients;
bool _listening;
bool _noDelay = false;

public:
void listenOnLocalhost(){}

NetworkServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) {
log_v("NetworkServer::NetworkServer(port=%d, ...)", port);
}
NetworkServer(const IPAddress& addr, uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(addr),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) {
log_v("NetworkServer::NetworkServer(addr=%s, port=%d, ...)", addr.toString().c_str(), port);
}
~NetworkServer(){ end();}
WiFiClient available() __attribute__((deprecated("Renamed to accept().")));
WiFiClient accept();
void begin(uint16_t port=0);
void begin(uint16_t port, int reuse_enable);
void setNoDelay(bool nodelay);
bool getNoDelay();
bool hasClient();

void end();
void close();
void stop();
operator bool(){return _listening;}
int setTimeout(uint32_t seconds);
};

#endif /* _WIFISERVER_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "WiFiUdp.h"
#include "NetworkUdp.h"
#include <new> //std::nothrow
#include <lwip/sockets.h>
#include <lwip/netdb.h>
Expand All @@ -26,7 +26,7 @@
#undef write
#undef read

WiFiUDP::WiFiUDP()
NetworkUDP::NetworkUDP()
: udp_server(-1)
, server_port(0)
, remote_port(0)
Expand All @@ -35,11 +35,11 @@ WiFiUDP::WiFiUDP()
, rx_buffer(0)
{}

WiFiUDP::~WiFiUDP(){
NetworkUDP::~NetworkUDP(){
stop();
}

uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
uint8_t NetworkUDP::begin(IPAddress address, uint16_t port){
stop();

server_port = port;
Expand Down Expand Up @@ -100,11 +100,11 @@ uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
return 1;
}

uint8_t WiFiUDP::begin(uint16_t p){
uint8_t NetworkUDP::begin(uint16_t p){
return begin(IPAddress(), p);
}

uint8_t WiFiUDP::beginMulticast(IPAddress address, uint16_t p){
uint8_t NetworkUDP::beginMulticast(IPAddress address, uint16_t p){
if(begin(IPAddress(), p)){
ip_addr_t addr;
address.to_ip_addr_t(&addr);
Expand Down Expand Up @@ -147,7 +147,7 @@ uint8_t WiFiUDP::beginMulticast(IPAddress address, uint16_t p){
return 0;
}

void WiFiUDP::stop(){
void NetworkUDP::stop(){
if(tx_buffer){
free(tx_buffer);
tx_buffer = NULL;
Expand Down Expand Up @@ -190,15 +190,15 @@ void WiFiUDP::stop(){
udp_server = -1;
}

int WiFiUDP::beginMulticastPacket(){
int NetworkUDP::beginMulticastPacket(){
if(!server_port || multicast_ip == IPAddress())
return 0;
remote_ip = multicast_ip;
remote_port = server_port;
return beginPacket();
}

int WiFiUDP::beginPacket(){
int NetworkUDP::beginPacket(){
if(!remote_port)
return 0;

Expand Down Expand Up @@ -226,13 +226,13 @@ int WiFiUDP::beginPacket(){
return 1;
}

int WiFiUDP::beginPacket(IPAddress ip, uint16_t port){
int NetworkUDP::beginPacket(IPAddress ip, uint16_t port){
remote_ip = ip;
remote_port = port;
return beginPacket();
}

int WiFiUDP::beginPacket(const char *host, uint16_t port){
int NetworkUDP::beginPacket(const char *host, uint16_t port){
struct hostent *server;
server = gethostbyname(host);
if (server == NULL){
Expand All @@ -242,7 +242,7 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port){
return beginPacket(IPAddress((const uint8_t *)(server->h_addr_list[0])), port);
}

int WiFiUDP::endPacket(){
int NetworkUDP::endPacket(){
ip_addr_t addr;
remote_ip.to_ip_addr_t(&addr);

Expand Down Expand Up @@ -272,7 +272,7 @@ int WiFiUDP::endPacket(){
return 1;
}

size_t WiFiUDP::write(uint8_t data){
size_t NetworkUDP::write(uint8_t data){
if(tx_buffer_len == 1460){
endPacket();
tx_buffer_len = 0;
Expand All @@ -281,14 +281,14 @@ size_t WiFiUDP::write(uint8_t data){
return 1;
}

size_t WiFiUDP::write(const uint8_t *buffer, size_t size){
size_t NetworkUDP::write(const uint8_t *buffer, size_t size){
size_t i;
for(i=0;i<size;i++)
write(buffer[i]);
return i;
}

int WiFiUDP::parsePacket(){
int NetworkUDP::parsePacket(){
if(rx_buffer)
return 0;
struct sockaddr_storage si_other_storage; // enough storage for v4 and v6
Expand Down Expand Up @@ -338,12 +338,12 @@ int WiFiUDP::parsePacket(){
return len;
}

int WiFiUDP::available(){
int NetworkUDP::available(){
if(!rx_buffer) return 0;
return rx_buffer->available();
}

int WiFiUDP::read(){
int NetworkUDP::read(){
if(!rx_buffer) return -1;
int out = rx_buffer->read();
if(!rx_buffer->available()){
Expand All @@ -354,11 +354,11 @@ int WiFiUDP::read(){
return out;
}

int WiFiUDP::read(unsigned char* buffer, size_t len){
int NetworkUDP::read(unsigned char* buffer, size_t len){
return read((char *)buffer, len);
}

int WiFiUDP::read(char* buffer, size_t len){
int NetworkUDP::read(char* buffer, size_t len){
if(!rx_buffer) return 0;
int out = rx_buffer->read(buffer, len);
if(!rx_buffer->available()){
Expand All @@ -369,22 +369,22 @@ int WiFiUDP::read(char* buffer, size_t len){
return out;
}

int WiFiUDP::peek(){
int NetworkUDP::peek(){
if(!rx_buffer) return -1;
return rx_buffer->peek();
}

void WiFiUDP::flush(){
void NetworkUDP::flush(){
if(!rx_buffer) return;
cbuf *b = rx_buffer;
rx_buffer = 0;
delete b;
}

IPAddress WiFiUDP::remoteIP(){
IPAddress NetworkUDP::remoteIP(){
return remote_ip;
}

uint16_t WiFiUDP::remotePort(){
uint16_t NetworkUDP::remotePort(){
return remote_port;
}
Loading

0 comments on commit a39dcfd

Please sign in to comment.