a819fed6feb3b87641c1651fc7ebe8b9c42d0bcc
[platform/core/system/deviced.git] / src / power / boot.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <bundle.h>
22 #include <eventsystem.h>
23 #include <libsyscommon/libgdbus.h>
24 #include <libsyscommon/libsystemd.h>
25
26 #include "core/log.h"
27 #include "shared/device-notifier.h"
28 #include "shared/common.h"
29 #include "display/poll.h"
30 #include "display/display-ops.h"
31 #include "shared/plugin.h"
32 #include "doze.h"
33
34 #define SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED     "StartupFinished"
35 #define SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED       "UserSessionStartupFinished"
36
37 static struct display_plugin *disp_plgn;
38 static guint sig_id[2] = {0, 0};
39
40 void remove_delayed_init_done_handler(void *data)
41 {
42         gdbus_signal_unsubscribe(NULL, sig_id[0]);
43         gdbus_signal_unsubscribe(NULL, sig_id[1]);
44 }
45
46 static void delayed_init_done_received(GDBusConnection  *conn,
47         const gchar      *sender,
48         const gchar      *path,
49         const gchar      *iface,
50         const gchar      *name,
51         GVariant         *param,
52         gpointer          data)
53 {
54         static int system_done = 0;
55         static int user_done = 0;
56
57         if (strcmp(name, SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED) == 0) {
58                 if (system_done)
59                         return;
60
61                 system_done = check_system_boot_finished();
62                 if (system_done == 0) {
63                         _E("System session is not ready yet.");
64                         return;
65                 }
66                 CRITICAL_LOG("System session is ready.");
67                 device_notify_once(DEVICE_NOTIFIER_DELAYED_INIT, &system_done);
68
69         } else if (strcmp(name, SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED) == 0) {
70                 if (user_done)
71                         return;
72                 user_done = 1;
73                 _I("User session is ready.");
74         }
75
76         if (!system_done || !user_done)
77                 return;
78
79         remove_delayed_init_done_handler(NULL);
80
81         _I("Real booting done. Unlock LCD_OFF.");
82         if (disp_plgn->pm_unlock_internal)
83                 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
84
85         _I("Signal booting done.");
86
87         doze_init();
88 }
89
90 void add_delayed_init_done_handler(void *data)
91 {
92         /* System Session is loaded completely */
93         /*ret = */
94         sig_id[0] = gdbus_signal_subscribe(NULL,
95                                         SYSTEMD_DBUS_PATH,
96                                         SYSTEMD_DBUS_IFACE_MANAGER,
97                                         SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED,
98                                         delayed_init_done_received,
99                                         NULL, NULL);
100
101         if (sig_id[0] <= 0)
102                 _E("Failed to init dbus signal(%s).", SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED);
103
104         /* User Session is loaded completely */
105         sig_id[1] = gdbus_signal_subscribe(NULL,
106                                         SYSTEMD_DBUS_PATH,
107                                         SYSTEMD_DBUS_IFACE_MANAGER,
108                                         SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED,
109                                         delayed_init_done_received,
110                                         NULL, NULL);
111
112         if (sig_id[1] <= 0)
113                 _E("Failed to init dbus signal(%s).", SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED);
114 }
115
116 static void __CONSTRUCTOR__ initialize(void)
117 {
118         disp_plgn = get_var_display_plugin();
119         if (!disp_plgn)
120                 _E("Failed to get display plugin variable.");
121 }