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);
62 m_sensor.set_privilege(PRIV_APPLICATION_SENSOR);
67 void sensor_provider::deinit(void)
78 const char *sensor_provider::get_uri(void)
80 return m_sensor.get_uri().c_str();
83 sensor_info *sensor_provider::get_sensor_info(void)
88 int sensor_provider::serialize(sensor_info *info, char **bytes)
91 raw_data_t *raw = new(std::nothrow) raw_data_t;
92 retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
94 info->serialize(*raw);
96 *bytes = new(std::nothrow) char[raw->size()];
97 retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
99 std::copy(raw->begin(), raw->end(), *bytes);
107 int sensor_provider::send_sensor_info(sensor_info *info)
112 size = serialize(info, &bytes);
114 ipc::message msg((const char *)bytes, size);
115 msg.set_type(CMD_PROVIDER_CONNECT);
117 m_channel->send_sync(&msg);
122 int sensor_provider::connect(void)
124 m_channel = m_client->connect(m_handler, &m_loop);
125 retvm_if(!m_channel, -EIO, "Failed to connect to server");
127 /* serialize and send sensor info */
128 send_sensor_info(get_sensor_info());
132 m_channel->read_sync(reply);
133 retv_if(reply.header()->err < 0, reply.header()->err);
135 m_connected.store(true);
137 _I("Provider URI[%s]", get_uri());
142 bool sensor_provider::disconnect(void)
144 retv_if(!is_connected(), false);
145 m_connected.store(false);
147 ipc::message msg(OP_SUCCESS);
150 msg.set_type(CMD_PROVIDER_DISCONNECT);
152 m_channel->send_sync(&msg);
153 m_channel->read_sync(reply);
155 m_channel->disconnect();
160 _I("Disconnected[%s]", get_uri());
165 void sensor_provider::restore(void)
167 ret_if(!is_connected());
168 retm_if(!connect(), "Failed to restore provider");
170 _D("Restored provider[%s]", get_uri());
173 int sensor_provider::publish(sensor_data_t *data, int len)
176 msg.set_type(CMD_PROVIDER_PUBLISH);
177 msg.enclose((const char *)data, len);
179 m_channel->send_sync(&msg);
184 bool sensor_provider::is_connected(void)
186 return m_connected.load();
189 void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
191 m_handler->set_start_cb(cb, user_data);
194 void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
196 m_handler->set_stop_cb(cb, user_data);
199 void sensor_provider::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data)
201 m_handler->set_interval_cb(cb, user_data);