e3e9f7367862a390cdfe4e35fd2fc10ad7568f49
[platform/core/system/sensord.git] / src / server / main.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <signal.h>
21 #include <sensor_log.h>
22 #include <server.h>
23 #include <dbus_util.h>
24 #include <sensor_loader.h>
25 #include <string>
26
27 #define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data"
28 #define SET_CAL 1
29
30 #define TIMEOUT 10
31
32 static void sig_term_handler(int signo, siginfo_t *info, void *data)
33 {
34         char proc_name[NAME_MAX];
35
36         get_proc_name(info->si_pid, proc_name);
37
38         _E("Received SIGTERM(%d) from %s(%d)\n", signo, proc_name, info->si_pid);
39
40         server::get_instance().stop();
41 }
42
43 static void signal_init(void)
44 {
45         struct sigaction sig_act;
46         memset(&sig_act, 0, sizeof(struct sigaction));
47
48         sig_act.sa_handler = SIG_IGN;
49         sigaction(SIGCHLD, &sig_act, NULL);
50         sigaction(SIGPIPE, &sig_act, NULL);
51
52         sig_act.sa_handler = NULL;
53         sig_act.sa_sigaction = sig_term_handler;
54         sig_act.sa_flags = SA_SIGINFO;
55         sigaction(SIGTERM, &sig_act, NULL);
56         sigaction(SIGABRT, &sig_act, NULL);
57         sigaction(SIGINT, &sig_act, NULL);
58 }
59
60 static void set_cal_data(void)
61 {
62         FILE *fp = fopen(CAL_NODE_PATH, "w");
63
64         if (!fp) {
65                 _I("Not support calibration_node");
66                 return;
67         }
68
69         fprintf(fp, "%d", SET_CAL);
70
71         if (fp)
72                 fclose(fp);
73
74         _I("Succeeded to set calibration data");
75
76         return;
77 }
78
79 static gboolean terminate(gpointer data)
80 {
81         std::vector<sensor_base *> sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
82
83         if (sensors.size() == 0) {
84                 _I("Terminating sensord..");
85                 server::get_instance().stop();
86         }
87
88         return FALSE;
89 }
90
91 int main(int argc, char *argv[])
92 {
93         _I("Sensord started");
94
95         signal_init();
96
97         init_dbus();
98
99         set_cal_data();
100
101         /* TODO: loading sequence has to be moved to server */
102         sensor_loader::get_instance().load();
103         g_timeout_add_seconds(TIMEOUT, terminate, NULL);
104
105         server::get_instance().run();
106         server::get_instance().stop();
107
108         fini_dbus();
109
110         _I("Sensord terminated");
111         return 0;
112 }