Skip to content

Commit

Permalink
Fixes: rexmit & hold
Browse files Browse the repository at this point in the history
- Fix: re-transmit mechanism - this was not working if waiting for an ack due to the `hold` variable being set.
- Fix: `hold` variable was not being set or unset where it should be
- Remove out_pos & dataPos variables from userdata struct, replace with data_pos variable
- Add attribute_packed to the userdata struct since it is now mis-aligned
- Now only transmit one RF24Network payload instead of a retry, since the rexmit mechanism is now working
  • Loading branch information
TMRh20 committed Jul 1, 2024
1 parent 92d05c4 commit ef264b6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
83 changes: 45 additions & 38 deletions RF24Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,16 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)
if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)) && u->state & (UIP_CLIENT_CONNECTED))
{

if (u->out_pos + payloadSize > UIP_TCP_MSS || u->hold)
if (u->data_pos + payloadSize > UIP_TCP_MSS || u->hold)
{
goto test2;
}

IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.print(F(" UIPClient.write: writePacket(")); Serial.print(u->packets_out); Serial.print(F(") pos: ")); Serial.print(u->out_pos); Serial.print(F(", buf[")); Serial.print(size - total_written); Serial.print(F("]: '")); Serial.write((uint8_t*)buf + total_written, payloadSize); Serial.println(F("'")););
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.print(F(" UIPClient.write: writePacket(")); Serial.print(u->packets_out); Serial.print(F(") pos: ")); Serial.print(u->data_pos); Serial.print(F(", buf[")); Serial.print(size - total_written); Serial.print(F("]: '")); Serial.write((uint8_t*)buf + total_written, payloadSize); Serial.println(F("'")););

memcpy(u->myData + u->out_pos, buf + total_written, payloadSize);
memcpy(u->myData + u->data_pos, buf + total_written, payloadSize);
u->packets_out = 1;
u->out_pos += payloadSize;
u->data_pos += payloadSize;

total_written += payloadSize;

Expand All @@ -217,7 +217,8 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)
// RF24EthernetClass::tick();
goto test2;
}
return u->out_pos;
u->hold = false;
return u->data_pos;
}
u->hold = false;
return -1;
Expand Down Expand Up @@ -278,18 +279,15 @@ void serialip_appcall(void)
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif
u->hold = (u->data_pos = (u->windowOpened = (u->packets_out = false)));

if (u->sent)
{
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
}
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
{
uip_stop();
u->state &= ~UIP_CLIENT_RESTART;
u->windowOpened = false;
u->restartTime = millis();
memcpy(&u->myData[u->dataPos + u->dataCnt], uip_appdata, uip_datalen());
memcpy(&u->myData[u->data_pos + u->dataCnt], uip_appdata, uip_datalen());
u->dataCnt += uip_datalen();

u->packets_in = 1;
Expand Down Expand Up @@ -327,7 +325,7 @@ void serialip_appcall(void)
{
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.println(F(" UIPClient uip_acked")););
u->state &= ~UIP_CLIENT_RESTART;
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
u->hold = (u->data_pos = (u->windowOpened = (u->packets_out = false)));
u->restartTime = millis();
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
Expand All @@ -337,39 +335,46 @@ void serialip_appcall(void)
/*******Polling**********/
if (uip_poll() || uip_rexmit())
{
if (uip_rexmit()) {
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(F("ReXmit, Len: ")););
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(u->data_pos));
uip_len = u->data_pos;
uip_send(u->myData, u->data_pos);
u->hold = true;
goto finish;
}
// IF_RF24ETHERNET_DEBUG_CLIENT( Serial.println(); Serial.println(F("UIPClient uip_poll")); );

if (u->packets_out != 0)
if (u->packets_out != 0 && !u->hold)
{
uip_len = u->out_pos;
uip_send(u->myData, u->out_pos);
uip_len = u->data_pos;
uip_send(u->myData, u->data_pos);
u->hold = true;
u->sent = true;
goto finish;
}
else
// Restart mechanism to keep connections going
// Only call this if the TCP window has already been re-opened, the connection is being polled, but no data
// has been acked
if (!(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
{

if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
{
u->restartTime = millis();
// Restart mechanism to keep connections going
// Only call this if the TCP window has already been re-opened, the connection is being polled, but no data
// has been acked
if (!(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
{

if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
{
u->restartTime = millis();
#if defined RF24ETHERNET_DEBUG_CLIENT || defined ETH_DEBUG_L1
Serial.println();
Serial.print(millis());
Serial.println();
Serial.print(millis());
#if UIP_CONNECTION_TIMEOUT > 0
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
Serial.println(UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
Serial.println(UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
#endif
#endif
u->restartInterval += 500;
u->restartInterval = rf24_min(u->restartInterval, 7000);
uip_restart();
}
u->restartInterval += 500;
u->restartInterval = rf24_min(u->restartInterval, 7000);
uip_restart();
}
}
}

/*******Close**********/
Expand Down Expand Up @@ -423,9 +428,11 @@ uip_userdata_t* RF24Client::_allocateData()
data->packets_in = 0;
data->packets_out = 0;
data->dataCnt = 0;
data->dataPos = 0;
data->out_pos = 0;
//data->dataPos = 0;
data->data_pos = 0;
data->hold = 0;
data->restartTime = millis();
data->restartInterval = 5000;
#if (UIP_CONNECTION_TIMEOUT > 0)
data->connectTimer = millis();
data->connectTimeout = UIP_CONNECTION_TIMEOUT;
Expand Down Expand Up @@ -483,15 +490,15 @@ int RF24Client::read(uint8_t* buf, size_t size)
}

size = rf24_min(data->dataCnt, size);
memcpy(buf, &data->myData[data->dataPos], size);
memcpy(buf, &data->myData[data->data_pos], size);
data->dataCnt -= size;

data->dataPos += size;
data->data_pos += size;

if (!data->dataCnt)
{
data->packets_in = 0;
data->dataPos = 0;
data->data_pos = 0;

if (uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS]) && !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
{
Expand Down Expand Up @@ -536,7 +543,7 @@ int RF24Client::peek()
{
if (available())
{
return data->myData[data->dataPos];
return data->myData[data->data_pos];
}
return -1;
}
Expand Down
6 changes: 2 additions & 4 deletions RF24Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ typedef struct
* Data structure for holding per connection data
* @warning <b> This is used internally and should not be accessed directly by users </b>
*/
typedef struct
typedef __attribute__((__packed__)) struct
{
bool hold;
bool sent;
bool packets_in;
bool packets_out;
bool windowOpened;
uint8_t state;
uint16_t out_pos;
uint16_t dataPos;
uint16_t data_pos;
uint16_t dataCnt;
#if UIP_CLIENT_TIMER >= 0
uint32_t timer;
Expand Down
13 changes: 5 additions & 8 deletions RF24Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,13 @@ void RF24EthernetClass::network_send()

bool ok = RF24Ethernet.network.write(headerOut, uip_buf, uip_len);

if (!ok) {
ok = RF24Ethernet.network.write(headerOut, uip_buf, uip_len);
#if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
if (!ok) {
Serial.println();
Serial.print(millis());
Serial.println(F(" *** RF24Ethernet Network Write Fail ***"));
}
#endif
if (!ok) {
Serial.println();
Serial.print(millis());
Serial.println(F(" *** RF24Ethernet Network Write Fail ***"));
}
#endif

#if defined ETH_DEBUG_L2
if (ok) {
Expand Down

0 comments on commit ef264b6

Please sign in to comment.