#include <zblib_request.h>
#include <zblib_driver.h>
#include <zblib_driver_manager.h>
+#include <hal-zigbee.h>
struct _zblib_async_init_info {
gboolean is_initialized; /**< Does initialization succeeded? */
GMainLoop *main_loop; /**< Service main-loop */
GSList *interface_objs; /**< ZigBee Service interface objects */
- GSList *plugins; /**< ZigBee plug-ins */
+ ZigBeePlugin *plugin; /**< ZigBee plug-ins */
GHashTable *request_table; /**< Request Hash table */
guint request_id; /**< Request ID */
- GSList *async_initializer; /**< Hash table for asynchronous initializer */
+ struct _zblib_async_init_info *async_initializer; /**< Information for asynchronous initializer */
zblib_plugin_init_finished_cb callback; /**< Callback */
};
-static GSList *__zblib_service_ref_async_initializer(ZigBeeService* service)
-{
- zblib_check_null_ret_error("service", service, NULL);
-
- return service->async_initializer;
-}
-
-static void __zblib_service_set_async_initializer(ZigBeeService* service, GSList* list)
-{
- zblib_check_null_ret("service", service);
-
- service->async_initializer = list;
-}
-
static void __on_zblib_init_async_finished(gboolean result, void *user_data)
{
GSList *list_async = NULL;
struct _zblib_async_init_info *info = (struct _zblib_async_init_info*)user_data;
char *plugin_name = NULL;
- int i = 0;
- int _finished_count = 0;
- int _succeeded_count = 0;
-
zblib_check_null_ret("info", info);
plugin = info->plugin;
}
g_free(plugin_name);
- /* Check all async initializer list */
- list_async = __zblib_service_ref_async_initializer(service);
- while (list_async != NULL) {
- /* Initialize each plug-in */
- struct _zblib_async_init_info *in =
- (struct _zblib_async_init_info *)(list_async->data);
- i++;
- if (G_UNLIKELY(NULL == in)) {
- /* LCOV_EXCL_START */
- Z_LOGE("Invalid parameter !");
- list_async = g_slist_next(list_async);
- continue;
- /* LCOV_EXCL_STOP */
- }
-
- if (in->is_finished)
- _finished_count++;
- if (in->is_initialized)
- _succeeded_count++;
-
+ /* Check async initializer */
+ struct _zblib_async_init_info *in = service->async_initializer;
+ if (G_UNLIKELY(NULL == in)) {
+ /* LCOV_EXCL_START */
+ Z_LOGE("Invalid parameter !");
list_async = g_slist_next(list_async);
+ return;
+ /* LCOV_EXCL_STOP */
}
- if (i == _finished_count) {
+ if (in->is_finished) {
gboolean ret = TRUE;
guint driver_noti_id = (ZBLIB_DRIVER_TYPE_MANAGER << 24)
| ZBLIB_MANAGER_NOTI_ZIGBEE_ENABLED;
- if (_finished_count == _succeeded_count) {
- /* All initializer finished successfully */
- Z_LOGD("All async init finished !");
- ret = TRUE;
- } else {
- /* Some initializers are failed */
- /* LCOV_EXCL_START */
- Z_LOGE("There are some failed plugin initializer !");
- ret = FALSE;
- /* LCOV_EXCL_STOP */
- }
- /* Notify whether zigbee service is enabled or not */
zblib_plugin_send_notification(plugin,
driver_noti_id, &ret, sizeof(ret));
}
}
-static void *__zblib_service_load_plugin(gchar *filename,
- ZblibPluginDescriptor_t **descriptor_out)
-{
- ZblibPluginDescriptor_t *descriptor = NULL;
- void *handle = NULL;
- struct stat stat_buf;
- char file_date[27];
-
- zblib_check_null_ret_error("descriptor_out", descriptor_out, NULL);
-
- /* Open .so */
- handle = dlopen(filename, RTLD_LAZY);
- if (G_UNLIKELY(NULL == handle)) {
- /* LCOV_EXCL_START */
- Z_LOGE("dlopen() failed:[%s]", filename);
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- /* Get symbol - "zigbee_plugin_descriptor" */
- descriptor = dlsym(handle, "zigbee_plugin_descriptor");
- if (G_UNLIKELY(NULL == descriptor)) {
- /* LCOV_EXCL_START */
- Z_LOGE("dlsym() failed:[%s]", "plugin_define_desc");
- dlclose(handle);
- return NULL;
- /* LCOV_EXCL_STOP */
- }
-
- Z_LOGD("%s plugin", descriptor->name);
- Z_LOGD(" - path = %s", filename);
- Z_LOGD(" - version = %d", descriptor->version);
-
- memset(&stat_buf, 0x00, sizeof(stat_buf));
- memset(&file_date, '\0', sizeof(file_date));
-
- if (0 == stat(filename, &stat_buf)) {
- if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
- if (1 < strlen(file_date))
- file_date[strlen(file_date)-1] = '\0';
- Z_LOGD(" - date = %s", file_date);
- }
- }
-
- /* Load plug-in */
- if (G_LIKELY(descriptor->load)) {
- if (G_UNLIKELY(FALSE == descriptor->load())) {
- /* LCOV_EXCL_START */
- Z_LOGW("load() failed... Skip this plugin!");
- dlclose(handle);
- return NULL;
- /* LCOV_EXCL_STOP */
- } else {
- Z_LOGI("Plug-in (%s) loaded!", descriptor->name);
- }
- }
-
- *descriptor_out = descriptor;
-
- return handle;
-}
-
static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
{
const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
}
/* LCOV_EXCL_STOP */
-static void __zblib_service_remove_async_list(GSList *list_async)
-{
- GSList *iter = NULL;
- if (0 < g_slist_length(list_async)) {
- iter = g_slist_nth(list_async, 0);
- while (iter) {
- if (iter) {
- struct _zblib_async_init_info *async_plugin =
- (struct _zblib_async_init_info *)(list_async->data);
- list_async = g_slist_remove(list_async, async_plugin);
- }
- iter = g_slist_nth(list_async, 0);
- }
- }
-}
-
ZigBeeService *zblib_service_new()
{
ZigBeeService *service;
}
}
- /* Free async initializer */
- if (service->async_initializer) {
- g_slist_free(service->async_initializer);
- service->async_initializer = NULL;
- }
-
- /* Free plug-ins */
- if (service->plugins) {
- /* LCOV_EXCL_START */
- g_slist_free(service->plugins);
- service->plugins = NULL;
- /* LCOV_EXCL_STOP */
- }
-
/* Unref 'g-main loop' */
if (service->main_loop)
g_main_loop_unref(service->main_loop);
return TRUE;
}
-gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
+gboolean zblib_service_load_plugin(ZigBeeService *service)
{
- gchar *plugin_name;
-
- zblib_check_null_ret_error("service", service, FALSE);
- zblib_check_null_ret_error("plugin", plugin, FALSE);
-
- plugin_name = zblib_plugin_get_plugin_name(plugin);
-
- /* All plug-ins would be appended */
- service->plugins = g_slist_append(service->plugins, plugin);
- Z_LOGD("%s added", plugin_name);
- g_free(plugin_name);
-
- return TRUE;
-}
-
-gboolean zblib_service_remove_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
-{
- zblib_check_null_ret_error("service", service, FALSE);
- zblib_check_null_ret_error("plugin", plugin, FALSE);
-
- /* Specific vendor plug-in would be removed */
- service->plugins = g_slist_remove(service->plugins, plugin);
-
- /* Deinitialize plugin */
- zblib_plugin_free(plugin);
-
- return TRUE;
-}
-
-gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_path)
-{
- const gchar *file = NULL;
- gchar *filename = NULL;
- GDir *dir = NULL;
- void *handle = NULL;
ZblibPluginDescriptor_t *descriptor = NULL;
- ZigBeePlugin *plugin = NULL;
- gboolean ret;
zblib_check_null_ret_error("service", service, FALSE);
- zblib_check_null_ret_error("plugin_path", plugin_path, FALSE);
- /* Open plug-in directory */
- dir = g_dir_open(plugin_path, 0, NULL);
- if (G_UNLIKELY(dir == NULL)) {
- /* LCOV_EXCL_START */
- Z_LOGE("Directory open failed!");
- return FALSE;
- /* LCOV_EXCL_STOP */
- }
-
- /* Scan through all libraries in plug-in directory */
- while ((file = g_dir_read_name(dir)) != NULL) {
- if ((g_str_has_prefix(file, "lib") == TRUE)
- || (g_str_has_suffix(file, ".so") == FALSE))
- continue; // LCOV_EXCL_LINE
+ hal_zigbee_get_backend();
- filename = g_build_filename(plugin_path, file, NULL);
+ hal_zigbee_get_descriptor(&descriptor);
- /* Load plug-in */
- handle = __zblib_service_load_plugin(filename, &descriptor);
- if (G_UNLIKELY(NULL == handle)) {
- /* LCOV_EXCL_START */
- g_free(filename);
- continue;
- /* LCOV_EXCL_STOP */
- }
+ Z_LOGD("%s plugin", descriptor->name);
+ Z_LOGD(" - version = %d", descriptor->version);
- /* Create new plug-in */
- plugin = zblib_plugin_new(service, filename, descriptor, handle);
- if (G_UNLIKELY(NULL == plugin)) {
+ /* Load plug-in */
+ if (G_LIKELY(descriptor->load)) {
+ if (G_UNLIKELY(FALSE == descriptor->load())) {
/* LCOV_EXCL_START */
- dlclose(handle);
- g_free(filename);
- continue;
+ Z_LOGW("load() failed... Skip this plugin!");
+ return FALSE;
/* LCOV_EXCL_STOP */
+ } else {
+ Z_LOGI("Plug-in (%s) loaded!", descriptor->name);
}
+ }
- /* Add new plug-in */
- ret = zblib_service_add_plugin(service, plugin);
- if (G_UNLIKELY(FALSE == ret))
- zblib_plugin_free(plugin); // LCOV_EXCL_LINE
-
- g_free(filename);
+ /* Create new plug-in */
+ service->plugin = zblib_plugin_new(service, descriptor);
+ if (G_UNLIKELY(NULL == service->plugin)) {
+ /* LCOV_EXCL_START */
+ return FALSE;
+ /* LCOV_EXCL_STOP */
}
- g_dir_close(dir);
return TRUE;
}
-gboolean zblib_service_initialize_plugins(ZigBeeService *service)
+gboolean zblib_service_initialize_plugin(ZigBeeService *service)
{
- GSList *list;
- GSList *list_async;
-
zblib_check_null_ret_error("service", service, FALSE);
- /* Refer plug-in list */
- list = zblib_service_ref_plugins(service);
- list_async = __zblib_service_ref_async_initializer(service);
- while (list != NULL) {
- /* Initialize each plug-in */
- ZigBeePlugin *plugin = (ZigBeePlugin *)(list->data);
- if (FALSE == __zblib_service_init_plugin(plugin)) {
- /* If there is no initializer, it should have asynchronous one */
- const ZblibPluginDescriptor_t *descriptor =
- zblib_plugin_get_descriptor(plugin);
- if (NULL != descriptor && NULL != descriptor->init_async) {
- /* Register async initializer */
- struct _zblib_async_init_info *info =
- g_try_new0(struct _zblib_async_init_info, 1);
- if (NULL == info) {
- /* LCOV_EXCL_START */
- Z_LOGE("Fatal : Failed to allocate memory");
- return FALSE;
- /* LCOV_EXCL_STOP */
- }
- info->is_finished = FALSE;
- info->is_initialized = FALSE;
- info->plugin = plugin;
- info->user_data = NULL;
-
- list_async = g_slist_append(list_async, info);
- } else {
+ /* Initialize plug-in */
+ ZigBeePlugin *plugin = service->plugin;
+ if (FALSE == __zblib_service_init_plugin(plugin)) {
+ /* If there is no initializer, it should have asynchronous one */
+ const ZblibPluginDescriptor_t *descriptor =
+ zblib_plugin_get_descriptor(plugin);
+ if (NULL != descriptor && NULL != descriptor->init_async) {
+ /* Register async initializer */
+ struct _zblib_async_init_info *info =
+ g_try_new0(struct _zblib_async_init_info, 1);
+ if (NULL == info) {
/* LCOV_EXCL_START */
- __zblib_service_remove_async_list(list_async);
- Z_LOGE("Fatal : Failed to initialize plugin");
+ Z_LOGE("Fatal : Failed to allocate memory");
return FALSE;
/* LCOV_EXCL_STOP */
}
+ info->is_finished = FALSE;
+ info->is_initialized = FALSE;
+ info->plugin = plugin;
+ info->user_data = NULL;
+
+ service->async_initializer = info;
+ } else {
+ /* LCOV_EXCL_START */
+ service->async_initializer = NULL;
+ Z_LOGE("Fatal : Failed to initialize plugin");
+ return FALSE;
+ /* LCOV_EXCL_STOP */
}
- list = g_slist_next(list);
}
- __zblib_service_set_async_initializer(service, list_async);
-
return TRUE;
}
-gboolean zblib_service_initialize_async_plugins(ZigBeeService *service)
+gboolean zblib_service_initialize_async_plugin(ZigBeeService *service)
{
- GSList *list_async;
-
zblib_check_null_ret_error("service", service, FALSE);
- /* Refer async initializer list */
- list_async = __zblib_service_ref_async_initializer(service);
- while (list_async != NULL) {
- /* Initialize each plug-in */
- struct _zblib_async_init_info *info =
- (struct _zblib_async_init_info *)(list_async->data);
-
- if (G_UNLIKELY(NULL == info)) {
- /* LCOV_EXCL_START */
- Z_LOGE("Invalid parameter !");
- list_async = g_slist_next(list_async);
- continue;
- /* LCOV_EXCL_STOP */
- }
+ /* Initialize each plug-in */
+ struct _zblib_async_init_info *info = service->async_initializer;
+ if (G_UNLIKELY(NULL == info)) {
+ /* LCOV_EXCL_START */
+ Z_LOGE("Invalid parameter !");
+ return FALSE;
+ /* LCOV_EXCL_STOP */
+ }
- if (G_UNLIKELY(FALSE == __zblib_service_init_async_plugin(
- info->plugin, __on_zblib_init_async_finished, info))) {
- /* LCOV_EXCL_START */
- Z_LOGE("Fatal : Failed to initialize mandatory plugin");
- return FALSE;
- /* LCOV_EXCL_STOP */
- }
- list_async = g_slist_next(list_async);
+ if (G_UNLIKELY(FALSE == __zblib_service_init_async_plugin(
+ info->plugin, __on_zblib_init_async_finished, info))) {
+ /* LCOV_EXCL_START */
+ Z_LOGE("Fatal : Failed to initialize mandatory plugin");
+ return FALSE;
+ /* LCOV_EXCL_STOP */
}
return TRUE;
}
-gboolean zblib_service_unload_plugins(ZigBeeService *service)
+gboolean zblib_service_unload_plugin(ZigBeeService *service)
{
- GSList *list = NULL;
- GSList *list_async = NULL;
-
zblib_check_null_ret_error("service", service, FALSE);
- /* Unload everyy async_plugin */
- list_async = __zblib_service_ref_async_initializer(service);
- __zblib_service_remove_async_list(list_async);
-
- list = zblib_service_ref_plugins(service);
- while (list != NULL) {
- ZigBeePlugin *plugin = list->data;
-
- /* Unload each plug-in */
- if (G_UNLIKELY(FALSE == __zblib_service_unload_plugin(plugin))) {
- /* LCOV_EXCL_START */
- list = g_slist_next(list);
- continue;
- /* LCOV_EXCL_STOP */
- }
-
- list = g_slist_next(list);
- zblib_service_remove_plugin(service, plugin);
+ /* Unload each plug-in */
+ if (G_UNLIKELY(FALSE == __zblib_service_unload_plugin(service->plugin))) {
+ /* LCOV_EXCL_START */
+ return FALSE;
+ /* LCOV_EXCL_STOP */
}
- return TRUE;
-}
+ service->async_initializer = NULL;
+ service->plugin = NULL;
-GSList *zblib_service_ref_plugins(ZigBeeService *service)
-{
- zblib_check_null_ret_error("service", service, NULL);
+ hal_zigbee_put_backend();
- return service->plugins;
+ return TRUE;
}
gboolean zblib_service_add_service_interface(ZigBeeService *service,
gboolean zblib_service_dispatch_request(ZigBeeService *service,
guint request_id)
{
- ZigBeePlugin *plugin = NULL;
- GSList *plugin_list = NULL;
gboolean ret = FALSE;
zblib_check_null_ret_error("service", service, FALSE);
- /* Fetch plugin_list */
- plugin_list = service->plugins;
- zblib_check_null_ret_error("plugin_list", plugin_list, FALSE);
-
- while (plugin_list) {
- plugin = (ZigBeePlugin *)plugin_list->data;
-
- /* Dispatch request to plugin */
- ret = zblib_plugin_dispatch_request(plugin, request_id);
-
- /* Move to next plugin */
- plugin_list = g_slist_next(plugin_list);
- }
+ /* Dispatch request to plugin */
+ ret = zblib_plugin_dispatch_request(service->plugin, request_id);
return ret;
}