07ec59cd7a4572c2b12a9f2e2bf9d5f1627eb2fc
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_group.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_group.h>
22
23 static void on_zcl_group_add_group_resp(ZigBeeServiceInterface *service_interface,
24         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
25 {
26         ZigbeeServiceInterfaceRespCbData_t *cb_data =
27                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
28
29         NOT_USED(cb_data);
30         NOT_USED(service_interface);
31         NOT_USED(request_id);
32         NOT_USED(resp_data);
33         NOT_USED(resp_data_len);
34 }
35
36 static gboolean on_zcl_group_add_group(ZigbeeZcl_group *zcl_group_object,
37         GDBusMethodInvocation *invocation,
38         gshort node_id,
39         gchar dest_ep,
40         gshort group_id,
41         GVariant *group_name,
42         gpointer user_data)
43 {
44         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
45         ZigbeeZclGroupAddGroup_t req;
46         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
47
48         GVariantIter *iter = NULL;
49         gint i = 0;
50
51         gboolean ret;
52
53         memset(&req, 0x0, sizeof(ZigbeeZclGroupAddGroup_t));
54
55         /* Update request structure */
56         req.node_id = node_id;
57         req.dest_ep = dest_ep;
58         req.group_id = group_id;
59         g_variant_get(group_name, "ay", &iter);
60         while (g_variant_iter_loop(iter, "y", req.group_name[i])) {
61                 i++;
62                 if (i >= ZIGBEE_ZCL_GROUP_NAME_MAX_LEN + 1)
63                         break;
64         }
65
66         /* Allocate response callback data */
67         resp_cb_data =
68                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
69                         invocation, NULL, 0);
70         if (NULL == resp_cb_data) {
71                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
72
73                 /* Send failure response */
74                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
75
76                 return TRUE;
77         }
78
79         /* Dispatch request */
80         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
81                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
82                 ZBLIB_ZCL_GROUP_OPS_ADD_GROUP,
83                 &req, sizeof(req),
84                 on_zcl_group_add_group_resp, resp_cb_data);
85         if (FALSE == ret) {
86                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
87
88                 /* Free response callback data */
89                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
90
91                 /* Send failure response */
92                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
93
94                 return TRUE;
95         }
96
97         return TRUE;
98 }
99
100 static void on_zcl_group_view_group_resp(ZigBeeServiceInterface *service_interface,
101         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
102 {
103         ZigbeeServiceInterfaceRespCbData_t *cb_data =
104                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
105
106         NOT_USED(cb_data);
107         NOT_USED(service_interface);
108         NOT_USED(request_id);
109         NOT_USED(resp_data);
110         NOT_USED(resp_data_len);
111 }
112
113 static gboolean on_zcl_group_view_group(ZigbeeZcl_group *zcl_group_object,
114         GDBusMethodInvocation *invocation,
115         gshort node_id,
116         gchar dest_ep,
117         gshort group_id,
118         gpointer user_data)
119 {
120         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
121         ZigbeeZclGroupViewGroup_t req;
122         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
123
124         gboolean ret;
125
126         memset(&req, 0x0, sizeof(ZigbeeZclGroupViewGroup_t));
127
128         /* Update request structure */
129         req.node_id = node_id;
130         req.dest_ep = dest_ep;
131         req.group_id = group_id;
132
133         /* Allocate response callback data */
134         resp_cb_data =
135                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
136                         invocation, NULL, 0);
137         if (NULL == resp_cb_data) {
138                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
139
140                 /* Send failure response */
141                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
142
143                 return TRUE;
144         }
145
146         /* Dispatch request */
147         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
148                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
149                 ZBLIB_ZCL_GROUP_OPS_VIEW_GROUP,
150                 &req, sizeof(req),
151                 on_zcl_group_view_group_resp, resp_cb_data);
152         if (FALSE == ret) {
153                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
154
155                 /* Free response callback data */
156                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
157
158                 /* Send failure response */
159                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
160
161                 return TRUE;
162         }
163
164         return TRUE;
165 }
166
167 static void on_zcl_group_get_group_membership_resp(ZigBeeServiceInterface *service_interface,
168         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
169 {
170         ZigbeeServiceInterfaceRespCbData_t *cb_data =
171                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
172
173         NOT_USED(cb_data);
174         NOT_USED(service_interface);
175         NOT_USED(request_id);
176         NOT_USED(resp_data);
177         NOT_USED(resp_data_len);
178 }
179
180 static gboolean on_zcl_group_get_group_membership(ZigbeeZcl_group *zcl_group_object,
181         GDBusMethodInvocation *invocation,
182         gshort node_id,
183         gchar dest_ep,
184         gchar group_count,
185         GVariant *group_list,
186         gpointer user_data)
187 {
188         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
189         ZigbeeZclGroupGetGroupMembership_t req;
190         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
191
192         GVariantIter *iter = NULL;
193         gint i = 0;
194
195         gboolean ret;
196
197         memset(&req, 0x0, sizeof(ZigbeeZclGroupGetGroupMembership_t));
198
199         /* Update request structure */
200         req.node_id = node_id;
201         req.dest_ep = dest_ep;
202         req.group_count = group_count;
203         req.group_list = g_malloc0(sizeof(gshort) * group_count);
204         g_variant_get(group_list, "aq", &iter);
205         while (g_variant_iter_loop(iter, "q", req.group_list[i])) {
206                 i++;
207                 if (i >= group_count)
208                         break;
209         }
210
211         /* Allocate response callback data */
212         resp_cb_data =
213                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
214                         invocation, NULL, 0);
215         if (NULL == resp_cb_data) {
216                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
217
218                 /* Send failure response */
219                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
220
221                 return TRUE;
222         }
223
224         /* Dispatch request */
225         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
226                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
227                 ZBLIB_ZCL_GROUP_OPS_GET_GROUP_MEMBERSHIP,
228                 &req, sizeof(req),
229                 on_zcl_group_get_group_membership_resp, resp_cb_data);
230
231         /* Free resource */
232         g_free(req.group_list);
233
234         if (FALSE == ret) {
235                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
236
237                 /* Free response callback data */
238                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
239
240                 /* Send failure response */
241                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
242
243                 return TRUE;
244         }
245
246         return TRUE;
247 }
248
249 static void on_zcl_group_remove_group_resp(ZigBeeServiceInterface *service_interface,
250         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
251 {
252         ZigbeeServiceInterfaceRespCbData_t *cb_data =
253                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
254
255         NOT_USED(cb_data);
256         NOT_USED(service_interface);
257         NOT_USED(request_id);
258         NOT_USED(resp_data);
259         NOT_USED(resp_data_len);
260 }
261
262 static gboolean on_zcl_group_remove_group(ZigbeeZcl_group *zcl_group_object,
263         GDBusMethodInvocation *invocation,
264         gshort node_id,
265         gchar dest_ep,
266         gshort group_id,
267         gpointer user_data)
268 {
269         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
270         ZigbeeZclGroupRemoveGroup_t req;
271         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
272
273         gboolean ret;
274
275         memset(&req, 0x0, sizeof(ZigbeeZclGroupRemoveGroup_t));
276
277         /* Update request structure */
278         req.node_id = node_id;
279         req.dest_ep = dest_ep;
280         req.group_id = group_id;
281
282         /* Allocate response callback data */
283         resp_cb_data =
284                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
285                         invocation, NULL, 0);
286         if (NULL == resp_cb_data) {
287                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
288
289                 /* Send failure response */
290                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
291
292                 return TRUE;
293         }
294
295         /* Dispatch request */
296         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
297                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
298                 ZBLIB_ZCL_GROUP_OPS_REMOVE_GROUP,
299                 &req, sizeof(req),
300                 on_zcl_group_remove_group_resp, resp_cb_data);
301         if (FALSE == ret) {
302                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
303
304                 /* Free response callback data */
305                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
306
307                 /* Send failure response */
308                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
309
310                 return TRUE;
311         }
312
313         return TRUE;
314 }
315
316 static void on_zcl_group_remove_all_group_resp(ZigBeeServiceInterface *service_interface,
317         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
318 {
319         ZigbeeServiceInterfaceRespCbData_t *cb_data =
320                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
321
322         NOT_USED(cb_data);
323         NOT_USED(service_interface);
324         NOT_USED(request_id);
325         NOT_USED(resp_data);
326         NOT_USED(resp_data_len);
327 }
328
329 static gboolean on_zcl_group_remove_all_group(ZigbeeZcl_group *zcl_group_object,
330         GDBusMethodInvocation *invocation,
331         gshort node_id,
332         gchar dest_ep,
333         gpointer user_data)
334 {
335         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
336         ZigbeeZclGroupRemoveAllGroup_t req;
337         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
338
339         gboolean ret;
340
341         memset(&req, 0x0, sizeof(ZigbeeZclGroupRemoveAllGroup_t));
342
343         /* Update request structure */
344         req.node_id = node_id;
345         req.dest_ep = dest_ep;
346
347         /* Allocate response callback data */
348         resp_cb_data =
349                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
350                         invocation, NULL, 0);
351         if (NULL == resp_cb_data) {
352                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
353
354                 /* Send failure response */
355                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
356
357                 return TRUE;
358         }
359
360         /* Dispatch request */
361         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
362                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
363                 ZBLIB_ZCL_GROUP_OPS_REMOVE_ALL_GROUP,
364                 &req, sizeof(req),
365                 on_zcl_group_remove_all_group_resp, resp_cb_data);
366         if (FALSE == ret) {
367                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
368
369                 /* Free response callback data */
370                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
371
372                 /* Send failure response */
373                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
374
375                 return TRUE;
376         }
377
378         return TRUE;
379 }
380
381 static void on_zcl_group_add_group_if_identifying_resp(ZigBeeServiceInterface *service_interface,
382         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
383 {
384         ZigbeeServiceInterfaceRespCbData_t *cb_data =
385                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
386
387         NOT_USED(cb_data);
388         NOT_USED(service_interface);
389         NOT_USED(request_id);
390         NOT_USED(resp_data);
391         NOT_USED(resp_data_len);
392 }
393
394 static gboolean on_zcl_group_add_group_if_identifying(ZigbeeZcl_group *zcl_group_object,
395         GDBusMethodInvocation *invocation,
396         gshort node_id,
397         gchar dest_ep,
398         gshort group_id,
399         GVariant *group_name,
400         gpointer user_data)
401 {
402         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
403         ZigbeeZclGroupAddGroupIfIdentifying_t req;
404         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
405
406         GVariantIter *iter = NULL;
407         gint i = 0;
408
409         gboolean ret;
410
411         memset(&req, 0x0, sizeof(ZigbeeZclGroupAddGroupIfIdentifying_t));
412
413         /* Update request structure */
414         req.node_id = node_id;
415         req.dest_ep = dest_ep;
416         req.group_id = group_id;
417         g_variant_get(group_name, "ay", &iter);
418         while (g_variant_iter_loop(iter, "y", req.group_name[i])) {
419                 i++;
420                 if (i >= ZIGBEE_ZCL_GROUP_NAME_MAX_LEN + 1)
421                         break;
422         }
423
424         /* Allocate response callback data */
425         resp_cb_data =
426                 zigbee_service_dbus_interface_create_resp_cb_data(zcl_group_object,
427                         invocation, NULL, 0);
428         if (NULL == resp_cb_data) {
429                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
430
431                 /* Send failure response */
432                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
433
434                 return TRUE;
435         }
436
437         /* Dispatch request */
438         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
439                 ZBLIB_DRIVER_TYPE_ZCL_GROUP,
440                 ZBLIB_ZCL_GROUP_OPS_ADD_GROUP_IF_IDENTIFYING,
441                 &req, sizeof(req),
442                 on_zcl_group_add_group_if_identifying_resp, resp_cb_data);
443         if (FALSE == ret) {
444                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
445
446                 /* Free response callback data */
447                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
448
449                 /* Send failure response */
450                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
451
452                 return TRUE;
453         }
454
455         return TRUE;
456 }
457
458 void zigbee_service_dbus_interface_zcl_group_notification(ZigBeeServiceInterface *service_interface,
459         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
460 {
461         if (NULL == service_interface) {
462                 Z_LOGE("service_interface is NULL");
463                 return;
464         }
465
466         NOT_USED(noti_id);
467         NOT_USED(noti_data);
468         NOT_USED(noti_data_len);
469         NOT_USED(noti_cb_data);
470 }
471
472 gboolean zigbee_service_dbus_interface_zcl_group_init(ZigBeeServiceInterface *service_interface,
473         ZigbeeObjectSkeleton *zigbee_object)
474 {
475         ZigbeeZcl_group *zcl_group_object;
476
477         if (NULL == service_interface) {
478                 Z_LOGE("service_interface is NULL");
479                 return FALSE;
480         }
481
482         zcl_group_object = zigbee_zcl_group_skeleton_new();
483         zigbee_object_skeleton_set_zcl_group(zigbee_object, zcl_group_object);
484         g_object_unref(zcl_group_object);
485
486         Z_LOGI("zcl_group_object: [%p]", zcl_group_object);
487
488         /*
489          * Register signal handlers for 'zcl_group' interface
490          */
491         g_signal_connect(zcl_group_object,
492                 "handle-add-group",
493                 G_CALLBACK(on_zcl_group_add_group), service_interface);
494
495         g_signal_connect(zcl_group_object,
496                 "handle-view-group",
497                 G_CALLBACK(on_zcl_group_view_group), service_interface);
498
499         g_signal_connect(zcl_group_object,
500                 "handle-get-group-membership",
501                 G_CALLBACK(on_zcl_group_get_group_membership), service_interface);
502
503         g_signal_connect(zcl_group_object,
504                 "handle-remove-group",
505                 G_CALLBACK(on_zcl_group_remove_group), service_interface);
506
507         g_signal_connect(zcl_group_object,
508                 "handle-remove-all-group",
509                 G_CALLBACK(on_zcl_group_remove_all_group), service_interface);
510
511         g_signal_connect(zcl_group_object,
512                 "handle-add-group-if-identifying",
513                 G_CALLBACK(on_zcl_group_add_group_if_identifying), service_interface);
514
515         return TRUE;
516 }