Integrate internal fixes
[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 {
45         sensor_policy_monitor::get_instance().add_listener(this);
46 }
47
48 sensor_listener_proxy::~sensor_listener_proxy()
49 {
50         sensor_policy_monitor::get_instance().remove_listener(this);
51         stop();
52 }
53
54 uint32_t sensor_listener_proxy::get_id(void)
55 {
56         return m_id;
57 }
58
59 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
60 {
61         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
62
63         update_event(msg);
64         update_accuracy(msg);
65
66         return OP_CONTINUE;
67 }
68
69 void sensor_listener_proxy::update_event(ipc::message *msg)
70 {
71         /* TODO: check axis orientation */
72         msg->header()->type = CMD_LISTENER_EVENT;
73         msg->header()->err = OP_SUCCESS;
74
75         m_ch->send(msg);
76 }
77
78 void sensor_listener_proxy::update_accuracy(ipc::message *msg)
79 {
80         sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
81
82         if (data->accuracy == m_last_accuracy)
83                 return;
84
85         m_last_accuracy = data->accuracy;
86
87         sensor_data_t acc_data;
88         acc_data.accuracy = m_last_accuracy;
89
90         ipc::message *acc_msg = new(std::nothrow) ipc::message();
91         retm_if(!acc_msg, "Failed to allocate memory");
92
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));
96
97         m_ch->send(acc_msg);
98 }
99
100 int sensor_listener_proxy::start(bool policy)
101 {
102         int ret;
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");
106
107         _D("Listener[%d] try to start", get_id());
108
109         ret = sensor->start(this);
110         retv_if (ret < 0, OP_ERROR);
111
112         /* m_started is changed only when it is explicitly called by user,
113          * not automatically determined by any pause policy. */
114         if (policy)
115                 return OP_SUCCESS;
116
117         m_started = true;
118         return OP_SUCCESS;
119 }
120
121 int sensor_listener_proxy::stop(bool policy)
122 {
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");
126
127         _D("Listener[%d] try to stop", get_id());
128
129         int ret = sensor->stop(this);
130         retv_if(ret < 0, OP_ERROR);
131
132         /* attributes and m_started are changed only when it is explicitly called by user,
133          * not automatically determined by any policy. */
134         if (policy)
135                 return OP_SUCCESS;
136
137         /* unset attributes */
138         set_interval(POLL_MAX_HZ_MS);
139         delete_batch_latency();
140
141         m_started = false;
142         return OP_SUCCESS;
143 }
144
145 int sensor_listener_proxy::set_interval(unsigned int interval)
146 {
147         sensor_handler *sensor = m_manager->get_sensor(m_uri);
148         retv_if(!sensor, -EINVAL);
149
150         _D("Listener[%d] try to set interval[%d]", get_id(), interval);
151
152         return sensor->set_interval(this, interval);
153 }
154
155 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
156 {
157         sensor_handler *sensor = m_manager->get_sensor(m_uri);
158         retv_if(!sensor, -EINVAL);
159
160         _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency);
161
162         return sensor->set_batch_latency(this, max_batch_latency);
163 }
164
165 int sensor_listener_proxy::delete_batch_latency(void)
166 {
167         sensor_handler *sensor = m_manager->get_sensor(m_uri);
168         retv_if(!sensor, -EINVAL);
169
170         _I("Listener[%d] try to delete batch latency", get_id());
171
172         return sensor->delete_batch_latency(this);
173 }
174
175 int sensor_listener_proxy::set_passive_mode(bool passive)
176 {
177         /* TODO: passive mode */
178         m_passive = passive;
179         return OP_SUCCESS;
180 }
181
182 int sensor_listener_proxy::set_attribute(int attribute, int value)
183 {
184         sensor_handler *sensor = m_manager->get_sensor(m_uri);
185         retv_if(!sensor, -EINVAL);
186
187         _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
188
189         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
190                 m_pause_policy = value;
191                 return OP_SUCCESS;
192         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
193                 m_axis_orientation = value;
194                 return OP_SUCCESS;
195         } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) {
196                 return flush();
197         }
198
199         return sensor->set_attribute(this, attribute, value);
200 }
201
202 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
203 {
204         sensor_handler *sensor = m_manager->get_sensor(m_uri);
205         retv_if(!sensor, -EINVAL);
206
207         _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
208
209         return sensor->set_attribute(this, attribute, value, len);
210 }
211
212 int sensor_listener_proxy::flush(void)
213 {
214         sensor_handler *sensor = m_manager->get_sensor(m_uri);
215         retv_if(!sensor, -EINVAL);
216
217         return sensor->flush(this);
218 }
219
220 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
221 {
222         sensor_handler *sensor = m_manager->get_sensor(m_uri);
223         retv_if(!sensor, -EINVAL);
224
225         return sensor->get_cache(data, len);
226 }
227
228 std::string sensor_listener_proxy::get_required_privileges(void)
229 {
230         sensor_handler *sensor = m_manager->get_sensor(m_uri);
231         retv_if(!sensor, "");
232
233         sensor_info info = sensor->get_sensor_info();
234         return info.get_privilege();
235 }
236
237 void sensor_listener_proxy::on_policy_changed(int policy, int value)
238 {
239         ret_if(m_started == false);
240         ret_if(policy != SENSORD_ATTRIBUTE_PAUSE_POLICY);
241         ret_if(m_pause_policy == SENSORD_PAUSE_NONE);
242
243         _D("power_save_state[%d], listener[%d] pause policy[%d]",
244                         value, get_id(), m_pause_policy);
245
246         if (value & m_pause_policy)
247                 stop(true);
248         if (!(value & m_pause_policy))
249                 start(true);
250 }