Skip to content

Commit 6135a88

Browse files
committed
add define to enable/disable connection handler
1 parent 00f785a commit 6135a88

File tree

11 files changed

+121
-35
lines changed

11 files changed

+121
-35
lines changed

extras/test/include/Arduino.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
******************************************************************************/
2424

2525
typedef std::string String;
26+
typedef struct {
27+
void * udp;
28+
} UDP;
2629

2730
/******************************************************************************
2831
FUNCTION PROTOTYPES

src/AIoTC_Config.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@
148148
#define BOARD_STM32H7
149149
#endif
150150

151-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) \
152-
|| defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33)
151+
#define CONNECTION_HANDLER_ENABLED (1)
152+
153+
#if ((defined(BOARD_STM32H7) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33)) &&\
154+
(defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1)))
153155
#define NETWORK_CONFIGURATOR_ENABLED (1)
154156
#else
155157
#define NETWORK_CONFIGURATOR_ENABLED (0)

src/ArduinoIoTCloud.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
******************************************************************************/
2727

2828
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
29-
: _connection{nullptr}
29+
: _time_service(TimeService)
30+
#if CONNECTION_HANDLER_ENABLED
31+
,_connection{nullptr}
32+
#endif
3033
#if NETWORK_CONFIGURATOR_ENABLED
3134
, _configurator{nullptr}
3235
#endif
33-
, _time_service(TimeService)
3436
, _thing_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
3537
, _lib_version{AIOT_CONFIG_LIB_VERSION}
3638
, _device_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

src/ArduinoIoTCloud.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
#include <AIoTC_Config.h>
2626

27-
#include <Arduino_ConnectionHandler.h>
27+
#if CONNECTION_HANDLER_ENABLED
28+
#include <Arduino_ConnectionHandler.h>
29+
#endif
30+
2831
#if NETWORK_CONFIGURATOR_ENABLED
2932
#include <Arduino_NetworkConfigurator.h>
3033
#endif
@@ -99,14 +102,16 @@ class ArduinoIoTCloudClass
99102
inline void setDeviceId(String const device_id) { _device_id = device_id; };
100103
inline String & getDeviceId() { return _device_id; };
101104

105+
#if CONNECTION_HANDLER_ENABLED
102106
inline ConnectionHandler * getConnection() { return _connection; }
107+
#endif
103108

104109
inline unsigned long getInternalTime() { return _time_service.getTime(); }
105110
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
106111

107-
#if NETWORK_CONFIGURATOR_ENABLED
112+
#if NETWORK_CONFIGURATOR_ENABLED
108113
inline void setConfigurator(NetworkConfiguratorClass & configurator) { _configurator = &configurator; }
109-
#endif
114+
#endif
110115
void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
111116

112117
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
@@ -151,11 +156,13 @@ class ArduinoIoTCloudClass
151156

152157
protected:
153158

159+
TimeServiceClass & _time_service;
160+
#if CONNECTION_HANDLER_ENABLED
154161
ConnectionHandler * _connection;
155-
#if NETWORK_CONFIGURATOR_ENABLED
162+
#endif
163+
#if NETWORK_CONFIGURATOR_ENABLED
156164
NetworkConfiguratorClass * _configurator;
157-
#endif
158-
TimeServiceClass & _time_service;
165+
#endif
159166
String _thing_id;
160167
String _lib_version;
161168

src/ArduinoIoTCloudTCP.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
8080
* PUBLIC MEMBER FUNCTIONS
8181
******************************************************************************/
8282

83+
#if CONNECTION_HANDLER_ENABLED
8384
int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
8485
{
8586
_connection = &connection;
@@ -89,6 +90,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWa
8990
return begin(_connection->getClient(), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect);
9091
#endif
9192
}
93+
#endif
9294

9395
int ArduinoIoTCloudTCP::begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect)
9496
{
@@ -342,8 +344,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init()
342344
_otaTLSClient.begin(_otaClient);
343345
#endif
344346

347+
#if CONNECTION_HANDLER_ENABLED
345348
/* Setup TimeService */
346-
_time_service.begin(_connection);
349+
if (_connection != nullptr) {
350+
_time_service.begin(_connection);
351+
} else
352+
#endif
353+
_time_service.begin(_ntpClient);
347354

348355
/* Since we do not control what code the user inserts
349356
* between ArduinoIoTCloudTCP::begin() and the first
@@ -354,8 +361,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init()
354361
if (_enableWatchdog) {
355362
/* Initialize watchdog hardware */
356363
watchdog_enable();
357-
/* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/
358-
watchdog_enable_network_feed(_connection->getInterface());
364+
#if CONNECTION_HANDLER_ENABLED
365+
if (_connection != nullptr) {
366+
/* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/
367+
watchdog_enable_network_feed(_connection->getInterface());
368+
}
369+
#endif
359370
}
360371
#endif
361372

@@ -364,13 +375,20 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init()
364375

365376
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
366377
{
367-
if (_connection->check() == NetworkConnectionState::CONNECTED)
368-
{
369-
if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired()))
370-
return State::SyncTime;
378+
#if CONNECTION_HANDLER_ENABLED
379+
if (_connection == nullptr) {
380+
return State::SyncTime;
371381
}
372382

383+
if (_connection->check() == NetworkConnectionState::CONNECTED) {
384+
if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) {
385+
return State::SyncTime;
386+
}
387+
}
373388
return State::ConnectPhy;
389+
#else
390+
return State::SyncTime;
391+
#endif
374392
}
375393

376394
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
7474
virtual void printDebugInfo() override;
7575
virtual void disconnect () override;
7676

77+
#if CONNECTION_HANDLER_ENABLED
7778
int begin(ConnectionHandler& connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
79+
#endif
7880
int begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
7981
int begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true);
8082

src/tls/utility/TLSClientBroker.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#pragma once
1212

13-
#include <Arduino_ConnectionHandler.h>
1413
#include <AIoTC_Config.h>
14+
#if CONNECTION_HANDLER_ENABLED
15+
#include <Arduino_ConnectionHandler.h>
16+
#endif
1517

1618
enum class ArduinoIoTAuthenticationMode
1719
{

src/tls/utility/TLSClientOta.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
#pragma once
1212

13-
#include <Arduino_ConnectionHandler.h>
1413
#include <AIoTC_Config.h>
14+
#if CONNECTION_HANDLER_ENABLED
15+
#include <Arduino_ConnectionHandler.h>
16+
#endif
17+
1518

1619
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
1720
/*
@@ -69,6 +72,7 @@
6972
public:
7073
void begin(Client* client);
7174

75+
#if CONNECTION_HANDLER_ENABLED
7276
static inline Client& getNewClient(NetworkAdapter net) {
7377
switch(net) {
7478
#ifdef BOARD_HAS_WIFI
@@ -97,4 +101,5 @@
97101
#endif // BOARD_HAS_CELLULAR
98102
}
99103
}
104+
#endif // CONNECTION_HANDLER_ENABLED
100105
};

src/utility/time/TimeService.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,17 @@ static time_t const EPOCH = 0;
116116
**************************************************************************************/
117117

118118
TimeServiceClass::TimeServiceClass()
119-
: _con_hdl(nullptr)
120-
, _is_rtc_configured(false)
119+
: _is_rtc_configured(false)
121120
, _is_tz_configured(false)
122121
, _timezone_offset(24 * 60 * 60)
123122
, _timezone_dst_until(0)
124123
, _last_sync_tick(0)
125124
, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
126125
, _sync_func(nullptr)
126+
#if CONNECTION_HANDLER_ENABLED
127+
, _con_hdl(nullptr)
128+
#endif
129+
, _ntp_client(nullptr)
127130
{
128131

129132
}
@@ -132,9 +135,26 @@ TimeServiceClass::TimeServiceClass()
132135
* PUBLIC MEMBER FUNCTIONS
133136
**************************************************************************************/
134137

138+
#if CONNECTION_HANDLER_ENABLED
135139
void TimeServiceClass::begin(ConnectionHandler * con_hdl)
136140
{
137141
_con_hdl = con_hdl;
142+
#ifdef HAS_TCP
143+
begin(&_con_hdl->getUDP());
144+
#else
145+
begin();
146+
#endif
147+
}
148+
#endif
149+
150+
void TimeServiceClass::begin(UDP * ntp_client)
151+
{
152+
_ntp_client = ntp_client;
153+
begin();
154+
}
155+
156+
void TimeServiceClass::begin()
157+
{
138158
initRTC();
139159
#ifdef HAS_LORA
140160
setRTC(EPOCH_AT_COMPILE_TIME);
@@ -293,11 +313,12 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input)
293313
#if defined(HAS_NOTECARD) || defined(HAS_TCP)
294314
bool TimeServiceClass::connected()
295315
{
296-
if(_con_hdl == nullptr) {
297-
return false;
298-
} else {
316+
#if CONNECTION_HANDLER_ENABLED
317+
if(_con_hdl != nullptr) {
299318
return _con_hdl->check() == NetworkConnectionState::CONNECTED;
300319
}
320+
#endif
321+
return true; // If no connection handler is used, assume we are connected
301322
}
302323

303324
unsigned long TimeServiceClass::getRemoteTime()
@@ -308,25 +329,34 @@ unsigned long TimeServiceClass::getRemoteTime()
308329
* This is the most reliable time source and it will
309330
* ensure a correct behaviour of the library.
310331
*/
311-
if(_con_hdl->getInterface() != NetworkAdapter::CELL) {
312-
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
332+
bool use_ntp = true;
333+
#if CONNECTION_HANDLER_ENABLED
334+
if((_con_hdl != nullptr) && (_con_hdl->getInterface() != NetworkAdapter::CELL)) {
335+
use_ntp = false;
336+
}
337+
#endif
338+
if (use_ntp && (_ntp_client != nullptr)) {
339+
unsigned long const ntp_time = NTPUtils::getTime(*_ntp_client);
313340
if(isTimeValid(ntp_time)) {
314341
return ntp_time;
315342
}
316343
}
317344
DEBUG_WARNING("TimeServiceClass::%s cannot get time from NTP, fallback on connection handler", __FUNCTION__);
318345
#endif /* HAS_TCP */
319346

347+
#if CONNECTION_HANDLER_ENABLED
320348
/* As fallback if NTP request fails try to obtain the
321349
* network time using the connection handler.
322350
*/
323-
unsigned long const connection_time = _con_hdl->getTime();
324-
if(isTimeValid(connection_time)) {
325-
return connection_time;
351+
if (_con_hdl != nullptr) {
352+
unsigned long const connection_time = _con_hdl->getTime();
353+
if(isTimeValid(connection_time)) {
354+
return connection_time;
355+
}
356+
DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__);
326357
}
327-
DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__);
358+
#endif
328359
}
329-
330360
/* Return known invalid value because we are not connected */
331361
return EPOCH;
332362
}

src/utility/time/TimeService.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
**************************************************************************************/
2424

2525
#include <AIoTC_Config.h>
26-
#include <Arduino_ConnectionHandler.h>
26+
#if CONNECTION_HANDLER_ENABLED
27+
#include <Arduino_ConnectionHandler.h>
28+
#endif
2729

2830
/******************************************************************************
2931
* TYPEDEF
@@ -42,7 +44,11 @@ class TimeServiceClass
4244

4345
TimeServiceClass();
4446

45-
void begin (ConnectionHandler * con_hdl);
47+
#if CONNECTION_HANDLER_ENABLED
48+
void begin(ConnectionHandler * con_hdl);
49+
#endif
50+
void begin(UDP * ntp_client);
51+
void begin();
4652
unsigned long getTime();
4753
void setTime(unsigned long time);
4854
unsigned long getLocalTime();
@@ -59,14 +65,18 @@ class TimeServiceClass
5965
static bool isTimeValid(unsigned long const time);
6066

6167
private:
62-
ConnectionHandler * _con_hdl;
68+
6369
bool _is_rtc_configured;
6470
bool _is_tz_configured;
6571
long _timezone_offset;
6672
unsigned long _timezone_dst_until;
6773
unsigned long _last_sync_tick;
6874
unsigned long _sync_interval_ms;
6975
syncTimeFunctionPtr _sync_func;
76+
#if CONNECTION_HANDLER_ENABLED
77+
ConnectionHandler * _con_hdl;
78+
#endif
79+
UDP * _ntp_client;
7080

7181
#if defined(HAS_NOTECARD) || defined(HAS_TCP)
7282
unsigned long getRemoteTime();

src/utility/watchdog/Watchdog.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* INCLUDE
2323
******************************************************************************/
2424

25-
#include <Arduino_ConnectionHandler.h>
25+
#include <AIoTC_Config.h>
26+
#if CONNECTION_HANDLER_ENABLED
27+
#include <Arduino_ConnectionHandler.h>
28+
#endif
2629

2730
/******************************************************************************
2831
* FUNCTION DECLARATION
@@ -31,7 +34,9 @@
3134
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
3235
void watchdog_enable();
3336
void watchdog_reset();
37+
#if CONNECTION_HANDLER_ENABLED
3438
void watchdog_enable_network_feed(NetworkAdapter ni);
39+
#endif
3540
#endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */
3641

3742
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)