sensorctl: add options/features for usability
[platform/core/system/sensord.git] / src / server / application_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 "application_sensor_handler.h"
21
22 #include <message.h>
23 #include <command_types.h>
24 #include <sensor_log.h>
25 #include <algorithm>
26
27 using namespace sensor;
28
29 application_sensor_handler::application_sensor_handler(const sensor_info &info, ipc::channel *ch)
30 : sensor_handler(info)
31 , m_ch(ch)
32 , m_started(false)
33 , m_prev_interval(0)
34 {
35 }
36
37 application_sensor_handler::~application_sensor_handler()
38 {
39 }
40
41 int application_sensor_handler::publish(sensor_data_t *data, int len)
42 {
43         std::string uri = m_info.get_uri();
44         return notify(uri.c_str(), data, len);
45 }
46
47 const sensor_info &application_sensor_handler::get_sensor_info(void)
48 {
49         return m_info;
50 }
51
52 int application_sensor_handler::start(sensor_observer *ob)
53 {
54         add_observer(ob);
55
56         if (observer_count() > 1 || m_started.load())
57                 return OP_SUCCESS; /* already started */
58
59         ipc::message msg;
60         msg.set_type(CMD_PROVIDER_START);
61         m_ch->send_sync(&msg);
62         m_started.store(true);
63
64         return OP_SUCCESS;
65 }
66
67 int application_sensor_handler::stop(sensor_observer *ob)
68 {
69         remove_observer(ob);
70
71         if (observer_count() > 0 || !m_started.load())
72                 return OP_SUCCESS; /* already started */
73
74         ipc::message msg;
75         msg.set_type(CMD_PROVIDER_STOP);
76         m_ch->send_sync(&msg);
77         m_started.store(false);
78
79         return OP_SUCCESS;
80 }
81
82 int application_sensor_handler::get_min_interval(void)
83 {
84         int interval;
85         std::vector<int> temp;
86
87         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
88                 if (it->second > 0)
89                     temp.push_back(it->second);
90
91         if (temp.empty())
92                 return m_info.get_min_interval();
93
94         interval = *std::min_element(temp.begin(), temp.end());
95
96         if (interval < m_info.get_min_interval())
97                 return m_info.get_min_interval();
98
99         return interval;
100 }
101
102 int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
103 {
104         retv_if(interval == m_prev_interval, OP_SUCCESS);
105
106         int32_t cur_interval = interval;
107
108         m_interval_map[ob] = cur_interval;
109         cur_interval = get_min_interval();
110
111         ipc::message msg;
112         cmd_provider_attr_int_t buf;
113         buf.attribute = SENSORD_ATTRIBUTE_INTERVAL;
114         buf.value = cur_interval;
115
116         msg.set_type(CMD_PROVIDER_ATTR_INT);
117         msg.enclose((const char *)&buf, sizeof(cmd_provider_attr_int_t));
118         m_ch->send_sync(&msg);
119
120         m_prev_interval = cur_interval;
121
122         return OP_SUCCESS;
123 }
124
125 int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
126 {
127         return OP_SUCCESS;
128 }
129
130 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
131 {
132         return OP_SUCCESS;
133 }
134
135 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
136 {
137         return OP_SUCCESS;
138 }
139
140 int application_sensor_handler::flush(sensor_observer *ob)
141 {
142         return OP_SUCCESS;
143 }
144
145 int application_sensor_handler::get_data(sensor_data_t **data, int *len)
146 {
147         return OP_SUCCESS;
148 }