@@ -196,7 +196,7 @@ uint8_t CountNumberOfPendingPresets(Delegate * delegate)
196
196
*
197
197
* @return true if the presetScenario is found, false otherwise.
198
198
*/
199
- bool PresetScenarioExistsInPresetTypes (Delegate * delegate, PresetScenarioEnum presetScenario)
199
+ uint8_t MaximumPresetScenarioCount (Delegate * delegate, PresetScenarioEnum presetScenario)
200
200
{
201
201
VerifyOrReturnValue (delegate != nullptr , false );
202
202
@@ -206,15 +206,15 @@ bool PresetScenarioExistsInPresetTypes(Delegate * delegate, PresetScenarioEnum p
206
206
auto err = delegate->GetPresetTypeAtIndex (i, presetType);
207
207
if (err != CHIP_NO_ERROR)
208
208
{
209
- return false ;
209
+ return 0 ;
210
210
}
211
211
212
212
if (presetType.presetScenario == presetScenario)
213
213
{
214
- return true ;
214
+ return presetType. numberOfPresets ;
215
215
}
216
216
}
217
- return false ;
217
+ return 0 ;
218
218
}
219
219
220
220
/* *
@@ -358,12 +358,17 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
358
358
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
359
359
}
360
360
361
+ size_t presetCount = 0 ;
362
+ size_t presetScenarioCount = 0 ;
361
363
if (preset.presetHandle .IsNull ())
362
364
{
363
365
if (IsBuiltIn (preset))
364
366
{
365
367
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
366
368
}
369
+ // We're going to insert this preset, so let's assume a count as though it had already been inserted
370
+ presetCount++;
371
+ presetScenarioCount++;
367
372
}
368
373
else
369
374
{
@@ -391,12 +396,49 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
391
396
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
392
397
}
393
398
}
394
-
395
- if (! PresetScenarioExistsInPresetTypes (delegate, preset. presetScenario ) )
399
+ uint8_t maximumPresetScenarioCount = MaximumPresetScenarioCount (delegate, preset. presetScenario );
400
+ if (maximumPresetScenarioCount == 0 )
396
401
{
402
+ // This is not a supported preset scenario
397
403
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
398
404
}
399
405
406
+ for (uint8_t i = 0 ; true ; i++)
407
+ {
408
+ PresetStructWithOwnedMembers otherPreset;
409
+ CHIP_ERROR err = delegate->GetPresetAtIndex (i, otherPreset);
410
+
411
+ if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
412
+ {
413
+ break ;
414
+ }
415
+ if (err != CHIP_NO_ERROR)
416
+ {
417
+ return CHIP_IM_GLOBAL_STATUS (InvalidInState);
418
+ }
419
+ presetCount++;
420
+ if (!preset.presetHandle .IsNull () && otherPreset.GetPresetHandle ().Value ().data_equal (preset.presetHandle .Value ()))
421
+ {
422
+ // This is the preset we're updating, so we don't care what its existing scenario is
423
+ presetScenarioCount++;
424
+ continue ;
425
+ }
426
+ if (preset.presetScenario == otherPreset.GetPresetScenario ())
427
+ {
428
+ presetScenarioCount++;
429
+ }
430
+ }
431
+
432
+ if (presetCount > delegate->GetNumberOfPresets ())
433
+ {
434
+ return CHIP_IM_GLOBAL_STATUS (ResourceExhausted);
435
+ }
436
+
437
+ if (presetScenarioCount > maximumPresetScenarioCount)
438
+ {
439
+ return CHIP_IM_GLOBAL_STATUS (ResourceExhausted);
440
+ }
441
+
400
442
if (preset.name .HasValue () && !PresetTypeSupportsNames (delegate, preset.presetScenario ))
401
443
{
402
444
return CHIP_IM_GLOBAL_STATUS (ConstraintError);
0 commit comments