1
+ /*
2
+ *
3
+ * Copyright (c) 2024 Project CHIP Authors
4
+ * All rights reserved.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include < DeviceEnergyManagementManager.h>
20
+ #include < device-energy-management-modes.h>
21
+ #include < EnergyManagementAppCmdLineOptions.h>
22
+
23
+ #include < app-common/zap-generated/ids/Attributes.h>
24
+ #include < app-common/zap-generated/ids/Clusters.h>
25
+ #include < app/ConcreteAttributePath.h>
26
+ #include < app/server/Server.h>
27
+ #include < lib/support/logging/CHIPLogging.h>
28
+
29
+ static constexpr int DEM_ENDPOINT = 1 ;
30
+
31
+ using namespace chip ;
32
+ using namespace chip ::app;
33
+ using namespace chip ::app::Clusters;
34
+ using namespace chip ::app::Clusters::DeviceEnergyManagement;
35
+
36
+ std::unique_ptr<DeviceEnergyManagementDelegate> gDEMDelegate ;
37
+ std::unique_ptr<DeviceEnergyManagementManager> gDEMInstance ;
38
+
39
+ DeviceEnergyManagement::DeviceEnergyManagementDelegate * GetDEMDelegate ()
40
+ {
41
+ VerifyOrDieWithMsg (gDEMDelegate .get () != nullptr , AppServer, " DEM Delegate is null" );
42
+
43
+ return gDEMDelegate .get ();
44
+ }
45
+
46
+ /*
47
+ * @brief Creates a Delegate and Instance for DEM
48
+ *
49
+ * The Instance is a container around the Delegate, so
50
+ * create the Delegate first, then wrap it in the Instance
51
+ * Then call the Instance->Init() to register the attribute and command handlers
52
+ */
53
+ CHIP_ERROR DeviceEnergyManagementInit ()
54
+ {
55
+ if (gDEMDelegate || gDEMInstance )
56
+ {
57
+ ChipLogError (AppServer, " DEM Instance or Delegate already exist." );
58
+ return CHIP_ERROR_INCORRECT_STATE;
59
+ }
60
+
61
+ gDEMDelegate = std::make_unique<DeviceEnergyManagementDelegate>();
62
+ if (!gDEMDelegate )
63
+ {
64
+ ChipLogError (AppServer, " Failed to allocate memory for DeviceEnergyManagementDelegate" );
65
+ return CHIP_ERROR_NO_MEMORY;
66
+ }
67
+
68
+ BitMask<DeviceEnergyManagement::Feature> featureMap = GetFeatureMapFromCmdLine ();
69
+
70
+ /* Manufacturer may optionally not support all features, commands & attributes */
71
+ gDEMInstance = std::make_unique<DeviceEnergyManagementManager>(EndpointId (DEM_ENDPOINT), *gDEMDelegate , featureMap);
72
+
73
+ if (!gDEMInstance )
74
+ {
75
+ ChipLogError (AppServer, " Failed to allocate memory for DeviceEnergyManagementManager" );
76
+ gDEMDelegate .reset ();
77
+ return CHIP_ERROR_NO_MEMORY;
78
+ }
79
+
80
+ gDEMDelegate ->SetDeviceEnergyManagementInstance (*gDEMInstance );
81
+
82
+ CHIP_ERROR err = gDEMInstance ->Init (); /* Register Attribute & Command handlers */
83
+ if (err != CHIP_NO_ERROR)
84
+ {
85
+ ChipLogError (AppServer, " Init failed on gDEMInstance" );
86
+ gDEMInstance .reset ();
87
+ gDEMDelegate .reset ();
88
+ return err;
89
+ }
90
+
91
+ return CHIP_NO_ERROR;
92
+ }
93
+
94
+ CHIP_ERROR DeviceEnergyManagementShutdown ()
95
+ {
96
+ /* Do this in the order Instance first, then delegate
97
+ * Ensure we call the Instance->Shutdown to free attribute & command handlers first
98
+ */
99
+ if (gDEMInstance )
100
+ {
101
+ /* deregister attribute & command handlers */
102
+ gDEMInstance ->Shutdown ();
103
+ gDEMInstance .reset ();
104
+ }
105
+ if (gDEMDelegate )
106
+ {
107
+ gDEMDelegate .reset ();
108
+ }
109
+ return CHIP_NO_ERROR;
110
+ }
0 commit comments