extcon: register dbus object on init 12/264912/3
authorYoungjae Cho <y0.cho@samsung.com>
Tue, 5 Oct 2021 04:30:19 +0000 (13:30 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Tue, 5 Oct 2021 05:53:01 +0000 (14:53 +0900)
Change-Id: Ia20bb373f1f20fb1b50d1957636d7f9625b5f07b
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/extcon/extcon.c

index 74dae19..46deb7e 100644 (file)
@@ -40,6 +40,9 @@
 static GList *extcon_list;
 static bool extcon_dev_available = false;
 
+static void extcon_deferred_init(void);
+static int booting_done(void *data);
+
 void add_extcon(struct extcon_ops *dev)
 {
        SYS_G_LIST_APPEND(extcon_list, dev);
@@ -303,6 +306,9 @@ static GVariant * dbus_get_extcon_status(GDBusConnection *conn,
        char *str;
        int ret;
 
+       if (!booting_done(NULL))
+               extcon_deferred_init();
+
        g_variant_get(param, "(s)", &str);
 
        dev = find_extcon(str);
@@ -327,6 +333,9 @@ static GVariant *dbus_enable_device(GDBusConnection *conn,
        char *device;
        int ret;
 
+       if (!booting_done(NULL))
+               extcon_deferred_init();
+
        g_variant_get(param, "(s)", &device);
 
        ret = extcon_update(device, NULL, "1");
@@ -342,6 +351,9 @@ static GVariant *dbus_disable_device(GDBusConnection *conn,
        char *device;
        int ret;
 
+       if (!booting_done(NULL))
+               extcon_deferred_init();
+
        g_variant_get(param, "(s)", &device);
 
        ret = extcon_update(device, NULL, "0");
@@ -466,44 +478,60 @@ static int event_handler_state_changed(void *data)
        return 0;
 }
 
-static int booting_done(void *data)
+/* defer extcon init until booting done as it takes long time.
+ * if dbus request is arrived before the booting done,
+ * initialize extcon on arriving that request even though
+ * system booting has not been finished */
+static void extcon_deferred_init(void)
 {
-       static int done;
-       int ret_dbus;
        GList *l;
        struct extcon_ops *dev;
        device_notifier_state_e state = DEVICE_NOTIFIER_STATE_START;
+       static int initialized = false;
 
-       if (!data)
-               return done;
-       done = *(int *) data;
-       if (!done)
-               return done;
-
-       if (!extcon_list)
-               return done;
+       if (initialized)
+               return;
 
        /* initialize extcon devices */
        SYS_G_LIST_FOREACH(extcon_list, l, dev) {
                _I("Extcon(%s) init.", dev->name);
                if (dev->init)
-                       dev->init(data);
+                       dev->init(NULL);
                dev->enabled = true;
        }
 
        event_handler_state_changed((void *)&state);
+       register_notifier(DEVICE_NOTIFIER_EVENT_HANDLER, event_handler_state_changed);
 
-       ret_dbus = gdbus_add_object(NULL, DEVICED_PATH_EXTCON, &dbus_interface);
-       if (ret_dbus < 0)
-               _E("Failed to init dbus method: %d", ret_dbus);
+       initialized = true;
+}
 
-       register_notifier(DEVICE_NOTIFIER_EVENT_HANDLER, event_handler_state_changed);
+static int booting_done(void *data)
+{
+       static int done;
+
+       if (!data)
+               return done;
+       done = *(int *) data;
+       if (!done)
+               return done;
+
+       if (!extcon_list)
+               return done;
+
+       extcon_deferred_init();
 
        return done;
 }
 
 static void extcon_init(void *data)
 {
+       int retval;
+
+       retval = gdbus_add_object(NULL, DEVICED_PATH_EXTCON, &dbus_interface);
+       if (retval < 0)
+               _E("Failed to init dbus method: %d", retval);
+
        register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
 }