4 * Copyright (c) 2017 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.
23 #include <systemd/sd-daemon.h>
24 #include <sensor_log.h>
25 #include <command_types.h>
26 #include <ipc_server.h>
28 #include "sensor_manager.h"
29 #include "server_channel_handler.h"
31 #define MAX_CONFIG_PATH 255
32 #define CAL_CONFIG_PATH "/etc/sensor_cal.conf"
34 //#define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data"
36 #define TIMEOUT_TERM 10
37 #define MAX_CONNECTION 1000
39 using namespace sensor;
41 ipc::event_loop server::m_loop;
42 std::atomic<bool> server::is_running(false);
51 server &server::instance(void)
57 void server::run(void)
61 retm_if(is_running.load(), "Server is running");
62 retm_if(!instance().init(), "Failed to initialize server");
67 void server::stop(void)
71 retm_if(!is_running.load(), "Server is not running");
77 bool server::init(void)
79 m_server = new(std::nothrow) ipc::ipc_server(SENSOR_CHANNEL_PATH);
80 retvm_if(!m_server, false, "Failed to allocate memory");
82 m_manager = new(std::nothrow) sensor_manager(&m_loop);
83 retvm_if(!m_manager, false, "Failed to allocate memory");
85 m_handler = new(std::nothrow) server_channel_handler(m_manager);
86 retvm_if(!m_handler, false, "Failed to allocate memory");
92 is_running.store(true);
93 sd_notify(0, "READY=1");
98 void server::deinit(void)
112 is_running.store(false);
115 static void set_cal_data(const char *path)
117 FILE *fp = fopen(path, "w");
118 retm_if(!fp, "There is no calibration file[%s]", path);
120 fprintf(fp, "%d", SET_CAL);
123 _I("Succeeded to set calibration data");
126 void server::init_calibration(void)
128 char path[MAX_CONFIG_PATH];
130 FILE *fp = fopen(CAL_CONFIG_PATH, "r");
131 retm_if(!fp, "There is no config file[%s]", CAL_CONFIG_PATH);
134 if (fgets(path, sizeof(path), fp) == NULL)
142 void server::init_server(void)
146 /* TODO: setting socket option */
147 m_server->set_option("max_connection", MAX_CONNECTION);
148 m_server->set_option(SO_TYPE, SOCK_STREAM);
149 m_server->bind(m_handler, &m_loop);
152 static gboolean terminate(gpointer data)
154 sensor_manager *mgr = reinterpret_cast<sensor_manager *>(data);
155 std::vector<sensor_handler *> sensors = mgr->get_sensors();
157 if (sensors.size() <= 0) {
158 _I("Terminating.. because there is no sensors");
165 void server::init_termination(void)
167 g_timeout_add_seconds(TIMEOUT_TERM, terminate, m_manager);