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
44 int server::get_systemd_socket(const char *name)
46 int type = SOCK_STREAM;
53 if (!strcmp(name, EVENT_CHANNEL_PATH)) {
54 type = SOCK_SEQPACKET;
59 if (sd_listen_fds(fd_env) < 0) {
60 _E("Failed to listen fds from systemd");
64 for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) {
65 fd = SD_LISTEN_FDS_START + fd_index;
67 if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
74 void server::accept_command_channel(void)
76 command_worker *cmd_worker;
77 _I("Command channel acceptor is started");
80 csocket client_command_socket;
82 if (!m_command_channel_accept_socket.accept(client_command_socket)) {
83 _E("Failed to accept command channel from a client");
87 _D("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
89 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
92 _E("Failed to allocate memory");
96 if (!cmd_worker->start())
101 void server::accept_event_channel(void)
103 _I("Event channel acceptor is started");
106 csocket client_event_socket;
108 if (!m_event_channel_accept_socket.accept(client_event_socket)) {
109 _E("Failed to accept event channel from a client");
113 _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd());
115 sensor_event_dispatcher::get_instance().accept_event_connections(client_event_socket);
119 void server::poll_event(void)
121 _I("Event poller is started");
123 sensor_event_poller poller;
125 if (!poller.poll()) {
126 _E("Failed to poll event");
131 void server::run(void)
133 m_mainloop = g_main_loop_new(NULL, false);
135 sensor_event_dispatcher::get_instance().run();
137 listen_command_channel();
138 listen_event_channel();
140 thread event_channel_accepter(&server::accept_event_channel, this);
141 event_channel_accepter.detach();
143 thread command_channel_accepter(&server::accept_command_channel, this);
144 command_channel_accepter.detach();
146 thread event_poller(&server::poll_event, this);
147 event_poller.detach();
149 sd_notify(0, "READY=1");
151 g_main_loop_run(m_mainloop);
152 g_main_loop_unref(m_mainloop);
157 bool server::listen_command_channel(void)
160 const int MAX_PENDING_CONNECTION = 10;
162 sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
165 _I("Succeeded to get systemd socket(%d)", sock_fd);
166 m_command_channel_accept_socket = csocket(sock_fd);
170 INFO("Failed to get systemd socket, create it by myself!");
171 if (!m_command_channel_accept_socket.create(SOCK_STREAM)) {
172 ERR("Failed to create command channel");
176 if (!m_command_channel_accept_socket.bind(COMMAND_CHANNEL_PATH)) {
177 ERR("Failed to bind command channel");
178 m_command_channel_accept_socket.close();
182 if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
183 ERR("Failed to listen command channel");
190 bool server::listen_event_channel(void)
193 const int MAX_PENDING_CONNECTION = 32;
195 sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH);
198 _I("Succeeded to get systemd socket(%d)", sock_fd);
199 m_event_channel_accept_socket = csocket(sock_fd);
203 INFO("Failed to get systemd socket, create it by myself!");
205 if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) {
206 ERR("Failed to create event channel");
210 if (!m_event_channel_accept_socket.bind(EVENT_CHANNEL_PATH)) {
211 ERR("Failed to bind event channel");
212 m_event_channel_accept_socket.close();
216 if (!m_event_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
217 ERR("Failed to listen event channel");
218 m_event_channel_accept_socket.close();
225 void server::stop(void)
228 g_main_loop_quit(m_mainloop);
230 m_command_channel_accept_socket.close();
231 m_event_channel_accept_socket.close();
234 server& server::get_instance()