sensord: ipc: add message class
[platform/core/system/sensord.git] / src / shared / sensor_info.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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 #include <sensor_log.h>
22 #include <algorithm>
23 #include <vector>
24 #include <string>
25
26 using std::vector;
27 using std::string;
28
29 sensor_type_t sensor_info::get_type(void)
30 {
31         return m_type;
32 }
33
34 sensor_id_t sensor_info::get_id(void)
35 {
36         return m_id;
37 }
38
39 sensor_privilege_t sensor_info::get_privilege(void)
40 {
41         return m_privilege;
42 }
43
44 const char* sensor_info::get_name(void)
45 {
46         return m_name.c_str();
47 }
48
49 const char* sensor_info::get_vendor(void)
50 {
51         return m_vendor.c_str();
52 }
53
54 float sensor_info::get_min_range(void)
55 {
56         return m_min_range;
57 }
58
59 float sensor_info::get_max_range(void)
60 {
61         return m_max_range;
62 }
63
64 float sensor_info::get_resolution(void)
65 {
66         return m_resolution;
67 }
68
69 int sensor_info::get_min_interval(void)
70 {
71         return m_min_interval;
72 }
73
74 int sensor_info::get_fifo_count(void)
75 {
76         return m_fifo_count;
77 }
78
79 int sensor_info::get_max_batch_count(void)
80 {
81         return m_max_batch_count;
82 }
83
84 unsigned int sensor_info::get_supported_event(void)
85 {
86         return m_supported_event;
87 }
88
89 bool sensor_info::is_supported_event(unsigned int event)
90 {
91         if (event != m_supported_event)
92                 return false;
93
94         return true;
95 }
96
97 bool sensor_info::is_wakeup_supported(void)
98 {
99         return m_wakeup_supported;
100 }
101
102 void sensor_info::set_type(sensor_type_t type)
103 {
104         m_type = type;
105 }
106
107 void sensor_info::set_id(sensor_id_t id)
108 {
109         m_id = id;
110 }
111
112 void sensor_info::set_privilege(sensor_privilege_t privilege)
113 {
114         m_privilege = privilege;
115 }
116
117 void sensor_info::set_name(const char *name)
118 {
119         m_name = name;
120 }
121
122 void sensor_info::set_vendor(const char *vendor)
123 {
124         m_vendor = vendor;
125 }
126
127 void sensor_info::set_min_range(float min_range)
128 {
129         m_min_range = min_range;
130 }
131
132 void sensor_info::set_max_range(float max_range)
133 {
134         m_max_range = max_range;
135 }
136
137 void sensor_info::set_resolution(float resolution)
138 {
139         m_resolution = resolution;
140 }
141
142 void sensor_info::set_min_interval(int min_interval)
143 {
144         m_min_interval = min_interval;
145 }
146
147 void sensor_info::set_fifo_count(int fifo_count)
148 {
149         m_fifo_count = fifo_count;
150 }
151
152 void sensor_info::set_max_batch_count(int max_batch_count)
153 {
154         m_max_batch_count = max_batch_count;
155 }
156
157 void sensor_info::set_supported_event(unsigned int event)
158 {
159         m_supported_event = event;
160 }
161
162 void sensor_info::set_wakeup_supported(bool supported)
163 {
164         m_wakeup_supported = supported;
165 }
166
167 void sensor_info::get_raw_data(raw_data_t &data)
168 {
169         put(data, (int) m_type);
170         put(data, m_id);
171         put(data, (int) m_privilege);
172         put(data, m_name);
173         put(data, m_vendor);
174         put(data, m_min_range);
175         put(data, m_max_range);
176         put(data, m_resolution);
177         put(data, m_min_interval);
178         put(data, m_fifo_count);
179         put(data, m_max_batch_count);
180         put(data, m_supported_event);
181         put(data, m_wakeup_supported);
182 }
183
184 void sensor_info::set_raw_data(const char *data, int data_len)
185 {
186         raw_data_t raw_data(&data[0], &data[data_len]);
187
188         auto it_r_data = raw_data.begin();
189
190         int type, privilege;
191         int64_t id;
192
193         it_r_data = get(it_r_data, type);
194         m_type = (sensor_type_t) type;
195         it_r_data = get(it_r_data, id);
196         m_id = (sensor_id_t) id;
197         it_r_data = get(it_r_data, privilege);
198         m_privilege = (sensor_privilege_t) privilege;
199         it_r_data = get(it_r_data, m_name);
200         it_r_data = get(it_r_data, m_vendor);
201         it_r_data = get(it_r_data, m_min_range);
202         it_r_data = get(it_r_data, m_max_range);
203         it_r_data = get(it_r_data, m_resolution);
204         it_r_data = get(it_r_data, m_min_interval);
205         it_r_data = get(it_r_data, m_fifo_count);
206         it_r_data = get(it_r_data, m_max_batch_count);
207         it_r_data = get(it_r_data, m_supported_event);
208         it_r_data = get(it_r_data, m_wakeup_supported);
209 }
210
211 void sensor_info::show(void)
212 {
213         _I("Type = %d", m_type);
214         _I("ID = %#llx", (int64_t)m_id);
215         _I("Privilege = %d", (int)m_privilege);
216         _I("Name = %s", m_name.c_str());
217         _I("Vendor = %s", m_vendor.c_str());
218         _I("Min_range = %f", m_min_range);
219         _I("Max_range = %f", m_max_range);
220         _I("Resolution = %f", m_resolution);
221         _I("Min_interval = %d", m_min_interval);
222         _I("Fifo_count = %d", m_fifo_count);
223         _I("Max_batch_count = %d", m_max_batch_count);
224         _I("Supported_event = %#x", m_supported_event);
225         _I("Wakeup_supported = %d", m_wakeup_supported);
226 }
227
228 void sensor_info::clear(void)
229 {
230         m_type = UNKNOWN_SENSOR;
231         m_id = -1;
232         m_privilege = SENSOR_PRIVILEGE_PUBLIC;
233         m_name.clear();
234         m_vendor.clear();
235         m_min_range = 0.0f;
236         m_max_range = 0.0f;
237         m_resolution = 0.0f;
238         m_min_interval = 0;
239         m_fifo_count = 0;
240         m_max_batch_count = 0;
241         m_supported_event = 0;
242         m_wakeup_supported = false;
243 }
244
245 void sensor_info::put(raw_data_t &data, int value)
246 {
247         char buffer[sizeof(value)];
248
249         int *temp = (int *)buffer;
250         *temp = value;
251
252         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
253 }
254
255 void sensor_info::put(raw_data_t &data, unsigned int value)
256 {
257         char buffer[sizeof(value)];
258
259         unsigned int *temp = (unsigned int *)buffer;
260         *temp = value;
261
262         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
263 }
264
265 void sensor_info::put(raw_data_t &data, int64_t value)
266 {
267         char buffer[sizeof(value)];
268
269         int64_t *temp = (int64_t *) buffer;
270         *temp = value;
271
272         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
273 }
274
275 void sensor_info::put(raw_data_t &data, float value)
276 {
277         char buffer[sizeof(value)];
278
279         float *temp = (float *) buffer;
280         *temp = value;
281
282         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
283 }
284
285 void sensor_info::put(raw_data_t &data, string &value)
286 {
287         put(data, (int) value.size());
288
289         copy(value.begin(), value.end(), back_inserter(data));
290 }
291
292 void sensor_info::put(raw_data_t &data, bool value)
293 {
294         char buffer[sizeof(value)];
295
296         bool *temp = (bool *) buffer;
297         *temp = value;
298
299         copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
300 }
301
302 raw_data_iterator sensor_info::get(raw_data_iterator it, int &value)
303 {
304         copy(it, it + sizeof(value), (char*) &value);
305
306         return it + sizeof(value);
307 }
308
309 raw_data_iterator sensor_info::get(raw_data_iterator it, unsigned int &value)
310 {
311         copy(it, it + sizeof(value), (char*) &value);
312
313         return it + sizeof(value);
314 }
315
316 raw_data_iterator sensor_info::get(raw_data_iterator it, int64_t &value)
317 {
318         copy(it, it + sizeof(value), (char*) &value);
319
320         return it + sizeof(value);
321 }
322
323 raw_data_iterator sensor_info::get(raw_data_iterator it, float &value)
324 {
325         copy(it, it + sizeof(value), (char*) &value);
326
327         return it + sizeof(value);
328 }
329
330 raw_data_iterator sensor_info::get(raw_data_iterator it, string &value)
331 {
332         int len;
333
334         it = get(it, len);
335
336         copy(it, it + len, back_inserter(value));
337
338         return it + len;
339 }
340
341 raw_data_iterator sensor_info::get(raw_data_iterator it, bool &value)
342 {
343         copy(it, it + sizeof(value), (char*) &value);
344
345         return it + sizeof(value);
346 }