sensord: support original path to load HAL library
[platform/core/system/sensord.git] / src / server / sensor_manager.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "sensor_manager.h"
21
22 #include <unistd.h>
23 #include <sensor_log.h>
24 #include <string>
25 #include <vector>
26 #include <memory>
27 #include <algorithm>
28
29 #include "sensor_event_handler.h"
30 #include "sensor_loader.h"
31 #include "physical_sensor_handler.h"
32 #include "fusion_sensor_handler.h"
33 #include "external_sensor_handler.h"
34 #include "fusion_sensor_handler.h"
35
36 using namespace sensor;
37
38 #define DEVICE_HAL_DIR_PATH_LEGACY LIBDIR "/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"
43
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;
48
49 sensor_manager::sensor_manager(ipc::event_loop *loop)
50 : m_loop(loop)
51 {
52 }
53
54 sensor_manager::~sensor_manager()
55 {
56 }
57
58 bool sensor_manager::init(void)
59 {
60         m_loader.load_hal(DEVICE_HAL_DIR_PATH_LEGACY, devices);
61         m_loader.load_hal(DEVICE_HAL_DIR_PATH, devices);
62         m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
63         m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
64         m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
65
66         retvm_if(devices.empty(), false, "There is no sensors");
67
68         /* TODO: support dynamic sensor */
69         create_physical_sensors(devices, physical_sensors);
70         create_fusion_sensors(fusion_sensors);
71         create_external_sensors(external_sensors);
72
73         init_sensors();
74
75         show();
76
77         return true;
78 }
79
80 bool sensor_manager::deinit(void)
81 {
82         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
83                 delete it->second;
84         m_sensors.clear();
85
86         external_sensors.clear();
87         fusion_sensors.clear();
88         physical_sensors.clear();
89         devices.clear();
90
91         m_loader.unload();
92
93         return true;
94 }
95
96 bool sensor_manager::is_supported(std::string uri)
97 {
98         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
99                 sensor_info info = it->second->get_sensor_info();
100
101                 if (info.get_uri() == uri)
102                         return true;
103         }
104
105         return false;
106 }
107
108 bool sensor_manager::register_sensor(sensor_handler *sensor)
109 {
110         retvm_if(!sensor, false, "Invalid sensor");
111
112         sensor_info info = sensor->get_sensor_info();
113
114         auto it = m_sensors.find(info.get_uri());
115         retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
116
117         m_sensors[info.get_uri()] = sensor;
118
119         _I("Registered[%s]", info.get_uri().c_str());
120
121         return true;
122 }
123
124 void sensor_manager::deregister_sensor(const std::string uri)
125 {
126         auto it = m_sensors.find(uri);
127         ret_if(it == m_sensors.end());
128
129         delete it->second;
130         m_sensors.erase(it);
131
132         _I("Deregistered[%s]", uri.c_str());
133 }
134
135 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
136 {
137         auto it = m_sensors.begin();
138         for (; it != m_sensors.end(); ++it) {
139                 std::size_t found = it->first.rfind(uri);
140                 if (found != std::string::npos)
141                         return it->second;
142         }
143
144         return NULL;
145 }
146
147 sensor_handler *sensor_manager::get_sensor(const std::string uri)
148 {
149         auto it = m_sensors.find(uri);
150         retv_if(it == m_sensors.end(), NULL);
151
152         return m_sensors[uri];
153 }
154
155 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
156 {
157         std::vector<sensor_handler *> sensors;
158
159         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
160                 sensors.push_back(it->second);
161
162         return sensors;
163 }
164
165 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
166                 physical_sensor_registry_t &psensors)
167 {
168         const sensor_info_t *info;
169         physical_sensor_handler *psensor;
170
171         for (auto it = devices.begin(); it != devices.end(); ++it) {
172                 int count = (*it)->get_sensors(&info);
173
174                 for (int i = 0; i < count; ++i) {
175                         /* TODO: psensors */
176                         psensor = new(std::nothrow) physical_sensor_handler(
177                                         info[i], it->get(), info[i].id, NULL);
178                         retm_if(!psensor, "Failed to allocate memory");
179
180                         sensor_info sinfo = psensor->get_sensor_info();
181                         m_sensors[sinfo.get_uri()] = psensor;
182                 }
183         }
184
185 }
186
187 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
188 {
189         const sensor_info2_t *info;
190         fusion_sensor_handler *fsensor;
191         std::vector<std::string> req_names;
192
193         for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
194                 int count = (*it)->get_sensors(&info);
195
196                 for (int i = 0; i < count; ++i) {
197                         bool support = true;
198                         req_names.clear();
199
200                         fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get());
201                         retm_if(!fsensor, "Failed to allocate memory");
202
203                         (*it)->get_required_sensors(req_names);
204                         for (unsigned int j = 0; j < req_names.size(); ++j) {
205                                 sensor_handler *sensor;
206                                 sensor = get_sensor_by_type(req_names[j]);
207
208                                 if (sensor == NULL) {
209                                         support = false;
210                                         break;
211                                 }
212
213                                 fsensor->add_required_sensor(sensor);
214                         }
215
216                         if (!support) {
217                                 delete fsensor;
218                                 /* TODO: remove plugin */
219                                 continue;
220                         }
221
222                         sensor_info sinfo = fsensor->get_sensor_info();
223                         m_sensors[sinfo.get_uri()] = fsensor;
224                 }
225         }
226 }
227
228 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
229 {
230         const sensor_info2_t *info;
231         external_sensor_handler *esensor;
232
233         for (auto it = esensors.begin(); it != esensors.end(); ++it) {
234                 int count = (*it)->get_sensors(&info);
235
236                 for (int i = 0; i < count; ++i) {
237                         esensor = new(std::nothrow) external_sensor_handler(info[i], it->get());
238                         retm_if(!esensor, "Failed to allocate memory");
239
240                         sensor_info sinfo = esensor->get_sensor_info();
241                         m_sensors[sinfo.get_uri()] = esensor;
242                 }
243         }
244 }
245
246 static void put_int_to_vec(std::vector<char> &data, int value)
247 {
248         char buf[sizeof(value)];
249
250         int *temp = (int *)buf;
251         *temp = value;
252
253         std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
254 }
255
256 /* packet format :
257  * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
258  */
259 size_t sensor_manager::serialize(int sock_fd, char **bytes)
260 {
261         sensor_info info;
262         std::vector<char> raw_list;
263
264         put_int_to_vec(raw_list, m_sensors.size());
265
266         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
267                 info = it->second->get_sensor_info();
268
269                 raw_data_t *raw = new(std::nothrow) raw_data_t();
270                 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
271
272                 info.serialize(*raw);
273
274                 /* copy size */
275                 put_int_to_vec(raw_list, raw->size());
276
277                 /* copy info */
278                 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
279
280                 delete raw;
281         }
282
283         *bytes = new(std::nothrow) char[raw_list.size()];
284         retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
285
286         std::copy(raw_list.begin(), raw_list.end(), *bytes);
287
288         return raw_list.size();
289 }
290
291 void sensor_manager::init_sensors(void)
292 {
293         physical_sensor_handler *sensor;
294
295         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
296                 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
297                 if (sensor == NULL)
298                         continue;
299
300                 /* it doesn't need to deregister handlers, they are consumed in event_loop */
301                 register_handler(sensor);
302         }
303 }
304
305 void sensor_manager::register_handler(physical_sensor_handler *sensor)
306 {
307         ret_if(sensor->get_poll_fd() < 0);
308
309         sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor);
310         retm_if(!handler, "Failed to allocate memory");
311
312         m_loop->add_event(sensor->get_poll_fd(),
313                         ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler);
314 }
315
316 void sensor_manager::show(void)
317 {
318         int index = 0;
319
320         _I("========== Loaded sensor information ==========\n");
321         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
322                 sensor_info info = it->second->get_sensor_info();
323
324                 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
325                 info.show();
326         }
327         _I("===============================================\n");
328 }