4 * Copyright (c) 2014 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_loader.h>
21 #include <libxml/xmlmemory.h>
22 #include <libxml/parser.h>
23 #include <sensor_hal.h>
24 #include <sensor_base.h>
25 #include <physical_sensor.h>
28 #include <sensor_logs.h>
29 #include <unordered_set>
32 #include <accel_sensor.h>
33 #ifdef ENABLE_AUTO_ROTATION
34 #include <auto_rotation_sensor.h>
40 #define DEVICE_PLUGINS_DIR_PATH "/usr/lib/sensor"
41 #define SENSOR_TYPE_SHIFT 32
42 #define SENSOR_INDEX_MASK 0xFFFFFFFF
44 sensor_loader::sensor_loader()
48 sensor_loader& sensor_loader::get_instance()
50 static sensor_loader inst;
54 bool sensor_loader::load(void)
56 std::vector<string> device_plugin_paths;
57 std::vector<string> unique_device_plugin_paths;
59 get_paths_from_dir(string(DEVICE_PLUGINS_DIR_PATH), device_plugin_paths);
61 std::unordered_set<string> s;
62 auto unique = [&s](vector<string> &paths, const string &path) {
63 if (s.insert(path).second)
64 paths.push_back(path);
67 for_each(device_plugin_paths.begin(), device_plugin_paths.end(),
68 [&](const string &path) {
69 unique(unique_device_plugin_paths, path);
73 for_each(unique_device_plugin_paths.begin(), unique_device_plugin_paths.end(),
74 [&](const string &path) {
76 load_sensor_devices(path, handle);
86 bool sensor_loader::load_sensor_devices(const string &path, void* &handle)
88 sensor_device_t *_devices = NULL;
89 sensor_device *device;
90 const sensor_handle_t *handles;
92 _I("load device: [%s]", path.c_str());
94 void *_handle = dlopen(path.c_str(), RTLD_NOW);
96 _E("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror());
102 create_t create_devices = (create_t) dlsym(_handle, "create");
103 if (!create_devices) {
104 _E("Failed to find symbols in %s", path.c_str());
109 int device_size = create_devices(&_devices);
111 _E("Failed to create devices, path is %s\n", path.c_str());
116 for (int i = 0; i < device_size; ++i) {
117 device = static_cast<sensor_device *>(_devices[i]);
119 int handle_size = device->get_sensors(&handles);
120 for (int j = 0; j < handle_size; ++j)
121 m_devices[&handles[j]] = device;
130 void sensor_loader::create_sensors(void)
132 create_physical_sensors<accel_sensor>(ACCELEROMETER_SENSOR);
133 create_physical_sensors<physical_sensor>(UNKNOWN_SENSOR);
135 create_virtual_sensors<auto_rotation_sensor>("Auto Rotation");
138 template<typename _sensor>
139 void sensor_loader::create_physical_sensors(sensor_type_t type)
142 const sensor_handle_t *handle;
143 physical_sensor *sensor;
144 sensor_device *device;
146 sensor_device_map_t::iterator it = m_devices.begin();
148 for (sensor_device_map_t::iterator it = m_devices.begin(); it != m_devices.end(); ++it) {
151 if (m_devices[handle] == NULL)
154 if (type != UNKNOWN_SENSOR) {
155 if (type != (sensor_type_t)(handle->type))
159 sensor = reinterpret_cast<physical_sensor *>(create_sensor<_sensor>());
162 _E("Memory allocation failed[%s]", handle->name);
166 index = (int32_t) (m_sensors.count(type));
168 sensor->set_id(((int64_t)handle->type << SENSOR_TYPE_SHIFT) | index);
169 sensor->set_sensor_handle(handle);
170 sensor->set_sensor_device(device);
172 std::shared_ptr<sensor_base> sensor_ptr(sensor);
173 m_sensors.insert(std::make_pair(type, sensor_ptr));
175 _I("created [%s] sensor", sensor->get_name());
177 m_devices[handle] = NULL;
182 template <typename _sensor>
183 void sensor_loader::create_virtual_sensors(const char *name)
187 virtual_sensor *instance;
189 instance = reinterpret_cast<virtual_sensor *>(create_sensor<_sensor>());
191 _E("Memory allocation failed[%s]", name);
195 if (!instance->init()) {
196 _E("Failed to init %s", name);
201 std::shared_ptr<sensor_base> sensor(instance);
202 type = sensor->get_type();
203 index = (int32_t)(m_sensors.count(type));
205 sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index);
207 m_sensors.insert(std::make_pair(type, sensor));
209 _I("created [%s] sensor", sensor->get_name());
212 template <typename _sensor>
213 sensor_base* sensor_loader::create_sensor(void)
215 sensor_base *instance = NULL;
218 instance = new _sensor;
219 } catch (std::exception &e) {
220 _E("Failed to create sensor, exception: %s", e.what());
223 _E("Failed to create sensor err: %d, cause: %s", err, strerror(err));
230 void sensor_loader::show_sensor_info(void)
232 _I("========== Loaded sensor information ==========\n");
236 auto it = m_sensors.begin();
238 while (it != m_sensors.end()) {
239 sensor_base *sensor = it->second.get();
242 sensor->get_sensor_info(info);
243 _I("No:%d [%s]\n", ++index, sensor->get_name());
248 _I("===============================================\n");
251 bool sensor_loader::get_paths_from_dir(const string &dir_path, vector<string> &plugin_paths)
254 struct dirent *dir_entry = NULL;
256 dir = opendir(dir_path.c_str());
259 _E("Failed to open dir: %s", dir_path.c_str());
265 while ((dir_entry = readdir(dir))) {
266 name = string(dir_entry->d_name);
267 plugin_paths.push_back(dir_path + "/" + name);
274 sensor_base* sensor_loader::get_sensor(sensor_type_t type)
276 auto it_plugins = m_sensors.find(type);
278 if (it_plugins == m_sensors.end())
281 return it_plugins->second.get();
284 sensor_base* sensor_loader::get_sensor(sensor_id_t id)
286 vector<sensor_base *> sensors;
288 sensor_type_t type = static_cast<sensor_type_t> (id >> SENSOR_TYPE_SHIFT);
289 unsigned int index = (id & SENSOR_INDEX_MASK);
291 sensors = get_sensors(type);
293 if (index >= sensors.size())
296 return sensors[index];
299 vector<sensor_type_t> sensor_loader::get_sensor_types(void)
301 vector<sensor_type_t> sensor_types;
303 auto it = m_sensors.begin();
305 while (it != m_sensors.end()) {
306 sensor_types.push_back((sensor_type_t)(it->first));
307 it = m_sensors.upper_bound(it->first);
313 vector<sensor_base *> sensor_loader::get_sensors(sensor_type_t type)
315 vector<sensor_base *> sensor_list;
316 std::pair<sensor_map_t::iterator, sensor_map_t::iterator> ret;
318 if ((int)(type) == (int)SENSOR_DEVICE_ALL)
319 ret = std::make_pair(m_sensors.begin(), m_sensors.end());
321 ret = m_sensors.equal_range(type);
323 for (auto it = ret.first; it != ret.second; ++it)
324 sensor_list.push_back(it->second.get());
329 vector<sensor_base *> sensor_loader::get_virtual_sensors(void)
331 vector<sensor_base *> virtual_list;
334 for (auto sensor_it = m_sensors.begin(); sensor_it != m_sensors.end(); ++sensor_it) {
335 sensor = sensor_it->second.get();
337 if (sensor && sensor->is_virtual() == true) {
338 virtual_list.push_back(sensor);