Skip to content

Commit

Permalink
Fix connection issues (nRF24#33)
Browse files Browse the repository at this point in the history
* Fix for connection issues
   - Found an issue where a connection is made but no data is sent. The connection can be left open indefinitely unless there is a timeout of sorts.

* Amend applicable examples

* Alter data struct
   - Put the boolean last in the data struct for memory alignment purposes
  • Loading branch information
TMRh20 committed Jul 8, 2022
1 parent 53295bc commit b310132
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions RF24Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ void serialip_appcall(void)
{
uip_userdata_t *u = (uip_userdata_t *)uip_conn->appstate;

if(u && u->connectTimeout > 0){
if(millis() - u->connectTimer > u->connectTimeout && u->initialData == false){
uip_close();
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.println("UIP Client close(timeout)"););
}
}

/*******Connected**********/
if (!u && uip_connected())
{
Expand All @@ -265,6 +272,8 @@ void serialip_appcall(void)
{
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.print(F(" UIPClient uip_newdata, uip_len:")); Serial.println(uip_len););

u->initialData = true;

if (u->sent)
{
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
Expand Down Expand Up @@ -428,6 +437,8 @@ uip_userdata_t *RF24Client::_allocateData()
data->dataPos = 0;
data->out_pos = 0;
data->hold = 0;
data->initialData = false;
data->connectTimer = millis();
return data;
}
}
Expand Down
3 changes: 3 additions & 0 deletions RF24Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ typedef struct {
uint32_t restartTime;
uint32_t restartInterval;
uint32_t connAbortTime;
uint32_t connectTimeout;
uint32_t connectTimer;
uint8_t myData[OUTPUT_BUFFER_SIZE];
bool initialData;
} uip_userdata_t;


Expand Down
10 changes: 10 additions & 0 deletions RF24Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,13 @@ size_t RF24Server::write(const uint8_t *buf, size_t size)
}
return ret;
}

void RF24Server::setTimeout(uint32_t timeout)
{
for(uint8_t i = 0; i < UIP_CONNS; i++){
uip_userdata_t *data = &RF24Client::all_data[i];
if(data){
data->connectTimeout = timeout;
}
}
}
1 change: 1 addition & 0 deletions RF24Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RF24Server : public Server {
size_t write(uint8_t);
size_t write(const uint8_t *buf, size_t size);
using Print::write;
void setTimeout(uint32_t timeout);

private:
uint16_t _port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ void setup() {
// Listen for incoming connections on TCP port 1000. Each incoming
// connection will result in the uip_callback() function being called.
server.begin();

// If no data is received from an incoming connection in the first 30 seconds,
// close the connection
server.setTimeout(30000);
}

uint32_t mesh_timer = 0;
Expand Down
1 change: 1 addition & 0 deletions examples/InteractiveServer_Mesh/InteractiveServer_Mesh.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void setup() {
Ethernet.set_gateway(gwIP);

server.begin();
server.setTimeout(30000);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void setup() {
Ethernet.set_gateway(gwIP);

server.begin();
server.setTimeout(30000);
// prepare GPIO2
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 0);
Expand Down

0 comments on commit b310132

Please sign in to comment.