16e47a3b61c17bdf8cdea5b4f7d20fce551bc39d
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface.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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <glib.h>
24 #include <dlog.h>
25
26 #include <zblib.h>
27 #include <zblib_service.h>
28 #include <zblib_service_interface.h>
29
30 #include "zigbee_service_interface.h"
31 #include "zigbee_service_interface_common.h"
32 #include "zigbee_service_dbus_interface.h"
33
34 #define ZIGBEE_DBUS_SERVICE_INTERFACE_NAME "zigbee-dbus"
35
36 static gboolean zigbee_on_manager_get_zigbee_state(ZigbeeManager *zigbee_mgr,
37         GDBusMethodInvocation *invocation, gpointer user_data)
38 {
39         Z_LOGI("Entered");
40
41         NOT_USED(zigbee_mgr);
42         NOT_USED(invocation);
43         NOT_USED(user_data);
44
45         /*
46          * TODO -
47          * Create and send request for processing
48          */
49
50         return TRUE;
51 }
52
53 static void zigbee_service_dbus_interface_initialize_interfaces(ZigBeeService *service,
54         ZigbeeObjectSkeleton *zigbee_object)
55 {
56         gboolean ret;
57
58         ret = zigbee_service_dbus_interface_alarm_init(service, zigbee_object);
59         Z_LOGD("ret: %d", ret);
60
61         ret = zigbee_service_dbus_interface_custom_init(service, zigbee_object);
62         Z_LOGD("ret: %d", ret);
63
64         ret = zigbee_service_dbus_interface_door_lock_init(service, zigbee_object);
65         Z_LOGD("ret: %d", ret);
66
67         ret = zigbee_service_dbus_interface_fan_control_init(service, zigbee_object);
68         Z_LOGD("ret: %d", ret);
69
70         ret = zigbee_service_dbus_interface_level_control_init(service, zigbee_object);
71         Z_LOGD("ret: %d", ret);
72
73         ret = zigbee_service_dbus_interface_mfglib_control_init(service, zigbee_object);
74         Z_LOGD("ret: %d", ret);
75
76         ret = zigbee_service_dbus_interface_on_off_init(service, zigbee_object);
77         Z_LOGD("ret: %d", ret);
78
79         ret = zigbee_service_dbus_interface_service_init(service, zigbee_object);
80         Z_LOGD("ret: %d", ret);
81
82         ret = zigbee_service_dbus_interface_thermostat_init(service, zigbee_object);
83         Z_LOGD("ret: %d", ret);
84
85         ret = zigbee_service_dbus_interface_zclbasic_control_init(service, zigbee_object);
86         Z_LOGD("ret: %d", ret);
87
88         ret = zigbee_service_dbus_interface_zcl_color_init(service, zigbee_object);
89         Z_LOGD("ret: %d", ret);
90
91         ret = zigbee_service_dbus_interface_zclglobal_control_init(service, zigbee_object);
92         Z_LOGD("ret: %d", ret);
93
94         ret = zigbee_service_dbus_interface_zcl_group_init(service, zigbee_object);
95         Z_LOGD("ret: %d", ret);
96
97         ret = zigbee_service_dbus_interface_zclias_control_init(service, zigbee_object);
98         Z_LOGD("ret: %d", ret);
99
100         ret = zigbee_service_dbus_interface_zclidentify_control_init(service, zigbee_object);
101         Z_LOGD("ret: %d", ret);
102
103         ret = zigbee_service_dbus_interface_zcl_poll_control_init(service, zigbee_object);
104         Z_LOGD("ret: %d", ret);
105
106         ret = zigbee_service_dbus_interface_zcl_scene_init(service, zigbee_object);
107         Z_LOGD("ret: %d", ret);
108
109         ret = zigbee_service_dbus_interface_zdo_bind_init(service, zigbee_object);
110         Z_LOGD("ret: %d", ret);
111
112         ret = zigbee_service_dbus_interface_zdodev_control_init(service, zigbee_object);
113         Z_LOGD("ret: %d", ret);
114 }
115
116 static void zigbee_on_name_lost(GDBusConnection *connection,
117         const gchar *name, gpointer user_data)
118 {
119         ZigbeeCustomData_t *custom_data = user_data;
120
121         Z_LOGW("'%s' - [Name Lost]", name);
122
123         NOT_USED(connection);
124
125         /* Bus name is 'lost' */
126         custom_data->name_acquired = FALSE;
127 }
128
129 static void zigbee_on_name_acquired(GDBusConnection *connection,
130         const gchar *name, gpointer user_data)
131 {
132         ZigbeeCustomData_t *custom_data = user_data;
133
134         Z_LOGI("'%s' - [Name Acquired]", name);
135
136         NOT_USED(connection);
137
138         /* Bus name is 'acquired' */
139         custom_data->name_acquired = TRUE;
140
141         if (TRUE == custom_data->sevice_interface_init_complete) {
142                 /* TODO - Emit zigbee_state signal */
143         }
144 }
145
146 static void zigbee_on_bus_acquired(GDBusConnection *connection,
147         const gchar *name, gpointer user_data)
148 {
149         ZigbeeObjectSkeleton *zigbee_object;
150         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
151         ZigBeeService *service = NULL;
152         ZigbeeCustomData_t *custom_data = NULL;
153         char *path = NULL;
154
155         Z_LOGI("'%s' - [BUS Acquired]", name);
156
157         if (NULL == service_interface) {
158                 Z_LOGE("service_interface is NULL!");
159                 return;
160         }
161
162         service = zblib_service_interface_ref_service(service_interface);
163         if (NULL == service) {
164                 Z_LOGE("service is NULL!");
165                 return;
166         }
167
168         custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
169         if (NULL == custom_data) {
170                 Z_LOGE("D-BUS service interface custom_data is NULL!");
171                 return;
172         }
173
174         /*
175          * Create ZigBee 'manager' D-BUS object
176          */
177         custom_data->zigbee_mgr = zigbee_manager_skeleton_new();
178
179         /*
180          * Set ZigBee 'manager' D-BUS object method(s)
181          */
182         g_signal_connect(custom_data->zigbee_mgr,
183                         "handle-get-zigbee-state",
184                         G_CALLBACK(zigbee_on_manager_get_zigbee_state),
185                         custom_data);
186
187         /*
188          * Export 'manager' interface on ZigBee D-BUS
189          */
190         g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(custom_data->zigbee_mgr),
191                 connection, ZIGBEE_DBUS_PATH, NULL);
192
193         /*
194          * Exports all objects managed by 'manager' on Connection (connection)
195          */
196         g_dbus_object_manager_server_set_connection(custom_data->manager, connection);
197
198         path = g_strdup_printf("%s/service", ZIGBEE_DBUS_PATH);
199         Z_LOGI("dbus object path: [%s]", path);
200
201         /*
202          * Create 'object' for specific path only once.
203          */
204         zigbee_object = g_hash_table_lookup(custom_data->objects, path);
205         if (zigbee_object) {
206                 Z_LOGW("ZigBee D-BUS interface object already created (object: %p)", zigbee_object);
207                 goto OUT;
208         }
209
210         /*
211          * Create ZigBee D-BUS object
212          */
213         zigbee_object = zigbee_object_skeleton_new(path);
214         Z_LOGI("ZigBee D-BUS object created (zigbee_object: [%p])", zigbee_object);
215
216         /*
217          * Insert ZigBee object to HASH table
218          */
219         g_hash_table_insert(custom_data->objects, g_strdup(path), zigbee_object);
220
221         /*
222          * Initialize interfaces
223          */
224         zigbee_service_dbus_interface_initialize_interfaces(service, zigbee_object);
225         Z_LOGI("ZigBee service interfaces initialized!!!");
226
227         /* Export the Object to Manager */
228         g_dbus_object_manager_server_export(custom_data->manager,
229                 G_DBUS_OBJECT_SKELETON(zigbee_object));
230
231         /* Servcie interface initialization completed */
232         custom_data->sevice_interface_init_complete = TRUE;
233
234         if (TRUE == custom_data->name_acquired) {
235                 /* TODO - Emit zigbee_state signal */
236         }
237 OUT:
238         g_free(path);
239 }
240
241 /**< ZigBee D-BUS Service interface methods */
242 struct zblib_service_interface_methods dbus_service_interface_methods = {
243         .send_response = NULL,
244         .send_notification = NULL,
245 };
246
247
248 /**< Zigbee service dbus interface initialization */
249 gboolean zigbee_service_dbus_interface_init(ZigBeeService *service)
250 {
251         ZigBeeServiceInterface *service_interface = NULL;
252         ZigbeeCustomData_t *interface_data = NULL;
253         gboolean ret;
254
255         if (NULL == service) {
256                 Z_LOGE("service is NULL");
257                 return FALSE;
258         }
259
260         /*
261          * Create ZigBee service interface object
262          */
263         service_interface = zblib_service_interface_new(service,
264                 ZIGBEE_DBUS_SERVICE_INTERFACE_NAME,
265                 &dbus_service_interface_methods);
266         if (NULL == service_interface) {
267                 Z_LOGE("Create D-BUS service interface failed!");
268
269                 return FALSE;
270         }
271
272         /*
273          * Add Service interface object to 'service'
274          */
275         ret = zblib_service_add_service_interface(service,
276                         service_interface);
277         if (FALSE == ret) {
278                 Z_LOGE("Add D-BUS service interface failed!");
279
280                 goto EXIT;
281         }
282
283         /*
284          * ZigBee D-BUS interface custom data
285          */
286         interface_data = g_malloc0(sizeof(ZigbeeCustomData_t));
287
288         /*
289          * Link interface data to service
290          */
291         ret = zblib_service_interface_link_user_data(service_interface,
292                         interface_data);
293         if (FALSE == ret) {
294                 Z_LOGE("Link D-BUS service interface data failed!");
295
296                 goto EXIT;
297         }
298
299         /* HASH table for maintaining 'objects' list */
300         interface_data->objects = g_hash_table_new(g_str_hash, g_str_equal);
301
302         /*
303          * Acquire "org.tizen.zigbee" named bus on D-BUS SYSTEM bus.
304          */
305         interface_data->bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
306                         ZIGBEE_DBUS_SERVICE,
307                         G_BUS_NAME_OWNER_FLAGS_REPLACE,
308                         zigbee_on_bus_acquired,
309                         zigbee_on_name_acquired,
310                         zigbee_on_name_lost,
311                         service_interface,
312                         NULL);
313         Z_LOGI("ZigBee D-BUS ID: [%d]", interface_data->bus_id);
314
315         interface_data->manager = g_dbus_object_manager_server_new(ZIGBEE_DBUS_PATH);
316
317         return TRUE;
318
319 EXIT:
320         g_free(interface_data);
321
322         /*
323          * Remove Service interface object from 'service'
324          */
325         ret = zblib_service_remove_service_interface(service,
326                         service_interface);
327         if (FALSE == ret) {
328                 Z_LOGE("Remove service interface failed!");
329         }
330
331         /*
332          * Free Service interface object
333          */
334         zblib_service_interface_free(service,
335                         service_interface);
336
337         return FALSE;
338 }
339
340 /**< Zigbee service dbus interface de-initialization */
341 void zigbee_service_dbus_interface_deinit(ZigBeeService *service)
342 {
343         ZigBeeServiceInterface *service_interface = NULL;
344         ZigbeeCustomData_t *interface_data = NULL;
345         gboolean ret;
346
347         if (NULL == service) {
348                 Z_LOGE("service is NULL");
349                 return;
350         }
351
352         service_interface = zblib_service_ref_service_interface(service,
353                 ZIGBEE_DBUS_SERVICE_INTERFACE_NAME);
354         if (NULL == service_interface) {
355                 Z_LOGE("D-BUS service interface not found!");
356
357                 return;
358         }
359
360         interface_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
361         if (NULL == service_interface) {
362                 Z_LOGE("D-BUS service interface not found!");
363
364                 goto EXIT;
365         }
366
367         /*
368          * Unown "org.tizen.zigbee" named bus on D-BUS SYSTEM bus
369          */
370         if (interface_data->bus_id > 0) {
371                 Z_LOGI("Unowning ZigBee Service interface D-BUS ID: [%d]", interface_data->bus_id);
372                 g_bus_unown_name(interface_data->bus_id);
373         }
374
375         /* Free resources */
376         g_hash_table_destroy(interface_data->objects);
377         g_free(interface_data);
378
379 EXIT:
380         /*
381          * Remove Service interface object from 'service'
382          */
383         ret = zblib_service_remove_service_interface(service,
384                         service_interface);
385         if (FALSE == ret) {
386                 Z_LOGE("Remove service interface failed!");
387         }
388
389         /*
390          * Free Service interface object
391          */
392         zblib_service_interface_free(service,
393                         service_interface);
394 }