Fix to follow coding convention
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_poll_control.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_poll_control.h>
22
23 static void *_service_interface_ref_zigbee_zcl_poll_control(
24         ZigBeeServiceInterface *service_interface)
25 {
26         ZigbeeObjectSkeleton *zigbee_object = NULL;
27         ZigbeeCustomData_t *custom_data = NULL;
28         ZigbeeZcl_poll_control *poll_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         poll_object = zigbee_object_get_zcl_poll_control(ZIGBEE_OBJECT(zigbee_object));
44         return poll_object;
45 }
46
47 static void on_zcl_poll_control_check_in_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_poll_control *poll_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         poll_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
68         zblib_check_null_free_and_ret("poll_object", poll_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_poll_control_complete_check_in_response(poll_object, invocation, payload->result);
74
75         g_free(cb_data);
76 }
77
78 static gboolean on_zcl_poll_control_check_in_response(ZigbeeZcl_poll_control *zcl_poll_control_object,
79         GDBusMethodInvocation *invocation,
80         gshort node_id,
81         gchar dest_ep,
82         gchar start_fast_polling,
83         gshort fast_poll_timeout,
84         gpointer user_data)
85 {
86         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
87         ZigbeeZclPollControlCheckInResponse_t req;
88         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
89
90         gboolean ret;
91
92         memset(&req, 0x0, sizeof(ZigbeeZclPollControlCheckInResponse_t));
93
94         /* Update request structure */
95         req.node_id = node_id;
96         req.dest_ep = dest_ep;
97         req.start_fast_polling = start_fast_polling;
98         req.fast_poll_timeout = fast_poll_timeout;
99
100         /* Allocate response callback data */
101         resp_cb_data =
102                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_poll_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_POLL_CONTROL,
116                 ZBLIB_ZCL_POLL_CONTROL_OPS_CHECK_IN_RESPONSE,
117                 &req, sizeof(req),
118                 on_zcl_poll_control_check_in_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 static void on_zcl_poll_control_fast_poll_stop_resp(ZigBeeServiceInterface *service_interface,
135         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
136 {
137         ZigbeeServiceInterfaceRespCbData_t *cb_data =
138                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
139
140         ZigbeeZcl_poll_control *poll_object = NULL;
141         GDBusMethodInvocation *invocation = NULL;
142
143         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
144
145         NOT_USED(service_interface);
146         NOT_USED(request_id);
147
148         if (NULL == resp_data || 0 == resp_data_len) {
149                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
150                 g_free(cb_data);
151                 return;
152         }
153
154         poll_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
155         zblib_check_null_free_and_ret("poll_object", poll_object, cb_data);
156
157         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
158         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
159
160         zigbee_zcl_poll_control_complete_fast_poll_stop(poll_object, invocation, payload->result);
161
162         g_free(cb_data);
163 }
164
165 static gboolean on_zcl_poll_control_fast_poll_stop(ZigbeeZcl_poll_control *zcl_poll_control_object,
166         GDBusMethodInvocation *invocation,
167         gshort node_id,
168         gchar dest_ep,
169         gpointer user_data)
170 {
171         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
172         ZigbeeZclPollControlFastPollStop_t req;
173         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
174
175         gboolean ret;
176
177         memset(&req, 0x0, sizeof(ZigbeeZclPollControlFastPollStop_t));
178
179         /* Update request structure */
180         req.node_id = node_id;
181         req.dest_ep = dest_ep;
182
183         /* Allocate response callback data */
184         resp_cb_data =
185                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_poll_control_object,
186                         invocation, NULL, 0);
187         if (NULL == resp_cb_data) {
188                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
189
190                 /* Send failure response */
191                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
192
193                 return TRUE;
194         }
195
196         /* Dispatch request */
197         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
198                 ZBLIB_DRIVER_TYPE_ZCL_POLL_CONTROL,
199                 ZBLIB_ZCL_POLL_CONTROL_OPS_FAST_POLL_STOP,
200                 &req, sizeof(req),
201                 on_zcl_poll_control_fast_poll_stop_resp, resp_cb_data);
202         if (FALSE == ret) {
203                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
204
205                 /* Free response callback data */
206                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
207
208                 /* Send failure response */
209                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
210
211                 return TRUE;
212         }
213
214         return TRUE;
215 }
216
217 static void on_zcl_poll_control_set_long_poll_interval_resp(ZigBeeServiceInterface *service_interface,
218         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
219 {
220         ZigbeeServiceInterfaceRespCbData_t *cb_data =
221                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
222
223         ZigbeeZcl_poll_control *poll_object = NULL;
224         GDBusMethodInvocation *invocation = NULL;
225
226         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
227
228         NOT_USED(service_interface);
229         NOT_USED(request_id);
230
231         if (NULL == resp_data || 0 == resp_data_len) {
232                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
233                 g_free(cb_data);
234                 return;
235         }
236
237         poll_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
238         zblib_check_null_free_and_ret("poll_object", poll_object, cb_data);
239
240         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
241         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
242
243         zigbee_zcl_poll_control_complete_set_long_poll_interval(poll_object, invocation, payload->result);
244
245         g_free(cb_data);
246 }
247
248 static gboolean on_zcl_poll_control_set_long_poll_interval(ZigbeeZcl_poll_control *zcl_poll_control_object,
249         GDBusMethodInvocation *invocation,
250         gshort node_id,
251         gchar dest_ep,
252         guint new_long_poll_interval,
253         gpointer user_data)
254 {
255         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
256         ZigbeeZclPollControlSetLongPollInterval_t req;
257         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
258
259         gboolean ret;
260
261         memset(&req, 0x0, sizeof(ZigbeeZclPollControlSetLongPollInterval_t));
262
263         /* Update request structure */
264         req.node_id = node_id;
265         req.dest_ep = dest_ep;
266         req.new_long_poll_interval = new_long_poll_interval;
267
268         /* Allocate response callback data */
269         resp_cb_data =
270                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_poll_control_object,
271                         invocation, NULL, 0);
272         if (NULL == resp_cb_data) {
273                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
274
275                 /* Send failure response */
276                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
277
278                 return TRUE;
279         }
280
281         /* Dispatch request */
282         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
283                 ZBLIB_DRIVER_TYPE_ZCL_POLL_CONTROL,
284                 ZBLIB_ZCL_POLL_CONTROL_OPS_SET_LONG_POLL_INTERVAL,
285                 &req, sizeof(req),
286                 on_zcl_poll_control_set_long_poll_interval_resp, resp_cb_data);
287         if (FALSE == ret) {
288                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
289
290                 /* Free response callback data */
291                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
292
293                 /* Send failure response */
294                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
295
296                 return TRUE;
297         }
298
299         return TRUE;
300 }
301
302 static void on_zcl_poll_control_set_short_poll_interval_resp(ZigBeeServiceInterface *service_interface,
303         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
304 {
305         ZigbeeServiceInterfaceRespCbData_t *cb_data =
306                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
307
308         ZigbeeZcl_poll_control *poll_object = NULL;
309         GDBusMethodInvocation *invocation = NULL;
310
311         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
312
313         NOT_USED(service_interface);
314         NOT_USED(request_id);
315
316         if (NULL == resp_data || 0 == resp_data_len) {
317                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
318                 g_free(cb_data);
319                 return;
320         }
321
322         poll_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
323         zblib_check_null_free_and_ret("poll_object", poll_object, cb_data);
324
325         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
326         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
327
328         zigbee_zcl_poll_control_complete_set_short_poll_interval(poll_object, invocation, payload->result);
329
330         g_free(cb_data);
331 }
332
333 static gboolean on_zcl_poll_control_set_short_poll_interval(ZigbeeZcl_poll_control *zcl_poll_control_object,
334         GDBusMethodInvocation *invocation,
335         gshort node_id,
336         gchar dest_ep,
337         guint new_short_poll_interval,
338         gpointer user_data)
339 {
340         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
341         ZigbeeZclPollControlSetShortPollInterval_t req;
342         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
343
344         gboolean ret;
345
346         memset(&req, 0x0, sizeof(ZigbeeZclPollControlSetShortPollInterval_t));
347
348         /* Update request structure */
349         req.node_id = node_id;
350         req.dest_ep = dest_ep;
351         req.new_short_poll_interval = new_short_poll_interval;
352
353         /* Allocate response callback data */
354         resp_cb_data =
355                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_poll_control_object,
356                         invocation, NULL, 0);
357         if (NULL == resp_cb_data) {
358                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
359
360                 /* Send failure response */
361                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
362
363                 return TRUE;
364         }
365
366         /* Dispatch request */
367         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
368                 ZBLIB_DRIVER_TYPE_ZCL_POLL_CONTROL,
369                 ZBLIB_ZCL_POLL_CONTROL_OPS_SET_SHORT_POLL_INTERVAL,
370                 &req, sizeof(req),
371                 on_zcl_poll_control_set_short_poll_interval_resp, resp_cb_data);
372         if (FALSE == ret) {
373                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
374
375                 /* Free response callback data */
376                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
377
378                 /* Send failure response */
379                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
380
381                 return TRUE;
382         }
383
384         return TRUE;
385 }
386
387 void zigbee_service_dbus_interface_zcl_poll_control_notification(ZigBeeServiceInterface *service_interface,
388         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
389 {
390         ZigbeeZcl_poll_control *poll_object;
391
392         zblib_check_null_ret("service_interface", service_interface);
393
394         if (NULL == noti_data || 0 == noti_data_len) {
395                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
396                 return;
397         }
398
399         poll_object = _service_interface_ref_zigbee_zcl_poll_control(service_interface);
400         zblib_check_null_ret("poll_object", poll_object);
401
402         NOT_USED(noti_cb_data);
403
404         switch (noti_id) {
405         case ZBLIB_ZCL_POLL_CONTROL_NOTI_CHECKIN_RESPONSE: {
406                 ZigbeeZclPollControlCheckinResponseNoti_t *rsp =
407                         (ZigbeeZclPollControlCheckinResponseNoti_t*)noti_data;
408
409                 Z_LOGD("checkin_response from : [0x%X]", rsp->node_id);
410
411                 zigbee_zcl_poll_control_emit_checkin_response(poll_object, rsp->node_id, rsp->src_ep);
412         }
413         break;
414         default:
415                 Z_LOGE("Unexpected notification [%x]", noti_id);
416         break;
417         }
418
419         /* ZigbeeZcl_poll_control should be dereferenced */
420         g_object_unref(poll_object);
421 }
422
423 gboolean zigbee_service_dbus_interface_zcl_poll_control_init(ZigBeeServiceInterface *service_interface,
424         ZigbeeObjectSkeleton *zigbee_object)
425 {
426         ZigbeeZcl_poll_control *zcl_poll_control_object;
427
428         if (NULL == service_interface) {
429                 Z_LOGE("service_interface is NULL");
430                 return FALSE;
431         }
432
433         zcl_poll_control_object = zigbee_zcl_poll_control_skeleton_new();
434         zigbee_object_skeleton_set_zcl_poll_control(zigbee_object, zcl_poll_control_object);
435         g_object_unref(zcl_poll_control_object);
436
437         Z_LOGI("zcl_poll_control_object: [%p]", zcl_poll_control_object);
438
439         /*
440          * Register signal handlers for 'zcl_poll_control' interface
441          */
442         g_signal_connect(zcl_poll_control_object,
443                 "handle-check-in-response",
444                 G_CALLBACK(on_zcl_poll_control_check_in_response), service_interface);
445
446         g_signal_connect(zcl_poll_control_object,
447                 "handle-fast-poll-stop",
448                 G_CALLBACK(on_zcl_poll_control_fast_poll_stop), service_interface);
449
450         g_signal_connect(zcl_poll_control_object,
451                 "handle-set-long-poll-interval",
452                 G_CALLBACK(on_zcl_poll_control_set_long_poll_interval), service_interface);
453
454         g_signal_connect(zcl_poll_control_object,
455                 "handle-set-short-poll-interval",
456                 G_CALLBACK(on_zcl_poll_control_set_short_poll_interval), service_interface);
457
458         return TRUE;
459 }