Skip to content

Commit 9d60489

Browse files
jmartinez-silabslpbeliveau-silabsbzbarsky-apple
authored
Add some constraint checks to the colorcontrol cluster commands (project-chip#36542)
* add some constraint checks to the colorcontrol cluster commands * Update src/app/clusters/color-control-server/color-control-server.cpp Co-authored-by: lpbeliveau-silabs <112982107+lpbeliveau-silabs@users.noreply.github.com> * Addrese comments. Move ConstraintCheck Before Unsupported command checks. Add contraint checks for ColorTemperatureMireds arguments * Update src/app/clusters/color-control-server/color-control-server.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> --------- Co-authored-by: lpbeliveau-silabs <112982107+lpbeliveau-silabs@users.noreply.github.com> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 099d173 commit 9d60489

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

src/app/clusters/color-control-server/color-control-server.cpp

+67-12
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,13 @@ EmberEventControl * ColorControlServer::configureHSVEventControl(EndpointId endp
12641264
* @param saturation Target saturation
12651265
* @param transitionTime Transition time in 10th of seconds
12661266
* @return Status::Success When successful,
1267-
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state.
1267+
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state,
1268+
* Status::ConstraintError if the saturation or tansitionTime are above maximum.
12681269
*/
12691270
Status ColorControlServer::moveToSaturation(EndpointId endpoint, uint8_t saturation, uint16_t transitionTime)
12701271
{
1272+
VerifyOrReturnError(saturation <= MAX_SATURATION_VALUE, Status::ConstraintError);
1273+
VerifyOrReturnError(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
12711274
Color16uTransitionState * colorSaturationTransitionState = getSaturationTransitionState(endpoint);
12721275
VerifyOrReturnError(nullptr != colorSaturationTransitionState, Status::UnsupportedEndpoint);
12731276

@@ -1307,6 +1310,7 @@ Status ColorControlServer::moveToSaturation(EndpointId endpoint, uint8_t saturat
13071310
* was called by MoveHue command and rate is a uint8 value.
13081311
* @return Status::Success When successful,
13091312
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state,
1313+
* Status::ConstraintError if the hue, saturation or transitionTime, are above maximum.
13101314
*/
13111315
Status ColorControlServer::moveToHueAndSaturation(EndpointId endpoint, uint16_t hue, uint8_t saturation, uint16_t transitionTime,
13121316
bool isEnhanced)
@@ -1315,6 +1319,10 @@ Status ColorControlServer::moveToHueAndSaturation(EndpointId endpoint, uint16_t
13151319
uint16_t halfWay = isEnhanced ? HALF_MAX_UINT16T : HALF_MAX_UINT8T;
13161320
bool moveUp;
13171321

1322+
VerifyOrReturnError((isEnhanced || hue <= MAX_HUE_VALUE), Status::ConstraintError);
1323+
VerifyOrReturnError(saturation <= MAX_SATURATION_VALUE, Status::ConstraintError);
1324+
VerifyOrReturnError(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
1325+
13181326
uint16_t epIndex = getEndpointIndex(endpoint);
13191327
Color16uTransitionState * colorSaturationTransitionState = getSaturationTransitionStateByIndex(epIndex);
13201328
ColorHueTransitionState * colorHueTransitionState = getColorHueTransitionStateByIndex(epIndex);
@@ -1502,10 +1510,11 @@ Status ColorControlServer::moveToHueCommand(EndpointId endpoint, uint16_t hue, D
15021510
bool isEnhanced)
15031511
{
15041512
MATTER_TRACE_SCOPE("moveToHue", "ColorControl");
1505-
VerifyOrReturnValue(moveDirection != DirectionEnum::kUnknownEnumValue, Status::InvalidCommand);
1506-
15071513
// Command Parameters constraint checks:
15081514
VerifyOrReturnValue((isEnhanced || hue <= MAX_HUE_VALUE), Status::ConstraintError);
1515+
VerifyOrReturnValue(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
1516+
1517+
VerifyOrReturnValue(moveDirection != DirectionEnum::kUnknownEnumValue, Status::InvalidCommand);
15091518

15101519
ColorHueTransitionState * colorHueTransitionState = getColorHueTransitionState(endpoint);
15111520
VerifyOrReturnValue(colorHueTransitionState != nullptr, Status::UnsupportedEndpoint);
@@ -1612,7 +1621,6 @@ Status ColorControlServer::moveToHueCommand(EndpointId endpoint, uint16_t hue, D
16121621
* was called by MoveHue command and rate is a uint8 value.
16131622
* @return Status::Success when successful,
16141623
* Status::ConstraintError when the other parameters are outside their defined value range.
1615-
16161624
*/
16171625
Status ColorControlServer::moveToHueAndSaturationCommand(EndpointId endpoint, uint16_t hue, uint8_t saturation,
16181626
uint16_t transitionTime, BitMask<OptionsBitmap> optionsMask,
@@ -1622,6 +1630,7 @@ Status ColorControlServer::moveToHueAndSaturationCommand(EndpointId endpoint, ui
16221630
// Command Parameters constraint checks:
16231631
VerifyOrReturnValue((isEnhanced || hue <= MAX_HUE_VALUE), Status::ConstraintError);
16241632
VerifyOrReturnValue(saturation <= MAX_SATURATION_VALUE, Status::ConstraintError);
1633+
VerifyOrReturnValue(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
16251634

16261635
VerifyOrReturnValue(shouldExecuteIfOff(endpoint, optionsMask, optionsOverride), Status::Success);
16271636

@@ -1646,12 +1655,19 @@ Status ColorControlServer::moveToHueAndSaturationCommand(EndpointId endpoint, ui
16461655
* @return Status::Success when successful,
16471656
* Status::InvalidCommand when StepSize is 0 or an unknown HueStepMode is provided
16481657
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a hue transition state.
1658+
* Status::ConstraintError when the other parameters are outside their defined value range.
16491659
*/
16501660
Status ColorControlServer::stepHueCommand(EndpointId endpoint, HueStepMode stepMode, uint16_t stepSize, uint16_t transitionTime,
16511661
BitMask<OptionsBitmap> optionsMask, BitMask<OptionsBitmap> optionsOverride,
16521662
bool isEnhanced)
16531663
{
16541664
MATTER_TRACE_SCOPE("stepHue", "ColorControl");
1665+
// Command Parameters constraint checks:
1666+
// The non-enhanced variant passed a uint8 type for transitionTime and the full range (0-255) is allowed
1667+
if (isEnhanced)
1668+
{
1669+
VerifyOrReturnValue(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
1670+
}
16551671
// Confirm validity of the step mode and step size received
16561672
VerifyOrReturnValue(stepMode != HueStepMode::kUnknownEnumValue, Status::InvalidCommand);
16571673
VerifyOrReturnValue(stepSize != 0, Status::InvalidCommand);
@@ -1787,14 +1803,16 @@ Status ColorControlServer::moveSaturationCommand(EndpointId endpoint, const Comm
17871803
* @param commandData Struct containing the parameters of the command.
17881804
* @return Status::Success when successful,
17891805
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state (verified in
1790-
* moveToSaturation function) Status::ConstraintError when a command parameters is outside its defined value range.
1806+
* moveToSaturation function)
1807+
* Status::ConstraintError when a command parameter is outside its defined value range.
17911808
*/
17921809
Status ColorControlServer::moveToSaturationCommand(EndpointId endpoint,
17931810
const Commands::MoveToSaturation::DecodableType & commandData)
17941811
{
17951812
MATTER_TRACE_SCOPE("moveToSaturation", "ColorControl");
17961813
// Command Parameters constraint checks:
17971814
VerifyOrReturnValue(commandData.saturation <= MAX_SATURATION_VALUE, Status::ConstraintError);
1815+
VerifyOrReturnValue(commandData.transitionTime <= kMaxTransitionTime, Status::ConstraintError);
17981816

17991817
VerifyOrReturnValue(shouldExecuteIfOff(endpoint, commandData.optionsMask, commandData.optionsOverride), Status::Success);
18001818
Status status = moveToSaturation(endpoint, commandData.saturation, commandData.transitionTime);
@@ -1810,7 +1828,7 @@ Status ColorControlServer::moveToSaturationCommand(EndpointId endpoint,
18101828
* @param commandData Struct containing the parameters of the command.
18111829
* @return Status::Success when successful,
18121830
* Status::InvalidCommand when a step size of 0 or an unknown SaturationStepMode is provided
1813-
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state.
1831+
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a saturation transition state,
18141832
*/
18151833
Status ColorControlServer::stepSaturationCommand(EndpointId endpoint, const Commands::StepSaturation::DecodableType & commandData)
18161834
{
@@ -1861,7 +1879,7 @@ Status ColorControlServer::stepSaturationCommand(EndpointId endpoint, const Comm
18611879
* @param commandData Struct containing the parameters of the command.
18621880
* @return Status::Success when successful,
18631881
* Status::InvalidCommand when an unknown action or direction is provided
1864-
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a hue transition state.
1882+
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a hue transition state,
18651883
*/
18661884
Status ColorControlServer::colorLoopCommand(EndpointId endpoint, const Commands::ColorLoopSet::DecodableType & commandData)
18671885
{
@@ -2146,10 +2164,18 @@ EmberEventControl * ColorControlServer::configureXYEventControl(EndpointId endpo
21462164
* @param colorX target X
21472165
* @param colorY target Y
21482166
* @param transitionTime transition time in 10th of seconds
2149-
* @return Status::Success if successful,Status::UnsupportedEndpoint XY is not supported on the endpoint
2167+
* @return Status::Success if successful,
2168+
* @return Status::Success when successful,
2169+
* Status::UnsupportedEndpoint XY is not supported on the endpoint,
2170+
* Status::ConstraintError when a command parameter is outside its defined value range.
21502171
*/
21512172
Status ColorControlServer::moveToColor(EndpointId endpoint, uint16_t colorX, uint16_t colorY, uint16_t transitionTime)
21522173
{
2174+
// Command Parameters constraint checks:
2175+
VerifyOrReturnValue(colorX <= MAX_CIE_XY_VALUE, Status::ConstraintError);
2176+
VerifyOrReturnValue(colorY <= MAX_CIE_XY_VALUE, Status::ConstraintError);
2177+
VerifyOrReturnValue(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
2178+
21532179
uint16_t epIndex = getEndpointIndex(endpoint);
21542180
Color16uTransitionState * colorXTransitionState = getXTransitionStateByIndex(epIndex);
21552181
Color16uTransitionState * colorYTransitionState = getYTransitionStateByIndex(epIndex);
@@ -2201,9 +2227,15 @@ Status ColorControlServer::moveToColor(EndpointId endpoint, uint16_t colorX, uin
22012227
* @return Status::Success when successful,
22022228
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a Color XY transition state (verified in
22032229
* moveToColor function),
2230+
* Status::ConstraintError when a command parameter is outside its defined value range.
22042231
*/
22052232
Status ColorControlServer::moveToColorCommand(EndpointId endpoint, const Commands::MoveToColor::DecodableType & commandData)
22062233
{
2234+
// Command Parameters constraint checks:
2235+
VerifyOrReturnValue(commandData.colorX <= MAX_CIE_XY_VALUE, Status::ConstraintError);
2236+
VerifyOrReturnValue(commandData.colorY <= MAX_CIE_XY_VALUE, Status::ConstraintError);
2237+
VerifyOrReturnValue(commandData.transitionTime <= kMaxTransitionTime, Status::ConstraintError);
2238+
22072239
VerifyOrReturnValue(shouldExecuteIfOff(endpoint, commandData.optionsMask, commandData.optionsOverride), Status::Success);
22082240

22092241
Status status = moveToColor(endpoint, commandData.colorX, commandData.colorY, commandData.transitionTime);
@@ -2302,10 +2334,14 @@ Status ColorControlServer::moveColorCommand(EndpointId endpoint, const Commands:
23022334
* @param commandData Struct containing the parameters of the command
23032335
* @return Status::Success when successful,
23042336
* Status::InvalidCommand when a step X and Y of 0 is provided
2305-
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a Color XY transition state.
2337+
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a Color XY transition state,
2338+
* Status::ConstraintError when a command parameter is outside its defined value range.
23062339
*/
23072340
Status ColorControlServer::stepColorCommand(EndpointId endpoint, const Commands::StepColor::DecodableType & commandData)
23082341
{
2342+
// Command Parameters constraint checks:
2343+
VerifyOrReturnValue(commandData.transitionTime <= kMaxTransitionTime, Status::ConstraintError);
2344+
23092345
VerifyOrReturnValue(commandData.stepX != 0 || commandData.stepY != 0, Status::InvalidCommand);
23102346

23112347
uint16_t epIndex = getEndpointIndex(endpoint);
@@ -2440,8 +2476,11 @@ ColorControlServer::Color16uTransitionState * ColorControlServer::getTempTransit
24402476
*/
24412477
Status ColorControlServer::moveToColorTemp(EndpointId aEndpoint, uint16_t colorTemperature, uint16_t transitionTime)
24422478
{
2443-
EndpointId endpoint = aEndpoint;
2479+
// Command Parameters constraint checks:
2480+
VerifyOrReturnValue(colorTemperature <= kMaxColorTemperatureMireds, Status::ConstraintError);
2481+
VerifyOrReturnValue(transitionTime <= kMaxTransitionTime, Status::ConstraintError);
24442482

2483+
EndpointId endpoint = aEndpoint;
24452484
Color16uTransitionState * colorTempTransitionState = getTempTransitionState(endpoint);
24462485
VerifyOrReturnError(nullptr != colorTempTransitionState, Status::UnsupportedEndpoint);
24472486

@@ -2638,10 +2677,15 @@ void ColorControlServer::updateTempCommand(EndpointId endpoint)
26382677
* @return Status::Success when successful,
26392678
* Status::InvalidCommand when a rate of 0 for a non-stop move or an unknown HueMoveMode is provided
26402679
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a color temp transition state.
2680+
* Status::ConstraintError when a command parameter is outside its defined value range.
26412681
*/
26422682
Status ColorControlServer::moveColorTempCommand(EndpointId endpoint,
26432683
const Commands::MoveColorTemperature::DecodableType & commandData)
26442684
{
2685+
// Command Parameters constraint checks:
2686+
VerifyOrReturnValue(commandData.colorTemperatureMinimumMireds <= kMaxColorTemperatureMireds, Status::ConstraintError);
2687+
VerifyOrReturnValue(commandData.colorTemperatureMaximumMireds <= kMaxColorTemperatureMireds, Status::ConstraintError);
2688+
26452689
// check moveMode and rate before any operation is done on the transition states
26462690
// rate value is ignored if the MoveMode is stop
26472691
VerifyOrReturnValue(commandData.moveMode != HueMoveMode::kUnknownEnumValue, Status::InvalidCommand);
@@ -2734,11 +2778,16 @@ Status ColorControlServer::moveColorTempCommand(EndpointId endpoint,
27342778
* @param commandData Struct containing the parameters of the command.
27352779
* @return Status::Success when successful,
27362780
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a color XY transition state (verified in
2737-
* moveToColorTemp function).
2781+
* moveToColorTemp function),
2782+
* Status::ConstraintError when a command parameter is outside its defined value range.
27382783
*/
27392784
Status ColorControlServer::moveToColorTempCommand(EndpointId endpoint,
27402785
const Commands::MoveToColorTemperature::DecodableType & commandData)
27412786
{
2787+
// Command Parameters constraint checks:
2788+
VerifyOrReturnValue(commandData.colorTemperatureMireds <= kMaxColorTemperatureMireds, Status::ConstraintError);
2789+
VerifyOrReturnValue(commandData.transitionTime <= kMaxTransitionTime, Status::ConstraintError);
2790+
27422791
VerifyOrReturnValue(shouldExecuteIfOff(endpoint, commandData.optionsMask, commandData.optionsOverride), Status::Success);
27432792

27442793
Status status = moveToColorTemp(endpoint, commandData.colorTemperatureMireds, commandData.transitionTime);
@@ -2754,11 +2803,17 @@ Status ColorControlServer::moveToColorTempCommand(EndpointId endpoint,
27542803
* @param commandData Struct containing the parameters of the command
27552804
* @return Status::Success when successful,
27562805
* Status::InvalidCommand when stepSize is 0 or an unknown stepMode is provided
2757-
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a color temp transition state.
2806+
* Status::UnsupportedEndpoint when the provided endpoint doesn't correspond with a color temp transition state,
2807+
* Status::ConstraintError when a command parameter is outside its defined value range.
27582808
*/
27592809
Status ColorControlServer::stepColorTempCommand(EndpointId endpoint,
27602810
const Commands::StepColorTemperature::DecodableType & commandData)
27612811
{
2812+
// Command Parameters constraint checks:
2813+
VerifyOrReturnValue(commandData.transitionTime <= kMaxTransitionTime, Status::ConstraintError);
2814+
VerifyOrReturnValue(commandData.colorTemperatureMinimumMireds <= kMaxColorTemperatureMireds, Status::ConstraintError);
2815+
VerifyOrReturnValue(commandData.colorTemperatureMaximumMireds <= kMaxColorTemperatureMireds, Status::ConstraintError);
2816+
27622817
// Confirm validity of the step mode and step size received
27632818
VerifyOrReturnValue(commandData.stepMode != HueStepMode::kUnknownEnumValue, Status::InvalidCommand);
27642819
VerifyOrReturnValue(commandData.stepSize != 0, Status::InvalidCommand);

src/app/clusters/color-control-server/color-control-server.h

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ class ColorControlServer
294294
MATTER_DM_COLOR_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
295295
static_assert(kColorControlClusterServerMaxEndpointCount <= kEmberInvalidEndpointIndex, "ColorControl endpoint count error");
296296

297+
static constexpr uint16_t kMaxTransitionTime = 65534; // Max value as defined by the spec.
298+
static constexpr uint16_t kMaxColorTemperatureMireds = 65279; // Max value as defined by the spec (0xFEFF).
299+
297300
#ifdef MATTER_DM_PLUGIN_COLOR_CONTROL_SERVER_HSV
298301
ColorHueTransitionState colorHueTransitionStates[kColorControlClusterServerMaxEndpointCount];
299302
Color16uTransitionState colorSatTransitionStates[kColorControlClusterServerMaxEndpointCount];

0 commit comments

Comments
 (0)