4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "sensor_listener_proxy.h"
24 #include <command_types.h>
25 #include <sensor_log.h>
26 #include <sensor_types.h>
28 #include "sensor_handler.h"
29 #include "sensor_policy_monitor.h"
31 using namespace sensor;
33 sensor_listener_proxy::sensor_listener_proxy(uint32_t id,
34 std::string uri, sensor_manager *manager, ipc::channel *ch)
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)
46 sensor_policy_monitor::get_instance().add_listener(this);
49 sensor_listener_proxy::~sensor_listener_proxy()
51 sensor_policy_monitor::get_instance().remove_listener(this);
55 uint32_t sensor_listener_proxy::get_id(void)
60 int sensor_listener_proxy::update(const char *uri, std::shared_ptr<ipc::message> msg)
62 retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
70 int sensor_listener_proxy::on_attribute_changed(std::shared_ptr<ipc::message> msg)
72 retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
73 _I("Proxy[%zu] call on_attribute_changed\n", get_id());
78 void sensor_listener_proxy::update_event(std::shared_ptr<ipc::message> msg)
80 /* TODO: check axis orientation */
81 msg->header()->type = CMD_LISTENER_EVENT;
82 msg->header()->err = OP_SUCCESS;
87 void sensor_listener_proxy::update_accuracy(std::shared_ptr<ipc::message> msg)
89 sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
91 if (data->accuracy == m_last_accuracy)
94 m_last_accuracy = data->accuracy;
96 sensor_data_t acc_data;
97 acc_data.accuracy = m_last_accuracy;
99 auto acc_msg = ipc::message::create();
101 retm_if(!acc_msg, "Failed to allocate memory");
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));
110 int sensor_listener_proxy::start(bool policy)
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");
117 _D("Listener[%d] try to start", get_id());
119 ret = sensor->start(this);
120 retv_if(ret < 0, OP_ERROR);
122 /* m_started is changed only when it is explicitly called by user,
123 * not automatically determined by any pause policy. */
131 int sensor_listener_proxy::stop(bool policy)
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");
137 _D("Listener[%d] try to stop", get_id());
139 int ret = sensor->stop(this);
140 retv_if(ret < 0, OP_ERROR);
142 /* attributes and m_started are changed only when it is explicitly called by user,
143 * not automatically determined by any policy. */
147 /* unset attributes */
148 set_interval(POLL_MAX_HZ_MS);
149 delete_batch_latency();
155 int sensor_listener_proxy::set_interval(int32_t interval)
157 sensor_handler *sensor = m_manager->get_sensor(m_uri);
158 retv_if(!sensor, -EINVAL);
160 _D("Listener[%d] try to set interval[%d]", get_id(), interval);
162 int ret = sensor->set_interval(this, interval);
163 apply_sensor_handler_need_to_notify_attribute_changed(sensor);
168 int sensor_listener_proxy::get_interval(int32_t& interval)
170 sensor_handler *sensor = m_manager->get_sensor(m_uri);
171 retv_if(!sensor, -EINVAL);
173 _D("Listener[%d] try to get interval[%d]", get_id());
175 return sensor->get_interval(this, interval);
178 int sensor_listener_proxy::set_max_batch_latency(int32_t max_batch_latency)
180 sensor_handler *sensor = m_manager->get_sensor(m_uri);
181 retv_if(!sensor, -EINVAL);
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);
190 int sensor_listener_proxy::get_max_batch_latency(int32_t& max_batch_latency)
192 sensor_handler *sensor = m_manager->get_sensor(m_uri);
193 retv_if(!sensor, -EINVAL);
195 _D("Listener[%d] try to get max batch latency[%d]", get_id());
197 return sensor->get_batch_latency(this, max_batch_latency);
200 int sensor_listener_proxy::delete_batch_latency(void)
202 sensor_handler *sensor = m_manager->get_sensor(m_uri);
203 retv_if(!sensor, -EINVAL);
205 _I("Listener[%d] try to delete batch latency", get_id());
207 return sensor->delete_batch_latency(this);
210 int sensor_listener_proxy::set_passive_mode(bool passive)
212 /* TODO: passive mode */
217 int sensor_listener_proxy::set_attribute(int32_t attribute, int32_t value)
219 sensor_handler *sensor = m_manager->get_sensor(m_uri);
220 retv_if(!sensor, -EINVAL);
222 _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
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);
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);
236 } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
240 int ret = sensor->set_attribute(this, attribute, value);
241 apply_sensor_handler_need_to_notify_attribute_changed(sensor);
246 int sensor_listener_proxy::get_attribute(int32_t attribute, int32_t *value)
248 sensor_handler *sensor = m_manager->get_sensor(m_uri);
249 retv_if(!sensor, -EINVAL);
251 _D("Listener[%d] try to get attribute[%d] int", get_id(), attribute);
253 if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
254 *value = m_pause_policy;
256 } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
257 *value = m_axis_orientation;
259 } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
263 return sensor->get_attribute(attribute, value);
266 int sensor_listener_proxy::set_attribute(int32_t attribute, const char *value, int len)
268 sensor_handler *sensor = m_manager->get_sensor(m_uri);
269 retv_if(!sensor, -EINVAL);
271 _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
273 int ret = sensor->set_attribute(this, attribute, value, len);
274 apply_sensor_handler_need_to_notify_attribute_changed(sensor);
279 int sensor_listener_proxy::get_attribute(int32_t attribute, char **value, int *len)
281 sensor_handler *sensor = m_manager->get_sensor(m_uri);
282 retv_if(!sensor, -EINVAL);
284 _D("Listener[%d] try to get attribute str[%d]", get_id(), attribute);
286 return sensor->get_attribute(attribute, value, len);
289 int sensor_listener_proxy::flush(void)
291 sensor_handler *sensor = m_manager->get_sensor(m_uri);
292 retv_if(!sensor, -EINVAL);
294 return sensor->flush(this);
297 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
299 sensor_handler *sensor = m_manager->get_sensor(m_uri);
300 retv_if(!sensor, -EINVAL);
302 return sensor->get_cache(data, len);
305 std::string sensor_listener_proxy::get_required_privileges(void)
307 sensor_handler *sensor = m_manager->get_sensor(m_uri);
308 retv_if(!sensor, "");
310 sensor_info info = sensor->get_sensor_info();
311 return info.get_privilege();
314 void sensor_listener_proxy::on_policy_changed(int policy, int value)
316 ret_if(m_started == false);
317 ret_if(policy != SENSORD_ATTRIBUTE_PAUSE_POLICY);
318 ret_if(m_pause_policy == SENSORD_PAUSE_NONE);
320 _D("power_save_state[%d], listener[%d] pause policy[%d]",
321 value, get_id(), m_pause_policy);
323 if (value & m_pause_policy)
325 if (!(value & m_pause_policy))
329 bool sensor_listener_proxy::notify_attribute_changed(int32_t attribute, int32_t value)
331 sensor_handler *sensor = m_manager->get_sensor(m_uri);
332 retv_if(!sensor, -EINVAL);
334 return sensor->notify_attribute_changed(m_id, attribute, value);
337 bool sensor_listener_proxy::notify_attribute_changed(int attribute, const char *value, int len)
339 sensor_handler *sensor = m_manager->get_sensor(m_uri);
340 retv_if(!sensor, -EINVAL);
342 return sensor->notify_attribute_changed(m_id, attribute, value, len);
345 bool sensor_listener_proxy::need_to_notify_attribute_changed()
347 return m_need_to_notify_attribute_changed;
350 void sensor_listener_proxy::set_need_to_notify_attribute_changed(bool value)
352 m_need_to_notify_attribute_changed = value;
355 void sensor_listener_proxy::apply_sensor_handler_need_to_notify_attribute_changed(sensor_handler *handler)
357 set_need_to_notify_attribute_changed(handler->need_to_notify_attribute_changed());
358 handler->set_need_to_notify_attribute_changed(false);