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.
23 #include <zblib_service.h>
24 #include <zblib_plugin.h>
25 #include <zblib_service_interface.h>
26 #include <zblib_request.h>
27 #include <zblib_driver.h>
28 #include <zblib_driver_manager.h>
30 struct _zblib_async_init_info {
31 gboolean is_initialized; /**< Does initialization succeeded? */
32 gboolean is_finished; /**< Does initialization finished? */
34 ZigBeePlugin *plugin; /**< ZigBee plugin */
35 void *user_data; /**< User data */
38 /**< ZigBee Service object */
39 struct zblib_service_type {
40 GMainLoop *main_loop; /**< Service main-loop */
42 GSList *interface_objs; /**< ZigBee Service interface objects */
43 GSList *plugins; /**< ZigBee plug-ins */
45 GHashTable *request_table; /**< Request Hash table */
46 guint request_id; /**< Request ID */
48 GSList *async_initializer; /**< Hash table for asynchronous initializer */
49 zblib_plugin_init_finished_cb callback; /**< Callback */
52 static GSList *__zblib_service_ref_async_initializer(ZigBeeService* service)
54 zblib_check_null_ret_error("service", service, NULL);
56 return service->async_initializer;
59 static void __zblib_service_set_async_initializer(ZigBeeService* service, GSList* list)
61 zblib_check_null_ret("service", service);
63 service->async_initializer = list;
66 static void __on_zblib_init_async_finished(gboolean result, void *user_data)
68 GSList *list_async = NULL;
69 ZigBeeService *service = NULL;
70 ZigBeePlugin *plugin = NULL;
71 struct _zblib_async_init_info *info = (struct _zblib_async_init_info*)user_data;
72 char *plugin_name = NULL;
75 int _finished_count = 0;
76 int _succeeded_count = 0;
78 zblib_check_null_ret("info", info);
80 plugin = info->plugin;
81 service = zblib_plugin_ref_service(plugin);
82 zblib_check_null_ret("service", service);
84 plugin_name = zblib_plugin_get_plugin_name(plugin);
86 info->is_finished = TRUE;
88 Z_LOGD("Plugin [%s] succeeded async init", plugin_name);
89 info->is_initialized = TRUE;
91 Z_LOGE("Plugin [%s] failed to init !", plugin_name);
95 /* Check all async initializer list */
96 list_async = __zblib_service_ref_async_initializer(service);
97 while (list_async != NULL) {
98 /* Initialize each plug-in */
99 struct _zblib_async_init_info *in =
100 (struct _zblib_async_init_info *)(list_async->data);
102 if (G_UNLIKELY(NULL == in)) {
103 Z_LOGE("Invalid parameter !");
104 list_async = g_slist_next(list_async);
110 if (in->is_initialized)
113 list_async = g_slist_next(list_async);
116 if (i == _finished_count) {
118 guint driver_noti_id = (ZBLIB_DRIVER_TYPE_MANAGER << 24)
119 | ZBLIB_MANAGER_NOTI_ZIGBEE_ENABLED;
121 if (_finished_count == _succeeded_count) {
122 /* All initializer finished successfully */
123 Z_LOGD("All async init finished !");
126 /* Some initializers are failed */
127 Z_LOGE("There are some failed plugin initializer !");
130 /* Notify whether zigbee service is enabled or not */
131 zblib_plugin_send_notification(plugin,
132 driver_noti_id, &ret, sizeof(ret));
136 static void *__zblib_service_load_plugin(gchar *filename,
137 ZblibPluginDescriptor_t **descriptor_out)
139 ZblibPluginDescriptor_t *descriptor = NULL;
141 struct stat stat_buf;
144 zblib_check_null_ret_error("descriptor_out", descriptor_out, NULL);
147 handle = dlopen(filename, RTLD_LAZY);
148 if (G_UNLIKELY(NULL == handle)) {
149 Z_LOGE("dlopen() failed:[%s]", filename);
153 /* Get symbol - "zigbee_plugin_descriptor" */
154 descriptor = dlsym(handle, "zigbee_plugin_descriptor");
155 if (G_UNLIKELY(NULL == descriptor)) {
156 Z_LOGE("dlsym() failed:[%s]", "plugin_define_desc");
161 Z_LOGD("%s plugin", descriptor->name);
162 Z_LOGD(" - path = %s", filename);
163 Z_LOGD(" - version = %d", descriptor->version);
165 memset(&stat_buf, 0x00, sizeof(stat_buf));
166 memset(&file_date, '\0', sizeof(file_date));
168 if (0 == stat(filename, &stat_buf)) {
169 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
170 if (1 < strlen(file_date))
171 file_date[strlen(file_date)-1] = '\0';
172 Z_LOGD(" - date = %s", file_date);
177 if (G_LIKELY(descriptor->load)) {
178 if (G_UNLIKELY(FALSE == descriptor->load())) {
179 Z_LOGW("load() failed... Skip this plugin!");
183 Z_LOGI("Plug-in (%s) loaded!", descriptor->name);
187 *descriptor_out = descriptor;
192 static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
194 const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
196 zblib_check_null_ret_error("descriptor", descriptor, FALSE);
197 if (NULL == descriptor->init) {
198 Z_LOGD("descriptor->init is NULL, trying async init");
202 if (G_UNLIKELY(FALSE == descriptor->init(plugin))) {
203 char *plugin_name = zblib_plugin_get_plugin_name(plugin);
204 if (G_UNLIKELY(NULL != plugin_name)) {
205 Z_LOGE("plugin(%s) init failed!", plugin_name);
214 static gboolean __zblib_service_init_async_plugin(ZigBeePlugin *plugin,
215 zblib_plugin_init_finished_cb callback, void *user_data)
217 const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
218 char *plugin_name = NULL;
220 zblib_check_null_ret_error("descriptor", descriptor, FALSE);
221 zblib_check_null_ret_error("descriptor->init_async", descriptor->init_async, FALSE);
223 plugin_name = zblib_plugin_get_plugin_name(plugin);
224 if (G_UNLIKELY(NULL != plugin_name))
225 plugin_name = g_strdup("NONAME");
227 if (G_UNLIKELY(FALSE == descriptor->init_async(plugin, callback, user_data))) {
228 Z_LOGE("plugin(%s) init failed!", plugin_name);
237 static gboolean __zblib_service_unload_plugin(ZigBeePlugin *plugin)
239 const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
240 char *plugin_name = NULL;
242 zblib_check_null_ret_error("descriptor", descriptor, FALSE);
243 zblib_check_null_ret_error("descriptor->unload", descriptor->unload, FALSE);
245 plugin_name = zblib_plugin_get_plugin_name(plugin);
247 descriptor->unload(plugin);
248 Z_LOGI("plugin(%s) unloaded!", plugin_name);
255 static void __zblib_service_remove_request_table_iter(gpointer key,
256 gpointer value, gpointer user_data)
258 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
259 gint request_id = GPOINTER_TO_INT(key);
263 zblib_check_null_ret("service_interface", service_interface);
265 Z_LOGD("Removing request id [%d]", request_id);
266 zblib_request_free(service_interface, request_id);
269 ZigBeeService *zblib_service_new()
271 ZigBeeService *service;
273 service = g_malloc0(sizeof(struct zblib_service_type));
275 /* Create g-main loop */
276 service->main_loop = g_main_loop_new(NULL, FALSE);
277 if (G_UNLIKELY(NULL == service->main_loop)) {
278 Z_LOGE("g-main loop creation failed!!!");
283 /* Create request hash table */
284 service->request_table = g_hash_table_new_full(g_direct_hash,
285 g_direct_equal, NULL, NULL);
290 void zblib_service_free(ZigBeeService *service)
292 zblib_check_null_ret("service", service);
294 if (service->request_table) {
295 GSList *interface_objs = NULL;
296 ZigBeeServiceInterface *service_interface = NULL;
298 interface_objs = service->interface_objs;
299 if (NULL == interface_objs) {
300 Z_LOGE("interface_objs is NULL");
302 while (interface_objs) {
303 service_interface = (ZigBeeServiceInterface *)interface_objs->data;
305 /* Remove left request */
306 g_hash_table_foreach(service->request_table,
307 __zblib_service_remove_request_table_iter,
310 /* Move to next service interface */
311 interface_objs = g_slist_next(interface_objs);
313 g_hash_table_remove_all(service->request_table);
314 g_hash_table_destroy(service->request_table);
315 service->request_table = NULL;
319 /* Free async initializer */
320 if (service->async_initializer) {
321 g_slist_free(service->async_initializer);
322 service->async_initializer = NULL;
326 if (service->plugins) {
327 g_slist_free(service->plugins);
328 service->plugins = NULL;
331 /* Unref 'g-main loop' */
332 if (service->main_loop)
333 g_main_loop_unref(service->main_loop);
338 gboolean zblib_service_run(ZigBeeService *service)
340 zblib_check_null_ret_error("service", service, FALSE);
341 zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
343 g_main_loop_run(service->main_loop);
348 gboolean zblib_service_exit(ZigBeeService *service)
350 zblib_check_null_ret_error("service", service, FALSE);
351 zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
353 g_main_loop_quit(service->main_loop);
358 gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
362 zblib_check_null_ret_error("service", service, FALSE);
363 zblib_check_null_ret_error("plugin", plugin, FALSE);
365 plugin_name = zblib_plugin_get_plugin_name(plugin);
367 /* All plug-ins would be appended */
368 service->plugins = g_slist_append(service->plugins, plugin);
369 Z_LOGD("%s added", plugin_name);
375 gboolean zblib_service_remove_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
377 zblib_check_null_ret_error("service", service, FALSE);
378 zblib_check_null_ret_error("plugin", plugin, FALSE);
380 /* Specific vendor plug-in would be removed */
381 service->plugins = g_slist_remove(service->plugins, plugin);
383 /* Deinitialize plugin */
384 zblib_plugin_free(plugin);
389 gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_path)
391 const gchar *file = NULL;
392 gchar *filename = NULL;
395 ZblibPluginDescriptor_t *descriptor = NULL;
396 ZigBeePlugin *plugin = NULL;
399 zblib_check_null_ret_error("service", service, FALSE);
400 zblib_check_null_ret_error("plugin_path", plugin_path, FALSE);
402 /* Open plug-in directory */
403 dir = g_dir_open(plugin_path, 0, NULL);
404 if (G_UNLIKELY(dir == NULL)) {
405 Z_LOGE("Directory open failed!");
409 /* Scan through all libraries in plug-in directory */
410 while ((file = g_dir_read_name(dir)) != NULL) {
411 if ((g_str_has_prefix(file, "lib") == TRUE)
412 || (g_str_has_suffix(file, ".so") == FALSE))
415 filename = g_build_filename(plugin_path, file, NULL);
418 handle = __zblib_service_load_plugin(filename, &descriptor);
419 if (G_UNLIKELY(NULL == handle)) {
424 /* Create new plug-in */
425 plugin = zblib_plugin_new(service, filename, descriptor, handle);
426 if (G_UNLIKELY(NULL == plugin)) {
431 /* Add new plug-in */
432 ret = zblib_service_add_plugin(service, plugin);
433 if (G_UNLIKELY(FALSE == ret))
434 zblib_plugin_free(plugin);
443 gboolean zblib_service_initialize_plugins(ZigBeeService *service)
448 zblib_check_null_ret_error("service", service, FALSE);
450 /* Refer plug-in list */
451 list = zblib_service_ref_plugins(service);
452 list_async = __zblib_service_ref_async_initializer(service);
453 while (list != NULL) {
454 /* Initialize each plug-in */
455 ZigBeePlugin *plugin = (ZigBeePlugin *)(list->data);
456 if (FALSE == __zblib_service_init_plugin(plugin)) {
457 /* If there is no initializer, it should have asynchronous one */
458 const ZblibPluginDescriptor_t *descriptor =
459 zblib_plugin_get_descriptor(plugin);
460 if (NULL != descriptor->init_async) {
461 /* Register async initializer */
462 struct _zblib_async_init_info *info =
463 g_try_new0(struct _zblib_async_init_info, 1);
465 Z_LOGE("Fatal : Failed to allocate memory");
468 info->is_finished = FALSE;
469 info->is_initialized = FALSE;
470 info->plugin = plugin;
471 info->user_data = NULL;
473 list_async = g_slist_append(list_async, info);
475 Z_LOGE("Fatal : Failed to initialize plugin");
479 list = g_slist_next(list);
482 __zblib_service_set_async_initializer(service, list_async);
487 gboolean zblib_service_initialize_async_plugins(ZigBeeService *service)
491 zblib_check_null_ret_error("service", service, FALSE);
493 /* Refer async initializer list */
494 list_async = __zblib_service_ref_async_initializer(service);
495 while (list_async != NULL) {
496 /* Initialize each plug-in */
497 struct _zblib_async_init_info *info =
498 (struct _zblib_async_init_info *)(list_async->data);
500 if (G_UNLIKELY(NULL == info)) {
501 Z_LOGE("Invalid parameter !");
502 list_async = g_slist_next(list_async);
506 if (G_UNLIKELY(FALSE == __zblib_service_init_async_plugin(
507 info->plugin, __on_zblib_init_async_finished, info))) {
508 Z_LOGE("Fatal : Failed to initialize mandatory plugin");
511 list_async = g_slist_next(list_async);
517 gboolean zblib_service_unload_plugins(ZigBeeService *service)
521 zblib_check_null_ret_error("service", service, FALSE);
523 list = zblib_service_ref_plugins(service);
524 while (list != NULL) {
525 ZigBeePlugin *plugin = list->data;
527 /* Unload each plug-in */
528 if (G_UNLIKELY(FALSE == __zblib_service_unload_plugin(plugin))) {
529 list = g_slist_next(list);
533 list = g_slist_next(list);
534 zblib_service_remove_plugin(service, plugin);
540 GSList *zblib_service_ref_plugins(ZigBeeService *service)
542 zblib_check_null_ret_error("service", service, NULL);
544 return service->plugins;
547 gboolean zblib_service_add_service_interface(ZigBeeService *service,
548 ZigBeeServiceInterface *service_interface)
552 zblib_check_null_ret_error("service", service, FALSE);
553 zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
555 object_name = zblib_service_interface_get_name(service_interface);
558 * All service interface objects would be appended
560 service->interface_objs = g_slist_append(service->interface_objs, service_interface);
561 Z_LOGD("%s added", object_name);
567 gboolean zblib_service_remove_service_interface(ZigBeeService *service,
568 ZigBeeServiceInterface *service_interface)
570 zblib_check_null_ret_error("service", service, FALSE);
571 zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
574 * service interface object would be removed
576 service->interface_objs = g_slist_remove(service->interface_objs, service_interface);
581 ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *service,
582 const gchar *service_interface_name)
584 ZigBeeServiceInterface *service_interface = NULL;
585 ZigBeeServiceInterface *tmp_service_interface = NULL;
590 zblib_check_null_ret_error("service", service, NULL);
591 zblib_check_null_ret_error("service_interface_name", service_interface_name, NULL);
593 list = service->interface_objs;
595 tmp_service_interface = (ZigBeeServiceInterface *)list->data;
597 /* Get object name of service_interface */
598 object_name = zblib_service_interface_get_name(tmp_service_interface);
599 if (g_strcmp0(service_interface_name, object_name) == 0) {
602 service_interface = tmp_service_interface;
607 list = g_slist_next(list);
610 if (NULL == service_interface) {
611 Z_LOGE("Service interface object of name '%s' not found!",
612 service_interface_name);
616 Z_LOGD("'%s' found", service_interface_name);
618 return service_interface;
621 GHashTable *zblib_service_ref_request_hash_table(ZigBeeService *service)
623 zblib_check_null_ret_error("service", service, NULL);
625 return service->request_table;
628 gint zblib_service_generate_request_id(ZigBeeService *service)
630 zblib_check_null_ret_error("service", service, -1);
632 /* Increment request ID */
633 service->request_id++;
635 return (gint)service->request_id;
638 gboolean zblib_service_dispatch_request(ZigBeeService *service,
641 ZigBeePlugin *plugin = NULL;
642 GSList *plugin_list = NULL;
643 gboolean ret = FALSE;
645 zblib_check_null_ret_error("service", service, FALSE);
647 /* Fetch plugin_list */
648 plugin_list = service->plugins;
649 zblib_check_null_ret_error("plugin_list", plugin_list, FALSE);
651 while (plugin_list) {
652 plugin = (ZigBeePlugin *)plugin_list->data;
654 /* Dispatch request to plugin */
655 ret = zblib_plugin_dispatch_request(plugin, request_id);
657 /* Move to next plugin */
658 plugin_list = g_slist_next(plugin_list);
664 void zblib_service_send_response(ZigBeeService *service,
665 guint request_id, gpointer resp_data, guint resp_data_len)
667 ZigBeeServiceInterface *service_interface = NULL;
669 zblib_check_null_ret("service", service);
671 service_interface = zblib_request_ref_service_interface(service, request_id);
672 zblib_check_null_ret("service_interface", service_interface);
674 /* Send response to service interface */
675 zblib_service_interface_send_response(service_interface,
676 request_id, resp_data, resp_data_len);
679 void zblib_service_send_notification(ZigBeeService *service,
680 guint noti_id, gpointer noti_data, guint noti_data_len)
682 GSList *interface_objs = NULL;
683 ZigBeeServiceInterface *service_interface = NULL;
685 zblib_check_null_ret("service", service);
687 interface_objs = service->interface_objs;
688 zblib_check_null_ret("interface_objs", interface_objs);
690 while (interface_objs) {
691 service_interface = (ZigBeeServiceInterface *)interface_objs->data;
693 /* Send notification to service interface */
694 zblib_service_interface_send_notification(service_interface,
695 noti_id, noti_data, noti_data_len);
697 /* Move to next service interface */
698 interface_objs = g_slist_next(interface_objs);