Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIRE136] Network redesign #234

Draft
wants to merge 79 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
960d8bc
reimplementing the ethernet drivers
andreagilardoni Dec 28, 2023
452accf
added library containing abstract network stack classes
andreagilardoni Dec 28, 2023
30725ad
reimplementing the tcp client with the aim of solving issues
andreagilardoni Dec 28, 2023
a0666af
cleaning up
andreagilardoni Dec 28, 2023
4c0123f
redefinition of network Interface classes
andreagilardoni Dec 28, 2023
2637cdf
restructuring CLwipIf singleton class
andreagilardoni Dec 28, 2023
216e3aa
rewriting CNetif base class
andreagilardoni Dec 28, 2023
28f9199
rewriting CEth CwifiStation and CSoftAP classes
andreagilardoni Dec 28, 2023
d40a06a
added possibility to pass a std::function to esphosted callbacks
andreagilardoni Dec 29, 2023
58c2860
defined Ethernet driver and interface instance
andreagilardoni Dec 29, 2023
3850758
removed unused endif
andreagilardoni Dec 29, 2023
9784bac
commented netif stats
andreagilardoni Dec 29, 2023
f5633d6
defined network interface abstrac class
andreagilardoni Dec 29, 2023
40772d1
fixing lwipClient
andreagilardoni Dec 29, 2023
cef42da
fixing imports and variable names
andreagilardoni Dec 29, 2023
e76ded7
cleaning CNetIf classes definition
andreagilardoni Dec 29, 2023
d39a90a
fixing class implementation and making it able to compile
andreagilardoni Dec 29, 2023
a28de2d
defined tmp utils library
andreagilardoni Dec 29, 2023
460a0d5
reimplementing wifi handle
andreagilardoni Dec 29, 2023
ae29df7
commnting code not yet ready to be compiled
andreagilardoni Dec 29, 2023
9a871ef
deleted lwip tcp wrapper
andreagilardoni Jan 2, 2024
0b6dd35
implemented netowrk interfaces specific clients
andreagilardoni Jan 2, 2024
f911f67
defined Wifi global variable for network interface
andreagilardoni Jan 2, 2024
4c86ebf
added scanforap method
andreagilardoni Jan 2, 2024
b371331
added comment
andreagilardoni Jan 4, 2024
1c58899
reimplementing Server class with ethernet and wifi subclasses
andreagilardoni Jan 4, 2024
ed6886d
fixing bug of missing nullptr check
andreagilardoni Jan 4, 2024
10a05bd
removed unused types
andreagilardoni Jan 4, 2024
42063d1
minor changes on file
andreagilardoni Jan 4, 2024
e41294a
Revert "defined Wifi global variable for network interface"
andreagilardoni Jan 5, 2024
f20a258
Revert "reimplementing wifi handle"
andreagilardoni Jan 5, 2024
552a153
applying a codestyle
andreagilardoni Jan 5, 2024
d239597
fixing EthernetServer class
andreagilardoni Jan 8, 2024
6e6c837
making WiFi class compile
andreagilardoni Jan 8, 2024
ea4ae60
fixing implementation of client and server
andreagilardoni Jan 8, 2024
87ae27a
fixing global definitions of WiFi interfaces
andreagilardoni Jan 8, 2024
0c210ee
making UDP derived classes compile
andreagilardoni Jan 8, 2024
2c10e0e
fixing retrocompatibility issues
andreagilardoni Jan 8, 2024
4cb8171
dealing with retrocompatibility
andreagilardoni Jan 8, 2024
c9ed50c
solving FIXME
andreagilardoni Jan 9, 2024
719e63f
fixing dns related functions
andreagilardoni Jan 9, 2024
58f9561
CNetIf - added config method to apply new static IP settings
JAndrassy Dec 14, 2023
45c85f3
adapting cherry pick
andreagilardoni Jan 9, 2024
664fc69
moving funtion in correct section in file
andreagilardoni Jan 9, 2024
3a1e29d
added dhcp_inform call
andreagilardoni Jan 9, 2024
b5eb21b
fixing compilation issue
andreagilardoni Jan 10, 2024
ae01d58
removing FIXME
andreagilardoni Jan 10, 2024
f8507fa
restoring CWiFi methods to extract scanned access point info
andreagilardoni Jan 10, 2024
e0ce22c
removing lwipMem files
andreagilardoni Jan 10, 2024
4a1e9d4
moving free_pbuf_chain to an external file in order to be shared from…
andreagilardoni Jan 10, 2024
4980778
moving lwipTypes udp_struct into udp file
andreagilardoni Jan 10, 2024
56c1d07
readapting udp class
andreagilardoni Jan 10, 2024
e1af45e
improving task syncrhonization between timer and main context
andreagilardoni Jan 12, 2024
0809792
fixing dns resolution bug
andreagilardoni Jan 12, 2024
9734322
cleaning code
andreagilardoni Jan 12, 2024
c3ba277
fine tuning lwip stack and compiling it
andreagilardoni Jan 12, 2024
66943b8
adding dns resolution with lwip example
andreagilardoni Jan 12, 2024
9cb6f55
enablig execution of network stack in interrrupt context]
andreagilardoni Jan 12, 2024
99ca0fa
added disconnect and link status for retrocompatibility
andreagilardoni Jan 17, 2024
7e88a57
fixing lwip server
andreagilardoni Jan 19, 2024
88f5091
blocking begin call on dhcp acquisition
andreagilardoni Jan 19, 2024
0f430c5
Making it possible to call multiple time the begin function on an int…
andreagilardoni Jan 22, 2024
ec3cba5
making netif compatible with connection handler usage
andreagilardoni Jan 22, 2024
e56d9f9
fixing return status of begin
andreagilardoni Jan 23, 2024
1ac341d
adding test files for lwipwrapper WIP
andreagilardoni Jan 23, 2024
0207e39
fixing connection issue on Ethernet and Wifi Client when host is passed
andreagilardoni Jan 23, 2024
3bbaccd
exporting Network stack synchronization to lwipClient
andreagilardoni Jan 23, 2024
c595cc0
fixing return codes
andreagilardoni Jan 23, 2024
ec5ea83
deleting pbuf when partially consumed
andreagilardoni Jan 23, 2024
564fcfc
Changed memory alignemnt according to renesas specifications
andreagilardoni Jan 24, 2024
6c5f0ea
improving examples
andreagilardoni Jan 24, 2024
6edeefe
setting dhcp acquired when dhcp server is informed of static ip
andreagilardoni Jan 24, 2024
896f7ca
improving performances of ssl client
andreagilardoni Jan 24, 2024
aa3c5a1
fixing counter overflow in tcp send
andreagilardoni Jan 24, 2024
9e05471
added tcp write example for tcp echo server
andreagilardoni Jan 25, 2024
e489e71
changing synchronization to timer disable
andreagilardoni Jan 26, 2024
e303948
improving write function to better handle memory errors and bigger bu…
andreagilardoni Jan 26, 2024
b58a521
making dns resolution to work automatically when interrupts are disabled
andreagilardoni Jan 26, 2024
c28b4bd
improving example
andreagilardoni Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixing lwip server
andreagilardoni committed Jan 19, 2024
commit 7e88a577e64119128aede33b897c273e26ef697a
3 changes: 2 additions & 1 deletion libraries/Ethernet/src/EthernetClient.h
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@ class EthernetClient : public lwipClient {
EthernetClient(struct tcp_pcb *pcb, lwipServer *server)
: lwipClient(pcb, server) {
}
EthernetClient(lwipClient c)
EthernetClient(const lwipClient &c)
: lwipClient(c) {
this->bindCNetIf(Ethernet);
}

int connect(IPAddress ip, uint16_t port) {
3 changes: 2 additions & 1 deletion libraries/Ethernet/src/EthernetServer.h
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ class EthernetServer: public lwipServer {
}

EthernetClient available() {
return EthernetClient(lwipServer::available());
lwipClient* res = available_ptr();
return res != nullptr ? EthernetClient(*res) : EthernetClient(CLIENT_NONE);
}
};
3 changes: 2 additions & 1 deletion libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@ class WiFiClient: public lwipClient {
WiFiClient(struct tcp_pcb *pcb, lwipServer *server)
: lwipClient(pcb, server) {
}
WiFiClient(lwipClient c)
WiFiClient(const lwipClient &c)
: lwipClient(c) {
this->bindCNetIf(WiFiStation);
}

int connect(IPAddress ip, uint16_t port) {
3 changes: 2 additions & 1 deletion libraries/WiFi/src/WiFiServer.h
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ class WiFiServer: public lwipServer {
}

WiFiClient available() {
return WiFiClient(lwipServer::available());
lwipClient* res = available_ptr();
return res != nullptr ? WiFiClient(*res) : WiFiClient(CLIENT_NONE);
}
};
35 changes: 19 additions & 16 deletions libraries/lwIpWrapper/src/lwipClient.cpp
Original file line number Diff line number Diff line change
@@ -80,7 +80,9 @@ lwipClient& lwipClient::operator=(lwipClient&& rhs) {
}

lwipClient::~lwipClient() {
this->stop();
if(this->tcp_info->state != TCP_CLOSING) {
this->stop();
}
}

int lwipClient::connect(const char* host, uint16_t port) {
@@ -231,7 +233,7 @@ err_t lwipClient::recv_callback(struct tcp_pcb* tpcb, struct pbuf* p, err_t err)
return ERR_OK;
}
arduino::lock();
if(this->tcp_info->state == TCP_CONNECTED) {
if(this->tcp_info->state == TCP_CONNECTED || this->tcp_info->state == TCP_ACCEPTED) {
if (this->tcp_info->pbuf_head == nullptr) {
// no need to increment the references of the pbuf,
// since it is already 1 and lwip shifts the control to this code
@@ -273,16 +275,11 @@ size_t lwipClient::write(const uint8_t* buffer, size_t size) {
} else if(res == ERR_MEM) {
// FIXME handle this: we get into this case only if the sent data cannot be put in the send queue
}

// TODO understand if the tcp_write will send data if the buffer is not full
// force send only if we filled the send buffer
// if (ERR_OK != tcp_output(this->tcp_info->pcb)) {
// // return 0;
// break;
// }
} while(buffer_cursor < buffer + size);
arduino::unlock();

tcp_output(this->tcp_info->pcb);

arduino::unlock();
return buffer - buffer_cursor;
}

@@ -342,13 +339,13 @@ void lwipClient::flush() {
}

void lwipClient::stop() {
tcp_recv(this->tcp_info->pcb, nullptr);
tcp_sent(this->tcp_info->pcb, nullptr);
tcp_poll(this->tcp_info->pcb, nullptr, 0);
tcp_err(this->tcp_info->pcb, nullptr);
tcp_accept(this->tcp_info->pcb, nullptr);

if(this->tcp_info->pcb != nullptr) {
tcp_recv(this->tcp_info->pcb, nullptr);
tcp_sent(this->tcp_info->pcb, nullptr);
tcp_poll(this->tcp_info->pcb, nullptr, 0);
tcp_err(this->tcp_info->pcb, nullptr);
tcp_accept(this->tcp_info->pcb, nullptr);

err_t err = tcp_close(this->tcp_info->pcb);
this->tcp_info->state = TCP_CLOSING;

@@ -362,6 +359,12 @@ void lwipClient::stop() {
// if(tcp->p != nullptr) {
// pbuf_free(tcp->p); // FIXME it happens that a pbuf, with ref == 0 is added for some reason
// }
if(this->tcp_info->server != nullptr) {
// need to first make the server point to nullptr, then remove the client, can cause infinite recursion
auto server = this->tcp_info->server;
this->tcp_info->server = nullptr;
server->remove(this);
}
}

uint8_t lwipClient::connected() {
40 changes: 31 additions & 9 deletions libraries/lwIpWrapper/src/lwipServer.cpp
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ extern "C" {
#include "CNetIf.h"
#include "lwipClient.h"
#include "lwipServer.h"
#include "utils.h"

err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err);

@@ -74,9 +75,12 @@ void lwipServer::begin(uint16_t port)
// }

void lwipServer::remove(lwipClient* client) {
arduino::lock();

bool found = false;
for (int i=0; i < size; i++) {
if(found) {
// we move the client to delete to the end of the array, then we remove it
clients[i-1] = clients[i];
} else if(*client == *clients[i]) {
found = true;
@@ -85,25 +89,41 @@ void lwipServer::remove(lwipClient* client) {

delete clients[--size];
clients[size] = nullptr;

arduino::unlock();
}

void lwipServer::accept(struct tcp_pcb* new_client) {
bool lwipServer::accept(struct tcp_pcb* new_client) {
bool res = false;
// this->clean();

arduino::lock();
if(size < MAX_CLIENT-1) {
clients[size] = new lwipClient(new_client, this);
size++;
clients_available++;
res = true;
}
arduino::unlock();

return res;
}

lwipClient lwipServer::available()
{
lwipClient* res = available_ptr();
return res != nullptr ? *res : CLIENT_NONE;
}

lwipClient* lwipServer::available_ptr()
{
lwipClient* res=nullptr;
arduino::lock();
if(size > 0 && clients_available>0) {
return *clients[size-clients_available--]; // TODO verify index
} else {
return CLIENT_NONE;
res = clients[size-clients_available--]; // TODO verify index
}
arduino::unlock();

return res;
}

size_t lwipServer::write(uint8_t b)
@@ -112,28 +132,30 @@ size_t lwipServer::write(uint8_t b)
}

size_t lwipServer::write(const uint8_t* buffer, size_t size) {
arduino::lock();
size_t written=0;
// this->clean();

for (int i = 0; i < MAX_CLIENT; i++) {
written += clients[i]->write(buffer, size);
}
arduino::unlock();

return written;
}

err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err) {
arduino::lock();
lwipServer* server = (lwipServer*) arg;
err_t ret_err;
err_t ret_err = ERR_OK;

/* set priority for the newly accepted tcp connection newpcb */
tcp_setprio(newpcb, TCP_PRIO_MIN);

if ((arg != NULL) && (ERR_OK == err)) {
server->accept(newpcb);
} else {
if ((arg == NULL) || (ERR_OK != err) || !server->accept(newpcb)) {
tcp_close(newpcb);
ret_err = ERR_ARG;
}
arduino::unlock();
return ret_err;
}
4 changes: 3 additions & 1 deletion libraries/lwIpWrapper/src/lwipServer.h
Original file line number Diff line number Diff line change
@@ -31,10 +31,12 @@ class lwipServer: public Server {
* - when a client connection is closed (by calling stop on it or delete on the client)
* the server is notified and the remove() method is called thus the client is removed from the server list.
*/
void accept(struct tcp_pcb* new_client);
bool accept(struct tcp_pcb* new_client);
// void clean();
void remove(lwipClient* client);

lwipClient* available_ptr();

uint16_t _port;
const IPAddress &listen_address;
tcp_pcb* server_pcb;