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 "permission_checker.h"
31 #include "sensor_loader.h"
32 #include "physical_sensor_handler.h"
33 #include "fusion_sensor_handler.h"
34 #include "external_sensor_handler.h"
35 #include "fusion_sensor_handler.h"
37 using namespace 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, devices);
61 m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
62 m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
63 m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
65 retvm_if(devices.empty(), false, "There is no sensors");
67 /* TODO: support dynamic sensor */
68 create_physical_sensors(devices, physical_sensors);
69 create_fusion_sensors(fusion_sensors);
70 create_external_sensors(external_sensors);
79 bool sensor_manager::deinit(void)
81 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
85 external_sensors.clear();
86 fusion_sensors.clear();
87 physical_sensors.clear();
95 bool sensor_manager::is_supported(std::string uri)
97 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
98 sensor_info info = it->second->get_sensor_info();
100 if (info.get_uri() == uri)
107 bool sensor_manager::register_sensor(sensor_handler *sensor)
109 retvm_if(!sensor, false, "Invalid sensor");
111 sensor_info info = sensor->get_sensor_info();
113 auto it = m_sensors.find(info.get_uri());
114 retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
116 m_sensors[info.get_uri()] = sensor;
118 _I("Registered[%s]", info.get_uri().c_str());
123 void sensor_manager::deregister_sensor(const std::string uri)
125 auto it = m_sensors.find(uri);
126 ret_if(it == m_sensors.end());
131 _I("Deregistered[%s]", uri.c_str());
134 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
136 auto it = m_sensors.begin();
137 for (; it != m_sensors.end(); ++it) {
138 std::size_t found = it->first.rfind(uri);
139 if (found != std::string::npos)
146 sensor_handler *sensor_manager::get_sensor(const std::string uri)
148 auto it = m_sensors.find(uri);
149 retv_if(it == m_sensors.end(), NULL);
151 return m_sensors[uri];
154 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
156 std::vector<sensor_handler *> sensors;
158 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
159 sensors.push_back(it->second);
164 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
165 physical_sensor_registry_t &psensors)
167 const sensor_info_t *info;
168 physical_sensor_handler *psensor;
170 for (auto it = devices.begin(); it != devices.end(); ++it) {
171 int count = (*it)->get_sensors(&info);
173 for (int i = 0; i < count; ++i) {
175 psensor = new(std::nothrow) physical_sensor_handler(
176 info[i], it->get(), info[i].id, NULL);
177 retm_if(!psensor, "Failed to allocate memory");
179 sensor_info sinfo = psensor->get_sensor_info();
180 m_sensors[sinfo.get_uri()] = psensor;
186 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
188 const sensor_info2_t *info;
189 fusion_sensor_handler *fsensor;
190 std::vector<std::string> req_names;
192 for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
193 int count = (*it)->get_sensors(&info);
195 for (int i = 0; i < count; ++i) {
199 fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get());
200 retm_if(!fsensor, "Failed to allocate memory");
202 (*it)->get_required_sensors(req_names);
203 for (unsigned int j = 0; j < req_names.size(); ++j) {
204 sensor_handler *sensor;
205 sensor = get_sensor_by_type(req_names[j]);
207 if (sensor == NULL) {
212 fsensor->add_required_sensor(sensor);
217 /* TODO: remove plugin */
221 sensor_info sinfo = fsensor->get_sensor_info();
222 m_sensors[sinfo.get_uri()] = fsensor;
227 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
229 const sensor_info2_t *info;
230 external_sensor_handler *esensor;
232 for (auto it = esensors.begin(); it != esensors.end(); ++it) {
233 int count = (*it)->get_sensors(&info);
235 for (int i = 0; i < count; ++i) {
236 esensor = new(std::nothrow) external_sensor_handler(info[i], it->get());
237 retm_if(!esensor, "Failed to allocate memory");
239 sensor_info sinfo = esensor->get_sensor_info();
240 m_sensors[sinfo.get_uri()] = esensor;
245 static void put_int_to_vec(std::vector<char> &data, int value)
247 char buf[sizeof(value)];
249 int *temp = (int *)buf;
252 std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
256 * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
258 size_t sensor_manager::serialize(int sock_fd, char **bytes)
260 static permission_checker checker;
263 std::vector<char> raw_list;
265 put_int_to_vec(raw_list, m_sensors.size());
267 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
268 info = it->second->get_sensor_info();
270 if (!checker.has_permission(sock_fd, info.get_permission()))
271 info.set_uri(SENSOR_URI_PERMISSION_DENIED);
273 raw_data_t *raw = new(std::nothrow) raw_data_t();
274 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
276 info.serialize(*raw);
279 put_int_to_vec(raw_list, raw->size());
282 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
287 *bytes = new(std::nothrow) char[raw_list.size()];
288 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
290 std::copy(raw_list.begin(), raw_list.end(), *bytes);
292 return raw_list.size();
295 void sensor_manager::init_sensors(void)
297 physical_sensor_handler *sensor;
299 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
300 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
304 /* it doesn't need to deregister handlers, they are consumed in event_loop */
305 register_handler(sensor);
309 void sensor_manager::register_handler(physical_sensor_handler *sensor)
311 ret_if(sensor->get_poll_fd() < 0);
313 sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor);
314 retm_if(!handler, "Failed to allocate memory");
316 m_loop->add_event(sensor->get_poll_fd(),
317 ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler);
320 void sensor_manager::show(void)
324 _I("========== Loaded sensor information ==========\n");
325 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
326 sensor_info info = it->second->get_sensor_info();
328 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
331 _I("===============================================\n");