# Internal Logging Option
#ADD_DEFINITIONS("-DTIZEN_DEBUG")
-ADD_SUBDIRECTORY(src/api)
+ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(tools)
SET(PC_LDFLAGS "-l${PROJECT_NAME}")
INCLUDE_DIRECTORIES(
- ${CMAKE_SOURCE_DIR}/src/shared
${CMAKE_CURRENT_SOURCE_DIR}
)
SET(SOURCES
fusion-util.c
geomagnetic-field.c
- api-sensor.cpp
- api-sensor-internal.cpp
- api-sensor-provider.cpp)
+ sensor.cpp
+ sensor-internal.cpp
+ sensor-provider.cpp)
IF("${SENSOR_RECORDER}" STREQUAL "on")
- SET(SOURCES ${SOURCES} api-sensor-recorder.cpp)
+ SET(SOURCES ${SOURCES} sensor-recorder.cpp)
ELSE()
- SET(SOURCES ${SOURCES} api-sensor-recorder-dummy.cpp)
+ SET(SOURCES ${SOURCES} sensor-recorder-dummy.cpp)
ENDIF()
ADD_LIBRARY(${PROJECT_NAME} SHARED ${SOURCES})
+++ /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-listener.h"
-
-#include <channel-handler.h>
-#include <sensor-log-private.h>
-#include <sensor-types.h>
-#include <command-types.h>
-#include <lock.h>
-
-#include <unordered_map>
-
-using namespace sensor;
-
-struct listener_info {
- sensor_listener *listener;
- cmutex lock;
-};
-
-static std::unordered_map<int, listener_info> listeners;
-static int listener_id;
-
-static gboolean listener_handler(GIOChannel *ch, GIOCondition condition, gpointer data)
-{
- int id = (long)data;
- Autolock(listeners[id].lock);
-
- sensor_listener *listener = listeners[id].listener;
- if (!listener) {
- listeners.erase(id);
- return false;
- }
-
- unsigned int cond = (unsigned int)condition;
- if (cond & (G_IO_HUP)) {
- listener->restore();
- return false;
- }
-
- ipc::message msg;
- bool ret = listener->read(msg);
- if (!ret)
- return false;
-
- listener->handle(msg);
-
- return true;
-}
-
-sensor_listener::sensor_listener(sensor_t sensor, GMainLoop *loop)
-: m_id(0)
-, l_id(listener_id++)
-, m_sensor(reinterpret_cast<sensor_info *>(sensor))
-, m_cmd_socket(NULL)
-, m_evt_socket(NULL)
-, m_evt_handler(NULL)
-, m_acc_handler(NULL)
-, m_attr_int_changed_handler(NULL)
-, m_attr_str_changed_handler(NULL)
-, m_loop(loop)
-, g_src(NULL)
-, m_connected(false)
-, m_started(false)
-{
- init();
-}
-
-sensor_listener::~sensor_listener()
-{
- deinit();
-}
-
-bool sensor_listener::init(void)
-{
- if (!connect())
- return false;
-
- listeners[l_id].listener = this;
-
- return true;
-}
-
-void sensor_listener::deinit(void)
-{
- Autolock(listeners[l_id].lock);
- _D("Deinitializing..");
- stop();
- disconnect();
-
- unset_event_handler();
- unset_accuracy_handler();
- unset_attribute_int_changed_handler();
- unset_attribute_str_changed_handler();
-
- listeners[l_id].listener = NULL;
-
- m_attributes_int.clear();
- m_attributes_str.clear();
- _D("Deinitialized..");
-}
-
-int sensor_listener::get_id(void)
-{
- return m_id;
-}
-
-sensor_t sensor_listener::get_sensor(void)
-{
- return static_cast<sensor_t>(m_sensor);
-}
-
-void sensor_listener::restore(void)
-{
- Autolock(listeners[l_id].lock);
-
- disconnect();
-
- retm_if(!connect(), "Failed to restore listener");
-
- _D("Restoring sensor listener");
-
- /* Restore attributes/status */
- if (m_started.load())
- start();
-
- auto interval = m_attributes_int.find(SENSORD_ATTRIBUTE_INTERVAL);
- if (interval != m_attributes_int.end())
- set_interval(m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL]);
-
- auto latency = m_attributes_int.find(SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY);
- if (latency != m_attributes_int.end())
- set_max_batch_latency(m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY]);
-
- auto power = m_attributes_int.find(SENSORD_ATTRIBUTE_PAUSE_POLICY);
- if (power != m_attributes_int.end())
- set_attribute(SENSORD_ATTRIBUTE_PAUSE_POLICY, m_attributes_int[SENSORD_ATTRIBUTE_PAUSE_POLICY]);
-
- _D("Restored listener[%d]", get_id());
-}
-
-bool sensor_listener::connect(void)
-{
- GIOChannel *ch = NULL;
- ipc::message msg;
- ipc::message reply;
- cmd_listener_connect_t buf = {0, };
-
- m_cmd_socket = new(std::nothrow) ipc::socket();
- retvm_if(!m_cmd_socket, false, "Failed to allocate memory");
-
- if (!m_cmd_socket->create(SENSOR_CHANNEL_PATH))
- goto fail;
-
- if (!m_cmd_socket->connect())
- goto fail;
-
- m_evt_socket = new(std::nothrow) ipc::socket();
- if (!m_evt_socket) {
- _E("Failed to allocate memory");
- goto fail;
- }
-
- if (!m_evt_socket->create(SENSOR_CHANNEL_PATH))
- goto fail;
-
- if (!m_evt_socket->connect())
- goto fail;
-
- memcpy(buf.sensor, m_sensor->get_uri().c_str(), m_sensor->get_uri().size());
- msg.set_type(CMD_LISTENER_CONNECT);
- msg.enclose((const char *)&buf, sizeof(buf));
- send(m_evt_socket, msg);
-
- read(m_evt_socket, reply);
- reply.disclose((char *)&buf, sizeof(buf));
-
- m_id = buf.listener_id;
- m_connected.store(true);
-
- ch = g_io_channel_unix_new(m_evt_socket->get_fd());
- retvm_if(!ch, false, "Failed to create g_io_channel_unix_new");
-
- g_src = g_io_create_watch(ch, (GIOCondition) (ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL));
- g_io_channel_unref(ch);
- if (!g_src) {
- _E("Failed to create g_io_create_watch");
- goto fail;
- }
-
- g_source_set_callback(g_src, (GSourceFunc) listener_handler, (gpointer)l_id, NULL);
- g_source_attach(g_src, g_main_loop_get_context(m_loop));
- g_source_unref(g_src);
-
- _I("Connected listener[%d] with sensor[%s]", get_id(), m_sensor->get_uri().c_str());
-
- return true;
-
-fail:
- delete m_evt_socket;
- m_evt_socket = NULL;
-
- delete m_cmd_socket;
- m_cmd_socket = NULL;
-
- return false;
-}
-
-void sensor_listener::disconnect(void)
-{
- ret_if(!is_connected());
- m_connected.store(false);
-
- _D("Disconnecting..");
-
- if (g_src && !g_source_is_destroyed(g_src)) {
- g_source_destroy(g_src);
- g_src = NULL;
- }
-
- delete m_evt_socket;
- m_evt_socket = NULL;
-
- delete m_cmd_socket;
- m_cmd_socket = NULL;
-
- _I("Disconnected[%d]", get_id());
-}
-
-bool sensor_listener::is_connected(void)
-{
- return m_connected.load();
-}
-
-ipc::channel_handler *sensor_listener::get_event_handler(void)
-{
- return m_evt_handler;
-}
-
-void sensor_listener::set_event_handler(ipc::channel_handler *handler)
-{
- delete m_evt_handler;
- m_evt_handler = handler;
-}
-
-void sensor_listener::unset_event_handler(void)
-{
- delete m_evt_handler;
- m_evt_handler = NULL;
-}
-
-ipc::channel_handler *sensor_listener::get_accuracy_handler(void)
-{
- return m_acc_handler;
-}
-
-void sensor_listener::set_accuracy_handler(ipc::channel_handler *handler)
-{
- delete m_acc_handler;
- m_acc_handler = handler;
-}
-
-void sensor_listener::unset_accuracy_handler(void)
-{
- delete m_acc_handler;
- m_acc_handler = NULL;
-}
-
-ipc::channel_handler *sensor_listener::get_attribute_int_changed_handler(void)
-{
- return m_attr_int_changed_handler;
-}
-
-void sensor_listener::set_attribute_int_changed_handler(ipc::channel_handler *handler)
-{
- delete m_attr_int_changed_handler;
- m_attr_int_changed_handler = handler;
-}
-
-void sensor_listener::unset_attribute_int_changed_handler(void)
-{
- delete m_attr_int_changed_handler;
- m_attr_int_changed_handler = NULL;
-}
-
-ipc::channel_handler *sensor_listener::get_attribute_str_changed_handler(void)
-{
- return m_attr_str_changed_handler;
-}
-
-void sensor_listener::set_attribute_str_changed_handler(ipc::channel_handler *handler)
-{
- delete m_attr_str_changed_handler;
- m_attr_str_changed_handler = handler;
-}
-
-void sensor_listener::unset_attribute_str_changed_handler(void)
-{
- delete m_attr_str_changed_handler;
- m_attr_str_changed_handler = NULL;
-}
-
-int sensor_listener::start(void)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_start_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EINVAL, "Failed to connect to server");
-
- buf.listener_id = m_id;
- msg.set_type(CMD_LISTENER_START);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0) {
- _E("Failed to start listener[%d], sensor[%s]", get_id(), m_sensor->get_uri().c_str());
- return reply.header()->err;
- }
-
- m_started.store(true);
-
- _I("Listener[%d] started", get_id());
-
- return OP_SUCCESS;
-}
-
-int sensor_listener::stop(void)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_stop_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EINVAL, "Failed to connect to server");
- retvm_if(!m_started.load(), -EAGAIN, "Already stopped");
-
- buf.listener_id = m_id;
- msg.set_type(CMD_LISTENER_STOP);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0) {
- _E("Failed to stop listener[%d]", get_id());
- return reply.header()->err;
- }
-
- m_started.store(false);
-
- _I("Listener[%d] stopped", get_id());
-
- return OP_SUCCESS;
-}
-
-int sensor_listener::get_interval(void)
-{
- auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_INTERVAL);
- retv_if(it == m_attributes_int.end(), -1);
-
- return m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL];
-}
-
-int sensor_listener::get_max_batch_latency(void)
-{
- auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY);
- retv_if(it == m_attributes_int.end(), -1);
-
- return m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY];
-}
-
-int sensor_listener::get_pause_policy(void)
-{
- auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_PAUSE_POLICY);
- retv_if(it == m_attributes_int.end(), -1);
-
- return m_attributes_int[SENSORD_ATTRIBUTE_PAUSE_POLICY];
-}
-
-int sensor_listener::get_passive_mode(void)
-{
- auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_PASSIVE_MODE);
- retv_if(it == m_attributes_int.end(), -1);
-
- return m_attributes_int[SENSORD_ATTRIBUTE_PASSIVE_MODE];
-}
-
-int sensor_listener::set_interval(unsigned int interval)
-{
- int _interval;
-
- /* TODO: move this logic to server */
- if (interval == 0)
- _interval = DEFAULT_INTERVAL;
- else if (interval < (unsigned int)m_sensor->get_min_interval())
- _interval = m_sensor->get_min_interval();
- else
- _interval = interval;
-
- _I("Listener[%d] set interval[%u]", get_id(), _interval);
-
- /* If it is not started, store the value only */
- if (!m_started.load()) {
- m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL] = _interval;
- return OP_SUCCESS;
- }
-
- return set_attribute(SENSORD_ATTRIBUTE_INTERVAL, _interval);
-}
-
-int sensor_listener::set_max_batch_latency(unsigned int max_batch_latency)
-{
- _I("Listener[%d] set max batch latency[%u]", get_id(), max_batch_latency);
-
- /* If it is not started, store the value only */
- if (!m_started.load()) {
- m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY] = max_batch_latency;
- return OP_SUCCESS;
- }
-
- return set_attribute(SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY, max_batch_latency);
-}
-
-int sensor_listener::set_passive_mode(bool passive)
-{
- _I("Listener[%d] set passive mode[%d]", get_id(), passive);
-
- return set_attribute(SENSORD_ATTRIBUTE_PASSIVE_MODE, passive);
-}
-
-int sensor_listener::flush(void)
-{
- _I("Listener[%d] flushes", get_id());
-
- return set_attribute(SENSORD_ATTRIBUTE_FLUSH, 1);
-}
-
-int sensor_listener::set_attribute(int attribute, int value)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_attr_int_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EIO, "Failed to connect to server");
-
- buf.listener_id = m_id;
- buf.attribute = attribute;
- buf.value = value;
- msg.set_type(CMD_LISTENER_SET_ATTR_INT);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0)
- return reply.header()->err;
-
- if (attribute != SENSORD_ATTRIBUTE_FLUSH) {
- update_attribute(attribute, value);
- }
-
- return OP_SUCCESS;
-}
-
-int sensor_listener::get_attribute(int attribute, int* value)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_attr_int_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EIO, "Failed to connect to server");
-
- buf.listener_id = m_id;
- buf.attribute = attribute;
- msg.set_type(CMD_LISTENER_GET_ATTR_INT);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0) {
- return reply.header()->err;
- }
-
- if (reply.header()->length && reply.body()) {
- *value = ((cmd_listener_attr_int_t *)reply.body())->value;
- return OP_SUCCESS;
- }
-
- return OP_ERROR;
-}
-
-void sensor_listener::update_attribute(int attribute, int value)
-{
- Autolock(listeners[l_id].lock);
- m_attributes_int[attribute] = value;
- _I("Update_attribute(int) listener[%d] attribute[%d] value[%d] attributes size[%d]", get_id(), attribute, value, m_attributes_int.size());
-}
-
-int sensor_listener::set_attribute(int attribute, const char *value, int len)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_attr_str_t *buf;
- size_t size;
-
- retvm_if(!m_cmd_socket, -EIO, "Failed to connect to server");
-
- size = sizeof(cmd_listener_attr_str_t) + len;
-
- buf = (cmd_listener_attr_str_t *) new(std::nothrow) char[size];
- retvm_if(!buf, -ENOMEM, "Failed to allocate memory");
-
- msg.set_type(CMD_LISTENER_SET_ATTR_STR);
- buf->listener_id = m_id;
- buf->attribute = attribute;
-
- memcpy(buf->value, value, len);
- buf->len = len;
-
- msg.enclose((char *)buf, size);
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- /* Message memory is released automatically after sending message,
- so it doesn't need to free memory */
-
- delete [] buf;
-
- if (reply.header()->err < 0)
- return reply.header()->err;
-
- update_attribute(attribute, value, len);
-
- return OP_SUCCESS;
-}
-
-int sensor_listener::get_attribute(int attribute, char **value, int* len)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_attr_str_t buf = {0, };
-
- buf.listener_id = m_id;
- buf.attribute = attribute;
-
- msg.set_type(CMD_LISTENER_GET_ATTR_STR);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
- if (reply.header()->err < 0) {
- return reply.header()->err;
- }
-
- if (reply.header()->length && reply.body()) {
- cmd_listener_attr_str_t * recv_buf = (cmd_listener_attr_str_t *)reply.body();
- char* p = (char *)recv_buf->value;
- *len = recv_buf->len;
- *value = (char *) malloc(*len);
- std::copy(p, p + recv_buf->len, *value);
- return OP_SUCCESS;
- }
-
- return OP_ERROR;
-}
-
-void sensor_listener::update_attribute(int attribute, const char *value, int len)
-{
- Autolock(listeners[l_id].lock);
- m_attributes_str[attribute].clear();
- m_attributes_str[attribute].insert(m_attributes_str[attribute].begin(), value, value + len);
- _I("Update_attribute(str) listener[%d] attribute[%d] value[%s] attributes size[%zu]", get_id(), attribute, value, m_attributes_int.size());
-}
-
-int sensor_listener::get_sensor_data(sensor_data_t *data)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_get_data_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EIO, "Failed to connect to server");
-
- buf.listener_id = m_id;
- msg.set_type(CMD_LISTENER_GET_DATA);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0) {
- return OP_ERROR;
- }
-
- reply.disclose((char *)&buf, sizeof(buf));
- int size = sizeof(sensor_data_t);
-
- if (buf.len > size || buf.len <= 0) {
- data->accuracy = -1;
- data->value_count = 0;
- return OP_ERROR;
- }
-
- memcpy(data, &buf.data, buf.len);
-
- _D("Listener[%d] read sensor data", get_id());
-
- return OP_SUCCESS;
-}
-
-int sensor_listener::get_sensor_data_list(sensor_data_t **data, int *count)
-{
- ipc::message msg;
- ipc::message reply;
- cmd_listener_get_data_list_t buf = {0, };
-
- retvm_if(!m_cmd_socket, -EIO, "Failed to connect to server");
-
- buf.listener_id = m_id;
- msg.set_type(CMD_LISTENER_GET_DATA_LIST);
- msg.enclose((char *)&buf, sizeof(buf));
-
- send(m_cmd_socket, msg);
- read(m_cmd_socket, reply);
-
- if (reply.header()->err < 0) {
- return reply.header()->err;
- }
-
- size_t size = reply.size();
- cmd_listener_get_data_list_t* reply_buf = (cmd_listener_get_data_list_t *) new(std::nothrow) char[size];
-
- retvm_if(!reply_buf, -ENOMEM, "Failed to allocate memory");
-
- reply.disclose((char *)reply_buf, size);
-
- if (reply_buf->len <= 0) {
- delete [] reply_buf;
- return OP_ERROR;
- }
-
- *count = reply_buf->data_count;
- *data = (sensor_data_t*) malloc(reply_buf->len);
-
- if (!(*data)) {
- _E("Memory allocation failed");
- delete [] reply_buf;
- return -ENOMEM;
- }
-
- memcpy(*data, reply_buf->data, reply_buf->len);
-
- _D("Listener[%d] read sensor data list", get_id());
- delete [] reply_buf;
- return OP_SUCCESS;
-}
-
-
-bool sensor_listener::read(ipc::message &msg)
-{
- return read(m_evt_socket, msg);
-}
-
-bool sensor_listener::read(ipc::socket *m_socket, ipc::message &msg)
-{
- Autolock(listeners[l_id].lock);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- ipc::message_header header;
- ssize_t size = 0;
- char buf[MAX_MSG_CAPACITY];
-
- /* header */
- size = m_socket->recv(&header, sizeof(ipc::message_header), false);
- if (size <= 0)
- return false;
-
- // check error from header
- if (header.err != 0) {
- msg.header()->err = header.err;
- return false;
- }
-
- /* body */
- if (header.length >= MAX_MSG_CAPACITY) {
- _E("header.length error %u", header.length);
- return false;
- }
-
- if (header.length > 0) {
- size = m_socket->recv(&buf, header.length, false);
- if (size <= 0)
- return false;
- }
-
- buf[header.length] = '\0';
- msg.enclose(reinterpret_cast<const void *>(buf), header.length);
- msg.set_type(header.type);
- msg.header()->err = header.err;
-
- return true;
-}
-
-bool sensor_listener::send(ipc::socket *m_socket, ipc::message &msg)
-{
- Autolock(listeners[l_id].lock);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- retvm_if(msg.size() >= MAX_MSG_CAPACITY, true, "Invaild message size[%u]", msg.size());
-
- ssize_t size = 0;
- char *buf = msg.body();
-
- /* header */
- size = m_socket->send(reinterpret_cast<void *>(msg.header()),
- sizeof(ipc::message_header), true);
- retvm_if(size <= 0, false, "Failed to send header");
-
- /* if body size is zero, skip to send body message */
- retv_if(msg.size() == 0, true);
-
- /* body */
- size = m_socket->send(buf, msg.size(), true);
- retvm_if(size <= 0, false, "Failed to send body");
-
- return true;
-}
-
-void sensor_listener::handle(ipc::message &msg)
-{
- ipc::channel_handler *handler = NULL;
- switch (msg.header()->type) {
- case CMD_LISTENER_EVENT:
- handler = m_evt_handler;
- break;
- case CMD_LISTENER_ACC_EVENT:
- handler = m_acc_handler;
- break;
- case CMD_LISTENER_SET_ATTR_INT:
- handler = m_attr_int_changed_handler;
- break;
- case CMD_LISTENER_SET_ATTR_STR:
- handler = m_attr_str_changed_handler;
- break;
- case CMD_LISTENER_CONNECTED:
- // Do nothing
- break;
- default:
- _W("Invalid command message");
- }
-
- if (handler)
- handler->read(NULL, msg);
-}
+++ /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_LISTENER_H__
-#define __SENSOR_LISTENER_H__
-
-#include <channel.h>
-#include <channel-handler.h>
-#include <event-loop.h>
-#include <sensor-info.h>
-#include <sensor-types.h>
-#include <lock.h>
-#include <glib.h>
-
-#include <map>
-#include <atomic>
-#include <vector>
-
-namespace sensor {
-
-class sensor_listener {
-public:
- sensor_listener(sensor_t sensor, GMainLoop *loop);
- virtual ~sensor_listener();
-
- int get_id(void);
- sensor_t get_sensor(void);
-
- ipc::channel_handler *get_event_handler(void);
- ipc::channel_handler *get_accuracy_handler(void);
- ipc::channel_handler *get_attribute_int_changed_handler(void);
- ipc::channel_handler *get_attribute_str_changed_handler(void);
-
- /* TODO: modify the listener so that it can register multiple handlers(1:n) */
- void set_event_handler(ipc::channel_handler *handler);
- void set_accuracy_handler(ipc::channel_handler *handler);
- void set_attribute_int_changed_handler(ipc::channel_handler *handler);
- void set_attribute_str_changed_handler(ipc::channel_handler *handler);
-
- void unset_event_handler(void);
- void unset_accuracy_handler(void);
- void unset_attribute_int_changed_handler(void);
- void unset_attribute_str_changed_handler(void);
-
- int start(void);
- int stop(void);
-
- int get_interval(void);
- int get_max_batch_latency(void);
- int get_pause_policy(void);
- int get_passive_mode(void);
-
- int set_interval(unsigned int interval);
- int set_max_batch_latency(unsigned int max_batch_latency);
- int set_passive_mode(bool passive);
- int set_attribute(int attribute, int value);
- int get_attribute(int attribute, int* value);
- void update_attribute(int attribute, int value);
- int set_attribute(int attribute, const char *value, int len);
- int get_attribute(int attribute, char **value, int *len);
- void update_attribute(int attribute, const char *value, int len);
- int get_sensor_data(sensor_data_t *data);
- int get_sensor_data_list(sensor_data_t **data, int *count);
- int flush(void);
-
- void restore(void);
-
- bool read(ipc::socket *m_socket, ipc::message &msg);
- bool read(ipc::message &msg);
- bool send(ipc::socket *m_socket, ipc::message &msg);
- void handle(ipc::message &msg);
-
-private:
- bool init(void);
- void deinit(void);
-
- bool connect(void);
- void disconnect(void);
- bool is_connected(void);
-
- int m_id;
- int l_id;
- sensor_info *m_sensor;
-
- ipc::socket *m_cmd_socket;
- ipc::socket *m_evt_socket;
-
- ipc::channel_handler *m_evt_handler;
- ipc::channel_handler *m_acc_handler;
- ipc::channel_handler *m_attr_int_changed_handler;
- ipc::channel_handler *m_attr_str_changed_handler;
-
- GMainLoop *m_loop;
- GSource *g_src;
- std::atomic<bool> m_connected;
- std::atomic<bool> m_started;
- std::map<int, int> m_attributes_int;
- std::map<int, std::vector<char>> m_attributes_str;
-};
-
-}
-
-#endif /* __SENSOR_LISTENER_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-manager.h"
-
-#include <sensor-log-private.h>
-#include <sensor-info.h>
-#include <sensor-utils.h>
-#include <command-types.h>
-#include <message.h>
-
-using namespace sensor;
-
-static gboolean manager_handler(GIOChannel *ch, GIOCondition condition, gpointer data)
-{
- sensor_manager *manager = (sensor_manager*) data;
- unsigned int cond = (unsigned int)condition;
-
- if (cond & (G_IO_HUP)) {
- manager->restore();
- return false;
- }
-
- ipc::message msg;
- bool ret = manager->read(msg);
- if (!ret)
- return false;
-
- manager->handle(msg);
-
- return true;
-}
-
-sensor_manager::sensor_manager()
-: m_cmd_socket(NULL)
-, m_mon_socket(NULL)
-, g_src(NULL)
-, m_connected(false)
-{
- m_loop = g_main_loop_new(NULL, false);
-}
-
-sensor_manager::~sensor_manager()
-{
- disconnect();
-
- if (m_loop) {
- g_main_loop_quit(m_loop);
- g_main_loop_unref(m_loop);
- m_loop = NULL;
- }
-}
-
-int sensor_manager::get_sensor(const char *uri, sensor_t *sensor)
-{
- if (!is_supported(uri)) {
- *sensor = NULL;
- _D("Not supported URI [%s]\n", uri);
- return -ENODATA;
- }
-
- sensor_info *info = get_info(uri);
- retvm_if(!info, -EACCES, "There is no accessible sensor for uri[%s]", uri);
-
- *sensor = (sensor_t)info;
- return OP_SUCCESS;
-}
-
-int sensor_manager::get_sensors(const char *uri, sensor_t **list, int *count)
-{
- retv_if(!is_supported(uri), -ENODATA);
-
- std::vector<sensor_info *> infos;
- int size;
-
- infos = get_infos(uri);
- size = infos.size();
- retvm_if(size == 0, -EACCES, "There is no accessible sensors for uri[%s]", uri);
-
- *list = (sensor_t *)malloc(sizeof(sensor_info *) * size);
- retvm_if(!*list, -ENOMEM, "Failed to allocate memory");
-
- for (int i = 0; i < size; ++i)
- *(*list + i) = infos[i];
-
- *count = size;
- return OP_SUCCESS;
-}
-
-bool sensor_manager::is_supported(sensor_t sensor)
-{
- retvm_if(!sensor, false, "Invalid sensor");
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- if (&*it == sensor)
- return true;
- }
-
- return false;
-}
-
-bool sensor_manager::is_supported(const char *uri)
-{
- if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
- return true;
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- if ((*it).get_uri() == uri)
- return true;
-
- std::size_t found = (*it).get_uri().find_last_of("/");
- if (found == std::string::npos)
- continue;
-
- if ((*it).get_uri().substr(0, found) == uri)
- return true;
- }
-
- return false;
-}
-
-int sensor_manager::add_sensor(sensor_info &info)
-{
- retv_if(is_supported(info.get_uri().c_str()), OP_ERROR);
-
- m_sensors.push_back(info);
-
- _I("Added sensor[%s]", info.get_uri().c_str());
-
- return OP_SUCCESS;
-}
-
-int sensor_manager::add_sensor(sensor_provider *provider)
-{
- retvm_if(!provider, -EINVAL, "Invalid parameter");
- return add_sensor(*(provider->get_sensor_info()));
-}
-
-int sensor_manager::remove_sensor(const char *uri)
-{
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- if ((*it).get_uri() == uri) {
- m_sensors.erase(it);
- _I("Removed sensor[%s]", uri);
-
- return OP_SUCCESS;
- }
- }
-
- return OP_ERROR;
-}
-
-int sensor_manager::remove_sensor(sensor_provider *provider)
-{
- retvm_if(!provider, -EINVAL, "Invalid parameter");
- return remove_sensor(provider->get_uri());
-}
-
-void sensor_manager::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
-{
- m_sensor_added_callbacks.emplace(cb, user_data);
-}
-
-void sensor_manager::remove_sensor_added_cb(sensord_added_cb cb)
-{
- m_sensor_added_callbacks.erase(cb);
-}
-
-void sensor_manager::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
-{
- m_sensor_removed_callbacks.emplace(cb, user_data);
-}
-
-void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb)
-{
- m_sensor_removed_callbacks.erase(cb);
-}
-
-bool sensor_manager::connect_channel(void)
-{
- ipc::message msg;
- GIOChannel *ch = NULL;
- bool ret = false;
-
- m_cmd_socket = new(std::nothrow) ipc::socket();
- retvm_if(!m_cmd_socket, false, "Failed to allocate memory");
-
- if (!m_cmd_socket->create(SENSOR_CHANNEL_PATH))
- goto fail;
-
- if (!m_cmd_socket->connect())
- goto fail;
-
- m_mon_socket = new(std::nothrow) ipc::socket();
- if (!m_mon_socket) {
- _E("Failed to allocate memory");
- goto fail;
- }
-
- if (!m_mon_socket->create(SENSOR_CHANNEL_PATH))
- goto fail;
-
- if (!m_mon_socket->connect())
- goto fail;
-
- ch = g_io_channel_unix_new(m_mon_socket->get_fd());
- retvm_if(!ch, false, "Failed to create g_io_channel_unix_new");
-
- g_src = g_io_create_watch(ch, (GIOCondition) (ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL));
- g_io_channel_unref(ch);
- if (!g_src) {
- _E("Failed to create g_io_create_watch");
- goto fail;
- }
-
- g_source_set_callback(g_src, (GSourceFunc) manager_handler, this, NULL);
- g_source_attach(g_src, g_main_loop_get_context(m_loop));
- g_source_unref(g_src);
-
- msg.set_type(CMD_MANAGER_CONNECT);
- ret = send(m_mon_socket, msg);
- if (!ret)
- goto fail;
-
- m_connected.store(true);
-
- _D("Connected");
- return true;
-
-fail:
- delete m_mon_socket;
- m_mon_socket = NULL;
-
- delete m_cmd_socket;
- m_cmd_socket = NULL;
-
- return false;
-}
-
-bool sensor_manager::connect(void)
-{
- retv_if(is_connected(), true);
- retv_if(!connect_channel(), false);
-
- return get_sensors_internal();
-}
-
-void sensor_manager::disconnect(void)
-{
- ret_if(!is_connected());
- m_connected.store(false);
-
- if (g_src && !g_source_is_destroyed(g_src)) {
- g_source_destroy(g_src);
- g_src = NULL;
- }
-
- delete m_mon_socket;
- m_mon_socket = NULL;
-
- delete m_cmd_socket;
- m_cmd_socket = NULL;
-
- _D("Disconnected");
-}
-
-bool sensor_manager::is_connected(void)
-{
- return m_connected.load();
-}
-
-void sensor_manager::restore(void)
-{
- ret_if(!is_connected());
-
- if (g_src && !g_source_is_destroyed(g_src)) {
- g_source_destroy(g_src);
- g_src = NULL;
- }
-
- delete m_mon_socket;
- m_mon_socket = NULL;
-
- delete m_cmd_socket;
- m_cmd_socket = NULL;
-
- m_connected.store(false);
- retm_if(!connect_channel(), "Failed to restore manager");
-
- _D("Restored manager");
-}
-
-void sensor_manager::decode_sensors(const char *buf, std::list<sensor_info> &infos)
-{
- int count = 0;
- sensor_info info;
- const int32_t *size;
- const char *data;
- cmd_manager_sensor_list_t *raw;
-
- raw = (cmd_manager_sensor_list_t *)buf;
- count = raw->sensor_cnt;
- size = (const int32_t *)raw->data;
- data = (const char *)raw->data + sizeof(int32_t);
-
- for (int i = 0; i < count; ++i) {
- info.clear();
- info.deserialize(data, size[0]);
- infos.push_back(info);
-
- size = (const int32_t *)((const char *)data + size[0]);
- data = (const char *)size + sizeof(int32_t);
- }
-
- _D("Sensor count : %d", count);
-}
-
-bool sensor_manager::get_sensors_internal(void)
-{
- retvm_if(!is_connected(), false, "Failed to get sensors");
-
- bool ret;
- ipc::message msg;
- ipc::message reply;
- char buf[MAX_BUF_SIZE];
-
- msg.set_type(CMD_MANAGER_SENSOR_LIST);
-
- ret = send(m_cmd_socket, msg);
- retvm_if(!ret, false, "Failed to send message");
-
- ret = read(m_cmd_socket, reply);
- retvm_if(!ret, false, "Failed to receive message");
-
- reply.disclose(buf, MAX_BUF_SIZE);
-
- if (!m_sensors.empty())
- m_sensors.clear();
-
- decode_sensors(buf, m_sensors);
-
- return true;
-}
-
-bool sensor_manager::has_privilege(std::string &uri)
-{
- retvm_if(!is_connected(), false, "Failed to get sensors");
-
- bool ret;
- ipc::message msg;
- ipc::message reply;
- cmd_has_privilege_t buf = {0, };
-
- msg.set_type(CMD_HAS_PRIVILEGE);
- memcpy(buf.sensor, uri.c_str(), uri.size());
- msg.enclose((const char *)&buf, sizeof(buf));
-
- ret = send(m_cmd_socket, msg);
- retvm_if(!ret, false, "Failed to send message");
-
- ret = read(m_cmd_socket, reply);
- retvm_if(!ret, false, "Failed to receive message");
-
- if (reply.header()->err == OP_SUCCESS)
- return true;
-
- _W("This client doesn't have the privilege for sensor[%s]", uri.c_str());
-
- return false;
-}
-
-sensor_info *sensor_manager::get_info(const char *uri)
-{
- if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
- return &m_sensors.front();
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- if ((*it).get_uri() != uri)
- continue;
-
- if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
- return &*it;
-
- return NULL;
- }
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- std::size_t found = (*it).get_uri().find_last_of("/");
- if (found == std::string::npos)
- continue;
- if ((*it).get_uri().substr(0, found) != uri)
- continue;
-
- if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
- return &*it;
- }
-
- return NULL;
-}
-
-std::vector<sensor_info *> sensor_manager::get_infos(const char *uri)
-{
- std::vector<sensor_info *> infos;
- bool all = false;
-
- if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
- all = true;
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- if ((*it).get_uri() != uri)
- continue;
-
- if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
- infos.push_back(&*it);
-
- return infos;
- }
-
- for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
- std::size_t found = (*it).get_uri().find_last_of("/");
- if (!all && found == std::string::npos)
- continue;
- if (!all && (*it).get_uri().substr(0, found) != uri)
- continue;
-
- if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
- infos.push_back(&*it);
- }
-
- return infos;
-}
-
-bool sensor_manager::read(ipc::message &msg)
-{
- return read(m_mon_socket, msg);
-}
-
-bool sensor_manager::read(ipc::socket *m_socket, ipc::message &msg)
-{
- AUTOLOCK(m_cmutex);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- ipc::message_header header;
- ssize_t size = 0;
- char buf[MAX_MSG_CAPACITY];
-
- /* header */
- size = m_socket->recv(&header, sizeof(ipc::message_header), false);
- if (size <= 0)
- return false;
-
- // check error from header
- if (header.err != 0) {
- msg.header()->err = header.err;
- return false;
- }
-
- /* body */
- if (header.length >= MAX_MSG_CAPACITY) {
- _E("header.length error %u", header.length);
- return false;
- }
-
- if (header.length > 0) {
- size = m_socket->recv(&buf, header.length, false);
- if (size <= 0)
- return false;
- }
-
- buf[header.length] = '\0';
- msg.enclose(reinterpret_cast<const void *>(buf), header.length);
- msg.set_type(header.type);
- msg.header()->err = header.err;
-
- return true;
-}
-
-bool sensor_manager::send(ipc::socket *m_socket, ipc::message &msg)
-{
- AUTOLOCK(m_cmutex);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- retvm_if(msg.size() >= MAX_MSG_CAPACITY, true, "Invaild message size[%u]", msg.size());
-
- ssize_t size = 0;
- char *buf = msg.body();
-
- /* header */
- size = m_socket->send(reinterpret_cast<void *>(msg.header()),
- sizeof(ipc::message_header), true);
- retvm_if(size <= 0, false, "Failed to send header");
-
- /* if body size is zero, skip to send body message */
- retv_if(msg.size() == 0, true);
-
- /* body */
- size = m_socket->send(buf, msg.size(), true);
- retvm_if(size <= 0, false, "Failed to send body");
-
- return true;
-}
-
-void sensor_manager::handle(ipc::message &msg)
-{
- switch (msg.header()->type) {
- case CMD_MANAGER_SENSOR_ADDED:
- on_sensor_added(msg);
- break;
- case CMD_MANAGER_SENSOR_REMOVED:
- on_sensor_removed(msg);
- break;
- }
-}
-
-void sensor_manager::on_sensor_added(ipc::message &msg)
-{
- ret_if(msg.header()->err < OP_SUCCESS);
-
- sensor_info info;
- info.clear();
- info.deserialize(msg.body(), msg.size());
-
- add_sensor(info);
-
- auto it = m_sensor_added_callbacks.begin();
- while (it != m_sensor_added_callbacks.end()) {
- it->first(info.get_uri().c_str(), it->second);
- ++it;
- }
-}
-
-void sensor_manager::on_sensor_removed(ipc::message &msg)
-{
- ret_if(msg.header()->err < 0);
- char uri[NAME_MAX] = {0, };
-
- msg.disclose(uri, NAME_MAX);
- remove_sensor(uri);
-
- auto it = m_sensor_removed_callbacks.begin();
- while (it != m_sensor_removed_callbacks.end()) {
- it->first(uri, it->second);
- ++it;
- }
-}
\ No newline at end of file
+++ /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_MANAGER_H__
-#define __SENSOR_MANAGER_H__
-
-#include <socket.h>
-#include <sensor-info.h>
-#include <event-loop.h>
-#include <sensor-internal.h>
-#include <sensor-provider.h>
-#include <lock.h>
-#include <glib.h>
-
-#include <list>
-#include <atomic>
-
-
-namespace sensor {
-
-class sensor_manager {
-public:
- sensor_manager();
- virtual ~sensor_manager();
-
- bool connect(void);
- void disconnect(void);
- void restore(void);
-
- int get_sensor(const char *uri, sensor_t *sensor);
- int get_sensors(const char *uri, sensor_t **list, int *count);
-
- bool is_supported(sensor_t sensor);
- bool is_supported(const char *uri);
-
- /* sensor provider */
- int add_sensor(sensor_info &info);
- int add_sensor(sensor_provider *provider);
- int remove_sensor(const char *uri);
- int remove_sensor(sensor_provider *provider);
-
- void add_sensor_added_cb(sensord_added_cb cb, void *user_data);
- void remove_sensor_added_cb(sensord_added_cb cb);
-
- void add_sensor_removed_cb(sensord_removed_cb cb, void *user_data);
- void remove_sensor_removed_cb(sensord_removed_cb cb);
-
- bool read(ipc::message &msg);
- void handle(ipc::message &msg);
-
-private:
- bool connect_channel(void);
- bool is_connected(void);
-
- void decode_sensors(const char *buf, std::list<sensor_info> &infos);
- bool get_sensors_internal(void);
-
- bool has_privilege(std::string &uri);
- sensor_info *get_info(const char *uri);
- std::vector<sensor_info *> get_infos(const char *uri);
-
- bool read(ipc::socket *m_socket, ipc::message &msg);
- bool send(ipc::socket *m_socket, ipc::message &msg);
-
- void on_sensor_added(ipc::message &msg);
- void on_sensor_removed(ipc::message &msg);
-
- ipc::socket *m_cmd_socket; /* get sensor information */
- ipc::socket *m_mon_socket; /* monitor sensors dinamically added/removed */
- GMainLoop *m_loop;
- GSource *g_src;
-
- cmutex m_cmutex;
- std::atomic<bool> m_connected;
-
- std::list<sensor_info> m_sensors;
- std::map<sensord_added_cb, void *> m_sensor_added_callbacks;
- std::map<sensord_removed_cb, void *> m_sensor_removed_callbacks;
-};
-
-}
-
-#endif /* __SENSOR_MANAGER_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.h"
-
-#include <glib.h>
-#include <message.h>
-#include <socket.h>
-#include <sensor-log-private.h>
-#include <sensor-types.h>
-#include <sensor-utils.h>
-#include <command-types.h>
-#include <lock.h>
-
-#include <cfloat>
-#include <cmath>
-
-#define DEFAULT_RESOLUTION 0.1
-
-using namespace sensor;
-
-static gboolean provider_handler(GIOChannel *ch, GIOCondition condition, gpointer data)
-{
- sensor_provider *p = (sensor_provider*) data;
- unsigned int cond = (unsigned int)condition;
-
- if (cond & (G_IO_HUP)) {
- p->restore();
- return false;
- }
-
- ipc::message msg;
- bool ret = p->read(msg);
- if (!ret)
- return false;
-
- p->handle(msg);
-
- return true;
-}
-
-sensor_provider::sensor_provider(const char *uri)
-: g_src(NULL)
-, m_socket(NULL)
-, m_connected(false)
-, m_start_cb(NULL)
-, m_stop_cb(NULL)
-, m_interval_changed_cb(NULL)
-, m_attribute_str_cb(NULL)
-, m_start_user_data(NULL)
-, m_stop_user_data(NULL)
-, m_interval_changed_user_data(NULL)
-, m_attribute_str_user_data(NULL)
-{
- init(uri);
- m_loop = g_main_loop_new(NULL, FALSE);
-}
-
-sensor_provider::~sensor_provider()
-{
- deinit();
-
- if (m_loop) {
- g_main_loop_quit(m_loop);
- g_main_loop_unref(m_loop);
- m_loop = NULL;
- }
-}
-
-bool sensor_provider::init(const char *uri)
-{
- m_sensor.set_uri(uri);
- m_sensor.set_min_range(-FLT_MAX);
- m_sensor.set_max_range(FLT_MAX);
- m_sensor.set_resolution(DEFAULT_RESOLUTION);
- /* TODO: temporary walkaround */
- const char *priv = sensor::utils::get_privilege(uri);
- m_sensor.set_privilege(priv);
-
- return true;
-}
-
-void sensor_provider::deinit(void)
-{
- disconnect();
-}
-
-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 = (char *) malloc(raw->size());
-
- if (!(*bytes)) {
- delete(raw);
- _E("Failed to allocate memory");
- return -ENOMEM;
- }
-
- 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);
- if (size < 0)
- return OP_ERROR;
-
- ipc::message msg((const char *)bytes, size);
- msg.set_type(CMD_PROVIDER_CONNECT);
-
- bool ret = send(msg);
- if (!ret)
- return OP_ERROR;
- return OP_SUCCESS;
-}
-
-int sensor_provider::connect(void)
-{
- m_socket = new(std::nothrow) ipc::socket();
- retvm_if(!m_socket, 0, "Failed to allocate memory");
-
- if (!m_socket->create(SENSOR_CHANNEL_PATH)) {
- delete m_socket;
- return 0;
- }
-
- if (!m_socket->connect())
- return 0;
-
- GIOChannel *ch = NULL;
-
- ch = g_io_channel_unix_new(m_socket->get_fd());
- retvm_if(!ch, 0, "Failed to create g_io_channel_unix_new");
-
- g_src = g_io_create_watch(ch, (GIOCondition) (ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL));
- g_io_channel_unref(ch);
- if (!g_src) {
- _E("Failed to create g_io_create_watch");
- return OP_ERROR;
- }
-
- g_source_set_callback(g_src, (GSourceFunc) provider_handler, this, NULL);
- g_source_attach(g_src, g_main_loop_get_context(m_loop));
- g_source_unref(g_src);
-
- /* serialize and send sensor info */
- send_sensor_info(get_sensor_info());
-
- /* check error */
- ipc::message reply;
-
- bool ret = read(reply);
- if (!ret)
- return OP_ERROR;
- retv_if(reply.header()->err < 0, reply.header()->err);
-
- m_connected.store(true);
-
- _I("Provider URI[%s]", get_uri());
-
- return OP_SUCCESS;
-}
-
-bool sensor_provider::disconnect(void)
-{
- AUTOLOCK(m_cmutex);
- retv_if(!is_connected(), false);
-
- if (g_src && !g_source_is_destroyed(g_src)) {
- g_source_destroy(g_src);
- g_src = NULL;
- }
-
- m_connected.store(false);
- delete m_socket;
- m_socket = NULL;
-
- _I("Disconnected[%s]", get_uri());
- return true;
-}
-
-bool sensor_provider::restore(void)
-{
- retv_if(!is_connected(), false);
- retvm_if(connect(), false, "Failed to restore provider");
-
- _D("Restored provider[%s]", get_uri());
- return true;
-}
-
-int sensor_provider::publish(const sensor_data_t &data)
-{
- ipc::message msg;
- msg.set_type(CMD_PROVIDER_PUBLISH);
- msg.enclose((const void *)(&data), sizeof(data));
-
- bool ret = send(msg);
- if (!ret)
- return OP_ERROR;
-
- return OP_SUCCESS;
-}
-
-int sensor_provider::publish(const sensor_data_t data[], const int count)
-{
- ipc::message msg;
- msg.set_type(CMD_PROVIDER_PUBLISH);
- msg.enclose((const void *)data, sizeof(sensor_data_t) * count);
-
- send(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_start_cb = cb;
- m_start_user_data = user_data;
-}
-
-void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data)
-{
- m_stop_cb = cb;
- m_stop_user_data = user_data;
-}
-
-void sensor_provider::set_interval_cb(sensord_provider_interval_changed_cb cb, void *user_data)
-{
- m_interval_changed_cb = cb;
- m_interval_changed_user_data = user_data;
-}
-
-void sensor_provider::set_attribute_str_cb(sensord_provider_attribute_str_cb cb, void *user_data)
-{
- m_attribute_str_cb = cb;
- m_attribute_str_user_data = user_data;
-}
-
-bool sensor_provider::read(ipc::message &msg)
-{
- AUTOLOCK(m_cmutex);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- ipc::message_header header;
- ssize_t size = 0;
- char buf[MAX_MSG_CAPACITY];
-
- /* header */
- size = m_socket->recv(&header, sizeof(ipc::message_header), false);
- if (size <= 0)
- return false;
-
- // check error from header
- if (header.err != 0) {
- msg.header()->err = header.err;
- return false;
- }
-
- /* body */
- if (header.length >= MAX_MSG_CAPACITY) {
- _E("header.length error %u", header.length);
- return false;
- }
-
- if (header.length > 0) {
- size = m_socket->recv(&buf, header.length, false);
- if (size <= 0)
- return false;
- }
-
- buf[header.length] = '\0';
- msg.enclose(reinterpret_cast<const void *>(buf), header.length);
- msg.set_type(header.type);
- msg.header()->err = header.err;
-
- return true;
-}
-
-void sensor_provider::handle(ipc::message &msg)
-{
- switch (msg.type()) {
- case CMD_PROVIDER_START:
- if (m_start_cb)
- m_start_cb(this, m_start_user_data);
- break;
- case CMD_PROVIDER_STOP:
- if (m_stop_cb)
- m_stop_cb(this, m_stop_user_data);
- break;
- case CMD_PROVIDER_ATTR_INT:
- cmd_provider_attr_int_t buf;
- msg.disclose((char *)&buf, sizeof(buf));
-
- if (buf.attribute == SENSORD_ATTRIBUTE_INTERVAL && m_interval_changed_cb)
- m_interval_changed_cb(this, buf.value, m_interval_changed_user_data);
- break;
- case CMD_PROVIDER_ATTR_STR:
- cmd_provider_attr_str_t *attr;
-
- attr = (cmd_provider_attr_str_t *) new(std::nothrow) char[msg.size()];
- retm_if(!attr, "Failed to allocate memory");
-
- msg.disclose((char *)attr, msg.size());
-
- if (m_attribute_str_cb)
- m_attribute_str_cb(this, attr->attribute, attr->value, attr->len, m_attribute_str_user_data);
-
- delete [] attr;
- break;
- }
-}
-
-bool sensor_provider::send(ipc::message &msg)
-{
- AUTOLOCK(m_cmutex);
- if (!m_socket) {
- _E("Socket is not connected");
- return false;
- }
-
- retvm_if(msg.size() >= MAX_MSG_CAPACITY, true, "Invaild message size[%u]", msg.size());
-
- ssize_t size = 0;
- char *buf = msg.body();
-
- /* header */
- size = m_socket->send(reinterpret_cast<void *>(msg.header()),
- sizeof(ipc::message_header), true);
- retvm_if(size <= 0, false, "Failed to send header");
-
- /* if body size is zero, skip to send body message */
- retv_if(msg.size() == 0, true);
-
- /* body */
- size = m_socket->send(buf, msg.size(), true);
- retvm_if(size <= 0, false, "Failed to send body");
-
- return true;
-}
+++ /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 <glib.h>
-#include <socket.h>
-#include <message.h>
-#include <event-loop.h>
-#include <sensor-internal.h>
-#include <sensor-info.h>
-#include <sensor-types.h>
-#include <lock.h>
-
-#include <map>
-#include <atomic>
-
-namespace sensor {
-
-class sensor_provider {
-public:
- sensor_provider(const char *uri);
- ~sensor_provider();
-
- const char *get_uri(void);
- sensor_info *get_sensor_info(void);
-
- int connect(void);
- bool disconnect(void);
- bool 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_interval_changed_cb cb, void *user_data);
- void set_attribute_str_cb(sensord_provider_attribute_str_cb cb, void *user_data);
-
- int publish(const sensor_data_t &data);
- int publish(const sensor_data_t data[], const int count);
-
- bool read(ipc::message &msg);
- bool send(ipc::message &msg);
- void handle(ipc::message &msg);
-
-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;
-
- GMainLoop *m_loop;
- GSource *g_src;
-
- ipc::socket *m_socket;
- std::atomic<bool> m_connected;
- sensor::cmutex m_cmutex;
-
- sensord_provider_start_cb m_start_cb;
- sensord_provider_stop_cb m_stop_cb;
- sensord_provider_interval_changed_cb m_interval_changed_cb;
- sensord_provider_attribute_str_cb m_attribute_str_cb;
-
- void *m_start_user_data;
- void *m_stop_user_data;
- void *m_interval_changed_user_data;
- void *m_attribute_str_user_data;
-};
-
-}
-
-#endif /* __SENSOR_PROVIDER_H__ */
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2018 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-reader.h"
-
-#include <sensor-log-private.h>
-#include <sensor-types.h>
-#include <chrono>
-
-using namespace sensor;
-
-sensor_reader::sensor_reader()
-: m_reader(NULL)
-, m_loop(NULL)
-, m_event_loop(NULL)
-, m_mutex()
-, m_cond()
-, m_running(false)
-{
- m_event_loop = new(std::nothrow) ipc::event_loop();
- m_reader = new(std::nothrow) std::thread(&sensor::sensor_reader::read_event, this);
- m_reader->detach();
-
- if (!m_running)
- wait_for_preparation();
-
- _I("Created");
-}
-
-sensor_reader::~sensor_reader()
-{
- _I("Destroying..");
- retm_if(!m_reader, "Invalid reader");
-
- m_running = false;
-
- m_event_loop->stop();
-
- delete m_reader;
- m_reader = NULL;
-
- delete m_event_loop;
- m_event_loop = NULL;
-
- _I("Destroyed");
-}
-
-GMainLoop *sensor_reader::get_event_loop(void)
-{
- retvm_if(!m_loop, NULL, "Invalid context");
-
- return m_loop;
-}
-
-void sensor_reader::wait_for_preparation(void)
-{
- std::unique_lock<std::mutex> lock(m_mutex);
- m_cond.wait_for(lock, std::chrono::seconds(1));
-}
-
-void sensor_reader::read_event(void)
-{
- _I("RUN");
- m_loop = g_main_loop_new(g_main_context_new(), false);
- m_event_loop->set_mainloop(m_loop);
-
- m_cond.notify_one();
- m_running = true;
-
- if (!m_event_loop->run())
- _E("Failed to run event loop");
-}
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2018 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_READER_H__
-#define __SENSOR_READER_H__
-
-#include <glib.h>
-#include <event-loop.h>
-
-#include <thread>
-#include <atomic>
-#include <condition_variable>
-
-namespace sensor {
-
-class sensor_reader {
-public:
- sensor_reader();
- ~sensor_reader();
-
- GMainLoop *get_event_loop(void);
-
-private:
- void wait_for_preparation(void);
- void read_event(void);
-
- std::thread *m_reader;
- GMainLoop *m_loop;
- ipc::event_loop *m_event_loop;
- std::mutex m_mutex;
- std::condition_variable m_cond;
- std::atomic<bool> m_running;
-};
-
-}
-
-#endif /* __SENSOR_READER_H__ */
+++ /dev/null
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-PROJECT(sensor-shared CXX)
-
-SET(DEPENDENTS "dlog libsystemd glib-2.0 gio-2.0 vconf libtzplatform-config")
-
-INCLUDE(FindPkgConfig)
-PKG_CHECK_MODULES(SHARED_PKGS REQUIRED ${DEPENDENTS})
-
-FOREACH(flag ${SHARED_PKGS_CFLAGS})
- SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
-ENDFOREACH(flag)
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
-
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
-
-FILE(GLOB_RECURSE SRCS *.cpp)
-ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS})
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SHARED_PKGS_LDFLAGS})
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
+++ /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 "channel-event-handler.h"
-
-#include "channel.h"
-#include "channel-handler.h"
-#include "sensor-log-private.h"
-
-using namespace ipc;
-
-channel_event_handler::channel_event_handler(channel *ch, channel_handler *handler)
-: m_ch(ch)
-, m_handler(handler)
-{
- _D("Create[%p]", this);
-}
-
-channel_event_handler::~channel_event_handler()
-{
- _D("Destroy[%p]", this);
- m_ch = NULL;
- m_handler = NULL;
-}
-
-bool channel_event_handler::handle(int fd, event_condition condition, void **data)
-{
- message msg;
-
- if (!m_ch || !m_ch->is_connected())
- return false;
-
- if (condition & (EVENT_HUP)) {
- //delete m_ch in g_io_handler to prevent double delete.
- if (data)
- *data = m_ch;
- m_ch = NULL;
- return false;
- }
-
- if (!m_ch->read_sync(msg, false)) {
- m_ch = NULL;
- return false;
- }
-
- return true;
-}
-
-void channel_event_handler::connected(channel *ch)
-{
- if (m_handler)
- m_handler->connected(ch);
-}
-
-void channel_event_handler::disconnected(channel *ch)
-{
- if (m_handler)
- m_handler->disconnected(ch);
-}
-
-void channel_event_handler::read(channel *ch, message &msg)
-{
- if (m_handler)
- m_handler->read(ch, msg);
-}
-
-void channel_event_handler::read_complete(channel *ch)
-{
- if (m_handler)
- m_handler->read_complete(ch);
-}
-
-void channel_event_handler::error_caught(channel *ch, int error)
-{
- if (m_handler)
- m_handler->error_caught(ch, error);
-}
+++ /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 __CHANNEL_EVENT_HANDLER_H__
-#define __CHANNEL_EVENT_HANDLER_H__
-
-#include <unordered_map>
-
-#include "event-handler.h"
-#include "channel-handler.h"
-
-namespace ipc {
-
-class channel;
-
-class channel_event_handler : public event_handler, public channel_handler {
-public:
- channel_event_handler(channel *ch, channel_handler *handler);
- virtual ~channel_event_handler();
-
- bool handle(int fd, event_condition condition, void **data);
-
- void connected(channel *ch);
- void disconnected(channel *ch);
- void read(channel *ch, message &msg);
- void read_complete(channel *ch);
- void error_caught(channel *ch, int error);
-
- void set_handler(int num, channel_handler* handler) {}
- void disconnect(void) {}
-
-private:
- channel *m_ch;
- channel_handler *m_handler;
-};
-
-}
-
-#endif /* __CHANNEL_EVENT_HANDLER_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.
- *
- */
-
-#ifndef __CHANNEL_HANDLER_H__
-#define __CHANNEL_HANDLER_H__
-
-namespace ipc {
-
-class channel;
-class message;
-class channel_handler;
-
-class channel_handler {
-public:
- virtual ~channel_handler() {}
-
- virtual void connected(channel *ch) = 0;
- virtual void disconnected(channel *ch) = 0;
- virtual void disconnect(void) = 0;
- virtual void read(channel *ch, message &msg) = 0;
- virtual void read_complete(channel *ch) = 0;
- virtual void error_caught(channel *ch, int error) = 0;
- virtual void set_handler(int num, channel_handler* handler) = 0;
-};
-
-}
-
-#endif /* __CHANNEL_HANDLER_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 "channel.h"
-
-#include <stdint.h>
-#include <unistd.h>
-#include <memory>
-#include <algorithm>
-
-#include "sensor-log-private.h"
-#include "channel-event-handler.h"
-#include "command-types.h"
-
-#define SYSTEMD_SOCK_BUF_SIZE (128*1024)
-
-using namespace ipc;
-using namespace sensor;
-
-class send_event_handler : public event_handler
-{
-public:
- send_event_handler(channel *ch, std::shared_ptr<message> msg)
- : m_ch(ch)
- , m_msg(msg)
- { }
-
- virtual ~send_event_handler()
- {
- if (m_ch) {
- m_ch->remove_pending_event_id(m_event_id);
- }
- }
-
- bool handle(int fd, event_condition condition, void** data)
- {
- if (!m_ch) {
- return false;
- }
-
- m_ch->remove_pending_event_id(m_event_id);
-
- if (!m_ch->is_connected()) {
- return false;
- }
-
- if (condition & (EVENT_IN | EVENT_HUP)) {
- return false;
- }
-
- if (!m_ch->send_sync(*m_msg)) {
- return false;
- }
-
- return false;
- }
-
-private:
- channel *m_ch;
- std::shared_ptr<message> m_msg;
-};
-
-class read_event_handler : public event_handler
-{
-public:
- read_event_handler(channel *ch)
- : m_ch(ch)
- { }
-
- bool handle(int fd, event_condition condition, void **data)
- {
- if (!m_ch) {
- return false;
- }
-
- m_ch->remove_pending_event_id(m_event_id);
-
- if (!m_ch->is_connected()) {
- return false;
- }
-
- if (condition & (EVENT_OUT | EVENT_HUP)) {
- return false;
- }
-
- message msg;
- if (!m_ch->read_sync(msg, false)) {
- return false;
- }
-
- return false;
- }
-
-private:
- channel *m_ch;
-};
-
-channel::channel(void)
-: m_fd(-1)
-, m_event_id(0)
-, m_socket(NULL)
-, m_handler(NULL)
-, m_loop(NULL)
-, m_connected(false)
-{
- _D("Create[%p]", this);
-}
-
-channel::~channel()
-{
- _D("Destroy[%p]", this);
- if (is_connected()) {
- disconnect();
- }
-}
-
-uint64_t channel::bind(void)
-{
- retv_if(!m_loop, 0);
- channel_event_handler* handler = dynamic_cast<channel_event_handler *>(m_handler);
-
- if (!handler) {
- _E("Failed to bind channel[%p] : handler[%p]", this, m_handler);
- m_event_id = 0;
- return 0;
- }
- m_event_id = m_loop->add_event(m_socket->get_fd(),
- (EVENT_IN | EVENT_HUP | EVENT_NVAL), handler);
-
- _D("Bind channel[%p] : handler[%p] event_id[%llu]", this, m_handler, m_event_id);
- return m_event_id;
-}
-
-uint64_t channel::bind(event_loop *loop, bool loop_bind)
-{
- m_loop = loop;
- m_connected.store(true);
-
- if (m_handler)
- m_handler->connected(this);
-
- if (loop_bind)
- bind();
-
- return m_event_id;
-}
-
-uint64_t channel::connect(channel_handler *handler, event_loop *loop, bool loop_bind)
-{
- m_socket = new(std::nothrow) socket();
- retvm_if(!m_socket, 0, "Failed to allocate memory");
-
- if (!m_socket->create(SENSOR_CHANNEL_PATH)) {
- delete m_socket;
- return 0;
- }
-
- if (!m_socket->connect())
- return 0;
-
- m_handler = new(std::nothrow) channel_event_handler(this, handler);
- retvm_if(!m_handler, 0, "Failed to allocate memory");
-
- bind(loop, loop_bind);
-
- _D("Connect channel[%p] : event id[%llu]", this, m_event_id);
- return m_event_id;
-}
-
-void channel::disconnect(void)
-{
- AUTOLOCK(m_cmutex);
- if (!is_connected()) {
- _D("Channel[%p] is not connected", this);
- return;
- }
-
- m_connected.store(false);
-
- _D("Disconnect channel[%p]", this);
-
- if (m_handler) {
- _D("Disconnect channel[%p] handler[%p]", this, m_handler);
- m_handler->disconnected(this);
- if (!m_loop)
- delete m_handler;
- m_handler = NULL;
- }
-
- if (m_loop) {
- for(auto id : m_pending_event_id) {
- _D("Remove channel[%p] pending event id[%llu]", this, id);
- m_loop->remove_event(id);
- }
- _D("Remove channel[%p] event[%llu]",this, m_event_id);
- m_loop->remove_event(m_event_id);
- m_event_id = 0;
- }
-
- if (m_socket) {
- _D("Release channel[%p] socket[%d]", this, m_socket->get_fd());
- delete m_socket;
- m_socket = NULL;
- }
- _D("Channel[%p] is disconnected", this);
-}
-
-bool channel::send(std::shared_ptr<message> msg)
-{
- int retry_cnt = 0;
- int cur_buffer_size = 0;
-
- retv_if(!m_loop, false);
-
- while (retry_cnt < 3) {
- cur_buffer_size = m_socket->get_current_buffer_size();
- if (cur_buffer_size <= SYSTEMD_SOCK_BUF_SIZE)
- break;
- usleep(3000);
- retry_cnt++;
- }
- retvm_if(retry_cnt >= 3, false, "Socket buffer[%d] is exceeded", cur_buffer_size);
-
- send_event_handler *handler = new(std::nothrow) send_event_handler(this, msg);
- retvm_if(!handler, false, "Failed to allocate memory");
-
- uint64_t event_id = m_loop->add_event(m_socket->get_fd(), (EVENT_OUT | EVENT_HUP | EVENT_NVAL), handler);
- if (event_id == 0) {
- _D("Failed to add send event handler");
- delete handler;
- return false;
- }
-
- m_pending_event_id.push_back(event_id);
- return true;
-}
-
-bool channel::send_sync(message &msg)
-{
- AUTOLOCK(m_cmutex);
- if (!is_connected()) {
- _D("Channel is not connected");
- return false;
- }
-
- retvm_if(msg.size() >= MAX_MSG_CAPACITY, true, "Invaild message size[%u]", msg.size());
-
- ssize_t size = 0;
- char *buf = msg.body();
-
- /* header */
- size = m_socket->send(reinterpret_cast<void *>(msg.header()),
- sizeof(message_header), true);
- retvm_if(size <= 0, false, "Failed to send header");
-
- /* if body size is zero, skip to send body message */
- retv_if(msg.size() == 0, true);
-
- /* body */
- size = m_socket->send(buf, msg.size(), true);
- retvm_if(size <= 0, false, "Failed to send body");
-
- return true;
-}
-
-bool channel::read(void)
-{
- retv_if(!m_loop, false);
-
- read_event_handler *handler = new(std::nothrow) read_event_handler(this);
- retvm_if(!handler, false, "Failed to allocate memory");
-
- uint64_t event_id = m_loop->add_event(m_socket->get_fd(), (EVENT_IN | EVENT_HUP | EVENT_NVAL), handler);
- if (event_id == 0) {
- _D("Failed to add read event handler");
- delete handler;
- return false;
- }
-
- m_pending_event_id.push_back(event_id);
- return true;
-}
-
-bool channel::read_sync(message &msg, bool select)
-{
- AUTOLOCK(m_cmutex);
- if (!is_connected()) {
- _D("Channel is not connected");
- return false;
- }
-
- message_header header;
- ssize_t size = 0;
- char buf[MAX_MSG_CAPACITY];
-
- /* header */
- size = m_socket->recv(&header, sizeof(message_header), select);
- if (size <= 0)
- return false;
-
- /* check error from header */
- if (m_handler && header.err != 0) {
- m_handler->error_caught(this, header.err);
- msg.header()->err = header.err;
- return true;
- }
-
- /* body */
- if (header.length >= MAX_MSG_CAPACITY) {
- _E("header.length error %u", header.length);
- return false;
- }
-
- if (header.length > 0) {
- size = m_socket->recv(&buf, header.length, select);
- if (size <= 0)
- return false;
- }
-
- buf[header.length] = '\0';
- msg.enclose(reinterpret_cast<const void *>(buf), header.length);
- msg.set_type(header.type);
- msg.header()->err = header.err;
-
- if (m_handler)
- m_handler->read(this, msg);
-
- return true;
-}
-
-bool channel::is_connected(void)
-{
- return m_connected.load();
-}
-
-bool channel::set_option(int type, int value)
-{
- switch (type) {
- case SO_SNDBUF:
- m_socket->set_buffer_size(type, value);
- break;
- case SO_RCVBUF:
- m_socket->set_buffer_size(type, value);
- break;
- default:
- break;
- }
-
- return true;
-}
-
-bool channel::get_option(int type, int &value) const
-{
- switch (type) {
- case 0:
- value = m_socket->get_current_buffer_size();
- break;
- case SO_SNDBUF:
- value = m_socket->get_buffer_size(type);
- break;
- case SO_RCVBUF:
- value = m_socket->get_buffer_size(type);
- break;
- default:
- break;
- }
-
- return true;
-}
-
-int channel::get_fd(void) const
-{
- return m_fd;
-}
-
-void channel::remove_pending_event_id(uint64_t id)
-{
- auto it = std::find(m_pending_event_id.begin(), m_pending_event_id.end(), id);
- if (it != m_pending_event_id.end()) {
- m_pending_event_id.erase(it);
- }
-}
+++ /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 __CHANNEL_H__
-#define __CHANNEL_H__
-
-#include <unistd.h>
-#include <atomic>
-#include <vector>
-
-#include "socket.h"
-#include "message.h"
-#include "event-loop.h"
-#include "channel-handler.h"
-#include "lock.h"
-
-namespace ipc {
-
-class channel_handler;
-
-class channel {
-public:
- /* move owernership of the socket to the channel */
- channel();
- ~channel();
-
- uint64_t bind(void);
- uint64_t bind(event_loop *loop, bool loop_bind);
-
- uint64_t connect(channel_handler *handler, event_loop *loop, bool loop_bind);
- void disconnect(void);
-
- bool is_connected(void);
-
- bool send(std::shared_ptr<message> msg);
- bool send_sync(message &msg);
-
- bool read(void);
- bool read_sync(message &msg, bool select = true);
-
- bool get_option(int type, int &value) const;
- bool set_option(int type, int value);
-
- int get_fd(void) const;
- void remove_pending_event_id(uint64_t id);
-
- event_loop *loop()
- {
- return m_loop;
- }
-
-private:
- int m_fd;
- uint64_t m_event_id;
- socket *m_socket;
- channel_handler *m_handler;
- event_loop *m_loop;
- std::vector<uint64_t> m_pending_event_id;
-
- std::atomic<bool> m_connected;
- sensor::cmutex m_cmutex;
-};
-
-}
-
-#endif /* __CHANNEL_H__ */
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2016 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 __COMMAND_TYPES_H__
-#define __COMMAND_TYPES_H__
-
-#include <sensor-types.h>
-#include <sensor-info.h>
-
-#define SENSOR_CHANNEL_PATH "/run/.sensord.socket"
-#define MAX_BUF_SIZE (16*1024)
-
-/* TODO: OOP - create serializer interface */
-enum cmd_type_e {
- CMD_DONE = -1,
- CMD_NONE = 0,
-
- /* Manager */
- CMD_MANAGER_CONNECT = 0x100,
- CMD_MANAGER_SENSOR_LIST,
- CMD_MANAGER_SENSOR_ADDED,
- CMD_MANAGER_SENSOR_REMOVED,
-
- /* Listener */
- CMD_LISTENER_EVENT = 0x200,
- CMD_LISTENER_ACC_EVENT,
- CMD_LISTENER_CONNECT,
- CMD_LISTENER_START,
- CMD_LISTENER_STOP,
- CMD_LISTENER_SET_ATTR_INT,
- CMD_LISTENER_SET_ATTR_STR,
- CMD_LISTENER_GET_DATA,
- CMD_LISTENER_GET_ATTR_INT,
- CMD_LISTENER_GET_ATTR_STR,
- CMD_LISTENER_GET_DATA_LIST,
- CMD_LISTENER_CONNECTED,
-
- /* Provider */
- CMD_PROVIDER_CONNECT = 0x300,
- CMD_PROVIDER_START,
- CMD_PROVIDER_STOP,
- CMD_PROVIDER_ATTR_INT,
- CMD_PROVIDER_PUBLISH,
- CMD_PROVIDER_ATTR_STR,
-
- /* Etc */
- CMD_HAS_PRIVILEGE = 0x1000,
-
- CMD_CNT,
-};
-
-typedef struct {
- int sensor_cnt;
- char data[0];
-} cmd_manager_sensor_list_t;
-
-typedef struct {
- int listener_id;
- char sensor[NAME_MAX];
-} cmd_listener_connect_t;
-
-typedef struct {
- int listener_id;
-} cmd_listener_start_t;
-
-typedef struct {
- int listener_id;
-} cmd_listener_stop_t;
-
-typedef struct {
- int listener_id;
- int attribute;
- int value;
-} cmd_listener_attr_int_t;
-
-typedef struct {
- int listener_id;
- int attribute;
- int len;
- char value[0];
-} cmd_listener_attr_str_t;
-
-typedef struct {
- int listener_id;
- int len;
- sensor_data_t data;
-} cmd_listener_get_data_t;
-
-typedef struct {
- int listener_id;
- int len;
- int data_count;
- sensor_data_t data[0];
-} cmd_listener_get_data_list_t;
-
-typedef struct {
- char info[0];
-} cmd_provider_connect_t;
-
-typedef struct {
- sensor_data_t data;
-} cmd_provider_publish_t;
-
-typedef struct {
- int attribute;
- int value;
-} cmd_provider_attr_int_t;
-
-typedef struct {
- int attribute;
- int len;
- char value[0];
-} cmd_provider_attr_str_t;
-
-typedef struct {
- char sensor[NAME_MAX];
-} cmd_has_privilege_t ;
-
-#endif /* __COMMAND_TYPES_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.
- *
- */
-
-#ifndef __EVENT_HANDLER_H__
-#define __EVENT_HANDLER_H__
-
-#include <stdint.h>
-namespace ipc {
-
-typedef unsigned int event_condition;
-
-class event_handler {
-public:
- event_handler()
- : m_event_id(0)
- {
- }
-
- virtual ~event_handler() {}
-
- virtual bool handle(int fd, event_condition condition, void **data) = 0;
- void set_event_id(uint64_t event_id)
- {
- m_event_id = event_id;
- }
-
-protected:
- uint64_t m_event_id;
-};
-
-}
-
-#endif /* __EVENT_HANDLER_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 "event-loop.h"
-
-#include <stdio.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <time.h>
-#include <sys/eventfd.h>
-#include <glib.h>
-
-#include <vector>
-#include <queue>
-
-#include "channel-event-handler.h"
-#include "sensor-log-private.h"
-#include "event-handler.h"
-#include "channel.h"
-
-#define BAD_HANDLE 0
-
-using namespace ipc;
-using namespace sensor;
-
-static std::vector<channel_handler*> channel_handler_release_list;
-static std::priority_queue<channel*> channel_release_queue;
-static sensor::cmutex release_lock;
-
-static void release_res()
-{
- AUTOLOCK(release_lock);
-
- channel *prev = NULL, *current = NULL;
- while (!channel_release_queue.empty()) {
- current = channel_release_queue.top();
- if (prev != current)
- delete current;
- prev = current;
- channel_release_queue.pop();
- }
-
- for (auto &it : channel_handler_release_list)
- delete it;
-
- /* To reduce memory allocation, swap to new data structure.
- This prevents occasional over-allocation of memory. */
- std::priority_queue<channel*>().swap(channel_release_queue);
- std::vector<channel_handler*>().swap(channel_handler_release_list);
-}
-
-static gboolean g_io_handler(GIOChannel *ch, GIOCondition condition, gpointer data)
-{
- uint64_t id;
- int fd;
- bool term;
- bool ret;
- event_loop *loop;
- event_handler *handler;
- unsigned int cond;
-
- cond = (unsigned int)condition;
-
- if (cond & (G_IO_HUP))
- cond &= ~(G_IO_IN | G_IO_OUT);
-
- handler_info *info = (handler_info *)data;
- loop = info->loop;
- handler = info->handler;
- retvm_if(!loop || !handler, FALSE, "Invalid event info");
-
- id = info->id;
- fd = info->fd;
- term = loop->is_terminator(fd);
-
- if (cond & G_IO_NVAL)
- return G_SOURCE_REMOVE;
-
- void *addr = NULL;
- ret = handler->handle(fd, (event_condition)cond, &addr);
-
- if (!ret && !term) {
- LOCK(release_lock);
- channel_release_queue.push((channel*)addr);
- UNLOCK(release_lock);
- if (!addr)
- loop->remove_event(id);
- ret = G_SOURCE_REMOVE;
- } else {
- ret = G_SOURCE_CONTINUE;
- }
-
- release_res();
-
- return ret;
-}
-
-static gint on_timer(gpointer data)
-{
- event_loop *loop = (event_loop *)data;
- loop->stop();
-
- return FALSE;
-}
-
-event_loop::event_loop()
-: m_mainloop(NULL)
-, m_running(false)
-, m_terminating(false)
-, m_sequence(1)
-, m_term_fd(-1)
-{
- m_mainloop = g_main_loop_new(NULL, FALSE);
-}
-
-event_loop::event_loop(GMainLoop *mainloop)
-: m_mainloop(NULL)
-, m_running(true)
-, m_terminating(false)
-, m_sequence(1)
-, m_term_fd(-1)
-{
- m_mainloop = mainloop;
-}
-
-event_loop::~event_loop()
-{
- if (m_term_fd != -1)
- close(m_term_fd);
-
- _D("Destoryed");
-}
-
-void event_loop::set_mainloop(GMainLoop *mainloop)
-{
- retm_if(!mainloop, "Invalid mainloop");
-
- m_mainloop = mainloop;
-}
-
-uint64_t event_loop::add_event(const int fd, const event_condition cond, event_handler *handler)
-{
- AUTOLOCK(m_cmutex);
- GIOChannel *ch = NULL;
- GSource *src = NULL;
-
- retvm_if(m_terminating.load(), BAD_HANDLE,
- "Failed to add event, because event_loop is being terminated");
-
- ch = g_io_channel_unix_new(fd);
- retvm_if(!ch, BAD_HANDLE, "Failed to create g_io_channel_unix_new");
-
- src = g_io_create_watch(ch, (GIOCondition)(cond));
- if (!src) {
- g_io_channel_unref(ch);
- _E("Failed to create g_io_create_watch");
- return BAD_HANDLE;
- }
-
- uint64_t id = m_sequence++;
- if (m_sequence == 0) {
- m_sequence = 1;
- }
-
- handler_info *info = new(std::nothrow) handler_info(id, fd, ch, src, handler, this);
- retvm_if(!info, BAD_HANDLE, "Failed to allocate memory");
-
- handler->set_event_id(id);
- g_source_set_callback(src, (GSourceFunc) g_io_handler, info, NULL);
- g_source_attach(src, g_main_loop_get_context(m_mainloop));
-
- m_handlers[id] = info;
-
- /* _D("Added event[%llu], fd[%d]", id, fd); */
- return id;
-}
-
-struct idler_data {
- void (*m_fn)(size_t, void*);
- void* m_data;
-};
-
-size_t event_loop::add_idle_event(unsigned int priority, void (*fn)(size_t, void*), void* data)
-{
- AUTOLOCK(m_cmutex);
- GSource *src;
-
- retvm_if(m_terminating.load(), 0,
- "Failed to remove event, because event_loop is terminated");
-
- src = g_idle_source_new();
- retvm_if(!src, 0, "Failed to allocate memory");
-
- idler_data *id = new idler_data();
- id->m_fn = fn;
- id->m_data = data;
-
- g_source_set_callback(src, [](gpointer gdata) -> gboolean {
- idler_data *id = (idler_data *)gdata;
- id->m_fn((size_t)id, id->m_data);
- delete id;
- return G_SOURCE_REMOVE;
- }, id, NULL);
-
- g_source_attach(src, g_main_loop_get_context (m_mainloop));
- g_source_unref(src);
-
- return (size_t)id;
-}
-
-bool event_loop::remove_event(uint64_t id)
-{
- AUTOLOCK(m_cmutex);
- auto it = m_handlers.find(id);
- retv_if(it == m_handlers.end(), false);
-
- release_info(it->second);
- m_handlers.erase(id);
-
- /* _D("Removed event[%llu]", id); */
- return true;
-}
-
-void event_loop::remove_all_events(void)
-{
- AUTOLOCK(m_cmutex);
- auto it = m_handlers.begin();
- while (it != m_handlers.end()) {
- release_info(it->second);
- it = m_handlers.erase(it);
- }
-}
-
-void event_loop::release_info(handler_info *info)
-{
- retm_if(!info->g_ch || info->id == 0, "Invalid handler information");
- /* _D("Releasing event..[%llu]", info->id); */
-
- g_source_destroy(info->g_src);
- g_source_unref(info->g_src);
-
- g_io_channel_unref(info->g_ch);
-
- info->g_ch = NULL;
- delete info->handler;
- info->handler = NULL;
-
- delete info;
-
- /* _D("Released event[%llu]", info->id); */
-}
-
-void event_loop::add_channel_release_queue(channel *ch)
-{
- AUTOLOCK(release_lock);
- channel_release_queue.push(ch);
-}
-
-void event_loop::add_channel_handler_release_list(channel_handler *handler)
-{
- AUTOLOCK(release_lock);
- channel_handler_release_list.push_back(handler);
-}
-
-class terminator : public event_handler
-{
-public:
- terminator(event_loop *loop)
- : m_loop(loop)
- { }
-
- bool handle(int fd, event_condition condition, void **data)
- {
- m_loop->terminate();
- return false;
- }
-
-private:
- event_loop *m_loop;
-};
-
-bool event_loop::run(int timeout)
-{
- retvm_if(!m_mainloop, false, "Invalid GMainLoop");
- retvm_if(is_running(), false, "Already started");
-
- if (timeout > 0) {
- GSource *src = g_timeout_source_new(timeout);
- g_source_set_callback(src, on_timer, this, NULL);
- g_source_attach(src, g_main_loop_get_context(m_mainloop));
- g_source_unref(src);
- }
-
- m_term_fd = eventfd(0, EFD_CLOEXEC);
- retv_if(m_term_fd == -1, false);
-
- terminator *handler = new(std::nothrow) terminator(this);
- retvm_if(!handler, false, "Failed to allocate memory");
-
- add_event(m_term_fd, EVENT_IN | EVENT_HUP | EVENT_NVAL, handler);
-
- m_running.store(true);
-
- _I("Started");
- g_main_loop_run(m_mainloop);
-
- return true;
-}
-
-void event_loop::stop(void)
-{
- ret_if(!is_running() || m_terminating.load());
-
- terminate();
-}
-
-void event_loop::terminate(void)
-{
- remove_all_events();
-
- if (m_mainloop) {
- g_main_loop_quit(m_mainloop);
- g_main_loop_unref(m_mainloop);
- m_mainloop = NULL;
- }
-
- m_running.store(false);
- m_terminating.store(false);
-
- _I("Terminated");
-}
-
-bool event_loop::is_running(void)
-{
- return m_running.load();
-}
-
-bool event_loop::is_terminator(int fd)
-{
- return (m_term_fd == fd);
-}
+++ /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 __EVENT_LOOP_H__
-#define __EVENT_LOOP_H__
-
-#include <stdint.h>
-#include <glib.h>
-#include <atomic>
-#include <map>
-
-#include "event-handler.h"
-#include "lock.h"
-
-namespace ipc {
-
-class channel;
-class channel_handler;
-
-enum event_condition_e {
- EVENT_IN = G_IO_IN,
- EVENT_OUT = G_IO_OUT,
- EVENT_HUP = G_IO_HUP,
- EVENT_NVAL = G_IO_NVAL,
-};
-
-/* move it to file */
-class idle_handler {
- virtual ~idle_handler();
- bool handle(int fd);
-};
-
-class event_loop;
-
-class handler_info {
-public:
- handler_info(uint64_t _id, int _fd, GIOChannel *_ch, GSource *_src, event_handler *_handler, event_loop *_loop)
- : id(_id)
- , fd(_fd)
- , g_ch(_ch)
- , g_src(_src)
- , handler(_handler)
- , loop(_loop)
- {}
-
- uint64_t id;
- int fd;
- GIOChannel *g_ch;
- GSource *g_src;
- event_handler *handler;
- event_loop *loop;
-};
-
-class event_loop {
-public:
- typedef unsigned int event_condition;
- typedef bool (*idle_cb)(void *);
-
- event_loop();
- event_loop(GMainLoop *mainloop);
- ~event_loop();
-
- void set_mainloop(GMainLoop *mainloop);
-
- uint64_t add_event(const int fd, const event_condition cond, event_handler *handler);
- size_t add_idle_event(unsigned int priority, void (*fn)(size_t, void*), void* data);
-
- bool remove_event(uint64_t id);
- void remove_all_events(void);
- void release_info(handler_info *info);
-
- void add_channel_release_queue(channel *ch);
- void add_channel_handler_release_list(channel_handler *handler);
-
- bool run(int timeout = 0);
- void stop(void);
- void terminate(void);
-
- bool is_running(void);
- bool is_terminator(int fd);
-
-private:
- GMainLoop *m_mainloop;
- std::atomic<bool> m_running;
- std::atomic<bool> m_terminating;
- std::atomic<uint64_t> m_sequence;
- std::map<uint64_t, handler_info *> m_handlers;
-
- int m_term_fd;
- sensor::cmutex m_cmutex;
-};
-
-}
-
-#endif /* __EVENT_LOOP_H__ */
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2013 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 <pthread.h>
-#include <stdio.h>
-#include <sys/time.h>
-#include <lock.h>
-#include <sensor-log-private.h>
-
-using namespace sensor;
-
-cmutex::cmutex()
-{
- pthread_mutexattr_t mutex_attr;
- pthread_mutexattr_init(&mutex_attr);
- pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&m_mutex, &mutex_attr);
- pthread_mutexattr_destroy(&mutex_attr);
-}
-
-cmutex::~cmutex()
-{
- pthread_mutex_destroy(&m_mutex);
-}
-
-void cmutex::lock(void)
-{
- pthread_mutex_lock(&m_mutex);
-}
-
-void cmutex::unlock(void)
-{
- pthread_mutex_unlock(&m_mutex);
-}
-
-int cmutex::try_lock(void)
-{
- return pthread_mutex_trylock(&m_mutex);
-}
-
-Autolock::Autolock(cmutex &m)
-: m_lock(m)
-{
- m_lock.lock();
-}
-
-Autolock::~Autolock()
-{
- m_lock.unlock();
-}
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2013 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 _LOCK_H_
-#define _LOCK_H_
-
-#include <pthread.h>
-
-namespace sensor {
-
-#ifndef MICROSECONDS
-#define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec)
-#endif
-
-#define AUTOLOCK(x) Autolock x##_autolock((x))
-#define LOCK(x) (x).lock()
-#define UNLOCK(x) (x).unlock()
-
-class cmutex {
-public:
- cmutex();
- virtual ~cmutex();
-
- void lock(void);
- void unlock(void);
- int try_lock(void);
-
-private:
- pthread_mutex_t m_mutex;
- static const int OWNER_INFO_LEN = 256;
- char m_owner_info[OWNER_INFO_LEN];
-};
-
-class Autolock {
-private:
- cmutex& m_lock;
-public:
- Autolock(cmutex &m);
- ~Autolock();
-};
-
-}
-#endif /* _LOCK_H_ */
+++ /dev/null
-/*
- * sensord
- *
- * Copyright (c) 2014 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.
- *
- */
-
-#pragma once
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#define _cleanup_(x) __attribute__((cleanup(x)))
-
-static inline void __freep(void *p)
-{
- free(*(void**) p);
-}
-
-static inline void __closep(int *fd)
-{
- if (*fd >= 0)
- close(*fd);
-}
-
-static inline void __fclosep(FILE **f)
-{
- if (*f)
- fclose(*f);
-}
-
-static inline void int_to_bytes(int value, int width, char data[])
-{
- /* Big-endian */
- for (int i = width - 1; i >= 0; i--) {
- data[i] = (value & 0xff);
- value = value >> 8;
- }
-}
-
-static inline unsigned int bytes_to_uint(char data[], int &cursor, int width)
-{
- unsigned int sum = 0;
-
- /* Big-endian */
- for (int i = 0; i < width; ++i)
- sum = (sum << 8) + (unsigned char)data[cursor++];
-
- return sum;
-}
-
-#define _cleanup_free_ _cleanup_(__freep)
-#define _cleanup_close_ _cleanup_(__closep)
-#define _cleanup_fclose_ _cleanup_(__fclosep)
-#define GET_BYTES(value, width, data) int_to_bytes(value, width, data)
-#define GET_UINT(data, cursor, width) bytes_to_uint(data, cursor, width)
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+++ /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 "message.h"
-
-#include <sensor-log-private.h>
-#include <atomic>
-
-using namespace ipc;
-
-#define UNDEFINED_TYPE -2
-
-static std::atomic<uint64_t> sequence(0);
-
-message::message(size_t capacity)
- : m_size(0)
- , m_capacity(capacity)
- , m_msg((char *)malloc(sizeof(char) * capacity))
-{
- m_header.id = sequence++;
- m_header.type = UNDEFINED_TYPE;
- m_header.length = m_size;
- m_header.err = 0;
-}
-
-message::message(const void *msg, size_t sz)
- : m_size(sz)
- , m_capacity(sz)
- , m_msg((char *)msg)
-{
- m_header.id = sequence++;
- m_header.type = UNDEFINED_TYPE;
- m_header.length = m_size;
- m_header.err = 0;
-}
-
-message::message(const message &msg)
- : m_size(msg.m_size)
- , m_capacity(msg.m_capacity)
- , m_msg((char *)malloc(sizeof(char) * msg.m_capacity))
-{
- ::memcpy(&m_header, &msg.m_header, sizeof(message_header));
- ::memcpy(m_msg, msg.m_msg, msg.m_size);
-}
-
-message::message(int error)
- : m_size(0)
- , m_capacity(0)
- , m_msg(NULL)
-{
- m_header.id = sequence++;
- m_header.type = UNDEFINED_TYPE;
- m_header.length = 0;
- m_header.err = error;
-}
-
-message::~message()
-{
- if (m_msg) {
- free(m_msg);
- m_msg = NULL;
- }
-}
-
-void message::enclose(const void *msg, const size_t sz)
-{
- if (!msg || sz == 0)
- return;
-
- if (m_capacity < sz)
- return;
-
- ::memcpy(reinterpret_cast<char *>(m_msg), msg, sz);
- m_size = sz;
- m_header.length = sz;
-}
-
-void message::enclose(int error)
-{
- m_header.err = error;
- m_header.length = 0;
- m_size = 0;
-}
-
-void message::disclose(void *msg, const size_t size)
-{
- if (!msg || !m_msg || m_size > size)
- return;
-
- ::memcpy(msg, m_msg, m_size);
-}
-
-uint32_t message::type(void)
-{
- return m_header.type;
-}
-
-void message::set_type(uint32_t msg_type)
-{
- m_header.type = msg_type;
-}
-
-size_t message::size(void)
-{
- return m_size;
-}
-
-message_header *message::header(void)
-{
- return &m_header;
-}
-
-char *message::body(void)
-{
- return m_msg;
-}
+++ /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 __MESSAGE_H__
-#define __MESSAGE_H__
-
-#include <stdlib.h> /* size_t */
-#include <atomic>
-#include <memory>
-
-#define MAX_MSG_CAPACITY (32*1024)
-#define MAX_HEADER_RESERVED 3
-
-namespace ipc {
-
-typedef struct message_header {
- uint64_t id { 0 };
- uint32_t type { 0 };
- size_t length { 0 };
- int32_t err { 0 };
- void *ancillary[MAX_HEADER_RESERVED] { nullptr };
-} message_header;
-
-class message {
-public:
- template <class... Args>
- static std::shared_ptr<message> create(Args&&... args)
- noexcept(noexcept(message(std::forward<Args>(args)...)))
- {
- return std::shared_ptr<message>(new (std::nothrow) message(std::forward<Args>(args)...));
- }
-
- message(size_t capacity = MAX_MSG_CAPACITY);
- message(const void *msg, size_t size);
- message(const message &msg);
- message(int err);
- ~message();
-
- void enclose(const void *msg, const size_t size);
- void enclose(int error);
- void disclose(void *msg, const size_t size);
-
- uint32_t type(void);
- void set_type(uint32_t type);
-
- size_t size(void);
-
- void ref(void);
- void unref(void);
- int ref_count(void);
-
- message_header *header(void);
- char *body(void);
-
-private:
- message_header m_header;
- size_t m_size;
- size_t m_capacity;
-
- char *m_msg;
-};
-
-}
-
-#endif /* __MESSAGE_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-info.h"
-
-#include <sensor-types.h>
-#include <sensor-log-private.h>
-#include <sensor-utils.h>
-
-#include <cfloat>
-#include <algorithm>
-#include <string>
-#include <cmath>
-
-
-#define MIN_RANGE -FLT_MAX
-#define MAX_RANGE FLT_MAX
-
-using namespace sensor;
-
-sensor_info::sensor_info()
-: m_type(UNKNOWN_SENSOR)
-, m_uri(SENSOR_UNKNOWN_NAME)
-, m_model(SENSOR_UNKNOWN_NAME)
-, m_vendor(SENSOR_UNKNOWN_NAME)
-, m_min_range(0)
-, m_max_range(0)
-, m_resolution(0)
-, m_min_interval(0)
-, m_max_interval(0)
-, m_max_batch_count(0)
-, m_wakeup_supported(false)
-, m_privilege("")
-{
-}
-
-sensor_info::sensor_info(const sensor_info &info)
-: m_type(info.m_type)
-, m_uri(info.m_uri)
-, m_model(info.m_model)
-, m_vendor(info.m_vendor)
-, m_min_range(info.m_min_range)
-, m_max_range(info.m_max_range)
-, m_resolution(info.m_resolution)
-, m_min_interval(info.m_min_interval)
-, m_max_interval(info.m_max_interval)
-, m_max_batch_count(info.m_max_batch_count)
-, m_wakeup_supported(info.m_wakeup_supported)
-, m_privilege(info.m_privilege)
-{
-}
-
-sensor_info::sensor_info(const sensor_info_t &info)
-{
- /* TODO: HAL should change name from single name to URI */
- const char *type = sensor::utils::get_uri((sensor_type_t)info.type);
- std::string uri(type);
- uri.append("/").append(info.name);
-
- set_type((sensor_type_t)info.type);
- set_uri(uri.c_str());
- set_model(info.model_name);
- set_vendor(info.vendor);
- set_min_range(info.min_range);
- set_max_range(info.max_range);
- set_resolution(info.resolution);
- set_min_interval(info.min_interval);
- set_max_interval(info.max_interval);
- set_max_batch_count(info.max_batch_count);
- set_wakeup_supported(info.wakeup_supported);
- /* TODO: sensor_info_t should have privilege string */
- set_privilege("");
-}
-
-sensor_info::sensor_info(const sensor_info2_t &info)
-{
- std::string uri(info.uri);
- std::size_t found = uri.find_last_of("/\\");
-
- set_type(info.type);
- set_uri(uri.c_str());
- set_model(uri.substr(found + 1, uri.length()).c_str());
- set_vendor(info.vendor);
- set_min_range(info.min_range);
- set_max_range(info.max_range);
- set_resolution(info.resolution);
- set_min_interval(info.min_interval);
- set_max_interval(info.max_interval);
- set_max_batch_count(info.max_batch_count);
- set_wakeup_supported(info.wakeup_supported);
- set_privilege(info.privilege);
-}
-
-sensor_type_t sensor_info::get_type(void)
-{
- return m_type;
-}
-
-std::string &sensor_info::get_uri(void)
-{
- return m_uri;
-}
-
-std::string &sensor_info::get_model(void)
-{
- return m_model;
-}
-
-std::string &sensor_info::get_vendor(void)
-{
- return m_vendor;
-}
-
-float sensor_info::get_min_range(void)
-{
- return m_min_range;
-}
-
-float sensor_info::get_max_range(void)
-{
- return m_max_range;
-}
-
-float sensor_info::get_resolution(void)
-{
- return m_resolution;
-}
-
-int sensor_info::get_min_interval(void)
-{
- return m_min_interval;
-}
-
-int sensor_info::get_max_interval(void)
-{
- return m_max_interval;
-}
-
-int sensor_info::get_max_batch_count(void)
-{
- return m_max_batch_count;
-}
-
-bool sensor_info::is_wakeup_supported(void)
-{
- return m_wakeup_supported;
-}
-
-std::string &sensor_info::get_privilege(void)
-{
- return m_privilege;
-}
-
-void sensor_info::set_type(sensor_type_t type)
-{
- m_type = type;
-}
-
-void sensor_info::set_uri(const char *name)
-{
- m_uri = name;
-}
-
-void sensor_info::set_model(const char *model)
-{
- m_model = model;
-}
-
-void sensor_info::set_vendor(const char *vendor)
-{
- m_vendor = vendor;
-}
-
-void sensor_info::set_min_range(float min_range)
-{
- m_min_range = min_range;
-
- if (!std::isnormal(m_min_range))
- m_min_range = 0; /* set value to 0 when the value is NaN, infinity, zero or subnormal */
- if (m_min_range < MIN_RANGE)
- m_min_range = MIN_RANGE;
- if (m_min_range > MAX_RANGE)
- m_min_range = MAX_RANGE;
-}
-
-void sensor_info::set_max_range(float max_range)
-{
- m_max_range = max_range;
-
- if (!std::isnormal(m_max_range))
- m_max_range = 0; /* set value to 0 when the value is NaN, infinity, zero or subnormal */
- if (m_max_range < MIN_RANGE)
- m_max_range = MIN_RANGE;
- if (m_max_range > MAX_RANGE)
- m_max_range = MAX_RANGE;
-}
-
-void sensor_info::set_resolution(float resolution)
-{
- m_resolution = resolution;
-}
-
-void sensor_info::set_min_interval(int min_interval)
-{
- m_min_interval = min_interval;
-}
-
-void sensor_info::set_max_interval(int max_interval)
-{
- m_max_interval = max_interval;
-}
-
-void sensor_info::set_max_batch_count(int max_batch_count)
-{
- m_max_batch_count = max_batch_count;
-}
-
-void sensor_info::set_wakeup_supported(bool supported)
-{
- m_wakeup_supported = supported;
-}
-
-void sensor_info::set_privilege(const char *privilege)
-{
- m_privilege = privilege;
-}
-
-void sensor_info::add_privilege(const char *privilege)
-{
- if (!m_privilege.empty())
- m_privilege.append(PRIV_DELIMITER);
- m_privilege.append(privilege);
-}
-
-void sensor_info::serialize(raw_data_t &data)
-{
- put(data, m_type);
- put(data, m_uri);
- put(data, m_model);
- put(data, m_vendor);
- put(data, m_min_range);
- put(data, m_max_range);
- put(data, m_resolution);
- put(data, m_min_interval);
- //put(data, m_max_interval); to do
- put(data, m_max_batch_count);
- put(data, m_wakeup_supported);
- put(data, m_privilege);
-}
-
-void sensor_info::deserialize(const char *data, int data_len)
-{
- int type;
-
- raw_data_t raw_data(&data[0], &data[data_len]);
- auto it = raw_data.begin();
- it = get(it, type);
- m_type = (sensor_type_t)type;
-
- it = get(it, m_uri);
- it = get(it, m_model);
- it = get(it, m_vendor);
- it = get(it, m_min_range);
- it = get(it, m_max_range);
- it = get(it, m_resolution);
- it = get(it, m_min_interval);
- //it = get(it, m_max_interval); to do
- it = get(it, m_max_batch_count);
- it = get(it, m_wakeup_supported);
- it = get(it, m_privilege);
-}
-
-void sensor_info::show(void)
-{
- _I("URI = %s", m_uri.c_str());
- _I("Model = %s", m_model.c_str());
- _I("Vendor = %s", m_vendor.c_str());
- _I("Min_range = %f", m_min_range);
- _I("Max_range = %f", m_max_range);
- _I("Resolution = %f", m_resolution);
- _I("Min_interval = %d", m_min_interval);
- _I("Max_batch_count = %d", m_max_batch_count);
- _I("Wakeup_supported = %d", m_wakeup_supported);
- _I("Privilege = %s", m_privilege.c_str());
-}
-
-void sensor_info::clear(void)
-{
- m_type = UNKNOWN_SENSOR;
- m_uri.clear();
- m_model.clear();
- m_vendor.clear();
- m_min_range = 0.0f;
- m_max_range = 0.0f;
- m_resolution = 0.0f;
- m_min_interval = 0;
- m_max_interval = 0;
- m_max_batch_count = 0;
- m_wakeup_supported = false;
- m_privilege.clear();
-}
-
-void sensor_info::put(raw_data_t &data, int value)
-{
- char buffer[sizeof(value)];
-
- int *temp = reinterpret_cast<int *>(buffer);
- *temp = value;
-
- copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
-}
-
-void sensor_info::put(raw_data_t &data, unsigned int value)
-{
- char buffer[sizeof(value)];
-
- unsigned int *temp = reinterpret_cast<unsigned int *>(buffer);
- *temp = value;
-
- copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
-}
-
-void sensor_info::put(raw_data_t &data, int64_t value)
-{
- char buffer[sizeof(value)];
-
- int64_t *temp = reinterpret_cast<int64_t *>(buffer);
- *temp = value;
-
- copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
-}
-
-void sensor_info::put(raw_data_t &data, float value)
-{
- char buffer[sizeof(value)];
-
- float *temp = reinterpret_cast<float *>(buffer);
- *temp = value;
-
- copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
-}
-
-void sensor_info::put(raw_data_t &data, std::string &value)
-{
- put(data, (int) value.size());
-
- copy(value.begin(), value.end(), back_inserter(data));
-}
-
-void sensor_info::put(raw_data_t &data, bool value)
-{
- char buffer[sizeof(value)];
-
- bool *temp = (bool *) buffer;
- *temp = value;
-
- copy(&buffer[0], &buffer[sizeof(buffer)], back_inserter(data));
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, int &value)
-{
- copy(it, it + sizeof(value), (char*) &value);
-
- return it + sizeof(value);
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, unsigned int &value)
-{
- copy(it, it + sizeof(value), (char*) &value);
-
- return it + sizeof(value);
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, int64_t &value)
-{
- copy(it, it + sizeof(value), (char*) &value);
-
- return it + sizeof(value);
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, float &value)
-{
- copy(it, it + sizeof(value), (char*) &value);
-
- return it + sizeof(value);
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, std::string &value)
-{
- int len;
-
- it = get(it, len);
-
- copy(it, it + len, back_inserter(value));
-
- return it + len;
-}
-
-raw_data_iterator sensor_info::get(raw_data_iterator it, bool &value)
-{
- copy(it, it + sizeof(value), (char*) &value);
-
- return it + sizeof(value);
-}
+++ /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_INFO_H__
-#define __SENSOR_INFO_H__
-
-#include <stdint.h>
-#include <string>
-#include <vector>
-#include <hal/hal-sensor-types.h>
-#include <sensor-types.h>
-
-namespace sensor {
-
-typedef std::vector<char> raw_data_t;
-typedef raw_data_t::iterator raw_data_iterator;
-
-/* TODO: builder */
-class sensor_info {
-public:
- sensor_info();
- sensor_info(const sensor_info &info);
- sensor_info(const sensor_info_t &info);
- sensor_info(const sensor_info2_t &info);
-
- /* TODO: it would be better to return type(URI) */
- sensor_type_t get_type(void);
- std::string &get_uri(void);
- std::string &get_model(void);
- std::string &get_vendor(void);
- float get_min_range(void);
- float get_max_range(void);
- float get_resolution(void);
- int get_min_interval(void);
- int get_max_interval(void);
- int get_max_batch_count(void);
- bool is_wakeup_supported(void);
- std::string &get_privilege(void);
-
- void set_type(sensor_type_t type);
- void set_uri(const char *name);
- void set_model(const char *name);
- void set_vendor(const char *vendor);
- void set_min_range(float min_range);
- void set_max_range(float max_range);
- void set_resolution(float resolution);
- void set_min_interval(int min_interval);
- void set_max_interval(int max_interval);
- void set_max_batch_count(int max_batch_count);
- void set_wakeup_supported(bool supported);
- void set_privilege(const char *privilege);
- void add_privilege(const char *privilege);
-
- void clear(void);
-
- void serialize(raw_data_t &data);
- void deserialize(const char *data, int data_len);
- void show(void);
-
-private:
- sensor_type_t m_type;
- std::string m_uri;
- std::string m_model;
- std::string m_vendor;
- float m_min_range;
- float m_max_range;
- float m_resolution;
- int m_min_interval;
- int m_max_interval;
- int m_max_batch_count;
- bool m_wakeup_supported;
- std::string m_privilege;
-
- /* TODO: use template */
- void put(raw_data_t &data, int value);
- void put(raw_data_t &data, unsigned int value);
- void put(raw_data_t &data, int64_t value);
- void put(raw_data_t &data, float value);
- void put(raw_data_t &data, std::string &value);
- void put(raw_data_t &data, bool value);
-
- /* TODO: use template */
- raw_data_iterator get(raw_data_iterator it, int &value);
- raw_data_iterator get(raw_data_iterator it, unsigned int &value);
- raw_data_iterator get(raw_data_iterator it, int64_t &value);
- raw_data_iterator get(raw_data_iterator it, float &value);
- raw_data_iterator get(raw_data_iterator it, std::string &value);
- raw_data_iterator get(raw_data_iterator it, bool &value);
-};
-
-}
-
-#endif /* __SENSOR_INFO_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-utils.h"
-
-#include <glib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
-#include <stddef.h>
-#include <map>
-
-#include <sensor-log-private.h>
-#include <sensor-types.h>
-
-#ifndef PATH_MAX
-#define PATH_MAX 256
-#endif
-
-/* TODO: move and define string type to sensor_type.h */
-static std::map<sensor_type_t, const char *> types = {
- {UNKNOWN_SENSOR, "http://tizen.org/sensor/general/unknown"},
- {ALL_SENSOR, "http://tizen.org/sensor/general/all"},
- {ACCELEROMETER_SENSOR, "http://tizen.org/sensor/general/accelerometer"},
- {GRAVITY_SENSOR, "http://tizen.org/sensor/general/gravity"},
- {LINEAR_ACCEL_SENSOR, "http://tizen.org/sensor/general/linear_acceleration"},
- {GEOMAGNETIC_SENSOR, "http://tizen.org/sensor/general/magnetic"},
- {ROTATION_VECTOR_SENSOR, "http://tizen.org/sensor/general/rotation_vector"},
- {ORIENTATION_SENSOR, "http://tizen.org/sensor/general/orientation"},
- {GYROSCOPE_SENSOR, "http://tizen.org/sensor/general/gyroscope"},
- {LIGHT_SENSOR, "http://tizen.org/sensor/general/light"},
- {PROXIMITY_SENSOR, "http://tizen.org/sensor/general/proximity"},
- {PRESSURE_SENSOR, "http://tizen.org/sensor/general/pressure"},
- {ULTRAVIOLET_SENSOR, "http://tizen.org/sensor/general/ultraviolet"},
- {TEMPERATURE_SENSOR, "http://tizen.org/sensor/general/temperature"},
- {HUMIDITY_SENSOR, "http://tizen.org/sensor/general/humidity"},
- {HRM_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor"},
- {HRM_LED_GREEN_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor.led_green"},
- {HRM_LED_IR_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor.led_ir"},
- {HRM_LED_RED_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor.led_red"},
- {GYROSCOPE_UNCAL_SENSOR, "http://tizen.org/sensor/general/gyroscope.uncalibrated"},
- {GEOMAGNETIC_UNCAL_SENSOR, "http://tizen.org/sensor/general/geomagnetic.uncalibrated"},
- {GYROSCOPE_RV_SENSOR, "http://tizen.org/sensor/general/gyroscope_rotation_vector"},
- {GEOMAGNETIC_RV_SENSOR, "http://tizen.org/sensor/general/geomagnetic_rotation_vector"},
- {GYROSCOPE_ORIENTATION_SENSOR, "http://tizen.org/sensor/general/gyroscope_orientation"},
- {GEOMAGNETIC_ORIENTATION_SENSOR, "http://tizen.org/sensor/general/geomagnetic_orientation"},
-
- {SIGNIFICANT_MOTION_SENSOR, "http://tizen.org/sensor/general/significant_motion"},
-
- {HRM_BATCH_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor.batch"},
- {HRM_LED_GREEN_BATCH_SENSOR, "http://tizen.org/sensor/healthinfo/heart_rate_monitor.led_green.batch"},
-
- {HUMAN_PEDOMETER_SENSOR, "http://tizen.org/sensor/healthinfo/human_pedometer"},
- {HUMAN_SLEEP_MONITOR_SENSOR, "http://tizen.org/sensor/healthinfo/human_sleep_monitor"},
- {HUMAN_SLEEP_DETECTOR_SENSOR, "http://tizen.org/sensor/healthinfo/human_sleep_detector"},
- {HUMAN_STRESS_MONITOR_SENSOR, "http://tizen.org/sensor/healthinfo/human_stress_monitor"},
-
- {LIDAR_SENSOR, "http://tizen.org/sensor/general/lidar"},
-
- {EXERCISE_WALKING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.walking"},
- {EXERCISE_RUNNING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.running"},
- {EXERCISE_HIKING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.hiking"},
- {EXERCISE_CYCLING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.cycling"},
- {EXERCISE_ELLIPTICAL_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.elliptical"},
- {EXERCISE_INDOOR_CYCLING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.indoor_cycling"},
- {EXERCISE_ROWING_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.rowing"},
- {EXERCISE_STEPPER_SENSOR, "http://tizen.org/sensor/healthinfo/exercise.stepper"},
-
-
- {FUSION_SENSOR, "http://tizen.org/sensor/general/fusion"},
- {AUTO_ROTATION_SENSOR, "http://tizen.org/sensor/general/auto_rotation"},
- {AUTO_BRIGHTNESS_SENSOR, "http://tizen.org/sensor/general/auto_brightness"},
-
- {GESTURE_MOVEMENT_SENSOR, "http://tizen.org/sensor/general/gesture_movement"},
- {GESTURE_WRIST_UP_SENSOR, "http://tizen.org/sensor/general/gesture_wrist_up"},
- {GESTURE_WRIST_DOWN_SENSOR, "http://tizen.org/sensor/general/gesture_wrist_down"},
- {GESTURE_MOVEMENT_STATE_SENSOR, "http://tizen.org/sensor/general/gesture_movement_state"},
- {GESTURE_PICK_UP_SENSOR, "http://tizen.org/sensor/general/gesture_pick_up"},
- {GESTURE_FACE_DOWN_SENSOR, "http://tizen.org/sensor/general/gesture_face_down"},
-
- {ACTIVITY_TRACKER_SENSOR, "http://tizen.org/sensor/general/activity_tracker"},
- {ACTIVITY_LEVEL_MONITOR_SENSOR, "http://tizen.org/sensor/general/activity_level_monitor"},
- {GPS_BATCH_SENSOR, "http://tizen.org/sensor/location/gps_batch"},
- {PPG_BATCH_SENSOR, "http://tizen.org/sensor/healthinfo/ppg_batch"},
- {GPS_TIMESYNC_SENSOR, "http://tizen.org/sensor/location/gps_timesync"},
-
- {HRM_CTRL_SENSOR, "http://tizen.org/sensor/general/hrm_ctrl"},
- {REG_CTRL_SENSOR, "http://tizen.org/sensor/general/reg_ctrl"},
- {GPS_CTRL_SENSOR, "http://tizen.org/sensor/general/gps_ctrl"},
-
- {WEAR_STATUS_SENSOR, "http://tizen.org/sensor/general/wear_status"},
- {WEAR_ON_MONITOR_SENSOR, "http://tizen.org/sensor/general/wear_on_monitor"},
- {NO_MOVE_DETECTOR_SENSOR, "http://tizen.org/sensor/general/no_move_detector"},
- {RESTING_HR_SENSOR, "http://tizen.org/sensor/healthinfo/resting_hr"},
- {STEP_LEVEL_MONITOR_SENSOR, "http://tizen.org/sensor/healthinfo/step_level_monitor"},
- {EXERCISE_STANDALONE_SENSOR, "http://tizen.org/sensor/healthinfo/exercise_standalone"},
- {EXERCISE_HR_SENSOR, "http://tizen.org/sensor/healthinfo/exercise_hr"},
- {WORKOUT_SENSOR, "http://tizen.org/sensor/healthinfo/workout"},
- {CYCLE_MONITOR_SENSOR, "http://tizen.org/sensor/healthinfo/cycle_monitor"},
- {STAIR_TRACKER_SENSOR, "http://tizen.org/sensor/healthinfo/stair_tracker"},
- {PRESSURE_INDICATOR_SENSOR, "http://tizen.org/sensor/general/pressure_indicator"},
- {PRESSURE_ALERT_SENSOR, "http://tizen.org/sensor/general/pressure_alert"},
- {HR_CALORIE_SENSOR, "http://tizen.org/sensor/healthinfo/hr_calorie"},
- {SWIMMING_TRACKER_SENSOR, "http://tizen.org/sensor/healthinfo/swimming_tracker"},
- {SWIMMING_OUTDOOR_SENSOR, "http://tizen.org/sensor/healthinfo/swimming_outdoor"},
- {AUTO_SWIMMING_SENSOR, "http://tizen.org/sensor/healthinfo/auto_swimming"},
- {INACTIVITY_DETECTOR_SENSOR, "http://tizen.org/sensor/general/inactivity_detector"},
- {STRESS_TRACKER_SENSOR, "http://tizen.org/sensor/healthinfo/stress_tracker"},
- {FAKE_MOTION_SENSOR, "http://tizen.org/sensor/general/fake_motion"},
- {GEOFENCE_SENSOR, "http://tizen.org/sensor/general/geofence"},
- {INACTIVITY_DETECTOR_SENSOR, "http://tizen.org/sensor/healthinfo/inactivity_detector"},
- {HRM_BP_SENSOR, "http://tizen.org/sensor/healthinfo/hrm_bp"},
- {ECG_SENSOR, "http://tizen.org/sensor/healthinfo/ecg"},
- {FALL_DETECTION_SENSOR, "http://tizen.org/sensor/general/fall_detection"},
-
- {CONTEXT_SENSOR, "http://tizen.org/sensor/general/context"},
- {MOTION_SENSOR, "http://tizen.org/sensor/general/motion"},
- {PIR_SENSOR, "http://tizen.org/sensor/general/pir"},
- {PIR_LONG_SENSOR, "http://tizen.org/sensor/general/pir_long"},
- {DUST_SENSOR, "http://tizen.org/sensor/general/dust"},
- {THERMOMETER_SENSOR, "http://tizen.org/sensor/general/thermometer"},
- {PEDOMETER_SENSOR, "http://tizen.org/sensor/healthinfo/pedometer"},
- {FLAT_SENSOR, "http://tizen.org/sensor/general/flat"},
- {HRM_RAW_SENSOR, "http://tizen.org/sensor/healthinfo/hrm_raw"},
- {TILT_SENSOR, "http://tizen.org/sensor/general/tilt"},
- {RV_RAW_SENSOR, "http://tizen.org/sensor/general/rv_raw"},
- {GSR_SENSOR, "http://tizen.org/sensor/healthinfo/gsr"},
- {SIMSENSE_SENSOR, "http://tizen.org/sensor/healthinfo/simsense"},
- {PPG_SENSOR, "http://tizen.org/sensor/healthinfo/ppg"},
-};
-
-const char *sensor::utils::get_uri(sensor_type_t type)
-{
- auto it = types.find(type);
- if (it == types.end())
- return types[UNKNOWN_SENSOR];
- return it->second;
-}
-
-const char *sensor::utils::get_privilege(std::string uri)
-{
- std::size_t start = 0;
- std::size_t end = uri.length();
- std::size_t size = uri.size();
-
- for (int i = 0; i < URI_PRIV_INDEX; ++i) {
- retv_if(start >= uri.length(), "");
- start = uri.find(URI_DELIMITER, start + 1);
- retv_if(start == std::string::npos, "");
- }
-
- end = uri.find(URI_DELIMITER, start + 1);
- retv_if(end == std::string::npos, "");
-
- size = end - (start + 1);
-
- if (uri.substr(start + 1, size) == PRIVILEGE_HEALTHINFO_STR)
- return PRIVILEGE_HEALTHINFO_URI;
- else if (uri.substr(start + 1, size) == PRIVILEGE_LOCATION_STR)
- return PRIVILEGE_LOCATION_URI;
- else if (uri.substr(start + 1, size) == PRIVILEGE_PLATFORM_STR)
- return PRIVILEGE_PLATFORM_URI;
-
- return "";
-}
-
-static void init_types(std::map<std::string, sensor_type_t> &sensor_types)
-{
- ret_if(!sensor_types.empty());
-
- for (auto it = types.begin(); it != types.end(); ++it) {
- std::string uri(it->second);
- std::size_t found = uri.find_last_of("/");
- std::size_t len = uri.length() - (found + 1);
-
- sensor_types.emplace(uri.substr(found + 1, len), it->first);
- }
-}
-
-sensor_type_t sensor::utils::get_type(std::string uri)
-{
- static std::map<std::string, sensor_type_t> sensor_types;
- init_types(sensor_types);
-
- std::size_t start = 0;
- std::size_t end = uri.length();
- std::size_t size = uri.size();
-
- for (int i = 0; i < URI_SENSOR_TYPE_INDEX; ++i) {
- retv_if(start >= uri.length(), UNKNOWN_SENSOR);
- start = uri.find(URI_DELIMITER, start + 1);
- retv_if(start == std::string::npos, UNKNOWN_SENSOR);
- }
-
- end = uri.find(URI_DELIMITER, start + 1);
- retv_if(end == std::string::npos, UNKNOWN_SENSOR);
-
- size = end - (start + 1);
-
- auto it = sensor_types.find(uri.substr(start + 1, size));
- retv_if(it == sensor_types.end(), UNKNOWN_SENSOR);
-
- return it->second;
-}
-
-unsigned long long sensor::utils::get_timestamp(void)
-{
- struct timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
-}
-
-unsigned long long sensor::utils::get_timestamp(timeval *t)
-{
- if (!t)
- return 0;
-
- return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
-}
-
-bool sensor::utils::get_proc_name(pid_t pid, char *process_name)
-{
- char buf[NAME_MAX];
-
- if (snprintf(buf, sizeof(buf), "%d process", pid) < 1) {
- return false;
- }
-
- strncpy(process_name, buf, NAME_MAX-1);
- process_name[NAME_MAX-1] = '\0';
-
- return true;
-}
-
-const char* sensor::utils::get_client_name(void)
-{
- const int pid_string_size = 10;
- static pid_t pid = -1;
- static char client_name[NAME_MAX + pid_string_size];
-
- char proc_name[NAME_MAX];
-
- if (pid == -1)
- {
- pid = getpid();
- get_proc_name(pid, proc_name);
- snprintf(client_name, sizeof(client_name), "%s(%d)", proc_name, pid);
- }
-
- return client_name;
-}
-
-std::vector<std::string> sensor::utils::tokenize(const std::string &in, const char *delim)
-{
- std::vector<std::string> tokens;
- char *input = g_strdup(in.c_str());
-
- char *save = NULL;
- char *token = strtok_r(input, delim, &save);
-
- while (token != NULL) {
- tokens.push_back(token);
- token = strtok_r(NULL, delim, &save);
- }
-
- g_free(input);
- return tokens;
-}
+++ /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_UTILS_H__
-#define __SENSOR_UTILS_H__
-
-#include <sensor-types.h>
-
-#include <time.h>
-#include <string>
-#include <vector>
-
-namespace sensor {
-
-namespace utils {
- const char *get_uri(sensor_type_t type);
- const char *get_privilege(std::string uri);
- sensor_type_t get_type(std::string uri);
-
- unsigned long long get_timestamp(void);
- unsigned long long get_timestamp(timeval *t);
-
- const char* get_client_name(void);
- bool get_proc_name(pid_t pid, char *process_name);
-
- std::vector<std::string> tokenize(const std::string &in, const char *delim);
-}
-
-}
-
-#endif /* __SENSOR_UTILS_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 "socket.h"
-
-#include <fcntl.h>
-#include <sys/un.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <systemd/sd-daemon.h>
-
-#include "sensor-log-private.h"
-
-#define SOCK_TIMEOUT 10
-#define SLEEP_10_MS usleep(10000)
-
-using namespace ipc;
-
-static bool set_close_on_exec(int fd)
-{
- if (::fcntl(fd, F_SETFL, FD_CLOEXEC) == -1)
- return false;
-
- return true;
-}
-
-static int create_systemd_socket(const std::string &path, int type)
-{
- int n;
- int listening;
-
- listening = (type == SOCK_STREAM) ? 1 : -1;
-
- n = sd_listen_fds(0);
- retvm_if(n < 0, -EPERM, "Failed to listen fds from systemd");
-
- for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; ++fd) {
- if (sd_is_socket_unix(fd, type, listening, path.c_str(), 0) > 0) {
- set_close_on_exec(fd);
- return fd;
- }
- }
-
- return -EPERM;
-}
-
-static int create_unix_socket(int type)
-{
- int sock_fd = ::socket(AF_UNIX, type, 0);
-
- if (sock_fd < 0) {
- _ERRNO(errno, _E, "Failed to create socket");
- return -EPERM;
- }
-
- set_close_on_exec(sock_fd);
-
- int optval = 1;
- if (::setsockopt(sock_fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0) {
- _ERRNO(errno, _E, "Failed to create socket[%d]", sock_fd);
- ::close(sock_fd);
- return -EPERM;
- }
-
- return sock_fd;
-}
-
-static bool select_fds(int fd, fd_set *read_fds, fd_set *write_fds, const int timeout)
-{
- struct timeval tv;
- int err;
-
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- while (true) {
- err = ::select(fd + 1, read_fds, write_fds, NULL, &tv);
- if (err <= 0)
- return false;
-
- if (read_fds && FD_ISSET(fd, read_fds))
- break;
- if (write_fds && FD_ISSET(fd, write_fds))
- break;
- }
-
- return true;
-}
-
-socket::socket()
-: m_sock_fd(-1)
-, m_mode(MSG_DONTWAIT | MSG_NOSIGNAL)
-, m_listening(false)
-{
-}
-
-socket::socket(int sock_fd)
-: m_sock_fd(sock_fd)
-, m_mode(MSG_DONTWAIT | MSG_NOSIGNAL)
-, m_listening(false)
-{
-}
-
-socket::socket(const socket &sock)
-: m_sock_fd(-1)
-, m_mode(MSG_DONTWAIT | MSG_NOSIGNAL)
-, m_listening(false)
-{
- if (this == &sock)
- return;
-
- m_sock_fd = sock.m_sock_fd;
- m_mode = sock.m_mode;
- m_listening.store(sock.m_listening);
-}
-
-socket::~socket()
-{
- close();
-}
-
-bool socket::connect(void)
-{
- sockaddr_un addr;
- fd_set write_fds;
- FD_ZERO(&write_fds);
- FD_SET(m_sock_fd, &write_fds);
-
- retvm_if(m_path.size() >= sizeof(sockaddr_un::sun_path), false,
- "Failed to create socket[%s]", m_path.c_str());
-
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, m_path.c_str(), sizeof(sockaddr_un::sun_path));
- addr.sun_path[m_path.size()] = '\0';
-
- if (::connect(m_sock_fd,
- reinterpret_cast<struct sockaddr *>(&addr),
- sizeof(struct sockaddr_un)) < 0) {
- _ERRNO(errno, _E, "Failed to connect() for socket[%d]", m_sock_fd);
- close();
- return false;
- }
-
- if (!select_fds(m_sock_fd, NULL, &write_fds, SOCK_TIMEOUT)) {
- _E("Failed to select for socket[%d]", m_sock_fd);
- close();
- return false;
- }
-
- if (!has_connected()) {
- close();
- return false;
- }
-
- return true;
-}
-
-bool socket::bind(void)
-{
- sockaddr_un addr;
- int file_mode;
-
- retvm_if(m_path.size() >= sizeof(sockaddr_un::sun_path), false,
- "Failed to create socket[%s]", m_path.c_str());
- retv_if(m_listening.load(), true);
-
- if (!access(m_path.c_str(), F_OK))
- unlink(m_path.c_str());
-
- addr.sun_family = AF_UNIX;
- ::strncpy(addr.sun_path, m_path.c_str(), sizeof(sockaddr_un::sun_path));
- addr.sun_path[m_path.size()] = '\0';
-
- if (::bind(m_sock_fd,
- reinterpret_cast<struct sockaddr *>(&addr),
- sizeof(struct sockaddr_un)) < 0) {
- _ERRNO(errno, _E, "Failed to bind for socket[%d]", m_sock_fd);
- close();
- return false;
- }
-
- /* TODO: Is this really necessary? */
- file_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
- if (chmod(m_path.c_str(), file_mode) < 0) {
- _ERRNO(errno, _E, "Failed to create socket[%d]", m_sock_fd);
- close();
- return false;
- }
-
- return true;
-}
-
-bool socket::listen(const int max_connections)
-{
- retv_if(m_listening.load(), true);
-
- if (::listen(m_sock_fd, max_connections) < 0) {
- _ERRNO(errno, _E, "Failed to listen() for socket[%d]", m_sock_fd);
- close();
- return false;
- }
-
- m_listening.store(true);
-
- _D("Listened[%d]", m_sock_fd);
-
- return true;
-}
-
-bool socket::accept(socket &client_sock)
-{
- int fd;
- fd_set read_fds;
- FD_ZERO(&read_fds);
- FD_SET(m_sock_fd, &read_fds);
-
- fd = ::accept(m_sock_fd, NULL, NULL);
-
- if (fd < 0) {
- _ERRNO(errno, _E, "Failed to accept[%d]", m_sock_fd);
- return false;
- }
-
- set_close_on_exec(fd);
- client_sock.set_fd(fd);
- /* TODO : socket type should be adjusted here */
-
- return true;
-}
-
-bool socket::close(void)
-{
- retv_if(m_sock_fd < 0, false);
-
- if (::close(m_sock_fd) < 0)
- return false;
-
- _D("Closed[%d]", m_sock_fd);
-
- m_sock_fd = -1;
- m_listening.store(false);
-
- return true;
-}
-
-int socket::get_fd(void) const
-{
- return m_sock_fd;
-}
-
-void socket::set_fd(int sock_fd)
-{
- m_sock_fd = sock_fd;
-}
-
-int socket::get_mode(void) const
-{
- return m_mode;
-}
-
-bool socket::set_mode(int mode)
-{
- /* TODO : implement send/recv message mode */
- return true;
-}
-
-bool socket::create(const std::string &path)
-{
- m_sock_fd = ::create_systemd_socket(path, SOCK_STREAM);
- if (m_sock_fd < 0)
- m_sock_fd = create_unix_socket(SOCK_STREAM);
- else
- m_listening.store(true);
-
- retvm_if((m_sock_fd < 0), false, "Failed to create socket");
-
- /* non-blocking mode */
- retvm_if(!set_blocking_mode(false), false, "Failed to set non-blocking mode");
- /* recv timeout */
- retvm_if(!set_recv_timeout(1), false, "Failed to set timeout");
- /* TODO */
- /*retvm_if(!set_reuse_addr(), false, "Failed to reuse address"); */
-
- m_path = path;
-
- return true;
-}
-
-ssize_t socket::send(const void *buffer, size_t size, bool select) const
-{
- if (select) {
- fd_set write_fds;
- FD_ZERO(&write_fds);
- FD_SET(m_sock_fd, &write_fds);
-
- if (!select_fds(m_sock_fd, NULL, &write_fds, SOCK_TIMEOUT)) {
- _E("Failed to send message(timeout)");
- return 0;
- }
- }
-
- return on_send(buffer, size);
-}
-
-ssize_t socket::recv(void* buffer, size_t size, bool select) const
-{
- if (select) {
- fd_set read_fds;
- FD_ZERO(&read_fds);
- FD_SET(m_sock_fd, &read_fds);
-
- if (!select_fds(m_sock_fd, &read_fds, NULL, SOCK_TIMEOUT)) {
- _E("Failed to receive message(timeout)");
- return 0;
- }
- }
-
- return on_recv(buffer, size);
-}
-
-int socket::get_sock_type(void)
-{
- socklen_t opt_len;
- int sock_type;
- opt_len = sizeof(sock_type);
-
- retvm_if(m_sock_fd < 0, false, "Invalid socket[%d]", m_sock_fd);
-
- if (getsockopt(m_sock_fd, SOL_SOCKET, SO_TYPE, &sock_type, &opt_len) < 0) {
- _ERRNO(errno, _E, "Failed to getsockopt from socket[%d]", m_sock_fd);
- return false;
- }
-
- return sock_type;
-}
-
-bool socket::set_recv_timeout(int sec)
-{
- struct timeval timeout = {sec, 0};
-
- retvm_if(m_sock_fd < 0, false, "Invalid socket[%d]", m_sock_fd);
-
- if (setsockopt(m_sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
- _ERRNO(errno, _E, "Failed to setsockopt[%d]", m_sock_fd);
- return false;
- }
-
- return true;
-}
-
-bool socket::set_sock_type(int type)
-{
- socklen_t opt_len;
- opt_len = sizeof(type);
-
- retvm_if(m_sock_fd < 0, false, "Invalid socket[%d]", m_sock_fd);
-
- if (setsockopt(m_sock_fd, SOL_SOCKET, SO_TYPE, &type, opt_len) < 0) {
- _ERRNO(errno, _E, "Failed to setsockopt[%d]", m_sock_fd);
- return false;
- }
-
- return true;
-}
-
-bool socket::set_blocking_mode(bool blocking)
-{
- int flags;
-
- flags = fcntl(m_sock_fd, F_GETFL);
- retvm_if(flags == -1, false, "Failed to fcntl(F_GETFL)[%d]", m_sock_fd);
-
- flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
-
- flags = fcntl(m_sock_fd, F_SETFL, flags);
- retvm_if(flags == -1, false, "Failed to fcntl(F_SETFL)[%d]", m_sock_fd);
-
- return true;
-}
-
-bool socket::has_connected(void)
-{
- int so_error;
- socklen_t len = sizeof(so_error);
-
- if (getsockopt(m_sock_fd, SOL_SOCKET, SO_ERROR, &so_error, &len) == -1) {
- _E("Failed to call getsockopt[%d]", m_sock_fd);
- return false;
- }
-
- if (so_error) {
- _E("Failed to connect[%d] : %d", m_sock_fd, so_error);
- return false;
- }
-
- return true;
-}
-
-bool socket::set_buffer_size(int type, int size)
-{
- retv_if(m_sock_fd < 0, false);
-
- int ret = 0;
-
- ret = setsockopt(m_sock_fd, SOL_SOCKET, type, &size, sizeof(size));
- retvm_if(ret < 0, false, "Failed to call setsocketopt()");
-
- return true;
-}
-
-int socket::get_buffer_size(int type)
-{
- retv_if(m_sock_fd < 0, false);
-
- int ret = 0;
- int buf_size = 0;
- socklen_t len;
-
- ret = getsockopt(m_sock_fd, SOL_SOCKET, type, &buf_size, &len);
- retvm_if(ret < 0, -EPERM, "Failed to call getsocketopt()");
-
- return buf_size;
-}
-
-int socket::get_current_buffer_size(void)
-{
- retv_if(m_sock_fd < 0, false);
-
- int queue_size = 0;
- ioctl(m_sock_fd, TIOCOUTQ, &queue_size);
-
- return queue_size;
-}
-
-ssize_t socket::on_send(const void *buffer, size_t size) const
-{
- ssize_t len = 0;
- size_t total_size = 0;
-
- do {
- len = ::send(get_fd(),
- reinterpret_cast<const char *>(buffer) + total_size,
- size - total_size, get_mode());
-
- if (len < 0) {
- if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
- SLEEP_10_MS;
- continue;
- }
-
- _ERRNO(errno, _E, "Failed to send(%d, %p, %u, %u) = %d",
- get_fd(), buffer, total_size, size - total_size, len);
- return -errno;
- }
-
- total_size += len;
- } while (total_size < size);
-
- return total_size;
-}
-
-ssize_t socket::on_recv(void *buffer, size_t size) const
-{
- ssize_t len = 0;
- size_t total_size = 0;
-
- do {
- len = ::recv(get_fd(),
- reinterpret_cast<char *>(buffer) + total_size,
- size - total_size,
- socket::get_mode());
-
- if (len == 0) {
- _E("Failed to recv(%d, %p + %x, %u) = %d, because the peer performed shutdown",
- get_fd(), buffer, total_size, size - total_size, len);
- return -1;
- }
-
- if (len < 0) {
- if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
- SLEEP_10_MS;
- continue;
- }
-
- _ERRNO(errno, _E, "Failed to recv(%d, %p, %u, %u) = %d",
- get_fd(), buffer, total_size, size - total_size, len);
- return -errno;
- }
-
- total_size += len;
- } while (total_size < size);
-
- return total_size;
-}
+++ /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 __SOCKET_H__
-#define __SOCKET_H__
-
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <string>
-#include <atomic>
-
-namespace ipc {
-
-class socket {
-public:
- socket();
- socket(int sock_fd);
- socket(const socket &sock);
- virtual ~socket();
-
- virtual bool create(const std::string &path);
-
- bool connect(void);
- bool bind(void);
- bool listen(const int max_connections);
- bool accept(socket &client_sock);
-
- bool close(void);
-
- int get_fd(void) const;
- void set_fd(int sock_fd);
-
- int get_mode(void) const;
- bool set_mode(int mode);
-
- bool set_blocking_mode(bool blocking);
- bool set_recv_timeout(int timeout);
-
- /* type : SO_SNDBUF, SO_RCVBUF */
- bool set_buffer_size(int type, int size);
- int get_buffer_size(int type);
- int get_current_buffer_size(void);
-
- ssize_t send(const void *buffer, size_t size, bool select = false) const;
- ssize_t recv(void* buffer, size_t size, bool select = false) const;
-
-private:
- ssize_t on_send(const void *buffer, size_t size) const;
- ssize_t on_recv(void* buffer, size_t size) const;
-
- int get_sock_type(void);
- bool set_sock_type(int type);
- bool has_connected(void);
-
- int m_sock_fd;
- int m_mode;
- std::atomic<bool> m_listening;
- std::string m_path;
-};
-
-}
-
-#endif /* __SOCKET_H__ */