Fix to follow coding convention
[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 static void *_service_interface_ref_zigbee_zcl_identify(ZigBeeServiceInterface *service_interface)
24 {
25         ZigbeeObjectSkeleton *zigbee_object = NULL;
26         ZigbeeCustomData_t *custom_data = NULL;
27         ZigbeeZcl_identify *id_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         id_object = zigbee_object_get_zcl_identify(ZIGBEE_OBJECT(zigbee_object));
43         return id_object;
44 }
45
46
47 static void on_zclidentify_control_identify_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_identify *id_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         id_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
68         zblib_check_null_free_and_ret("id_object", id_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_identify_complete_identify(id_object, invocation, payload->result);
74
75         g_free(cb_data);
76 }
77
78 static gboolean on_zclidentify_control_identify(ZigbeeZcl_identify *zclidentify_control_object,
79         GDBusMethodInvocation *invocation,
80         gshort node_id,
81         gchar dest_ep,
82         gshort identifytime,
83         gpointer user_data)
84 {
85         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
86         ZigbeeZclIdentifyIdentify_t req;
87         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
88
89         gboolean ret;
90
91         memset(&req, 0x0, sizeof(ZigbeeZclIdentifyIdentify_t));
92
93         /* Update request structure */
94         req.node_id = node_id;
95         req.dest_ep = dest_ep;
96         req.identify_time = identifytime;
97
98         /* Allocate response callback data */
99         resp_cb_data =
100                 zigbee_service_dbus_interface_create_resp_cb_data(zclidentify_control_object,
101                         invocation, NULL, 0);
102         if (NULL == resp_cb_data) {
103                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
104
105                 /* Send failure response */
106                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
107
108                 return TRUE;
109         }
110
111         /* Dispatch request */
112         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
113                 ZBLIB_DRIVER_TYPE_ZCL_IDENTIFY,
114                 ZBLIB_ZCL_IDENTIFY_OPS_IDENTIFY,
115                 &req, sizeof(req),
116                 on_zclidentify_control_identify_resp, resp_cb_data);
117         if (FALSE == ret) {
118                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
119
120                 /* Free response callback data */
121                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
122
123                 /* Send failure response */
124                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
125
126                 return TRUE;
127         }
128
129         return TRUE;
130 }
131
132 static void on_zclidentify_control_query_resp(ZigBeeServiceInterface *service_interface,
133         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
134 {
135         ZigbeeServiceInterfaceRespCbData_t *cb_data =
136                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
137
138         ZigbeeZcl_identify *id_object = NULL;
139         GDBusMethodInvocation *invocation = NULL;
140
141         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
142
143         NOT_USED(service_interface);
144         NOT_USED(request_id);
145
146         if (NULL == resp_data || 0 == resp_data_len) {
147                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
148                 g_free(cb_data);
149                 return;
150         }
151
152         id_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
153         zblib_check_null_free_and_ret("id_object", id_object, cb_data);
154
155         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
156         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
157
158         zigbee_zcl_identify_complete_query(id_object, invocation, payload->result);
159
160         g_free(cb_data);
161 }
162
163 static gboolean on_zclidentify_control_query(ZigbeeZcl_identify *zclidentify_control_object,
164         GDBusMethodInvocation *invocation,
165         gshort node_id,
166         gchar dest_ep,
167         gpointer user_data)
168 {
169         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
170         ZigbeeZclIdentifyQuery_t req;
171         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
172
173         gboolean ret;
174
175         memset(&req, 0x0, sizeof(ZigbeeZclIdentifyQuery_t));
176
177         /* Update request structure */
178         req.node_id = node_id;
179         req.dest_ep = dest_ep;
180
181         /* Allocate response callback data */
182         resp_cb_data =
183                 zigbee_service_dbus_interface_create_resp_cb_data(zclidentify_control_object,
184                         invocation, NULL, 0);
185         if (NULL == resp_cb_data) {
186                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
187
188                 /* Send failure response */
189                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
190
191                 return TRUE;
192         }
193
194         /* Dispatch request */
195         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
196                 ZBLIB_DRIVER_TYPE_ZCL_IDENTIFY,
197                 ZBLIB_ZCL_IDENTIFY_OPS_QUERY,
198                 &req, sizeof(req),
199                 on_zclidentify_control_query_resp, resp_cb_data);
200         if (FALSE == ret) {
201                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
202
203                 /* Free response callback data */
204                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
205
206                 /* Send failure response */
207                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
208
209                 return TRUE;
210         }
211
212         return TRUE;
213 }
214
215 void zigbee_service_dbus_interface_zcl_identify_notification(ZigBeeServiceInterface *service_interface,
216         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
217 {
218         ZigbeeZcl_identify *id_object;
219
220         zblib_check_null_ret("service_interface", service_interface);
221
222         if (NULL == noti_data || 0 == noti_data_len) {
223                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
224                 return;
225         }
226
227         id_object = _service_interface_ref_zigbee_zcl_identify(service_interface);
228         zblib_check_null_ret("id_object", id_object);
229
230         NOT_USED(noti_cb_data);
231
232         switch (noti_id) {
233         case ZBLIB_ZCL_IDENTIFY_NOTI_QUERY_RSP: {
234                 ZigbeeZclIdentifyQueryNoti_t *rsp =     (ZigbeeZclIdentifyQueryNoti_t*)noti_data;
235
236                 Z_LOGD("query_rsp from : [0x%X]", rsp->node_id);
237
238                 zigbee_zcl_identify_emit_query_rsp(id_object, rsp->node_id, rsp->identify_time);
239         }
240         break;
241         default:
242                 Z_LOGE("Unexpected notification [%x]", noti_id);
243         break;
244         }
245
246         /* ZigbeeZcl_identify should be dereferenced */
247         g_object_unref(id_object);
248 }
249
250 gboolean zigbee_service_dbus_interface_zcl_identify_init(ZigBeeServiceInterface *service_interface,
251         ZigbeeObjectSkeleton *zigbee_object)
252 {
253         ZigbeeZcl_identify *zclidentify_control_object;
254
255         if (NULL == service_interface) {
256                 Z_LOGE("service_interface is NULL");
257                 return FALSE;
258         }
259
260         zclidentify_control_object = zigbee_zcl_identify_skeleton_new();
261         zigbee_object_skeleton_set_zcl_identify(zigbee_object, zclidentify_control_object);
262         g_object_unref(zclidentify_control_object);
263
264         Z_LOGI("zclidentify_control_object: [%p]", zclidentify_control_object);
265
266         /*
267          * Register signal handlers for 'zclidentify_control' interface
268          */
269         g_signal_connect(zclidentify_control_object,
270                 "handle-identify",
271                 G_CALLBACK(on_zclidentify_control_identify), service_interface);
272
273         g_signal_connect(zclidentify_control_object,
274                 "handle-query",
275                 G_CALLBACK(on_zclidentify_control_query), service_interface);
276
277         return TRUE;
278 }