Skip to content

Commit

Permalink
Merge pull request #48 from nRF24/fixOutgoingData
Browse files Browse the repository at this point in the history
Fixes: rexmit mechanism & hold
  • Loading branch information
TMRh20 authored Jul 1, 2024
2 parents 92d05c4 + 2c5ed33 commit 992c185
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
65 changes: 36 additions & 29 deletions RF24Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)
// RF24EthernetClass::tick();
goto test2;
}
u->hold = false;
return u->out_pos;
}
u->hold = false;
Expand Down Expand Up @@ -278,18 +279,15 @@ void serialip_appcall(void)
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif
u->hold = (u->out_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->in_pos + u->dataCnt], uip_appdata, uip_datalen());
u->dataCnt += uip_datalen();

u->packets_in = 1;
Expand Down Expand Up @@ -337,39 +335,46 @@ void serialip_appcall(void)
/*******Polling**********/
if (uip_poll() || uip_rexmit())
{
if (uip_rexmit()) {
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print(F("ReXmit, Len: ")););
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(u->out_pos));
uip_len = u->out_pos;
uip_send(u->myData, u->out_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);
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->in_pos = 0;
data->out_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->in_pos], size);
data->dataCnt -= size;

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

if (!data->dataCnt)
{
data->packets_in = 0;
data->dataPos = 0;
data->in_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->in_pos];
}
return -1;
}
Expand Down
5 changes: 2 additions & 3 deletions RF24Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ 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 struct __attribute__((__packed__))
{
bool hold;
bool sent;
bool packets_in;
bool packets_out;
bool windowOpened;
uint8_t state;
uint16_t in_pos;
uint16_t out_pos;
uint16_t dataPos;
uint16_t dataCnt;
#if UIP_CLIENT_TIMER >= 0
uint32_t timer;
Expand Down
16 changes: 7 additions & 9 deletions RF24Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,16 @@ void RF24EthernetClass::network_send()
{
RF24NetworkHeader headerOut(00, EXTERNAL_DATA_TYPE);

#if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
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
Serial.println();
Serial.print(millis());
Serial.println(F(" *** RF24Ethernet Network Write Fail ***"));
}
#else
RF24Ethernet.network.write(headerOut, uip_buf, uip_len);
#endif

#if defined ETH_DEBUG_L2
if (ok) {
Expand Down

0 comments on commit 992c185

Please sign in to comment.