Update for attribute changed callback and attribute getter
[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 {
34 }
35
36 application_sensor_handler::~application_sensor_handler()
37 {
38 }
39
40 int application_sensor_handler::publish(sensor_data_t *data, int len)
41 {
42         std::string uri = m_info.get_uri();
43         return notify(uri.c_str(), data, len);
44 }
45
46 const sensor_info &application_sensor_handler::get_sensor_info(void)
47 {
48         return m_info;
49 }
50
51 int application_sensor_handler::start(sensor_observer *ob)
52 {
53         add_observer(ob);
54
55         if (observer_count() > 1 || m_started.load())
56                 return OP_SUCCESS; /* already started */
57
58         ipc::message msg;
59         msg.set_type(CMD_PROVIDER_START);
60         m_ch->send_sync(msg);
61         m_started.store(true);
62
63         _I("Started[%s]", m_info.get_uri().c_str());
64
65         return OP_SUCCESS;
66 }
67
68 int application_sensor_handler::stop(sensor_observer *ob)
69 {
70         remove_observer(ob);
71
72         if (observer_count() > 0 || !m_started.load())
73                 return OP_SUCCESS; /* already started */
74
75         ipc::message msg;
76         msg.set_type(CMD_PROVIDER_STOP);
77         m_ch->send_sync(msg);
78         m_started.store(false);
79
80         _I("Stopped[%s]", m_info.get_uri().c_str());
81
82         return OP_SUCCESS;
83 }
84
85 int application_sensor_handler::get_min_interval(void)
86 {
87         int interval;
88         std::vector<int> temp;
89
90         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
91                 if (it->second > 0)
92                     temp.push_back(it->second);
93
94         if (temp.empty())
95                 return m_info.get_min_interval();
96
97         interval = *std::min_element(temp.begin(), temp.end());
98
99         if (interval < m_info.get_min_interval())
100                 return m_info.get_min_interval();
101
102         return interval;
103 }
104
105 int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
106 {
107         retv_if(interval == m_prev_interval, OP_SUCCESS);
108
109         int32_t cur_interval = interval;
110
111         m_interval_map[ob] = cur_interval;
112         cur_interval = get_min_interval();
113
114         ipc::message msg;
115         cmd_provider_attr_int_t buf;
116         buf.attribute = SENSORD_ATTRIBUTE_INTERVAL;
117         buf.value = cur_interval;
118
119         msg.set_type(CMD_PROVIDER_ATTR_INT);
120         msg.enclose((const char *)&buf, sizeof(cmd_provider_attr_int_t));
121         m_ch->send_sync(msg);
122
123         update_prev_interval(cur_interval);
124         return OP_SUCCESS;
125 }
126
127 int application_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval)
128 {
129         interval = m_prev_interval;
130         return OP_SUCCESS;
131 }
132
133 int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
134 {
135         update_prev_latency(latency);
136         return OP_SUCCESS;
137 }
138
139 int application_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency)
140 {
141         latency = m_prev_latency;
142         return OP_SUCCESS;
143 }
144
145 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
146 {
147         update_attribute(attr, value);
148         return OP_SUCCESS;
149 }
150
151 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
152 {
153         ipc::message msg;
154         cmd_provider_attr_str_t *buf;
155         size_t size;
156
157         size = sizeof(cmd_provider_attr_str_t) + len;
158
159         buf = (cmd_provider_attr_str_t *) malloc(sizeof(char) * size);
160         retvm_if(!buf, -ENOMEM, "Failed to allocate memory");
161
162         msg.set_type(CMD_PROVIDER_ATTR_STR);
163         memcpy(buf->value, value, len);
164         buf->attribute = attr;
165         buf->len = len;
166
167         msg.enclose((char *)buf, size);
168
169         m_ch->send_sync(msg);
170
171         _I("Set attribute[%d] to sensor[%s]", attr, m_info.get_uri().c_str());
172         update_attribute(attr, value, len);
173         return OP_SUCCESS;
174 }
175
176 int application_sensor_handler::flush(sensor_observer *ob)
177 {
178         return OP_SUCCESS;
179 }
180
181 int application_sensor_handler::get_data(sensor_data_t **data, int *len)
182 {
183         return OP_SUCCESS;
184 }