4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <systemd/sd-daemon.h>
22 #include <sensor_loader.h>
23 #include <command_common.h>
24 #include <command_worker.h>
26 #include <sys/epoll.h>
27 #include <sensor_event_poller.h>
29 #define SYSTEMD_SOCKET_MAX 2
43 int server::get_systemd_socket(const char *name)
45 int type = SOCK_STREAM;
52 if (!strcmp(name, EVENT_CHANNEL_PATH)) {
53 type = SOCK_SEQPACKET;
58 if (sd_listen_fds(fd_env) < 0) {
59 _E("Failed to listen fds from systemd");
63 for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) {
64 fd = SD_LISTEN_FDS_START + fd_index;
66 if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
73 void server::accept_command_channel(void)
75 command_worker *cmd_worker;
76 _I("Command channel acceptor is started");
79 csocket client_command_socket;
81 if (!m_command_channel_accept_socket.accept(client_command_socket)) {
82 _E("Failed to accept command channel from a client");
86 _D("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
88 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
91 _E("Failed to allocate memory");
95 if (!cmd_worker->start())
100 void server::accept_event_channel(void)
102 _I("Event channel acceptor is started");
105 csocket client_event_socket;
107 if (!m_event_channel_accept_socket.accept(client_event_socket)) {
108 _E("Failed to accept event channel from a client");
112 _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd());
114 sensor_event_dispatcher::get_instance().accept_event_connections(client_event_socket);
118 void server::poll_event(void)
120 _I("Event poller is started");
122 sensor_event_poller poller;
124 if (!poller.poll()) {
125 _E("Failed to poll event");
130 void server::run(void)
132 m_mainloop = g_main_loop_new(NULL, false);
134 sensor_event_dispatcher::get_instance().run();
136 listen_command_channel();
137 listen_event_channel();
139 thread event_channel_accepter(&server::accept_event_channel, this);
140 event_channel_accepter.detach();
142 thread command_channel_accepter(&server::accept_command_channel, this);
143 command_channel_accepter.detach();
145 thread event_poller(&server::poll_event, this);
146 event_poller.detach();
148 sd_notify(0, "READY=1");
150 g_main_loop_run(m_mainloop);
151 g_main_loop_unref(m_mainloop);
156 bool server::listen_command_channel(void)
159 const int MAX_PENDING_CONNECTION = 10;
161 sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
164 _I("Succeeded to get systemd socket(%d)", sock_fd);
165 m_command_channel_accept_socket = csocket(sock_fd);
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");
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();
181 if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
182 ERR("Failed to listen command channel");
189 bool server::listen_event_channel(void)
192 const int MAX_PENDING_CONNECTION = 32;
194 sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH);
197 _I("Succeeded to get systemd socket(%d)", sock_fd);
198 m_event_channel_accept_socket = csocket(sock_fd);
202 INFO("Failed to get systemd socket, create it by myself!");
204 if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) {
205 ERR("Failed to create event channel");
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();
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();
224 void server::stop(void)
227 g_main_loop_quit(m_mainloop);
229 m_command_channel_accept_socket.close();
230 m_event_channel_accept_socket.close();
233 server& server::get_instance()