Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cffdbbd

Browse files
committedApr 4, 2024·
- simplify span usage
- encode mutablecharspan instead of creating a new charspan - encode null when GetLocalTime() fails - simplify granularity validity check - change size comparison to compare against actual buffer size instead of attribute list size macro - use unknown enum to check for invalid enums - minor logging changes
1 parent a7c9a7b commit cffdbbd

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed
 

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

+32-35
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ static bool emitDSTTableEmptyEvent(EndpointId ep)
144144

145145
if (CHIP_NO_ERROR != error)
146146
{
147-
ChipLogError(Zcl, "Unable to emit DSTTableEmpty event [ep=%d]", ep);
147+
ChipLogError(Zcl, "DSTTableEmptyEvent failed");
148148
return false;
149149
}
150-
ChipLogProgress(Zcl, "Emit DSTTableEmpty event [ep=%d]", ep);
150+
ChipLogProgress(Zcl, "DSTTableEmptyEvent");
151151

152152
// TODO: re-schedule event for after min 1hr https://github.com/project-chip/connectedhomeip/issues/27200
153153
// delegate->scheduleDSTTableEmptyEvent()
@@ -164,11 +164,11 @@ static bool emitDSTStatusEvent(EndpointId ep, bool dstOffsetActive)
164164

165165
if (CHIP_NO_ERROR != error)
166166
{
167-
ChipLogError(Zcl, "Unable to emit DSTStatus event [ep=%d]", ep);
167+
ChipLogError(Zcl, "DSTStatusEvent failed");
168168
return false;
169169
}
170170

171-
ChipLogProgress(Zcl, "Emit DSTStatus event [ep=%d]", ep);
171+
ChipLogProgress(Zcl, "DSTStatusEvent active: %d", dstOffsetActive);
172172
return true;
173173
}
174174

@@ -190,11 +190,11 @@ static bool emitTimeZoneStatusEvent(EndpointId ep)
190190

191191
if (CHIP_NO_ERROR != error)
192192
{
193-
ChipLogError(Zcl, "Unable to emit TimeZoneStatus event [ep=%d]", ep);
193+
ChipLogError(Zcl, "TimeZoneStatusEvent failed");
194194
return false;
195195
}
196196

197-
ChipLogProgress(Zcl, "Emit TimeZoneStatus event [ep=%d]", ep);
197+
ChipLogProgress(Zcl, "TimeZoneStatusEvent offset: %d", static_cast<int>(tz.offset));
198198
return true;
199199
}
200200

@@ -207,13 +207,13 @@ static bool emitTimeFailureEvent(EndpointId ep)
207207

208208
if (CHIP_NO_ERROR != error)
209209
{
210-
ChipLogError(Zcl, "Unable to emit TimeFailure event [ep=%d]", ep);
210+
ChipLogError(Zcl, "TimeFailureEvent failed");
211211
return false;
212212
}
213213

214214
// TODO: re-schedule event for after min 1hr if no time is still available
215215
// https://github.com/project-chip/connectedhomeip/issues/27200
216-
ChipLogProgress(Zcl, "Emit TimeFailure event [ep=%d]", ep);
216+
ChipLogProgress(Zcl, "TimeFailureEvent");
217217
return true;
218218
}
219219

@@ -226,13 +226,13 @@ static bool emitMissingTrustedTimeSourceEvent(EndpointId ep)
226226

227227
if (CHIP_NO_ERROR != error)
228228
{
229-
ChipLogError(Zcl, "Unable to emit MissingTrustedTimeSource event [ep=%d]", ep);
229+
ChipLogError(Zcl, "Unable to emit MissingTrustedTimeSource event");
230230
return false;
231231
}
232232

233233
// TODO: re-schedule event for after min 1hr if TTS is null or cannot be reached
234234
// https://github.com/project-chip/connectedhomeip/issues/27200
235-
ChipLogProgress(Zcl, "Emit MissingTrustedTimeSource event [ep=%d]", ep);
235+
ChipLogProgress(Zcl, "Emit MissingTrustedTimeSource event");
236236
return true;
237237
}
238238

@@ -536,10 +536,6 @@ CHIP_ERROR TimeSynchronizationServer::SetTimeZone(const DataModel::DecodableList
536536
size_t items;
537537
VerifyOrReturnError(CHIP_NO_ERROR == tzL.ComputeSize(&items), CHIP_IM_GLOBAL_STATUS(InvalidCommand));
538538

539-
if (items > CHIP_CONFIG_TIME_ZONE_LIST_MAX_SIZE)
540-
{
541-
return CHIP_ERROR_BUFFER_TOO_SMALL;
542-
}
543539
if (items == 0)
544540
{
545541
return ClearTimeZone();
@@ -564,6 +560,12 @@ CHIP_ERROR TimeSynchronizationServer::SetTimeZone(const DataModel::DecodableList
564560
uint8_t i = 0;
565561
InitTimeZone();
566562

563+
if (items > mTimeZoneObj.timeZoneList.size())
564+
{
565+
LoadTimeZone();
566+
return CHIP_ERROR_BUFFER_TOO_SMALL;
567+
}
568+
567569
while (newTzL.Next())
568570
{
569571
auto & tzStore = mTimeZoneObj.timeZoneList[i];
@@ -589,20 +591,14 @@ CHIP_ERROR TimeSynchronizationServer::SetTimeZone(const DataModel::DecodableList
589591
tzStore.timeZone.validAt = newTz.validAt;
590592
if (newTz.name.HasValue() && newTz.name.Value().size() > 0)
591593
{
592-
size_t len = newTz.name.Value().size();
593-
if (len > sizeof(tzStore.name))
594-
{
595-
ReturnErrorOnFailure(LoadTimeZone());
596-
return CHIP_ERROR_IM_MALFORMED_COMMAND_DATA_IB;
597-
}
598594
memset(tzStore.name, 0, sizeof(tzStore.name));
599-
chip::MutableCharSpan tempSpan(tzStore.name, len);
595+
chip::MutableCharSpan tempSpan(tzStore.name);
600596
if (CHIP_NO_ERROR != CopyCharSpanToMutableCharSpan(newTz.name.Value(), tempSpan))
601597
{
602598
ReturnErrorOnFailure(LoadTimeZone());
603599
return CHIP_IM_GLOBAL_STATUS(InvalidCommand);
604600
}
605-
tzStore.timeZone.name.SetValue(CharSpan(tzStore.name, len));
601+
tzStore.timeZone.name.SetValue(tempSpan);
606602
}
607603
else
608604
{
@@ -661,11 +657,6 @@ CHIP_ERROR TimeSynchronizationServer::SetDSTOffset(const DataModel::DecodableLis
661657
size_t items;
662658
VerifyOrReturnError(CHIP_NO_ERROR == dstL.ComputeSize(&items), CHIP_IM_GLOBAL_STATUS(InvalidCommand));
663659

664-
if (items > CHIP_CONFIG_DST_OFFSET_LIST_MAX_SIZE)
665-
{
666-
return CHIP_ERROR_BUFFER_TOO_SMALL;
667-
}
668-
669660
if (items == 0)
670661
{
671662
return ClearDSTOffset();
@@ -675,6 +666,12 @@ CHIP_ERROR TimeSynchronizationServer::SetDSTOffset(const DataModel::DecodableLis
675666
size_t i = 0;
676667
InitDSTOffset();
677668

669+
if (items > mDstOffsetObj.dstOffsetList.size())
670+
{
671+
LoadDSTOffset();
672+
return CHIP_ERROR_BUFFER_TOO_SMALL;
673+
}
674+
678675
while (newDstL.Next())
679676
{
680677
auto & dst = mDstOffsetObj.dstOffsetList[i];
@@ -975,7 +972,7 @@ CHIP_ERROR TimeSynchronizationAttrAccess::ReadDefaultNtp(EndpointId endpoint, At
975972
err = TimeSynchronizationServer::Instance().GetDefaultNtp(dntp);
976973
if (err == CHIP_NO_ERROR)
977974
{
978-
err = aEncoder.Encode(CharSpan(buffer, dntp.size()));
975+
err = aEncoder.Encode(dntp);
979976
}
980977
else if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
981978
{
@@ -1017,9 +1014,10 @@ CHIP_ERROR TimeSynchronizationAttrAccess::ReadDSTOffset(EndpointId endpoint, Att
10171014
CHIP_ERROR TimeSynchronizationAttrAccess::ReadLocalTime(EndpointId endpoint, AttributeValueEncoder & aEncoder)
10181015
{
10191016
DataModel::Nullable<uint64_t> localTime;
1020-
CHIP_ERROR err = TimeSynchronizationServer::Instance().GetLocalTime(endpoint, localTime);
1021-
err = aEncoder.Encode(localTime);
1022-
return err;
1017+
VerifyOrReturnError(CHIP_NO_ERROR == TimeSynchronizationServer::Instance().GetLocalTime(endpoint, localTime),
1018+
aEncoder.EncodeNull());
1019+
ReturnErrorOnFailure(aEncoder.Encode(localTime));
1020+
return CHIP_NO_ERROR;
10231021
}
10241022

10251023
CHIP_ERROR TimeSynchronizationAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
@@ -1090,19 +1088,18 @@ bool emberAfTimeSynchronizationClusterSetUTCTimeCallback(
10901088
const auto & timeSource = commandData.timeSource;
10911089

10921090
auto currentGranularity = TimeSynchronizationServer::Instance().GetGranularity();
1093-
if (granularity < GranularityEnum::kNoTimeGranularity || granularity > GranularityEnum::kMicrosecondsGranularity)
1091+
if (granularity == GranularityEnum::kUnknownEnumValue)
10941092
{
10951093
commandObj->AddStatus(commandPath, Status::InvalidCommand);
10961094
return true;
10971095
}
1098-
if (timeSource.HasValue() && (timeSource.Value() < TimeSourceEnum::kNone || timeSource.Value() > TimeSourceEnum::kGnss))
1096+
if (timeSource.HasValue() && timeSource.Value() == TimeSourceEnum::kUnknownEnumValue)
10991097
{
11001098
commandObj->AddStatus(commandPath, Status::InvalidCommand);
11011099
return true;
11021100
}
11031101

1104-
if (granularity != GranularityEnum::kNoTimeGranularity &&
1105-
(currentGranularity == GranularityEnum::kNoTimeGranularity || granularity >= currentGranularity) &&
1102+
if (granularity > currentGranularity &&
11061103
CHIP_NO_ERROR ==
11071104
TimeSynchronizationServer::Instance().SetUTCTime(commandPath.mEndpointId, utcTime, granularity, TimeSourceEnum::kAdmin))
11081105
{

0 commit comments

Comments
 (0)
Please sign in to comment.