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.
24 #include <systemd/sd-daemon.h>
25 #include <sensor_log.h>
26 #include <command_types.h>
27 #include <ipc_server.h>
29 #include "sensor_manager.h"
30 #include "server_channel_handler.h"
32 #define MAX_CONFIG_PATH 255
33 #define CAL_CONFIG_PATH "/etc/sensor_cal.conf"
35 //#define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data"
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");
91 is_running.store(true);
92 sd_notify(0, "READY=1");
97 void server::deinit(void)
111 is_running.store(false);
114 static void set_cal_data(const char *path)
116 struct stat file_stat;
118 if (lstat(path, &file_stat) != 0)
121 if (!S_ISREG(file_stat.st_mode))
124 FILE *fp = fopen(path, "w");
125 retm_if(!fp, "There is no calibration file[%s]", path);
127 fprintf(fp, "%d", SET_CAL);
130 _I("Succeeded to set calibration data");
133 void server::init_calibration(void)
135 char path[MAX_CONFIG_PATH];
137 FILE *fp = fopen(CAL_CONFIG_PATH, "r");
138 retm_if(!fp, "There is no config file[%s]", CAL_CONFIG_PATH);
141 if (fgets(path, sizeof(path), fp) == NULL)
149 void server::init_server(void)
153 /* TODO: setting socket option */
154 m_server->set_option("max_connection", MAX_CONNECTION);
155 m_server->set_option(SO_TYPE, SOCK_STREAM);
156 m_server->bind(m_handler, &m_loop);