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>
23 static void *_service_interface_ref_zigbee_zdo_bind(
24 ZigBeeServiceInterface *service_interface)
26 ZigbeeObjectSkeleton *zigbee_object = NULL;
27 ZigbeeCustomData_t *custom_data = NULL;
28 ZigbeeZdo_bind *bind_object = NULL;
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!");
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);
43 bind_object = zigbee_object_get_zdo_bind(ZIGBEE_OBJECT(zigbee_object));
47 static void on_zdo_bind_bind_req_resp(ZigBeeServiceInterface *service_interface,
48 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
50 ZigbeeServiceInterfaceRespCbData_t *cb_data =
51 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
53 ZigbeeZdo_bind *bind_object = NULL;
54 GDBusMethodInvocation *invocation = NULL;
56 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
58 NOT_USED(service_interface);
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);
67 bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
68 zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
70 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
71 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
73 zigbee_zdo_bind_complete_bind_req(bind_object, invocation, payload->result);
78 static gboolean on_zdo_bind_bind_req(ZigbeeZdo_bind *zdo_bind_object,
79 GDBusMethodInvocation *invocation,
90 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
91 ZigbeeZdoBindBindReq_t req;
92 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
94 GVariantIter *iter = NULL;
99 memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
101 /* Update request structure */
102 req.node_id = node_id;
103 g_variant_get(src_addr, "a(y)", &iter);
104 while (g_variant_iter_loop(iter, "(y)", req.src_addr[i])) {
106 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
110 req.cluster_id = cluster_id;
111 g_variant_get(dst_addr, "a(y)", &iter);
112 while (g_variant_iter_loop(iter, "(y)", req.dst_addr[i])) {
114 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
117 req.bind_type = bind_type;
118 req.group_addr = group_addr;
121 /* Allocate response callback data */
123 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
124 invocation, NULL, 0);
125 if (NULL == resp_cb_data) {
126 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
128 /* Send failure response */
129 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
134 /* Dispatch request */
135 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
136 ZBLIB_DRIVER_TYPE_ZDO_BIND,
137 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
139 on_zdo_bind_bind_req_resp, resp_cb_data);
141 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
143 /* Free response callback data */
144 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
146 /* Send failure response */
147 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
155 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
156 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
158 ZigbeeServiceInterfaceRespCbData_t *cb_data =
159 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
161 ZigbeeZdo_bind *bind_object = NULL;
162 GDBusMethodInvocation *invocation = NULL;
164 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
166 NOT_USED(service_interface);
167 NOT_USED(request_id);
169 if (NULL == resp_data || 0 == resp_data_len) {
170 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
175 bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
176 zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
178 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
179 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
181 zigbee_zdo_bind_complete_unbind_req(bind_object, invocation, payload->result);
186 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
187 GDBusMethodInvocation *invocation,
198 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
199 ZigbeeZdoBindUnbindReq_t req;
200 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
202 GVariantIter *iter = NULL;
207 memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
209 /* Update request structure */
210 req.node_id = node_id;
211 g_variant_get(src_addr, "a(y)", &iter);
212 while (g_variant_iter_loop(iter, "(y)", req.src_addr[i])) {
214 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
218 req.cluster_id = cluster_id;
219 g_variant_get(dst_addr, "a(y)", &iter);
220 while (g_variant_iter_loop(iter, "(y)", req.dst_addr[i])) {
222 if (i >= ZIGBEE_ZDO_BIND_ADDRESS_MAX_LEN + 1)
225 req.bind_type = bind_type;
226 req.group_addr = group_addr;
229 /* Allocate response callback data */
231 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
232 invocation, NULL, 0);
233 if (NULL == resp_cb_data) {
234 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
236 /* Send failure response */
237 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
242 /* Dispatch request */
243 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
244 ZBLIB_DRIVER_TYPE_ZDO_BIND,
245 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
247 on_zdo_bind_unbind_req_resp, resp_cb_data);
249 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
251 /* Free response callback data */
252 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
254 /* Send failure response */
255 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
263 void zigbee_service_dbus_interface_zdo_bind_notification(ZigBeeServiceInterface *service_interface,
264 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
266 ZigbeeZdo_bind *bind_object;
268 zblib_check_null_ret("service_interface", service_interface);
270 if (NULL == noti_data || 0 == noti_data_len) {
271 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
275 bind_object = _service_interface_ref_zigbee_zdo_bind(service_interface);
276 zblib_check_null_ret("bind_object", bind_object);
278 NOT_USED(noti_cb_data);
281 case ZBLIB_ZDO_BIND_NOTI_BIND_REQ: {
282 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
284 Z_LOGD("'bind_rsp' status : [0x%X]", rsp->status);
286 zigbee_zdo_bind_emit_bind_rsp(bind_object, rsp->status);
289 case ZBLIB_ZDO_BIND_NOTI_UNBIND_REQ: {
290 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
292 Z_LOGD("'unbind_rsp' status : [0x%X]", rsp->status);
294 zigbee_zdo_bind_emit_unbind_rsp(bind_object, rsp->status);
298 Z_LOGE("Unexpected notification [%x]", noti_id);
302 /* ZigbeeZdo_bind should be dereferenced */
303 g_object_unref(bind_object);
306 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
307 ZigbeeObjectSkeleton *zigbee_object)
309 ZigbeeZdo_bind *zdo_bind_object;
311 if (NULL == service_interface) {
312 Z_LOGE("service_interface is NULL");
316 zdo_bind_object = zigbee_zdo_bind_skeleton_new();
317 zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
318 g_object_unref(zdo_bind_object);
320 Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
323 * Register signal handlers for 'zdo_bind' interface
325 g_signal_connect(zdo_bind_object,
327 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
329 g_signal_connect(zdo_bind_object,
331 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);