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"
30 using namespace sensor;
32 sensor_listener_proxy::sensor_listener_proxy(uint32_t id,
33 std::string uri, sensor_manager *manager, ipc::channel *ch)
39 , m_pause_policy(SENSORD_PAUSE_ALL)
40 , m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED)
41 , m_last_accuracy(SENSOR_ACCURACY_UNDEFINED)
45 sensor_listener_proxy::~sensor_listener_proxy()
50 uint32_t sensor_listener_proxy::get_id(void)
55 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
57 retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
65 void sensor_listener_proxy::update_event(ipc::message *msg)
67 /* TODO: check axis orientation */
68 msg->header()->type = CMD_LISTENER_EVENT;
69 msg->header()->err = OP_SUCCESS;
74 void sensor_listener_proxy::update_accuracy(ipc::message *msg)
76 sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
78 if (data->accuracy == m_last_accuracy)
81 m_last_accuracy = data->accuracy;
83 sensor_data_t acc_data;
84 acc_data.accuracy = m_last_accuracy;
86 ipc::message *acc_msg = new(std::nothrow) ipc::message();
87 retm_if(!acc_msg, "Failed to allocate memory");
89 acc_msg->header()->type = CMD_LISTENER_ACC_EVENT;
90 acc_msg->header()->err = OP_SUCCESS;
91 acc_msg->enclose(&acc_data, sizeof(acc_data));
96 int sensor_listener_proxy::start(void)
98 sensor_handler *sensor = m_manager->get_sensor(m_uri);
99 retv_if(!sensor, -EINVAL);
101 _D("Listener[%d] try to start", get_id());
103 /* TODO: listen pause policy */
104 return sensor->start(this);
107 int sensor_listener_proxy::stop(void)
109 sensor_handler *sensor = m_manager->get_sensor(m_uri);
110 retv_if(!sensor, -EINVAL);
112 /* TODO: listen pause policy */
114 _D("Listener[%d] try to stop", get_id());
116 int ret = sensor->stop(this);
117 retv_if(ret < 0, OP_ERROR);
119 /* unset attributes */
120 set_interval(POLL_1HZ_MS);
121 set_max_batch_latency(0);
126 int sensor_listener_proxy::set_interval(unsigned int interval)
128 sensor_handler *sensor = m_manager->get_sensor(m_uri);
129 retv_if(!sensor, -EINVAL);
131 _D("Listener[%d] try to set interval[%d]", get_id(), interval);
133 return sensor->set_interval(this, interval);
136 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
138 sensor_handler *sensor = m_manager->get_sensor(m_uri);
139 retv_if(!sensor, -EINVAL);
141 _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency);
143 return sensor->set_batch_latency(this, max_batch_latency);
146 int sensor_listener_proxy::set_passive_mode(bool passive)
148 /* TODO: passive mode */
153 int sensor_listener_proxy::set_attribute(int attribute, int value)
155 sensor_handler *sensor = m_manager->get_sensor(m_uri);
156 retv_if(!sensor, -EINVAL);
158 _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
160 if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
161 m_pause_policy = value;
163 } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
164 m_axis_orientation = value;
168 return sensor->set_attribute(this, attribute, value);
171 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
173 sensor_handler *sensor = m_manager->get_sensor(m_uri);
174 retv_if(!sensor, -EINVAL);
176 _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
178 return sensor->set_attribute(this, attribute, value, len);
181 int sensor_listener_proxy::flush(void)
183 sensor_handler *sensor = m_manager->get_sensor(m_uri);
184 retv_if(!sensor, -EINVAL);
186 return sensor->flush(this);
189 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
191 sensor_handler *sensor = m_manager->get_sensor(m_uri);
192 retv_if(!sensor, -EINVAL);
194 return sensor->get_cache(data, len);
197 std::string sensor_listener_proxy::get_required_privileges(void)
199 sensor_handler *sensor = m_manager->get_sensor(m_uri);
200 retv_if(!sensor, "");
202 sensor_info info = sensor->get_sensor_info();
203 return info.get_privilege();