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 #ifdef ENABLE_AUTO_ROTATION
33 #include <auto_rotation_sensor.h>
39 #define DEVICE_PLUGINS_DIR_PATH "/usr/lib/sensor"
40 #define SENSOR_TYPE_SHIFT 32
41 #define SENSOR_INDEX_MASK 0xFFFFFFFF
43 sensor_loader::sensor_loader()
47 sensor_loader& sensor_loader::get_instance()
49 static sensor_loader inst;
53 bool sensor_loader::load_devices(const string &path, vector<sensor_device_t> &devices, void* &handle)
56 sensor_device_t *_devices = NULL;
58 INFO("load device: [%s]", path.c_str());
60 void *_handle = dlopen(path.c_str(), RTLD_NOW);
63 ERR("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror());
69 create_t create_devices = (create_t) dlsym(_handle, "create");
71 if (!create_devices) {
72 ERR("Failed to find symbols in %s", path.c_str());
77 size = create_devices(&_devices);
80 ERR("Failed to create devices, path is %s\n", path.c_str());
87 for (int i = 0; i < size; ++i)
88 devices.push_back(_devices[i]);
97 physical_sensor* sensor_loader::create_sensor(sensor_handle_t handle, sensor_device *device)
100 physical_sensor *sensor;
102 index = (int32_t) (m_sensors.count((sensor_type_t)handle.type));
104 sensor = new(std::nothrow) physical_sensor();
106 ERR("Memory allocation failed[%s]", handle.name);
110 sensor->set_id((int64_t)handle.type << SENSOR_TYPE_SHIFT | index);
111 sensor->set_sensor_handle(handle);
112 sensor->set_sensor_device(device);
117 bool sensor_loader::load_physical_sensors(std::vector<sensor_device_t> &devices)
120 sensor_device *device;
121 const sensor_handle_t *handles;
122 physical_sensor *sensor;
124 for (void *device_ptr : devices) {
125 device = static_cast<sensor_device *>(device_ptr);
127 size = device->get_sensors(&handles);
129 for (int i = 0; i < size; ++i) {
130 sensor = create_sensor(handles[i], device);
134 std::shared_ptr<sensor_base> sensor_ptr(sensor);
135 m_sensors.insert(std::make_pair((sensor_type_t)(handles[i].type), sensor_ptr));
137 INFO("inserted [%s] sensor", sensor->get_name());
144 bool sensor_loader::load_sensors(void)
146 vector<string> device_plugin_paths;
147 vector<string> unique_device_plugin_paths;
149 get_paths_from_dir(string(DEVICE_PLUGINS_DIR_PATH), device_plugin_paths);
151 std::unordered_set<string> s;
152 auto unique = [&s](vector<string> &paths, const string &path) {
153 if (s.insert(path).second)
154 paths.push_back(path);
157 for_each(device_plugin_paths.begin(), device_plugin_paths.end(),
158 [&](const string &path) {
159 unique(unique_device_plugin_paths, path);
163 for_each(unique_device_plugin_paths.begin(), unique_device_plugin_paths.end(),
164 [&](const string &path) {
166 std::vector<sensor_device_t> devices;
168 load_devices(path, devices, handle);
169 load_physical_sensors(devices);
173 load_virtual_sensors();
179 template <typename _sensor>
180 void sensor_loader::load_virtual_sensor(const char *name)
184 virtual_sensor *instance = NULL;
187 instance = new _sensor;
188 } catch (std::exception &e) {
189 ERR("Failed to create %s sensor, exception: %s", name, e.what());
192 ERR("Failed to create %s sensor err: %d, cause: %s", name, err, strerror(err));
196 if (!instance->init()) {
197 ERR("Failed to init %s", name);
202 std::shared_ptr<sensor_base> sensor(instance);
204 type = sensor->get_type();
205 index = (int16_t)(m_sensors.count(type));
207 sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index);
209 m_sensors.insert(std::make_pair(type, sensor));
212 void sensor_loader::load_virtual_sensors(void)
214 load_virtual_sensor<auto_rotation_sensor>("Auto Rotation");
217 void sensor_loader::show_sensor_info(void)
219 INFO("========== Loaded sensor information ==========\n");
223 auto it = m_sensors.begin();
225 while (it != m_sensors.end()) {
226 sensor_base *sensor = it->second.get();
229 sensor->get_sensor_info(info);
230 INFO("No:%d [%s]\n", ++index, sensor->get_name());
235 INFO("===============================================\n");
238 bool sensor_loader::get_paths_from_dir(const string &dir_path, vector<string> &plugin_paths)
241 struct dirent *dir_entry = NULL;
243 dir = opendir(dir_path.c_str());
246 ERR("Failed to open dir: %s", dir_path.c_str());
252 while ((dir_entry = readdir(dir))) {
253 name = string(dir_entry->d_name);
254 plugin_paths.push_back(dir_path + "/" + name);
261 sensor_base* sensor_loader::get_sensor(sensor_type_t type)
263 auto it_plugins = m_sensors.find(type);
265 if (it_plugins == m_sensors.end())
268 return it_plugins->second.get();
271 sensor_base* sensor_loader::get_sensor(sensor_id_t id)
273 vector<sensor_base *> sensors;
275 sensor_type_t type = static_cast<sensor_type_t> (id >> SENSOR_TYPE_SHIFT);
276 unsigned int index = (id & SENSOR_INDEX_MASK);
278 sensors = get_sensors(type);
280 if (index >= sensors.size())
283 return sensors[index];
286 vector<sensor_type_t> sensor_loader::get_sensor_types(void)
288 vector<sensor_type_t> sensor_types;
290 auto it = m_sensors.begin();
292 while (it != m_sensors.end()) {
293 sensor_types.push_back((sensor_type_t)(it->first));
294 it = m_sensors.upper_bound(it->first);
300 vector<sensor_base *> sensor_loader::get_sensors(sensor_type_t type)
302 vector<sensor_base *> sensor_list;
303 std::pair<sensor_map_t::iterator, sensor_map_t::iterator> ret;
305 if ((int)(type) == (int)SENSOR_DEVICE_ALL)
306 ret = std::make_pair(m_sensors.begin(), m_sensors.end());
308 ret = m_sensors.equal_range(type);
310 for (auto it = ret.first; it != ret.second; ++it)
311 sensor_list.push_back(it->second.get());
316 vector<sensor_base *> sensor_loader::get_virtual_sensors(void)
318 vector<sensor_base *> virtual_list;
321 for (auto sensor_it = m_sensors.begin(); sensor_it != m_sensors.end(); ++sensor_it) {
322 sensor = sensor_it->second.get();
324 if (sensor && sensor->is_virtual() == true) {
325 virtual_list.push_back(sensor);