Fix build error on plugin build
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_door_lock.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_door_lock.h>
22
23 static void on_door_lock_subscribe_lock_event_resp(ZigBeeServiceInterface *service_interface,
24         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
25 {
26         ZigbeeServiceInterfaceRespCbData_t *cb_data =
27                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
28
29         NOT_USED(cb_data);
30         NOT_USED(service_interface);
31         NOT_USED(request_id);
32         NOT_USED(resp_data);
33         NOT_USED(resp_data_len);
34 }
35
36 static gboolean on_door_lock_subscribe_lock_event(ZigbeeDoor_lock *door_lock_object,
37         GDBusMethodInvocation *invocation,
38         GVariant *eui64,
39         gchar endpoint,
40         gpointer user_data)
41 {
42         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
43         ZigbeeDoorLockSubscriberLockEvent_t req;
44         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
45
46         GVariantIter *iter = NULL;
47         guint i = 0;
48
49         gboolean ret;
50
51         memset(&req, 0x0, sizeof(ZigbeeDoorLockSubscriberLockEvent_t));
52
53         /* Update request structure */
54         g_variant_get(eui64, "ay", &iter);
55         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
56                 i++;
57                 if (i >= ZIGBEE_EUI64_SIZE)
58                         break;
59         }
60         req.endpoint = endpoint;
61
62         /* Allocate response callback data */
63         resp_cb_data =
64                 zigbee_service_dbus_interface_create_resp_cb_data(door_lock_object,
65                         invocation, NULL, 0);
66         if (NULL == resp_cb_data) {
67                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
68
69                 /* Send failure response */
70                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
71
72                 return TRUE;
73         }
74
75         /* Dispatch request */
76         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
77                 ZBLIB_DRIVER_TYPE_DOOR_LOCK,
78                 ZBLIB_DOOR_LOCK_OPS_SUBSCRIBE_LOCK_EVENT,
79                 &req, sizeof(req),
80                 on_door_lock_subscribe_lock_event_resp, resp_cb_data);
81         if (FALSE == ret) {
82                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
83
84                 /* Free response callback data */
85                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
86
87                 /* Send failure response */
88                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
89
90                 return TRUE;
91         }
92
93         return TRUE;
94 }
95
96 static void on_door_lock_set_door_lock_pin_resp(ZigBeeServiceInterface *service_interface,
97         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
98 {
99         ZigbeeServiceInterfaceRespCbData_t *cb_data =
100                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
101
102         NOT_USED(cb_data);
103         NOT_USED(service_interface);
104         NOT_USED(request_id);
105         NOT_USED(resp_data);
106         NOT_USED(resp_data_len);
107 }
108
109 static gboolean on_door_lock_set_door_lock_pin(ZigbeeDoor_lock *door_lock_object,
110         GDBusMethodInvocation *invocation,
111         gshort uid,
112         gchar ustatus,
113         gchar utype,
114         GVariant *eui64,
115         gchar endpoint,
116         gchar *pin,
117         gpointer user_data)
118 {
119         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
120         ZigbeeDoorLockSetDoorLockPin_t req;
121         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
122
123         GVariantIter *iter = NULL;
124         guint i = 0;
125
126         gboolean ret;
127
128         memset(&req, 0x0, sizeof(ZigbeeDoorLockSetDoorLockPin_t));
129
130         /* Update request structure */
131         req.uid = uid;
132         req.ustatus = ustatus;
133         req.utype = utype;
134         g_variant_get(eui64, "ay", &iter);
135         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
136                 i++;
137                 if (i >= ZIGBEE_EUI64_SIZE)
138                         break;
139         }
140         req.endpoint = endpoint;
141         g_strlcpy(req.pin, pin, ZIGBEE_DOOR_LOCK_MAX_PIN_LEN + 1);
142
143         /* Allocate response callback data */
144         resp_cb_data =
145                 zigbee_service_dbus_interface_create_resp_cb_data(door_lock_object,
146                         invocation, NULL, 0);
147         if (NULL == resp_cb_data) {
148                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
149
150                 /* Send failure response */
151                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
152
153                 return TRUE;
154         }
155
156         /* Dispatch request */
157         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
158                 ZBLIB_DRIVER_TYPE_DOOR_LOCK,
159                 ZBLIB_DOOR_LOCK_OPS_SET_DOOR_LOCK_PIN,
160                 &req, sizeof(req),
161                 on_door_lock_set_door_lock_pin_resp, resp_cb_data);
162         if (FALSE == ret) {
163                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
164
165                 /* Free response callback data */
166                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
167
168                 /* Send failure response */
169                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
170
171                 return TRUE;
172         }
173
174         return TRUE;
175 }
176
177 static void on_door_lock_set_door_lock_resp(ZigBeeServiceInterface *service_interface,
178         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
179 {
180         ZigbeeServiceInterfaceRespCbData_t *cb_data =
181                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
182
183         NOT_USED(cb_data);
184         NOT_USED(service_interface);
185         NOT_USED(request_id);
186         NOT_USED(resp_data);
187         NOT_USED(resp_data_len);
188 }
189
190 static gboolean on_door_lock_set_door_lock(ZigbeeDoor_lock *door_lock_object,
191         GDBusMethodInvocation *invocation,
192         GVariant *eui64,
193         gchar endpoint,
194         gchar *pin,
195         gint lock_unlock_type,
196         gpointer user_data)
197 {
198         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
199         ZigbeeDoorLockSetDoorLock_t req;
200         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
201
202         GVariantIter *iter = NULL;
203         guint i = 0;
204
205         gboolean ret;
206
207         memset(&req, 0x0, sizeof(ZigbeeDoorLockSetDoorLock_t));
208
209         /* Update request structure */
210         g_variant_get(eui64, "ay", &iter);
211         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
212                 i++;
213                 if (i >= ZIGBEE_EUI64_SIZE)
214                         break;
215         }
216         req.endpoint = endpoint;
217         g_strlcpy(req.pin, pin, ZIGBEE_DOOR_LOCK_MAX_PIN_LEN + 1);
218         req.lock_unlock_type = lock_unlock_type;
219
220         /* Allocate response callback data */
221         resp_cb_data =
222                 zigbee_service_dbus_interface_create_resp_cb_data(door_lock_object,
223                         invocation, NULL, 0);
224         if (NULL == resp_cb_data) {
225                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
226
227                 /* Send failure response */
228                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
229
230                 return TRUE;
231         }
232
233         /* Dispatch request */
234         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
235                 ZBLIB_DRIVER_TYPE_DOOR_LOCK,
236                 ZBLIB_DOOR_LOCK_OPS_SET_DOOR_LOCK,
237                 &req, sizeof(req),
238                 on_door_lock_set_door_lock_resp, resp_cb_data);
239         if (FALSE == ret) {
240                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
241
242                 /* Free response callback data */
243                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
244
245                 /* Send failure response */
246                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
247
248                 return TRUE;
249         }
250
251         return TRUE;
252 }
253
254 static void on_door_lock_get_lock_state_resp(ZigBeeServiceInterface *service_interface,
255         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
256 {
257         ZigbeeServiceInterfaceRespCbData_t *cb_data =
258                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
259
260         NOT_USED(cb_data);
261         NOT_USED(service_interface);
262         NOT_USED(request_id);
263         NOT_USED(resp_data);
264         NOT_USED(resp_data_len);
265 }
266
267 static gboolean on_door_lock_get_lock_state(ZigbeeDoor_lock *door_lock_object,
268         GDBusMethodInvocation *invocation,
269         GVariant *eui64,
270         gchar endpoint,
271         gpointer user_data)
272 {
273         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
274         ZigbeeDoorLockGetDoorLock_t req;
275         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
276
277         GVariantIter *iter = NULL;
278         guint i = 0;
279
280         gboolean ret;
281
282         memset(&req, 0x0, sizeof(ZigbeeDoorLockGetDoorLock_t));
283
284         /* Update request structure */
285         g_variant_get(eui64, "ay", &iter);
286         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
287                 i++;
288                 if (i >= ZIGBEE_EUI64_SIZE)
289                         break;
290         }
291         req.endpoint = endpoint;
292
293         /* Allocate response callback data */
294         resp_cb_data =
295                 zigbee_service_dbus_interface_create_resp_cb_data(door_lock_object,
296                         invocation, NULL, 0);
297         if (NULL == resp_cb_data) {
298                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
299
300                 /* Send failure response */
301                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
302
303                 return TRUE;
304         }
305
306         /* Dispatch request */
307         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
308                 ZBLIB_DRIVER_TYPE_DOOR_LOCK,
309                 ZBLIB_DOOR_LOCK_OPS_GET_LOCK_STATE,
310                 &req, sizeof(req),
311                 on_door_lock_get_lock_state_resp, resp_cb_data);
312         if (FALSE == ret) {
313                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
314
315                 /* Free response callback data */
316                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
317
318                 /* Send failure response */
319                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
320
321                 return TRUE;
322         }
323
324         return TRUE;
325 }
326
327 gboolean zigbee_service_dbus_interface_door_lock_init(ZigBeeServiceInterface *service_interface,
328         ZigbeeObjectSkeleton *zigbee_object)
329 {
330         ZigbeeDoor_lock *door_lock_object;
331
332         if (NULL == service_interface) {
333                 Z_LOGE("service_interface is NULL");
334                 return FALSE;
335         }
336
337         door_lock_object = zigbee_door_lock_skeleton_new();
338         zigbee_object_skeleton_set_door_lock(zigbee_object, door_lock_object);
339         g_object_unref(door_lock_object);
340
341         Z_LOGI("door_lock_object: [%p]", door_lock_object);
342
343         /*
344          * Register signal handlers for 'door_lock' interface
345          */
346         g_signal_connect(door_lock_object,
347                 "handle-subscribe-lock-event",
348                 G_CALLBACK(on_door_lock_subscribe_lock_event), service_interface);
349
350         g_signal_connect(door_lock_object,
351                 "handle-set-door-lock-pin",
352                 G_CALLBACK(on_door_lock_set_door_lock_pin), service_interface);
353
354         g_signal_connect(door_lock_object,
355                 "handle-set-door-lock",
356                 G_CALLBACK(on_door_lock_set_door_lock), service_interface);
357
358         g_signal_connect(door_lock_object,
359                 "handle-get-lock-state",
360                 G_CALLBACK(on_door_lock_get_lock_state), service_interface);
361
362         return TRUE;
363 }