sensord: replace usleep() code with sleep()
[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 using namespace sensor;
29
30 sensor_listener_proxy::sensor_listener_proxy(
31                 uint32_t id, sensor_handler *sensor, ipc::channel *ch)
32 : m_id(id)
33 , m_sensor(sensor)
34 , m_ch(ch)
35 , m_passive(false)
36 , m_pause_policy(SENSORD_PAUSE_ALL)
37 , m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED)
38 {
39 }
40
41 sensor_listener_proxy::~sensor_listener_proxy()
42 {
43         stop();
44 }
45
46 uint32_t sensor_listener_proxy::get_id(void)
47 {
48         return m_id;
49 }
50
51 int sensor_listener_proxy::update(const char *uri, ipc::message *msg)
52 {
53         retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE);
54
55         /* TODO: check axis orientation */
56         msg->header()->type = CMD_LISTENER_EVENT;
57         msg->header()->err = OP_SUCCESS;
58
59         m_ch->send(msg);
60
61         return OP_CONTINUE;
62 }
63
64 int sensor_listener_proxy::start(void)
65 {
66         /* TODO: listen pause policy */
67         return m_sensor->start(this);
68 }
69
70 int sensor_listener_proxy::stop(void)
71 {
72         /* TODO: listen pause policy */
73         int ret;
74
75         ret = m_sensor->stop(this);
76         retv_if(ret < 0, OP_ERROR);
77
78         /* unset attributes */
79         set_interval(POLL_1HZ_MS);
80         set_max_batch_latency(0);
81
82         return OP_SUCCESS;
83 }
84
85 int sensor_listener_proxy::set_interval(unsigned int interval)
86 {
87         return m_sensor->set_interval(this, interval);
88 }
89
90 int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency)
91 {
92         return m_sensor->set_batch_latency(this, max_batch_latency);
93 }
94
95 int sensor_listener_proxy::set_passive_mode(bool passive)
96 {
97         /* TODO: passive mode */
98         m_passive = passive;
99         return OP_SUCCESS;
100 }
101
102 int sensor_listener_proxy::set_attribute(int attribute, int value)
103 {
104         if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) {
105                 m_pause_policy = value;
106                 return OP_SUCCESS;
107         } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) {
108                 m_axis_orientation = value;
109                 return OP_SUCCESS;
110         }
111
112         return m_sensor->set_attribute(this, attribute, value);
113 }
114
115 int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len)
116 {
117         return m_sensor->set_attribute(this, attribute, value, len);
118 }
119
120 int sensor_listener_proxy::flush(void)
121 {
122         return m_sensor->flush(this);
123 }
124
125 int sensor_listener_proxy::get_data(sensor_data_t **data, int *len)
126 {
127         /* TODO : caching the last data & retry logic if there is no data */
128         return m_sensor->get_data(data, len);
129 }
130
131 std::string sensor_listener_proxy::get_required_privileges(void)
132 {
133         sensor_info info = m_sensor->get_sensor_info();
134         return info.get_privilege();
135 }