Add missing memory deallocation
[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                 g_free(cb_data);
62                 return;
63         }
64
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);
67
68         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
69         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
70
71         zigbee_zcl_on_off_complete_set_on_off(on_off_object, invocation,
72                 payload->result);
73
74         g_free(cb_data);
75 }
76
77 static gboolean on_on_off_set_on_off(ZigbeeZcl_on_off *on_off_object,
78         GDBusMethodInvocation *invocation,
79         gshort node_id,
80         gchar endpoint,
81         gchar on_off_type,
82         gpointer user_data)
83 {
84         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
85         ZigbeeZclOnOffSetOnOff_t req;
86         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
87
88         gboolean ret;
89
90         memset(&req, 0x0, sizeof(ZigbeeZclOnOffSetOnOff_t));
91
92         /* Update request structure */
93         req.node_id = node_id;
94         req.endpoint = endpoint;
95         req.on_off_type = on_off_type;
96
97         /* Allocate response callback data */
98         resp_cb_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!");
103
104                 /* Send failure response */
105                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
106
107                 return TRUE;
108         }
109
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,
114                 &req, sizeof(req),
115                 on_on_off_set_on_off_resp, resp_cb_data);
116         if (FALSE == ret) {
117                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
118
119                 /* Free response callback data */
120                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
121
122                 /* Send failure response */
123                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
124
125                 return TRUE;
126         }
127
128         return TRUE;
129 }
130
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)
133 {
134         ZigbeeServiceInterfaceRespCbData_t *cb_data =
135                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
136
137         ZigbeeZcl_on_off *on_off_object;
138         GDBusMethodInvocation *invocation;
139         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
140
141         NOT_USED(service_interface);
142         NOT_USED(request_id);
143
144         if (NULL == resp_data || 0 == resp_data_len) {
145                 Z_LOGE("resp_data is null");
146                 g_free(cb_data);
147                 return;
148         }
149
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);
152
153         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
154         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
155
156         zigbee_zcl_on_off_complete_get_on_off_state(on_off_object, invocation,
157                 payload->result);
158
159         g_free(cb_data);
160 }
161
162 static gboolean on_on_off_get_on_off_state(ZigbeeZcl_on_off *on_off_object,
163         GDBusMethodInvocation *invocation,
164         gshort node_id,
165         gchar endpoint,
166         gpointer user_data)
167 {
168         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
169         ZigbeeZclOnOffGetOnOffState_t req;
170         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
171
172         gboolean ret;
173
174         memset(&req, 0x0, sizeof(ZigbeeZclOnOffGetOnOffState_t));
175
176         /* Update request structure */
177         req.node_id = node_id;
178         req.endpoint = endpoint;
179
180         /* Allocate response callback data */
181         resp_cb_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!");
186
187                 /* Send failure response */
188                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
189
190                 return TRUE;
191         }
192
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,
197                 &req, sizeof(req),
198                 on_on_off_get_on_off_state_resp, resp_cb_data);
199         if (FALSE == ret) {
200                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
201
202                 /* Free response callback data */
203                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
204
205                 /* Send failure response */
206                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
207
208                 return TRUE;
209         }
210
211         return TRUE;
212 }
213
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)
216 {
217         ZigbeeZcl_on_off *on_off_object = NULL;
218
219         zblib_check_null_ret("service_interface", service_interface);
220
221         if (NULL == noti_data || 0 == noti_data_len) {
222                 Z_LOGE("noti_data is NULL");
223                 return;
224         }
225
226         on_off_object = _service_interface_ref_zigbee_zcl_on_off(service_interface);
227         zblib_check_null_ret("on_off_object", on_off_object);
228
229         NOT_USED(noti_cb_data);
230
231         switch(noti_id) {
232         case ZBLIB_ZCL_ON_OFF_NOTI_ON_OFF_STATE: {
233                 ZigbeeZclOnOffOnOffState_t *state_t =
234                         (ZigbeeZclOnOffOnOffState_t*)noti_data;
235
236                 Z_LOGD("on off state : [0x%X]", state_t->state);
237
238                 zigbee_zcl_on_off_emit_on_off_state(on_off_object, state_t->state);
239         break;
240         }
241         default:
242                 Z_LOGE("Unexpected notification [%x]", noti_id);
243         break;
244         }
245
246         /* ZigbeeZcl_on_off should be dereferenced */
247         g_object_unref(on_off_object);
248 }
249
250 gboolean zigbee_service_dbus_interface_zcl_on_off_init(ZigBeeServiceInterface *service_interface,
251         ZigbeeObjectSkeleton *zigbee_object)
252 {
253         ZigbeeZcl_on_off *on_off_object;
254
255         if (NULL == service_interface) {
256                 Z_LOGE("service_interface is NULL");
257                 return FALSE;
258         }
259
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);
263
264         Z_LOGI("on_off_object: [%p]", on_off_object);
265
266         /*
267          * Register signal handlers for 'on_off' interface
268          */
269         g_signal_connect(on_off_object,
270                 "handle-set-on-off",
271                 G_CALLBACK(on_on_off_set_on_off), service_interface);
272
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);
276
277         return TRUE;
278 }