2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Suresh Kumar N (suresh.n@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "zigbee_service_interface_common.h"
21 #include <zblib_driver_zdo_bind.h>
24 static void *_service_interface_ref_zigbee_zdo_bind(
25 ZigBeeServiceInterface *service_interface)
27 ZigbeeObjectSkeleton *zigbee_object = NULL;
28 ZigbeeCustomData_t *custom_data = NULL;
29 ZigbeeZdo_bind *bind_object = NULL;
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!");
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);
44 bind_object = zigbee_object_get_zdo_bind(ZIGBEE_OBJECT(zigbee_object));
48 static void on_zdo_bind_bind_req_resp(ZigBeeServiceInterface *service_interface,
49 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
51 ZigbeeServiceInterfaceRespCbData_t *cb_data =
52 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
54 ZigbeeZdo_bind *bind_object = NULL;
55 GDBusMethodInvocation *invocation = NULL;
57 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
59 NOT_USED(service_interface);
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);
68 bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
69 zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
71 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
72 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
74 zigbee_zdo_bind_complete_bind_req(bind_object, invocation, payload->result);
79 static gboolean on_zdo_bind_bind_req(ZigbeeZdo_bind *zdo_bind_object,
80 GDBusMethodInvocation *invocation,
91 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
92 ZigbeeZdoBindBindReq_t req;
93 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
95 GVariantIter *iter = NULL;
101 memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
103 /* Update request structure */
104 req.node_id = node_id;
105 g_variant_get(src_addr, "a(y)", &iter);
106 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
107 req.src_addr[i] = value;
111 req.cluster_id = cluster_id;
114 g_variant_get(dst_addr, "a(y)", &iter);
115 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
116 req.dst_addr[i] = value;
119 req.bind_type = bind_type;
120 req.group_addr = group_addr;
123 /* Allocate response callback data */
125 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
126 invocation, NULL, 0);
127 if (NULL == resp_cb_data) {
128 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
130 /* Send failure response */
131 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
136 /* Dispatch request */
137 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
138 ZBLIB_DRIVER_TYPE_ZDO_BIND,
139 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
141 on_zdo_bind_bind_req_resp, resp_cb_data);
143 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
145 /* Free response callback data */
146 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
148 /* Send failure response */
149 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
157 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
158 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
160 ZigbeeServiceInterfaceRespCbData_t *cb_data =
161 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
163 ZigbeeZdo_bind *bind_object = NULL;
164 GDBusMethodInvocation *invocation = NULL;
166 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
168 NOT_USED(service_interface);
169 NOT_USED(request_id);
171 if (NULL == resp_data || 0 == resp_data_len) {
172 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
177 bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
178 zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
180 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
181 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
183 zigbee_zdo_bind_complete_unbind_req(bind_object, invocation, payload->result);
188 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
189 GDBusMethodInvocation *invocation,
200 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
201 ZigbeeZdoBindUnbindReq_t req;
202 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
204 GVariantIter *iter = NULL;
210 memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
212 /* Update request structure */
213 req.node_id = node_id;
214 g_variant_get(src_addr, "a(y)", &iter);
215 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
216 req.src_addr[i] = value;
220 req.cluster_id = cluster_id;
223 g_variant_get(dst_addr, "a(y)", &iter);
224 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
225 req.dst_addr[i] = value;
228 req.bind_type = bind_type;
229 req.group_addr = group_addr;
232 /* Allocate response callback data */
234 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
235 invocation, NULL, 0);
236 if (NULL == resp_cb_data) {
237 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
239 /* Send failure response */
240 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
245 /* Dispatch request */
246 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
247 ZBLIB_DRIVER_TYPE_ZDO_BIND,
248 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
250 on_zdo_bind_unbind_req_resp, resp_cb_data);
252 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
254 /* Free response callback data */
255 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
257 /* Send failure response */
258 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
266 void zigbee_service_dbus_interface_zdo_bind_notification(ZigBeeServiceInterface *service_interface,
267 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
269 ZigbeeZdo_bind *bind_object;
271 zblib_check_null_ret("service_interface", service_interface);
273 if (NULL == noti_data || 0 == noti_data_len) {
274 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
278 bind_object = _service_interface_ref_zigbee_zdo_bind(service_interface);
279 zblib_check_null_ret("bind_object", bind_object);
281 NOT_USED(noti_cb_data);
284 case ZBLIB_ZDO_BIND_NOTI_BIND_REQ: {
285 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
287 Z_LOGD("'bind_rsp' status : [0x%X]", rsp->status);
289 zigbee_zdo_bind_emit_bind_rsp(bind_object, rsp->status);
292 case ZBLIB_ZDO_BIND_NOTI_UNBIND_REQ: {
293 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
295 Z_LOGD("'unbind_rsp' status : [0x%X]", rsp->status);
297 zigbee_zdo_bind_emit_unbind_rsp(bind_object, rsp->status);
301 Z_LOGE("Unexpected notification [%x]", noti_id);
305 /* ZigbeeZdo_bind should be dereferenced */
306 g_object_unref(bind_object);
310 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
311 ZigbeeObjectSkeleton *zigbee_object)
313 ZigbeeZdo_bind *zdo_bind_object;
315 if (NULL == service_interface) {
316 /* LCOV_EXCL_START */
317 Z_LOGE("service_interface is NULL");
322 zdo_bind_object = zigbee_zdo_bind_skeleton_new();
323 zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
324 g_object_unref(zdo_bind_object);
326 Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
329 * Register signal handlers for 'zdo_bind' interface
331 g_signal_connect(zdo_bind_object,
333 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
335 g_signal_connect(zdo_bind_object,
337 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);