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_zcl_on_off.h>
23 static void *_service_interface_ref_zigbee_zcl_on_off(ZigBeeServiceInterface *service_interface)
25 ZigbeeObjectSkeleton *zigbee_object = NULL;
26 ZigbeeCustomData_t *custom_data = NULL;
27 ZigbeeZcl_on_off *on_off_object = NULL;
29 custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
30 if (NULL == custom_data) {
31 Z_LOGE("D-BUS service interface custom_data is NULL!");
35 /* Get zigbee object */
36 zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
37 if (NULL == zigbee_object) {
38 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
42 on_off_object = zigbee_object_get_zcl_on_off(ZIGBEE_OBJECT(zigbee_object));
46 static void on_on_off_set_on_off_resp(ZigBeeServiceInterface *service_interface,
47 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
49 ZigbeeServiceInterfaceRespCbData_t *cb_data =
50 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
52 ZigbeeZcl_on_off *on_off_object;
53 GDBusMethodInvocation *invocation;
54 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
56 NOT_USED(service_interface);
59 if (NULL == resp_data || 0 == resp_data_len) {
60 Z_LOGE("resp_data is null");
64 on_off_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
65 zblib_check_null_free_and_ret("on_off_object", on_off_object, cb_data);
67 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
68 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
70 zigbee_zcl_on_off_complete_set_on_off(on_off_object, invocation,
76 static gboolean on_on_off_set_on_off(ZigbeeZcl_on_off *on_off_object,
77 GDBusMethodInvocation *invocation,
83 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
84 ZigbeeZclOnOffSetOnOff_t req;
85 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
89 memset(&req, 0x0, sizeof(ZigbeeZclOnOffSetOnOff_t));
91 /* Update request structure */
92 req.node_id = node_id;
93 req.endpoint = endpoint;
94 req.on_off_type = on_off_type;
96 /* Allocate response callback data */
98 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
100 if (NULL == resp_cb_data) {
101 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
103 /* Send failure response */
104 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
109 /* Dispatch request */
110 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
111 ZBLIB_DRIVER_TYPE_ZCL_ON_OFF,
112 ZBLIB_ZCL_ON_OFF_OPS_SET_ON_OFF,
114 on_on_off_set_on_off_resp, resp_cb_data);
116 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
118 /* Free response callback data */
119 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
121 /* Send failure response */
122 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
130 static void on_on_off_get_on_off_state_resp(ZigBeeServiceInterface *service_interface,
131 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
133 ZigbeeServiceInterfaceRespCbData_t *cb_data =
134 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
136 ZigbeeZcl_on_off *on_off_object;
137 GDBusMethodInvocation *invocation;
138 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
140 NOT_USED(service_interface);
141 NOT_USED(request_id);
143 if (NULL == resp_data || 0 == resp_data_len) {
144 Z_LOGE("resp_data is null");
148 on_off_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
149 zblib_check_null_free_and_ret("on_off_object", on_off_object, cb_data);
151 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
152 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
154 zigbee_zcl_on_off_complete_get_on_off_state(on_off_object, invocation,
160 static gboolean on_on_off_get_on_off_state(ZigbeeZcl_on_off *on_off_object,
161 GDBusMethodInvocation *invocation,
166 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
167 ZigbeeZclOnOffGetOnOffState_t req;
168 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
172 memset(&req, 0x0, sizeof(ZigbeeZclOnOffGetOnOffState_t));
174 /* Update request structure */
175 req.node_id = node_id;
176 req.endpoint = endpoint;
178 /* Allocate response callback data */
180 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
181 invocation, NULL, 0);
182 if (NULL == resp_cb_data) {
183 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
185 /* Send failure response */
186 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
191 /* Dispatch request */
192 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
193 ZBLIB_DRIVER_TYPE_ZCL_ON_OFF,
194 ZBLIB_ZCL_ON_OFF_OPS_GET_ON_OFF_STATE,
196 on_on_off_get_on_off_state_resp, resp_cb_data);
198 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
200 /* Free response callback data */
201 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
203 /* Send failure response */
204 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
212 void zigbee_service_dbus_interface_zcl_on_off_notification(ZigBeeServiceInterface *service_interface,
213 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
215 ZigbeeZcl_on_off *on_off_object = NULL;
217 zblib_check_null_ret("service_interface", service_interface);
219 if (NULL == noti_data || 0 == noti_data_len) {
220 Z_LOGE("noti_data is NULL");
224 on_off_object = _service_interface_ref_zigbee_zcl_on_off(service_interface);
225 zblib_check_null_ret("on_off_object", on_off_object);
227 NOT_USED(noti_cb_data);
230 case ZBLIB_ZCL_ON_OFF_NOTI_ON_OFF_STATE: {
231 ZigbeeZclOnOffOnOffState_t *state_t =
232 (ZigbeeZclOnOffOnOffState_t*)noti_data;
234 Z_LOGD("on off state : [0x%X]", state_t->state);
236 zigbee_zcl_on_off_emit_on_off_state(on_off_object, state_t->state);
240 Z_LOGE("Unexpected notification [%x]", noti_id);
244 /* ZigbeeZcl_on_off should be dereferenced */
245 g_object_unref(on_off_object);
248 gboolean zigbee_service_dbus_interface_zcl_on_off_init(ZigBeeServiceInterface *service_interface,
249 ZigbeeObjectSkeleton *zigbee_object)
251 ZigbeeZcl_on_off *on_off_object;
253 if (NULL == service_interface) {
254 Z_LOGE("service_interface is NULL");
258 on_off_object = zigbee_zcl_on_off_skeleton_new();
259 zigbee_object_skeleton_set_zcl_on_off(zigbee_object, on_off_object);
260 g_object_unref(on_off_object);
262 Z_LOGI("on_off_object: [%p]", on_off_object);
265 * Register signal handlers for 'on_off' interface
267 g_signal_connect(on_off_object,
269 G_CALLBACK(on_on_off_set_on_off), service_interface);
271 g_signal_connect(on_off_object,
272 "handle-get-on-off-state",
273 G_CALLBACK(on_on_off_get_on_off_state), service_interface);