From 24bfb2b11c5703b4fb7ccfc2fbf990a8b7842f49 Mon Sep 17 00:00:00 2001 From: erwinpan1 Date: Fri, 14 Jun 2024 10:18:25 +0800 Subject: [PATCH] Using BufferReader / BufferWriter to avoid Endianness Using BufferReader / BufferWriter to avoid Endianness issue --- .../chef-resource-monitoring-delegates.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/examples/chef/common/clusters/resource-monitoring/chef-resource-monitoring-delegates.cpp b/examples/chef/common/clusters/resource-monitoring/chef-resource-monitoring-delegates.cpp index fd6f3aa47150a0..fa9b5fe33bb280 100644 --- a/examples/chef/common/clusters/resource-monitoring/chef-resource-monitoring-delegates.cpp +++ b/examples/chef/common/clusters/resource-monitoring/chef-resource-monitoring-delegates.cpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -123,12 +125,17 @@ ChefResourceMonitorInstance::ExternalAttributeWrite(const EmberAfAttributeMetada } break; case HepaFilterMonitoring::Attributes::LastChangedTime::Id: { + // We already know the input is a buffer started with a uint16_t as the length + chip::Encoding::LittleEndian::Reader bufReader(buffer, sizeof(uint16_t)); + uint16_t tlvLen; + VerifyOrReturnError( CHIP_NO_ERROR == bufReader.Read16(&tlvLen).StatusCode(), Protocols::InteractionModel::Status::UnsupportedWrite); + + // Read from TLV uint32_t newValue = 0; - uint16_t tlvLen = *(uint16_t *) buffer; - chip::TLV::TLVReader reader; - reader.Init(buffer + sizeof(uint16_t), tlvLen); - reader.Next(); - reader.Get(newValue); + chip::TLV::TLVReader tlvReader; + tlvReader.Init(buffer + sizeof(uint16_t), tlvLen); + tlvReader.Next(); + tlvReader.Get(newValue); DataModel::Nullable newLastChangedTime = DataModel::MakeNullable(newValue); ret = UpdateLastChangedTime(newLastChangedTime); } @@ -163,7 +170,7 @@ chefResourceMonitoringExternalWriteCallback(chip::EndpointId endpoint, chip::Clu { Protocols::InteractionModel::Status ret = Protocols::InteractionModel::Status::Success; AttributeId attributeId = attributeMetadata->attributeId; - ChipLogProgress(Zcl, "chefResourceMonitoringExternalWriteCallback EP: %d, Cluster: %d, Att: %d", static_cast(endpoint), + ChipLogProgress(Zcl, "chefResourceMonitoringExternalWriteCallback EP: %d, Cluster: %04x, Att: %04x", static_cast(endpoint), static_cast(clusterId), static_cast(attributeId)); switch (clusterId) @@ -232,8 +239,11 @@ ChefResourceMonitorInstance::ExternalAttributeRead(const EmberAfAttributeMetadat } break; case HepaFilterMonitoring::Attributes::LastChangedTime::Id: { + // Only LastChangedTime needs to handle Endianness DataModel::Nullable lastChangedTime = GetLastChangedTime(); - *(uint32_t *) buffer = lastChangedTime.IsNull() ? 0 : lastChangedTime.Value(); + chip::Encoding::LittleEndian::BufferWriter bufWriter(buffer, sizeof(uint16_t)); + + bufWriter.Put32(lastChangedTime.IsNull() ? 0 : lastChangedTime.Value()); } break; case HepaFilterMonitoring::Attributes::DegradationDirection::Id: