Skip to content

Commit 8fe1fac

Browse files
authored
Adding a function to register a callback function for retrieving a global timestamp. (#55)
This is necessary because right now we are relying on the RTC within the SAMD MCU which is instantiated (RTCZero) within the ArduinoIoTCloud library and reference within ArduinoCloudThing via extern devlaration. Due to the extern binding this is a very brittle dependency which can be easily destroyed, it is therefore better to explicitly register a function which provides the time (this can be serviced by the TimeService class available in ArduinoIoTCloud
1 parent 12fc6bb commit 8fe1fac

4 files changed

+19
-4
lines changed

ArduinoCloudProperty.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ArduinoCloudProperty::ArduinoCloudProperty()
3939
_min_delta_property(0.0f),
4040
_min_time_between_updates_millis(0),
4141
_permission(Permission::Read),
42+
_get_time_func{nullptr},
4243
_update_callback_func(nullptr),
4344
_sync_callback_func(nullptr),
4445
_has_been_updated_once(false),
@@ -56,9 +57,10 @@ ArduinoCloudProperty::ArduinoCloudProperty()
5657
/******************************************************************************
5758
PUBLIC MEMBER FUNCTIONS
5859
******************************************************************************/
59-
void ArduinoCloudProperty::init(String const name, Permission const permission) {
60+
void ArduinoCloudProperty::init(String const name, Permission const permission, GetTimeCallbackFunc func) {
6061
_name = name;
6162
_permission = permission;
63+
_get_time_func = func;
6264
}
6365

6466
ArduinoCloudProperty & ArduinoCloudProperty::onUpdate(UpdateCallbackFunc func) {
@@ -259,7 +261,11 @@ String ArduinoCloudProperty::getAttributeName(String propertyName, char separato
259261

260262
void ArduinoCloudProperty::updateLocalTimestamp() {
261263
if (isReadableByCloud()) {
262-
_last_local_change_timestamp = getTimestamp();
264+
if (_get_time_func) {
265+
_last_local_change_timestamp = _get_time_func();
266+
} else {
267+
_last_local_change_timestamp = getTimestamp();
268+
}
263269
}
264270
}
265271

ArduinoCloudProperty.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ enum class UpdatePolicy {
122122
};
123123

124124
typedef void(*UpdateCallbackFunc)(void);
125+
typedef unsigned long(*GetTimeCallbackFunc)();
125126

126127
/******************************************************************************
127128
CLASS DECLARATION
@@ -131,7 +132,7 @@ class ArduinoCloudProperty {
131132
typedef void(*SyncCallbackFunc)(ArduinoCloudProperty &property);
132133
public:
133134
ArduinoCloudProperty();
134-
void init(String const name, Permission const permission);
135+
void init(String const name, Permission const permission, GetTimeCallbackFunc func);
135136

136137
/* Composable configuration of the ArduinoCloudProperty class */
137138
ArduinoCloudProperty & onUpdate(UpdateCallbackFunc func);
@@ -192,6 +193,7 @@ class ArduinoCloudProperty {
192193

193194
private:
194195
Permission _permission;
196+
GetTimeCallbackFunc _get_time_func;
195197
UpdateCallbackFunc _update_callback_func;
196198
void (*_sync_callback_func)(ArduinoCloudProperty &property);
197199

ArduinoCloudThing.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void PrintFreeRam(void) {
4444
******************************************************************************/
4545

4646
ArduinoCloudThing::ArduinoCloudThing() :
47+
_get_time_func{nullptr},
4748
_numPrimitivesProperties(0),
4849
_numProperties(0),
4950
_isSyncMessage(false),
@@ -59,6 +60,10 @@ ArduinoCloudThing::ArduinoCloudThing() :
5960
void ArduinoCloudThing::begin() {
6061
}
6162

63+
void ArduinoCloudThing::registerGetTimeCallbackFunc(GetTimeCallbackFunc func) {
64+
_get_time_func = func;
65+
}
66+
6267
int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPayload) {
6368

6469
// check if backing storage and cloud has diverged
@@ -87,7 +92,7 @@ int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPaylo
8792
}
8893

8994
ArduinoCloudProperty& ArduinoCloudThing::addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier) {
90-
property.init(name, permission);
95+
property.init(name, permission, _get_time_func);
9196
if (isPropertyInContainer(name)) {
9297
return (*getProperty(name));
9398
} else {

ArduinoCloudThing.h

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class ArduinoCloudThing {
7777
ArduinoCloudThing();
7878

7979
void begin();
80+
void registerGetTimeCallbackFunc(GetTimeCallbackFunc func);
8081
//if propertyIdentifier is different from -1, an integer identifier is associated to the added property to be use instead of the property name when the parameter lightPayload is true in the encode method
8182
ArduinoCloudProperty & addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier = -1);
8283

@@ -93,6 +94,7 @@ class ArduinoCloudThing {
9394
String getPropertyNameByIdentifier(int propertyIdentifier);
9495

9596
private:
97+
GetTimeCallbackFunc _get_time_func;
9698
LinkedList<ArduinoCloudProperty *> _property_list;
9799
/* Keep track of the number of primitive properties in the Thing. If 0 it allows the early exit in updateTimestampOnLocallyChangedProperties() */
98100
int _numPrimitivesProperties;

0 commit comments

Comments
 (0)