sensord: check if the event value is a valid number
[platform/core/system/sensord.git] / src / shared / sensor_info.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_info.h"
21
22 #include <sensor_types.h>
23 #include <sensor_types_private.h>
24 #include <sensor_log.h>
25 #include <cfloat>
26 #include <algorithm>
27 #include <string>
28 #include <cmath>
29
30 #include "sensor_utils.h"
31
32 #define MIN_RANGE -FLT_MAX
33 #define MAX_RANGE FLT_MAX
34
35 using namespace sensor;
36
37 sensor_info::sensor_info()
38 : m_type(UNKNOWN_SENSOR)
39 , m_uri(SENSOR_UNKNOWN_NAME)
40 , m_model(SENSOR_UNKNOWN_NAME)
41 , m_vendor(SENSOR_UNKNOWN_NAME)
42 , m_min_range(0)
43 , m_max_range(0)
44 , m_resolution(0)
45 , m_min_interval(0)
46 , m_max_batch_count(0)
47 , m_wakeup_supported(false)
48 , m_privilege("")
49 {
50 }
51
52 sensor_info::sensor_info(const sensor_info &info)
53 : m_type(info.m_type)
54 , m_uri(info.m_uri)
55 , m_model(info.m_model)
56 , m_vendor(info.m_vendor)
57 , m_min_range(info.m_min_range)
58 , m_max_range(info.m_max_range)
59 , m_resolution(info.m_resolution)
60 , m_min_interval(info.m_min_interval)
61 , m_max_batch_count(info.m_max_batch_count)
62 , m_wakeup_supported(info.m_wakeup_supported)
63 , m_privilege(info.m_privilege)
64 {
65 }
66
67 sensor_info::sensor_info(const sensor_info_t &info)
68 {
69         /* TODO: HAL should change name from single name to URI */
70         const char *type = sensor::utils::get_uri((sensor_type_t)info.type);
71         std::string uri(type);
72         uri.append("/").append(info.name);
73
74         set_type((sensor_type_t)info.type);
75         set_uri(uri.c_str());
76         set_model(info.model_name);
77         set_vendor(info.vendor);
78         set_min_range(info.min_range);
79         set_max_range(info.max_range);
80         set_resolution(info.resolution);
81         set_min_interval(info.min_interval);
82         set_max_batch_count(info.max_batch_count);
83         set_wakeup_supported(info.wakeup_supported);
84         /* TODO: sensor_info_t should have privilege string */
85         set_privilege("");
86 }
87
88 sensor_info::sensor_info(const sensor_info2_t &info)
89 {
90         std::string uri(info.uri);
91         std::size_t found = uri.find_last_of("/\\");
92
93         set_type(info.type);
94         set_uri(uri.c_str());
95         set_model(uri.substr(found + 1, uri.length()).c_str());
96         set_vendor(info.vendor);
97         set_min_range(info.min_range);
98         set_max_range(info.max_range);
99         set_resolution(info.resolution);
100         set_min_interval(info.min_interval);
101         set_max_batch_count(info.max_batch_count);
102         set_wakeup_supported(info.wakeup_supported);
103         set_privilege(info.privilege);
104 }
105
106 sensor_type_t sensor_info::get_type(void)
107 {
108         return m_type;
109 }
110
111 std::string &sensor_info::get_uri(void)
112 {
113         return m_uri;
114 }
115
116 std::string &sensor_info::get_model(void)
117 {
118         return m_model;
119 }
120
121 std::string &sensor_info::get_vendor(void)
122 {
123         return m_vendor;
124 }
125
126 float sensor_info::get_min_range(void)
127 {
128         return m_min_range;
129 }
130
131 float sensor_info::get_max_range(void)
132 {
133         return m_max_range;
134 }
135
136 float sensor_info::get_resolution(void)
137 {
138         return m_resolution;
139 }
140
141 int sensor_info::get_min_interval(void)
142 {
143         return m_min_interval;
144 }
145
146 int sensor_info::get_max_batch_count(void)
147 {
148         return m_max_batch_count;
149 }
150
151 bool sensor_info::is_wakeup_supported(void)
152 {
153         return m_wakeup_supported;
154 }
155
156 std::string &sensor_info::get_privilege(void)
157 {
158         return m_privilege;
159 }
160
161 void sensor_info::set_type(sensor_type_t type)
162 {
163         m_type = type;
164 }
165
166 void sensor_info::set_uri(const char *name)
167 {
168         m_uri = name;
169 }
170
171 void sensor_info::set_model(const char *model)
172 {
173         m_model = model;
174 }
175
176 void sensor_info::set_vendor(const char *vendor)
177 {
178         m_vendor = vendor;
179 }
180
181 void sensor_info::set_min_range(float min_range)
182 {
183         m_min_range = min_range;
184
185         if (!std::isnormal(m_min_range))
186                 m_min_range = 0; /* set value to 0 when the value is NaN, infinity, zero or subnormal */
187         if (m_min_range < MIN_RANGE)
188                 m_min_range = MIN_RANGE;
189         if (m_min_range > MAX_RANGE)
190                 m_min_range = MAX_RANGE;
191 }
192
193 void sensor_info::set_max_range(float max_range)
194 {
195         m_max_range = max_range;
196
197         if (!std::isnormal(m_max_range))
198                 m_max_range = 0; /* set value to 0 when the value is NaN, infinity, zero or subnormal */
199         if (m_max_range < MIN_RANGE)
200                 m_max_range = MIN_RANGE;
201         if (m_max_range > MAX_RANGE)
202                 m_max_range = MAX_RANGE;
203 }
204
205 void sensor_info::set_resolution(float resolution)
206 {
207         m_resolution = resolution;
208 }
209
210 void sensor_info::set_min_interval(int min_interval)
211 {
212         m_min_interval = min_interval;
213 }
214
215 void sensor_info::set_max_batch_count(int max_batch_count)
216 {
217         m_max_batch_count = max_batch_count;
218 }
219
220 void sensor_info::set_wakeup_supported(bool supported)
221 {
222         m_wakeup_supported = supported;
223 }
224
225 void sensor_info::set_privilege(const char *privilege)
226 {
227         m_privilege = privilege;
228 }
229
230 void sensor_info::add_privilege(const char *privilege)
231 {
232         if (!m_privilege.empty())
233                 m_privilege.append(PRIV_DELIMITER);
234         m_privilege.append(privilege);
235 }
236
237 void sensor_info::serialize(raw_data_t &data)
238 {
239         put(data, m_type);
240         put(data, m_uri);
241         put(data, m_model);
242         put(data, m_vendor);
243         put(data, m_min_range);
244         put(data, m_max_range);
245         put(data, m_resolution);
246         put(data, m_min_interval);
247         put(data, m_max_batch_count);
248         put(data, m_wakeup_supported);
249         put(data, m_privilege);
250 }
251
252 void sensor_info::deserialize(const char *data, int data_len)
253 {
254         int type;
255
256         raw_data_t raw_data(&data[0], &data[data_len]);
257         auto it = raw_data.begin();
258         it = get(it, type);
259         m_type = (sensor_type_t)type;
260
261         it = get(it, m_uri);
262         it = get(it, m_model);
263         it = get(it, m_vendor);
264         it = get(it, m_min_range);
265         it = get(it, m_max_range);
266         it = get(it, m_resolution);
267         it = get(it, m_min_interval);
268         it = get(it, m_max_batch_count);
269         it = get(it, m_wakeup_supported);
270         it = get(it, m_privilege);
271 }
272
273 void sensor_info::show(void)
274 {
275         _I("URI = %s", m_uri.c_str());
276         _I("Model = %s", m_model.c_str());
277         _I("Vendor = %s", m_vendor.c_str());
278         _I("Min_range = %f", m_min_range);
279         _I("Max_range = %f", m_max_range);
280         _I("Resolution = %f", m_resolution);
281         _I("Min_interval = %d", m_min_interval);
282         _I("Max_batch_count = %d", m_max_batch_count);
283         _I("Wakeup_supported = %d", m_wakeup_supported);
284         _I("Privilege = %s", m_privilege.c_str());
285 }
286
287 void sensor_info::clear(void)
288 {
289         m_type = UNKNOWN_SENSOR;
290         m_uri.clear();
291         m_model.clear();
292         m_vendor.clear();
293         m_min_range = 0.0f;
294         m_max_range = 0.0f;
295         m_resolution = 0.0f;
296         m_min_interval = 0;
297         m_max_batch_count = 0;
298         m_wakeup_supported = false;
299         m_privilege.clear();
300 }
301
302 void sensor_info::put(raw_data_t &data, int value)
303 {
304         char buffer[sizeof(value)];
305
306         int *temp = reinterpret_cast<int *>(buffer);
307         *temp = value;
308
309         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
310 }
311
312 void sensor_info::put(raw_data_t &data, unsigned int value)
313 {
314         char buffer[sizeof(value)];
315
316         unsigned int *temp = reinterpret_cast<unsigned int *>(buffer);
317         *temp = value;
318
319         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
320 }
321
322 void sensor_info::put(raw_data_t &data, int64_t value)
323 {
324         char buffer[sizeof(value)];
325
326         int64_t *temp = reinterpret_cast<int64_t *>(buffer);
327         *temp = value;
328
329         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
330 }
331
332 void sensor_info::put(raw_data_t &data, float value)
333 {
334         char buffer[sizeof(value)];
335
336         float *temp = reinterpret_cast<float *>(buffer);
337         *temp = value;
338
339         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
340 }
341
342 void sensor_info::put(raw_data_t &data, std::string &value)
343 {
344         put(data, (int) value.size());
345
346         copy(value.begin(), value.end(), back_inserter(data));
347 }
348
349 void sensor_info::put(raw_data_t &data, bool value)
350 {
351         char buffer[sizeof(value)];
352
353         bool *temp = (bool *) buffer;
354         *temp = value;
355
356         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
357 }
358
359 raw_data_iterator sensor_info::get(raw_data_iterator it, int &value)
360 {
361         copy(it, it + sizeof(value), (char*) &value);
362
363         return it + sizeof(value);
364 }
365
366 raw_data_iterator sensor_info::get(raw_data_iterator it, unsigned int &value)
367 {
368         copy(it, it + sizeof(value), (char*) &value);
369
370         return it + sizeof(value);
371 }
372
373 raw_data_iterator sensor_info::get(raw_data_iterator it, int64_t &value)
374 {
375         copy(it, it + sizeof(value), (char*) &value);
376
377         return it + sizeof(value);
378 }
379
380 raw_data_iterator sensor_info::get(raw_data_iterator it, float &value)
381 {
382         copy(it, it + sizeof(value), (char*) &value);
383
384         return it + sizeof(value);
385 }
386
387 raw_data_iterator sensor_info::get(raw_data_iterator it, std::string &value)
388 {
389         int len;
390
391         it = get(it, len);
392
393         copy(it, it + len, back_inserter(value));
394
395         return it + len;
396 }
397
398 raw_data_iterator sensor_info::get(raw_data_iterator it, bool &value)
399 {
400         copy(it, it + sizeof(value), (char*) &value);
401
402         return it + sizeof(value);
403 }