[realtek-ambz] Move WiFiClient and WiFiServer to NetUtils library

This commit is contained in:
Kuba Szczodrzyński
2022-06-27 13:53:15 +02:00
parent 0493ec2245
commit f4ff13d6b7
13 changed files with 205 additions and 174 deletions

View File

@@ -1,7 +1,29 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
#include "WiFiClient.h"
#include "WiFiPriv.h"
#if LT_ARD_HAS_WIFI && LT_HAS_LWIP
#include "LwIPClient.h"
#define MAX_SOCK_NUM 4
#define WIFI_CLIENT_CONNECT_TIMEOUT 3000
#define WIFI_CLIENT_READ_TIMEOUT 3000
#define WIFI_CLIENT_WRITE_RETRY 10
#define WIFI_CLIENT_SELECT_TIMEOUT 1000
#define WIFI_CLIENT_FLUSH_BUF_SIZE 1024
// disable #defines removing lwip_ prefix
#undef LWIP_COMPAT_SOCKETS
#define LWIP_COMPAT_SOCKETS 0
extern "C" {
#include <lwip/api.h>
#include <lwip/dns.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <sys/time.h>
} // extern "C"
class SocketHandle {
public:
@@ -14,28 +36,28 @@ class SocketHandle {
}
};
WiFiClient::WiFiClient() {
LT_V_WC("WiFiClient()");
LwIPClient::LwIPClient() {
LT_V_WC("LwIPClient()");
_connected = false;
_sock = NULL;
_rxBuffer = NULL;
_timeout = WIFI_CLIENT_CONNECT_TIMEOUT;
}
WiFiClient::WiFiClient(int sock) {
LT_V_WC("WiFiClient(%d)", sock);
LwIPClient::LwIPClient(int sock) {
LT_V_WC("LwIPClient(%d)", sock);
_connected = true;
_sock = std::make_shared<SocketHandle>(sock);
_rxBuffer = std::make_shared<LwIPRxBuffer>(sock);
_timeout = WIFI_CLIENT_CONNECT_TIMEOUT;
}
WiFiClient::~WiFiClient() {
LT_V_WC("~WiFiClient()");
LwIPClient::~LwIPClient() {
LT_V_WC("~LwIPClient()");
stop();
}
WiFiClient &WiFiClient::operator=(const WiFiClient &other) {
LwIPClient &LwIPClient::operator=(const LwIPClient &other) {
stop();
_connected = other._connected;
_sock = other._sock;
@@ -47,22 +69,22 @@ bool IWiFiClient::operator==(const IWiFiClient &other) const {
return fd() == other.fd() && remoteIP() == other.remoteIP() && remotePort() == other.remotePort();
}
int WiFiClient::connect(IPAddress ip, uint16_t port) {
int LwIPClient::connect(IPAddress ip, uint16_t port) {
return connect(ip, port, _timeout);
}
int WiFiClient::connect(const char *host, uint16_t port) {
int LwIPClient::connect(const char *host, uint16_t port) {
return connect(host, port, _timeout);
}
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout) {
int LwIPClient::connect(const char *host, uint16_t port, int32_t timeout) {
IPAddress ip = WiFi.hostByName(host);
if (!ip)
return 0;
return connect(ip, port, timeout);
}
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
int sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
return -1;
@@ -128,11 +150,11 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
return 1;
}
size_t WiFiClient::write(uint8_t data) {
size_t LwIPClient::write(uint8_t data) {
return write(&data, 1);
}
size_t WiFiClient::write(Stream &stream) {
size_t LwIPClient::write(Stream &stream) {
uint8_t *buf = (uint8_t *)malloc(1360);
if (!buf) {
return 0;
@@ -149,7 +171,7 @@ size_t WiFiClient::write(Stream &stream) {
return written;
}
size_t WiFiClient::write(const uint8_t *buf, size_t size) {
size_t LwIPClient::write(const uint8_t *buf, size_t size) {
if (_sock < 0 || !_connected || !size) {
setWriteError();
return 0;
@@ -195,7 +217,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) {
return written;
}
int WiFiClient::available() {
int LwIPClient::available() {
if (!_connected || !_rxBuffer)
return 0;
int res = _rxBuffer->available();
@@ -205,23 +227,23 @@ int WiFiClient::available() {
return res;
}
int WiFiClient::fd() const {
int LwIPClient::fd() const {
if (!_sock)
return -1;
return _sock->fd;
}
int WiFiClient::socket() {
int LwIPClient::socket() {
return fd();
}
int WiFiClient::setTimeout(uint32_t seconds) {
int LwIPClient::setTimeout(uint32_t seconds) {
Client::setTimeout(seconds * 1000);
lwip_setsockopt(fd(), SOL_SOCKET, SO_RCVTIMEO, &_timeout, sizeof(_timeout));
return lwip_setsockopt(fd(), SOL_SOCKET, SO_SNDTIMEO, &_timeout, sizeof(_timeout));
}
int WiFiClient::read() {
int LwIPClient::read() {
uint8_t data;
int res = read(&data, 1);
if (res < 0)
@@ -231,7 +253,7 @@ int WiFiClient::read() {
return data;
}
int WiFiClient::read(uint8_t *buf, size_t size) {
int LwIPClient::read(uint8_t *buf, size_t size) {
int res = -1;
if (_rxBuffer) {
res = _rxBuffer->read(buf, size);
@@ -242,7 +264,7 @@ int WiFiClient::read(uint8_t *buf, size_t size) {
return res;
}
int WiFiClient::peek() {
int LwIPClient::peek() {
int res = -1;
if (_rxBuffer) {
res = _rxBuffer->peek();
@@ -253,7 +275,7 @@ int WiFiClient::peek() {
return res;
}
void WiFiClient::flush() {
void LwIPClient::flush() {
int res;
size_t len = available();
if (!len)
@@ -272,14 +294,14 @@ void WiFiClient::flush() {
free(buf);
}
void WiFiClient::stop() {
void LwIPClient::stop() {
LT_V_WC("stop()");
_connected = false;
_sock = NULL;
_rxBuffer = NULL;
}
uint8_t WiFiClient::connected() {
uint8_t LwIPClient::connected() {
if (_connected) {
uint8_t dummy;
if (lwip_recv(fd(), &dummy, 0, MSG_DONTWAIT) <= 0) {
@@ -323,34 +345,36 @@ uint16_t __attribute__((noinline)) getport(int sock, int (*func)(int, struct soc
return ntohs(s->sin_port);
}
IPAddress WiFiClient::remoteIP() const {
IPAddress LwIPClient::remoteIP() const {
return getaddr(fd(), lwip_getpeername);
}
IPAddress WiFiClient::remoteIP(int fd) const {
IPAddress LwIPClient::remoteIP(int fd) const {
return getaddr(fd, lwip_getpeername);
}
uint16_t WiFiClient::remotePort() const {
uint16_t LwIPClient::remotePort() const {
return getport(fd(), lwip_getpeername);
}
uint16_t WiFiClient::remotePort(int fd) const {
uint16_t LwIPClient::remotePort(int fd) const {
return getport(fd, lwip_getpeername);
}
IPAddress WiFiClient::localIP() const {
IPAddress LwIPClient::localIP() const {
return getaddr(fd(), lwip_getsockname);
}
IPAddress WiFiClient::localIP(int fd) const {
IPAddress LwIPClient::localIP(int fd) const {
return getaddr(fd, lwip_getsockname);
}
uint16_t WiFiClient::localPort() const {
uint16_t LwIPClient::localPort() const {
return getport(fd(), lwip_getsockname);
}
uint16_t WiFiClient::localPort(int fd) const {
uint16_t LwIPClient::localPort(int fd) const {
return getport(fd, lwip_getsockname);
}
#endif

View File

@@ -0,0 +1,56 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
#pragma once
#include <api/WiFi/WiFi.h>
#include <api/WiFiClient.h>
#include <lwip/LwIPRxBuffer.h>
#include <memory>
class SocketHandle;
class LwIPClient : public IWiFiClient {
private:
bool _connected;
std::shared_ptr<SocketHandle> _sock;
std::shared_ptr<LwIPRxBuffer> _rxBuffer;
public:
LwIPClient();
LwIPClient(int sock);
~LwIPClient();
int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port);
int connect(IPAddress ip, uint16_t port, int32_t timeout);
int connect(const char *host, uint16_t port, int32_t timeout);
size_t write(uint8_t data);
size_t write(const uint8_t *buf, size_t size);
size_t write(Stream &stream);
int available();
int fd() const;
int socket();
int setTimeout(uint32_t seconds);
int read();
int read(uint8_t *buf, size_t size);
int peek();
void flush();
void stop();
uint8_t connected();
LwIPClient &operator=(const LwIPClient &other);
IPAddress remoteIP() const;
IPAddress remoteIP(int sock) const;
uint16_t remotePort() const;
uint16_t remotePort(int sock) const;
IPAddress localIP() const;
IPAddress localIP(int sock) const;
uint16_t localPort() const;
uint16_t localPort(int sock) const;
using Print::write;
};

View File

@@ -1,18 +1,16 @@
#ifdef LT_HAS_LWIP
#if LT_HAS_LWIP
#include "LwIPRxBuffer.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// disable #defines removing lwip_ prefix
#undef LWIP_COMPAT_SOCKETS
#define LWIP_COMPAT_SOCKETS 0
extern "C" {
#include <lwip/sockets.h>
#ifdef __cplusplus
} // extern "C"
#endif
size_t LwIPRxBuffer::r_available() {
if (_sock < 0) {

View File

@@ -1,17 +1,30 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
#include "WiFiServer.h"
#include "WiFiPriv.h"
#if LT_ARD_HAS_WIFI && LT_HAS_LWIP
WiFiServer::WiFiServer(uint32_t addr, uint16_t port, uint8_t maxClients)
#include "LwIPServer.h"
// disable #defines removing lwip_ prefix
#undef LWIP_COMPAT_SOCKETS
#define LWIP_COMPAT_SOCKETS 0
extern "C" {
#include <lwip/api.h>
// #include <lwip/dns.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <sys/time.h>
}
LwIPServer::LwIPServer(uint32_t addr, uint16_t port, uint8_t maxClients)
: _sock(-1), _sockAccepted(-1), _addr(addr), _port(port), _maxClients(maxClients), _active(false), _noDelay(false) {
}
WiFiServer::operator bool() {
LwIPServer::operator bool() {
return _active;
}
bool WiFiServer::begin(uint16_t port, bool reuseAddr) {
bool LwIPServer::begin(uint16_t port, bool reuseAddr) {
if (_active)
return true;
if (port)
@@ -50,7 +63,7 @@ bool WiFiServer::begin(uint16_t port, bool reuseAddr) {
_sockAccepted = -1;
}
void WiFiServer::end() {
void LwIPServer::end() {
if (_sock == -1)
return;
lwip_close(_sock);
@@ -58,7 +71,7 @@ void WiFiServer::end() {
_active = -1;
}
WiFiClient WiFiServer::accept() {
WiFiClient LwIPServer::accept() {
if (!_active)
return WiFiClient();
@@ -96,7 +109,7 @@ WiFiClient WiFiServer::accept() {
return WiFiClient();
}
int WiFiServer::setTimeout(uint32_t seconds) {
int LwIPServer::setTimeout(uint32_t seconds) {
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
@@ -105,15 +118,15 @@ int WiFiServer::setTimeout(uint32_t seconds) {
return lwip_setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
void WiFiServer::setNoDelay(bool noDelay) {
void LwIPServer::setNoDelay(bool noDelay) {
_noDelay = noDelay;
}
bool WiFiServer::getNoDelay() {
bool LwIPServer::getNoDelay() {
return _noDelay;
}
bool WiFiServer::hasClient() {
bool LwIPServer::hasClient() {
if (_sockAccepted >= 0) {
return true;
}
@@ -125,3 +138,5 @@ bool WiFiServer::hasClient() {
}
return false;
}
#endif

View File

@@ -0,0 +1,47 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
#pragma once
#include <api/WiFi/WiFi.h>
#include <api/WiFiServer.h>
#include <WiFiClient.h>
class LwIPServer : public IWiFiServer<WiFiClient> {
private:
int _sock;
int _sockAccepted;
uint32_t _addr;
uint16_t _port;
uint8_t _maxClients;
bool _active;
bool _noDelay = false;
private:
LwIPServer(uint32_t addr, uint16_t port = 80, uint8_t maxClients = 4);
public:
LwIPServer(uint16_t port = 80, uint8_t maxClients = 4) : LwIPServer((uint32_t)0, port, maxClients) {}
LwIPServer(int port = 80, uint8_t maxClients = 4) : LwIPServer((uint32_t)0, port, maxClients) {}
LwIPServer(const IPAddress &addr, uint16_t port = 80, uint8_t maxClients = 4)
: LwIPServer((uint32_t)addr, port, maxClients) {}
operator bool();
bool begin(uint16_t port = 0, bool reuseAddr = true);
void end();
WiFiClient accept();
size_t write(const uint8_t *buffer, size_t size) {
return 0;
}
void stopAll() {}
int setTimeout(uint32_t seconds);
void setNoDelay(bool noDelay);
bool getNoDelay();
bool hasClient();
};

View File

@@ -4,22 +4,14 @@
#include "MbedTLSClient.h"
#include <IPAddress.h>
#include <WiFi.h>
#include <WiFiClient.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include <mbedtls/debug.h>
#include <mbedtls/platform.h>
#include <mbedtls/sha256.h>
#include <mbedtls/ssl.h>
#ifdef __cplusplus
} // extern "C"
#endif
MbedTLSClient::MbedTLSClient() : WiFiClient() {
init(); // ensure the context is zero filled

View File

@@ -2,20 +2,17 @@
#pragma once
#include <api/WiFi/WiFi.h>
#include <api/WiFiClient.h>
#include <api/WiFiClientSecure.h>
#include <WiFiClient.h> // extend family's WiFiClient impl
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include <mbedtls/net.h>
#ifdef __cplusplus
} // extern "C"
#endif
class MbedTLSClient : public WiFiClient, public IWiFiClientSecure {
private:

View File

@@ -1,55 +1,8 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
#pragma once
#include <LwIPRxBuffer.h>
#include <api/WiFiClient.h>
#include <memory>
#include <api/WiFi/WiFi.h>
#include <lwip/LwIPClient.h>
class SocketHandle;
class WiFiClient : public IWiFiClient {
private:
bool _connected;
std::shared_ptr<SocketHandle> _sock;
std::shared_ptr<LwIPRxBuffer> _rxBuffer;
public:
WiFiClient();
WiFiClient(int sock);
~WiFiClient();
int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port);
int connect(IPAddress ip, uint16_t port, int32_t timeout);
int connect(const char *host, uint16_t port, int32_t timeout);
size_t write(uint8_t data);
size_t write(const uint8_t *buf, size_t size);
size_t write(Stream &stream);
int available();
int fd() const;
int socket();
int setTimeout(uint32_t seconds);
int read();
int read(uint8_t *buf, size_t size);
int peek();
void flush();
void stop();
uint8_t connected();
WiFiClient &operator=(const WiFiClient &other);
IPAddress remoteIP() const;
IPAddress remoteIP(int sock) const;
uint16_t remotePort() const;
uint16_t remotePort(int sock) const;
IPAddress localIP() const;
IPAddress localIP(int sock) const;
uint16_t localPort() const;
uint16_t localPort(int sock) const;
using Print::write;
};
typedef LwIPClient WiFiClient;

View File

@@ -2,7 +2,7 @@
#pragma once
#include <WiFi.h>
#include <api/WiFi/WiFi.h>
#include <ssl/MbedTLSClient.h>
typedef MbedTLSClient WiFiClientSecure;

View File

@@ -2,27 +2,15 @@
#pragma once
#include "WiFi.h"
#include <api/WiFi/WiFi.h>
extern "C" {
// these are defined in PIO builder (for IDE to understand)
// copy defines from PIO builder (for IDE to understand)
#define LWIP_TIMEVAL_PRIVATE 0
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_SO_RCVBUF 1
#define MAX_SOCK_NUM 4
#define WIFI_CLIENT_CONNECT_TIMEOUT 3000
#define WIFI_CLIENT_READ_TIMEOUT 3000
#define WIFI_CLIENT_WRITE_RETRY 10
#define WIFI_CLIENT_SELECT_TIMEOUT 1000
#define WIFI_CLIENT_FLUSH_BUF_SIZE 1024
// disable #defines removing lwip_ prefix
#undef LWIP_COMPAT_SOCKETS
#define LWIP_COMPAT_SOCKETS 0
#include <lwip/sockets.h>
#include <FreeRTOS.h>
#include <autoconf.h>
#include <dhcp/dhcps.h>
@@ -31,7 +19,6 @@ extern "C" {
#include <lwip/err.h>
#include <lwip_netconf.h>
#include <semphr.h>
#include <sys/time.h>
#include <wifi_conf.h>
#include <wifi_constants.h>
#include <wifi_structures.h>

View File

@@ -1,46 +1,8 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
#pragma once
#include <api/WiFiServer.h>
#include <api/WiFi/WiFi.h>
#include <lwip/LwIPServer.h>
#include "WiFiClient.h"
class WiFiServer : public IWiFiServer<WiFiClient> {
private:
int _sock;
int _sockAccepted;
uint32_t _addr;
uint16_t _port;
uint8_t _maxClients;
bool _active;
bool _noDelay = false;
private:
WiFiServer(uint32_t addr, uint16_t port = 80, uint8_t maxClients = 4);
public:
WiFiServer(uint16_t port = 80, uint8_t maxClients = 4) : WiFiServer((uint32_t)0, port, maxClients) {}
WiFiServer(int port = 80, uint8_t maxClients = 4) : WiFiServer((uint32_t)0, port, maxClients) {}
WiFiServer(const IPAddress &addr, uint16_t port = 80, uint8_t maxClients = 4)
: WiFiServer((uint32_t)addr, port, maxClients) {}
operator bool();
bool begin(uint16_t port = 0, bool reuseAddr = true);
void end();
WiFiClient accept();
size_t write(const uint8_t *buffer, size_t size) {
return 0;
}
void stopAll() {}
int setTimeout(uint32_t seconds);
void setNoDelay(bool noDelay);
bool getNoDelay();
bool hasClient();
};
typedef LwIPServer WiFiServer;

View File

@@ -24,7 +24,7 @@ env.Append(
# which conflicts with C++ built-in bool
# so it's either -fpermissive or this:
("bool", "bool"),
# implemented features
# LibreTuya configuration
("LT_ARD_HAS_WIFI", "1"),
("LT_ARD_HAS_MD5", "1"),
# not broken anymore with printf() library