Merge branch 'devel/tizen' into tizen
[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         _I("Started[%s]", m_info.get_uri().c_str());
65
66         return OP_SUCCESS;
67 }
68
69 int application_sensor_handler::stop(sensor_observer *ob)
70 {
71         remove_observer(ob);
72
73         if (observer_count() > 0 || !m_started.load())
74                 return OP_SUCCESS; /* already started */
75
76         ipc::message msg;
77         msg.set_type(CMD_PROVIDER_STOP);
78         m_ch->send_sync(&msg);
79         m_started.store(false);
80
81         _I("Stopped[%s]", m_info.get_uri().c_str());
82
83         return OP_SUCCESS;
84 }
85
86 int application_sensor_handler::get_min_interval(void)
87 {
88         int interval;
89         std::vector<int> temp;
90
91         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
92                 if (it->second > 0)
93                     temp.push_back(it->second);
94
95         if (temp.empty())
96                 return m_info.get_min_interval();
97
98         interval = *std::min_element(temp.begin(), temp.end());
99
100         if (interval < m_info.get_min_interval())
101                 return m_info.get_min_interval();
102
103         return interval;
104 }
105
106 int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
107 {
108         retv_if(interval == m_prev_interval, OP_SUCCESS);
109
110         int32_t cur_interval = interval;
111
112         m_interval_map[ob] = cur_interval;
113         cur_interval = get_min_interval();
114
115         ipc::message msg;
116         cmd_provider_attr_int_t buf;
117         buf.attribute = SENSORD_ATTRIBUTE_INTERVAL;
118         buf.value = cur_interval;
119
120         msg.set_type(CMD_PROVIDER_ATTR_INT);
121         msg.enclose((const char *)&buf, sizeof(cmd_provider_attr_int_t));
122         m_ch->send_sync(&msg);
123
124         m_prev_interval = cur_interval;
125
126         _I("Set interval[%d] to sensor[%s]", cur_interval, m_info.get_uri().c_str());
127
128         return OP_SUCCESS;
129 }
130
131 int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
132 {
133         return OP_SUCCESS;
134 }
135
136 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
137 {
138         return OP_SUCCESS;
139 }
140
141 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
142 {
143         return OP_SUCCESS;
144 }
145
146 int application_sensor_handler::flush(sensor_observer *ob)
147 {
148         return OP_SUCCESS;
149 }
150
151 int application_sensor_handler::get_data(sensor_data_t **data, int *len)
152 {
153         return OP_SUCCESS;
154 }