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>
25 #include <command_types.h>
31 #include "sensor_event_handler.h"
32 #include "sensor_loader.h"
33 #include "physical_sensor_handler.h"
34 #include "fusion_sensor_handler.h"
35 #include "external_sensor_handler.h"
36 #include "fusion_sensor_handler.h"
38 using namespace sensor;
40 #define DEVICE_HAL_DIR_PATH_LEGACY LIBDIR "/sensor"
41 #define DEVICE_HAL_DIR_PATH LIBDIR "/sensor/hal"
42 #define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical"
43 #define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion"
44 #define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external"
46 static device_sensor_registry_t devices;
47 static physical_sensor_registry_t physical_sensors;
48 static fusion_sensor_registry_t fusion_sensors;
49 static external_sensor_registry_t external_sensors;
51 sensor_manager::sensor_manager(ipc::event_loop *loop)
56 sensor_manager::~sensor_manager()
60 bool sensor_manager::init(void)
62 m_loader.load_hal(DEVICE_HAL_DIR_PATH_LEGACY, devices);
63 m_loader.load_hal(DEVICE_HAL_DIR_PATH, devices);
64 m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
65 m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
66 m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
68 retvm_if(devices.empty() && external_sensors.empty(), false, "There is no sensors");
70 /* TODO: support dynamic sensor */
71 create_physical_sensors(devices, physical_sensors);
72 create_fusion_sensors(fusion_sensors);
73 create_external_sensors(external_sensors);
82 bool sensor_manager::deinit(void)
84 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
88 external_sensors.clear();
89 fusion_sensors.clear();
90 physical_sensors.clear();
98 bool sensor_manager::is_supported(std::string uri)
100 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
101 sensor_info info = it->second->get_sensor_info();
103 if (info.get_uri() == uri)
110 int sensor_manager::serialize(sensor_info *info, char **bytes)
113 raw_data_t *raw = new(std::nothrow) raw_data_t;
114 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
116 info->serialize(*raw);
118 *bytes = new(std::nothrow) char[raw->size()];
119 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
121 std::copy(raw->begin(), raw->end(), *bytes);
129 void sensor_manager::send(ipc::message &msg)
131 for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
132 (*it)->send_sync(&msg);
135 void sensor_manager::send_added_msg(sensor_info *info)
140 size = serialize(info, &bytes);
141 retm_if(size == -ENOMEM, "Failed to serialize");
143 ipc::message msg((const char *)bytes, size);
144 msg.set_type(CMD_MANAGER_SENSOR_ADDED);
150 void sensor_manager::send_removed_msg(const std::string &uri)
153 msg.set_type(CMD_MANAGER_SENSOR_REMOVED);
154 msg.enclose(uri.c_str(), uri.size());
159 bool sensor_manager::register_sensor(sensor_handler *sensor)
161 retvm_if(!sensor, false, "Invalid sensor");
163 sensor_info info = sensor->get_sensor_info();
165 auto it = m_sensors.find(info.get_uri());
166 retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
168 m_sensors[info.get_uri()] = sensor;
170 send_added_msg(&info);
172 _I("Registered[%s]", info.get_uri().c_str());
177 void sensor_manager::deregister_sensor(const std::string uri)
179 auto it = m_sensors.find(uri);
180 ret_if(it == m_sensors.end());
185 send_removed_msg(uri);
187 _I("Deregistered[%s]", uri.c_str());
190 void sensor_manager::register_channel(ipc::channel *ch)
193 m_channels.push_back(ch);
196 void sensor_manager::deregister_channel(ipc::channel *ch)
200 for (auto it = m_channels.begin(); it != m_channels.end(); ++it) {
202 m_channels.erase(it);
208 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
210 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
211 if (it->first == uri)
214 std::size_t found = it->first.find_last_of("/");
215 if (found == std::string::npos)
218 if (it->first.substr(0, found) == uri)
225 sensor_handler *sensor_manager::get_sensor(const std::string uri)
227 auto it = m_sensors.find(uri);
228 retv_if(it == m_sensors.end(), NULL);
230 return m_sensors[uri];
233 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
235 std::vector<sensor_handler *> sensors;
237 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
238 sensors.push_back(it->second);
243 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
244 physical_sensor_registry_t &psensors)
246 const sensor_info_t *info;
247 physical_sensor_handler *psensor;
249 for (auto it = devices.begin(); it != devices.end(); ++it) {
250 int count = (*it)->get_sensors(&info);
252 for (int i = 0; i < count; ++i) {
254 psensor = new(std::nothrow) physical_sensor_handler(
255 info[i], it->get(), info[i].id, NULL);
256 retm_if(!psensor, "Failed to allocate memory");
258 sensor_info sinfo = psensor->get_sensor_info();
259 m_sensors[sinfo.get_uri()] = psensor;
264 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
266 const sensor_info2_t *info;
267 const required_sensor_s *required_sensors;
268 fusion_sensor_handler *fsensor;
269 sensor_handler *sensor = NULL;
271 for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
274 (*it)->get_sensor_info(&info);
276 fsensor = new(std::nothrow) fusion_sensor_handler(info[0], it->get());
277 retm_if(!fsensor, "Failed to allocate memory");
279 int count = (*it)->get_required_sensors(&required_sensors);
280 for (int i = 0; i < count; ++i) {
281 sensor = get_sensor_by_type(required_sensors[i].uri);
283 if (sensor == NULL) {
288 fsensor->add_required_sensor(required_sensors[i].id, sensor);
296 sensor_info sinfo = fsensor->get_sensor_info();
297 m_sensors[sinfo.get_uri()] = fsensor;
301 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
303 const sensor_info2_t *info;
304 external_sensor_handler *esensor;
306 for (auto it = esensors.begin(); it != esensors.end(); ++it) {
307 (*it)->get_sensor_info(&info);
309 esensor = new(std::nothrow) external_sensor_handler(info[0], it->get());
310 retm_if(!esensor, "Failed to allocate memory");
312 sensor_info sinfo = esensor->get_sensor_info();
313 m_sensors[sinfo.get_uri()] = esensor;
317 static void put_int_to_vec(std::vector<char> &data, int value)
319 char buf[sizeof(value)];
321 int *temp = (int *)buf;
324 std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
327 /* TODO: remove socket fd parameter */
329 * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
331 size_t sensor_manager::serialize(int sock_fd, char **bytes)
334 std::vector<char> raw_list;
336 put_int_to_vec(raw_list, m_sensors.size());
338 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
339 info = it->second->get_sensor_info();
341 raw_data_t *raw = new(std::nothrow) raw_data_t();
342 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
344 info.serialize(*raw);
347 put_int_to_vec(raw_list, raw->size());
350 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
355 *bytes = new(std::nothrow) char[raw_list.size()];
356 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
358 std::copy(raw_list.begin(), raw_list.end(), *bytes);
360 return raw_list.size();
363 void sensor_manager::init_sensors(void)
365 physical_sensor_handler *sensor;
367 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
368 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
372 /* it doesn't need to deregister handlers, they are consumed in event_loop */
373 register_handler(sensor);
377 void sensor_manager::register_handler(physical_sensor_handler *sensor)
379 ret_if(sensor->get_poll_fd() < 0);
381 sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor);
382 retm_if(!handler, "Failed to allocate memory");
384 m_loop->add_event(sensor->get_poll_fd(),
385 ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler);
388 void sensor_manager::show(void)
392 _I("========== Loaded sensor information ==========\n");
393 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
394 sensor_info info = it->second->get_sensor_info();
396 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
399 _I("===============================================\n");