Skip to content

Commit 9709fca

Browse files
Adding ping as api of connection handler
1 parent 1c65703 commit 9709fca

15 files changed

+142
-5
lines changed

src/CatM1ConnectionHandler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
9595
}
9696

9797
DEBUG_INFO(F("Sending PING to outer space..."));
98-
int const ping_result = GSM.ping("time.arduino.cc");
98+
int const ping_result = ping("time.arduino.cc");
9999
DEBUG_INFO(F("GSM.ping(): %d"), ping_result);
100100
if (ping_result < 0)
101101
{
@@ -140,4 +140,16 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected()
140140
}
141141
}
142142

143+
int CatM1ConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
144+
return GSM.ping(ip);
145+
}
146+
147+
int CatM1ConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
148+
return GSM.ping(hostname);
149+
}
150+
151+
int CatM1ConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
152+
return GSM.ping(host);
153+
}
154+
143155
#endif /* #ifdef BOARD_HAS_CATM1_NBIOT */

src/CatM1ConnectionHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class CatM1ConnectionHandler : public ConnectionHandler
4141
virtual Client & getClient() override { return _gsm_client; };
4242
virtual UDP & getUDP() override { return _gsm_udp; };
4343

44+
int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1) override;
45+
int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1) override;
46+
int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1) override;
4447

4548
protected:
4649

src/CellularConnectionHandler.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ NetworkConnectionState CellularConnectionHandler::update_handleDisconnected()
111111
return NetworkConnectionState::CLOSED;
112112
}
113113

114+
int CellularConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
115+
return 0;
116+
}
117+
118+
int CellularConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
119+
return 0;
120+
}
121+
122+
int CellularConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
123+
return 0;
124+
}
125+
114126
#endif /* #ifdef BOARD_HAS_CELLULAR */

src/CellularConnectionHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class CellularConnectionHandler : public ConnectionHandler
4141
virtual Client & getClient() override { return _gsm_client; };
4242
virtual UDP & getUDP() override;
4343

44+
int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1) override;
45+
int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1) override;
46+
int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1) override;
4447

4548
protected:
4649

src/ConnectionHandlerInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class ConnectionHandler {
5757
virtual UDP &getUDP() = 0;
5858

5959
virtual unsigned long getTime() = 0;
60+
61+
virtual int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1) = 0;
62+
virtual int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1) = 0;
63+
virtual int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1) = 0;
6064
#endif
6165

6266
NetworkConnectionState getStatus() __attribute__((deprecated)) {

src/EthernetConnectionHandler.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
110110
return NetworkConnectionState::CONNECTED;
111111
}
112112

113-
int ping_result = Ethernet.ping("time.arduino.cc");
113+
int ping_result = ping("time.arduino.cc");
114114
DEBUG_INFO(F("Ethernet.ping(): %d"), ping_result);
115115
if (ping_result < 0)
116116
{
@@ -157,4 +157,28 @@ NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected()
157157
}
158158
}
159159

160+
int EthernetConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
161+
#if defined(ARDUINO_ARCH_ZEPHYR)
162+
return 0;
163+
#else
164+
return Ethernet.ping(ip);
165+
#endif // ARDUINO_ARCH_ZEPHYR
166+
}
167+
168+
int EthernetConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
169+
#if defined(ARDUINO_ARCH_ZEPHYR)
170+
return 0;
171+
#else
172+
return Ethernet.ping(hostname);
173+
#endif // ARDUINO_ARCH_ZEPHYR
174+
}
175+
176+
int EthernetConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
177+
#if defined(ARDUINO_ARCH_ZEPHYR)
178+
return 0;
179+
#else
180+
return Ethernet.ping(host);
181+
#endif // ARDUINO_ARCH_ZEPHYR
182+
}
183+
160184
#endif /* #ifdef BOARD_HAS_ETHERNET */

src/EthernetConnectionHandler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class EthernetConnectionHandler : public ConnectionHandler
5858
virtual Client & getClient() override{ return _eth_client; }
5959
virtual UDP & getUDP() override { return _eth_udp; }
6060

61+
int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1) override;
62+
int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1) override;
63+
int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1) override;
64+
6165
protected:
6266

6367
virtual NetworkConnectionState update_handleInit () override;

src/GSMConnectionHandler.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
105105
}
106106

107107
DEBUG_INFO(F("Sending PING to outer space..."));
108-
int const ping_result = _gprs.ping("time.arduino.cc");
108+
int const ping_result = ping("time.arduino.cc");
109109
DEBUG_INFO(F("GPRS.ping(): %d"), ping_result);
110110
if (ping_result < 0)
111111
{
@@ -144,8 +144,20 @@ NetworkConnectionState GSMConnectionHandler::update_handleDisconnected()
144144
}
145145
else
146146
{
147-
return NetworkConnectionState::CLOSED;
147+
return NetworkConnectionState::CLOSED;
148148
}
149149
}
150150

151+
int GSMConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
152+
return _gprs.ping(ip);
153+
}
154+
155+
int GSMConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
156+
return _gprs.ping(hostname);
157+
}
158+
159+
int GSMConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
160+
return _gprs.ping(host);
161+
}
162+
151163
#endif /* #ifdef BOARD_HAS_GSM */

src/GSMConnectionHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class GSMConnectionHandler : public ConnectionHandler
4040
virtual Client & getClient() override { return _gsm_client; };
4141
virtual UDP & getUDP() override { return _gsm_udp; };
4242

43+
int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1) override;
44+
int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1) override;
45+
int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1) override;
4346

4447
protected:
4548

src/GenericConnectionHandler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ void GenericConnectionHandler::setKeepAlive(bool keep_alive) {
115115
}
116116
}
117117

118+
#if !defined(BOARD_HAS_LORA)
119+
int GenericConnectionHandler::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
120+
return _ch != nullptr ? _ch->ping(ip, ttl, count) : 0;
121+
}
122+
123+
int GenericConnectionHandler::ping(const String &hostname, uint8_t ttl, uint8_t count) {
124+
return _ch != nullptr ? _ch->ping(hostname, ttl, count) : 0;
125+
}
126+
127+
int GenericConnectionHandler::ping(const char* host, uint8_t ttl, uint8_t count) {
128+
return _ch != nullptr ? _ch->ping(host, ttl, count) : 0;
129+
}
130+
#endif // !defined(BOARD_HAS_LORA)
131+
118132
static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) {
119133
switch(adapter) {
120134
#if defined(BOARD_HAS_WIFI)

0 commit comments

Comments
 (0)