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_LEGACY1 LIBDIR "/sensor"
41 #define DEVICE_HAL_DIR_PATH_LEGACY2 HAL_LIBDIR "/sensorhub/" // only for emulator
42 #define DEVICE_HAL_DIR_PATH_LEGACY3 LIBDIR "/sensor/hal"
43 #define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical"
44 #define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion"
45 #define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external"
47 static device_sensor_registry_t devices;
48 static physical_sensor_registry_t physical_sensors;
49 static fusion_sensor_registry_t fusion_sensors;
50 static external_sensor_registry_t external_sensors;
52 sensor_manager::sensor_manager(ipc::event_loop *loop)
55 _I("Create[%p]", this);
58 sensor_manager::~sensor_manager()
60 _I("Destroy[%p]", this);
63 bool sensor_manager::init(void)
65 m_loader.load_hal(devices);
66 m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY1, devices);
67 m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY2, devices);
68 m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY3, devices);
69 m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
70 m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
71 m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
73 retvm_if(devices.empty() && external_sensors.empty(), false, "There is no sensors");
75 /* TODO: support dynamic sensor */
76 create_physical_sensors(devices, physical_sensors);
77 create_fusion_sensors(fusion_sensors);
78 create_external_sensors(external_sensors);
84 _I("sensor manager initialization is complete");
88 bool sensor_manager::deinit(void)
90 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
94 external_sensors.clear();
95 fusion_sensors.clear();
96 physical_sensors.clear();
104 bool sensor_manager::is_supported(std::string uri)
106 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
107 sensor_info info = it->second->get_sensor_info();
109 if (info.get_uri() == uri)
116 int sensor_manager::serialize(sensor_info *info, char **bytes)
119 raw_data_t *raw = new(std::nothrow) raw_data_t;
120 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
122 info->serialize(*raw);
124 *bytes = (char *) malloc(raw->size());
127 _E("Failed to allocate memory");
131 std::copy(raw->begin(), raw->end(), *bytes);
139 void sensor_manager::send(std::shared_ptr<ipc::message> msg)
141 for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
145 void sensor_manager::send_added_msg(sensor_info *info)
150 size = serialize(info, &bytes);
152 _E("Failed to message serialize");
156 auto msg = ipc::message::create((const char *)bytes, size);
157 msg->set_type(CMD_MANAGER_SENSOR_ADDED);
162 void sensor_manager::send_removed_msg(const std::string &uri)
164 auto msg = ipc::message::create();
165 msg->set_type(CMD_MANAGER_SENSOR_REMOVED);
166 msg->enclose(uri.c_str(), uri.size());
171 bool sensor_manager::register_sensor(sensor_handler *sensor)
173 retvm_if(!sensor, false, "Invalid sensor");
175 sensor_info info = sensor->get_sensor_info();
177 auto it = m_sensors.find(info.get_uri());
178 retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
180 m_sensors[info.get_uri()] = sensor;
182 send_added_msg(&info);
184 _I("Registered[%s]", info.get_uri().c_str());
189 void sensor_manager::deregister_sensor(const std::string uri)
191 auto it = m_sensors.find(uri);
192 ret_if(it == m_sensors.end());
197 send_removed_msg(uri);
199 _I("Deregistered[%s]", uri.c_str());
202 void sensor_manager::register_channel(ipc::channel *ch)
205 m_channels.push_back(ch);
208 void sensor_manager::deregister_channel(ipc::channel *ch)
212 for (auto it = m_channels.begin(); it != m_channels.end(); ++it) {
214 m_channels.erase(it);
220 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
222 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
223 if (it->first == uri)
226 std::size_t found = it->first.find_last_of("/");
227 if (found == std::string::npos)
230 if (it->first.substr(0, found) == uri)
237 sensor_handler *sensor_manager::get_sensor(const std::string uri)
239 auto it = m_sensors.find(uri);
240 retv_if(it == m_sensors.end(), NULL);
242 return m_sensors[uri];
245 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
247 std::vector<sensor_handler *> sensors;
249 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
250 sensors.push_back(it->second);
255 static physical_sensor *create_physical_sensor(std::string uri, physical_sensor_registry_t &psensors)
257 for (auto it = psensors.begin(); it != psensors.end(); ++it) {
258 if (uri.find((*it)->get_uri()) != std::string::npos) {
259 _D("%s, %s", uri.c_str(), (*it)->get_uri().c_str());
260 return (*it)->clone();
267 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
268 physical_sensor_registry_t &psensors)
270 const sensor_info_t *info;
271 physical_sensor_handler *psensor;
273 for (auto it = devices.begin(); it != devices.end(); ++it) {
274 int count = (*it)->get_sensors(&info);
276 for (int i = 0; i < count; ++i) {
277 physical_sensor *sensor = NULL;
278 sensor_info pinfo(info[i]);
279 std::string uri = pinfo.get_uri();
281 sensor = create_physical_sensor(uri, psensors);
283 sensor->set_device(it->get());
285 psensor = new(std::nothrow) physical_sensor_handler(
286 info[i], it->get(), info[i].id, sensor);
287 retm_if(!psensor, "Failed to allocate memory");
289 m_sensors[uri] = psensor;
294 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
296 const sensor_info2_t *info;
297 const required_sensor_s *required_sensors;
298 fusion_sensor_handler *fsensor;
299 sensor_handler *sensor = NULL;
301 for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
304 (*it)->get_sensor_info(&info);
306 fsensor = new(std::nothrow) fusion_sensor_handler(info[0], it->get());
307 retm_if(!fsensor, "Failed to allocate memory");
309 int count = (*it)->get_required_sensors(&required_sensors);
310 for (int i = 0; i < count; ++i) {
311 sensor = get_sensor_by_type(required_sensors[i].uri);
313 if (sensor == NULL) {
318 fsensor->add_required_sensor(required_sensors[i].id, sensor);
326 sensor_info sinfo = fsensor->get_sensor_info();
327 m_sensors[sinfo.get_uri()] = fsensor;
329 (*it)->set_fusion_sensor_handler(fsensor);
333 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
335 const sensor_info2_t *info;
336 external_sensor_handler *esensor;
338 for (auto it = esensors.begin(); it != esensors.end(); ++it) {
339 (*it)->get_sensor_info(&info);
341 esensor = new(std::nothrow) external_sensor_handler(info[0], it->get());
342 retm_if(!esensor, "Failed to allocate memory");
344 sensor_info sinfo = esensor->get_sensor_info();
345 m_sensors[sinfo.get_uri()] = esensor;
349 static void put_int_to_vec(std::vector<char> &data, int value)
351 char buf[sizeof(value)];
353 int *temp = (int *)buf;
356 std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
359 /* TODO: remove socket fd parameter */
361 * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
363 size_t sensor_manager::serialize(int sock_fd, char **bytes)
366 std::vector<char> raw_list;
368 put_int_to_vec(raw_list, m_sensors.size());
370 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
371 info = it->second->get_sensor_info();
373 raw_data_t *raw = new(std::nothrow) raw_data_t();
374 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
376 info.serialize(*raw);
379 put_int_to_vec(raw_list, raw->size());
382 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
387 *bytes = new(std::nothrow) char[raw_list.size()];
388 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
390 std::copy(raw_list.begin(), raw_list.end(), *bytes);
392 return raw_list.size();
395 void sensor_manager::init_sensors(void)
397 physical_sensor_handler *sensor;
399 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
400 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
404 /* it doesn't need to deregister handlers, they are consumed in event_loop */
405 register_handler(sensor);
409 void sensor_manager::register_handler(physical_sensor_handler *sensor)
411 sensor_event_handler *handler = NULL;
412 int fd = sensor->get_poll_fd();
416 auto it = m_event_handlers.find(fd);
418 if (it != m_event_handlers.end()) {
419 it->second->add_sensor(sensor);
423 handler = new(std::nothrow) sensor_event_handler();
424 retm_if(!handler, "Failed to allocate memory");
426 handler->add_sensor(sensor);
427 m_event_handlers[fd] = handler;
429 if (m_loop->add_event(fd, ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler) == 0) {
430 _D("Failed to add sensor event handler");
431 handler->remove_sensor(sensor);
433 auto iter = m_event_handlers.find(fd);
434 if (iter != m_event_handlers.end()) {
435 m_event_handlers.erase(iter);
441 void sensor_manager::show(void)
445 _I("========== Loaded sensor information ==========\n");
446 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
447 sensor_info info = it->second->get_sensor_info();
449 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
452 _I("===============================================\n");