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