41eca1f8b8bb9e3a85f947efcd9c17d09e0f563f
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zdo_bind.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_zdo_bind.h>
22
23 static void on_zdo_bind_bind_req_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_zdo_bind_bind_req(ZigbeeZdo_bind *zdo_bind_object,
37         GDBusMethodInvocation *invocation,
38         gshort node_id,
39         GVariant *src_addr,
40         gchar src_ep,
41         gshort cluster_id,
42         GVariant *dst_addr,
43         gchar bind_type,
44         gshort group_addr,
45         gchar dst_ep,
46         gpointer user_data)
47 {
48         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
49         ZigbeeZdoBindBindReq_t req;
50         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
51
52         GVariantIter *iter = NULL;
53         gint i = 0;
54
55         gboolean ret;
56
57         memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
58
59         /* Update request structure */
60         req.node_id = node_id;
61         g_variant_get(src_addr, "ay", &iter);
62         while (g_variant_iter_loop(iter, "y", req.src_addr[i])) {
63                 i++;
64                 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
65                         break;
66         }
67         req.src_ep = src_ep;
68         req.cluster_id = cluster_id;
69         g_variant_get(dst_addr, "ay", &iter);
70         while (g_variant_iter_loop(iter, "y", req.dst_addr[i])) {
71                 i++;
72                 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
73                         break;
74         }
75         req.bind_type = bind_type;
76         req.group_addr = group_addr;
77         req.dst_ep = dst_ep;
78
79         /* Allocate response callback data */
80         resp_cb_data =
81                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
82                         invocation, NULL, 0);
83         if (NULL == resp_cb_data) {
84                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
85
86                 /* Send failure response */
87                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
88
89                 return TRUE;
90         }
91
92         /* Dispatch request */
93         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
94                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
95                 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
96                 &req, sizeof(req),
97                 on_zdo_bind_bind_req_resp, resp_cb_data);
98         if (FALSE == ret) {
99                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
100
101                 /* Free response callback data */
102                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
103
104                 /* Send failure response */
105                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
106
107                 return TRUE;
108         }
109
110         return TRUE;
111 }
112
113 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
114         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
115 {
116         ZigbeeServiceInterfaceRespCbData_t *cb_data =
117                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
118
119         NOT_USED(cb_data);
120         NOT_USED(service_interface);
121         NOT_USED(request_id);
122         NOT_USED(resp_data);
123         NOT_USED(resp_data_len);
124 }
125
126 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
127         GDBusMethodInvocation *invocation,
128         gshort node_id,
129         GVariant *src_addr,
130         gchar src_ep,
131         gshort cluster_id,
132         GVariant *dst_addr,
133         gchar bind_type,
134         gshort group_addr,
135         gchar dst_ep,
136         gpointer user_data)
137 {
138         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
139         ZigbeeZdoBindUnbindReq_t req;
140         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
141
142         GVariantIter *iter = NULL;
143         gint i = 0;
144
145         gboolean ret;
146
147         memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
148
149         /* Update request structure */
150         req.node_id = node_id;
151         g_variant_get(src_addr, "ay", &iter);
152         while (g_variant_iter_loop(iter, "y", req.src_addr[i])) {
153                 i++;
154                 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
155                         break;
156         }
157         req.src_ep = src_ep;
158         req.cluster_id = cluster_id;
159         g_variant_get(dst_addr, "ay", &iter);
160         while (g_variant_iter_loop(iter, "y", req.dst_addr[i])) {
161                 i++;
162                 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
163                         break;
164         }
165         req.bind_type = bind_type;
166         req.group_addr = group_addr;
167         req.dst_ep = dst_ep;
168
169         /* Allocate response callback data */
170         resp_cb_data =
171                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
172                         invocation, NULL, 0);
173         if (NULL == resp_cb_data) {
174                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
175
176                 /* Send failure response */
177                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
178
179                 return TRUE;
180         }
181
182         /* Dispatch request */
183         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
184                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
185                 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
186                 &req, sizeof(req),
187                 on_zdo_bind_unbind_req_resp, resp_cb_data);
188         if (FALSE == ret) {
189                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
190
191                 /* Free response callback data */
192                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
193
194                 /* Send failure response */
195                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
196
197                 return TRUE;
198         }
199
200         return TRUE;
201 }
202
203 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
204         ZigbeeObjectSkeleton *zigbee_object)
205 {
206         ZigbeeZdo_bind *zdo_bind_object;
207
208         if (NULL == service_interface) {
209                 Z_LOGE("service_interface is NULL");
210                 return FALSE;
211         }
212
213         zdo_bind_object = zigbee_zdo_bind_skeleton_new();
214         zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
215         g_object_unref(zdo_bind_object);
216
217         Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
218
219         /*
220          * Register signal handlers for 'zdo_bind' interface
221          */
222         g_signal_connect(zdo_bind_object,
223                 "handle-bind-req",
224                 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
225
226         g_signal_connect(zdo_bind_object,
227                 "handle-unbind-req",
228                 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);
229
230         return TRUE;
231 }