forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice-area-server.cpp
1109 lines (919 loc) · 36 KB
/
service-area-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Copyright (c) 2023 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.
*
*/
#include "service-area-server.h"
#include "service-area-delegate.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/Commands.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandler.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <app/util/util.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <protocols/interaction_model/StatusCode.h>
using Status = chip::Protocols::InteractionModel::Status;
namespace chip {
namespace app {
namespace Clusters {
namespace ServiceArea {
// ****************************************************************************
// Service Area Server Instance
Instance::Instance(Delegate * aDelegate, EndpointId aEndpointId, BitMask<Feature> aFeature) :
AttributeAccessInterface(MakeOptional(aEndpointId), Id), CommandHandlerInterface(MakeOptional(aEndpointId), Id),
mDelegate(aDelegate), mEndpointId(aEndpointId), mClusterId(Id), mFeature(aFeature)
{
ChipLogProgress(Zcl, "Service Area: Instance constructor");
mDelegate->SetInstance(this);
}
Instance::~Instance()
{
CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this);
AttributeAccessInterfaceRegistry::Instance().Unregister(this);
}
CHIP_ERROR Instance::Init()
{
ChipLogProgress(Zcl, "Service Area: INIT");
// Check if the cluster has been selected in zap
VerifyOrReturnError(emberAfContainsServer(mEndpointId, Id), CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Zcl, "Service Area: The cluster with Id %lu was not enabled in zap.", long(Id)));
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this));
VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INCORRECT_STATE);
return mDelegate->Init();
}
//*************************************************************************
// core functions
CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
ChipLogDetail(Zcl, "Service Area: Reading attribute with ID " ChipLogFormatMEI, ChipLogValueMEI(aPath.mAttributeId));
switch (aPath.mAttributeId)
{
case Attributes::SupportedAreas::Id:
return ReadSupportedAreas(aEncoder);
case Attributes::SupportedMaps::Id:
return ReadSupportedMaps(aEncoder);
case Attributes::SelectedAreas::Id:
return ReadSelectedAreas(aEncoder);
case Attributes::CurrentArea::Id:
return aEncoder.Encode(GetCurrentArea());
case Attributes::EstimatedEndTime::Id:
return aEncoder.Encode(GetEstimatedEndTime());
case Attributes::Progress::Id:
return ReadProgress(aEncoder);
case Attributes::FeatureMap::Id:
return aEncoder.Encode(mFeature);
default:
ChipLogProgress(Zcl, "Service Area: Unsupported attribute with ID " ChipLogFormatMEI, ChipLogValueMEI(aPath.mAttributeId));
}
return CHIP_NO_ERROR;
}
void Instance::InvokeCommand(HandlerContext & handlerContext)
{
switch (handlerContext.mRequestPath.mCommandId)
{
case Commands::SelectAreas::Id:
return CommandHandlerInterface::HandleCommand<Commands::SelectAreas::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & req) { HandleSelectAreasCmd(ctx, req); });
case Commands::SkipArea::Id:
return CommandHandlerInterface::HandleCommand<Commands::SkipArea::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & req) { HandleSkipCurrentAreaCmd(ctx, req); });
}
}
//*************************************************************************
// attribute readers
CHIP_ERROR Instance::ReadSupportedAreas(AttributeValueEncoder & aEncoder)
{
if (mDelegate->GetNumberOfSupportedAreas() == 0)
{
return aEncoder.EncodeEmptyList();
}
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
uint8_t locationIndex = 0;
AreaStructureWrapper supportedArea;
while (mDelegate->GetSupportedAreaByIndex(locationIndex++, supportedArea))
{
ReturnErrorOnFailure(encoder.Encode(supportedArea));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR Instance::ReadSupportedMaps(AttributeValueEncoder & aEncoder)
{
if (mDelegate->GetNumberOfSupportedMaps() == 0)
{
return aEncoder.EncodeEmptyList();
}
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
uint32_t mapIndex = 0;
MapStructureWrapper supportedMap;
while (mDelegate->GetSupportedMapByIndex(mapIndex++, supportedMap))
{
ReturnErrorOnFailure(encoder.Encode(supportedMap));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR Instance::ReadSelectedAreas(AttributeValueEncoder & aEncoder)
{
if (mDelegate->GetNumberOfSelectedAreas() == 0)
{
return aEncoder.EncodeEmptyList();
}
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
uint32_t locationIndex = 0;
uint32_t selectedArea;
while (mDelegate->GetSelectedAreaByIndex(locationIndex++, selectedArea))
{
ReturnErrorOnFailure(encoder.Encode(selectedArea));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR Instance::ReadProgress(AttributeValueEncoder & aEncoder)
{
if (mDelegate->GetNumberOfProgressElements() == 0)
{
return aEncoder.EncodeEmptyList();
}
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
uint32_t locationIndex = 0;
Structs::ProgressStruct::Type progressElement;
while (mDelegate->GetProgressElementByIndex(locationIndex++, progressElement))
{
ReturnErrorOnFailure(encoder.Encode(progressElement));
}
return CHIP_NO_ERROR;
});
}
//*************************************************************************
// command handlers
void Instance::HandleSelectAreasCmd(HandlerContext & ctx, const Commands::SelectAreas::DecodableType & req)
{
ChipLogDetail(Zcl, "Service Area: HandleSelectAreasCmd");
// On receipt of this command the device SHALL respond with a SelectAreasResponse command.
auto exitResponse = [ctx](SelectAreasStatus status, CharSpan statusText) {
Commands::SelectAreasResponse::Type response{
.status = status,
.statusText = statusText,
};
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
};
size_t numberOfAreas = 0;
// Get the number of Selected Areas in the command parameter and check that it is valid.
{
if (CHIP_NO_ERROR != req.newAreas.ComputeSize(&numberOfAreas))
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand);
return;
}
// If the device determines that it can't operate at all locations from the list,
// the SelectAreasResponse command's Status field SHALL indicate InvalidSet.
if (numberOfAreas > kMaxNumSelectedAreas)
{
exitResponse(SelectAreasStatus::kInvalidSet, "invalid number of locations"_span);
return;
}
}
// if number of selected locations in parameter matches number in attribute - the locations *might* be the same
bool matchesCurrentSelectedAreas = (numberOfAreas == mDelegate->GetNumberOfSelectedAreas());
if (numberOfAreas != 0)
{
// do as much parameter validation as we can
{
uint32_t ignoredIndex = 0;
uint32_t oldSelectedArea;
uint32_t i = 0;
auto iAreaIter = req.newAreas.begin();
while (iAreaIter.Next())
{
uint32_t aSelectedArea = iAreaIter.GetValue();
// each item in this list SHALL match the AreaID field of an entry on the SupportedAreas attribute's list
// If the Status field is set to UnsupportedArea, the StatusText field SHALL be an empty string.
if (!IsSupportedArea(aSelectedArea))
{
exitResponse(SelectAreasStatus::kUnsupportedArea, ""_span);
return;
}
// Checking for duplicate locations.
uint32_t j = 0;
auto jAreaIter = req.newAreas.begin();
while (j < i)
{
jAreaIter.Next(); // Since j < i and i is valid, we can safely call Next() without checking the return value.
if (jAreaIter.GetValue() == aSelectedArea)
{
exitResponse(SelectAreasStatus::kDuplicatedAreas, ""_span);
return;
}
j += 1;
}
// check to see if parameter list and attribute still match
if (matchesCurrentSelectedAreas)
{
if (!mDelegate->GetSelectedAreaByIndex(ignoredIndex, oldSelectedArea) || (aSelectedArea != oldSelectedArea))
{
matchesCurrentSelectedAreas = false;
}
}
i += 1;
}
// after iterating with Next through DecodableType - check for failure
if (CHIP_NO_ERROR != iAreaIter.GetStatus())
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::InvalidCommand);
return;
}
}
}
// If the newAreas field is the same as the value of the SelectedAreas attribute
// the SelectAreasResponse command SHALL have the Status field set to Success and
// the StatusText field MAY be supplied with a human-readable string or include an empty string.
if (matchesCurrentSelectedAreas)
{
exitResponse(SelectAreasStatus::kSuccess, ""_span);
return;
}
char delegateStatusBuffer[kMaxSizeStatusText];
MutableCharSpan delegateStatusText(delegateStatusBuffer);
// If the current state of the device doesn't allow for the locations to be selected,
// the SelectAreasResponse command SHALL have the Status field set to InvalidInMode.
// if the Status field is set to InvalidInMode, the StatusText field SHOULD indicate why the request is not allowed,
// given the current mode of the device, which may involve other clusters.
// (note - locationStatusText to be filled out by delegated function for if return value is false)
if (!mDelegate->IsSetSelectedAreasAllowed(delegateStatusText))
{
exitResponse(SelectAreasStatus::kInvalidInMode, delegateStatusText);
return;
}
// Reset in case the delegate accidentally modified this string.
delegateStatusText = MutableCharSpan(delegateStatusBuffer);
// ask the device to handle SelectAreas Command
// (note - locationStatusText to be filled out by delegated function for kInvalidInMode and InvalidSet)
auto locationStatus = SelectAreasStatus::kSuccess;
if (!mDelegate->IsValidSelectAreasSet(req, locationStatus, delegateStatusText))
{
exitResponse(locationStatus, delegateStatusText);
return;
}
{
// If the device successfully accepts the request, the server will attempt to operate at the location(s)
// indicated by the entries of the newArea field, when requested to operate,
// the SelectAreasResponse command SHALL have the Status field set to Success,
// and the SelectedAreas attribute SHALL be set to the value of the newAreas field.
mDelegate->ClearSelectedAreas();
if (numberOfAreas != 0)
{
auto locationIter = req.newAreas.begin();
uint32_t ignored;
while (locationIter.Next())
{
mDelegate->AddSelectedArea(locationIter.GetValue(), ignored);
}
}
NotifySelectedAreasChanged();
}
exitResponse(SelectAreasStatus::kSuccess, ""_span);
}
void Instance::HandleSkipCurrentAreaCmd(HandlerContext & ctx, const Commands::SkipArea::DecodableType & req)
{
ChipLogDetail(Zcl, "Service Area: HandleSkipCurrentArea");
// On receipt of this command the device SHALL respond with a SkipCurrentAreaResponse command.
auto exitResponse = [ctx](SkipAreaStatus status, CharSpan statusText) {
Commands::SkipAreaResponse::Type response{
.status = status,
.statusText = statusText,
};
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
};
// If the SelectedAreas attribute is null, the response status should be set to InvalidAreaList.
// If the Status field is set to InvalidAreaList, the StatusText field SHALL be an empty string.
if (mDelegate->GetNumberOfSelectedAreas() == 0)
{
ChipLogError(Zcl, "Selected Areas attribute is null");
exitResponse(SkipAreaStatus::kInvalidAreaList, ""_span);
return;
}
// If the CurrentArea attribute is null, the status should be set to InvalidInMode.
// If the Status field is not set to Success, or InvalidAreaList, the StatusText field SHALL include a vendor defined error
// description.
if (mCurrentArea.IsNull())
{
exitResponse(SkipAreaStatus::kInvalidInMode, "Current Area attribute is null"_span);
return;
}
// have the device attempt to skip
// If the Status field is not set to Success, or InvalidAreaList, the StatusText field SHALL include a vendor defined error
// description. InvalidInMode | The received request cannot be handled due to the current mode of the device. (skipStatusText to
// be filled out by delegated function on failure.)
char skipStatusBuffer[kMaxSizeStatusText];
MutableCharSpan skipStatusText(skipStatusBuffer);
if (!mDelegate->HandleSkipCurrentArea(req.skippedArea, skipStatusText))
{
exitResponse(SkipAreaStatus::kInvalidInMode, skipStatusText);
return;
}
exitResponse(SkipAreaStatus::kSuccess, ""_span);
}
//*************************************************************************
// attribute notifications
void Instance::NotifySupportedAreasChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::SupportedAreas::Id);
}
void Instance::NotifySupportedMapsChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::SupportedMaps::Id);
}
void Instance::NotifySelectedAreasChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::SelectedAreas::Id);
}
void Instance::NotifyCurrentAreaChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::CurrentArea::Id);
}
void Instance::NotifyEstimatedEndTimeChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::EstimatedEndTime::Id);
}
void Instance::NotifyProgressChanged()
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::Progress::Id);
}
// ****************************************************************************
// Supported Areas manipulators
bool Instance::IsSupportedArea(uint32_t aAreaId)
{
uint32_t ignoredIndex;
AreaStructureWrapper ignoredArea;
return mDelegate->GetSupportedAreaById(aAreaId, ignoredIndex, ignoredArea);
}
bool Instance::IsValidSupportedArea(const AreaStructureWrapper & aArea)
{
// If the LocationInfo field is null, the LandmarkInfo field SHALL NOT be null.
// If the LandmarkInfo field is null, the LocationInfo field SHALL NOT be null.
if (aArea.areaDesc.locationInfo.IsNull() && aArea.areaDesc.landmarkInfo.IsNull())
{
ChipLogDetail(Zcl, "IsValidAsSupportedArea %u - must have locationInfo and/or LandmarkInfo", aArea.areaID);
return false;
}
// If LocationInfo is not null, and its LocationName field is an empty string, at least one of the following SHALL NOT
// be null: LocationInfo's FloorNumber field, LocationInfo's AreaType field, the LandmarkInfo
if (!aArea.areaDesc.locationInfo.IsNull())
{
if (aArea.areaDesc.locationInfo.Value().locationName.empty() && aArea.areaDesc.locationInfo.Value().floorNumber.IsNull() &&
aArea.areaDesc.locationInfo.Value().areaType.IsNull() && aArea.areaDesc.landmarkInfo.IsNull())
{
ChipLogDetail(Zcl, "IsValidAsSupportedArea %u - AreaName is empty string, FloorNumber, AreaType, LandmarkInfo are null",
aArea.areaID);
return false;
}
}
// The mapID field SHALL be null if SupportedMaps is not supported or SupportedMaps is an empty list.
if (mFeature.Has(Feature::kMaps) && (mDelegate->GetNumberOfSupportedMaps() > 0))
{
if (aArea.mapID.IsNull())
{
ChipLogDetail(Zcl, "IsValidSupportedArea %u - map Id should not be null when there are supported maps", aArea.areaID);
return false;
}
// If the SupportedMaps attribute is not null, mapID SHALL be the ID of an entry from the SupportedMaps attribute.
if (!IsSupportedMap(aArea.mapID.Value()))
{
ChipLogError(Zcl, "IsValidSupportedArea %u - map Id %u is not in supported map list", aArea.areaID,
aArea.mapID.Value());
return false;
}
}
else
{
if (!aArea.mapID.IsNull())
{
ChipLogDetail(Zcl, "IsValidSupportedArea %u - map Id %u is not in empty supported map list", aArea.areaID,
aArea.mapID.Value());
return false;
}
}
return true;
}
bool Instance::IsUniqueSupportedArea(const AreaStructureWrapper & aArea, bool ignoreAreaId)
{
BitMask<AreaStructureWrapper::IsEqualConfig> config;
if (ignoreAreaId)
{
config.Set(AreaStructureWrapper::IsEqualConfig::kIgnoreAreaID);
}
// If the SupportedMaps attribute is not null, each entry in this list SHALL have a unique value for the combination of the
// MapID and LocationInfo fields. If the SupportedMaps attribute is null, each entry in this list SHALL have a unique value for
// the LocationInfo field.
if (mDelegate->GetNumberOfSupportedMaps() == 0)
{
config.Set(AreaStructureWrapper::IsEqualConfig::kIgnoreMapId);
}
uint8_t locationIndex = 0;
AreaStructureWrapper entry;
while (mDelegate->GetSupportedAreaByIndex(locationIndex++, entry))
{
if (aArea.IsEqual(entry, config))
{
return false;
}
}
return true;
}
bool Instance::ReportEstimatedEndTimeChange(const DataModel::Nullable<uint32_t> & aEstimatedEndTime)
{
if (mEstimatedEndTime == aEstimatedEndTime)
{
return false;
}
// The value of this attribute SHALL only be reported in the following cases:
// - when it changes from null.
if (mEstimatedEndTime.IsNull())
{
return true;
}
// - when it changes from 0
if (mEstimatedEndTime.Value() == 0)
{
return true;
}
if (aEstimatedEndTime.IsNull())
{
return false;
}
// From this point we know that mEstimatedEndTime and aEstimatedEndTime are not null and not the same.
// - when it changes to 0
if (aEstimatedEndTime.Value() == 0)
{
return true;
}
// - when it decreases
return (aEstimatedEndTime.Value() < mEstimatedEndTime.Value());
}
bool Instance::AddSupportedArea(AreaStructureWrapper & aNewArea)
{
// Does device mode allow this attribute to be updated?
if (!mDelegate->IsSupportedAreasChangeAllowed())
{
return false;
}
// Check there is space for the entry.
if (mDelegate->GetNumberOfSupportedAreas() >= kMaxNumSupportedAreas)
{
ChipLogError(Zcl, "AddSupportedArea %u - too many entries", aNewArea.areaID);
return false;
}
// Verify cluster requirements concerning valid fields and field relationships.
if (!IsValidSupportedArea(aNewArea))
{
ChipLogError(Zcl, "AddSupportedArea %u - not a valid location object", aNewArea.areaID);
return false;
}
// Each entry in Supported Areas SHALL have a unique value for the ID field.
// If the SupportedMaps attribute is not null, each entry in this list SHALL have a unique value for the combination of the
// MapID and AreaInfo fields. If the SupportedMaps attribute is null, each entry in this list SHALL have a unique value for
// the AreaInfo field.
if (!IsUniqueSupportedArea(aNewArea, false))
{
ChipLogError(Zcl, "AddSupportedArea %u - not a unique location object", aNewArea.areaID);
return false;
}
// Add the SupportedArea to the SupportedAreas attribute.
uint32_t ignoredIndex;
if (!mDelegate->AddSupportedArea(aNewArea, ignoredIndex))
{
return false;
}
NotifySupportedAreasChanged();
return true;
}
bool Instance::ModifySupportedArea(AreaStructureWrapper & aNewArea)
{
bool mapIDChanged = false;
uint32_t listIndex;
// get existing supported location to modify
AreaStructureWrapper supportedArea;
if (!mDelegate->GetSupportedAreaById(aNewArea.areaID, listIndex, supportedArea))
{
ChipLogError(Zcl, "ModifySupportedArea %u - not a supported areaID", aNewArea.areaID);
return false;
}
{
// check for mapID change
if ((aNewArea.mapID.IsNull() != supportedArea.mapID.IsNull()) ||
(!aNewArea.mapID.IsNull() && !supportedArea.mapID.IsNull() && (aNewArea.mapID.Value() != supportedArea.mapID.Value())))
{
// does device mode allow this attribute to be updated?
if (!mDelegate->IsSupportedAreasChangeAllowed())
{
return false;
}
mapIDChanged = true;
}
// verify cluster requirements concerning valid fields and field relationships
if (!IsValidSupportedArea(aNewArea))
{
ChipLogError(Zcl, "ModifySupportedArea %u - not a valid location object", aNewArea.areaID);
return false;
}
// Updated location description must not match another existing location description.
// We ignore comparing the area ID as one of the locations will match this one.
if (!IsUniqueSupportedArea(aNewArea, true))
{
ChipLogError(Zcl, "ModifySupportedArea %u - not a unique location object", aNewArea.areaID);
return false;
}
// Replace the supported location with the modified location.
if (!mDelegate->ModifySupportedArea(listIndex, aNewArea))
{
return false;
}
}
if (mapIDChanged)
{
mDelegate->HandleSupportedAreasUpdated();
}
NotifySupportedAreasChanged();
return true;
}
bool Instance::ClearSupportedAreas()
{
// does device mode allow this attribute to be updated?
if (!mDelegate->IsSupportedAreasChangeAllowed())
{
return false;
}
if (mDelegate->ClearSupportedAreas())
{
mDelegate->HandleSupportedAreasUpdated();
NotifySupportedAreasChanged();
return true;
}
return false;
}
//*************************************************************************
// Supported Maps manipulators
bool Instance::IsSupportedMap(uint32_t aMapId)
{
uint32_t ignoredIndex;
MapStructureWrapper ignoredMap;
return mDelegate->GetSupportedMapById(aMapId, ignoredIndex, ignoredMap);
}
bool Instance::AddSupportedMap(uint32_t aMapId, const CharSpan & aMapName)
{
// check max# of list entries
if (mDelegate->GetNumberOfSupportedMaps() >= kMaxNumSupportedMaps)
{
ChipLogError(Zcl, "AddSupportedMap %u - maximum number of entries", aMapId);
return false;
}
// Map name SHALL include readable text that describes the map name (cannot be empty string).
if (aMapName.empty())
{
ChipLogError(Zcl, "AddSupportedMap %u - Name must not be empty string", aMapId);
return false;
}
// Each entry in this list SHALL have a unique value for the Name field.
uint8_t mapIndex = 0;
MapStructureWrapper entry;
while (mDelegate->GetSupportedMapByIndex(mapIndex++, entry))
{
// the name cannot be the same as an existing map
if (entry.IsNameEqual(aMapName))
{
ChipLogError(Zcl, "AddSupportedMap %u - A map already exists with same name '%.*s'", aMapId,
static_cast<int>(entry.GetName().size()), entry.GetName().data());
return false;
}
// Each entry in this list SHALL have a unique value for the MapID field.
if (aMapId == entry.mapID)
{
ChipLogError(Zcl, "AddSupportedMap - non-unique Id %u", aMapId);
return false;
}
}
{
// add to supported maps attribute
MapStructureWrapper newMap(aMapId, aMapName);
uint32_t ignoredIndex;
if (!mDelegate->AddSupportedMap(newMap, ignoredIndex))
{
return false;
}
}
// map successfully added
NotifySupportedMapsChanged();
return true;
}
bool Instance::RenameSupportedMap(uint32_t aMapId, const CharSpan & newMapName)
{
uint32_t modifiedIndex;
MapStructureWrapper modifiedMap;
// get existing entry
if (!mDelegate->GetSupportedMapById(aMapId, modifiedIndex, modifiedMap))
{
ChipLogError(Zcl, "RenameSupportedMap Id %u - map does not exist", aMapId);
return false;
}
// Map name SHALL include readable text that describes the map's name. It cannot be empty string.
if (newMapName.empty())
{
ChipLogError(Zcl, "RenameSupportedMap %u - Name must not be empty string", aMapId);
return false;
}
// update the local copy of the map
modifiedMap.Set(modifiedMap.mapID, newMapName);
// Each entry in this list SHALL have a unique value for the Name field.
uint32_t loopIndex = 0;
MapStructureWrapper entry;
while (mDelegate->GetSupportedMapByIndex(loopIndex, entry))
{
if (modifiedIndex == loopIndex)
{
continue; // don't check local modified map against its own list entry
}
if (entry.IsNameEqual(newMapName))
{
ChipLogError(Zcl, "RenameSupportedMap %u - map already exists with same name '%.*s'", aMapId,
static_cast<int>(entry.GetName().size()), entry.GetName().data());
return false;
}
++loopIndex;
}
if (!mDelegate->ModifySupportedMap(modifiedIndex, modifiedMap))
{
return false;
}
// map successfully renamed
NotifySupportedMapsChanged();
return true;
}
bool Instance::ClearSupportedMaps()
{
// does device mode allow this attribute to be updated?
if (!mDelegate->IsSupportedMapChangeAllowed())
{
return false;
}
if (mDelegate->ClearSupportedMaps())
{
ClearSupportedAreas();
NotifySupportedMapsChanged();
return true;
}
return false;
}
//*************************************************************************
// Selected Areas manipulators
bool Instance::AddSelectedArea(uint32_t & aSelectedArea)
{
// check max# of list entries
if (mDelegate->GetNumberOfSelectedAreas() >= kMaxNumSelectedAreas)
{
ChipLogError(Zcl, "AddSelectedArea %u - maximum number of entries", aSelectedArea);
return false;
}
// each item in this list SHALL match the AreaID field of an entry on the SupportedAreas attribute's list
if (!IsSupportedArea(aSelectedArea))
{
ChipLogError(Zcl, "AddSelectedArea %u - not a supported location", aSelectedArea);
return false;
}
// each entry in this list SHALL have a unique value
if (mDelegate->IsSelectedArea(aSelectedArea))
{
ChipLogError(Zcl, "AddSelectedArea %u - duplicated location", aSelectedArea);
return false;
}
// Does device mode allow modification of selected locations?
char locationStatusBuffer[kMaxSizeStatusText];
MutableCharSpan locationStatusText(locationStatusBuffer);
if (!mDelegate->IsSetSelectedAreasAllowed(locationStatusText))
{
ChipLogError(Zcl, "AddSelectedArea %u - %.*s", aSelectedArea, static_cast<int>(locationStatusText.size()),
locationStatusText.data());
return false;
}
uint32_t ignoredIndex;
return mDelegate->AddSelectedArea(aSelectedArea, ignoredIndex);
}
bool Instance::ClearSelectedAreas()
{
if (mDelegate->ClearSelectedAreas())
{
NotifySelectedAreasChanged();
return true;
}
return false;
}
//*************************************************************************
// Current Area manipulators
DataModel::Nullable<uint32_t> Instance::GetCurrentArea()
{
return mCurrentArea;
}
bool Instance::SetCurrentArea(const DataModel::Nullable<uint32_t> & aCurrentArea)
{
// If not null, the value of this attribute SHALL match the AreaID field of an entry on the SupportedAreas attribute's
// list.
if ((!aCurrentArea.IsNull()) && (!IsSupportedArea(aCurrentArea.Value())))
{
ChipLogError(Zcl, "SetCurrentArea %u - location is not supported", aCurrentArea.Value());
return false;
}
bool notifyChange = mCurrentArea != aCurrentArea;
mCurrentArea = aCurrentArea;
if (notifyChange)
{
NotifyCurrentAreaChanged();
}
// EstimatedEndTime SHALL be null if the CurrentArea attribute is null.
if (mCurrentArea.IsNull())
{
SetEstimatedEndTime(DataModel::NullNullable);
}
return true;
}
//*************************************************************************
// Estimated End Time manipulators
DataModel::Nullable<uint32_t> Instance::GetEstimatedEndTime()
{
return mEstimatedEndTime;
}
bool Instance::SetEstimatedEndTime(const DataModel::Nullable<uint32_t> & aEstimatedEndTime)
{
// EstimatedEndTime SHALL be null if the CurrentArea attribute is null.
if (mCurrentArea.IsNull() && !aEstimatedEndTime.IsNull())
{
ChipLogError(Zcl, "SetEstimatedEndTime - must be null if Current Area is null");
return false;
}
bool notifyChange = ReportEstimatedEndTimeChange(aEstimatedEndTime);
mEstimatedEndTime = aEstimatedEndTime;
if (notifyChange)
{
NotifyEstimatedEndTimeChanged();
}
// success
return true;
}
//*************************************************************************
// Progress list manipulators
bool Instance::AddPendingProgressElement(uint32_t aAreaId)
{
// create progress element
Structs::ProgressStruct::Type inactiveProgress = { aAreaId, OperationalStatusEnum::kPending };
// check max# of list entries
if (mDelegate->GetNumberOfProgressElements() >= kMaxNumProgressElements)
{
ChipLogError(Zcl, "AddPendingProgressElement - maximum number of entries");
return false;
}
// For each entry in this list, the AreaID field SHALL match an entry on the SupportedAreas attribute's list.
if (!IsSupportedArea(aAreaId))
{
ChipLogError(Zcl, "AddPendingProgressElement - not a supported location %u", aAreaId);
return false;
}
// Each entry in this list SHALL have a unique value for the AreaID field.
if (mDelegate->IsProgressElement(aAreaId))
{
ChipLogError(Zcl, "AddPendingProgressElement - progress element already exists for location %u", aAreaId);
return false;
}
uint32_t ignoredIndex;
if (!mDelegate->AddProgressElement(inactiveProgress, ignoredIndex))
{
return false;
}
NotifyProgressChanged();
return true;
}
bool Instance::SetProgressStatus(uint32_t aAreaId, OperationalStatusEnum opStatus)
{
uint32_t listIndex;
Structs::ProgressStruct::Type progressElement;
if (!mDelegate->GetProgressElementById(aAreaId, listIndex, progressElement))
{
ChipLogError(Zcl, "SetProgressStatus - progress element does not exist for location %u", aAreaId);
return false;
}
// If the status value is not changing, there in no need to modify the existing element.
if (progressElement.status == opStatus)
{
return true;
}
// set the progress status in the local copy
progressElement.status = opStatus;
// TotalOperationalTime SHALL be null if the Status field is not set to Completed or Skipped.