4 * Copyright (c) 2013 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.
22 #include <sensor_common.h>
23 #include <sensor_loader.h>
24 #include <sensor_hal.h>
25 #include <sensor_base.h>
26 #include <sensor_log.h>
27 #include <physical_sensor.h>
28 #include <virtual_sensor.h>
29 #include <unordered_set>
32 #include <hrm_sensor.h>
34 #ifdef ENABLE_AUTO_ROTATION
35 #include <auto_rotation_sensor.h>
38 #include <gravity_sensor.h>
40 #ifdef ENABLE_LINEAR_ACCEL
41 #include <linear_accel_sensor.h>
47 #define DEVICE_HAL_DIR_PATH "/usr/lib/sensor"
49 sensor_loader::sensor_loader()
53 sensor_loader::~sensor_loader()
55 for (auto it = m_handles.begin(); it != m_handles.end(); ++it)
61 sensor_loader& sensor_loader::get_instance()
63 static sensor_loader inst;
67 bool sensor_loader::load(void)
69 std::vector<string> device_hal_paths;
70 std::vector<string> unique_device_hal_paths;
72 get_paths_from_dir(string(DEVICE_HAL_DIR_PATH), device_hal_paths);
74 std::unordered_set<string> s;
75 auto unique = [&s](vector<string> &paths, const string &path) {
76 if (s.insert(path).second)
77 paths.push_back(path);
80 for_each(device_hal_paths.begin(), device_hal_paths.end(),
81 [&](const string &path) {
82 unique(unique_device_hal_paths, path);
86 for_each(unique_device_hal_paths.begin(), unique_device_hal_paths.end(),
87 [&](const string &path) {
89 load_sensor_devices(path, handle);
90 m_handles.push_back(handle);
100 bool sensor_loader::load_sensor_devices(const string &path, void* &handle)
102 sensor_device_t *_devices = NULL;
103 sensor_device *device = NULL;
104 const sensor_info_t *infos;
106 _I("load device: [%s]", path.c_str());
108 void *_handle = dlopen(path.c_str(), RTLD_NOW);
110 _E("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror());
116 /* TODO: The out-param of the create function should be const */
117 create_t create_devices = (create_t) dlsym(_handle, "create");
118 if (!create_devices) {
119 _E("Failed to find symbols in %s", path.c_str());
124 int device_size = create_devices(&_devices);
126 _E("Failed to create devices, path is %s\n", path.c_str());
131 for (int i = 0; i < device_size; ++i) {
132 device = static_cast<sensor_device *>(_devices[i]);
134 int info_size = device->get_sensors(&infos);
135 for (int j = 0; j < info_size; ++j)
136 m_devices[&infos[j]] = device;
144 void sensor_loader::create_sensors(void)
146 /* HRM sensors need SENSOR_PERMISSION_BIO */
147 create_physical_sensors<hrm_sensor>(HRM_RAW_SENSOR);
148 create_physical_sensors<hrm_sensor>(HRM_SENSOR);
149 create_physical_sensors<hrm_sensor>(HRM_LED_GREEN_SENSOR);
150 create_physical_sensors<hrm_sensor>(HRM_LED_IR_SENSOR);
151 create_physical_sensors<hrm_sensor>(HRM_LED_RED_SENSOR);
153 create_physical_sensors<physical_sensor>(UNKNOWN_SENSOR);
155 #ifdef ENABLE_AUTO_ROTATION
156 create_virtual_sensors<auto_rotation_sensor>("Auto Rotation");
158 #ifdef ENABLE_GRAVITY
159 create_virtual_sensors<gravity_sensor>("Gravity");
161 #ifdef ENABLE_LINEAR_ACCEL
162 create_virtual_sensors<linear_accel_sensor>("Linear Accel");
166 template<typename _sensor>
167 void sensor_loader::create_physical_sensors(sensor_type_t type)
170 const sensor_info_t *info;
171 physical_sensor *sensor;
172 sensor_device *device;
174 sensor_device_map_t::iterator it;
176 for (it = m_devices.begin(); it != m_devices.end(); ++it) {
179 if (m_devices[info] == NULL)
182 if (type != UNKNOWN_SENSOR) {
183 if (type != (sensor_type_t)(info->type))
187 sensor = dynamic_cast<physical_sensor *>(create_sensor<_sensor>());
190 _E("Memory allocation failed[%s]", info->name);
194 sensor_type_t _type = (sensor_type_t)info->type;
195 index = (int32_t)m_sensors.count(_type);
197 sensor->set_id(((int64_t)_type << SENSOR_TYPE_SHIFT) | index);
198 sensor->set_sensor_info(info);
199 sensor->set_sensor_device(device);
201 std::shared_ptr<sensor_base> sensor_ptr(sensor);
202 m_sensors.insert(std::make_pair(_type, sensor_ptr));
204 _I("created [%s] sensor", sensor->get_name());
206 m_devices[info] = NULL;
210 template <typename _sensor>
211 void sensor_loader::create_virtual_sensors(const char *name)
215 virtual_sensor *instance;
217 instance = dynamic_cast<virtual_sensor *>(create_sensor<_sensor>());
219 _E("Memory allocation failed[%s]", name);
223 if (!instance->init()) {
224 _E("Failed to init %s", name);
229 std::shared_ptr<sensor_base> sensor(instance);
230 type = sensor->get_type();
231 index = (int32_t)(m_sensors.count(type));
233 sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index);
235 m_sensors.insert(std::make_pair(type, sensor));
237 _I("created [%s] sensor", sensor->get_name());
240 template <typename _sensor>
241 sensor_base* sensor_loader::create_sensor(void)
243 sensor_base *instance = NULL;
246 instance = new _sensor;
247 } catch (std::exception &e) {
248 _E("Failed to create sensor, exception: %s", e.what());
251 _ERRNO(errno, _E, "Failed to create sensor");
258 void sensor_loader::show_sensor_info(void)
260 _I("========== Loaded sensor information ==========\n");
264 auto it = m_sensors.begin();
266 while (it != m_sensors.end()) {
267 sensor_base *sensor = it->second.get();
270 sensor->get_sensor_info(info);
271 _I("No:%d [%s]\n", ++index, sensor->get_name());
276 _I("===============================================\n");
279 bool sensor_loader::get_paths_from_dir(const string &dir_path, vector<string> &hal_paths)
282 struct dirent dir_entry;
283 struct dirent *result;
287 dir = opendir(dir_path.c_str());
290 _E("Failed to open dir: %s", dir_path.c_str());
295 error = readdir_r(dir, &dir_entry, &result);
303 name = string(dir_entry.d_name);
305 if (name == "." || name == "..")
308 hal_paths.push_back(dir_path + "/" + name);
315 sensor_base* sensor_loader::get_sensor(sensor_type_t type)
317 auto it = m_sensors.find(type);
319 if (it == m_sensors.end())
322 return it->second.get();
325 sensor_base* sensor_loader::get_sensor(sensor_id_t id)
327 vector<sensor_base *> sensors;
329 sensor_type_t type = static_cast<sensor_type_t> (id >> SENSOR_TYPE_SHIFT);
330 unsigned int index = (id & SENSOR_INDEX_MASK);
332 sensors = get_sensors(type);
334 if (index >= sensors.size())
337 return sensors[index];
340 vector<sensor_type_t> sensor_loader::get_sensor_types(void)
342 vector<sensor_type_t> sensor_types;
344 auto it = m_sensors.begin();
346 while (it != m_sensors.end()) {
347 sensor_types.push_back((sensor_type_t)(it->first));
348 it = m_sensors.upper_bound(it->first);
354 vector<sensor_base *> sensor_loader::get_sensors(sensor_type_t type)
356 vector<sensor_base *> sensor_list;
357 std::pair<sensor_map_t::iterator, sensor_map_t::iterator> ret;
359 if ((int)(type) == (int)SENSOR_DEVICE_ALL)
360 ret = std::make_pair(m_sensors.begin(), m_sensors.end());
362 ret = m_sensors.equal_range(type);
364 for (auto it = ret.first; it != ret.second; ++it)
365 sensor_list.push_back(it->second.get());
370 vector<sensor_base *> sensor_loader::get_virtual_sensors(void)
372 vector<sensor_base *> virtual_list;
375 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
376 sensor = it->second.get();
378 if (!sensor || !sensor->is_virtual())
381 virtual_list.push_back(sensor);