ab6a7b91292045eeb83123ef5ee936a755cd1b07
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_on_off.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_zcl_on_off.h>
22
23 static void *_service_interface_ref_zigbee_zcl_on_off(ZigBeeServiceInterface *service_interface)
24 {
25         ZigbeeObjectSkeleton *zigbee_object = NULL;
26         ZigbeeCustomData_t *custom_data = NULL;
27         ZigbeeZcl_on_off *on_off_object = NULL;
28
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!");
32                 return NULL;
33         }
34
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);
39                 return NULL;
40         }
41
42         on_off_object = zigbee_object_get_zcl_on_off(ZIGBEE_OBJECT(zigbee_object));
43         return on_off_object;
44 }
45
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)
48 {
49         ZigbeeServiceInterfaceRespCbData_t *cb_data =
50                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
51
52         ZigbeeZcl_on_off *on_off_object;
53         GDBusMethodInvocation *invocation;
54         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
55
56         NOT_USED(service_interface);
57         NOT_USED(request_id);
58
59         if (NULL == resp_data || 0 == resp_data_len) {
60                 Z_LOGE("resp_data is null");
61                 return;
62         }
63
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);
66
67         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
68         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
69
70         zigbee_zcl_on_off_complete_set_on_off(on_off_object, invocation,
71                 payload->result);
72
73         g_free(cb_data);
74 }
75
76 static gboolean on_on_off_set_on_off(ZigbeeZcl_on_off *on_off_object,
77         GDBusMethodInvocation *invocation,
78         gshort node_id,
79         gchar endpoint,
80         gchar on_off_type,
81         gpointer user_data)
82 {
83         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
84         ZigbeeZclOnOffSetOnOff_t req;
85         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
86
87         gboolean ret;
88
89         memset(&req, 0x0, sizeof(ZigbeeZclOnOffSetOnOff_t));
90
91         /* Update request structure */
92         req.node_id = node_id;
93         req.endpoint = endpoint;
94         req.on_off_type = on_off_type;
95
96         /* Allocate response callback data */
97         resp_cb_data =
98                 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
99                         invocation, NULL, 0);
100         if (NULL == resp_cb_data) {
101                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
102
103                 /* Send failure response */
104                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
105
106                 return TRUE;
107         }
108
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,
113                 &req, sizeof(req),
114                 on_on_off_set_on_off_resp, resp_cb_data);
115         if (FALSE == ret) {
116                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
117
118                 /* Free response callback data */
119                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
120
121                 /* Send failure response */
122                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
123
124                 return TRUE;
125         }
126
127         return TRUE;
128 }
129
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)
132 {
133         ZigbeeServiceInterfaceRespCbData_t *cb_data =
134                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
135
136         ZigbeeZcl_on_off *on_off_object;
137         GDBusMethodInvocation *invocation;
138         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
139
140         NOT_USED(service_interface);
141         NOT_USED(request_id);
142
143         if (NULL == resp_data || 0 == resp_data_len) {
144                 Z_LOGE("resp_data is null");
145                 return;
146         }
147
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);
150
151         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
152         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
153
154         zigbee_zcl_on_off_complete_get_on_off_state(on_off_object, invocation,
155                 payload->result);
156
157         g_free(cb_data);
158 }
159
160 static gboolean on_on_off_get_on_off_state(ZigbeeZcl_on_off *on_off_object,
161         GDBusMethodInvocation *invocation,
162         gshort node_id,
163         gchar endpoint,
164         gpointer user_data)
165 {
166         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
167         ZigbeeZclOnOffGetOnOffState_t req;
168         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
169
170         gboolean ret;
171
172         memset(&req, 0x0, sizeof(ZigbeeZclOnOffGetOnOffState_t));
173
174         /* Update request structure */
175         req.node_id = node_id;
176         req.endpoint = endpoint;
177
178         /* Allocate response callback data */
179         resp_cb_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!");
184
185                 /* Send failure response */
186                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
187
188                 return TRUE;
189         }
190
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,
195                 &req, sizeof(req),
196                 on_on_off_get_on_off_state_resp, resp_cb_data);
197         if (FALSE == ret) {
198                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
199
200                 /* Free response callback data */
201                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
202
203                 /* Send failure response */
204                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
205
206                 return TRUE;
207         }
208
209         return TRUE;
210 }
211
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)
214 {
215         ZigbeeZcl_on_off *on_off_object = NULL;
216
217         zblib_check_null_ret("service_interface", service_interface);
218
219         if (NULL == noti_data || 0 == noti_data_len) {
220                 Z_LOGE("noti_data is NULL");
221                 return;
222         }
223
224         on_off_object = _service_interface_ref_zigbee_zcl_on_off(service_interface);
225         zblib_check_null_ret("on_off_object", on_off_object);
226
227         NOT_USED(noti_cb_data);
228
229         switch(noti_id) {
230         case ZBLIB_ZCL_ON_OFF_NOTI_ON_OFF_STATE: {
231                 ZigbeeZclOnOffOnOffState_t *state_t =
232                         (ZigbeeZclOnOffOnOffState_t*)noti_data;
233
234                 Z_LOGD("on off state : [0x%X]", state_t->state);
235
236                 zigbee_zcl_on_off_emit_on_off_state(on_off_object, state_t->state);
237         break;
238         }
239         default:
240                 Z_LOGE("Unexpected notification [%x]", noti_id);
241         break;
242         }
243
244         /* ZigbeeZcl_on_off should be dereferenced */
245         g_object_unref(on_off_object);
246 }
247
248 gboolean zigbee_service_dbus_interface_zcl_on_off_init(ZigBeeServiceInterface *service_interface,
249         ZigbeeObjectSkeleton *zigbee_object)
250 {
251         ZigbeeZcl_on_off *on_off_object;
252
253         if (NULL == service_interface) {
254                 Z_LOGE("service_interface is NULL");
255                 return FALSE;
256         }
257
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);
261
262         Z_LOGI("on_off_object: [%p]", on_off_object);
263
264         /*
265          * Register signal handlers for 'on_off' interface
266          */
267         g_signal_connect(on_off_object,
268                 "handle-set-on-off",
269                 G_CALLBACK(on_on_off_set_on_off), service_interface);
270
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);
274
275         return TRUE;
276 }