sensord: move common part to base sensor handler
[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         return m_device->enable(m_hal_id);
108 }
109
110 int physical_sensor_handler::stop(sensor_observer *ob)
111 {
112         retv_if(!m_device, -EINVAL);
113
114         int policy = OP_DEFAULT;
115
116         if (m_sensor) {
117                 policy = m_sensor->stop(ob);
118                 retv_if(policy <= OP_ERROR, policy);
119         }
120
121         remove_observer(ob);
122
123         if (policy == OP_DEFAULT) {
124                 if (observer_count() >= 1)
125                         return OP_SUCCESS; /* already stopped */
126         }
127
128         return m_device->disable(m_hal_id);
129 }
130
131 int physical_sensor_handler::get_min_interval(void)
132 {
133         int interval;
134         std::vector<int> temp;
135
136         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
137                 if (it->second > 0)
138                     temp.push_back(it->second);
139
140         if (temp.empty())
141                 return m_info.get_min_interval();
142
143         interval = *std::min_element(temp.begin(), temp.end());
144
145         if (interval < m_info.get_min_interval())
146                 return m_info.get_min_interval();
147
148         return interval;
149 }
150
151 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
152 {
153         retv_if(!m_device, -EINVAL);
154
155         bool ret = false;
156         int32_t cur_interval = interval;
157         int policy = OP_DEFAULT;
158
159         if (m_sensor) {
160                 policy = m_sensor->set_interval(ob, cur_interval);
161                 retv_if(policy <= OP_ERROR, policy);
162         }
163
164         m_interval_map[ob] = cur_interval;
165
166         if (policy == OP_DEFAULT)
167                 cur_interval = get_min_interval();
168
169         retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
170
171         ret = m_device->set_interval(m_hal_id, cur_interval);
172
173         m_prev_interval = cur_interval;
174
175         return (ret ? OP_SUCCESS : OP_ERROR);
176 }
177
178 int physical_sensor_handler::get_min_batch_latency(void)
179 {
180         int batch_latency;
181         std::vector<int> temp;
182
183         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
184                 if (it->second > 0)
185                     temp.push_back(it->second);
186
187         if (temp.empty())
188                 return 0;
189
190         batch_latency = *std::min_element(temp.begin(), temp.end());
191
192         return batch_latency;
193 }
194
195 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
196 {
197         retv_if(!m_device, -EINVAL);
198
199         bool ret = false;
200         int _latency = latency;
201         int policy = OP_DEFAULT;
202
203         if (m_sensor) {
204                 policy = m_sensor->set_batch_latency(ob, latency);
205                 retv_if(policy <= OP_ERROR, policy);
206         }
207
208         m_batch_latency_map[ob] = _latency;
209
210         if (_latency <= latency)
211                 return OP_SUCCESS;
212
213         ret = m_device->set_batch_latency(m_hal_id, _latency);
214
215         return (ret ? OP_SUCCESS : OP_ERROR);
216 }
217
218 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
219 {
220         retv_if(!m_device, -EINVAL);
221
222         bool ret = false;
223         int policy = OP_DEFAULT;
224
225         if (m_sensor) {
226                 policy = m_sensor->set_attribute(ob, attr, value);
227                 retv_if(policy <= OP_ERROR, policy);
228         }
229
230         /*
231          * TODO: default policy for attribute?
232         if (policy == OP_DEFAULT) {
233                 if (observer_count() > 1)
234                         return OP_SUCCESS;
235         }
236         */
237
238         ret = m_device->set_attribute_int(m_hal_id, attr, value);
239
240         return (ret ? OP_SUCCESS : OP_ERROR);
241 }
242
243 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
244 {
245         retv_if(!m_device, -EINVAL);
246
247         bool ret = false;
248         int policy = OP_DEFAULT;
249
250         if (m_sensor) {
251                 policy = m_sensor->set_attribute(ob, attr, value, len);
252                 retv_if(policy <= OP_ERROR, policy);
253         }
254
255         /*
256          * TODO: default policy for attribute?
257         if (policy == OP_DEFAULT) {
258                 if (observer_count() > 1)
259                         return OP_SUCCESS;
260         }
261         */
262
263         ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
264
265         return (ret ? OP_SUCCESS : OP_ERROR);
266 }
267
268 int physical_sensor_handler::flush(sensor_observer *ob)
269 {
270         retv_if(!m_device, -EINVAL);
271         int ret = false;
272
273         if (m_sensor) {
274                 ret = m_sensor->flush(ob);
275                 retv_if(ret <= OP_ERROR, ret);
276         }
277
278         ret = m_device->flush(m_hal_id);
279
280         return (ret ? OP_SUCCESS : OP_ERROR);
281 }
282
283 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
284 {
285         retv_if(!m_device, -EINVAL);
286
287         int remains = m_device->get_data(m_hal_id, data, len);
288         retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");
289
290         return remains;
291 }
292