Implement ZCL IAS Zone Cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_ias_zone.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
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 "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_zcl_ias_zone.h>
22
23 static void *_service_interface_ref_zigbee_zcl_ias_zone(ZigBeeServiceInterface *service_interface)
24 {
25         ZigbeeObjectSkeleton *zigbee_object = NULL;
26         ZigbeeCustomData_t *custom_data = NULL;
27         ZigbeeZcl_ias_zone *ias_object = NULL;
28
29         custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
30         if (NULL == custom_data) {
31                 Z_LOGE("D-BUS service interface custom_data is NULL!");
32                 return NULL;
33         }
34
35         /* Get zigbee object */
36         zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
37         if (NULL == zigbee_object) {
38                 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
39                 return NULL;
40         }
41
42         ias_object = zigbee_object_get_zcl_ias_zone(ZIGBEE_OBJECT(zigbee_object));
43         return ias_object;
44 }
45
46 static void on_zclias_control_enroll_response_resp(ZigBeeServiceInterface *service_interface,
47         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
48 {
49         ZigbeeServiceInterfaceRespCbData_t *cb_data =
50                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
51
52         ZigbeeZcl_ias_zone *ias_object = NULL;
53         GDBusMethodInvocation *invocation = NULL;
54
55         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
56
57         NOT_USED(service_interface);
58         NOT_USED(request_id);
59
60         if (NULL == resp_data || 0 == resp_data_len) {
61                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
62                 g_free(cb_data);
63                 return;
64         }
65
66         ias_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
67         zblib_check_null_free_and_ret("ias_object", ias_object, cb_data);
68
69         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
70         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
71
72         zigbee_zcl_ias_zone_complete_enroll_response(ias_object, invocation, payload->result);
73
74         g_free(cb_data);
75 }
76
77 static gboolean on_zclias_control_enroll_response(ZigbeeZcl_ias_zone *zclias_control_object,
78         GDBusMethodInvocation *invocation,
79         gshort node_id,
80         gchar dest_ep,
81         gchar enroll_code,
82         gchar zone_id,
83         gpointer user_data)
84 {
85         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
86         ZigbeeZclIasZoneEnrollResponse_t req;
87         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
88
89         gboolean ret;
90
91         memset(&req, 0x0, sizeof(ZigbeeZclIasZoneEnrollResponse_t));
92
93         /* Update request structure */
94         req.node_id = node_id;
95         req.dest_ep = dest_ep;
96         req.enroll_code = enroll_code;
97         req.zone_id = zone_id;
98
99         /* Allocate response callback data */
100         resp_cb_data =
101                 zigbee_service_dbus_interface_create_resp_cb_data(zclias_control_object,
102                         invocation, NULL, 0);
103         if (NULL == resp_cb_data) {
104                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
105
106                 /* Send failure response */
107                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
108
109                 return TRUE;
110         }
111
112         /* Dispatch request */
113         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
114                 ZBLIB_DRIVER_TYPE_ZCL_IAS_ZONE,
115                 ZBLIB_ZCL_IAS_ZONE_OPS_ENROLL_RESPONSE,
116                 &req, sizeof(req),
117                 on_zclias_control_enroll_response_resp, resp_cb_data);
118         if (FALSE == ret) {
119                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
120
121                 /* Free response callback data */
122                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
123
124                 /* Send failure response */
125                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
126
127                 return TRUE;
128         }
129
130         return TRUE;
131 }
132
133 void zigbee_service_dbus_interface_zcl_ias_zone_notification(ZigBeeServiceInterface *service_interface,
134         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
135 {
136         ZigbeeZcl_ias_zone *ias_object;
137
138         zblib_check_null_ret("service_interface", service_interface);
139
140         if (NULL == noti_data || 0 == noti_data_len) {
141                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
142                 return;
143         }
144
145         ias_object = _service_interface_ref_zigbee_zcl_ias_zone(service_interface);
146         zblib_check_null_ret("ias_object", ias_object);
147
148         NOT_USED(noti_cb_data);
149
150         switch(noti_id) {
151         case ZBLIB_ZCL_IAS_ZONE_NOTI_ZONE_STATUS_CHANGE_NOTI: {
152                 ZigbeeZclIasZoneStatusChangeNoti_t *rsp =
153                         (ZigbeeZclIasZoneStatusChangeNoti_t*)noti_data;
154
155                 Z_LOGD("status_change_rsp from : [0x%X]", rsp->node_id);
156
157                 zigbee_zcl_ias_zone_emit_status_change_rsp(ias_object, rsp->node_id, rsp->src_ep,
158                         rsp->zone_status, rsp->extended_status, rsp->zone_id, rsp->delay);
159         }
160         break;
161         case ZBLIB_ZCL_IAS_ZONE_NOTI_ENROLL_REQUEST: {
162                 ZigbeeZclIasEnrollRequestNoti_t *rsp =
163                         (ZigbeeZclIasEnrollRequestNoti_t*)noti_data;
164
165                 Z_LOGD("enroll_request from : [0x%X]", rsp->node_id);
166
167                 zigbee_zcl_ias_zone_emit_enroll_request(ias_object, rsp->node_id, rsp->src_ep,
168                         rsp->zone_type, rsp->mfg_code);
169         }
170         break;
171         default:
172                 Z_LOGE("Unexpected notification [%x]", noti_id);
173         break;
174         }
175
176         /* ZigbeeZcl_ias_zone should be dereferenced */
177         g_object_unref(ias_object);
178 }
179
180 gboolean zigbee_service_dbus_interface_zcl_ias_zone_init(ZigBeeServiceInterface *service_interface,
181         ZigbeeObjectSkeleton *zigbee_object)
182 {
183         ZigbeeZcl_ias_zone *zclias_control_object;
184
185         if (NULL == service_interface) {
186                 Z_LOGE("service_interface is NULL");
187                 return FALSE;
188         }
189
190         zclias_control_object = zigbee_zcl_ias_zone_skeleton_new();
191         zigbee_object_skeleton_set_zcl_ias_zone(zigbee_object, zclias_control_object);
192         g_object_unref(zclias_control_object);
193
194         Z_LOGI("zclias_control_object: [%p]", zclias_control_object);
195
196         /*
197          * Register signal handlers for 'zclias_control' interface
198          */
199         g_signal_connect(zclias_control_object,
200                 "handle-enroll-response",
201                 G_CALLBACK(on_zclias_control_enroll_response), service_interface);
202
203         return TRUE;
204 }