Skip to content

Commit 24bfb2b

Browse files
committed
Using BufferReader / BufferWriter to avoid Endianness
Using BufferReader / BufferWriter to avoid Endianness issue
1 parent b1b1f82 commit 24bfb2b

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

examples/chef/common/clusters/resource-monitoring/chef-resource-monitoring-delegates.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <app/clusters/resource-monitoring-server/resource-monitoring-cluster-objects.h>
2121
#include <app/clusters/resource-monitoring-server/resource-monitoring-server.h>
2222
#include <lib/core/TLVReader.h>
23+
#include <lib/support/BufferReader.h>
24+
#include <lib/support/BufferWriter.h>
2325
#include <resource-monitoring/chef-resource-monitoring-delegates.h>
2426
#include <utility>
2527

@@ -123,12 +125,17 @@ ChefResourceMonitorInstance::ExternalAttributeWrite(const EmberAfAttributeMetada
123125
}
124126
break;
125127
case HepaFilterMonitoring::Attributes::LastChangedTime::Id: {
128+
// We already know the input is a buffer started with a uint16_t as the length
129+
chip::Encoding::LittleEndian::Reader bufReader(buffer, sizeof(uint16_t));
130+
uint16_t tlvLen;
131+
VerifyOrReturnError( CHIP_NO_ERROR == bufReader.Read16(&tlvLen).StatusCode(), Protocols::InteractionModel::Status::UnsupportedWrite);
132+
133+
// Read from TLV
126134
uint32_t newValue = 0;
127-
uint16_t tlvLen = *(uint16_t *) buffer;
128-
chip::TLV::TLVReader reader;
129-
reader.Init(buffer + sizeof(uint16_t), tlvLen);
130-
reader.Next();
131-
reader.Get(newValue);
135+
chip::TLV::TLVReader tlvReader;
136+
tlvReader.Init(buffer + sizeof(uint16_t), tlvLen);
137+
tlvReader.Next();
138+
tlvReader.Get(newValue);
132139
DataModel::Nullable<uint32_t> newLastChangedTime = DataModel::MakeNullable(newValue);
133140
ret = UpdateLastChangedTime(newLastChangedTime);
134141
}
@@ -163,7 +170,7 @@ chefResourceMonitoringExternalWriteCallback(chip::EndpointId endpoint, chip::Clu
163170
{
164171
Protocols::InteractionModel::Status ret = Protocols::InteractionModel::Status::Success;
165172
AttributeId attributeId = attributeMetadata->attributeId;
166-
ChipLogProgress(Zcl, "chefResourceMonitoringExternalWriteCallback EP: %d, Cluster: %d, Att: %d", static_cast<int>(endpoint),
173+
ChipLogProgress(Zcl, "chefResourceMonitoringExternalWriteCallback EP: %d, Cluster: %04x, Att: %04x", static_cast<int>(endpoint),
167174
static_cast<int>(clusterId), static_cast<int>(attributeId));
168175

169176
switch (clusterId)
@@ -232,8 +239,11 @@ ChefResourceMonitorInstance::ExternalAttributeRead(const EmberAfAttributeMetadat
232239
}
233240
break;
234241
case HepaFilterMonitoring::Attributes::LastChangedTime::Id: {
242+
// Only LastChangedTime needs to handle Endianness
235243
DataModel::Nullable<uint32_t> lastChangedTime = GetLastChangedTime();
236-
*(uint32_t *) buffer = lastChangedTime.IsNull() ? 0 : lastChangedTime.Value();
244+
chip::Encoding::LittleEndian::BufferWriter bufWriter(buffer, sizeof(uint16_t));
245+
246+
bufWriter.Put32(lastChangedTime.IsNull() ? 0 : lastChangedTime.Value());
237247
}
238248
break;
239249
case HepaFilterMonitoring::Attributes::DegradationDirection::Id:

0 commit comments

Comments
 (0)