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