Skip to content

Commit f1b1a23

Browse files
committed
Simplifying implementation for retrieving the connection member variable for the static non-member 'getTime' function by exploiting the fact that ArduinoIoTCloudClass is anyway a global variable within the file
1 parent e418dbc commit f1b1a23

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ const static int CONNECT_SUCCESS = 1;
4040
const static int CONNECT_FAILURE = 0;
4141
const static int CONNECT_FAILURE_SUBSCRIBE = -1;
4242

43-
static ConnectionHandler *getTimeConnection = NULL;
44-
4543
static unsigned long getTime() {
46-
if (!getTimeConnection) {
44+
if (!ArduinoCloud.getConnection()) {
4745
return 0;
4846
}
49-
unsigned long time = getTimeConnection->getTime();
47+
ConnectionHandler * connection = ArduinoCloud.getConnection();
48+
unsigned long time = connection->getTime();
5049
Debug.print(DBG_DEBUG, "NTP time: %lu", time);
5150
if (!NTPUtils::isTimeValid(time)) {
5251
Debug.print(DBG_ERROR, "Bogus NTP time from API, fallback to UDP method");
53-
time = NTPUtils(getTimeConnection->getUDP()).getTime();
52+
time = NTPUtils(connection->getUDP()).getTime();
5453
}
5554
#ifdef ARDUINO_ARCH_SAMD
5655
rtc.setEpoch(time);
@@ -148,6 +147,8 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
148147
Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure.");
149148
return 0;
150149
}
150+
151+
ArduinoBearSSL.onGetTime(getTime);
151152
#endif /* BOARD_HAS_ECCX08 */
152153

153154
if (_sslClient) {
@@ -163,7 +164,6 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
163164
}
164165
#elif defined(BOARD_ESP)
165166
_sslClient = new WiFiClientSecure();
166-
Debug.print(DBG_VERBOSE, "new WiFiClientSecure()");
167167
#endif
168168

169169
#ifdef BOARD_HAS_ECCX08
@@ -178,16 +178,6 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
178178
_mqttClient->setUsernamePassword(_device_id, _password);
179179
#endif
180180

181-
// Bind ArduinoBearSSL callback using static "non-method" function
182-
if (_connection != NULL) {
183-
getTimeConnection = _connection;
184-
#ifdef BOARD_HAS_ECCX08
185-
ArduinoBearSSL.onGetTime(getTime);
186-
#endif
187-
}
188-
189-
// TODO: Find a better way to allow callback into object method
190-
// Begin function for the MQTTClient
191181
mqttClientBegin();
192182

193183
Thing.begin();

src/ArduinoIoTCloud.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class ArduinoIoTCloudClass {
113113
inline String getDeviceId() const {
114114
return _device_id;
115115
};
116+
inline ConnectionHandler * getConnection() {
117+
return _connection;
118+
}
116119

117120
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
118121

0 commit comments

Comments
 (0)