Skip to content

Commit

Permalink
Use std::optional<timeout> instead of magic zero value
Browse files Browse the repository at this point in the history
  • Loading branch information
hasty committed Aug 8, 2024
1 parent fc9429a commit 072f0e3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
5 changes: 3 additions & 2 deletions examples/thermostat/linux/include/thermostat-delegate-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class ThermostatDelegate : public Delegate
public:
static inline ThermostatDelegate & GetInstance() { return sInstance; }

System::Clock::Milliseconds16 GetAtomicWriteTimeout(DataModel::DecodableList<chip::AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest) override;
std::optional<System::Clock::Milliseconds16>
GetAtomicWriteTimeout(DataModel::DecodableList<chip::AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest) override;

CHIP_ERROR GetPresetTypeAtIndex(size_t index, Structs::PresetTypeStruct::Type & presetType) override;

Expand Down
5 changes: 3 additions & 2 deletions examples/thermostat/linux/thermostat-delegate-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ CHIP_ERROR ThermostatDelegate::SetActivePresetHandle(const DataModel::Nullable<B
return CHIP_NO_ERROR;
}

System::Clock::Milliseconds16 ThermostatDelegate::GetAtomicWriteTimeout(DataModel::DecodableList<AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest)
std::optional<System::Clock::Milliseconds16>
ThermostatDelegate::GetAtomicWriteTimeout(DataModel::DecodableList<AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest)
{
auto attributeIdsIter = attributeRequests.begin();
bool requestedPresets = false, requestedSchedules = false;
Expand Down
5 changes: 3 additions & 2 deletions src/app/clusters/thermostat-server/thermostat-delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class Delegate
* @param[out] timeoutRequest The timeout proposed by the client.
* @return The maximum allowed timeout; zero if the request is invalid.
*/
virtual System::Clock::Milliseconds16 GetAtomicWriteTimeout(DataModel::DecodableList<AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest) = 0;
virtual std::optional<System::Clock::Milliseconds16>
GetAtomicWriteTimeout(DataModel::DecodableList<AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest) = 0;

/**
* @brief Get the preset type at a given index in the PresetTypes attribute
Expand Down
11 changes: 6 additions & 5 deletions src/app/clusters/thermostat-server/thermostat-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,17 +1418,18 @@ void handleAtomicBegin(CommandHandler * commandObj, const ConcreteCommandPath &
// needs to keep track of a pending preset list now.
delegate->InitializePendingPresets();

System::Clock::Milliseconds16 timeout =
auto timeout =
delegate->GetAtomicWriteTimeout(commandData.attributeRequests, System::Clock::Milliseconds16(commandData.timeout.Value()));

if (timeout == System::Clock::Milliseconds16(0))
if (!timeout.has_value())
{
commandObj->AddStatus(commandPath, imcode::InvalidCommand);
return;
}
ScheduleTimer(endpoint, timeout);
gThermostatAttrAccess.SetAtomicWrite(endpoint, GetSourceScopedNodeId(commandObj), true);
sendAtomicResponse(commandObj, commandPath, imcode::Success, imcode::Success, imcode::Success, MakeOptional(timeout.count()));
ScheduleTimer(endpoint, timeout.value());
gThermostatAttrAccess.SetAtomicWrite(endpoint, GetSourceScopedNodeId(commandObj), kAtomicWriteState_Open);
sendAtomicResponse(commandObj, commandPath, imcode::Success, imcode::Success, imcode::Success,
MakeOptional(timeout.value().count()));
}

imcode commitPresets(Delegate * delegate, EndpointId endpoint)
Expand Down

0 comments on commit 072f0e3

Please sign in to comment.