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