@@ -152,51 +152,14 @@ bool GetMatchingPresetInPresets(Delegate * delegate, const DataModel::Nullable<B
152
152
}
153
153
154
154
/* *
155
- * @brief Returns the length of the list of presets if the pending presets were to be applied. The size of the pending presets list
156
- * calculated, after all the constraint checks are done, is the new size of the updated Presets attribute since the pending
157
- * preset list is expected to have all existing presets with or without edits plus new presets.
158
- * This is called before changes are actually applied.
159
- *
160
- * @param[in] delegate The delegate to use.
161
- *
162
- * @return count of the updated Presets attribute if the pending presets were applied to it. Return 0 for error cases.
163
- */
164
- uint8_t CountNumberOfPendingPresets (Delegate * delegate)
165
- {
166
- uint8_t numberOfPendingPresets = 0 ;
167
-
168
- VerifyOrReturnValue (delegate != nullptr , 0 );
169
-
170
- for (uint8_t i = 0 ; true ; i++)
171
- {
172
- PresetStructWithOwnedMembers pendingPreset;
173
- CHIP_ERROR err = delegate->GetPendingPresetAtIndex (i, pendingPreset);
174
-
175
- if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
176
- {
177
- break ;
178
- }
179
- if (err != CHIP_NO_ERROR)
180
- {
181
- ChipLogError (Zcl, " CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT,
182
- err.Format ());
183
- return 0 ;
184
- }
185
- numberOfPendingPresets++;
186
- }
187
-
188
- return numberOfPendingPresets;
189
- }
190
-
191
- /* *
192
- * @brief Checks if the presetScenario is present in the PresetTypes attribute.
155
+ * @brief Gets the maximum number of presets allowed for a given preset scenario.
193
156
*
194
157
* @param[in] delegate The delegate to use.
195
158
* @param[in] presetScenario The presetScenario to match with.
196
159
*
197
- * @return true if the presetScenario is found, false otherwise.
160
+ * @return The maximum number of presets allowed for the preset scenario
198
161
*/
199
- bool PresetScenarioExistsInPresetTypes (Delegate * delegate, PresetScenarioEnum presetScenario)
162
+ uint8_t MaximumPresetScenarioCount (Delegate * delegate, PresetScenarioEnum presetScenario)
200
163
{
201
164
VerifyOrReturnValue (delegate != nullptr , false );
202
165
@@ -206,15 +169,17 @@ bool PresetScenarioExistsInPresetTypes(Delegate * delegate, PresetScenarioEnum p
206
169
auto err = delegate->GetPresetTypeAtIndex (i, presetType);
207
170
if (err != CHIP_NO_ERROR)
208
171
{
209
- return false ;
172
+ // Either we failed to fetch the next preset type, in which case we should error higher,
173
+ // or we exhausted the list trying to find the preset type
174
+ return 0 ;
210
175
}
211
176
212
177
if (presetType.presetScenario == presetScenario)
213
178
{
214
- return true ;
179
+ return presetType. numberOfPresets ;
215
180
}
216
181
}
217
- return false ;
182
+ return 0 ;
218
183
}
219
184
220
185
/* *
@@ -359,6 +324,10 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
359
324
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
360
325
}
361
326
327
+ // We're going to append this preset, so let's assume a count as though it had already been inserted
328
+ size_t presetCount = 1 ;
329
+ size_t presetScenarioCount = 1 ;
330
+
362
331
if (preset.GetPresetHandle ().IsNull ())
363
332
{
364
333
if (IsBuiltIn (preset))
@@ -410,8 +379,12 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
410
379
}
411
380
}
412
381
413
- if (!PresetScenarioExistsInPresetTypes (delegate, preset.GetPresetScenario ()))
382
+ size_t maximumPresetCount = delegate->GetNumberOfPresets ();
383
+ size_t maximumPresetScenarioCount = MaximumPresetScenarioCount (delegate, preset.GetPresetScenario ());
384
+
385
+ if (maximumPresetScenarioCount == 0 )
414
386
{
387
+ // This is not a supported preset scenario
415
388
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
416
389
}
417
390
@@ -423,16 +396,37 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
423
396
// Before adding this preset to the pending presets, if the expected length of the pending presets' list
424
397
// exceeds the total number of presets supported, return RESOURCE_EXHAUSTED. Note that the preset has not been appended yet.
425
398
426
- uint8_t numberOfPendingPresets = CountNumberOfPendingPresets (delegate);
399
+ for (uint8_t i = 0 ; true ; i++)
400
+ {
401
+ PresetStructWithOwnedMembers otherPreset;
402
+ CHIP_ERROR err = delegate->GetPendingPresetAtIndex (i, otherPreset);
427
403
428
- // We will be adding one more preset, so reject if the length is already at max.
429
- if (numberOfPendingPresets >= delegate->GetNumberOfPresets ())
404
+ if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
405
+ {
406
+ break ;
407
+ }
408
+ if (err != CHIP_NO_ERROR)
409
+ {
410
+ return CHIP_IM_GLOBAL_STATUS (InvalidInState);
411
+ }
412
+ presetCount++;
413
+ if (preset.GetPresetScenario () == otherPreset.GetPresetScenario ())
414
+ {
415
+ presetScenarioCount++;
416
+ }
417
+ }
418
+
419
+ if (presetCount > maximumPresetCount)
430
420
{
421
+ ChipLogError (Zcl, " Preset count exceeded %zu: %zu " , maximumPresetCount, presetCount);
431
422
return CHIP_IM_GLOBAL_STATUS (ResourceExhausted);
432
423
}
433
424
434
- // TODO #34556 : Check if the number of presets for each presetScenario exceeds the max number of presets supported for that
435
- // scenario. We plan to support only one preset for each presetScenario for our use cases so defer this for re-evaluation.
425
+ if (presetScenarioCount > maximumPresetScenarioCount)
426
+ {
427
+ ChipLogError (Zcl, " Preset scenario count exceeded %zu: %zu " , maximumPresetScenarioCount, presetScenarioCount);
428
+ return CHIP_IM_GLOBAL_STATUS (ResourceExhausted);
429
+ }
436
430
437
431
return delegate->AppendToPendingPresetList (preset);
438
432
}
0 commit comments