Update for attribute changed callback and attribute getter
[platform/core/system/sensord.git] / src / server / sensor_listener_proxy.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_listener_proxy.h"
21
22 #include <channel.h>
23 #include <message.h>
24 #include <command_types.h>
25 #include <sensor_log.h>
26 #include <sensor_types.h>
27
28 #include "sensor_handler.h"
29 #include "sensor_policy_monitor.h"
30
31 using namespace sensor;
32
33 sensor_listener_proxy::sensor_listener_proxy(uint32_t id,
34                         std::string uri, sensor_manager *manager, ipc::channel *ch)
35 : m_id(id)
36 , m_uri(uri)
37 , m_manager(manager)
38 , m_ch(ch)
39 , m_started(false)
40 , m_passive(false)
41 , m_pause_policy(SENSORD_PAUSE_ALL)
42 , m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED)
43 , m_last_accuracy(SENSOR_ACCURACY_UNDEFINED)
44 , m_need_to_notify_attribute_changed(false)
45 {
46         sensor_policy_monitor::get_instance().add_listener(this);
47 }
48
49 sensor_listener_proxy::~sensor_listener_proxy()
50 {
51         sensor_policy_monitor::get_instance().remove_listener(this);
52         stop();
53 }
54
55 uint32_t sensor_listener_proxy::get_id(void)
56 {
57         return m_id;
58 }
59
60 int sensor_listener_proxy::update(const char *uri, std::shared_ptr<ipc::message> msg)
61 {
62         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
63
64         update_event(msg);
65         update_accuracy(msg);
66
67         return OP_CONTINUE;
68 }
69
70 int sensor_listener_proxy::on_attribute_changed(std::shared_ptr<ipc::message> msg)
71 {
72         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
73         _I("Proxy[%zu] call on_attribute_changed\n", get_id());
74         m_ch->send(msg);
75         return OP_CONTINUE;
76 }
77
78 void sensor_listener_proxy::update_event(std::shared_ptr<ipc::message> msg)
79 {
80         /* TODO: check axis orientation */
81         msg->header()->type = CMD_LISTENER_EVENT;
82         msg->header()->err = OP_SUCCESS;
83
84         m_ch->send(msg);
85 }
86
87 void sensor_listener_proxy::update_accuracy(std::shared_ptr<ipc::message> msg)
88 {
89         sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
90
91         if (data->accuracy == m_last_accuracy)
92                 return;
93
94         m_last_accuracy = data->accuracy;
95
96         sensor_data_t acc_data;
97         acc_data.accuracy = m_last_accuracy;
98
99         auto acc_msg = ipc::message::create();
100
101         retm_if(!acc_msg, "Failed to allocate memory");
102
103         acc_msg->header()->type = CMD_LISTENER_ACC_EVENT;
104         acc_msg->header()->err = OP_SUCCESS;
105         acc_msg->enclose(&acc_data, sizeof(acc_data));
106
107         m_ch->send(acc_msg);
108 }
109
110 int sensor_listener_proxy::start(bool policy)
111 {
112         int ret;
113         sensor_handler *sensor = m_manager->get_sensor(m_uri);
114         retv_if(!sensor, -EINVAL);
115         retvm_if(m_started && !policy, OP_SUCCESS, "Sensor is already started");
116
117         _D("Listener[%d] try to start", get_id());
118
119         ret = sensor->start(this);
120         retv_if(ret < 0, OP_ERROR);
121
122         /* m_started is changed only when it is explicitly called by user,
123          * not automatically determined by any pause policy. */
124         if (policy)
125                 return OP_SUCCESS;
126
127         m_started = true;
128         return OP_SUCCESS;
129 }
130
131 int sensor_listener_proxy::stop(bool policy)
132 {
133         sensor_handler *sensor = m_manager->get_sensor(m_uri);
134         retv_if(!sensor, -EINVAL);
135         retvm_if(!m_started && !policy, OP_SUCCESS, "Sensor is already stopped");
136
137         _D("Listener[%d] try to stop", get_id());
138
139         int ret = sensor->stop(this);
140         retv_if(ret < 0, OP_ERROR);
141
142         /* attributes and m_started are changed only when it is explicitly called by user,
143          * not automatically determined by any policy. */
144         if (policy)
145                 return OP_SUCCESS;
146
147         /* unset attributes */
148         set_interval(POLL_MAX_HZ_MS);
149         delete_batch_latency();
150
151         m_started = false;
152         return OP_SUCCESS;
153 }
154
155 int sensor_listener_proxy::set_interval(int32_t interval)
156 {
157         sensor_handler *sensor = m_manager->get_sensor(m_uri);
158         retv_if(!sensor, -EINVAL);
159
160         _D("Listener[%d] try to set interval[%d]", get_id(), interval);
161
162         int ret = sensor->set_interval(this, interval);
163         apply_sensor_handler_need_to_notify_attribute_changed(sensor);
164
165         return ret;
166 }
167
168 int sensor_listener_proxy::get_interval(int32_t& interval)
169 {
170         sensor_handler *sensor = m_manager->get_sensor(m_uri);
171         retv_if(!sensor, -EINVAL);
172
173         _D("Listener[%d] try to get interval[%d]", get_id());
174
175         return sensor->get_interval(this, interval);
176 }
177
178 int sensor_listener_proxy::set_max_batch_latency(int32_t max_batch_latency)
179 {
180         sensor_handler *sensor = m_manager->get_sensor(m_uri);
181         retv_if(!sensor, -EINVAL);
182
183         _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency);
184         int ret = sensor->set_batch_latency(this, max_batch_latency);
185         apply_sensor_handler_need_to_notify_attribute_changed(sensor);
186
187         return ret;
188 }
189
190 int sensor_listener_proxy::get_max_batch_latency(int32_t& max_batch_latency)
191 {
192         sensor_handler *sensor = m_manager->get_sensor(m_uri);
193         retv_if(!sensor, -EINVAL);
194
195         _D("Listener[%d] try to get max batch latency[%d]", get_id());
196
197         return sensor->get_batch_latency(this, max_batch_latency);
198 }
199
200 int sensor_listener_proxy::delete_batch_latency(void)
201 {
202         sensor_handler *sensor = m_manager->get_sensor(m_uri);
203         retv_if(!sensor, -EINVAL);
204
205         _I("Listener[%d] try to delete batch latency", get_id());
206
207         return sensor->delete_batch_latency(this);
208 }
209
210 int sensor_listener_proxy::set_passive_mode(bool passive)
211 {
212         /* TODO: passive mode */
213         m_passive = passive;
214         return OP_SUCCESS;
215 }
216
217 int sensor_listener_proxy::set_attribute(int32_t attribute, int32_t value)
218 {
219         sensor_handler *sensor = m_manager->get_sensor(m_uri);
220         retv_if(!sensor, -EINVAL);
221
222         _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
223
224         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
225                 if (m_pause_policy != value) {
226                         m_pause_policy = value;
227                         set_need_to_notify_attribute_changed(true);
228                 }
229                 return OP_SUCCESS;
230         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
231                 if (m_axis_orientation != value) {
232                         m_axis_orientation = value;
233                         set_need_to_notify_attribute_changed(true);
234                 }
235                 return OP_SUCCESS;
236         } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
237                 return flush();
238         }
239
240         int ret = sensor->set_attribute(this, attribute, value);
241         apply_sensor_handler_need_to_notify_attribute_changed(sensor);
242
243         return ret;
244 }
245
246 int sensor_listener_proxy::get_attribute(int32_t attribute, int32_t *value)
247 {
248         sensor_handler *sensor = m_manager->get_sensor(m_uri);
249         retv_if(!sensor, -EINVAL);
250
251         _D("Listener[%d] try to get attribute[%d] int", get_id(), attribute);
252
253         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
254                 *value = m_pause_policy;
255                 return OP_SUCCESS;
256         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
257                 *value = m_axis_orientation;
258                 return OP_SUCCESS;
259         } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
260                 return -EINVAL;
261         }
262
263         return sensor->get_attribute(attribute, value);
264 }
265
266 int sensor_listener_proxy::set_attribute(int32_t attribute, const char *value, int len)
267 {
268         sensor_handler *sensor = m_manager->get_sensor(m_uri);
269         retv_if(!sensor, -EINVAL);
270
271         _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
272
273         int ret = sensor->set_attribute(this, attribute, value, len);
274         apply_sensor_handler_need_to_notify_attribute_changed(sensor);
275
276         return ret;
277 }
278
279 int sensor_listener_proxy::get_attribute(int32_t attribute, char **value, int *len)
280 {
281         sensor_handler *sensor = m_manager->get_sensor(m_uri);
282         retv_if(!sensor, -EINVAL);
283
284         _D("Listener[%d] try to get attribute str[%d]", get_id(), attribute);
285
286         return sensor->get_attribute(attribute, value, len);
287 }
288
289 int sensor_listener_proxy::flush(void)
290 {
291         sensor_handler *sensor = m_manager->get_sensor(m_uri);
292         retv_if(!sensor, -EINVAL);
293
294         return sensor->flush(this);
295 }
296
297 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
298 {
299         sensor_handler *sensor = m_manager->get_sensor(m_uri);
300         retv_if(!sensor, -EINVAL);
301
302         return sensor->get_cache(data, len);
303 }
304
305 std::string sensor_listener_proxy::get_required_privileges(void)
306 {
307         sensor_handler *sensor = m_manager->get_sensor(m_uri);
308         retv_if(!sensor, "");
309
310         sensor_info info = sensor->get_sensor_info();
311         return info.get_privilege();
312 }
313
314 void sensor_listener_proxy::on_policy_changed(int policy, int value)
315 {
316         ret_if(m_started == false);
317         ret_if(policy != SENSORD_ATTRIBUTE_PAUSE_POLICY);
318         ret_if(m_pause_policy == SENSORD_PAUSE_NONE);
319
320         _D("power_save_state[%d], listener[%d] pause policy[%d]",
321                         value, get_id(), m_pause_policy);
322
323         if (value & m_pause_policy)
324                 stop(true);
325         if (!(value & m_pause_policy))
326                 start(true);
327 }
328
329 bool sensor_listener_proxy::notify_attribute_changed(int32_t attribute, int32_t value)
330 {
331         sensor_handler *sensor = m_manager->get_sensor(m_uri);
332         retv_if(!sensor, -EINVAL);
333
334         return sensor->notify_attribute_changed(m_id, attribute, value);
335 }
336
337 bool sensor_listener_proxy::notify_attribute_changed(int attribute, const char *value, int len)
338 {
339         sensor_handler *sensor = m_manager->get_sensor(m_uri);
340         retv_if(!sensor, -EINVAL);
341
342         return sensor->notify_attribute_changed(m_id, attribute, value, len);
343 }
344
345 bool sensor_listener_proxy::need_to_notify_attribute_changed()
346 {
347         return m_need_to_notify_attribute_changed;
348 }
349
350 void sensor_listener_proxy::set_need_to_notify_attribute_changed(bool value)
351 {
352         m_need_to_notify_attribute_changed = value;
353 }
354
355 void sensor_listener_proxy::apply_sensor_handler_need_to_notify_attribute_changed(sensor_handler *handler)
356 {
357         set_need_to_notify_attribute_changed(handler->need_to_notify_attribute_changed());
358         handler->set_need_to_notify_attribute_changed(false);
359 }