Merge branch 'tizen' into tizen_4.0
[platform/core/system/sensord.git] / src / server / physical_sensor_handler.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 "physical_sensor_handler.h"
21
22 #include <sensor_log.h>
23 #include <command_types.h>
24 #include <message.h>
25 #include <algorithm>
26
27 using namespace sensor;
28
29 physical_sensor_handler::physical_sensor_handler(const sensor_info &info,
30                 sensor_device *device, int hal_id,
31                 physical_sensor *sensor)
32 : sensor_handler(info)
33 , m_device(device)
34 , m_sensor(sensor)
35 , m_hal_id(hal_id)
36 , m_prev_interval(0)
37 {
38 }
39
40 physical_sensor_handler::~physical_sensor_handler()
41 {
42 }
43
44 const sensor_info &physical_sensor_handler::get_sensor_info(void)
45 {
46         return m_info;
47 }
48
49 int physical_sensor_handler::get_hal_id(void)
50 {
51         return m_hal_id;
52 }
53
54 int physical_sensor_handler::get_poll_fd(void)
55 {
56         retv_if(!m_device, -EINVAL);
57
58         return m_device->get_poll_fd();
59 }
60
61 int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
62 {
63         retv_if(observer_count() == 0, OP_ERROR);
64         retv_if(!m_device, -EINVAL);
65
66         int size;
67         uint32_t *_ids;
68
69         size = m_device->read_fd(&_ids);
70         retv_if(size == 0, -ENODATA);
71
72         for (int i = 0; i < size; ++i)
73                 ids.push_back(_ids[i]);
74
75         return OP_SUCCESS;
76 }
77
78 int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
79 {
80         retv_if(!m_device, -EINVAL);
81
82         if (m_sensor) {
83                 int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
84                 retv_if(ret <= OP_ERROR, ret);
85         }
86
87         return OP_SUCCESS;
88 }
89 int physical_sensor_handler::start(sensor_observer *ob)
90 {
91         retv_if(!m_device, -EINVAL);
92
93         int policy = OP_DEFAULT;
94
95         if (m_sensor) {
96                 policy = m_sensor->start(ob);
97                 retv_if(policy <= OP_ERROR, policy);
98         }
99
100         add_observer(ob);
101
102         if (policy == OP_DEFAULT) {
103                 if (observer_count() > 1)
104                         return OP_SUCCESS; /* already started */
105         }
106
107         _I("Started[%s]", m_info.get_uri().c_str());
108
109         return m_device->enable(m_hal_id);
110 }
111
112 int physical_sensor_handler::stop(sensor_observer *ob)
113 {
114         retv_if(!m_device, -EINVAL);
115
116         int policy = OP_DEFAULT;
117
118         if (m_sensor) {
119                 policy = m_sensor->stop(ob);
120                 retv_if(policy <= OP_ERROR, policy);
121         }
122
123         remove_observer(ob);
124
125         if (policy == OP_DEFAULT) {
126                 if (observer_count() >= 1)
127                         return OP_SUCCESS; /* already stopped */
128         }
129
130         _I("Stopped[%s]", m_info.get_uri().c_str());
131
132         return m_device->disable(m_hal_id);
133 }
134
135 int physical_sensor_handler::get_min_interval(void)
136 {
137         int interval;
138         std::vector<int> temp;
139
140         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
141                 if (it->second > 0)
142                     temp.push_back(it->second);
143
144         if (temp.empty())
145                 return m_info.get_min_interval();
146
147         interval = *std::min_element(temp.begin(), temp.end());
148
149         if (interval < m_info.get_min_interval())
150                 return m_info.get_min_interval();
151
152         return interval;
153 }
154
155 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
156 {
157         retv_if(!m_device, -EINVAL);
158
159         bool ret = false;
160         int32_t cur_interval = interval;
161         int policy = OP_DEFAULT;
162
163         if (m_sensor) {
164                 policy = m_sensor->set_interval(ob, cur_interval);
165                 retv_if(policy <= OP_ERROR, policy);
166         }
167
168         m_interval_map[ob] = cur_interval;
169
170         if (policy == OP_DEFAULT)
171                 cur_interval = get_min_interval();
172
173         retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
174
175         ret = m_device->set_interval(m_hal_id, cur_interval);
176
177         m_prev_interval = cur_interval;
178
179         _I("Set interval[%d] to sensor[%s]", cur_interval, m_info.get_uri().c_str());
180
181         return (ret ? OP_SUCCESS : OP_ERROR);
182 }
183
184 int physical_sensor_handler::get_min_batch_latency(void)
185 {
186         int batch_latency;
187         std::vector<int> temp;
188
189         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
190                 if (it->second > 0)
191                     temp.push_back(it->second);
192
193         if (temp.empty())
194                 return 0;
195
196         batch_latency = *std::min_element(temp.begin(), temp.end());
197
198         return batch_latency;
199 }
200
201 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
202 {
203         retv_if(!m_device, -EINVAL);
204
205         bool ret = false;
206         int _latency = latency;
207         int policy = OP_DEFAULT;
208
209         if (m_sensor) {
210                 policy = m_sensor->set_batch_latency(ob, latency);
211                 retv_if(policy <= OP_ERROR, policy);
212         }
213
214         m_batch_latency_map[ob] = _latency;
215
216         if (_latency <= latency)
217                 return OP_SUCCESS;
218
219         ret = m_device->set_batch_latency(m_hal_id, _latency);
220
221         return (ret ? OP_SUCCESS : OP_ERROR);
222 }
223
224 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
225 {
226         retv_if(!m_device, -EINVAL);
227
228         bool ret = false;
229         int policy = OP_DEFAULT;
230
231         if (m_sensor) {
232                 policy = m_sensor->set_attribute(ob, attr, value);
233                 retv_if(policy <= OP_ERROR, policy);
234         }
235
236         /*
237          * TODO: default policy for attribute?
238         if (policy == OP_DEFAULT) {
239                 if (observer_count() > 1)
240                         return OP_SUCCESS;
241         }
242         */
243
244         ret = m_device->set_attribute_int(m_hal_id, attr, value);
245
246         return (ret ? OP_SUCCESS : OP_ERROR);
247 }
248
249 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
250 {
251         retv_if(!m_device, -EINVAL);
252
253         bool ret = false;
254         int policy = OP_DEFAULT;
255
256         if (m_sensor) {
257                 policy = m_sensor->set_attribute(ob, attr, value, len);
258                 retv_if(policy <= OP_ERROR, policy);
259         }
260
261         /*
262          * TODO: default policy for attribute?
263         if (policy == OP_DEFAULT) {
264                 if (observer_count() > 1)
265                         return OP_SUCCESS;
266         }
267         */
268
269         ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
270
271         return (ret ? OP_SUCCESS : OP_ERROR);
272 }
273
274 int physical_sensor_handler::flush(sensor_observer *ob)
275 {
276         retv_if(!m_device, -EINVAL);
277         int ret = false;
278
279         if (m_sensor) {
280                 ret = m_sensor->flush(ob);
281                 retv_if(ret <= OP_ERROR, ret);
282         }
283
284         ret = m_device->flush(m_hal_id);
285
286         return (ret ? OP_SUCCESS : OP_ERROR);
287 }
288
289 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
290 {
291         retv_if(!m_device, -EINVAL);
292
293         int remains = m_device->get_data(m_hal_id, data, len);
294         retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");
295
296         return remains;
297 }
298