4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "application_sensor_handler.h"
23 #include <command_types.h>
24 #include <sensor_log.h>
27 using namespace sensor;
29 application_sensor_handler::application_sensor_handler(const sensor_info &info, ipc::channel *ch)
30 : sensor_handler(info)
36 application_sensor_handler::~application_sensor_handler()
40 int application_sensor_handler::publish(sensor_data_t *data, int len)
42 std::string uri = m_info.get_uri();
43 return notify(uri.c_str(), data, len);
46 const sensor_info &application_sensor_handler::get_sensor_info(void)
51 int application_sensor_handler::start(sensor_observer *ob)
55 if (observer_count() > 1 || m_started.load())
56 return OP_SUCCESS; /* already started */
59 msg.set_type(CMD_PROVIDER_START);
61 m_started.store(true);
63 _I("Started[%s]", m_info.get_uri().c_str());
68 int application_sensor_handler::stop(sensor_observer *ob)
72 if (observer_count() > 0 || !m_started.load())
73 return OP_SUCCESS; /* already started */
76 msg.set_type(CMD_PROVIDER_STOP);
78 m_started.store(false);
80 _I("Stopped[%s]", m_info.get_uri().c_str());
85 int application_sensor_handler::get_min_interval(void)
88 std::vector<int> temp;
90 for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
92 temp.push_back(it->second);
95 return m_info.get_min_interval();
97 interval = *std::min_element(temp.begin(), temp.end());
99 if (interval < m_info.get_min_interval())
100 return m_info.get_min_interval();
105 int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
107 retv_if(interval == m_prev_interval, OP_SUCCESS);
109 int32_t cur_interval = interval;
111 m_interval_map[ob] = cur_interval;
112 cur_interval = get_min_interval();
115 cmd_provider_attr_int_t buf;
116 buf.attribute = SENSORD_ATTRIBUTE_INTERVAL;
117 buf.value = cur_interval;
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);
123 update_prev_interval(cur_interval);
127 int application_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval)
129 interval = m_prev_interval;
133 int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
135 update_prev_latency(latency);
139 int application_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency)
141 latency = m_prev_latency;
145 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
147 update_attribute(attr, value);
151 int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
154 cmd_provider_attr_str_t *buf;
157 size = sizeof(cmd_provider_attr_str_t) + len;
159 buf = (cmd_provider_attr_str_t *) malloc(sizeof(char) * size);
160 retvm_if(!buf, -ENOMEM, "Failed to allocate memory");
162 msg.set_type(CMD_PROVIDER_ATTR_STR);
163 memcpy(buf->value, value, len);
164 buf->attribute = attr;
167 msg.enclose((char *)buf, size);
169 m_ch->send_sync(msg);
171 _I("Set attribute[%d] to sensor[%s]", attr, m_info.get_uri().c_str());
172 update_attribute(attr, value, len);
176 int application_sensor_handler::flush(sensor_observer *ob)
181 int application_sensor_handler::get_data(sensor_data_t **data, int *len)