Skip to content

Commit a797d3f

Browse files
d-a-vincowizglenlee
authored andcommitted
keepalive api (default 2h,75s,9 not enabled) (esp8266#3937)
1 parent 1e96e65 commit a797d3f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,29 @@ void WiFiClient::stopAllExcept(WiFiClient* except)
344344
}
345345
}
346346
}
347+
348+
349+
void WiFiClient::keepAlive (uint16_t idle_sec, uint16_t intv_sec, uint8_t count)
350+
{
351+
_client->keepAlive(idle_sec, intv_sec, count);
352+
}
353+
354+
bool WiFiClient::isKeepAliveEnabled () const
355+
{
356+
return _client->isKeepAliveEnabled();
357+
}
358+
359+
uint16_t WiFiClient::getKeepAliveIdle () const
360+
{
361+
return _client->getKeepAliveIdle();
362+
}
363+
364+
uint16_t WiFiClient::getKeepAliveInterval () const
365+
{
366+
return _client->getKeepAliveInterval();
367+
}
368+
369+
uint8_t WiFiClient::getKeepAliveCount () const
370+
{
371+
return _client->getKeepAliveCount();
372+
}

libraries/ESP8266WiFi/src/WiFiClient.h

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#define WIFICLIENT_MAX_PACKET_SIZE 1460
3232

33+
#define TCP_DEFAULT_KEEPALIVE_IDLE_SEC 7200 // 2 hours
34+
#define TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC 75 // 75 sec
35+
#define TCP_DEFAULT_KEEPALIVE_COUNT 9 // fault after 9 failures
36+
3337
class ClientContext;
3438
class WiFiServer;
3539

@@ -85,6 +89,13 @@ class WiFiClient : public Client, public SList<WiFiClient> {
8589
static void stopAll();
8690
static void stopAllExcept(WiFiClient * c);
8791

92+
void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT);
93+
bool isKeepAliveEnabled () const;
94+
uint16_t getKeepAliveIdle () const;
95+
uint16_t getKeepAliveInterval () const;
96+
uint8_t getKeepAliveCount () const;
97+
void disableKeepAlive () { keepAlive(0, 0, 0); }
98+
8899
protected:
89100

90101
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);

libraries/ESP8266WiFi/src/include/ClientContext.h

+35
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class ClientContext
4343
tcp_sent(pcb, &_s_sent);
4444
tcp_err(pcb, &_s_error);
4545
tcp_poll(pcb, &_s_poll, 1);
46+
47+
// not enabled by default for 2.4.0
48+
//keepAlive();
4649
}
4750

4851
err_t abort()
@@ -341,6 +344,38 @@ class ClientContext
341344
return _write_from_source(new BufferedStreamDataSource<ProgmemStream>(stream, size));
342345
}
343346

347+
void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT)
348+
{
349+
if (idle_sec && intv_sec && count) {
350+
_pcb->so_options |= SOF_KEEPALIVE;
351+
_pcb->keep_idle = (uint32_t)1000 * idle_sec;
352+
_pcb->keep_intvl = (uint32_t)1000 * intv_sec;
353+
_pcb->keep_cnt = count;
354+
}
355+
else
356+
_pcb->so_options &= ~SOF_KEEPALIVE;
357+
}
358+
359+
bool isKeepAliveEnabled () const
360+
{
361+
return !!(_pcb->so_options & SOF_KEEPALIVE);
362+
}
363+
364+
uint16_t getKeepAliveIdle () const
365+
{
366+
return isKeepAliveEnabled()? (_pcb->keep_idle + 500) / 1000: 0;
367+
}
368+
369+
uint16_t getKeepAliveInterval () const
370+
{
371+
return isKeepAliveEnabled()? (_pcb->keep_intvl + 500) / 1000: 0;
372+
}
373+
374+
uint8_t getKeepAliveCount () const
375+
{
376+
return isKeepAliveEnabled()? _pcb->keep_cnt: 0;
377+
}
378+
344379
protected:
345380

346381
bool _is_timeout()

0 commit comments

Comments
 (0)