Change dbus function name
[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 <glib.h>
23 #include <sys/reboot.h>
24
25 #include <system_info.h>
26 #include <systemd/sd-daemon.h>
27 #include <libsyscommon/libgdbus.h>
28
29 #include "shared/log.h"
30 #include "shared/common.h"
31 #include "haptic.h"
32
33 #define VIBRATION_FEATURE              "http://tizen.org/feature/feedback.vibration"
34
35 static GMainLoop *mainloop = NULL;
36
37 static void sig_quit(int signo)
38 {
39         _D("Received SIGTERM signal(%d)", signo);
40 }
41
42 static void sig_usr1(int signo)
43 {
44         _D("Received SIGUSR1 signal(%d), feedbackd'll be finished.", signo);
45
46         if (mainloop) {
47                 if (g_main_loop_is_running(mainloop))
48                         g_main_loop_quit(mainloop);
49         }
50 }
51
52 static void dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
53 {
54         _I("sd_notify(READY=1)");
55         sd_notify(0, "READY=1");
56 }
57
58 int main(int argc, char **argv)
59 {
60         int ret;
61         dbus_handle_h handle = NULL;
62         bool haptic_avail = false;
63
64         ret = system_info_get_platform_bool(VIBRATION_FEATURE, &haptic_avail);
65         if ((ret < 0) || !haptic_avail) {
66                 _I("Vibration feature is not supported.");
67                 sd_notify(0, "READY=1");
68                 return 0;
69         }
70
71         mainloop = g_main_loop_new(NULL, FALSE);
72
73         handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, FALSE);
74         if (!handle)
75                 _E("Failed to get dbus connection.");;
76
77         ret = haptic_probe();
78         if (ret != 0) {
79                 _E("'Haptic' probe fail.");
80                 g_main_loop_unref(mainloop);
81                 return ret;
82         }
83         haptic_init();
84
85         ret = gdbus_request_name(handle, VIBRATOR_BUS_NAME, dbus_name_acquired, NULL);
86         if (ret <= 0) {
87                 _E("Failed to request bus name.");
88                 gdbus_check_name_owner(NULL, VIBRATOR_BUS_NAME);
89         }
90
91         signal(SIGTERM, sig_quit);
92         signal(SIGUSR1, sig_usr1);
93
94         /* g_main_loop */
95         g_main_loop_run(mainloop);
96
97         _D("'Haptic' deinitialize.");
98         haptic_exit();
99
100         if (mainloop) {
101                 g_main_loop_unref(mainloop);
102                 mainloop = NULL;
103         }
104
105         return 0;
106 }