ethernet webserver works too
This commit is contained in:
90
lib/WebServer/src/hardware/W5500EthClient.cpp
Normal file
90
lib/WebServer/src/hardware/W5500EthClient.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "W5500EthClient.h"
|
||||
|
||||
W5500EthClient::W5500EthClient(EthernetClient *wifiClient)
|
||||
: _ethClient(wifiClient)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
W5500EthClient::~W5500EthClient()
|
||||
{
|
||||
_ethClient = nullptr;
|
||||
}
|
||||
|
||||
uint8_t W5500EthClient::connected()
|
||||
{
|
||||
return _ethClient->connected();
|
||||
}
|
||||
|
||||
int W5500EthClient::setTimeout(uint32_t seconds)
|
||||
{
|
||||
// return _ethClient->setTimeout(seconds);
|
||||
}
|
||||
|
||||
size_t W5500EthClient::write(const char *buffer, size_t size)
|
||||
{
|
||||
return _ethClient->write(buffer, size);
|
||||
}
|
||||
|
||||
IPAddress W5500EthClient::localIP()
|
||||
{
|
||||
return IPAddress(0,0,0,0);
|
||||
}
|
||||
|
||||
void W5500EthClient::stop()
|
||||
{
|
||||
_ethClient->stop();
|
||||
}
|
||||
|
||||
size_t W5500EthClient::write_P(const char *buf, size_t size)
|
||||
{
|
||||
return _ethClient->write(buf, size);
|
||||
}
|
||||
|
||||
int W5500EthClient::available()
|
||||
{
|
||||
return _ethClient->available();
|
||||
}
|
||||
|
||||
String W5500EthClient::readStringUntil(char terminator)
|
||||
{
|
||||
return _ethClient->readStringUntil(terminator);
|
||||
}
|
||||
|
||||
size_t W5500EthClient::readBytes(char *buffer, size_t length)
|
||||
{
|
||||
return _ethClient->readBytes(buffer, length);
|
||||
}
|
||||
|
||||
void W5500EthClient::flush()
|
||||
{
|
||||
_ethClient->flush();
|
||||
}
|
||||
|
||||
int W5500EthClient::read()
|
||||
{
|
||||
return _ethClient->read();
|
||||
}
|
||||
|
||||
unsigned long W5500EthClient::getTimeout(void)
|
||||
{
|
||||
return _ethClient->getTimeout();
|
||||
}
|
||||
|
||||
size_t W5500EthClient::write(Stream &stream)
|
||||
{
|
||||
uint8_t * buf = (uint8_t *)malloc(1360);
|
||||
if(!buf){
|
||||
return 0;
|
||||
}
|
||||
size_t toRead = 0, toWrite = 0, written = 0;
|
||||
size_t available = stream.available();
|
||||
while(available){
|
||||
toRead = (available > 1360)?1360:available;
|
||||
toWrite = stream.readBytes(buf, toRead);
|
||||
written += _ethClient->write(buf, toWrite);
|
||||
available = stream.available();
|
||||
}
|
||||
free(buf);
|
||||
return written;
|
||||
}
|
||||
29
lib/WebServer/src/hardware/W5500EthClient.h
Normal file
29
lib/WebServer/src/hardware/W5500EthClient.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <EthernetClient.h>
|
||||
#include "EthClient.h"
|
||||
|
||||
class W5500EthClient : public EthClient
|
||||
{
|
||||
public:
|
||||
explicit W5500EthClient(EthernetClient* wifiClient);
|
||||
virtual ~W5500EthClient();
|
||||
|
||||
uint8_t connected() override;
|
||||
int available() override;
|
||||
unsigned long getTimeout(void) override;
|
||||
int setTimeout(uint32_t seconds) override;
|
||||
int read() override;
|
||||
size_t write(const char *buffer, size_t size) override;
|
||||
size_t write(Stream &stream) override;
|
||||
size_t write_P(const char *buf, size_t size) override;
|
||||
String readStringUntil(char terminator) override;
|
||||
size_t readBytes(char *buffer, size_t length) override;
|
||||
IPAddress localIP() override;
|
||||
void stop() override;
|
||||
void flush() override;
|
||||
|
||||
private:
|
||||
EthernetClient* _ethClient;
|
||||
};
|
||||
75
lib/WebServer/src/hardware/W5500EthServer.cpp
Normal file
75
lib/WebServer/src/hardware/W5500EthServer.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "W5500EthServer.h"
|
||||
|
||||
|
||||
W5500EthServer::W5500EthServer(IPAddress address, int port)
|
||||
: EthServer(address, port),
|
||||
_ethServer(address, port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
W5500EthServer::W5500EthServer(int port)
|
||||
: EthServer(port),
|
||||
_ethServer(port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void W5500EthServer::close()
|
||||
{
|
||||
// _ethServer.close();
|
||||
}
|
||||
|
||||
void W5500EthServer::begin(const int port)
|
||||
{
|
||||
_ethServer.begin(port);
|
||||
}
|
||||
|
||||
void W5500EthServer::setNoDelay(const bool value)
|
||||
{
|
||||
// _ethServer.setNoDelay(value);
|
||||
}
|
||||
|
||||
EthClient* W5500EthServer::available()
|
||||
{
|
||||
if(_W5500EthClient != nullptr)
|
||||
{
|
||||
delete _W5500EthClient;
|
||||
_W5500EthClient = nullptr;
|
||||
}
|
||||
|
||||
_ethClient = _ethServer.available();
|
||||
_W5500EthClient = new W5500EthClient(&_ethClient);
|
||||
return _W5500EthClient;
|
||||
}
|
||||
|
||||
|
||||
void W5500EthServer::discardClient()
|
||||
{
|
||||
if(_W5500EthClient != nullptr)
|
||||
{
|
||||
delete _W5500EthClient;
|
||||
_W5500EthClient = nullptr;
|
||||
}
|
||||
|
||||
_ethClient = EthernetClient();
|
||||
}
|
||||
|
||||
|
||||
// EthernetServerImpl
|
||||
void EthernetServerImpl::begin(uint16_t port)
|
||||
{
|
||||
EthernetServer::begin();
|
||||
}
|
||||
|
||||
EthernetServerImpl::EthernetServerImpl(int address, int port)
|
||||
: EthernetServer(port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EthernetServerImpl::EthernetServerImpl(int port)
|
||||
: EthernetServer(port)
|
||||
{
|
||||
|
||||
}
|
||||
35
lib/WebServer/src/hardware/W5500EthServer.h
Normal file
35
lib/WebServer/src/hardware/W5500EthServer.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "EthServer.h"
|
||||
#include "W5500EthClient.h"
|
||||
#include <WiFiServer.h>
|
||||
#include <EthernetServer.h>
|
||||
|
||||
class EthernetServerImpl : public EthernetServer
|
||||
{
|
||||
public:
|
||||
EthernetServerImpl(int address, int port);
|
||||
explicit EthernetServerImpl(int port);
|
||||
|
||||
virtual void begin(uint16_t port);
|
||||
};
|
||||
|
||||
class W5500EthServer : public EthServer
|
||||
{
|
||||
public:
|
||||
W5500EthServer(IPAddress address, int port);
|
||||
explicit W5500EthServer(int port);
|
||||
|
||||
virtual EthClient* available();
|
||||
virtual void discardClient();
|
||||
|
||||
virtual void begin(const int port = 80);
|
||||
virtual void close();
|
||||
|
||||
virtual void setNoDelay(const bool value);
|
||||
|
||||
private:
|
||||
EthernetServerImpl _ethServer;
|
||||
EthernetClient _ethClient;
|
||||
W5500EthClient* _W5500EthClient = nullptr;
|
||||
};
|
||||
77
lib/WebServer/src/hardware/WifiEthClient.cpp
Normal file
77
lib/WebServer/src/hardware/WifiEthClient.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "WifiEthClient.h"
|
||||
|
||||
WifiEthClient::WifiEthClient(WiFiClient *wifiClient)
|
||||
: _wifiClient(wifiClient)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WifiEthClient::~WifiEthClient()
|
||||
{
|
||||
_wifiClient = nullptr;
|
||||
}
|
||||
|
||||
uint8_t WifiEthClient::connected()
|
||||
{
|
||||
return _wifiClient->connected();
|
||||
}
|
||||
|
||||
int WifiEthClient::setTimeout(uint32_t seconds)
|
||||
{
|
||||
return _wifiClient->setTimeout(seconds);
|
||||
}
|
||||
|
||||
size_t WifiEthClient::write(const char *buffer, size_t size)
|
||||
{
|
||||
return _wifiClient->write(buffer, size);
|
||||
}
|
||||
|
||||
IPAddress WifiEthClient::localIP()
|
||||
{
|
||||
return _wifiClient->localIP();
|
||||
}
|
||||
|
||||
void WifiEthClient::stop()
|
||||
{
|
||||
_wifiClient->stop();
|
||||
}
|
||||
|
||||
size_t WifiEthClient::write_P(const char *buf, size_t size)
|
||||
{
|
||||
return _wifiClient->write_P(buf, size);
|
||||
}
|
||||
|
||||
int WifiEthClient::available()
|
||||
{
|
||||
return _wifiClient->available();
|
||||
}
|
||||
|
||||
String WifiEthClient::readStringUntil(char terminator)
|
||||
{
|
||||
return _wifiClient->readStringUntil(terminator);
|
||||
}
|
||||
|
||||
size_t WifiEthClient::readBytes(char *buffer, size_t length)
|
||||
{
|
||||
return _wifiClient->readBytes(buffer, length);
|
||||
}
|
||||
|
||||
void WifiEthClient::flush()
|
||||
{
|
||||
_wifiClient->flush();
|
||||
}
|
||||
|
||||
int WifiEthClient::read()
|
||||
{
|
||||
return _wifiClient->read();
|
||||
}
|
||||
|
||||
unsigned long WifiEthClient::getTimeout(void)
|
||||
{
|
||||
return _wifiClient->getTimeout();
|
||||
}
|
||||
|
||||
size_t WifiEthClient::write(Stream &stream)
|
||||
{
|
||||
return _wifiClient->write(stream);
|
||||
}
|
||||
28
lib/WebServer/src/hardware/WifiEthClient.h
Normal file
28
lib/WebServer/src/hardware/WifiEthClient.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include "EthClient.h"
|
||||
|
||||
class WifiEthClient : public EthClient
|
||||
{
|
||||
public:
|
||||
explicit WifiEthClient(WiFiClient* wifiClient);
|
||||
virtual ~WifiEthClient();
|
||||
|
||||
uint8_t connected() override;
|
||||
int available() override;
|
||||
unsigned long getTimeout(void) override;
|
||||
int setTimeout(uint32_t seconds) override;
|
||||
int read() override;
|
||||
size_t write(const char *buffer, size_t size) override;
|
||||
size_t write(Stream &stream) override;
|
||||
size_t write_P(const char *buf, size_t size) override;
|
||||
String readStringUntil(char terminator) override;
|
||||
size_t readBytes(char *buffer, size_t length) override;
|
||||
IPAddress localIP() override;
|
||||
void stop() override;
|
||||
void flush() override;
|
||||
|
||||
private:
|
||||
WiFiClient* _wifiClient;
|
||||
};
|
||||
56
lib/WebServer/src/hardware/WifiEthServer.cpp
Normal file
56
lib/WebServer/src/hardware/WifiEthServer.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "WifiEthServer.h"
|
||||
|
||||
|
||||
WifiEthServer::WifiEthServer(IPAddress address, int port)
|
||||
: EthServer(address, port),
|
||||
_wifiServer(address, port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WifiEthServer::WifiEthServer(int port)
|
||||
: EthServer(port),
|
||||
_wifiServer(port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WifiEthServer::close()
|
||||
{
|
||||
_wifiServer.close();
|
||||
}
|
||||
|
||||
void WifiEthServer::begin(const int port)
|
||||
{
|
||||
_wifiServer.begin(port);
|
||||
}
|
||||
|
||||
void WifiEthServer::setNoDelay(const bool value)
|
||||
{
|
||||
_wifiServer.setNoDelay(value);
|
||||
}
|
||||
|
||||
EthClient* WifiEthServer::available()
|
||||
{
|
||||
if(_wifiEthClient != nullptr)
|
||||
{
|
||||
delete _wifiEthClient;
|
||||
_wifiEthClient = nullptr;
|
||||
}
|
||||
|
||||
_wifiClient = _wifiServer.available();
|
||||
_wifiEthClient = new WifiEthClient(&_wifiClient);
|
||||
return _wifiEthClient;
|
||||
}
|
||||
|
||||
|
||||
void WifiEthServer::discardClient()
|
||||
{
|
||||
if(_wifiEthClient != nullptr)
|
||||
{
|
||||
delete _wifiEthClient;
|
||||
_wifiEthClient = nullptr;
|
||||
}
|
||||
|
||||
_wifiClient = WiFiClient();
|
||||
}
|
||||
25
lib/WebServer/src/hardware/WifiEthServer.h
Normal file
25
lib/WebServer/src/hardware/WifiEthServer.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "EthServer.h"
|
||||
#include "WifiEthClient.h"
|
||||
#include <WiFiServer.h>
|
||||
|
||||
class WifiEthServer : public EthServer
|
||||
{
|
||||
public:
|
||||
WifiEthServer(IPAddress address, int port);
|
||||
explicit WifiEthServer(int port);
|
||||
|
||||
virtual EthClient* available();
|
||||
virtual void discardClient();
|
||||
|
||||
virtual void begin(const int port = 80);
|
||||
virtual void close();
|
||||
|
||||
virtual void setNoDelay(const bool value);
|
||||
|
||||
private:
|
||||
WiFiServer _wifiServer;
|
||||
WiFiClient _wifiClient;
|
||||
WifiEthClient* _wifiEthClient = nullptr;
|
||||
};
|
||||
Reference in New Issue
Block a user