Merge branch 'devel/tizen' into tizen
[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 {
42 }
43
44 sensor_listener_proxy::~sensor_listener_proxy()
45 {
46         stop();
47 }
48
49 uint32_t sensor_listener_proxy::get_id(void)
50 {
51         return m_id;
52 }
53
54 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
55 {
56         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
57
58         /* TODO: check axis orientation */
59         msg->header()->type = CMD_LISTENER_EVENT;
60         msg->header()->err = OP_SUCCESS;
61
62         m_ch->send(msg);
63
64         return OP_CONTINUE;
65 }
66
67 int sensor_listener_proxy::start(void)
68 {
69         sensor_handler *sensor = m_manager->get_sensor(m_uri);
70         retv_if(!sensor, -EINVAL);
71
72         /* TODO: listen pause policy */
73         return sensor->start(this);
74 }
75
76 int sensor_listener_proxy::stop(void)
77 {
78         sensor_handler *sensor = m_manager->get_sensor(m_uri);
79         retv_if(!sensor, -EINVAL);
80
81         /* TODO: listen pause policy */
82         int ret;
83
84         ret = sensor->stop(this);
85         retv_if(ret < 0, OP_ERROR);
86
87         /* unset attributes */
88         set_interval(POLL_1HZ_MS);
89         set_max_batch_latency(0);
90
91         return OP_SUCCESS;
92 }
93
94 int sensor_listener_proxy::set_interval(unsigned int interval)
95 {
96         sensor_handler *sensor = m_manager->get_sensor(m_uri);
97         retv_if(!sensor, -EINVAL);
98
99         return sensor->set_interval(this, interval);
100 }
101
102 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
103 {
104         sensor_handler *sensor = m_manager->get_sensor(m_uri);
105         retv_if(!sensor, -EINVAL);
106
107         return sensor->set_batch_latency(this, max_batch_latency);
108 }
109
110 int sensor_listener_proxy::set_passive_mode(bool passive)
111 {
112         /* TODO: passive mode */
113         m_passive = passive;
114         return OP_SUCCESS;
115 }
116
117 int sensor_listener_proxy::set_attribute(int attribute, int value)
118 {
119         sensor_handler *sensor = m_manager->get_sensor(m_uri);
120         retv_if(!sensor, -EINVAL);
121
122         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
123                 m_pause_policy = value;
124                 return OP_SUCCESS;
125         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
126                 m_axis_orientation = value;
127                 return OP_SUCCESS;
128         }
129
130         return sensor->set_attribute(this, attribute, value);
131 }
132
133 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
134 {
135         sensor_handler *sensor = m_manager->get_sensor(m_uri);
136         retv_if(!sensor, -EINVAL);
137
138         return sensor->set_attribute(this, attribute, value, len);
139 }
140
141 int sensor_listener_proxy::flush(void)
142 {
143         sensor_handler *sensor = m_manager->get_sensor(m_uri);
144         retv_if(!sensor, -EINVAL);
145
146         return sensor->flush(this);
147 }
148
149 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
150 {
151         sensor_handler *sensor = m_manager->get_sensor(m_uri);
152         retv_if(!sensor, -EINVAL);
153
154         /* TODO : caching the last data & retry logic if there is no data */
155         return sensor->get_data(data, len);
156 }
157
158 std::string sensor_listener_proxy::get_required_privileges(void)
159 {
160         sensor_handler *sensor = m_manager->get_sensor(m_uri);
161         retv_if(!sensor, "");
162
163         sensor_info info = sensor->get_sensor_info();
164         return info.get_privilege();
165 }