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 <ipc_client.h>
27 #include <command_types.h>
29 #include "sensor_provider_channel_handler.h"
31 using namespace sensor;
33 sensor_provider::sensor_provider(const char *uri)
42 sensor_provider::~sensor_provider()
47 bool sensor_provider::init(const char *uri)
49 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
50 retvm_if(!m_client, false, "Failed to allocate memory");
52 m_handler = new(std::nothrow) channel_handler(this);
58 m_sensor.set_uri(uri);
59 m_sensor.set_min_range(0);
60 m_sensor.set_max_range(1);
61 m_sensor.set_resolution(1);
66 void sensor_provider::deinit(void)
77 const char *sensor_provider::get_uri(void)
79 return m_sensor.get_uri().c_str();
82 sensor_info *sensor_provider::get_sensor_info(void)
87 int sensor_provider::serialize(sensor_info *info, char **bytes)
90 raw_data_t *raw = new(std::nothrow) raw_data_t;
91 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
93 info->serialize(*raw);
95 *bytes = new(std::nothrow) char[raw->size()];
96 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
98 std::copy(raw->begin(), raw->end(), *bytes);
106 int sensor_provider::send_sensor_info(sensor_info *info)
111 size = serialize(info, &bytes);
113 ipc::message msg((const char *)bytes, size);
114 msg.set_type(CMD_PROVIDER_CONNECT);
116 m_channel->send_sync(&msg);
121 int sensor_provider::connect(void)
123 m_channel = m_client->connect(m_handler, &m_loop);
124 retvm_if(!m_channel, -EIO, "Failed to connect to server");
126 /* serialize and send sensor info */
127 send_sensor_info(get_sensor_info());
131 m_channel->read_sync(reply);
132 retv_if(reply.header()->err < 0, reply.header()->err);
134 m_connected.store(true);
136 _I("Provider URI[%s]", get_uri());
141 bool sensor_provider::disconnect(void)
143 retv_if(!is_connected(), false);
144 m_connected.store(false);
146 ipc::message msg(OP_SUCCESS);
149 msg.set_type(CMD_PROVIDER_DISCONNECT);
151 m_channel->send_sync(&msg);
152 m_channel->read_sync(reply);
154 m_channel->disconnect();
159 _I("Disconnected[%s]", get_uri());
164 void sensor_provider::restore(void)
166 ret_if(!is_connected());
167 retm_if(!connect(), "Failed to restore provider");
169 _D("Restored provider[%s]", get_uri());
172 int sensor_provider::publish(sensor_data_t *data, int len)
175 msg.set_type(CMD_PROVIDER_PUBLISH);
176 msg.enclose((const char *)data, len);
178 m_channel->send_sync(&msg);
183 bool sensor_provider::is_connected(void)
185 return m_connected.load();
188 void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
190 m_handler->set_start_cb(cb, user_data);
193 void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
195 m_handler->set_stop_cb(cb, user_data);
198 void sensor_provider::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data)
200 m_handler->set_interval_cb(cb, user_data);