Revert "Coverity issues Fix"
[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
30 using namespace sensor;
31
32 sensor_listener_proxy::sensor_listener_proxy(uint32_t id,
33                         std::string uri, sensor_manager *manager, ipc::channel *ch)
34 : m_id(id)
35 , m_uri(uri)
36 , m_manager(manager)
37 , m_ch(ch)
38 , m_passive(false)
39 , m_pause_policy(SENSORD_PAUSE_ALL)
40 , m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED)
41 , m_last_accuracy(SENSOR_ACCURACY_UNDEFINED)
42 {
43 }
44
45 sensor_listener_proxy::~sensor_listener_proxy()
46 {
47         stop();
48 }
49
50 uint32_t sensor_listener_proxy::get_id(void)
51 {
52         return m_id;
53 }
54
55 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
56 {
57         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
58
59         update_event(msg);
60         update_accuracy(msg);
61
62         return OP_CONTINUE;
63 }
64
65 void sensor_listener_proxy::update_event(ipc::message *msg)
66 {
67         /* TODO: check axis orientation */
68         msg->header()->type = CMD_LISTENER_EVENT;
69         msg->header()->err = OP_SUCCESS;
70
71         m_ch->send(msg);
72 }
73
74 void sensor_listener_proxy::update_accuracy(ipc::message *msg)
75 {
76         sensor_data_t *data = reinterpret_cast<sensor_data_t *>(msg->body());
77
78         if (data->accuracy == m_last_accuracy)
79                 return;
80
81         m_last_accuracy = data->accuracy;
82
83         sensor_data_t acc_data;
84         acc_data.accuracy = m_last_accuracy;
85
86         ipc::message *acc_msg = new(std::nothrow) ipc::message();
87         retm_if(!acc_msg, "Failed to allocate memory");
88
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));
92
93         m_ch->send(acc_msg);
94 }
95
96 int sensor_listener_proxy::start(void)
97 {
98         sensor_handler *sensor = m_manager->get_sensor(m_uri);
99         retv_if(!sensor, -EINVAL);
100
101         _D("Listener[%d] try to start", get_id());
102
103         /* TODO: listen pause policy */
104         return sensor->start(this);
105 }
106
107 int sensor_listener_proxy::stop(void)
108 {
109         sensor_handler *sensor = m_manager->get_sensor(m_uri);
110         retv_if(!sensor, -EINVAL);
111
112         /* TODO: listen pause policy */
113
114         _D("Listener[%d] try to stop", get_id());
115
116         int ret = sensor->stop(this);
117         retv_if(ret < 0, OP_ERROR);
118
119         /* unset attributes */
120         set_interval(POLL_1HZ_MS);
121         set_max_batch_latency(0);
122
123         return OP_SUCCESS;
124 }
125
126 int sensor_listener_proxy::set_interval(unsigned int interval)
127 {
128         sensor_handler *sensor = m_manager->get_sensor(m_uri);
129         retv_if(!sensor, -EINVAL);
130
131         _D("Listener[%d] try to set interval[%d]", get_id(), interval);
132
133         return sensor->set_interval(this, interval);
134 }
135
136 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
137 {
138         sensor_handler *sensor = m_manager->get_sensor(m_uri);
139         retv_if(!sensor, -EINVAL);
140
141         _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency);
142
143         return sensor->set_batch_latency(this, max_batch_latency);
144 }
145
146 int sensor_listener_proxy::set_passive_mode(bool passive)
147 {
148         /* TODO: passive mode */
149         m_passive = passive;
150         return OP_SUCCESS;
151 }
152
153 int sensor_listener_proxy::set_attribute(int attribute, int value)
154 {
155         sensor_handler *sensor = m_manager->get_sensor(m_uri);
156         retv_if(!sensor, -EINVAL);
157
158         _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value);
159
160         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
161                 m_pause_policy = value;
162                 return OP_SUCCESS;
163         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
164                 m_axis_orientation = value;
165                 return OP_SUCCESS;
166         }
167
168         return sensor->set_attribute(this, attribute, value);
169 }
170
171 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
172 {
173         sensor_handler *sensor = m_manager->get_sensor(m_uri);
174         retv_if(!sensor, -EINVAL);
175
176         _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len);
177
178         return sensor->set_attribute(this, attribute, value, len);
179 }
180
181 int sensor_listener_proxy::flush(void)
182 {
183         sensor_handler *sensor = m_manager->get_sensor(m_uri);
184         retv_if(!sensor, -EINVAL);
185
186         return sensor->flush(this);
187 }
188
189 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
190 {
191         sensor_handler *sensor = m_manager->get_sensor(m_uri);
192         retv_if(!sensor, -EINVAL);
193
194         return sensor->get_cache(data, len);
195 }
196
197 std::string sensor_listener_proxy::get_required_privileges(void)
198 {
199         sensor_handler *sensor = m_manager->get_sensor(m_uri);
200         retv_if(!sensor, "");
201
202         sensor_info info = sensor->get_sensor_info();
203         return info.get_privilege();
204 }