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)
45 sensor_policy_monitor::get_instance().add_listener(this);
48 sensor_listener_proxy::~sensor_listener_proxy()
50 sensor_policy_monitor::get_instance().remove_listener(this);
54 uint32_t sensor_listener_proxy::get_id(void)
59 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
61 retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
69 void sensor_listener_proxy::update_event(ipc::message *msg)
71 /* TODO: check axis orientation */
72 msg->header()->type = CMD_LISTENER_EVENT;
73 msg->header()->err = OP_SUCCESS;
78 void sensor_listener_proxy::update_accuracy(ipc::message *msg)
80 sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
82 if (data->accuracy == m_last_accuracy)
85 m_last_accuracy = data->accuracy;
87 sensor_data_t acc_data;
88 acc_data.accuracy = m_last_accuracy;
90 ipc::message *acc_msg = new(std::nothrow) ipc::message();
91 retm_if(!acc_msg, "Failed to allocate memory");
93 acc_msg->header()->type = CMD_LISTENER_ACC_EVENT;
94 acc_msg->header()->err = OP_SUCCESS;
95 acc_msg->enclose(&acc_data, sizeof(acc_data));
100 int sensor_listener_proxy::start(bool policy)
103 sensor_handler *sensor = m_manager->get_sensor(m_uri);
104 retv_if(!sensor, -EINVAL);
105 retvm_if(m_started && !policy, OP_SUCCESS, "Sensor is already started");
107 _D("Listener[%d] try to start", get_id());
109 ret = sensor->start(this);
110 retv_if (ret < 0, OP_ERROR);
112 /* m_started is changed only when it is explicitly called by user,
113 * not automatically determined by any pause policy. */
121 int sensor_listener_proxy::stop(bool policy)
123 sensor_handler *sensor = m_manager->get_sensor(m_uri);
124 retv_if(!sensor, -EINVAL);
125 retvm_if(!m_started && !policy, OP_SUCCESS, "Sensor is already stopped");
127 _D("Listener[%d] try to stop", get_id());
129 int ret = sensor->stop(this);
130 retv_if(ret < 0, OP_ERROR);
132 /* attributes and m_started are changed only when it is explicitly called by user,
133 * not automatically determined by any policy. */
137 /* unset attributes */
138 set_interval(POLL_MAX_HZ_MS);
139 delete_batch_latency();
145 int sensor_listener_proxy::set_interval(unsigned int interval)
147 sensor_handler *sensor = m_manager->get_sensor(m_uri);
148 retv_if(!sensor, -EINVAL);
150 _D("Listener[%d] try to set interval[%d]", get_id(), interval);
152 return sensor->set_interval(this, interval);
155 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
157 sensor_handler *sensor = m_manager->get_sensor(m_uri);
158 retv_if(!sensor, -EINVAL);
160 _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency);
162 return sensor->set_batch_latency(this, max_batch_latency);
165 int sensor_listener_proxy::delete_batch_latency(void)
167 sensor_handler *sensor = m_manager->get_sensor(m_uri);
168 retv_if(!sensor, -EINVAL);
170 _I("Listener[%d] try to delete batch latency", get_id());
172 return sensor->delete_batch_latency(this);
175 int sensor_listener_proxy::set_passive_mode(bool passive)
177 /* TODO: passive mode */
182 int sensor_listener_proxy::set_attribute(int attribute, int value)
184 sensor_handler *sensor = m_manager->get_sensor(m_uri);
185 retv_if(!sensor, -EINVAL);
187 _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
189 if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
190 m_pause_policy = value;
192 } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
193 m_axis_orientation = value;
195 } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
199 return sensor->set_attribute(this, attribute, value);
202 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
204 sensor_handler *sensor = m_manager->get_sensor(m_uri);
205 retv_if(!sensor, -EINVAL);
207 _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
209 return sensor->set_attribute(this, attribute, value, len);
212 int sensor_listener_proxy::flush(void)
214 sensor_handler *sensor = m_manager->get_sensor(m_uri);
215 retv_if(!sensor, -EINVAL);
217 return sensor->flush(this);
220 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
222 sensor_handler *sensor = m_manager->get_sensor(m_uri);
223 retv_if(!sensor, -EINVAL);
225 return sensor->get_cache(data, len);
228 std::string sensor_listener_proxy::get_required_privileges(void)
230 sensor_handler *sensor = m_manager->get_sensor(m_uri);
231 retv_if(!sensor, "");
233 sensor_info info = sensor->get_sensor_info();
234 return info.get_privilege();
237 void sensor_listener_proxy::on_policy_changed(int policy, int value)
239 ret_if(m_started == false);
240 ret_if(policy != SENSORD_ATTRIBUTE_PAUSE_POLICY);
241 ret_if(m_pause_policy == SENSORD_PAUSE_NONE);
243 _D("power_save_state[%d], listener[%d] pause policy[%d]",
244 value, get_id(), m_pause_policy);
246 if (value & m_pause_policy)
248 if (!(value & m_pause_policy))