Skip to content

Commit f6b3594

Browse files
authored
Timesync improvements - part 1 (#32848)
* change cluster revision in light-switch-app * added delegate functions for better usability * - made AttemptToGetTimeFromTrustedNode() public API - free readclient memory after read is complete * added default implementation for some delegate functions * added documentation for delegate functions
1 parent 5570be9 commit f6b3594

File tree

7 files changed

+26
-35
lines changed

7 files changed

+26
-35
lines changed

examples/light-switch-app/light-switch-common/light-switch-app.matter

+1-1
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,7 @@ endpoint 0 {
27922792
callback attribute acceptedCommandList;
27932793
callback attribute attributeList;
27942794
ram attribute featureMap default = 0x0B;
2795-
ram attribute clusterRevision default = 1;
2795+
ram attribute clusterRevision default = 2;
27962796

27972797
handle command SetUTCTime;
27982798
handle command SetTrustedTimeSource;

examples/light-switch-app/light-switch-common/light-switch-app.zap

+1-1
Original file line numberDiff line numberDiff line change
@@ -3826,7 +3826,7 @@
38263826
"storageOption": "RAM",
38273827
"singleton": 0,
38283828
"bounded": 0,
3829-
"defaultValue": "1",
3829+
"defaultValue": "2",
38303830
"reportable": 1,
38313831
"minInterval": 1,
38323832
"maxInterval": 65534,

src/app/clusters/time-synchronization-server/DefaultTimeSyncDelegate.cpp

-22
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
using chip::TimeSyncDataProvider;
2525
using namespace chip::app::Clusters::TimeSynchronization;
2626

27-
void DefaultTimeSyncDelegate::TimeZoneListChanged(const Span<TimeSyncDataProvider::TimeZoneStore> timeZoneList)
28-
{
29-
// placeholder implementation
30-
}
31-
32-
bool DefaultTimeSyncDelegate::HandleUpdateDSTOffset(chip::CharSpan name)
33-
{
34-
// placeholder implementation
35-
return false;
36-
}
37-
3827
bool DefaultTimeSyncDelegate::IsNTPAddressValid(chip::CharSpan ntp)
3928
{
4029
// placeholder implementation
@@ -67,14 +56,3 @@ CHIP_ERROR DefaultTimeSyncDelegate::UpdateTimeFromPlatformSource(chip::Callback:
6756
}
6857
return CHIP_ERROR_NOT_IMPLEMENTED;
6958
}
70-
71-
CHIP_ERROR DefaultTimeSyncDelegate::UpdateTimeUsingNTPFallback(const CharSpan & fallbackNTP,
72-
chip::Callback::Callback<OnFallbackNTPCompletion> * callback)
73-
{
74-
return CHIP_ERROR_NOT_IMPLEMENTED;
75-
}
76-
77-
void DefaultTimeSyncDelegate::UTCTimeAvailabilityChanged(uint64_t time)
78-
{
79-
// placeholder implementation
80-
}

src/app/clusters/time-synchronization-server/DefaultTimeSyncDelegate.h

-5
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ class DefaultTimeSyncDelegate : public Delegate
2929

3030
public:
3131
DefaultTimeSyncDelegate() : Delegate(){};
32-
void TimeZoneListChanged(const Span<TimeSyncDataProvider::TimeZoneStore> timeZoneList) override;
33-
bool HandleUpdateDSTOffset(CharSpan name) override;
3432
bool IsNTPAddressValid(CharSpan ntp) override;
3533
bool IsNTPAddressDomain(CharSpan ntp) override;
3634
CHIP_ERROR UpdateTimeFromPlatformSource(chip::Callback::Callback<OnTimeSyncCompletion> * callback) override;
37-
CHIP_ERROR UpdateTimeUsingNTPFallback(const CharSpan & fallbackNTP,
38-
chip::Callback::Callback<OnFallbackNTPCompletion> * callback) override;
39-
void UTCTimeAvailabilityChanged(uint64_t time) override;
4035
};
4136

4237
} // namespace TimeSynchronization

src/app/clusters/time-synchronization-server/time-synchronization-delegate.h

+19-5
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ class Delegate
5656
*
5757
* @param timeZoneList new time zone list
5858
*/
59-
virtual void TimeZoneListChanged(const Span<TimeSyncDataProvider::TimeZoneStore> timeZoneList) = 0;
59+
virtual void TimeZoneListChanged(const Span<TimeSyncDataProvider::TimeZoneStore> timeZoneList) {}
6060
/**
6161
* @brief Give the delegate the chance to call SetDSTOffset on the TimeSynchronizationServer with a list of
6262
* DST offsets based on the provided time zone name. If the delegate does so, it should return true.
6363
* If the delegate does not want to set DST offsets based on the time zone, it should return false.
6464
*
6565
* @param name name of active time zone
6666
*/
67-
virtual bool HandleUpdateDSTOffset(const CharSpan name) = 0;
67+
virtual bool HandleUpdateDSTOffset(const CharSpan name) { return false; }
6868
/**
6969
* @brief Returns true if the provided string is a valid NTP address (either domain name or IPv6 address).
7070
*
@@ -104,12 +104,26 @@ class Delegate
104104
* a CHIP_ERROR.
105105
*/
106106
virtual CHIP_ERROR UpdateTimeUsingNTPFallback(const CharSpan & fallbackNTP,
107-
chip::Callback::Callback<OnFallbackNTPCompletion> * callback) = 0;
107+
chip::Callback::Callback<OnFallbackNTPCompletion> * callback)
108+
{
109+
return CHIP_ERROR_NOT_IMPLEMENTED;
110+
}
108111

109112
/**
110-
* @brief Signals application that UTCTime has changed through the timesync cluster.
113+
* @brief Signals application that UTCTime has changed through the timesync cluster. This gets called when
114+
* time is available for the first time or is updated. Therefore, @param time will always have a valid value.
115+
* The negative case of time being unavailable is handled by NotifyTimeFailure().
116+
*/
117+
virtual void UTCTimeAvailabilityChanged(uint64_t time) {}
118+
/**
119+
* @brief Signals application that a new trusted time source is available. The application can then decide
120+
* if it wants to attempt to query for time from this source.
121+
*/
122+
virtual void TrustedTimeSourceAvailabilityChanged(bool available, GranularityEnum granularity) {}
123+
/**
124+
* @brief Signals application that fetching time has failed. The reason is not relevant.
111125
*/
112-
virtual void UTCTimeAvailabilityChanged(uint64_t time) = 0;
126+
virtual void NotifyTimeFailure() {}
113127

114128
virtual ~Delegate() = default;
115129

src/app/clusters/time-synchronization-server/time-synchronization-server.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static bool emitTimeFailureEvent(EndpointId ep)
215215
// TODO: re-schedule event for after min 1hr if no time is still available
216216
// https://github.com/project-chip/connectedhomeip/issues/27200
217217
ChipLogProgress(Zcl, "Emit TimeFailure event [ep=%d]", ep);
218+
GetDelegate()->NotifyTimeFailure();
218219
return true;
219220
}
220221

@@ -356,6 +357,7 @@ void TimeSynchronizationServer::OnDone(ReadClient * apReadClient)
356357
SetUTCTime(kRootEndpointId, mTimeReadInfo->utcTime.Value(), ourGranularity, TimeSourceEnum::kNodeTimeCluster);
357358
if (err == CHIP_NO_ERROR)
358359
{
360+
mTimeReadInfo = nullptr;
359361
return;
360362
}
361363
}
@@ -504,6 +506,7 @@ CHIP_ERROR TimeSynchronizationServer::SetTrustedTimeSource(const DataModel::Null
504506
{
505507
AttemptToGetTime();
506508
}
509+
GetDelegate()->TrustedTimeSourceAvailabilityChanged(!mTrustedTimeSource.IsNull(), mGranularity);
507510
return err;
508511
}
509512

src/app/clusters/time-synchronization-server/time-synchronization-server.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class TimeSynchronizationServer : public FabricTable::Delegate
123123
// ReadClient::Callback functions
124124
void OnAttributeData(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData, const StatusIB & aStatus) override;
125125
void OnDone(ReadClient * apReadClient) override;
126+
127+
CHIP_ERROR AttemptToGetTimeFromTrustedNode();
126128
#endif
127129

128130
// Platform event handler functions
@@ -166,7 +168,6 @@ class TimeSynchronizationServer : public FabricTable::Delegate
166168

167169
// Called when the platform is set up - attempts to get time using the recommended source list in the spec.
168170
void AttemptToGetTime();
169-
CHIP_ERROR AttemptToGetTimeFromTrustedNode();
170171
// Attempts to get fallback NTP from the delegate (last available source)
171172
// If successful, the function will set mGranulatiry and the time source
172173
// If unsuccessful, it will emit a TimeFailure event.

0 commit comments

Comments
 (0)