Skip to content

Commit f027069

Browse files
Apply suggestions from code review
Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 9c511bf commit f027069

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

examples/thermostat/linux/include/thermostat-delegate-impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace Thermostat {
2828
/**
2929
* The ThermostatDelegate class serves as the instance delegate for storing Presets related information and providing it to the
3030
* Thermostat server code. It also manages the presets attribute and provides methods to write to presets, edit presets, maintain a
31-
* pending presets list and either commit the presets when requested or discard the changes. It also provide API's to get and set
31+
* pending presets list and either commit the presets when requested or discard the changes. It also provides APIs to get and set
3232
* the attribute values.
3333
*
3434
*/
3535

3636
static constexpr uint8_t kMaxNumberOfPresetTypes = 6;
3737

3838
// We will support only one preset of each preset type.
39-
static constexpr uint8_t kMaxNumberOfPresetTypesOfEachType = 1;
39+
static constexpr uint8_t kMaxNumberOfPresetsOfEachType = 1;
4040

4141
class ThermostatDelegate : public Delegate
4242
{

examples/thermostat/linux/thermostat-delegate-impl.cpp

+5-13
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ThermostatDelegate::ThermostatDelegate()
5656
InitializePresetTypes();
5757
InitializePresets();
5858

59-
memset(mActivePresetHandleData, 0, kPresetHandleSize);
59+
memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData));
6060
mActivePresetHandleDataSize = 0;
6161
}
6262

@@ -83,7 +83,7 @@ void ThermostatDelegate::InitializePresetTypes()
8383

8484
void ThermostatDelegate::InitializePresets()
8585
{
86-
// Initilaize the presets with 2 built in presets - occupied and unoccupied.
86+
// Initialize the presets with 2 built in presets - occupied and unoccupied.
8787
PresetScenarioEnum presetScenarioEnumArray[2] = { PresetScenarioEnum::kOccupied, PresetScenarioEnum::kUnoccupied };
8888
static_assert(ArraySize(presetScenarioEnumArray) <= ArraySize(mPresets));
8989

@@ -136,23 +136,15 @@ CHIP_ERROR ThermostatDelegate::GetPresetAtIndex(size_t index, PresetStructWithOw
136136

137137
CHIP_ERROR ThermostatDelegate::GetActivePresetHandle(MutableByteSpan & activePresetHandle)
138138
{
139-
if (mActivePresetHandleDataSize > 0)
140-
{
141-
CopySpanToMutableSpan(ByteSpan(mActivePresetHandleData, mActivePresetHandleDataSize), activePresetHandle);
142-
}
143-
else
144-
{
145-
activePresetHandle.reduce_size(0);
146-
}
147-
return CHIP_NO_ERROR;
139+
return CopySpanToMutableSpan(ByteSpan(mActivePresetHandleData, mActivePresetHandleDataSize), activePresetHandle);
148140
}
149141

150142
CHIP_ERROR ThermostatDelegate::SetActivePresetHandle(const DataModel::Nullable<ByteSpan> & newActivePresetHandle)
151143
{
152144
if (!newActivePresetHandle.IsNull())
153145
{
154146
size_t newActivePresetHandleSize = newActivePresetHandle.Value().size();
155-
if (newActivePresetHandleSize > kPresetHandleSize)
147+
if (newActivePresetHandleSize > sizeof(mActivePresetHandleData))
156148
{
157149
ChipLogError(NotSpecified,
158150
"Failed to set ActivePresetHandle. newActivePresetHandle size %u is larger than preset handle size %u",
@@ -166,7 +158,7 @@ CHIP_ERROR ThermostatDelegate::SetActivePresetHandle(const DataModel::Nullable<B
166158
}
167159
else
168160
{
169-
memset(mActivePresetHandleData, 0, kPresetHandleSize);
161+
memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData));
170162
mActivePresetHandleDataSize = 0;
171163
ChipLogDetail(NotSpecified, "Clear ActivePresetHandle");
172164
}

examples/thermostat/linux/thermostat-manager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ int16_t ThermostatManager::GetCurrentTemperature()
289289
DataModel::Nullable<int16_t> currentTemperature;
290290
currentTemperature.SetNull();
291291
LocalTemperature::Get(kThermostatEndpoint, currentTemperature);
292-
return (!currentTemperature.IsNull()) ? currentTemperature.Value() : 0;
292+
return currentTemperature.ValueOr(0);
293293
}
294294

295295
int16_t ThermostatManager::GetCurrentHeatingSetPoint()

src/app/clusters/thermostat-server/PresetStructWithOwnedMembers.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ CHIP_ERROR PresetStructWithOwnedMembers::SetName(const Optional<DataModel::Nulla
8686
if (newNameSize > kPresetNameSize)
8787
{
8888
ChipLogError(Zcl, "Failed to set Preset name. New name size (%u) > allowed preset name size (%u)",
89-
static_cast<uint8_t>(newNameSize), static_cast<uint8_t>(kPresetNameSize));
89+
static_cast<unsigned>(newNameSize), static_cast<unsigned>(kPresetNameSize));
9090
return CHIP_ERROR_NO_MEMORY;
9191
}
9292
MutableCharSpan targetSpan(presetNameData);

src/app/clusters/thermostat-server/thermostat-server.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ Delegate * GetDelegate(EndpointId endpoint)
9292
*/
9393
bool IsValidPresetEntry(const PresetStruct::Type & preset)
9494
{
95-
// If the presetHandle is not null, the size of the handle does not exceed 16 bytes, return true.
95+
// Check that the preset handle is not too long.
9696
if (!preset.presetHandle.IsNull() && preset.presetHandle.Value().size() > kPresetHandleSize)
9797
{
9898
return false;
9999
}
100100

101-
// Return true if the preset scenario is valid, false otherwise.
101+
// Ensure we have a valid PresetScenario.
102102
return (preset.presetScenario != PresetScenarioEnum::kUnknownEnumValue);
103103
}
104104

@@ -419,7 +419,7 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate)
419419
*
420420
* @return true if the presetScenario is found, false otherwise.
421421
*/
422-
bool FindPresetScenarioInPresetTypes(Delegate * delegate, PresetScenarioEnum presetScenario)
422+
bool PresetScenarioExistsInPresetTypes(Delegate * delegate, PresetScenarioEnum presetScenario)
423423
{
424424
VerifyOrReturnValue(delegate != nullptr, false);
425425

@@ -786,7 +786,7 @@ CHIP_ERROR ThermostatAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
786786
Delegate * delegate = GetDelegate(aPath.mEndpointId);
787787
VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INCORRECT_STATE, ChipLogError(Zcl, "Delegate is null"));
788788

789-
ReturnErrorOnFailure(aEncoder.Encode(gThermostatAttrAccess.GetPresetsEditable(aPath.mEndpointId)));
789+
ReturnErrorOnFailure(aEncoder.Encode(GetPresetsEditable(aPath.mEndpointId)));
790790
}
791791
break;
792792
case ActivePresetHandle::Id: {
@@ -855,15 +855,15 @@ CHIP_ERROR ThermostatAttrAccess::Write(const ConcreteDataAttributePath & aPath,
855855
VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INCORRECT_STATE, ChipLogError(Zcl, "Delegate is null"));
856856

857857
// Presets are not editable, return INVALID_IN_STATE.
858-
VerifyOrReturnError(gThermostatAttrAccess.GetPresetsEditable(endpoint), CHIP_IM_GLOBAL_STATUS(InvalidInState),
858+
VerifyOrReturnError(GetPresetsEditable(endpoint), CHIP_IM_GLOBAL_STATUS(InvalidInState),
859859
ChipLogError(Zcl, "Presets are not editable"));
860860

861861
// Check if the OriginatorScopedNodeId at the endpoint is the same as the node editing the presets,
862862
// otherwise return BUSY.
863863
const Access::SubjectDescriptor subjectDescriptor = aDecoder.GetSubjectDescriptor();
864864
ScopedNodeId scopedNodeId = ScopedNodeId(subjectDescriptor.subject, subjectDescriptor.fabricIndex);
865865

866-
if (gThermostatAttrAccess.GetOriginatorScopedNodeId(endpoint) != scopedNodeId)
866+
if (GetOriginatorScopedNodeId(endpoint) != scopedNodeId)
867867
{
868868
ChipLogError(Zcl, "Another node is editing presets. Server is busy. Try again later");
869869
return CHIP_IM_GLOBAL_STATUS(Busy);
@@ -1273,7 +1273,7 @@ bool emberAfThermostatClusterSetActivePresetRequestCallback(
12731273
if (err != CHIP_NO_ERROR)
12741274
{
12751275
ChipLogError(Zcl, "Failed to set ActivePresetHandle with error %" CHIP_ERROR_FORMAT, err.Format());
1276-
commandObj->AddStatus(commandPath, imcode::Failure);
1276+
commandObj->AddStatus(commandPath, StatusIB(err).mStatus);
12771277
return true;
12781278
}
12791279

@@ -1509,7 +1509,7 @@ bool emberAfThermostatClusterCommitPresetsSchedulesRequestCallback(
15091509
return SendResponseAndCleanUp(delegate, endpoint, commandObj, commandPath, imcode::ConstraintError);
15101510
}
15111511

1512-
// If the preset type for the preset scenario does not supports names and a name is specified, return CONSTRAINT_ERROR.
1512+
// If the preset type for the preset scenario does not supports name and a name is specified, return CONSTRAINT_ERROR.
15131513
if (!PresetTypeSupportsNames(delegate, presetScenario) && pendingPreset.GetName().HasValue())
15141514
{
15151515
return SendResponseAndCleanUp(delegate, endpoint, commandObj, commandPath, imcode::ConstraintError);
@@ -1522,7 +1522,7 @@ bool emberAfThermostatClusterCommitPresetsSchedulesRequestCallback(
15221522
pendingPreset.SetCoolingSetpoint(MakeOptional(EnforceCoolingSetpointLimits(coolingSetpointValue.Value(), endpoint)));
15231523
}
15241524

1525-
Optional<int16_t> heatingSetpointValue = preset.GetHeatingSetpoint();
1525+
Optional<int16_t> heatingSetpointValue = pendingPreset.GetHeatingSetpoint();
15261526
if (heatingSetpointValue.HasValue())
15271527
{
15281528
pendingPreset.SetHeatingSetpoint(MakeOptional(EnforceHeatingSetpointLimits(heatingSetpointValue.Value(), endpoint)));

0 commit comments

Comments
 (0)