Update for attribute changed callback and attribute getter
[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 {
37 }
38
39 physical_sensor_handler::~physical_sensor_handler()
40 {
41 }
42
43 const sensor_info &physical_sensor_handler::get_sensor_info(void)
44 {
45         return m_info;
46 }
47
48 int physical_sensor_handler::get_hal_id(void)
49 {
50         return m_hal_id;
51 }
52
53 int physical_sensor_handler::get_poll_fd(void)
54 {
55         retv_if(!m_device, -EINVAL);
56
57         return m_device->get_poll_fd();
58 }
59
60 int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
61 {
62         retv_if(!m_device, -EINVAL);
63
64         int size;
65         uint32_t *_ids;
66
67         size = m_device->read_fd(&_ids);
68         retv_if(size == 0, -ENODATA);
69
70         for (int i = 0; i < size; ++i)
71                 ids.push_back(_ids[i]);
72
73         return OP_SUCCESS;
74 }
75
76 int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
77 {
78         retv_if(!m_device, -EINVAL);
79
80         if (m_sensor) {
81                 int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
82                 retv_if(ret <= OP_ERROR, ret);
83         }
84
85         return OP_SUCCESS;
86 }
87 int physical_sensor_handler::start(sensor_observer *ob)
88 {
89         retv_if(!m_device, -EINVAL);
90
91         bool ret;
92         int policy = OP_DEFAULT;
93
94         ret = add_observer(ob);
95         retvm_if(!ret, OP_SUCCESS, "Listener is already added");
96
97         if (m_sensor) {
98                 policy = m_sensor->start(ob);
99                 if (policy <= OP_ERROR) {
100                         remove_observer(ob);
101                         return policy;
102                 }
103         }
104
105         if (policy == OP_DEFAULT) {
106                 if (observer_count() > 1)
107                         return OP_SUCCESS; /* already started */
108         }
109
110         _I("Started[%s]", m_info.get_uri().c_str());
111
112         return m_device->enable(m_hal_id);
113 }
114
115 int physical_sensor_handler::stop(sensor_observer *ob)
116 {
117         retv_if(!m_device, -EINVAL);
118
119         int policy = OP_DEFAULT;
120
121         if (m_sensor) {
122                 policy = m_sensor->stop(ob);
123                 retv_if(policy <= OP_ERROR, policy);
124         }
125
126         remove_observer(ob);
127
128         if (policy == OP_DEFAULT) {
129                 if (observer_count() >= 1)
130                         return OP_SUCCESS; /* already stopped */
131         }
132
133         _I("Stopped[%s]", m_info.get_uri().c_str());
134
135         return m_device->disable(m_hal_id);
136 }
137
138 int physical_sensor_handler::get_min_interval(void)
139 {
140         int interval;
141         std::vector<int> temp;
142
143         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
144                 if (it->second > 0)
145                     temp.push_back(it->second);
146
147         if (temp.empty())
148                 return m_info.get_min_interval();
149
150         interval = *std::min_element(temp.begin(), temp.end());
151
152         if (interval < m_info.get_min_interval())
153                 return m_info.get_min_interval();
154
155         return interval;
156 }
157
158 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
159 {
160         retv_if(!m_device, -EINVAL);
161
162         bool ret = false;
163         int32_t cur_interval = interval;
164         int policy = OP_DEFAULT;
165
166         if (m_sensor) {
167                 policy = m_sensor->set_interval(ob, cur_interval);
168                 retv_if(policy <= OP_ERROR, policy);
169         }
170
171         m_interval_map[ob] = cur_interval;
172
173         if (policy == OP_DEFAULT)
174                 cur_interval = get_min_interval();
175
176         retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
177
178         ret = m_device->set_interval(m_hal_id, cur_interval);
179
180         update_prev_interval(cur_interval);
181
182         return (ret ? OP_SUCCESS : OP_ERROR);
183 }
184
185 int physical_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval)
186 {
187         retv_if(!m_device, -EINVAL);
188         interval = m_prev_interval;
189         return OP_SUCCESS;
190 }
191
192 int physical_sensor_handler::get_min_batch_latency(void)
193 {
194         int batch_latency;
195         std::vector<int> temp;
196
197         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
198                 temp.push_back(it->second);
199
200         if (temp.empty())
201                 return -1;
202
203         batch_latency = *std::min_element(temp.begin(), temp.end());
204
205         return batch_latency;
206 }
207
208 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
209 {
210         retv_if(!m_device, -EINVAL);
211
212         bool ret = false;
213         int32_t cur_latency = latency;
214         int policy = OP_DEFAULT;
215
216         if (m_sensor) {
217                 policy = m_sensor->set_batch_latency(ob, latency);
218                 retv_if(policy <= OP_ERROR, policy);
219         }
220
221         m_batch_latency_map[ob] = cur_latency;
222
223         if (policy == OP_DEFAULT)
224                 cur_latency = get_min_batch_latency();
225
226         retv_if(m_prev_latency == cur_latency, OP_SUCCESS);
227
228         ret = m_device->set_batch_latency(m_hal_id, cur_latency);
229
230         update_prev_latency(cur_latency);
231
232         return (ret ? OP_SUCCESS : OP_ERROR);
233 }
234
235 int physical_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency)
236 {
237         retv_if(!m_device, -EINVAL);
238         latency = m_prev_latency;
239         return OP_SUCCESS;
240 }
241
242 int physical_sensor_handler::delete_batch_latency(sensor_observer *ob)
243 {
244         bool ret = false;
245         int policy = OP_DEFAULT;
246         int32_t latency;
247
248         m_batch_latency_map.erase(ob);
249
250         latency = get_min_batch_latency();
251         retv_if(m_prev_latency == latency, OP_SUCCESS);
252
253         ret = m_device->set_batch_latency(m_hal_id, latency);
254
255         m_prev_latency = latency;
256
257         return (ret ? OP_SUCCESS : OP_ERROR);
258 }
259
260 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
261 {
262         retv_if(!m_device, -EINVAL);
263
264         bool ret = false;
265         int policy = OP_DEFAULT;
266
267         if (m_sensor) {
268                 policy = m_sensor->set_attribute(ob, attr, value);
269                 retv_if(policy <= OP_ERROR, policy);
270         }
271
272         /*
273          * TODO: default policy for attribute?
274         if (policy == OP_DEFAULT) {
275                 if (observer_count() > 1)
276                         return OP_SUCCESS;
277         }
278         */
279
280         ret = m_device->set_attribute_int(m_hal_id, attr, value);
281
282         if (ret) {
283                 update_attribute(attr, value);
284         }
285         return (ret ? OP_SUCCESS : OP_ERROR);
286 }
287
288 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
289 {
290         retv_if(!m_device, -EINVAL);
291
292         bool ret = false;
293         int policy = OP_DEFAULT;
294
295         if (m_sensor) {
296                 policy = m_sensor->set_attribute(ob, attr, value, len);
297                 retv_if(policy <= OP_ERROR, policy);
298         }
299
300         /*
301          * TODO: default policy for attribute?
302         if (policy == OP_DEFAULT) {
303                 if (observer_count() > 1)
304                         return OP_SUCCESS;
305         }
306         */
307
308         ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
309
310         if (ret) {
311                 update_attribute(attr, value, len);
312         }
313
314         return (ret ? OP_SUCCESS : OP_ERROR);
315 }
316
317 int physical_sensor_handler::flush(sensor_observer *ob)
318 {
319         retv_if(!m_device, -EINVAL);
320         int ret = false;
321
322         if (m_sensor) {
323                 ret = m_sensor->flush(ob);
324                 retv_if(ret <= OP_ERROR, ret);
325         }
326
327         ret = m_device->flush(m_hal_id);
328
329         return (ret ? OP_SUCCESS : OP_ERROR);
330 }
331
332 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
333 {
334         retv_if(!m_device, -EINVAL);
335
336         int remains = m_device->get_data(m_hal_id, data, len);
337         retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");
338
339         return remains;
340 }
341