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>
30 #include "sensor_provider_channel_handler.h"
32 using namespace sensor;
34 sensor_provider::sensor_provider(const char *uri)
43 sensor_provider::~sensor_provider()
48 bool sensor_provider::init(const char *uri)
50 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
51 retvm_if(!m_client, false, "Failed to allocate memory");
53 m_handler = new(std::nothrow) channel_handler(this);
59 m_sensor.set_uri(uri);
60 m_sensor.set_min_range(0);
61 m_sensor.set_max_range(1);
62 m_sensor.set_resolution(1);
63 /* TODO: temporary walkaround */
64 const char *priv = sensor::utils::get_privilege(uri);
65 m_sensor.set_privilege(priv);
70 void sensor_provider::deinit(void)
81 const char *sensor_provider::get_uri(void)
83 return m_sensor.get_uri().c_str();
86 sensor_info *sensor_provider::get_sensor_info(void)
91 int sensor_provider::serialize(sensor_info *info, char **bytes)
94 raw_data_t *raw = new(std::nothrow) raw_data_t;
95 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
97 info->serialize(*raw);
99 *bytes = new(std::nothrow) char[raw->size()];
100 retvm_if(!*bytes, -ENOMEM, "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);
117 ipc::message msg((const char *)bytes, size);
118 msg.set_type(CMD_PROVIDER_CONNECT);
120 m_channel->send_sync(&msg);
125 int sensor_provider::connect(void)
127 m_channel = m_client->connect(m_handler, &m_loop);
128 retvm_if(!m_channel, -EIO, "Failed to connect to server");
130 /* serialize and send sensor info */
131 send_sensor_info(get_sensor_info());
135 m_channel->read_sync(reply);
136 retv_if(reply.header()->err < 0, reply.header()->err);
138 m_connected.store(true);
140 _I("Provider URI[%s]", get_uri());
145 bool sensor_provider::disconnect(void)
147 retv_if(!is_connected(), false);
148 m_connected.store(false);
150 m_channel->disconnect();
154 _I("Disconnected[%s]", get_uri());
159 void sensor_provider::restore(void)
161 ret_if(!is_connected());
162 retm_if(!connect(), "Failed to restore provider");
164 _D("Restored provider[%s]", get_uri());
167 int sensor_provider::publish(sensor_data_t *data, int len)
170 msg.set_type(CMD_PROVIDER_PUBLISH);
171 msg.enclose((const char *)data, len);
173 m_channel->send_sync(&msg);
178 bool sensor_provider::is_connected(void)
180 return m_connected.load();
183 void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
185 m_handler->set_start_cb(cb, user_data);
188 void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
190 m_handler->set_stop_cb(cb, user_data);
193 void sensor_provider::set_interval_cb(sensord_provider_interval_changed_cb cb, void *user_data)
195 m_handler->set_interval_cb(cb, user_data);