Skip to content

Commit 992c185

Browse files
authored
Merge pull request #48 from nRF24/fixOutgoingData
Fixes: rexmit mechanism & hold
2 parents 92d05c4 + 2c5ed33 commit 992c185

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

RF24Client.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)
217217
// RF24EthernetClass::tick();
218218
goto test2;
219219
}
220+
u->hold = false;
220221
return u->out_pos;
221222
}
222223
u->hold = false;
@@ -278,18 +279,15 @@ void serialip_appcall(void)
278279
#if UIP_CONNECTION_TIMEOUT > 0
279280
u->connectTimer = millis();
280281
#endif
282+
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
281283

282-
if (u->sent)
283-
{
284-
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
285-
}
286284
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
287285
{
288286
uip_stop();
289287
u->state &= ~UIP_CLIENT_RESTART;
290288
u->windowOpened = false;
291289
u->restartTime = millis();
292-
memcpy(&u->myData[u->dataPos + u->dataCnt], uip_appdata, uip_datalen());
290+
memcpy(&u->myData[u->in_pos + u->dataCnt], uip_appdata, uip_datalen());
293291
u->dataCnt += uip_datalen();
294292

295293
u->packets_in = 1;
@@ -337,39 +335,46 @@ void serialip_appcall(void)
337335
/*******Polling**********/
338336
if (uip_poll() || uip_rexmit())
339337
{
338+
if (uip_rexmit()) {
339+
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print(F("ReXmit, Len: ")););
340+
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(u->out_pos));
341+
uip_len = u->out_pos;
342+
uip_send(u->myData, u->out_pos);
343+
u->hold = true;
344+
goto finish;
345+
}
340346
// IF_RF24ETHERNET_DEBUG_CLIENT( Serial.println(); Serial.println(F("UIPClient uip_poll")); );
341347

342-
if (u->packets_out != 0)
348+
if (u->packets_out != 0 && !u->hold)
343349
{
344350
uip_len = u->out_pos;
345351
uip_send(u->myData, u->out_pos);
346352
u->hold = true;
347-
u->sent = true;
348353
goto finish;
349354
}
350-
else
351-
// Restart mechanism to keep connections going
352-
// Only call this if the TCP window has already been re-opened, the connection is being polled, but no data
353-
// has been acked
354-
if (!(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
355-
{
356355

357-
if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
358-
{
359-
u->restartTime = millis();
356+
// Restart mechanism to keep connections going
357+
// Only call this if the TCP window has already been re-opened, the connection is being polled, but no data
358+
// has been acked
359+
if (!(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
360+
{
361+
362+
if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
363+
{
364+
u->restartTime = millis();
360365
#if defined RF24ETHERNET_DEBUG_CLIENT || defined ETH_DEBUG_L1
361-
Serial.println();
362-
Serial.print(millis());
366+
Serial.println();
367+
Serial.print(millis());
363368
#if UIP_CONNECTION_TIMEOUT > 0
364-
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
365-
Serial.println(UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
369+
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
370+
Serial.println(UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
366371
#endif
367372
#endif
368-
u->restartInterval += 500;
369-
u->restartInterval = rf24_min(u->restartInterval, 7000);
370-
uip_restart();
371-
}
373+
u->restartInterval += 500;
374+
u->restartInterval = rf24_min(u->restartInterval, 7000);
375+
uip_restart();
372376
}
377+
}
373378
}
374379

375380
/*******Close**********/
@@ -423,9 +428,11 @@ uip_userdata_t* RF24Client::_allocateData()
423428
data->packets_in = 0;
424429
data->packets_out = 0;
425430
data->dataCnt = 0;
426-
data->dataPos = 0;
431+
data->in_pos = 0;
427432
data->out_pos = 0;
428433
data->hold = 0;
434+
data->restartTime = millis();
435+
data->restartInterval = 5000;
429436
#if (UIP_CONNECTION_TIMEOUT > 0)
430437
data->connectTimer = millis();
431438
data->connectTimeout = UIP_CONNECTION_TIMEOUT;
@@ -483,15 +490,15 @@ int RF24Client::read(uint8_t* buf, size_t size)
483490
}
484491

485492
size = rf24_min(data->dataCnt, size);
486-
memcpy(buf, &data->myData[data->dataPos], size);
493+
memcpy(buf, &data->myData[data->in_pos], size);
487494
data->dataCnt -= size;
488495

489-
data->dataPos += size;
496+
data->in_pos += size;
490497

491498
if (!data->dataCnt)
492499
{
493500
data->packets_in = 0;
494-
data->dataPos = 0;
501+
data->in_pos = 0;
495502

496503
if (uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS]) && !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
497504
{
@@ -536,7 +543,7 @@ int RF24Client::peek()
536543
{
537544
if (available())
538545
{
539-
return data->myData[data->dataPos];
546+
return data->myData[data->in_pos];
540547
}
541548
return -1;
542549
}

RF24Client.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,15 @@ typedef struct
5050
* Data structure for holding per connection data
5151
* @warning <b> This is used internally and should not be accessed directly by users </b>
5252
*/
53-
typedef struct
53+
typedef struct __attribute__((__packed__))
5454
{
5555
bool hold;
56-
bool sent;
5756
bool packets_in;
5857
bool packets_out;
5958
bool windowOpened;
6059
uint8_t state;
60+
uint16_t in_pos;
6161
uint16_t out_pos;
62-
uint16_t dataPos;
6362
uint16_t dataCnt;
6463
#if UIP_CLIENT_TIMER >= 0
6564
uint32_t timer;

RF24Ethernet.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,16 @@ void RF24EthernetClass::network_send()
315315
{
316316
RF24NetworkHeader headerOut(00, EXTERNAL_DATA_TYPE);
317317

318+
#if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
318319
bool ok = RF24Ethernet.network.write(headerOut, uip_buf, uip_len);
319-
320320
if (!ok) {
321-
ok = RF24Ethernet.network.write(headerOut, uip_buf, uip_len);
322-
#if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
323-
if (!ok) {
324-
Serial.println();
325-
Serial.print(millis());
326-
Serial.println(F(" *** RF24Ethernet Network Write Fail ***"));
327-
}
328-
#endif
321+
Serial.println();
322+
Serial.print(millis());
323+
Serial.println(F(" *** RF24Ethernet Network Write Fail ***"));
329324
}
325+
#else
326+
RF24Ethernet.network.write(headerOut, uip_buf, uip_len);
327+
#endif
330328

331329
#if defined ETH_DEBUG_L2
332330
if (ok) {

0 commit comments

Comments
 (0)