3134e6322fc4ccb8ad536f85f6392a4af8013e06
[platform/core/system/feedbackd.git] / src / core / main.c
1 /*
2  * feedbackd
3  *
4  * Copyright (c) 2016 - 2017 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
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/reboot.h>
23 #include <glib.h>
24 #include <libsyscommon/dbus-system.h>
25 #include <systemd/sd-daemon.h>
26
27 #include "core/log.h"
28 #include "core/common.h"
29 #include "haptic/haptic.h"
30
31 static GMainLoop *mainloop = NULL;
32
33 static void sig_quit(int signo)
34 {
35         _D("Received SIGTERM signal(%d)", signo);
36 }
37
38 static void sig_usr1(int signo)
39 {
40         _D("Received SIGUSR1 signal(%d), feedbackd'll be finished.", signo);
41
42         if (mainloop) {
43                 if (g_main_loop_is_running(mainloop))
44                         g_main_loop_quit(mainloop);
45                 mainloop = NULL;
46         }
47 }
48
49 static void dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
50 {
51         _I("sd_notify(READY=1)");
52         sd_notify(0, "READY=1");
53 }
54
55 int main(int argc, char **argv)
56 {
57         int ret;
58         dbus_handle_h handle = NULL;
59
60         mainloop = g_main_loop_new(NULL, FALSE);
61
62         handle = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, FALSE);
63         if (!handle)
64                 _E("Failed to get dbus connection.");;
65
66         ret = haptic_probe();
67         if (ret != 0) {
68                 _E("'Haptic' probe fail.");
69                 return ret;
70         }
71         haptic_init();
72
73         ret = dbus_handle_request_bus_name(handle, VIBRATOR_BUS_NAME, dbus_name_acquired, NULL);
74         if (ret <= 0) {
75                 _E("Failed to request bus name.");
76                 dbus_handle_check_owner_name(NULL, VIBRATOR_BUS_NAME);
77         }
78
79         signal(SIGTERM, sig_quit);
80         signal(SIGUSR1, sig_usr1);
81
82         /* g_main_loop */
83         g_main_loop_run(mainloop);
84
85         _D("'Haptic' deinitialize.");
86         haptic_exit();
87         g_main_loop_unref(mainloop);
88         mainloop = NULL;
89
90         return 0;
91 }