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_LEGACY LIBDIR "/sensor"
39 #define DEVICE_HAL_DIR_PATH LIBDIR "/sensor/hal"
40 #define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical"
41 #define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion"
42 #define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external"
44 static device_sensor_registry_t devices;
45 static physical_sensor_registry_t physical_sensors;
46 static fusion_sensor_registry_t fusion_sensors;
47 static external_sensor_registry_t external_sensors;
49 sensor_manager::sensor_manager(ipc::event_loop *loop)
54 sensor_manager::~sensor_manager()
58 bool sensor_manager::init(void)
60 m_loader.load_hal(DEVICE_HAL_DIR_PATH_LEGACY, devices);
61 m_loader.load_hal(DEVICE_HAL_DIR_PATH, devices);
62 m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
63 m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
64 m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
66 retvm_if(devices.empty(), false, "There is no sensors");
68 /* TODO: support dynamic sensor */
69 create_physical_sensors(devices, physical_sensors);
70 create_fusion_sensors(fusion_sensors);
71 create_external_sensors(external_sensors);
80 bool sensor_manager::deinit(void)
82 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
86 external_sensors.clear();
87 fusion_sensors.clear();
88 physical_sensors.clear();
96 bool sensor_manager::is_supported(std::string uri)
98 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
99 sensor_info info = it->second->get_sensor_info();
101 if (info.get_uri() == uri)
108 bool sensor_manager::register_sensor(sensor_handler *sensor)
110 retvm_if(!sensor, false, "Invalid sensor");
112 sensor_info info = sensor->get_sensor_info();
114 auto it = m_sensors.find(info.get_uri());
115 retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
117 m_sensors[info.get_uri()] = sensor;
119 _I("Registered[%s]", info.get_uri().c_str());
124 void sensor_manager::deregister_sensor(const std::string uri)
126 auto it = m_sensors.find(uri);
127 ret_if(it == m_sensors.end());
132 _I("Deregistered[%s]", uri.c_str());
135 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
137 auto it = m_sensors.begin();
138 for (; it != m_sensors.end(); ++it) {
139 std::size_t found = it->first.rfind(uri);
140 if (found != std::string::npos)
147 sensor_handler *sensor_manager::get_sensor(const std::string uri)
149 auto it = m_sensors.find(uri);
150 retv_if(it == m_sensors.end(), NULL);
152 return m_sensors[uri];
155 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
157 std::vector<sensor_handler *> sensors;
159 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
160 sensors.push_back(it->second);
165 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
166 physical_sensor_registry_t &psensors)
168 const sensor_info_t *info;
169 physical_sensor_handler *psensor;
171 for (auto it = devices.begin(); it != devices.end(); ++it) {
172 int count = (*it)->get_sensors(&info);
174 for (int i = 0; i < count; ++i) {
176 psensor = new(std::nothrow) physical_sensor_handler(
177 info[i], it->get(), info[i].id, NULL);
178 retm_if(!psensor, "Failed to allocate memory");
180 sensor_info sinfo = psensor->get_sensor_info();
181 m_sensors[sinfo.get_uri()] = psensor;
187 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
189 const sensor_info2_t *info;
190 fusion_sensor_handler *fsensor;
191 std::vector<std::string> req_names;
193 for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
194 int count = (*it)->get_sensors(&info);
196 for (int i = 0; i < count; ++i) {
200 fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get());
201 retm_if(!fsensor, "Failed to allocate memory");
203 (*it)->get_required_sensors(req_names);
204 for (unsigned int j = 0; j < req_names.size(); ++j) {
205 sensor_handler *sensor;
206 sensor = get_sensor_by_type(req_names[j]);
208 if (sensor == NULL) {
213 fsensor->add_required_sensor(sensor);
218 /* TODO: remove plugin */
222 sensor_info sinfo = fsensor->get_sensor_info();
223 m_sensors[sinfo.get_uri()] = fsensor;
228 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
230 const sensor_info2_t *info;
231 external_sensor_handler *esensor;
233 for (auto it = esensors.begin(); it != esensors.end(); ++it) {
234 int count = (*it)->get_sensors(&info);
236 for (int i = 0; i < count; ++i) {
237 esensor = new(std::nothrow) external_sensor_handler(info[i], it->get());
238 retm_if(!esensor, "Failed to allocate memory");
240 sensor_info sinfo = esensor->get_sensor_info();
241 m_sensors[sinfo.get_uri()] = esensor;
246 static void put_int_to_vec(std::vector<char> &data, int value)
248 char buf[sizeof(value)];
250 int *temp = (int *)buf;
253 std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
257 * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
259 size_t sensor_manager::serialize(int sock_fd, char **bytes)
262 std::vector<char> raw_list;
264 put_int_to_vec(raw_list, m_sensors.size());
266 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
267 info = it->second->get_sensor_info();
269 raw_data_t *raw = new(std::nothrow) raw_data_t();
270 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
272 info.serialize(*raw);
275 put_int_to_vec(raw_list, raw->size());
278 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
283 *bytes = new(std::nothrow) char[raw_list.size()];
284 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
286 std::copy(raw_list.begin(), raw_list.end(), *bytes);
288 return raw_list.size();
291 void sensor_manager::init_sensors(void)
293 physical_sensor_handler *sensor;
295 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
296 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
300 /* it doesn't need to deregister handlers, they are consumed in event_loop */
301 register_handler(sensor);
305 void sensor_manager::register_handler(physical_sensor_handler *sensor)
307 ret_if(sensor->get_poll_fd() < 0);
309 sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor);
310 retm_if(!handler, "Failed to allocate memory");
312 m_loop->add_event(sensor->get_poll_fd(),
313 ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler);
316 void sensor_manager::show(void)
320 _I("========== Loaded sensor information ==========\n");
321 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
322 sensor_info info = it->second->get_sensor_info();
324 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
327 _I("===============================================\n");