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.
20 #include "sensor_manager.h"
23 #include <sensor_log.h>
29 #include "sensor_event_handler.h"
30 #include "sensor_loader.h"
31 #include "physical_sensor_handler.h"
32 #include "fusion_sensor_handler.h"
33 #include "external_sensor_handler.h"
34 #include "fusion_sensor_handler.h"
36 using namespace sensor;
38 #define DEVICE_HAL_DIR_PATH LIBDIR "/sensor/hal"
39 #define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical"
40 #define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion"
41 #define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external"
43 static device_sensor_registry_t devices;
44 static physical_sensor_registry_t physical_sensors;
45 static fusion_sensor_registry_t fusion_sensors;
46 static external_sensor_registry_t external_sensors;
48 sensor_manager::sensor_manager(ipc::event_loop *loop)
53 sensor_manager::~sensor_manager()
57 bool sensor_manager::init(void)
59 m_loader.load_hal(DEVICE_HAL_DIR_PATH, devices);
60 m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
61 m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
62 m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
64 retvm_if(devices.empty(), false, "There is no sensors");
66 /* TODO: support dynamic sensor */
67 create_physical_sensors(devices, physical_sensors);
68 create_fusion_sensors(fusion_sensors);
69 create_external_sensors(external_sensors);
78 bool sensor_manager::deinit(void)
80 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
84 external_sensors.clear();
85 fusion_sensors.clear();
86 physical_sensors.clear();
94 bool sensor_manager::is_supported(std::string uri)
96 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
97 sensor_info info = it->second->get_sensor_info();
99 if (info.get_uri() == uri)
106 bool sensor_manager::register_sensor(sensor_handler *sensor)
108 retvm_if(!sensor, false, "Invalid sensor");
110 sensor_info info = sensor->get_sensor_info();
112 auto it = m_sensors.find(info.get_uri());
113 retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
115 m_sensors[info.get_uri()] = sensor;
117 _I("Registered[%s]", info.get_uri().c_str());
122 void sensor_manager::deregister_sensor(const std::string uri)
124 auto it = m_sensors.find(uri);
125 ret_if(it == m_sensors.end());
130 _I("Deregistered[%s]", uri.c_str());
133 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
135 auto it = m_sensors.begin();
136 for (; it != m_sensors.end(); ++it) {
137 std::size_t found = it->first.rfind(uri);
138 if (found != std::string::npos)
145 sensor_handler *sensor_manager::get_sensor(const std::string uri)
147 auto it = m_sensors.find(uri);
148 retv_if(it == m_sensors.end(), NULL);
150 return m_sensors[uri];
153 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
155 std::vector<sensor_handler *> sensors;
157 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
158 sensors.push_back(it->second);
163 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
164 physical_sensor_registry_t &psensors)
166 const sensor_info_t *info;
167 physical_sensor_handler *psensor;
169 for (auto it = devices.begin(); it != devices.end(); ++it) {
170 int count = (*it)->get_sensors(&info);
172 for (int i = 0; i < count; ++i) {
174 psensor = new(std::nothrow) physical_sensor_handler(
175 info[i], it->get(), info[i].id, NULL);
176 retm_if(!psensor, "Failed to allocate memory");
178 sensor_info sinfo = psensor->get_sensor_info();
179 m_sensors[sinfo.get_uri()] = psensor;
185 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
187 const sensor_info2_t *info;
188 fusion_sensor_handler *fsensor;
189 std::vector<std::string> req_names;
191 for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
192 int count = (*it)->get_sensors(&info);
194 for (int i = 0; i < count; ++i) {
198 fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get());
199 retm_if(!fsensor, "Failed to allocate memory");
201 (*it)->get_required_sensors(req_names);
202 for (unsigned int j = 0; j < req_names.size(); ++j) {
203 sensor_handler *sensor;
204 sensor = get_sensor_by_type(req_names[j]);
206 if (sensor == NULL) {
211 fsensor->add_required_sensor(sensor);
216 /* TODO: remove plugin */
220 sensor_info sinfo = fsensor->get_sensor_info();
221 m_sensors[sinfo.get_uri()] = fsensor;
226 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
228 const sensor_info2_t *info;
229 external_sensor_handler *esensor;
231 for (auto it = esensors.begin(); it != esensors.end(); ++it) {
232 int count = (*it)->get_sensors(&info);
234 for (int i = 0; i < count; ++i) {
235 esensor = new(std::nothrow) external_sensor_handler(info[i], it->get());
236 retm_if(!esensor, "Failed to allocate memory");
238 sensor_info sinfo = esensor->get_sensor_info();
239 m_sensors[sinfo.get_uri()] = esensor;
244 static void put_int_to_vec(std::vector<char> &data, int value)
246 char buf[sizeof(value)];
248 int *temp = (int *)buf;
251 std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
255 * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
257 size_t sensor_manager::serialize(int sock_fd, char **bytes)
260 std::vector<char> raw_list;
262 put_int_to_vec(raw_list, m_sensors.size());
264 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
265 info = it->second->get_sensor_info();
267 raw_data_t *raw = new(std::nothrow) raw_data_t();
268 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
270 info.serialize(*raw);
273 put_int_to_vec(raw_list, raw->size());
276 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
281 *bytes = new(std::nothrow) char[raw_list.size()];
282 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
284 std::copy(raw_list.begin(), raw_list.end(), *bytes);
286 return raw_list.size();
289 void sensor_manager::init_sensors(void)
291 physical_sensor_handler *sensor;
293 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
294 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
298 /* it doesn't need to deregister handlers, they are consumed in event_loop */
299 register_handler(sensor);
303 void sensor_manager::register_handler(physical_sensor_handler *sensor)
305 ret_if(sensor->get_poll_fd() < 0);
307 sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor);
308 retm_if(!handler, "Failed to allocate memory");
310 m_loop->add_event(sensor->get_poll_fd(),
311 ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler);
314 void sensor_manager::show(void)
318 _I("========== Loaded sensor information ==========\n");
319 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
320 sensor_info info = it->second->get_sensor_info();
322 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
325 _I("===============================================\n");