Skip to content

Commit

Permalink
Fix MaxMeasuredValue is out of range Temperature Measurement Cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Mar 3, 2022
1 parent e0a5a75 commit 80e2017
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/media-playback-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/target-navigator-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/temperature-measurement-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thermostat-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thermostat-user-interface-configuration-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/time-format-localization-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/fixed-label-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/user-label-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/temperature-measurement-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/****************************************************************************
* @file
* @brief Implementation for the Temperature Measurement Server Cluster
***************************************************************************/

#include <app-common/zap-generated/af-structs.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/util/attribute-storage.h>
#include <lib/core/CHIPEncoding.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/PlatformManager.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::TemperatureMeasurement;
using namespace chip::app::Clusters::TemperatureMeasurement::Attributes;

constexpr int16_t kMinMeasuredValueLowerLimit = -27315; // -273.15°C
constexpr int16_t kMinMeasuredValueUpperLimit = 32766; // 327.66°C
constexpr int16_t kMaxMeasuredValueLowerLimit = -27314; // -273.14°C
constexpr int16_t kMaxMeasuredValueUpperLimit = 32767; // 327.67°C
constexpr int16_t kMaxToleranceValue = 2048;

// =============================================================================
// Pre-change callbacks for cluster attributes
// =============================================================================

using Status = Protocols::InteractionModel::Status;

Protocols::InteractionModel::Status MatterTemperatureMeasurementClusterServerPreAttributeChangedCallback(
const app::ConcreteAttributePath & attributePath, EmberAfAttributeType attributeType, uint16_t size, uint8_t * value)
{
EndpointId endpoint = attributePath.mEndpointId;
int16_t requested;

DataModel::Nullable<int16_t> MinMeasuredValue;
DataModel::Nullable<int16_t> MaxMeasuredValue;

if (MeasuredValue::Get(endpoint, MinMeasuredValue) != EMBER_ZCL_STATUS_SUCCESS)
{
MinMeasuredValue.SetNull();
}

if (MeasuredValue::Get(endpoint, MaxMeasuredValue) != EMBER_ZCL_STATUS_SUCCESS)
{
MaxMeasuredValue.SetNull();
}

switch (attributePath.mAttributeId)
{
case MeasuredValue::Id: {
requested = static_cast<int16_t>(chip::Encoding::LittleEndian::Get16(value));

if (requested < (MinMeasuredValue.IsNull() ? kMinMeasuredValueLowerLimit : MinMeasuredValue.Value()))
{
return Status::InvalidValue;
}
else if (requested > (MaxMeasuredValue.IsNull() ? kMaxMeasuredValueUpperLimit : MaxMeasuredValue.Value()))
{
return Status::InvalidValue;
}

return Status::Success;
}
case MinMeasuredValue::Id: {
requested = static_cast<int16_t>(chip::Encoding::LittleEndian::Get16(value));

if (requested < kMinMeasuredValueLowerLimit || requested > kMinMeasuredValueUpperLimit)
{
return Status::InvalidValue;
}

return Status::Success;
}
case MaxMeasuredValue::Id: {
requested = static_cast<int16_t>(chip::Encoding::LittleEndian::Get16(value));

if (requested < kMaxMeasuredValueLowerLimit || requested > kMaxMeasuredValueUpperLimit)
{
return Status::InvalidValue;
}

return Status::Success;
}
case Tolerance::Id: {
requested = static_cast<int16_t>(chip::Encoding::LittleEndian::Get16(value));

if (requested < 0 || requested > kMaxToleranceValue)
{
return Status::InvalidValue;
}

return Status::Success;
}
default:
return Status::Success;
}
}
3 changes: 2 additions & 1 deletion src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ var endpointClusterWithAttributeChanged = [
'Window Covering',
];
var endpointClusterWithPreAttribute = [
'IAS Zone', 'Door Lock', 'Thermostat User Interface Configuration', 'Time Format Localization', 'Localization Configuration'
'IAS Zone', 'Door Lock', 'Thermostat User Interface Configuration', 'Time Format Localization', 'Localization Configuration',
'Temperature Measurement'
];
var endpointClusterWithMessageSent = [ 'IAS Zone' ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ limitations under the License.
<define>TEMP_MEASUREMENT_CLUSTER</define>
<client tick="false" init="false">true</client>
<server tick="false" init="false">true</server>
<attribute side="server" code="0x0000" define="TEMP_MEASURED_VALUE" type="INT16S" min="0x954d" max="0x7fff" writable="false" optional="false">MeasuredValue</attribute>
<attribute side="server" code="0x0001" define="TEMP_MIN_MEASURED_VALUE" type="INT16S" min="0x954d" max="0x7ffe" writable="false" default="0x8000" optional="false">MinMeasuredValue</attribute>
<attribute side="server" code="0x0002" define="TEMP_MAX_MEASURED_VALUE" type="INT16S" min="0x954e" max="0x7fff" writable="false" default="0x8000" optional="false">MaxMeasuredValue</attribute>
<attribute side="server" code="0x0000" define="TEMP_MEASURED_VALUE" type="INT16S" min="0x954d" max="0x7fff" writable="false" isNullable="true" optional="false">MeasuredValue</attribute>
<attribute side="server" code="0x0001" define="TEMP_MIN_MEASURED_VALUE" type="INT16S" min="0x954d" max="0x7ffe" writable="false" isNullable="true" default="0x8000" optional="false">MinMeasuredValue</attribute>
<attribute side="server" code="0x0002" define="TEMP_MAX_MEASURED_VALUE" type="INT16S" min="0x954e" max="0x7fff" writable="false" isNullable="true" default="0x8000" optional="false">MaxMeasuredValue</attribute>
<attribute side="server" code="0x0003" define="TEMP_TOLERANCE" type="INT16U" min="0" max="0x8000" writable="false" default="0" optional="true">Tolerance</attribute>
</cluster>
</configurator>
2 changes: 1 addition & 1 deletion src/app/zap_cluster_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
'SOFTWARE_DIAGNOSTICS_CLUSTER': ['software-diagnostics-server'],
'SWITCH_CLUSTER': ['switch-server'],
'TARGET_NAVIGATOR_CLUSTER': ['target-navigator-server'],
'TEMP_MEASUREMENT_CLUSTER': [],
'TEMP_MEASUREMENT_CLUSTER': ['temperature-measurement-server'],
'TEST_CLUSTER': ['test-cluster-server'],
'THERMOSTAT_CLUSTER': ['thermostat-server'],
'THERMOSTAT_UI_CONFIG_CLUSTER': ['thermostat-user-interface-configuration-server'],
Expand Down

0 comments on commit 80e2017

Please sign in to comment.