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