sensord: clean up the code detected by style checker
[platform/core/system/sensord.git] / src / server / server.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 <systemd/sd-daemon.h>
21 #include <server.h>
22 #include <sensor_loader.h>
23 #include <command_common.h>
24 #include <command_worker.h>
25 #include <thread>
26 #include <sys/epoll.h>
27 #include <sensor_event_poller.h>
28
29 #define SYSTEMD_SOCKET_MAX    2
30
31 using std::thread;
32
33 server::server()
34 : m_mainloop(NULL)
35 {
36 }
37
38 server::~server()
39 {
40         stop();
41 }
42
43 int server::get_systemd_socket(const char *name)
44 {
45         int type = SOCK_STREAM;
46         int listening = 1;
47         size_t length = 0;
48         int fd = -1;
49         int fd_env = 1;
50         int fd_index;
51
52         if (!strcmp(name, EVENT_CHANNEL_PATH)) {
53                 type = SOCK_SEQPACKET;
54                 listening = -1;
55                 fd_env = 0;
56         }
57
58         if (sd_listen_fds(fd_env) < 0) {
59                 _E("Failed to listen fds from systemd");
60                 return -1;
61         }
62
63         for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) {
64                 fd = SD_LISTEN_FDS_START + fd_index;
65
66                 if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
67                         return fd;
68         }
69
70         return -1;
71 }
72
73 void server::accept_command_channel(void)
74 {
75         command_worker *cmd_worker;
76         _I("Command channel acceptor is started");
77
78         while (true) {
79                 csocket client_command_socket;
80
81                 if (!m_command_channel_accept_socket.accept(client_command_socket)) {
82                         _E("Failed to accept command channel from a client");
83                         continue;
84                 }
85
86                 _D("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
87
88                 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
89
90                 if (!cmd_worker) {
91                         _E("Failed to allocate memory");
92                         continue;
93                 }
94
95                 if (!cmd_worker->start())
96                         delete cmd_worker;
97         }
98 }
99
100 void server::accept_event_channel(void)
101 {
102         _I("Event channel acceptor is started");
103
104         while (true) {
105                 csocket client_event_socket;
106
107                 if (!m_event_channel_accept_socket.accept(client_event_socket)) {
108                         _E("Failed to accept event channel from a client");
109                         continue;
110                 }
111
112                 _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd());
113
114                 sensor_event_dispatcher::get_instance().accept_event_connections(client_event_socket);
115         }
116 }
117
118 void server::poll_event(void)
119 {
120         _I("Event poller is started");
121
122         sensor_event_poller poller;
123
124         if (!poller.poll()) {
125                 _E("Failed to poll event");
126                 return;
127         }
128 }
129
130 void server::run(void)
131 {
132         m_mainloop = g_main_loop_new(NULL, false);
133
134         sensor_event_dispatcher::get_instance().run();
135
136         listen_command_channel();
137         listen_event_channel();
138
139         thread event_channel_accepter(&server::accept_event_channel, this);
140         event_channel_accepter.detach();
141
142         thread command_channel_accepter(&server::accept_command_channel, this);
143         command_channel_accepter.detach();
144
145         thread event_poller(&server::poll_event, this);
146         event_poller.detach();
147
148         sd_notify(0, "READY=1");
149
150         g_main_loop_run(m_mainloop);
151         g_main_loop_unref(m_mainloop);
152
153         return;
154 }
155
156 bool server::listen_command_channel(void)
157 {
158         int sock_fd = -1;
159         const int MAX_PENDING_CONNECTION = 10;
160
161         sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
162
163         if (sock_fd >= 0) {
164                 _I("Succeeded to get systemd socket(%d)", sock_fd);
165                 m_command_channel_accept_socket = csocket(sock_fd);
166                 return true;
167         }
168
169         INFO("Failed to get systemd socket, create it by myself!");
170         if (!m_command_channel_accept_socket.create(SOCK_STREAM)) {
171                 ERR("Failed to create command channel");
172                 return false;
173         }
174
175         if (!m_command_channel_accept_socket.bind(COMMAND_CHANNEL_PATH)) {
176                 ERR("Failed to bind command channel");
177                 m_command_channel_accept_socket.close();
178                 return false;
179         }
180
181         if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
182                 ERR("Failed to listen command channel");
183                 return false;
184         }
185
186         return true;
187 }
188
189 bool server::listen_event_channel(void)
190 {
191         int sock_fd = -1;
192         const int MAX_PENDING_CONNECTION = 32;
193
194         sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH);
195
196         if (sock_fd >= 0) {
197                 _I("Succeeded to get systemd socket(%d)", sock_fd);
198                 m_event_channel_accept_socket = csocket(sock_fd);
199                 return true;
200         }
201
202         INFO("Failed to get systemd socket, create it by myself!");
203
204         if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) {
205                 ERR("Failed to create event channel");
206                 return false;
207         }
208
209         if (!m_event_channel_accept_socket.bind(EVENT_CHANNEL_PATH)) {
210                 ERR("Failed to bind event channel");
211                 m_event_channel_accept_socket.close();
212                 return false;
213         }
214
215         if (!m_event_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
216                 ERR("Failed to listen event channel");
217                 m_event_channel_accept_socket.close();
218                 return false;
219         }
220
221         return true;
222 }
223
224 void server::stop(void)
225 {
226         if (m_mainloop)
227                 g_main_loop_quit(m_mainloop);
228
229         m_command_channel_accept_socket.close();
230         m_event_channel_accept_socket.close();
231 }
232
233 server& server::get_instance()
234 {
235         static server inst;
236         return inst;
237 }