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_loader.h"
23 #include <sys/types.h>
27 #include <sensor_log.h>
28 #include <sensor_hal.h>
29 #include <physical_sensor.h>
30 #include <fusion_sensor.h>
33 using namespace sensor;
35 sensor_loader::sensor_loader()
39 sensor_loader::~sensor_loader()
43 void sensor_loader::load_hal(const std::string &path, device_sensor_registry_t &devices)
45 load<sensor_device>(path, devices);
48 void sensor_loader::load_physical_sensor(const std::string &path, physical_sensor_registry_t &sensors)
50 load<physical_sensor>(path, sensors);
53 void sensor_loader::load_fusion_sensor(const std::string &path, fusion_sensor_registry_t &sensors)
55 load<fusion_sensor>(path, sensors);
58 void sensor_loader::load_external_sensor(const std::string &path, external_sensor_registry_t &sensors)
60 load<external_sensor>(path, sensors);
63 void sensor_loader::unload(void)
65 for (auto it = m_modules.begin(); it != m_modules.end(); ++it)
70 bool sensor_loader::load(const std::string &dir_path, std::vector<std::shared_ptr<T>> &sensors)
74 std::vector<std::string> module_paths;
77 ret = get_module_paths(dir_path, module_paths);
80 for (auto &path : module_paths) {
81 handle = dlopen(path.c_str(), RTLD_NOW);
82 retvm_if(!handle, false, "Failed to dlopen from %s because %s", path.c_str(), dlerror());
84 /* TODO: out-param of the create function should be const */
85 create_t create = reinterpret_cast<create_t>(dlsym(handle, "create"));
87 _E("Failed to find symbols from %s", path.c_str());
92 int size = create(&results);
93 if (size <= 0 || !results) {
94 _E("Failed to create sensors from %s", path.c_str());
99 for (int i = 0; i < size; ++i)
100 sensors.emplace_back(static_cast<T *>(results[i]));
102 m_modules[path.c_str()] = handle;
108 bool sensor_loader::get_module_paths(const std::string &dir_path, std::vector<std::string> &paths)
111 struct dirent *entry;
113 std::string filename;
115 dir = opendir(dir_path.c_str());
116 retvm_if(!dir, false, "Failed to open directory[%s]", dir_path.c_str());
119 entry = readdir(dir);
122 filename = std::string(entry->d_name);
124 if (filename == "." || filename == "..")
127 std::string full_path = dir_path + "/" + filename;
129 if (lstat(full_path.c_str(), &buf) != 0)
132 if (S_ISDIR(buf.st_mode))
135 paths.push_back(full_path);