forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon-off-server.cpp
1018 lines (867 loc) · 38.1 KB
/
on-off-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) 2020 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 "on-off-server.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/data-model/Nullable.h>
#include <app/reporting/reporting.h>
#include <app/util/af.h>
#include <app/util/config.h>
#include <app/util/util.h>
#include <protocols/interaction_model/StatusCode.h>
#include <tracing/macros.h>
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
#include <app/clusters/scenes-server/scenes-server.h>
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
#include <app/clusters/level-control/level-control.h>
#endif // MATTER_DM_PLUGIN_LEVEL_CONTROL
#ifdef MATTER_DM_PLUGIN_MODE_BASE
// nogncheck because the gn dependency checker does not understand
// conditional includes, so will fail in an application that has an On/Off
// cluster but no ModeBase-derived cluster.
#include <app/clusters/mode-base-server/mode-base-cluster-objects.h> // nogncheck
#include <app/clusters/mode-base-server/mode-base-server.h> // nogncheck
#endif // MATTER_DM_PLUGIN_MODE_BASE
#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>
using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OnOff;
using chip::Protocols::InteractionModel::Status;
namespace {
#ifdef MATTER_DM_PLUGIN_MODE_BASE
/**
* For all ModeBase alias clusters on the given endpoint, if the OnOff feature is supported and
* the OnMode attribute is set, update the CurrentMode attribute value to the OnMode value.
* @param endpoint
*/
void UpdateModeBaseCurrentModeToOnMode(EndpointId endpoint)
{
for (auto & modeBaseInstance : ModeBase::GetModeBaseInstanceList())
{
if (modeBaseInstance.GetEndpointId() == endpoint)
{
if (modeBaseInstance.HasFeature(ModeBase::Feature::kOnOff))
{
ModeBase::Attributes::OnMode::TypeInfo::Type onMode = modeBaseInstance.GetOnMode();
if (!onMode.IsNull())
{
Status status = modeBaseInstance.UpdateCurrentMode(onMode.Value());
if (status == Status::Success)
{
ChipLogProgress(Zcl, "Changed the Current Mode to %x", onMode.Value());
}
else
{
ChipLogError(Zcl, "Failed to Changed the Current Mode to %x: %u", onMode.Value(), to_underlying(status));
}
}
}
}
}
}
#endif // MATTER_DM_PLUGIN_MODE_BASE
} // namespace
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
static bool LevelControlWithOnOffFeaturePresent(EndpointId endpoint)
{
if (!emberAfContainsServer(endpoint, LevelControl::Id))
{
return false;
}
return LevelControlHasFeature(endpoint, LevelControl::Feature::kOnOff);
}
#endif // MATTER_DM_PLUGIN_LEVEL_CONTROL
static constexpr size_t kOnOffMaxEnpointCount =
MATTER_DM_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
#if defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
static void sceneOnOffCallback(EndpointId endpoint);
using OnOffEndPointPair = scenes::DefaultSceneHandlerImpl::EndpointStatePair<bool>;
using OnOffTransitionTimeInterface =
scenes::DefaultSceneHandlerImpl::TransitionTimeInterface<kOnOffMaxEnpointCount, MATTER_DM_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT>;
class DefaultOnOffSceneHandler : public scenes::DefaultSceneHandlerImpl
{
public:
DefaultSceneHandlerImpl::StatePairBuffer<bool, kOnOffMaxEnpointCount> mSceneEndpointStatePairs;
// As per spec, 1 attribute is scenable in the on off cluster
static constexpr uint8_t scenableAttributeCount = 1;
DefaultOnOffSceneHandler() = default;
~DefaultOnOffSceneHandler() override {}
// Default function for OnOff cluster, only puts the OnOff cluster ID in the span if supported on the given endpoint
virtual void GetSupportedClusters(EndpointId endpoint, Span<ClusterId> & clusterBuffer) override
{
ClusterId * buffer = clusterBuffer.data();
if (emberAfContainsServer(endpoint, OnOff::Id) && clusterBuffer.size() >= 1)
{
buffer[0] = OnOff::Id;
clusterBuffer.reduce_size(1);
}
else
{
clusterBuffer.reduce_size(0);
}
}
// Default function for OnOff cluster, only checks if OnOff is enabled on the endpoint
bool SupportsCluster(EndpointId endpoint, ClusterId cluster) override
{
return (cluster == OnOff::Id) && (emberAfContainsServer(endpoint, OnOff::Id));
}
/// @brief Serialize the Cluster's EFS value
/// @param endpoint target endpoint
/// @param cluster target cluster
/// @param serializedBytes data to serialize into EFS
/// @return CHIP_NO_ERROR if successfully serialized the data, CHIP_ERROR_INVALID_ARGUMENT otherwise
CHIP_ERROR SerializeSave(EndpointId endpoint, ClusterId cluster, MutableByteSpan & serializedBytes) override
{
using AttributeValuePair = ScenesManagement::Structs::AttributeValuePair::Type;
bool currentValue;
// read current on/off value
Status status = Attributes::OnOff::Get(endpoint, ¤tValue);
if (status != Status::Success)
{
ChipLogError(Zcl, "ERR: reading on/off %x", to_underlying(status));
return CHIP_ERROR_READ_FAILED;
}
AttributeValuePair pairs[scenableAttributeCount];
pairs[0].attributeID = Attributes::OnOff::Id;
pairs[0].attributeValue = currentValue;
app::DataModel::List<AttributeValuePair> attributeValueList(pairs);
return EncodeAttributeValueList(attributeValueList, serializedBytes);
}
/// @brief Default EFS interaction when applying scene to the OnOff Cluster
/// @param endpoint target endpoint
/// @param cluster target cluster
/// @param serializedBytes Data from nvm
/// @param timeMs transition time in ms
/// @return CHIP_NO_ERROR if value as expected, CHIP_ERROR_INVALID_ARGUMENT otherwise
CHIP_ERROR ApplyScene(EndpointId endpoint, ClusterId cluster, const ByteSpan & serializedBytes,
scenes::TransitionTimeMs timeMs) override
{
app::DataModel::DecodableList<ScenesManagement::Structs::AttributeValuePair::DecodableType> attributeValueList;
VerifyOrReturnError(cluster == OnOff::Id, CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(DecodeAttributeValueList(serializedBytes, attributeValueList));
size_t attributeCount = 0;
ReturnErrorOnFailure(attributeValueList.ComputeSize(&attributeCount));
VerifyOrReturnError(attributeCount <= scenableAttributeCount, CHIP_ERROR_BUFFER_TOO_SMALL);
auto pair_iterator = attributeValueList.begin();
while (pair_iterator.Next())
{
auto & decodePair = pair_iterator.GetValue();
VerifyOrReturnError(decodePair.attributeID == Attributes::OnOff::Id, CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(
mSceneEndpointStatePairs.InsertPair(OnOffEndPointPair(endpoint, static_cast<bool>(decodePair.attributeValue))));
}
// Verify that the EFS was completely read
CHIP_ERROR err = pair_iterator.GetStatus();
if (CHIP_NO_ERROR != err)
{
mSceneEndpointStatePairs.RemovePair(endpoint);
return err;
}
// This handler assumes it is being used with the default handler for the level control. Therefore if the level control
// cluster with on off feature is present on the endpoint and the level control handler is registered, it assumes this
// handler will take action on the on-off state. This assumes the level control attributes were also saved in the scene.
// This is to prevent a behavior where the on off state is set by this handler, and then the level control handler or vice
// versa.
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
if (!(LevelControlWithOnOffFeaturePresent(endpoint) &&
ScenesManagement::ScenesServer::Instance().IsHandlerRegistered(endpoint, LevelControlServer::GetSceneHandler())))
#endif
{
VerifyOrReturnError(mTransitionTimeInterface.sceneEventControl(endpoint) != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
OnOffServer::Instance().scheduleTimerCallbackMs(mTransitionTimeInterface.sceneEventControl(endpoint), timeMs);
}
return CHIP_NO_ERROR;
}
private:
OnOffTransitionTimeInterface mTransitionTimeInterface = OnOffTransitionTimeInterface(OnOff::Id, sceneOnOffCallback);
};
static DefaultOnOffSceneHandler sOnOffSceneHandler;
static void sceneOnOffCallback(EndpointId endpoint)
{
OnOffEndPointPair savedState;
ReturnOnFailure(sOnOffSceneHandler.mSceneEndpointStatePairs.GetPair(endpoint, savedState));
CommandId command = (savedState.mValue) ? Commands::On::Id : Commands::Off::Id;
OnOffServer::Instance().setOnOffValue(endpoint, command, false);
ReturnOnFailure(sOnOffSceneHandler.mSceneEndpointStatePairs.RemovePair(endpoint));
}
#endif // defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
/**********************************************************
* Attributes Definition
*********************************************************/
static OnOffEffect * firstEffect = nullptr;
OnOffServer OnOffServer::instance;
static EmberEventControl gEventControls[kOnOffMaxEnpointCount];
/**********************************************************
* Function definition
*********************************************************/
static OnOffEffect * inst(EndpointId endpoint);
/**********************************************************
* Matter timer scheduling glue logic
*********************************************************/
void OnOffServer::timerCallback(System::Layer *, void * callbackContext)
{
auto control = static_cast<EmberEventControl *>(callbackContext);
(control->callback)(control->endpoint);
}
void OnOffServer::scheduleTimerCallbackMs(EmberEventControl * control, uint32_t delayMs)
{
CHIP_ERROR err = DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(delayMs), timerCallback, control);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "OnOff Server failed to schedule event: %" CHIP_ERROR_FORMAT, err.Format());
}
}
void OnOffServer::cancelEndpointTimerCallback(EmberEventControl * control)
{
DeviceLayer::SystemLayer().CancelTimer(timerCallback, control);
}
void OnOffServer::cancelEndpointTimerCallback(EndpointId endpoint)
{
auto control = OnOffServer::getEventControl(endpoint, Span<EmberEventControl>(gEventControls));
if (control)
{
cancelEndpointTimerCallback(control);
}
}
void MatterOnOffClusterServerShutdownCallback(EndpointId endpoint)
{
ChipLogProgress(Zcl, "Shuting down on/off server cluster on endpoint %d", endpoint);
OnOffServer::Instance().cancelEndpointTimerCallback(endpoint);
}
/**********************************************************
* OnOff Implementation
*********************************************************/
OnOffServer & OnOffServer::Instance()
{
return instance;
}
chip::scenes::SceneHandler * OnOffServer::GetSceneHandler()
{
#if defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
return &sOnOffSceneHandler;
#else
return nullptr;
#endif // defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
}
bool OnOffServer::HasFeature(chip::EndpointId endpoint, Feature feature)
{
bool success;
uint32_t featureMap;
success = (Attributes::FeatureMap::Get(endpoint, &featureMap) == Status::Success);
return success ? ((featureMap & to_underlying(feature)) != 0) : false;
}
Status OnOffServer::getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue)
{
// read current on/off value
Status status = Attributes::OnOff::Get(endpoint, currentOnOffValue);
if (status != Status::Success)
{
ChipLogProgress(Zcl, "ERR: reading on/off %x", to_underlying(status));
}
ChipLogProgress(Zcl, "On/Off ep%d value: %d", endpoint, *currentOnOffValue);
return status;
}
/** @brief On/off Cluster Set Value
*
* This function is called when the on/off value needs to be set, either through
* normal channels or as a result of a level change.
*
* @param endpoint Ver.: always
* @param command Ver.: always
* @param initiatedByLevelChange Ver.: always
* @param forceSend Send value of the On/Off even when it is the same as current On/Off value.
* This parameter is useful at the start when you try to determine startup state of On/Off relay that does not store state after
* losing power
*/
Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange, bool forceSend)
{
MATTER_TRACE_SCOPE("setOnOffValue", "OnOff");
Status status;
bool currentValue, newValue;
// read current on/off value
status = Attributes::OnOff::Get(endpoint, ¤tValue);
if (status != Status::Success)
{
ChipLogProgress(Zcl, "ERR: reading on/off %x", to_underlying(status));
return status;
}
if (forceSend)
{
if (command == Commands::Off::Id)
{
newValue = false;
}
else if (command == Commands::On::Id)
{
newValue = true;
}
// I guess that, I can only get ON/OFF at startup
}
else
{
// if the value is already what we want to set it to then do nothing
if ((!currentValue && command == Commands::Off::Id) || (currentValue && command == Commands::On::Id))
{
ChipLogProgress(Zcl, "Endpoint %x On/off already set to new value", endpoint);
return Status::Success;
}
// we either got a toggle, or an on when off, or an off when on,
// so we need to swap the value
newValue = !currentValue;
ChipLogProgress(Zcl, "Toggle ep%x on/off from state %x to %x", endpoint, currentValue, newValue);
}
// the sequence of updating on/off attribute and kick off level change effect should
// be depend on whether we are turning on or off. If we are turning on the light, we
// should update the on/off attribute before kicking off level change, if we are
// turning off the light, we should do the opposite, that is kick off level change
// before updating the on/off attribute.
if (newValue) // Set On
{
if (SupportsLightingApplications(endpoint))
{
uint16_t onTime = 0;
Attributes::OnTime::Get(endpoint, &onTime);
if (onTime == 0)
{
ChipLogProgress(Zcl, "On Command - OffWaitTime : 0");
Attributes::OffWaitTime::Set(endpoint, 0);
// Stop timer on the endpoint
EmberEventControl * event = getEventControl(endpoint, Span<EmberEventControl>(gEventControls));
if (event != nullptr)
{
cancelEndpointTimerCallback(event);
ChipLogProgress(Zcl, "On/Toggle Command - Stop Timer");
}
}
Attributes::GlobalSceneControl::Set(endpoint, true);
}
// write the new on/off value
status = Attributes::OnOff::Set(endpoint, newValue);
if (status != Status::Success)
{
ChipLogProgress(Zcl, "ERR: writing on/off %x", to_underlying(status));
return status;
}
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
// If initiatedByLevelChange is false, then we assume that the level change
// ZCL stuff has not happened and we do it here
if (!initiatedByLevelChange && LevelControlWithOnOffFeaturePresent(endpoint))
{
emberAfOnOffClusterLevelControlEffectCallback(endpoint, newValue);
}
#endif
#ifdef MATTER_DM_PLUGIN_MODE_SELECT
// If OnMode is not a null value, then change the current mode to it.
if (emberAfContainsServer(endpoint, ModeSelect::Id) &&
emberAfContainsAttribute(endpoint, ModeSelect::Id, ModeSelect::Attributes::OnMode::Id))
{
ModeSelect::Attributes::OnMode::TypeInfo::Type onMode;
if (ModeSelect::Attributes::OnMode::Get(endpoint, onMode) == Status::Success && !onMode.IsNull())
{
ChipLogProgress(Zcl, "Changing Current Mode to %x", onMode.Value());
status = ModeSelect::Attributes::CurrentMode::Set(endpoint, onMode.Value());
}
}
#endif
#ifdef MATTER_DM_PLUGIN_MODE_BASE
// If OnMode is not a null value, then change the current mode to it.
UpdateModeBaseCurrentModeToOnMode(endpoint);
#endif
}
else // Set Off
{
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
// If initiatedByLevelChange is false, then we assume that the level change
// ZCL stuff has not happened and we do it here
if (!initiatedByLevelChange && LevelControlWithOnOffFeaturePresent(endpoint))
{
emberAfOnOffClusterLevelControlEffectCallback(endpoint, newValue);
}
else
#endif
{
// write the new on/off value
status = Attributes::OnOff::Set(endpoint, newValue);
if (status != Status::Success)
{
ChipLogProgress(Zcl, "ERR: writing on/off %x", to_underlying(status));
return status;
}
if (SupportsLightingApplications(endpoint))
{
ChipLogProgress(Zcl, "Off completed. reset OnTime to 0");
Attributes::OnTime::Set(endpoint, 0); // Reset onTime
}
}
}
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
// the scene has been changed (the value of on/off has changed) so
// the current scene as described in the attribute table is invalid,
// so mark it as invalid (just writes the valid/invalid attribute)
ScenesManagement::ScenesServer::Instance().MakeSceneInvalidForAllFabrics(endpoint);
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
// The returned status is based solely on the On/Off cluster. Errors in the
// Level Control and/or Scenes cluster are ignored.
return Status::Success;
}
void OnOffServer::initOnOffServer(chip::EndpointId endpoint)
{
#ifndef IGNORE_ON_OFF_CLUSTER_START_UP_ON_OFF
// StartUp behavior relies on OnOff and StartUpOnOff attributes being non-volatile.
if (SupportsLightingApplications(endpoint) && areStartUpOnOffServerAttributesNonVolatile(endpoint))
{
// Read the StartUpOnOff attribute and set the OnOff attribute as per
// following from zcl 7 14-0127-20i-zcl-ch-3-general.doc.
// 3.8.2.2.5 StartUpOnOff Attribute
// The StartUpOnOff attribute SHALL define the desired startup behavior of a
// lamp device when it is supplied with power and this state SHALL be
// reflected in the OnOff attribute. The values of the StartUpOnOff
// attribute are listed below.
// Table 3 46. Values of the StartUpOnOff Attribute
// Value Action on power up
// 0x00 Set the OnOff attribute to 0 (off).
// 0x01 Set the OnOff attribute to 1 (on).
// 0x02 If the previous value of the OnOff attribute is equal to 0,
// set the OnOff attribute to 1.If the previous value of the OnOff
// attribute is equal to 1, set the OnOff attribute to 0 (toggle).
// 0x03-0xfe These values are reserved. No action.
// 0xff This value cannot happen.
// null Set the OnOff attribute to its previous value.
bool onOffValueForStartUp = false;
Status status = getOnOffValueForStartUp(endpoint, onOffValueForStartUp);
if (status == Status::Success)
{
status = setOnOffValue(endpoint, onOffValueForStartUp, true, true);
}
#if defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
// Registers Scene handlers for the On/Off cluster on the server
app::Clusters::ScenesManagement::ScenesServer::Instance().RegisterSceneHandler(endpoint,
OnOffServer::Instance().GetSceneHandler());
#endif // defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS
#ifdef MATTER_DM_PLUGIN_MODE_SELECT
// If OnMode is not a null value, then change the current mode to it.
if (onOffValueForStartUp && emberAfContainsServer(endpoint, ModeSelect::Id) &&
emberAfContainsAttribute(endpoint, ModeSelect::Id, ModeSelect::Attributes::OnMode::Id))
{
ModeSelect::Attributes::OnMode::TypeInfo::Type onMode;
if (ModeSelect::Attributes::OnMode::Get(endpoint, onMode) == Status::Success && !onMode.IsNull())
{
ChipLogProgress(Zcl, "Changing Current Mode to %x", onMode.Value());
status = ModeSelect::Attributes::CurrentMode::Set(endpoint, onMode.Value());
}
}
#endif
}
#endif // IGNORE_ON_OFF_CLUSTER_START_UP_ON_OFF
emberAfPluginOnOffClusterServerPostInitCallback(endpoint);
}
/** @brief Get the OnOff value when server starts.
*
* This function determines how StartUpOnOff affects the OnOff value when the server starts.
*
* @param endpoint Ver.: always
* @param onOffValueForStartUp Ver.: always
*/
Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & onOffValueForStartUp)
{
app::DataModel::Nullable<OnOff::StartUpOnOffEnum> startUpOnOff;
Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff);
if (status == Status::Success)
{
// Initialise updated value to 0
bool updatedOnOff = false;
status = Attributes::OnOff::Get(endpoint, &updatedOnOff);
if (status == Status::Success)
{
if (!startUpOnOff.IsNull())
{
switch (startUpOnOff.Value())
{
case OnOff::StartUpOnOffEnum::kOff:
updatedOnOff = false; // Off
break;
case OnOff::StartUpOnOffEnum::kOn:
updatedOnOff = true; // On
break;
case OnOff::StartUpOnOffEnum::kToggle:
updatedOnOff = !updatedOnOff;
break;
default:
// All other values 0x03- 0xFE are reserved - no action.
break;
}
}
onOffValueForStartUp = updatedOnOff;
}
}
return status;
}
bool OnOffServer::offCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath)
{
MATTER_TRACE_SCOPE("OffCommand", "OnOff");
Status status = setOnOffValue(commandPath.mEndpointId, Commands::Off::Id, false);
commandObj->AddStatus(commandPath, status);
return true;
}
bool OnOffServer::onCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath)
{
MATTER_TRACE_SCOPE("OnCommand", "OnOff");
Status status = setOnOffValue(commandPath.mEndpointId, Commands::On::Id, false);
commandObj->AddStatus(commandPath, status);
return true;
}
bool OnOffServer::toggleCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath)
{
MATTER_TRACE_SCOPE("ToggleCommand", "OnOff");
Status status = setOnOffValue(commandPath.mEndpointId, Commands::Toggle::Id, false);
commandObj->AddStatus(commandPath, status);
return true;
}
bool OnOffServer::offWithEffectCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::OffWithEffect::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("offWithEffectCommand", "OnOff");
auto effectId = commandData.effectIdentifier;
auto effectVariant = commandData.effectVariant;
chip::EndpointId endpoint = commandPath.mEndpointId;
Status status = Status::Success;
if (SupportsLightingApplications(endpoint))
{
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
FabricIndex fabric = commandObj->GetAccessingFabricIndex();
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
bool globalSceneControl = false;
OnOff::Attributes::GlobalSceneControl::Get(endpoint, &globalSceneControl);
bool isOnBeforeCommand = false;
OnOff::Attributes::OnOff::Get(endpoint, &isOnBeforeCommand);
if (globalSceneControl)
{
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
GroupId groupId = ZCL_SCENES_GLOBAL_SCENE_GROUP_ID;
if (commandObj->GetExchangeContext()->IsGroupExchangeContext())
{
groupId = commandObj->GetExchangeContext()->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
}
ScenesManagement::ScenesServer::Instance().StoreCurrentScene(fabric, endpoint, groupId,
ZCL_SCENES_GLOBAL_SCENE_SCENE_ID);
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
OnOff::Attributes::GlobalSceneControl::Set(endpoint, false);
}
// Only apply effect if OnOff is on
if (isOnBeforeCommand)
{
OnOffEffect * effect = inst(endpoint);
if (effect != nullptr && effect->mOffWithEffectTrigger != nullptr)
{
effect->mEffectIdentifier = effectId;
effect->mEffectVariant = effectVariant;
effect->mOffWithEffectTrigger(effect);
}
}
status = setOnOffValue(endpoint, Commands::Off::Id, false);
}
else
{
status = Status::UnsupportedCommand;
}
commandObj->AddStatus(commandPath, status);
return true;
}
bool OnOffServer::OnWithRecallGlobalSceneCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath)
{
MATTER_TRACE_SCOPE("OnWithRecallGlobalSceneCommand", "OnOff");
chip::EndpointId endpoint = commandPath.mEndpointId;
if (!SupportsLightingApplications(endpoint))
{
commandObj->AddStatus(commandPath, Status::UnsupportedCommand);
return true;
}
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
FabricIndex fabric = commandObj->GetAccessingFabricIndex();
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
bool globalSceneControl = false;
OnOff::Attributes::GlobalSceneControl::Get(endpoint, &globalSceneControl);
if (globalSceneControl)
{
commandObj->AddStatus(commandPath, Status::Success);
return true;
}
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
GroupId groupId = ZCL_SCENES_GLOBAL_SCENE_GROUP_ID;
if (commandObj->GetExchangeContext()->IsGroupExchangeContext())
{
groupId = commandObj->GetExchangeContext()->GetSessionHandle()->AsIncomingGroupSession()->GetGroupId();
}
ScenesManagement::ScenesServer::Instance().RecallScene(fabric, endpoint, groupId, ZCL_SCENES_GLOBAL_SCENE_SCENE_ID);
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
OnOff::Attributes::GlobalSceneControl::Set(endpoint, true);
setOnOffValue(endpoint, Commands::On::Id, false);
commandObj->AddStatus(commandPath, Status::Success);
return true;
}
uint32_t OnOffServer::calculateNextWaitTimeMS()
{
const chip::System::Clock::Timestamp currentTime = chip::System::SystemClock().GetMonotonicTimestamp();
chip::System::Clock::Timestamp waitTime = ON_OFF_UPDATE_TIME_MS;
chip::System::Clock::Timestamp latency;
if (currentTime > nextDesiredOnWithTimedOffTimestamp)
{
latency = currentTime - nextDesiredOnWithTimedOffTimestamp;
if (latency >= ON_OFF_UPDATE_TIME_MS)
waitTime = chip::System::Clock::Milliseconds32(1);
else
waitTime -= latency;
}
nextDesiredOnWithTimedOffTimestamp += ON_OFF_UPDATE_TIME_MS;
return (uint32_t) waitTime.count();
}
bool OnOffServer::OnWithTimedOffCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::OnWithTimedOff::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("OnWithTimedOffCommand", "OnOff");
BitFlags<OnOffControlBitmap> onOffControl = commandData.onOffControl;
uint16_t onTime = commandData.onTime;
uint16_t offWaitTime = commandData.offWaitTime;
Status status = Status::Success;
chip::EndpointId endpoint = commandPath.mEndpointId;
bool isOn = false;
uint16_t currentOffWaitTime = MAX_ON_OFF_TIME_VALUE;
uint16_t currentOnTime = 0;
EmberEventControl * event = configureEventControl(endpoint);
VerifyOrExit(event != nullptr, status = Status::UnsupportedEndpoint);
VerifyOrExit(SupportsLightingApplications(endpoint), status = Status::UnsupportedCommand);
OnOff::Attributes::OnOff::Get(endpoint, &isOn);
// OnOff is off and the commands is only accepted if on
if (onOffControl.Has(OnOffControlBitmap::kAcceptOnlyWhenOn) && !isOn)
{
commandObj->AddStatus(commandPath, Status::Success);
return true;
}
OnOff::Attributes::OffWaitTime::Get(endpoint, ¤tOffWaitTime);
OnOff::Attributes::OnTime::Get(endpoint, ¤tOnTime);
if (currentOffWaitTime > 0 && !isOn)
{
uint16_t newOffWaitTime = currentOffWaitTime < offWaitTime ? currentOffWaitTime : offWaitTime;
OnOff::Attributes::OffWaitTime::Set(endpoint, newOffWaitTime);
currentOffWaitTime = newOffWaitTime;
}
else
{
uint16_t newOnTime = currentOnTime > onTime ? currentOnTime : onTime;
OnOff::Attributes::OnTime::Set(endpoint, newOnTime);
OnOff::Attributes::OffWaitTime::Set(endpoint, offWaitTime);
setOnOffValue(endpoint, Commands::On::Id, false);
currentOnTime = newOnTime;
currentOffWaitTime = offWaitTime;
}
ChipLogProgress(Zcl, "On Time: %d | off wait Time: %d", currentOnTime, currentOffWaitTime);
if (currentOnTime < MAX_ON_OFF_TIME_VALUE && currentOffWaitTime < MAX_ON_OFF_TIME_VALUE)
{
nextDesiredOnWithTimedOffTimestamp = chip::System::SystemClock().GetMonotonicTimestamp() + ON_OFF_UPDATE_TIME_MS;
scheduleTimerCallbackMs(configureEventControl(endpoint), ON_OFF_UPDATE_TIME_MS.count());
}
exit:
commandObj->AddStatus(commandPath, status);
return true;
}
/**
* @brief Updates OnOff values after timer is finished
*
* @param[in] endpoint endpoint associated with the finished timer
*/
void OnOffServer::updateOnOffTimeCommand(chip::EndpointId endpoint)
{
ChipLogDetail(Zcl, "Timer callback - Entering callback");
bool isOn = false;
OnOff::Attributes::OnOff::Get(endpoint, &isOn);
if (isOn) // OnOff On case
{
// Restart Timer
scheduleTimerCallbackMs(configureEventControl(endpoint), calculateNextWaitTimeMS());
// Update onTime values
uint16_t onTime = MIN_ON_OFF_TIME_VALUE;
OnOff::Attributes::OnTime::Get(endpoint, &onTime);
ChipLogDetail(Zcl, "Timer callback - On Time: %d", onTime);
if (onTime > 0)
{
onTime--;
OnOff::Attributes::OnTime::Set(endpoint, onTime);
}
if (onTime == 0)
{
ChipLogDetail(Zcl, "Timer callback - Turning off OnOff");
OnOff::Attributes::OffWaitTime::Set(endpoint, 0);
setOnOffValue(endpoint, Commands::Off::Id, false);
}
}
else // OnOff Off Case
{
uint16_t offWaitTime = 0;
OnOff::Attributes::OffWaitTime::Get(endpoint, &offWaitTime);
// Validate before decreasing counter
if (offWaitTime > 0)
{
offWaitTime--;
OnOff::Attributes::OffWaitTime::Set(endpoint, offWaitTime);
}
ChipLogDetail(Zcl, "Timer Callback - wait Off Time: %d", offWaitTime);
// Validate if necessary to restart timer
if (offWaitTime > 0)
{
// Restart Timer
scheduleTimerCallbackMs(configureEventControl(endpoint), calculateNextWaitTimeMS());
}
else
{
ChipLogProgress(Zcl, "Timer Callback - wait Off Time cycle finished");
// Stop timer on the endpoint
cancelEndpointTimerCallback(getEventControl(endpoint, Span<EmberEventControl>(gEventControls)));
}
}
}
#ifndef IGNORE_ON_OFF_CLUSTER_START_UP_ON_OFF
bool OnOffServer::areStartUpOnOffServerAttributesNonVolatile(EndpointId endpoint)
{
return !emberAfIsKnownVolatileAttribute(endpoint, OnOff::Id, Attributes::OnOff::Id) &&
!emberAfIsKnownVolatileAttribute(endpoint, OnOff::Id, Attributes::StartUpOnOff::Id);
}
#endif // IGNORE_ON_OFF_CLUSTER_START_UP_ON_OFF
/**
* @brief event control object for an endpoint
*
* @param[in] endpoint target endpoint
* @param[in] eventControlArray Array where to find the event control
* @param[in] eventControlArraySize Size of the event control array
* @return EmberEventControl* configured event control
*/
EmberEventControl * OnOffServer::getEventControl(EndpointId endpoint, const Span<EmberEventControl> & eventControlArray)
{
uint16_t index = emberAfGetClusterServerEndpointIndex(endpoint, OnOff::Id, MATTER_DM_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT);
if (index >= eventControlArray.size())
{
return nullptr;
}
return &eventControlArray[index];
}
/**
* @brief Configures EventControl callback when using XY colors
*
* @param[in] endpoint endpoint to start timer for
* @return EmberEventControl* configured event control
*/
EmberEventControl * OnOffServer::configureEventControl(EndpointId endpoint)
{
EmberEventControl * controller = getEventControl(endpoint, Span<EmberEventControl>(gEventControls));
VerifyOrReturnError(controller != nullptr, nullptr);
controller->endpoint = endpoint;
controller->callback = &onOffWaitTimeOffEventHandler;
return controller;
}
/**********************************************************
* OnOffEffect Implementation
*********************************************************/
static OnOffEffect * inst(EndpointId endpoint)
{
OnOffEffect * current = firstEffect;
while (current != nullptr && current->mEndpoint != endpoint)
{
current = current->next();
}
return current;
}
static inline void reg(OnOffEffect * inst)
{
inst->setNext(firstEffect);
firstEffect = inst;
}
static inline void unreg(OnOffEffect * inst)
{
if (firstEffect == inst)
{
firstEffect = firstEffect->next();
}
else
{
OnOffEffect * previous = firstEffect;
OnOffEffect * current = firstEffect->next();
while (current != nullptr && current != inst)
{
previous = current;
current = current->next();
}
if (current != nullptr)
{
previous->setNext(current->next());
}
}
}
OnOffEffect::OnOffEffect(chip::EndpointId endpoint, OffWithEffectTriggerCommand offWithEffectTrigger,
EffectIdentifierEnum effectIdentifier, uint8_t effectVariant) :
mEndpoint(endpoint),
mOffWithEffectTrigger(offWithEffectTrigger), mEffectIdentifier(effectIdentifier), mEffectVariant(effectVariant)
{
reg(this);
};
OnOffEffect::~OnOffEffect()
{
unreg(this);
};
/**********************************************************
* Callbacks Implementation
*********************************************************/
bool emberAfOnOffClusterOffCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::Off::DecodableType & commandData)
{
return OnOffServer::Instance().offCommand(commandObj, commandPath);
}
bool emberAfOnOffClusterOnCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::On::DecodableType & commandData)
{
return OnOffServer::Instance().onCommand(commandObj, commandPath);
}
bool emberAfOnOffClusterToggleCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::Toggle::DecodableType & commandData)
{
return OnOffServer::Instance().toggleCommand(commandObj, commandPath);
}
bool emberAfOnOffClusterOffWithEffectCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::OffWithEffect::DecodableType & commandData)
{
return OnOffServer::Instance().offWithEffectCommand(commandObj, commandPath, commandData);
}
bool emberAfOnOffClusterOnWithRecallGlobalSceneCallback(app::CommandHandler * commandObj,
const app::ConcreteCommandPath & commandPath,
const Commands::OnWithRecallGlobalScene::DecodableType & commandData)
{
return OnOffServer::Instance().OnWithRecallGlobalSceneCommand(commandObj, commandPath);
}
bool emberAfOnOffClusterOnWithTimedOffCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,