sensorctl: add options/features for usability
[platform/core/system/sensord.git] / src / server / external_sensor_handler.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 "external_sensor_handler.h"
21
22 #include <message.h>
23 #include <sensor_log.h>
24 #include <external_sensor.h>
25 #include <algorithm>
26
27 using namespace sensor;
28
29 class external_sensor_notifier : public sensor_notifier {
30 public:
31         external_sensor_notifier(external_sensor_handler *sensor);
32
33         int notify(void);
34
35 private:
36         external_sensor_handler *m_sensor;
37 };
38
39 external_sensor_notifier::external_sensor_notifier(external_sensor_handler *sensor)
40 : m_sensor(sensor)
41 {
42 }
43
44 int external_sensor_notifier::notify(void)
45 {
46         /* TODO: Change thread-safe function(add handler to event-loop) */
47         sensor_data_t *data;
48         int len;
49
50         if (m_sensor->get_data(&data, &len) < 0)
51                 return OP_ERROR;
52
53         /* TODO: pointer would be better */
54         sensor_info info = m_sensor->get_sensor_info();
55
56         return m_sensor->notify(info.get_uri().c_str(), data, len);
57 }
58
59 external_sensor_handler::external_sensor_handler(const sensor_info &info,
60                 external_sensor *sensor)
61 : sensor_handler(info)
62 , m_sensor(sensor)
63 , m_notifier(NULL)
64 , m_policy(OP_DEFAULT)
65 {
66         init();
67 }
68
69 external_sensor_handler::~external_sensor_handler()
70 {
71         deinit();
72 }
73
74 bool external_sensor_handler::init(void)
75 {
76         m_notifier = new(std::nothrow) external_sensor_notifier(this);
77         retvm_if(!m_notifier, false, "Failed to allocate memory");
78
79         m_sensor->set_notifier(m_notifier);
80         return true;
81 }
82
83 void external_sensor_handler::deinit(void)
84 {
85         delete m_notifier;
86         m_notifier = NULL;
87 }
88
89 const sensor_info &external_sensor_handler::get_sensor_info(void)
90 {
91         return m_info;
92 }
93
94 int external_sensor_handler::start(sensor_observer *ob)
95 {
96         retv_if(!m_sensor, -EINVAL);
97
98         int policy = m_sensor->start(ob);
99         retv_if(policy <= OP_ERROR, policy);
100
101         add_observer(ob);
102
103         return OP_SUCCESS;
104 }
105
106 int external_sensor_handler::stop(sensor_observer *ob)
107 {
108         retv_if(!m_sensor, -EINVAL);
109
110         int policy = m_sensor->stop(ob);
111         retv_if(policy <= OP_ERROR, policy);
112
113         remove_observer(ob);
114
115         return OP_SUCCESS;
116 }
117
118 int external_sensor_handler::get_min_interval(void)
119 {
120         int interval;
121         std::vector<int> temp;
122
123         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
124                 if (it->second > 0)
125                     temp.push_back(it->second);
126
127         if (temp.empty())
128                 return m_info.get_min_interval();
129
130         interval = *std::min_element(temp.begin(), temp.end());
131
132         if (interval < m_info.get_min_interval())
133                 return m_info.get_min_interval();
134
135         return interval;
136 }
137
138 int external_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
139 {
140         retv_if(!m_sensor, -EINVAL);
141
142         int _interval = interval;
143
144         if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) {
145                 m_policy = m_sensor->set_interval(ob, interval);
146                 retv_if(m_policy <= OP_ERROR, m_policy);
147         }
148
149         m_interval_map[ob] = interval;
150
151         if (m_policy == OP_DEFAULT && observer_count() > 0) {
152                 _interval = get_min_interval();
153                 return m_sensor->set_interval(ob, _interval);
154         }
155
156         return OP_SUCCESS;
157 }
158
159 int external_sensor_handler::get_min_batch_latency(void)
160 {
161         int batch_latency;
162         std::vector<int> temp;
163
164         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
165                 if (it->second > 0)
166                     temp.push_back(it->second);
167
168         if (temp.empty())
169                 return 0;
170
171         batch_latency = *std::min_element(temp.begin(), temp.end());
172
173         return batch_latency;
174 }
175
176 int external_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
177 {
178         retv_if(!m_sensor, -EINVAL);
179
180         int _latency = latency;
181
182         if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) {
183                 m_policy = m_sensor->set_batch_latency(ob, latency);
184                 retv_if(m_policy <= OP_ERROR, m_policy);
185         }
186
187         m_batch_latency_map[ob] = _latency;
188
189         if (m_policy == OP_DEFAULT && observer_count() > 0) {
190                 _latency = get_min_batch_latency();
191                 return m_sensor->set_batch_latency(ob, latency);
192         }
193         return OP_SUCCESS;
194 }
195
196 int external_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
197 {
198         retv_if(!m_sensor, -EINVAL);
199
200         if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) {
201                 m_policy = m_sensor->set_attribute(ob, attr, value);
202                 retv_if(m_policy <= OP_ERROR, m_policy);
203         }
204
205         if (m_policy == OP_DEFAULT) {
206                 /* default logic */
207         }
208         return OP_SUCCESS;
209 }
210
211 int external_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
212 {
213         retv_if(!m_sensor, -EINVAL);
214
215         if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) {
216                 m_policy = m_sensor->set_attribute(ob, attr, value, len);
217                 retv_if(m_policy <= OP_ERROR, m_policy);
218         }
219
220         if (m_policy == OP_DEFAULT) {
221                 /* default logic */
222         }
223         return OP_SUCCESS;
224 }
225
226 int external_sensor_handler::get_data(sensor_data_t **data, int *len)
227 {
228         return m_sensor->get_data(data, len);
229 }
230
231 int external_sensor_handler::flush(sensor_observer *ob)
232 {
233         retv_if(!m_sensor, -EINVAL);
234
235         m_sensor->flush(this);
236
237         return OP_SUCCESS;
238 }