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