334802bef1283426239706b124afc5f55127b78b
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_identify.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_identify.h>
22
23 /* LCOV_EXCL_START */
24 static void *_service_interface_ref_zigbee_zcl_identify(ZigBeeServiceInterface *service_interface)
25 {
26         ZigbeeObjectSkeleton *zigbee_object = NULL;
27         ZigbeeCustomData_t *custom_data = NULL;
28         ZigbeeZcl_identify *id_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         id_object = zigbee_object_get_zcl_identify(ZIGBEE_OBJECT(zigbee_object));
44         return id_object;
45 }
46
47
48 static void on_zclidentify_control_identify_resp(ZigBeeServiceInterface *service_interface,
49         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
50 {
51         ZigbeeServiceInterfaceRespCbData_t *cb_data =
52                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
53
54         ZigbeeZcl_identify *id_object = NULL;
55         GDBusMethodInvocation *invocation = NULL;
56
57         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
58
59         NOT_USED(service_interface);
60         NOT_USED(request_id);
61
62         if (NULL == resp_data || 0 == resp_data_len) {
63                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
64                 g_free(cb_data);
65                 return;
66         }
67
68         id_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
69         zblib_check_null_free_and_ret("id_object", id_object, cb_data);
70
71         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
72         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
73
74         zigbee_zcl_identify_complete_identify(id_object, invocation, payload->result);
75
76         g_free(cb_data);
77 }
78
79 static gboolean on_zclidentify_control_identify(ZigbeeZcl_identify *zclidentify_control_object,
80         GDBusMethodInvocation *invocation,
81         gshort node_id,
82         gchar dest_ep,
83         gshort identifytime,
84         gpointer user_data)
85 {
86         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
87         ZigbeeZclIdentifyIdentify_t req;
88         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
89
90         gboolean ret;
91
92         memset(&req, 0x0, sizeof(ZigbeeZclIdentifyIdentify_t));
93
94         /* Update request structure */
95         req.node_id = node_id;
96         req.dest_ep = dest_ep;
97         req.identify_time = identifytime;
98
99         /* Allocate response callback data */
100         resp_cb_data =
101                 zigbee_service_dbus_interface_create_resp_cb_data(zclidentify_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_IDENTIFY,
115                 ZBLIB_ZCL_IDENTIFY_OPS_IDENTIFY,
116                 &req, sizeof(req),
117                 on_zclidentify_control_identify_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 static void on_zclidentify_control_query_resp(ZigBeeServiceInterface *service_interface,
134         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
135 {
136         ZigbeeServiceInterfaceRespCbData_t *cb_data =
137                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
138
139         ZigbeeZcl_identify *id_object = NULL;
140         GDBusMethodInvocation *invocation = NULL;
141
142         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
143
144         NOT_USED(service_interface);
145         NOT_USED(request_id);
146
147         if (NULL == resp_data || 0 == resp_data_len) {
148                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
149                 g_free(cb_data);
150                 return;
151         }
152
153         id_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
154         zblib_check_null_free_and_ret("id_object", id_object, cb_data);
155
156         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
157         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
158
159         zigbee_zcl_identify_complete_query(id_object, invocation, payload->result);
160
161         g_free(cb_data);
162 }
163
164 static gboolean on_zclidentify_control_query(ZigbeeZcl_identify *zclidentify_control_object,
165         GDBusMethodInvocation *invocation,
166         gshort node_id,
167         gchar dest_ep,
168         gpointer user_data)
169 {
170         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
171         ZigbeeZclIdentifyQuery_t req;
172         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
173
174         gboolean ret;
175
176         memset(&req, 0x0, sizeof(ZigbeeZclIdentifyQuery_t));
177
178         /* Update request structure */
179         req.node_id = node_id;
180         req.dest_ep = dest_ep;
181
182         /* Allocate response callback data */
183         resp_cb_data =
184                 zigbee_service_dbus_interface_create_resp_cb_data(zclidentify_control_object,
185                         invocation, NULL, 0);
186         if (NULL == resp_cb_data) {
187                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
188
189                 /* Send failure response */
190                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
191
192                 return TRUE;
193         }
194
195         /* Dispatch request */
196         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
197                 ZBLIB_DRIVER_TYPE_ZCL_IDENTIFY,
198                 ZBLIB_ZCL_IDENTIFY_OPS_QUERY,
199                 &req, sizeof(req),
200                 on_zclidentify_control_query_resp, resp_cb_data);
201         if (FALSE == ret) {
202                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
203
204                 /* Free response callback data */
205                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
206
207                 /* Send failure response */
208                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
209
210                 return TRUE;
211         }
212
213         return TRUE;
214 }
215
216 void zigbee_service_dbus_interface_zcl_identify_notification(ZigBeeServiceInterface *service_interface,
217         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
218 {
219         ZigbeeZcl_identify *id_object;
220
221         zblib_check_null_ret("service_interface", service_interface);
222
223         if (NULL == noti_data || 0 == noti_data_len) {
224                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
225                 return;
226         }
227
228         id_object = _service_interface_ref_zigbee_zcl_identify(service_interface);
229         zblib_check_null_ret("id_object", id_object);
230
231         NOT_USED(noti_cb_data);
232
233         switch (noti_id) {
234         case ZBLIB_ZCL_IDENTIFY_NOTI_QUERY_RSP: {
235                 ZigbeeZclIdentifyQueryNoti_t *rsp =     (ZigbeeZclIdentifyQueryNoti_t*)noti_data;
236
237                 Z_LOGD("query_rsp from : [0x%X]", rsp->node_id);
238
239                 zigbee_zcl_identify_emit_query_rsp(id_object, rsp->node_id, rsp->identify_time);
240         }
241         break;
242         default:
243                 Z_LOGE("Unexpected notification [%x]", noti_id);
244         break;
245         }
246
247         /* ZigbeeZcl_identify should be dereferenced */
248         g_object_unref(id_object);
249 }
250 /* LCOV_EXCL_STOP */
251
252 gboolean zigbee_service_dbus_interface_zcl_identify_init(ZigBeeServiceInterface *service_interface,
253         ZigbeeObjectSkeleton *zigbee_object)
254 {
255         ZigbeeZcl_identify *zclidentify_control_object;
256
257         if (NULL == service_interface) {
258                 /* LCOV_EXCL_START */
259                 Z_LOGE("service_interface is NULL");
260                 return FALSE;
261                 /* LCOV_EXCL_STOP */
262         }
263
264         zclidentify_control_object = zigbee_zcl_identify_skeleton_new();
265         zigbee_object_skeleton_set_zcl_identify(zigbee_object, zclidentify_control_object);
266         g_object_unref(zclidentify_control_object);
267
268         Z_LOGI("zclidentify_control_object: [%p]", zclidentify_control_object);
269
270         /*
271          * Register signal handlers for 'zclidentify_control' interface
272          */
273         g_signal_connect(zclidentify_control_object,
274                 "handle-identify",
275                 G_CALLBACK(on_zclidentify_control_identify), service_interface);
276
277         g_signal_connect(zclidentify_control_object,
278                 "handle-query",
279                 G_CALLBACK(on_zclidentify_control_query), service_interface);
280
281         return TRUE;
282 }