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.
27 #include <zblib_service.h>
28 #include <zblib_service_interface.h>
30 #include "zigbee_service_interface.h"
31 #include "zigbee_service_interface_common.h"
32 #include "zigbee_service_dbus_interface.h"
34 #define ZIGBEE_DBUS_SERVICE_INTERFACE_NAME "zigbee-dbus"
36 static gboolean zigbee_on_manager_get_zigbee_state(ZigbeeManager *zigbee_mgr,
37 GDBusMethodInvocation *invocation, gpointer user_data)
47 * Create and send request for processing
53 static void zigbee_service_dbus_interface_initialize_interfaces(ZigBeeService *service,
54 ZigbeeObjectSkeleton *zigbee_object)
58 ret = zigbee_service_dbus_interface_alarm_init(service, zigbee_object);
59 Z_LOGD("ret: %d", ret);
61 ret = zigbee_service_dbus_interface_custom_init(service, zigbee_object);
62 Z_LOGD("ret: %d", ret);
64 ret = zigbee_service_dbus_interface_door_lock_init(service, zigbee_object);
65 Z_LOGD("ret: %d", ret);
67 ret = zigbee_service_dbus_interface_fan_control_init(service, zigbee_object);
68 Z_LOGD("ret: %d", ret);
70 ret = zigbee_service_dbus_interface_level_control_init(service, zigbee_object);
71 Z_LOGD("ret: %d", ret);
73 ret = zigbee_service_dbus_interface_mfglib_control_init(service, zigbee_object);
74 Z_LOGD("ret: %d", ret);
76 ret = zigbee_service_dbus_interface_on_off_init(service, zigbee_object);
77 Z_LOGD("ret: %d", ret);
79 ret = zigbee_service_dbus_interface_service_init(service, zigbee_object);
80 Z_LOGD("ret: %d", ret);
82 ret = zigbee_service_dbus_interface_thermostat_init(service, zigbee_object);
83 Z_LOGD("ret: %d", ret);
85 ret = zigbee_service_dbus_interface_zclbasic_control_init(service, zigbee_object);
86 Z_LOGD("ret: %d", ret);
88 ret = zigbee_service_dbus_interface_zcl_color_init(service, zigbee_object);
89 Z_LOGD("ret: %d", ret);
91 ret = zigbee_service_dbus_interface_zclglobal_control_init(service, zigbee_object);
92 Z_LOGD("ret: %d", ret);
94 ret = zigbee_service_dbus_interface_zcl_group_init(service, zigbee_object);
95 Z_LOGD("ret: %d", ret);
97 ret = zigbee_service_dbus_interface_zclias_control_init(service, zigbee_object);
98 Z_LOGD("ret: %d", ret);
100 ret = zigbee_service_dbus_interface_zclidentify_control_init(service, zigbee_object);
101 Z_LOGD("ret: %d", ret);
103 ret = zigbee_service_dbus_interface_zcl_poll_control_init(service, zigbee_object);
104 Z_LOGD("ret: %d", ret);
106 ret = zigbee_service_dbus_interface_zcl_scene_init(service, zigbee_object);
107 Z_LOGD("ret: %d", ret);
109 ret = zigbee_service_dbus_interface_zdo_bind_init(service, zigbee_object);
110 Z_LOGD("ret: %d", ret);
112 ret = zigbee_service_dbus_interface_zdodev_control_init(service, zigbee_object);
113 Z_LOGD("ret: %d", ret);
116 static void zigbee_on_name_lost(GDBusConnection *connection,
117 const gchar *name, gpointer user_data)
119 ZigbeeCustomData_t *custom_data = user_data;
121 Z_LOGW("'%s' - [Name Lost]", name);
123 NOT_USED(connection);
125 /* Bus name is 'lost' */
126 custom_data->name_acquired = FALSE;
129 static void zigbee_on_name_acquired(GDBusConnection *connection,
130 const gchar *name, gpointer user_data)
132 ZigbeeCustomData_t *custom_data = user_data;
134 Z_LOGI("'%s' - [Name Acquired]", name);
136 NOT_USED(connection);
138 /* Bus name is 'acquired' */
139 custom_data->name_acquired = TRUE;
141 if (TRUE == custom_data->sevice_interface_init_complete) {
142 /* TODO - Emit zigbee_state signal */
146 static void zigbee_on_bus_acquired(GDBusConnection *connection,
147 const gchar *name, gpointer user_data)
149 ZigbeeObjectSkeleton *zigbee_object;
150 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
151 ZigBeeService *service = NULL;
152 ZigbeeCustomData_t *custom_data = NULL;
155 Z_LOGI("'%s' - [BUS Acquired]", name);
157 if (NULL == service_interface) {
158 Z_LOGE("service_interface is NULL!");
162 service = zblib_service_interface_ref_service(service_interface);
163 if (NULL == service) {
164 Z_LOGE("service is NULL!");
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!");
175 * Create ZigBee 'manager' D-BUS object
177 custom_data->zigbee_mgr = zigbee_manager_skeleton_new();
180 * Set ZigBee 'manager' D-BUS object method(s)
182 g_signal_connect(custom_data->zigbee_mgr,
183 "handle-get-zigbee-state",
184 G_CALLBACK(zigbee_on_manager_get_zigbee_state),
188 * Export 'manager' interface on ZigBee D-BUS
190 g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(custom_data->zigbee_mgr),
191 connection, ZIGBEE_DBUS_PATH, NULL);
194 * Exports all objects managed by 'manager' on Connection (connection)
196 g_dbus_object_manager_server_set_connection(custom_data->manager, connection);
198 path = g_strdup_printf("%s/service", ZIGBEE_DBUS_PATH);
199 Z_LOGI("dbus object path: [%s]", path);
202 * Create 'object' for specific path only once.
204 zigbee_object = g_hash_table_lookup(custom_data->objects, path);
206 Z_LOGW("ZigBee D-BUS interface object already created (object: %p)", zigbee_object);
211 * Create ZigBee D-BUS object
213 zigbee_object = zigbee_object_skeleton_new(path);
214 Z_LOGI("ZigBee D-BUS object created (zigbee_object: [%p])", zigbee_object);
217 * Insert ZigBee object to HASH table
219 g_hash_table_insert(custom_data->objects, g_strdup(path), zigbee_object);
222 * Initialize interfaces
224 zigbee_service_dbus_interface_initialize_interfaces(service, zigbee_object);
225 Z_LOGI("ZigBee service interfaces initialized!!!");
227 /* Export the Object to Manager */
228 g_dbus_object_manager_server_export(custom_data->manager,
229 G_DBUS_OBJECT_SKELETON(zigbee_object));
231 /* Servcie interface initialization completed */
232 custom_data->sevice_interface_init_complete = TRUE;
234 if (TRUE == custom_data->name_acquired) {
235 /* TODO - Emit zigbee_state signal */
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,
248 /**< Zigbee service dbus interface initialization */
249 gboolean zigbee_service_dbus_interface_init(ZigBeeService *service)
251 ZigBeeServiceInterface *service_interface = NULL;
252 ZigbeeCustomData_t *interface_data = NULL;
255 if (NULL == service) {
256 Z_LOGE("service is NULL");
261 * Create ZigBee service interface object
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!");
273 * Add Service interface object to 'service'
275 ret = zblib_service_add_service_interface(service,
278 Z_LOGE("Add D-BUS service interface failed!");
284 * ZigBee D-BUS interface custom data
286 interface_data = g_malloc0(sizeof(ZigbeeCustomData_t));
289 * Link interface data to service
291 ret = zblib_service_interface_link_user_data(service_interface,
294 Z_LOGE("Link D-BUS service interface data failed!");
299 /* HASH table for maintaining 'objects' list */
300 interface_data->objects = g_hash_table_new(g_str_hash, g_str_equal);
303 * Acquire "org.tizen.zigbee" named bus on D-BUS SYSTEM bus.
305 interface_data->bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
307 G_BUS_NAME_OWNER_FLAGS_REPLACE,
308 zigbee_on_bus_acquired,
309 zigbee_on_name_acquired,
313 Z_LOGI("ZigBee D-BUS ID: [%d]", interface_data->bus_id);
315 interface_data->manager = g_dbus_object_manager_server_new(ZIGBEE_DBUS_PATH);
320 g_free(interface_data);
323 * Remove Service interface object from 'service'
325 ret = zblib_service_remove_service_interface(service,
328 Z_LOGE("Remove service interface failed!");
332 * Free Service interface object
334 zblib_service_interface_free(service,
340 /**< Zigbee service dbus interface de-initialization */
341 void zigbee_service_dbus_interface_deinit(ZigBeeService *service)
343 ZigBeeServiceInterface *service_interface = NULL;
344 ZigbeeCustomData_t *interface_data = NULL;
347 if (NULL == service) {
348 Z_LOGE("service is NULL");
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!");
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!");
368 * Unown "org.tizen.zigbee" named bus on D-BUS SYSTEM bus
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);
376 g_hash_table_destroy(interface_data->objects);
377 g_free(interface_data);
381 * Remove Service interface object from 'service'
383 ret = zblib_service_remove_service_interface(service,
386 Z_LOGE("Remove service interface failed!");
390 * Free Service interface object
392 zblib_service_interface_free(service,