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;
100 memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
102 /* Update request structure */
103 req.node_id = node_id;
104 g_variant_get(src_addr, "a(y)", &iter);
105 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
106 req.src_addr[i] = value;
110 req.cluster_id = cluster_id;
113 g_variant_get(dst_addr, "a(y)", &iter);
114 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
115 req.dst_addr[i] = value;
118 req.bind_type = bind_type;
119 req.group_addr = group_addr;
122 /* Allocate response callback data */
124 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
125 invocation, NULL, 0);
126 if (NULL == resp_cb_data) {
127 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
129 /* Send failure response */
130 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
135 /* Dispatch request */
136 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
137 ZBLIB_DRIVER_TYPE_ZDO_BIND,
138 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
140 on_zdo_bind_bind_req_resp, resp_cb_data);
142 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
144 /* Free response callback data */
145 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
147 /* Send failure response */
148 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
156 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
157 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
159 ZigbeeServiceInterfaceRespCbData_t *cb_data =
160 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
162 ZigbeeZdo_bind *bind_object = NULL;
163 GDBusMethodInvocation *invocation = NULL;
165 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
167 NOT_USED(service_interface);
168 NOT_USED(request_id);
170 if (NULL == resp_data || 0 == resp_data_len) {
171 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
176 bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
177 zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
179 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
180 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
182 zigbee_zdo_bind_complete_unbind_req(bind_object, invocation, payload->result);
187 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
188 GDBusMethodInvocation *invocation,
199 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
200 ZigbeeZdoBindUnbindReq_t req;
201 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
203 GVariantIter *iter = NULL;
209 memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
211 /* Update request structure */
212 req.node_id = node_id;
213 g_variant_get(src_addr, "a(y)", &iter);
214 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
215 req.src_addr[i] = value;
219 req.cluster_id = cluster_id;
222 g_variant_get(dst_addr, "a(y)", &iter);
223 while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
224 req.dst_addr[i] = value;
227 req.bind_type = bind_type;
228 req.group_addr = group_addr;
231 /* Allocate response callback data */
233 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
234 invocation, NULL, 0);
235 if (NULL == resp_cb_data) {
236 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
238 /* Send failure response */
239 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
244 /* Dispatch request */
245 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
246 ZBLIB_DRIVER_TYPE_ZDO_BIND,
247 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
249 on_zdo_bind_unbind_req_resp, resp_cb_data);
251 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
253 /* Free response callback data */
254 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
256 /* Send failure response */
257 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
265 void zigbee_service_dbus_interface_zdo_bind_notification(ZigBeeServiceInterface *service_interface,
266 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
268 ZigbeeZdo_bind *bind_object;
270 zblib_check_null_ret("service_interface", service_interface);
272 if (NULL == noti_data || 0 == noti_data_len) {
273 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
277 bind_object = _service_interface_ref_zigbee_zdo_bind(service_interface);
278 zblib_check_null_ret("bind_object", bind_object);
280 NOT_USED(noti_cb_data);
283 case ZBLIB_ZDO_BIND_NOTI_BIND_REQ: {
284 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
286 Z_LOGD("'bind_rsp' status : [0x%X]", rsp->status);
288 zigbee_zdo_bind_emit_bind_rsp(bind_object, rsp->status);
291 case ZBLIB_ZDO_BIND_NOTI_UNBIND_REQ: {
292 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
294 Z_LOGD("'unbind_rsp' status : [0x%X]", rsp->status);
296 zigbee_zdo_bind_emit_unbind_rsp(bind_object, rsp->status);
300 Z_LOGE("Unexpected notification [%x]", noti_id);
304 /* ZigbeeZdo_bind should be dereferenced */
305 g_object_unref(bind_object);
308 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
309 ZigbeeObjectSkeleton *zigbee_object)
311 ZigbeeZdo_bind *zdo_bind_object;
313 if (NULL == service_interface) {
314 Z_LOGE("service_interface is NULL");
318 zdo_bind_object = zigbee_zdo_bind_skeleton_new();
319 zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
320 g_object_unref(zdo_bind_object);
322 Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
325 * Register signal handlers for 'zdo_bind' interface
327 g_signal_connect(zdo_bind_object,
329 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
331 g_signal_connect(zdo_bind_object,
333 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);