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");
65 on_off_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
66 zblib_check_null_free_and_ret("on_off_object", on_off_object, cb_data);
68 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
69 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
71 zigbee_zcl_on_off_complete_set_on_off(on_off_object, invocation,
77 static gboolean on_on_off_set_on_off(ZigbeeZcl_on_off *on_off_object,
78 GDBusMethodInvocation *invocation,
84 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
85 ZigbeeZclOnOffSetOnOff_t req;
86 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
90 memset(&req, 0x0, sizeof(ZigbeeZclOnOffSetOnOff_t));
92 /* Update request structure */
93 req.node_id = node_id;
94 req.endpoint = endpoint;
95 req.on_off_type = on_off_type;
97 /* Allocate response callback data */
99 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
100 invocation, NULL, 0);
101 if (NULL == resp_cb_data) {
102 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
104 /* Send failure response */
105 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
110 /* Dispatch request */
111 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
112 ZBLIB_DRIVER_TYPE_ZCL_ON_OFF,
113 ZBLIB_ZCL_ON_OFF_OPS_SET_ON_OFF,
115 on_on_off_set_on_off_resp, resp_cb_data);
117 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
119 /* Free response callback data */
120 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
122 /* Send failure response */
123 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
131 static void on_on_off_get_on_off_state_resp(ZigBeeServiceInterface *service_interface,
132 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
134 ZigbeeServiceInterfaceRespCbData_t *cb_data =
135 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
137 ZigbeeZcl_on_off *on_off_object;
138 GDBusMethodInvocation *invocation;
139 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
141 NOT_USED(service_interface);
142 NOT_USED(request_id);
144 if (NULL == resp_data || 0 == resp_data_len) {
145 Z_LOGE("resp_data is null");
150 on_off_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
151 zblib_check_null_free_and_ret("on_off_object", on_off_object, cb_data);
153 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
154 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
156 zigbee_zcl_on_off_complete_get_on_off_state(on_off_object, invocation,
162 static gboolean on_on_off_get_on_off_state(ZigbeeZcl_on_off *on_off_object,
163 GDBusMethodInvocation *invocation,
168 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
169 ZigbeeZclOnOffGetOnOffState_t req;
170 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
174 memset(&req, 0x0, sizeof(ZigbeeZclOnOffGetOnOffState_t));
176 /* Update request structure */
177 req.node_id = node_id;
178 req.endpoint = endpoint;
180 /* Allocate response callback data */
182 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
183 invocation, NULL, 0);
184 if (NULL == resp_cb_data) {
185 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
187 /* Send failure response */
188 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
193 /* Dispatch request */
194 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
195 ZBLIB_DRIVER_TYPE_ZCL_ON_OFF,
196 ZBLIB_ZCL_ON_OFF_OPS_GET_ON_OFF_STATE,
198 on_on_off_get_on_off_state_resp, resp_cb_data);
200 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
202 /* Free response callback data */
203 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
205 /* Send failure response */
206 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
214 void zigbee_service_dbus_interface_zcl_on_off_notification(ZigBeeServiceInterface *service_interface,
215 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
217 ZigbeeZcl_on_off *on_off_object = NULL;
219 zblib_check_null_ret("service_interface", service_interface);
221 if (NULL == noti_data || 0 == noti_data_len) {
222 Z_LOGE("noti_data is NULL");
226 on_off_object = _service_interface_ref_zigbee_zcl_on_off(service_interface);
227 zblib_check_null_ret("on_off_object", on_off_object);
229 NOT_USED(noti_cb_data);
232 case ZBLIB_ZCL_ON_OFF_NOTI_ON_OFF_STATE: {
233 ZigbeeZclOnOffOnOffState_t *state_t =
234 (ZigbeeZclOnOffOnOffState_t*)noti_data;
236 Z_LOGD("on off state : [0x%X]", state_t->state);
238 zigbee_zcl_on_off_emit_on_off_state(on_off_object, state_t->state);
242 Z_LOGE("Unexpected notification [%x]", noti_id);
246 /* ZigbeeZcl_on_off should be dereferenced */
247 g_object_unref(on_off_object);
250 gboolean zigbee_service_dbus_interface_zcl_on_off_init(ZigBeeServiceInterface *service_interface,
251 ZigbeeObjectSkeleton *zigbee_object)
253 ZigbeeZcl_on_off *on_off_object;
255 if (NULL == service_interface) {
256 Z_LOGE("service_interface is NULL");
260 on_off_object = zigbee_zcl_on_off_skeleton_new();
261 zigbee_object_skeleton_set_zcl_on_off(zigbee_object, on_off_object);
262 g_object_unref(on_off_object);
264 Z_LOGI("on_off_object: [%p]", on_off_object);
267 * Register signal handlers for 'on_off' interface
269 g_signal_connect(on_off_object,
271 G_CALLBACK(on_on_off_set_on_off), service_interface);
273 g_signal_connect(on_off_object,
274 "handle-get-on-off-state",
275 G_CALLBACK(on_on_off_get_on_off_state), service_interface);