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.h>
25 #include <sensor_types.h>
26 #include <sensor_utils.h>
27 #include <ipc_client.h>
28 #include <command_types.h>
32 #include "sensor_provider_channel_handler.h"
34 #define DEFAULT_RESOLUTION 0.1
36 using namespace sensor;
38 sensor_provider::sensor_provider(const char *uri)
47 sensor_provider::~sensor_provider()
52 bool sensor_provider::init(const char *uri)
54 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
55 retvm_if(!m_client, false, "Failed to allocate memory");
57 m_handler = new(std::nothrow) channel_handler(this);
63 m_sensor.set_uri(uri);
64 m_sensor.set_min_range(-FLT_MAX);
65 m_sensor.set_max_range(FLT_MAX);
66 m_sensor.set_resolution(DEFAULT_RESOLUTION);
67 /* TODO: temporary walkaround */
68 const char *priv = sensor::utils::get_privilege(uri);
69 m_sensor.set_privilege(priv);
74 void sensor_provider::deinit(void)
85 const char *sensor_provider::get_uri(void)
87 return m_sensor.get_uri().c_str();
90 sensor_info *sensor_provider::get_sensor_info(void)
95 int sensor_provider::serialize(sensor_info *info, char **bytes)
98 raw_data_t *raw = new(std::nothrow) raw_data_t;
99 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
101 info->serialize(*raw);
103 *bytes = new(std::nothrow) char[raw->size()];
104 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
106 std::copy(raw->begin(), raw->end(), *bytes);
114 int sensor_provider::send_sensor_info(sensor_info *info)
119 size = serialize(info, &bytes);
121 ipc::message msg((const char *)bytes, size);
122 msg.set_type(CMD_PROVIDER_CONNECT);
124 m_channel->send_sync(&msg);
129 int sensor_provider::connect(void)
131 m_channel = m_client->connect(m_handler, &m_loop);
132 retvm_if(!m_channel, -EIO, "Failed to connect to server");
134 /* serialize and send sensor info */
135 send_sensor_info(get_sensor_info());
139 m_channel->read_sync(reply);
140 retv_if(reply.header()->err < 0, reply.header()->err);
142 m_connected.store(true);
144 _I("Provider URI[%s]", get_uri());
149 bool sensor_provider::disconnect(void)
151 retv_if(!is_connected(), false);
152 m_connected.store(false);
154 m_channel->disconnect();
158 _I("Disconnected[%s]", get_uri());
163 void sensor_provider::restore(void)
165 ret_if(!is_connected());
166 retm_if(!connect(), "Failed to restore provider");
168 _D("Restored provider[%s]", get_uri());
171 int sensor_provider::publish(sensor_data_t *data, int len)
173 for (int i = 0; i < data->value_count; ++i) {
174 if (!(data->values[i] >= m_sensor.get_min_range() &&
175 data->values[i] <= m_sensor.get_max_range())) {
182 msg.set_type(CMD_PROVIDER_PUBLISH);
183 msg.enclose((const char *)data, len);
185 m_channel->send_sync(&msg);
190 bool sensor_provider::is_connected(void)
192 return m_connected.load();
195 void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
197 m_handler->set_start_cb(cb, user_data);
200 void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
202 m_handler->set_stop_cb(cb, user_data);
205 void sensor_provider::set_interval_cb(sensord_provider_interval_changed_cb cb, void *user_data)
207 m_handler->set_interval_cb(cb, user_data);