Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / clusters / door-lock-server / door-lock-server-core.cpp
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *
20  *    Copyright (c) 2020 Silicon Labs
21  *
22  *    Licensed under the Apache License, Version 2.0 (the "License");
23  *    you may not use this file except in compliance with the License.
24  *    You may obtain a copy of the License at
25  *
26  *        http://www.apache.org/licenses/LICENSE-2.0
27  *
28  *    Unless required by applicable law or agreed to in writing, software
29  *    distributed under the License is distributed on an "AS IS" BASIS,
30  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  *    See the License for the specific language governing permissions and
32  *    limitations under the License.
33  */
34 /****************************************************************************
35  * @file
36  * @brief Routines for the Door Lock Server plugin.
37  *******************************************************************************
38  ******************************************************************************/
39
40 #include "af.h"
41 #include "door-lock-server.h"
42
43 #include "gen/attribute-id.h"
44 #include "gen/attribute-type.h"
45 #include "gen/cluster-id.h"
46
47 static void setActuatorEnable(void)
48 {
49     // The Door Lock cluster test spec expects this attribute set to be true by
50     // default...
51     bool troo = true;
52     EmberAfStatus status =
53         emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID, ZCL_ACTUATOR_ENABLED_ATTRIBUTE_ID,
54                                     (uint8_t *) &troo, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
55     if (status != EMBER_ZCL_STATUS_SUCCESS)
56     {
57         emberAfDoorLockClusterPrintln("Failed to write ActuatorEnabled attribute: 0x%X", status);
58     }
59 }
60
61 static void setDoorState(void)
62 {
63     uint8_t state        = EMBER_ZCL_DOOR_STATE_ERROR_UNSPECIFIED;
64     EmberAfStatus status = emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID,
65                                                        ZCL_DOOR_STATE_ATTRIBUTE_ID, (uint8_t *) &state, ZCL_ENUM8_ATTRIBUTE_TYPE);
66     if (status != EMBER_ZCL_STATUS_SUCCESS)
67     {
68         emberAfDoorLockClusterPrintln("Failed to write DoorState attribute: 0x%X", status);
69     }
70 }
71
72 static void setLanguage(void)
73 {
74     uint8_t englishString[] = { 0x02, 'e', 'n' };
75     EmberAfStatus status    = emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID,
76                                                        ZCL_LANGUAGE_ATTRIBUTE_ID, englishString, ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
77     if (status != EMBER_ZCL_STATUS_SUCCESS)
78     {
79         emberAfDoorLockClusterPrintln("Failed to write Language attribute: 0x%X", status);
80     }
81 }
82
83 void emberAfPluginDoorLockServerInitCallback(void)
84 {
85     emAfPluginDoorLockServerInitUser();
86     emAfPluginDoorLockServerInitSchedule();
87
88     setActuatorEnable();
89     setDoorState();
90     setLanguage();
91 }
92
93 void emAfPluginDoorLockServerWriteAttributes(const EmAfPluginDoorLockServerAttributeData * data, uint8_t dataLength,
94                                              const char * description)
95 {
96     for (uint8_t i = 0; i < dataLength; i++)
97     {
98         EmberAfStatus status = emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID, data[i].id,
99                                                            (uint8_t *) &data[i].value, ZCL_INT16U_ATTRIBUTE_TYPE);
100         if (status != EMBER_ZCL_STATUS_SUCCESS)
101         {
102             emberAfDoorLockClusterPrintln("Failed to write %s attribute 0x%2X: 0x%X", data[i].id, status, description);
103         }
104     }
105 }
106
107 EmberAfStatus emAfPluginDoorLockServerNoteDoorStateChanged(EmberAfDoorState state)
108 {
109     EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
110
111 #ifdef ZCL_USING_DOOR_LOCK_CLUSTER_DOOR_STATE_ATTRIBUTE
112     status = emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID, ZCL_DOOR_STATE_ATTRIBUTE_ID,
113                                          (uint8_t *) &state, ZCL_ENUM8_ATTRIBUTE_TYPE);
114     if (status != EMBER_ZCL_STATUS_SUCCESS)
115     {
116         return status;
117     }
118 #endif
119
120 #if defined(ZCL_USING_DOOR_LOCK_CLUSTER_DOOR_OPEN_EVENTS_ATTRIBUTE) ||                                                             \
121     defined(ZCL_USING_DOOR_LOCK_CLUSTER_DOOR_CLOSED_EVENTS_ATTRIBUTE)
122     if (state == EMBER_ZCL_DOOR_STATE_OPEN || state == EMBER_ZCL_DOOR_STATE_CLOSED)
123     {
124         AttributeId attributeId =
125             (state == EMBER_ZCL_DOOR_STATE_OPEN ? ZCL_DOOR_OPEN_EVENTS_ATTRIBUTE_ID : ZCL_DOOR_CLOSED_EVENTS_ATTRIBUTE_ID);
126         uint32_t events;
127         status = emberAfReadServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID, attributeId, (uint8_t *) &events,
128                                             sizeof(events));
129         if (status == EMBER_ZCL_STATUS_SUCCESS)
130         {
131             events++;
132             status = emberAfWriteServerAttribute(DOOR_LOCK_SERVER_ENDPOINT, ZCL_DOOR_LOCK_CLUSTER_ID, attributeId,
133                                                  (uint8_t *) &events, ZCL_INT32U_ATTRIBUTE_TYPE);
134         }
135     }
136 #endif
137
138     return status;
139 }