Add logs in sensord booting
[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 <message.h>
25 #include <command_types.h>
26 #include <string>
27 #include <vector>
28 #include <memory>
29 #include <algorithm>
30
31 #include "sensor_event_handler.h"
32 #include "sensor_loader.h"
33 #include "physical_sensor_handler.h"
34 #include "fusion_sensor_handler.h"
35 #include "external_sensor_handler.h"
36 #include "fusion_sensor_handler.h"
37
38 using namespace sensor;
39
40 #define DEVICE_HAL_DIR_PATH_LEGACY1 LIBDIR "/sensor"
41 #define DEVICE_HAL_DIR_PATH_LEGACY2 HAL_LIBDIR "/sensorhub/" // only for emulator
42 #define DEVICE_HAL_DIR_PATH_LEGACY3 LIBDIR "/sensor/hal"
43 #define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical"
44 #define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion"
45 #define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external"
46
47 static device_sensor_registry_t devices;
48 static physical_sensor_registry_t physical_sensors;
49 static fusion_sensor_registry_t fusion_sensors;
50 static external_sensor_registry_t external_sensors;
51
52 sensor_manager::sensor_manager(ipc::event_loop *loop)
53 : m_loop(loop)
54 {
55         _I("Create[%p]", this);
56 }
57
58 sensor_manager::~sensor_manager()
59 {
60         _I("Destroy[%p]", this);
61 }
62
63 bool sensor_manager::init(void)
64 {
65         m_loader.load_hal(devices);
66         m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY1, devices);
67         m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY2, devices);
68         m_loader.load_hal_legacy(DEVICE_HAL_DIR_PATH_LEGACY3, devices);
69         m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors);
70         m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors);
71         m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors);
72
73         retvm_if(devices.empty() && external_sensors.empty(), false, "There is no sensors");
74
75         /* TODO: support dynamic sensor */
76         create_physical_sensors(devices, physical_sensors);
77         create_fusion_sensors(fusion_sensors);
78         create_external_sensors(external_sensors);
79
80         init_sensors();
81
82         show();
83
84         _I("sensor manager initialization is complete");
85         return true;
86 }
87
88 bool sensor_manager::deinit(void)
89 {
90         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
91                 delete it->second;
92         m_sensors.clear();
93
94         external_sensors.clear();
95         fusion_sensors.clear();
96         physical_sensors.clear();
97         devices.clear();
98
99         m_loader.unload();
100
101         return true;
102 }
103
104 bool sensor_manager::is_supported(std::string uri)
105 {
106         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
107                 sensor_info info = it->second->get_sensor_info();
108
109                 if (info.get_uri() == uri)
110                         return true;
111         }
112
113         return false;
114 }
115
116 int sensor_manager::serialize(sensor_info *info, char **bytes)
117 {
118         int size;
119         raw_data_t *raw = new(std::nothrow) raw_data_t;
120         retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
121
122         info->serialize(*raw);
123
124         *bytes = (char *) malloc(raw->size());
125         if (!(*bytes)) {
126                 delete raw;
127                 _E("Failed to allocate memory");
128                 return -ENOMEM;
129         }
130
131         std::copy(raw->begin(), raw->end(), *bytes);
132
133         size = raw->size();
134         delete raw;
135
136         return size;
137 }
138
139 void sensor_manager::send(std::shared_ptr<ipc::message> msg)
140 {
141         for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
142                 (*it)->send(msg);
143 }
144
145 void sensor_manager::send_added_msg(sensor_info *info)
146 {
147         char *bytes;
148         int size;
149
150         size = serialize(info, &bytes);
151         if (size < 0) {
152                 _E("Failed to message serialize");
153                 return;
154         }
155
156         auto msg = ipc::message::create((const char *)bytes, size);
157         msg->set_type(CMD_MANAGER_SENSOR_ADDED);
158
159         send(msg);
160 }
161
162 void sensor_manager::send_removed_msg(const std::string &uri)
163 {
164         auto msg = ipc::message::create();
165         msg->set_type(CMD_MANAGER_SENSOR_REMOVED);
166         msg->enclose(uri.c_str(), uri.size());
167
168         send(msg);
169 }
170
171 bool sensor_manager::register_sensor(sensor_handler *sensor)
172 {
173         retvm_if(!sensor, false, "Invalid sensor");
174
175         sensor_info info = sensor->get_sensor_info();
176
177         auto it = m_sensors.find(info.get_uri());
178         retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name");
179
180         m_sensors[info.get_uri()] = sensor;
181
182         send_added_msg(&info);
183
184         _I("Registered[%s]", info.get_uri().c_str());
185
186         return true;
187 }
188
189 void sensor_manager::deregister_sensor(const std::string uri)
190 {
191         auto it = m_sensors.find(uri);
192         ret_if(it == m_sensors.end());
193
194         delete it->second;
195         m_sensors.erase(it);
196
197         send_removed_msg(uri);
198
199         _I("Deregistered[%s]", uri.c_str());
200 }
201
202 void sensor_manager::register_channel(ipc::channel *ch)
203 {
204         ret_if(!ch);
205         m_channels.push_back(ch);
206 }
207
208 void sensor_manager::deregister_channel(ipc::channel *ch)
209 {
210         ret_if(!ch);
211
212         for (auto it = m_channels.begin(); it != m_channels.end(); ++it) {
213                 if (*it == ch) {
214                         m_channels.erase(it);
215                         return;
216                 }
217         }
218 }
219
220 sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri)
221 {
222         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
223                 if (it->first == uri)
224                         return it->second;
225
226                 std::size_t found = it->first.find_last_of("/");
227                 if (found == std::string::npos)
228                         continue;
229
230                 if (it->first.substr(0, found) == uri)
231                         return it->second;
232         }
233
234         return NULL;
235 }
236
237 sensor_handler *sensor_manager::get_sensor(const std::string uri)
238 {
239         auto it = m_sensors.find(uri);
240         retv_if(it == m_sensors.end(), NULL);
241
242         return m_sensors[uri];
243 }
244
245 std::vector<sensor_handler *> sensor_manager::get_sensors(void)
246 {
247         std::vector<sensor_handler *> sensors;
248
249         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it)
250                 sensors.push_back(it->second);
251
252         return sensors;
253 }
254
255 static physical_sensor *create_physical_sensor(std::string uri, physical_sensor_registry_t &psensors)
256 {
257         for (auto it = psensors.begin(); it != psensors.end(); ++it) {
258                 if (uri.find((*it)->get_uri()) != std::string::npos) {
259                         _D("%s, %s", uri.c_str(), (*it)->get_uri().c_str());
260                         return (*it)->clone();
261                 }
262         }
263
264         return NULL;
265 }
266
267 void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices,
268                 physical_sensor_registry_t &psensors)
269 {
270         const sensor_info_t *info;
271         physical_sensor_handler *psensor;
272
273         for (auto it = devices.begin(); it != devices.end(); ++it) {
274                 int count = (*it)->get_sensors(&info);
275
276                 for (int i = 0; i < count; ++i) {
277                         physical_sensor *sensor = NULL;
278                         sensor_info pinfo(info[i]);
279                         std::string uri = pinfo.get_uri();
280
281                         sensor = create_physical_sensor(uri, psensors);
282                         if (sensor)
283                                 sensor->set_device(it->get());
284
285                         psensor = new(std::nothrow) physical_sensor_handler(
286                                         info[i], it->get(), info[i].id, sensor);
287                         retm_if(!psensor, "Failed to allocate memory");
288
289                         m_sensors[uri] = psensor;
290                 }
291         }
292 }
293
294 void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
295 {
296         const sensor_info2_t *info;
297         const required_sensor_s *required_sensors;
298         fusion_sensor_handler *fsensor;
299         sensor_handler *sensor = NULL;
300
301         for (auto it = fsensors.begin(); it != fsensors.end(); ++it) {
302                 bool support = true;
303
304                 (*it)->get_sensor_info(&info);
305
306                 fsensor = new(std::nothrow) fusion_sensor_handler(info[0], it->get());
307                 retm_if(!fsensor, "Failed to allocate memory");
308
309                 int count = (*it)->get_required_sensors(&required_sensors);
310                 for (int i = 0; i < count; ++i) {
311                         sensor = get_sensor_by_type(required_sensors[i].uri);
312
313                         if (sensor == NULL) {
314                                 support = false;
315                                 break;
316                         }
317
318                         fsensor->add_required_sensor(required_sensors[i].id, sensor);
319                 }
320
321                 if (!support) {
322                         delete fsensor;
323                         continue;
324                 }
325
326                 sensor_info sinfo = fsensor->get_sensor_info();
327                 m_sensors[sinfo.get_uri()] = fsensor;
328
329                 (*it)->set_fusion_sensor_handler(fsensor);
330         }
331 }
332
333 void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors)
334 {
335         const sensor_info2_t *info;
336         external_sensor_handler *esensor;
337
338         for (auto it = esensors.begin(); it != esensors.end(); ++it) {
339                 (*it)->get_sensor_info(&info);
340
341                 esensor = new(std::nothrow) external_sensor_handler(info[0], it->get());
342                 retm_if(!esensor, "Failed to allocate memory");
343
344                 sensor_info sinfo = esensor->get_sensor_info();
345                 m_sensors[sinfo.get_uri()] = esensor;
346         }
347 }
348
349 static void put_int_to_vec(std::vector<char> &data, int value)
350 {
351         char buf[sizeof(value)];
352
353         int *temp = (int *)buf;
354         *temp = value;
355
356         std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data));
357 }
358
359 /* TODO: remove socket fd parameter */
360 /* packet format :
361  * [count:4] {[size:4] [info:n] [size:4] [info:n] ...}
362  */
363 size_t sensor_manager::serialize(int sock_fd, char **bytes)
364 {
365         sensor_info info;
366         std::vector<char> raw_list;
367
368         put_int_to_vec(raw_list, m_sensors.size());
369
370         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
371                 info = it->second->get_sensor_info();
372
373                 raw_data_t *raw = new(std::nothrow) raw_data_t();
374                 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
375
376                 info.serialize(*raw);
377
378                 /* copy size */
379                 put_int_to_vec(raw_list, raw->size());
380
381                 /* copy info */
382                 std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list));
383
384                 delete raw;
385         }
386
387         *bytes = new(std::nothrow) char[raw_list.size()];
388         retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
389
390         std::copy(raw_list.begin(), raw_list.end(), *bytes);
391
392         return raw_list.size();
393 }
394
395 void sensor_manager::init_sensors(void)
396 {
397         physical_sensor_handler *sensor;
398
399         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
400                 sensor = dynamic_cast<physical_sensor_handler *>(it->second);
401                 if (sensor == NULL)
402                         continue;
403
404                 /* it doesn't need to deregister handlers, they are consumed in event_loop */
405                 register_handler(sensor);
406         }
407 }
408
409 void sensor_manager::register_handler(physical_sensor_handler *sensor)
410 {
411         sensor_event_handler *handler = NULL;
412         int fd = sensor->get_poll_fd();
413
414         ret_if(fd < 0);
415
416         auto it = m_event_handlers.find(fd);
417
418         if (it != m_event_handlers.end()) {
419                 it->second->add_sensor(sensor);
420                 return;
421         }
422
423         handler = new(std::nothrow) sensor_event_handler();
424         retm_if(!handler, "Failed to allocate memory");
425
426         handler->add_sensor(sensor);
427         m_event_handlers[fd] = handler;
428
429         if (m_loop->add_event(fd, ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler) == 0) {
430                 _D("Failed to add sensor event handler");
431                 handler->remove_sensor(sensor);
432
433                 auto iter = m_event_handlers.find(fd);
434                 if (iter != m_event_handlers.end()) {
435                         m_event_handlers.erase(iter);
436                 }
437                 delete handler;
438         }
439 }
440
441 void sensor_manager::show(void)
442 {
443         int index = 0;
444
445         _I("========== Loaded sensor information ==========\n");
446         for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
447                 sensor_info info = it->second->get_sensor_info();
448
449                 _I("Sensor #%d[%s]: ", ++index, it->first.c_str());
450                 info.show();
451         }
452         _I("===============================================\n");
453 }