Support asynchronous plugin initializer
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_service.c
index 681dc31..72e26e7 100644 (file)
 #include <zblib_plugin.h>
 #include <zblib_service_interface.h>
 #include <zblib_request.h>
+#include <zblib_driver.h>
+#include <zblib_driver_manager.h>
+
+struct _zblib_async_init_info {
+       gboolean is_initialized; /**< Does initialization succeeded? */
+       gboolean is_finished; /**< Does initialization finished? */
+
+       ZigBeePlugin *plugin; /**< ZigBee plugin */
+       void *user_data; /**< User data */
+};
 
 /**< ZigBee Service object */
 struct zblib_service_type {
@@ -34,8 +44,95 @@ struct zblib_service_type {
 
        GHashTable *request_table; /**< Request Hash table */
        guint request_id; /**< Request ID */
+
+       GSList *async_initializer; /**< Hash table 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;
+       ZigBeeService *service = NULL;
+       ZigBeePlugin *plugin = 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;
+       service = zblib_plugin_ref_service(plugin);
+       plugin_name = zblib_plugin_get_plugin_name(plugin);
+
+       zblib_check_null_ret("service", service);
+
+       info->is_finished = TRUE;
+       if (result) {
+               Z_LOGD("Plugin [%s] succeeded async init", plugin_name);
+               info->is_initialized = TRUE;
+       } else {
+               Z_LOGE("Plugin [%s] failed to init !", plugin_name);
+       }
+       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)) {
+                       Z_LOGE("Invalid parameter !");
+                       list_async = g_slist_next(list_async);
+                       continue;
+               }
+
+               if (in->is_finished)
+                       _finished_count++;
+               if (in->is_initialized)
+                       _succeeded_count++;
+
+               list_async = g_slist_next(list_async);
+       }
+
+       if (i == _finished_count) {
+               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 */
+                       Z_LOGE("There are some failed plugin initializer !");
+                       ret = FALSE;
+               }
+               /* 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)
 {
@@ -97,7 +194,10 @@ static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
        const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
 
        zblib_check_null_ret_error("descriptor", descriptor, FALSE);
-       zblib_check_null_ret_error("descriptor->init", descriptor->init, FALSE);
+       if (NULL == descriptor->init) {
+               Z_LOGD("descriptor->init is NULL, trying async init");
+               return FALSE;
+       }
 
        if (G_UNLIKELY(FALSE == descriptor->init(plugin))) {
                char *plugin_name = zblib_plugin_get_plugin_name(plugin);
@@ -111,6 +211,28 @@ static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
        return TRUE;
 }
 
+static gboolean __zblib_service_init_async_plugin(ZigBeePlugin *plugin,
+               zblib_plugin_init_finished_cb callback, void *user_data)
+{
+       const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
+       char *plugin_name = zblib_plugin_get_plugin_name(plugin);
+
+       zblib_check_null_ret_error("descriptor", descriptor, FALSE);
+       zblib_check_null_ret_error("descriptor->init_async", descriptor->init_async, FALSE);
+
+       if (G_UNLIKELY(NULL != plugin_name))
+               plugin_name = g_strdup("NONAME");
+
+       if (G_UNLIKELY(FALSE == descriptor->init_async(plugin, callback, user_data))) {
+               Z_LOGE("plugin(%s) init failed!", plugin_name);
+               g_free(plugin_name);
+               return FALSE;
+       }
+
+       g_free(plugin_name);
+       return TRUE;
+}
+
 static gboolean __zblib_service_unload_plugin(ZigBeePlugin *plugin)
 {
        const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
@@ -193,6 +315,12 @@ void zblib_service_free(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) {
                g_slist_free(service->plugins);
@@ -315,20 +443,74 @@ gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_p
 gboolean zblib_service_initialize_plugins(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 */
-               if (G_UNLIKELY(FALSE == __zblib_service_init_plugin((ZigBeePlugin *)(list->data)))) {
-                       list = g_slist_next(list);
-                       continue;
+               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->init_async) {
+                               /* Register async initializer */
+                               struct _zblib_async_init_info *info =
+                                       g_try_new0(struct _zblib_async_init_info, 1);
+                               if(NULL == info) {
+                                       Z_LOGE("Fatal : Failed to allocate memory");
+                                       return FALSE;
+                               }
+                               info->is_finished = FALSE;
+                               info->is_initialized = FALSE;
+                               info->plugin = plugin;
+                               info->user_data = NULL;
+
+                               list_async = g_slist_append(list_async, info);
+                       } else {
+                               Z_LOGE("Fatal : Failed to initialize plugin");
+                               return FALSE;
+                       }
                }
                list = g_slist_next(list);
        }
 
+       __zblib_service_set_async_initializer(service, list_async);
+
+       return TRUE;
+}
+
+gboolean zblib_service_initialize_async_plugins(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)) {
+                       Z_LOGE("Invalid parameter !");
+                       list_async = g_slist_next(list_async);
+                       continue;
+               }
+
+               if (G_UNLIKELY(FALSE == __zblib_service_init_async_plugin(
+                                       info->plugin, __on_zblib_init_async_finished, info))) {
+                       Z_LOGE("Fatal : Failed to initialize mandatory plugin");
+                       return FALSE;
+               }
+               list_async = g_slist_next(list_async);
+       }
+
        return TRUE;
 }