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 "sensor-provider.h"
24 #include <sensor-log-private.h>
25 #include <sensor-types-private.h>
26 #include <sensor-utils.h>
27 #include <command-types.h>
31 #include "sensor-provider-channel-handler.h"
33 #define DEFAULT_RESOLUTION 0.1
35 using namespace sensor;
37 sensor_provider::sensor_provider(const char *uri)
45 sensor_provider::~sensor_provider()
50 bool sensor_provider::init(const char *uri)
52 m_handler = new(std::nothrow) channel_handler(this);
57 m_sensor.set_uri(uri);
58 m_sensor.set_min_range(-FLT_MAX);
59 m_sensor.set_max_range(FLT_MAX);
60 m_sensor.set_resolution(DEFAULT_RESOLUTION);
61 /* TODO: temporary walkaround */
62 const char *priv = sensor::utils::get_privilege(uri);
63 m_sensor.set_privilege(priv);
68 void sensor_provider::deinit(void)
76 const char *sensor_provider::get_uri(void)
78 return m_sensor.get_uri().c_str();
81 sensor_info *sensor_provider::get_sensor_info(void)
86 int sensor_provider::serialize(sensor_info *info, char **bytes)
89 raw_data_t *raw = new(std::nothrow) raw_data_t;
90 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
92 info->serialize(*raw);
94 *bytes = (char *) malloc(raw->size());
98 _E("Failed to allocate memory");
102 std::copy(raw->begin(), raw->end(), *bytes);
110 int sensor_provider::send_sensor_info(sensor_info *info)
115 size = serialize(info, &bytes);
119 ipc::message msg((const char *)bytes, size);
120 msg.set_type(CMD_PROVIDER_CONNECT);
122 m_channel->send_sync(msg);
127 int sensor_provider::connect(void)
129 m_channel = new(std::nothrow) ipc::channel();
130 retvm_if(!m_channel, -EIO, "Failed to allocate memory");
131 m_channel->connect(m_handler, &m_loop, true);
133 /* serialize and send sensor info */
134 send_sensor_info(get_sensor_info());
138 m_channel->read_sync(reply);
139 retv_if(reply.header()->err < 0, reply.header()->err);
141 m_connected.store(true);
143 _I("Provider URI[%s]", get_uri());
148 bool sensor_provider::disconnect(void)
150 retv_if(!is_connected(), false);
151 m_connected.store(false);
153 m_channel->disconnect();
157 _I("Disconnected[%s]", get_uri());
162 void sensor_provider::restore(void)
164 ret_if(!is_connected());
165 retm_if(!connect(), "Failed to restore provider");
167 _D("Restored provider[%s]", get_uri());
170 int sensor_provider::publish(const sensor_data_t &data)
173 msg.set_type(CMD_PROVIDER_PUBLISH);
174 msg.enclose((const void *)(&data), sizeof(data));
176 m_channel->send_sync(msg);
181 int sensor_provider::publish(const sensor_data_t data[], const int count)
184 msg.set_type(CMD_PROVIDER_PUBLISH);
185 msg.enclose((const void *)data, sizeof(sensor_data_t) * count);
187 m_channel->send_sync(msg);
192 bool sensor_provider::is_connected(void)
194 return m_connected.load();
197 void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
199 m_handler->set_start_cb(cb, user_data);
202 void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
204 m_handler->set_stop_cb(cb, user_data);
207 void sensor_provider::set_interval_cb(sensord_provider_interval_changed_cb cb, void *user_data)
209 m_handler->set_interval_cb(cb, user_data);
212 void sensor_provider::set_attribute_str_cb(sensord_provider_attribute_str_cb cb, void *user_data)
214 m_handler->set_attribute_str_cb(cb, user_data);