--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "sensor_provider.h"
+
+#include <message.h>
+#include <channel.h>
+#include <sensor_log.h>
+#include <sensor_types.h>
+#include <ipc_client.h>
+#include <command_types.h>
+
+#include "sensor_provider_handler.h"
+
+using namespace sensor;
+
+sensor_provider::sensor_provider(const char *uri)
+: m_client(NULL)
+, m_channel(NULL)
+, m_handler(NULL)
+, m_connected(false)
+{
+ init(uri);
+}
+
+sensor_provider::~sensor_provider()
+{
+ deinit();
+}
+
+bool sensor_provider::init(const char *uri)
+{
+ m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
+ retvm_if(!m_client, false, "Failed to allocate memory");
+
+ m_handler = new(std::nothrow) sensor_provider_handler(this);
+ if (!m_handler) {
+ delete m_client;
+ return false;
+ }
+
+ m_sensor.set_uri(uri);
+ m_sensor.set_min_range(0);
+ m_sensor.set_max_range(1);
+ m_sensor.set_resolution(1);
+ m_sensor.set_privilege(PRIV_APPLICATION_SENSOR);
+
+ return true;
+}
+
+void sensor_provider::deinit(void)
+{
+ disconnect();
+
+ delete m_handler;
+ m_handler = NULL;
+
+ delete m_client;
+ m_client = NULL;
+}
+
+const char *sensor_provider::get_uri(void)
+{
+ return m_sensor.get_uri().c_str();
+}
+
+sensor_info *sensor_provider::get_sensor_info(void)
+{
+ return &m_sensor;
+}
+
+int sensor_provider::serialize(sensor_info *info, char **bytes)
+{
+ int size;
+ raw_data_t *raw = new(std::nothrow) raw_data_t;
+ retvm_if(!raw, -ENOMEM, "Failed to allocated memory");
+
+ info->serialize(*raw);
+
+ *bytes = new(std::nothrow) char[raw->size()];
+ retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory");
+
+ std::copy(raw->begin(), raw->end(), *bytes);
+
+ size = raw->size();
+ delete raw;
+
+ return size;
+}
+
+int sensor_provider::send_sensor_info(sensor_info *info)
+{
+ char *bytes;
+ int size;
+
+ size = serialize(info, &bytes);
+
+ ipc::message msg((const char *)bytes, size);
+ msg.set_type(CMD_PROVIDER_CONNECT);
+
+ m_channel->send_sync(&msg);
+
+ return OP_SUCCESS;
+}
+
+bool sensor_provider::connect(void)
+{
+ m_channel = m_client->connect(m_handler, &m_loop);
+ retvm_if(!m_channel, false, "Failed to connect to server");
+
+ /* serialize and send sensor info */
+ send_sensor_info(get_sensor_info());
+
+ /* check error */
+ ipc::message reply;
+ m_channel->read_sync(reply);
+ retv_if(reply.header()->err < 0, reply.header()->err);
+
+ m_connected.store(true);
+
+ _I("Provider URI[%s]", get_uri());
+
+ return true;
+}
+
+bool sensor_provider::disconnect(void)
+{
+ retv_if(!is_connected(), false);
+ m_connected.store(false);
+
+ ipc::message msg(OP_SUCCESS);
+ ipc::message reply;
+
+ msg.set_type(CMD_PROVIDER_DISCONNECT);
+
+ m_channel->send_sync(&msg);
+ m_channel->read_sync(reply);
+
+ m_channel->disconnect();
+
+ delete m_channel;
+ m_channel = NULL;
+
+ _I("Disconnected[%s]", get_uri());
+
+ return true;
+}
+
+void sensor_provider::restore(void)
+{
+ ret_if(!is_connected());
+ retm_if(!connect(), "Failed to restore provider");
+
+ _D("Restored provider[%s]", get_uri());
+}
+
+int sensor_provider::publish(sensor_data_t *data, int len)
+{
+ ipc::message msg;
+ msg.set_type(CMD_PROVIDER_PUBLISH);
+ msg.enclose((const char *)data, len);
+
+ m_channel->send_sync(&msg);
+
+ return OP_SUCCESS;
+}
+
+bool sensor_provider::is_connected(void)
+{
+ return m_connected.load();
+}
+
+void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data)
+{
+ m_handler->set_start_cb(cb, user_data);
+}
+
+void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
+{
+ m_handler->set_stop_cb(cb, user_data);
+}
+
+void sensor_provider::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data)
+{
+ m_handler->set_interval_cb(cb, user_data);
+}
+
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __SENSOR_PROVIDER_H__
+#define __SENSOR_PROVIDER_H__
+
+#include <ipc_client.h>
+#include <channel.h>
+#include <channel_handler.h>
+#include <event_loop.h>
+#include <sensor_internal.h>
+#include <sensor_info.h>
+#include <sensor_types.h>
+#include <map>
+#include <atomic>
+
+#include "sensor_provider_handler.h"
+
+namespace sensor {
+
+class sensor_provider {
+public:
+ sensor_provider(const char *uri);
+ virtual ~sensor_provider();
+
+ const char *get_uri(void);
+ sensor_info *get_sensor_info(void);
+
+ bool connect(void);
+ bool disconnect(void);
+ void restore(void);
+
+ void set_start_cb(sensord_provider_start_cb cb, void *user_data);
+ void set_stop_cb(sensord_provider_stop_cb cb, void *user_data);
+ void set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data);
+
+ int publish(sensor_data_t *data, int len);
+
+private:
+ bool init(const char *uri);
+ void deinit(void);
+
+ bool is_connected(void);
+
+ int serialize(sensor_info *info, char **bytes);
+ int send_sensor_info(sensor_info *info);
+
+ sensor_info m_sensor;
+
+ ipc::ipc_client *m_client;
+ ipc::channel *m_channel;
+ ipc::event_loop m_loop;
+ sensor_provider_handler *m_handler;
+ std::atomic<bool> m_connected;
+};
+
+}
+
+#endif /* __SENSOR_PROVIDER_H__ */
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "sensor_provider_handler.h"
+
+#include <command_types.h>
+#include <sensor_log.h>
+#include "sensor_provider.h"
+
+using namespace sensor;
+
+sensor_provider_handler::sensor_provider_handler(sensor_provider *provider)
+: m_provider(provider)
+{
+}
+
+void sensor_provider_handler::connected(ipc::channel *ch)
+{
+ _I("Connected");
+}
+
+void sensor_provider_handler::disconnected(ipc::channel *ch)
+{
+ /* TODO */
+ /* m_provider->restore(); */
+}
+
+void sensor_provider_handler::read(ipc::channel *ch, ipc::message &msg)
+{
+ switch (msg.type()) {
+ case CMD_PROVIDER_START:
+ m_start_cb(m_provider, m_start_user_data);
+ break;
+ case CMD_PROVIDER_STOP:
+ m_stop_cb(m_provider, m_stop_user_data);
+ break;
+ case CMD_PROVIDER_ATTR_INT:
+ cmd_provider_attr_int_t buf;
+ msg.disclose((char *)&buf);
+
+ if (buf.attribute == SENSORD_ATTRIBUTE_INTERVAL)
+ m_set_interval_cb(m_provider, buf.value, m_set_interval_user_data);
+ break;
+ }
+}
+
+void sensor_provider_handler::read_complete(ipc::channel *ch)
+{
+}
+
+void sensor_provider_handler::error_caught(ipc::channel *ch, int error)
+{
+}
+
+void sensor_provider_handler::set_start_cb(sensord_provider_start_cb cb, void *user_data)
+{
+ m_start_cb = cb;
+ m_start_user_data = user_data;
+}
+
+void sensor_provider_handler::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
+{
+ m_stop_cb = cb;
+ m_stop_user_data = user_data;
+}
+
+void sensor_provider_handler::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data)
+{
+ m_set_interval_cb = cb;
+ m_set_interval_user_data = user_data;
+}
--- /dev/null
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __SENSOR_PROVIDER_HANDLER__
+#define __SENSOR_PROVIDER_HANDLER__
+
+#include <sensor_internal.h>
+#include <channel_handler.h>
+
+namespace sensor {
+
+class sensor_provider;
+
+class sensor_provider_handler : public ipc::channel_handler
+{
+public:
+ sensor_provider_handler(sensor_provider *provider);
+
+ void connected(ipc::channel *ch);
+ void disconnected(ipc::channel *ch);
+ void read(ipc::channel *ch, ipc::message &msg);
+
+ void read_complete(ipc::channel *ch);
+ void error_caught(ipc::channel *ch, int error);
+
+ void set_start_cb(sensord_provider_start_cb cb, void *user_data);
+ void set_stop_cb(sensord_provider_stop_cb cb, void *user_data);
+ void set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data);
+
+private:
+ sensor_provider *m_provider;
+
+ sensord_provider_start_cb m_start_cb;
+ sensord_provider_stop_cb m_stop_cb;
+ sensord_provider_set_interval_cb m_set_interval_cb;
+
+ void *m_start_user_data;
+ void *m_stop_user_data;
+ void *m_set_interval_user_data;
+};
+
+}
+
+#endif /* __SENSOR_PROVIDER_HANDLER__ */