From feb74e9870e092bc3086ef9162af02fb62b75c2c Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 14:24:56 +0900 Subject: [PATCH 01/16] sensord: add testcase for sensor-provider APIs 1. add "http://tizen.org/sensor/mysensor/mysensor" 2. publish events to sensor listeners 3. start/stop/set_interval callbacks 4. sensor_added_cb/sensor_removed_cb callbacks Change-Id: I98c49388ecfcd677c2c517cfb1e1d4cf3f405f26 Signed-off-by: kibak.yoon --- src/sensorctl/testcase/unit_client.cpp | 180 +++++++++++++++++++++++++++++++-- 1 file changed, 170 insertions(+), 10 deletions(-) diff --git a/src/sensorctl/testcase/unit_client.cpp b/src/sensorctl/testcase/unit_client.cpp index 1034573..661e4a0 100644 --- a/src/sensorctl/testcase/unit_client.cpp +++ b/src/sensorctl/testcase/unit_client.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -132,23 +133,182 @@ TESTCASE(sensor_api_all, all_p_1) return true; } -TESTCASE(sensor_api_provider, provider_base_p_1) +static bool m_start = false; + +static void start_cb(sensord_provider_h provider, void *user_data) +{ + m_start = true; + _N("START\n"); +} + +static void stop_cb(sensord_provider_h provider, void *user_data) +{ + m_start = false; + _N("STOP\n"); +} + +static void interval_cb(sensord_provider_h provider, unsigned int interval_ms, void *user_data) +{ + _N("Interval : %d\n", interval_ms); +} + +static gboolean publish(gpointer gdata) +{ + if (!m_start) return TRUE; + + sensord_provider_h *provider = reinterpret_cast(gdata); + + sensor_data_t data; + data.accuracy = 3; + data.timestamp = sensor::utils::get_timestamp(); + data.value_count = 3; + data.values[0] = 1; + data.values[1] = 2; + data.values[2] = 3; + + _N("[%llu] %f %f %f\n", data.timestamp, data.values[0], data.values[1], data.values[2]); + sensord_provider_publish(provider, data); + return TRUE; +} + +/* TODO: change it from manual test to auto-test */ +TESTCASE(sensor_api_mysensor_provider, provider_p_1) { + const char *uri = "http://example.org/mysensor/mysensor"; + const char *name = "mysensor"; + const char *vendor = "tizen"; + + int err = 0; + sensor_t sensor; + sensord_provider_h provider; + + err = sensord_create_provider(uri, &provider); + ASSERT_EQ(err, 0); + + err = sensord_provider_set_name(provider, name); + ASSERT_EQ(err, 0); + err = sensord_provider_set_vendor(provider, vendor); + ASSERT_EQ(err, 0); + err = sensord_provider_set_range(provider, 0.0f, 1.0f); + ASSERT_EQ(err, 0); + err = sensord_provider_set_resolution(provider, 0.01f); + ASSERT_EQ(err, 0); + + err = sensord_add_provider(provider); + ASSERT_EQ(err, 0); + + err = sensord_provider_set_start_cb(provider, start_cb, NULL); + ASSERT_EQ(err, 0); + err = sensord_provider_set_stop_cb(provider, stop_cb, NULL); + ASSERT_EQ(err, 0); + err = sensord_provider_set_set_interval_cb(provider, interval_cb, NULL); + ASSERT_EQ(err, 0); + + err = sensord_get_default_sensor_by_uri(uri, &sensor); + ASSERT_EQ(err, 0); + + g_timeout_add_seconds(1, publish, provider); + mainloop::run(); + + err = sensord_remove_provider(provider); + ASSERT_EQ(err, 0); + err = sensord_destroy_provider(provider); + ASSERT_EQ(err, 0); + + return true; +} + +TESTCASE(sensor_api_mysensor_listener, listener_p_1) +{ + const char *uri = "http://example.org/mysensor/mysensor"; + int err; bool ret; int handle; + sensor_t sensor; - handle = sensord_external_connect("TEST", NULL, NULL); - ASSERT_GE(handle, 0); + called = false; - for (int k = 0; k < 1000; ++k) { - float data[3] = { 1, 2, 3 }; - ret = sensord_external_post(handle, 1, data, 3); - ASSERT_TRUE(ret); - sleep(1); - } + err = sensord_get_default_sensor_by_uri(uri, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + ASSERT_EQ(err, 0); + + ret = sensord_register_event(handle, 1, 100, 100, basic_cb, NULL); + ASSERT_TRUE(ret); + + ret = sensord_start(handle, 0); + ASSERT_TRUE(ret); - ret = sensord_external_disconnect(handle); + ret = sensord_change_event_interval(handle, 0, 100); ASSERT_TRUE(ret); + mainloop::run(); + + ret = sensord_stop(handle); + ASSERT_TRUE(ret); + + ret = sensord_unregister_event(handle, 1); + ASSERT_TRUE(ret); + + ret = sensord_disconnect(handle); + ASSERT_TRUE(ret); + + return true; +} + +static void add_mysensor(void) +{ + const char *uri = "http://example.org/mysensor/mysensor"; + const char *name = "mysensor"; + const char *vendor = "tizen"; + + sensord_provider_h provider; + + sensord_create_provider(uri, &provider); + sensord_provider_set_name(provider, name); + sensord_provider_set_vendor(provider, vendor); + sensord_provider_set_range(provider, 0.0f, 1.0f); + sensord_provider_set_resolution(provider, 0.01f); + + sensord_add_provider(provider); + + sensord_remove_provider(provider); + sensord_destroy_provider(provider); +} + +static bool added = false; + +static void added_cb(const char *uri, void *user_data) +{ + _I("ADDED[%s]\n", uri); + added = true; +} + +static void removed_cb(const char *uri, void *user_data) +{ + _I("REMOVED[%s]\n", uri); + if (added) + mainloop::stop(); +} + +TESTCASE(sensor_api_mysensor_cb, mysensor_cb_p_1) +{ + int ret; + + ret = sensord_add_sensor_added_cb(added_cb, NULL); + ASSERT_EQ(ret, 0); + ret = sensord_add_sensor_removed_cb(removed_cb, NULL); + ASSERT_EQ(ret, 0); + + add_mysensor(); + + mainloop::run(); + + ret = sensord_remove_sensor_added_cb(added_cb); + ASSERT_EQ(ret, 0); + ret = sensord_remove_sensor_removed_cb(removed_cb); + ASSERT_EQ(ret, 0); + return true; } -- 2.7.4 From 58b1ae9412d02ba8ffd9e48c1f45992289b6cd0d Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:33:27 +0900 Subject: [PATCH 02/16] sensord: remove set/get API for the URI of sensor type - It doesn't necessary because it is already contained in the URI. Change-Id: I3a360004441558a5498face4211c5512db68a1e9 Signed-off-by: kibak.yoon --- src/server/application_sensor_handler.cpp | 3 +-- src/shared/sensor_info.cpp | 26 ++++---------------------- src/shared/sensor_info.h | 6 +----- 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/server/application_sensor_handler.cpp b/src/server/application_sensor_handler.cpp index 48e50be..1070156 100644 --- a/src/server/application_sensor_handler.cpp +++ b/src/server/application_sensor_handler.cpp @@ -35,8 +35,7 @@ application_sensor_handler::~application_sensor_handler() int application_sensor_handler::post(sensor_data_t *data, int len) { - std::string uri = m_info.get_type_uri(); - + std::string uri = m_info.get_uri(); return notify(uri.c_str(), data, len); } diff --git a/src/shared/sensor_info.cpp b/src/shared/sensor_info.cpp index 7ff31f4..c2d95c3 100644 --- a/src/shared/sensor_info.cpp +++ b/src/shared/sensor_info.cpp @@ -30,7 +30,6 @@ using namespace sensor; sensor_info::sensor_info() : m_type(UNKNOWN_SENSOR) -, m_type_uri(SENSOR_UNKNOWN_TYPE) , m_uri(SENSOR_UNKNOWN_NAME) , m_model(SENSOR_UNKNOWN_NAME) , m_vendor(SENSOR_UNKNOWN_NAME) @@ -46,7 +45,6 @@ sensor_info::sensor_info() sensor_info::sensor_info(const sensor_info &info) : m_type(info.m_type) -, m_type_uri(info.m_type_uri) , m_uri(info.m_uri) , m_model(info.m_model) , m_vendor(info.m_vendor) @@ -63,13 +61,12 @@ sensor_info::sensor_info(const sensor_info &info) sensor_info::sensor_info(const sensor_info_t &info) { /* TODO: HAL should change name from single name to URI */ - const char *type_uri = sensor::utils::get_uri((sensor_type_t)info.type); - std::string name(type_uri); - name.append("/").append(info.name); + 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_type_uri(type_uri); - set_uri(name.c_str()); + set_uri(uri.c_str()); set_model(info.model_name); set_vendor(info.vendor); set_min_range(info.min_range); @@ -88,7 +85,6 @@ sensor_info::sensor_info(const sensor_info2_t &info) std::size_t found = uri.find_last_of("/\\"); set_type(info.type); - set_type_uri(uri.substr(0, found).c_str()); set_uri(uri.c_str()); set_model(uri.substr(found + 1, uri.length()).c_str()); set_vendor(info.vendor); @@ -106,11 +102,6 @@ sensor_type_t sensor_info::get_type(void) return m_type; } -std::string &sensor_info::get_type_uri(void) -{ - return m_type_uri; -} - std::string &sensor_info::get_uri(void) { return m_uri; @@ -166,11 +157,6 @@ void sensor_info::set_type(sensor_type_t type) m_type = type; } -void sensor_info::set_type_uri(const char *type_uri) -{ - m_type_uri = type_uri; -} - void sensor_info::set_uri(const char *name) { m_uri = name; @@ -224,7 +210,6 @@ void sensor_info::set_privilege(const char *privilege) void sensor_info::serialize(raw_data_t &data) { put(data, m_type); - put(data, m_type_uri); put(data, m_uri); put(data, m_model); put(data, m_vendor); @@ -246,7 +231,6 @@ void sensor_info::deserialize(const char *data, int data_len) it = get(it, type); m_type = (sensor_type_t)type; - it = get(it, m_type_uri); it = get(it, m_uri); it = get(it, m_model); it = get(it, m_vendor); @@ -261,7 +245,6 @@ void sensor_info::deserialize(const char *data, int data_len) void sensor_info::show(void) { - _I("Type = %s", m_type_uri.c_str()); _I("URI = %s", m_uri.c_str()); _I("Model = %s", m_model.c_str()); _I("Vendor = %s", m_vendor.c_str()); @@ -277,7 +260,6 @@ void sensor_info::show(void) void sensor_info::clear(void) { m_type = UNKNOWN_SENSOR; - m_type_uri.clear(); m_uri.clear(); m_model.clear(); m_vendor.clear(); diff --git a/src/shared/sensor_info.h b/src/shared/sensor_info.h index 2611948..91b6ba8 100644 --- a/src/shared/sensor_info.h +++ b/src/shared/sensor_info.h @@ -39,9 +39,8 @@ public: sensor_info(const sensor_info_t &info); sensor_info(const sensor_info2_t &info); - /* TODO: it would be better to get_type() returns type(URI) */ + /* TODO: it would be better to return type(URI) */ sensor_type_t get_type(void); - std::string &get_type_uri(void); std::string &get_uri(void); std::string &get_model(void); std::string &get_vendor(void); @@ -53,9 +52,7 @@ public: bool is_wakeup_supported(void); std::string &get_privilege(void); - /* TODO: it would be better to get_type() returns type(URI) */ void set_type(sensor_type_t type); - void set_type_uri(const char *type_uri); void set_uri(const char *name); void set_model(const char *name); void set_vendor(const char *vendor); @@ -75,7 +72,6 @@ public: private: sensor_type_t m_type; - std::string m_type_uri; std::string m_uri; std::string m_model; std::string m_vendor; -- 2.7.4 From 5fb868cf636b312b4fcdf825f646bad870e33341 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:39:17 +0900 Subject: [PATCH 03/16] sensord: add command types/macros for sensor provider Change-Id: I6f4b5adcd116eadd0a94dd0fef69027030ac6ca6 Signed-off-by: kibak.yoon --- include/sensor_types.h | 3 +++ src/server/server_channel_handler.cpp | 2 +- src/shared/command_types.h | 30 +++++++++++++++++------------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/sensor_types.h b/include/sensor_types.h index 9c9426c..5342f23 100644 --- a/include/sensor_types.h +++ b/include/sensor_types.h @@ -60,6 +60,9 @@ extern "C" #define SENSOR_UNKNOWN_TYPE "http://tizen.org/sensor/unknown" #define SENSOR_UNKNOWN_NAME "Unknown" +#define PRIV_APPLICATION_SENSOR "http://tizen.org/privilege/applicationsensor" +#define PRIV_APPLICATION_SENSOR_WRITE "http://tizen.org/privilege/applicationsensor.write" + typedef int64_t sensor_id_t; typedef void *sensor_t; diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index 6fad489..d5ad86f 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -82,7 +82,7 @@ void server_channel_handler::read(channel *ch, message &msg) err = provider_connect(ch, msg); break; case CMD_PROVIDER_DISCONNECT: err = provider_disconnect(ch, msg); break; - case CMD_PROVIDER_POST: + case CMD_PROVIDER_PUBLISH: err = provider_post(ch, msg); break; case CMD_HAS_PRIVILEGE: err = has_privileges(ch, msg); break; diff --git a/src/shared/command_types.h b/src/shared/command_types.h index a75ef6e..0ada031 100644 --- a/src/shared/command_types.h +++ b/src/shared/command_types.h @@ -32,7 +32,11 @@ enum cmd_type_e { CMD_NONE = 0, /* Manager */ + CMD_MANAGER_CONNECT, + CMD_MANAGER_DISCONNECT, CMD_MANAGER_SENSOR_LIST, + CMD_MANAGER_SENSOR_ADDED, + CMD_MANAGER_SENSOR_REMOVED, /* Listener */ CMD_LISTENER_EVENT, @@ -45,11 +49,13 @@ enum cmd_type_e { CMD_LISTENER_ATTR_STR, CMD_LISTENER_GET_DATA, - /* Provider(Dyanmic/External sensor) */ - CMD_PROVIDER_CMD_EVENT, + /* Provider */ CMD_PROVIDER_CONNECT, CMD_PROVIDER_DISCONNECT, - CMD_PROVIDER_POST, + CMD_PROVIDER_START, + CMD_PROVIDER_STOP, + CMD_PROVIDER_ATTR_INT, + CMD_PROVIDER_PUBLISH, CMD_HAS_PRIVILEGE, @@ -98,22 +104,20 @@ typedef struct { } cmd_listener_get_data_t; typedef struct { - int provider_id; - int sensor_id; - char sensor[NAME_MAX]; - char s_info[0]; + char info[0]; } cmd_provider_connect_t; typedef struct { - int provider_id; - char sensor[NAME_MAX]; } cmd_provider_disconnect_t; typedef struct { - int provider_id; - char sensor[NAME_MAX]; - sensorhub_data_t base_data; -} cmd_provider_post_t; + sensor_data_t data; +} cmd_provider_publish_t; + +typedef struct { + int attribute; + int value; +} cmd_provider_attr_int_t; typedef struct { char sensor[NAME_MAX]; -- 2.7.4 From 9c8b05e50e8110d164560bfd74e0c26d7a576c17 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:45:21 +0900 Subject: [PATCH 04/16] sensord: add sensor_provider for app-level sensor Change-Id: Ic0e02eafabe455e711a52ae1db1e86dae707f975 Signed-off-by: kibak.yoon --- src/client/sensor_provider.cpp | 203 +++++++++++++++++++++++++++++++++ src/client/sensor_provider.h | 75 ++++++++++++ src/client/sensor_provider_handler.cpp | 87 ++++++++++++++ src/client/sensor_provider_handler.h | 60 ++++++++++ 4 files changed, 425 insertions(+) create mode 100644 src/client/sensor_provider.cpp create mode 100644 src/client/sensor_provider.h create mode 100644 src/client/sensor_provider_handler.cpp create mode 100644 src/client/sensor_provider_handler.h diff --git a/src/client/sensor_provider.cpp b/src/client/sensor_provider.cpp new file mode 100644 index 0000000..b07fec5 --- /dev/null +++ b/src/client/sensor_provider.cpp @@ -0,0 +1,203 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include "sensor_provider_handler.h" + +using namespace sensor; + +sensor_provider::sensor_provider(const char *uri) +: m_client(NULL) +, m_channel(NULL) +, m_handler(NULL) +, m_connected(false) +{ + init(uri); +} + +sensor_provider::~sensor_provider() +{ + deinit(); +} + +bool sensor_provider::init(const char *uri) +{ + m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); + retvm_if(!m_client, false, "Failed to allocate memory"); + + m_handler = new(std::nothrow) sensor_provider_handler(this); + if (!m_handler) { + delete m_client; + return false; + } + + m_sensor.set_uri(uri); + m_sensor.set_min_range(0); + m_sensor.set_max_range(1); + m_sensor.set_resolution(1); + m_sensor.set_privilege(PRIV_APPLICATION_SENSOR); + + return true; +} + +void sensor_provider::deinit(void) +{ + disconnect(); + + delete m_handler; + m_handler = NULL; + + delete m_client; + m_client = NULL; +} + +const char *sensor_provider::get_uri(void) +{ + return m_sensor.get_uri().c_str(); +} + +sensor_info *sensor_provider::get_sensor_info(void) +{ + return &m_sensor; +} + +int sensor_provider::serialize(sensor_info *info, char **bytes) +{ + int size; + raw_data_t *raw = new(std::nothrow) raw_data_t; + retvm_if(!raw, -ENOMEM, "Failed to allocated memory"); + + info->serialize(*raw); + + *bytes = new(std::nothrow) char[raw->size()]; + retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory"); + + std::copy(raw->begin(), raw->end(), *bytes); + + size = raw->size(); + delete raw; + + return size; +} + +int sensor_provider::send_sensor_info(sensor_info *info) +{ + char *bytes; + int size; + + size = serialize(info, &bytes); + + ipc::message msg((const char *)bytes, size); + msg.set_type(CMD_PROVIDER_CONNECT); + + m_channel->send_sync(&msg); + + return OP_SUCCESS; +} + +bool sensor_provider::connect(void) +{ + m_channel = m_client->connect(m_handler, &m_loop); + retvm_if(!m_channel, false, "Failed to connect to server"); + + /* serialize and send sensor info */ + send_sensor_info(get_sensor_info()); + + /* check error */ + ipc::message reply; + m_channel->read_sync(reply); + retv_if(reply.header()->err < 0, reply.header()->err); + + m_connected.store(true); + + _I("Provider URI[%s]", get_uri()); + + return true; +} + +bool sensor_provider::disconnect(void) +{ + retv_if(!is_connected(), false); + m_connected.store(false); + + ipc::message msg(OP_SUCCESS); + ipc::message reply; + + msg.set_type(CMD_PROVIDER_DISCONNECT); + + m_channel->send_sync(&msg); + m_channel->read_sync(reply); + + m_channel->disconnect(); + + delete m_channel; + m_channel = NULL; + + _I("Disconnected[%s]", get_uri()); + + return true; +} + +void sensor_provider::restore(void) +{ + ret_if(!is_connected()); + retm_if(!connect(), "Failed to restore provider"); + + _D("Restored provider[%s]", get_uri()); +} + +int sensor_provider::publish(sensor_data_t *data, int len) +{ + ipc::message msg; + msg.set_type(CMD_PROVIDER_PUBLISH); + msg.enclose((const char *)data, len); + + m_channel->send_sync(&msg); + + return OP_SUCCESS; +} + +bool sensor_provider::is_connected(void) +{ + return m_connected.load(); +} + +void sensor_provider::set_start_cb(sensord_provider_start_cb cb, void *user_data) +{ + m_handler->set_start_cb(cb, user_data); +} + +void sensor_provider::set_stop_cb(sensord_provider_stop_cb cb, void *user_data) +{ + m_handler->set_stop_cb(cb, user_data); +} + +void sensor_provider::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data) +{ + m_handler->set_interval_cb(cb, user_data); +} + diff --git a/src/client/sensor_provider.h b/src/client/sensor_provider.h new file mode 100644 index 0000000..7fc4429 --- /dev/null +++ b/src/client/sensor_provider.h @@ -0,0 +1,75 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sensor_provider_handler.h" + +namespace sensor { + +class sensor_provider { +public: + sensor_provider(const char *uri); + virtual ~sensor_provider(); + + const char *get_uri(void); + sensor_info *get_sensor_info(void); + + bool connect(void); + bool disconnect(void); + void restore(void); + + void set_start_cb(sensord_provider_start_cb cb, void *user_data); + void set_stop_cb(sensord_provider_stop_cb cb, void *user_data); + void set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data); + + int publish(sensor_data_t *data, int len); + +private: + bool init(const char *uri); + void deinit(void); + + bool is_connected(void); + + int serialize(sensor_info *info, char **bytes); + int send_sensor_info(sensor_info *info); + + sensor_info m_sensor; + + ipc::ipc_client *m_client; + ipc::channel *m_channel; + ipc::event_loop m_loop; + sensor_provider_handler *m_handler; + std::atomic m_connected; +}; + +} + +#endif /* __SENSOR_PROVIDER_H__ */ diff --git a/src/client/sensor_provider_handler.cpp b/src/client/sensor_provider_handler.cpp new file mode 100644 index 0000000..7f3f366 --- /dev/null +++ b/src/client/sensor_provider_handler.cpp @@ -0,0 +1,87 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "sensor_provider_handler.h" + +#include +#include +#include "sensor_provider.h" + +using namespace sensor; + +sensor_provider_handler::sensor_provider_handler(sensor_provider *provider) +: m_provider(provider) +{ +} + +void sensor_provider_handler::connected(ipc::channel *ch) +{ + _I("Connected"); +} + +void sensor_provider_handler::disconnected(ipc::channel *ch) +{ + /* TODO */ + /* m_provider->restore(); */ +} + +void sensor_provider_handler::read(ipc::channel *ch, ipc::message &msg) +{ + switch (msg.type()) { + case CMD_PROVIDER_START: + m_start_cb(m_provider, m_start_user_data); + break; + case CMD_PROVIDER_STOP: + m_stop_cb(m_provider, m_stop_user_data); + break; + case CMD_PROVIDER_ATTR_INT: + cmd_provider_attr_int_t buf; + msg.disclose((char *)&buf); + + if (buf.attribute == SENSORD_ATTRIBUTE_INTERVAL) + m_set_interval_cb(m_provider, buf.value, m_set_interval_user_data); + break; + } +} + +void sensor_provider_handler::read_complete(ipc::channel *ch) +{ +} + +void sensor_provider_handler::error_caught(ipc::channel *ch, int error) +{ +} + +void sensor_provider_handler::set_start_cb(sensord_provider_start_cb cb, void *user_data) +{ + m_start_cb = cb; + m_start_user_data = user_data; +} + +void sensor_provider_handler::set_stop_cb(sensord_provider_stop_cb cb, void *user_data) +{ + m_stop_cb = cb; + m_stop_user_data = user_data; +} + +void sensor_provider_handler::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data) +{ + m_set_interval_cb = cb; + m_set_interval_user_data = user_data; +} diff --git a/src/client/sensor_provider_handler.h b/src/client/sensor_provider_handler.h new file mode 100644 index 0000000..16bf479 --- /dev/null +++ b/src/client/sensor_provider_handler.h @@ -0,0 +1,60 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __SENSOR_PROVIDER_HANDLER__ +#define __SENSOR_PROVIDER_HANDLER__ + +#include +#include + +namespace sensor { + +class sensor_provider; + +class sensor_provider_handler : public ipc::channel_handler +{ +public: + sensor_provider_handler(sensor_provider *provider); + + void connected(ipc::channel *ch); + void disconnected(ipc::channel *ch); + void read(ipc::channel *ch, ipc::message &msg); + + void read_complete(ipc::channel *ch); + void error_caught(ipc::channel *ch, int error); + + void set_start_cb(sensord_provider_start_cb cb, void *user_data); + void set_stop_cb(sensord_provider_stop_cb cb, void *user_data); + void set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data); + +private: + sensor_provider *m_provider; + + sensord_provider_start_cb m_start_cb; + sensord_provider_stop_cb m_stop_cb; + sensord_provider_set_interval_cb m_set_interval_cb; + + void *m_start_user_data; + void *m_stop_user_data; + void *m_set_interval_user_data; +}; + +} + +#endif /* __SENSOR_PROVIDER_HANDLER__ */ -- 2.7.4 From 247275695842b0f7e1c3b2b5bd40fa74302e5ce5 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:48:07 +0900 Subject: [PATCH 05/16] sensord: detect that a sensor is added or removed in runtime (client-side) Change-Id: Ie9c4e93ca77f911226bce6b72f9517185bb43dd2 Signed-off-by: kibak.yoon --- src/client/sensor_manager.cpp | 111 +++++++++++++++++++++------------ src/client/sensor_manager.h | 21 ++++--- src/client/sensor_manager_handler.cpp | 113 ++++++++++++++++++++++++++++++++++ src/client/sensor_manager_handler.h | 61 ++++++++++++++++++ 4 files changed, 261 insertions(+), 45 deletions(-) create mode 100644 src/client/sensor_manager_handler.cpp create mode 100644 src/client/sensor_manager_handler.h diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index 2d14a4d..11c61d9 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -31,37 +31,11 @@ using namespace sensor; -class manager_handler : public ipc::channel_handler -{ -public: - manager_handler(sensor_manager *manager) - : m_manager(manager) - {} - void connected(ipc::channel *ch) {} - void disconnected(ipc::channel *ch) - { - /* If channel->disconnect() is not explicitly called, it will be restored */ - m_manager->restore(); - } - - void read(ipc::channel *ch, ipc::message &msg) - { - /* TODO: if dynamic sensor is loaded, - * it will be called with the sensor information */ - } - - void read_complete(ipc::channel *ch) {} - void error_caught(ipc::channel *ch, int error) {} - -private: - sensor_manager *m_manager; -}; - sensor_manager::sensor_manager() : m_client(NULL) -, m_handler(NULL) , m_channel(NULL) , m_connected(false) +, m_handler(NULL) { init(); } @@ -71,16 +45,6 @@ sensor_manager::~sensor_manager() deinit(); } -int sensor_manager::get_sensor(sensor_type_t type, sensor_t *sensor) -{ - return get_sensor(utils::get_uri(type), sensor); -} - -int sensor_manager::get_sensors(sensor_type_t type, sensor_t **list, int *count) -{ - return get_sensors(utils::get_uri(type), list, count); -} - int sensor_manager::get_sensor(const char *uri, sensor_t *sensor) { if (!is_supported(uri)) { @@ -143,12 +107,65 @@ bool sensor_manager::is_supported(const char *uri) 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); + + 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); + 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_handler->add_sensor_added_cb(cb, user_data); +} + +void sensor_manager::remove_sensor_added_cb(sensord_added_cb cb) +{ + m_handler->remove_sensor_added_cb(cb); +} + +void sensor_manager::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data) +{ + m_handler->add_sensor_removed_cb(cb, user_data); +} + +void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb) +{ + m_handler->remove_sensor_removed_cb(cb); +} + bool sensor_manager::init(void) { m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) manager_handler(this); + m_handler = new(std::nothrow) sensor_manager_handler(this); if (!m_handler) { delete m_client; m_client = NULL; @@ -174,6 +191,16 @@ bool sensor_manager::connect_channel(void) m_channel = m_client->connect(m_handler, &m_loop); retvm_if(!m_channel, false, "Failed to connect to server"); + ipc::message msg; + msg.set_type(CMD_MANAGER_CONNECT); + m_channel->send_sync(&msg); + m_channel->read_sync(msg); + + if (msg.header()->err < 0) { + /* TODO: if failed, disconnect channel */ + return false; + } + m_connected.store(true); _D("Connected"); @@ -192,6 +219,14 @@ void sensor_manager::disconnect(void) { ret_if(!is_connected()); + ipc::message msg; + ipc::message reply; + msg.set_type(CMD_MANAGER_DISCONNECT); + + m_channel->send_sync(&msg); + m_channel->read_sync(reply); + retm_if(reply.header()->err < 0, "Failed to disconnect"); + m_connected.store(false); m_channel->disconnect(); diff --git a/src/client/sensor_manager.h b/src/client/sensor_manager.h index 8839943..d0fabf8 100644 --- a/src/client/sensor_manager.h +++ b/src/client/sensor_manager.h @@ -28,6 +28,8 @@ #include #include "sensor_internal.h" +#include "sensor_provider.h" +#include "sensor_manager_handler.h" namespace sensor { @@ -38,20 +40,25 @@ public: bool connect(void); void disconnect(void); + void restore(void); - int get_sensor(sensor_type_t type, sensor_t *sensor); - int get_sensors(sensor_type_t type, sensor_t **list, int *count); 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); - void restore(void); + /* 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); - /* TODO: register sensor_provider by using manager */ - /* int register_sensor(sensor_provider *provider); */ - /* int unregister_sensor(const char *uri) */ + void add_sensor_removed_cb(sensord_removed_cb cb, void *user_data); + void remove_sensor_removed_cb(sensord_removed_cb cb); private: typedef std::vector sensor_list_t; @@ -70,10 +77,10 @@ private: std::vector get_infos(const char *uri); ipc::ipc_client *m_client; - ipc::channel_handler *m_handler; ipc::channel *m_channel; ipc::event_loop m_loop; std::atomic m_connected; + sensor_manager_handler *m_handler; sensor_list_t m_sensors; }; diff --git a/src/client/sensor_manager_handler.cpp b/src/client/sensor_manager_handler.cpp new file mode 100644 index 0000000..5ba2acb --- /dev/null +++ b/src/client/sensor_manager_handler.cpp @@ -0,0 +1,113 @@ +/* + * 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_handler.h" + +#include +#include +#include "sensor_manager.h" + +using namespace sensor; + +sensor_manager_handler::sensor_manager_handler(sensor_manager *manager) +: m_manager(manager) +{ +} + +void sensor_manager_handler::connected(ipc::channel *ch) +{ +} + +void sensor_manager_handler::disconnected(ipc::channel *ch) +{ + /* If channel->disconnect() is not explicitly called, it will be restored */ + m_manager->restore(); +} + +void sensor_manager_handler::read(ipc::channel *ch, ipc::message &msg) +{ + switch (msg.header()->type) { + case CMD_MANAGER_SENSOR_ADDED: + on_sensor_added(ch, msg); + break; + case CMD_MANAGER_SENSOR_REMOVED: + on_sensor_removed(ch, msg); + break; + } +} + +void sensor_manager_handler::read_complete(ipc::channel *ch) +{ +} + +void sensor_manager_handler::error_caught(ipc::channel *ch, int error) +{ +} + +void sensor_manager_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg) +{ + ret_if(msg.header()->err < OP_SUCCESS); + + sensor_info info; + info.clear(); + info.deserialize(msg.body(), msg.size()); + + m_manager->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_handler::on_sensor_removed(ipc::channel *ch, ipc::message &msg) +{ + ret_if(msg.header()->err < 0); + char uri[NAME_MAX] = {0, }; + + msg.disclose(uri); + m_manager->remove_sensor(uri); + + auto it = m_sensor_removed_callbacks.begin(); + while (it != m_sensor_removed_callbacks.end()) { + it->first(uri, it->second); + ++it; + } +} + +void sensor_manager_handler::add_sensor_added_cb(sensord_added_cb cb, void *user_data) +{ + m_sensor_added_callbacks.emplace(cb, user_data); +} + +void sensor_manager_handler::remove_sensor_added_cb(sensord_added_cb cb) +{ + m_sensor_added_callbacks.erase(cb); +} + +void sensor_manager_handler::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data) +{ + m_sensor_removed_callbacks.emplace(cb, user_data); +} + +void sensor_manager_handler::remove_sensor_removed_cb(sensord_removed_cb cb) +{ + m_sensor_removed_callbacks.erase(cb); +} diff --git a/src/client/sensor_manager_handler.h b/src/client/sensor_manager_handler.h new file mode 100644 index 0000000..4f1ac73 --- /dev/null +++ b/src/client/sensor_manager_handler.h @@ -0,0 +1,61 @@ +/* + * 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_HANDLER__ +#define __SENSOR_MANAGER_HANDLER__ + +#include +#include +#include + +namespace sensor { + +class sensor_manager; + +class sensor_manager_handler : public ipc::channel_handler +{ +public: + sensor_manager_handler(sensor_manager *manager); + void connected(ipc::channel *ch); + void disconnected(ipc::channel *ch); + void read(ipc::channel *ch, ipc::message &msg); + void read_complete(ipc::channel *ch); + void error_caught(ipc::channel *ch, int error); + + void on_sensor_added(ipc::channel *ch, ipc::message &msg); + void on_sensor_removed(ipc::channel *ch, ipc::message &msg); + + 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); + +private: + typedef std::map sensor_added_cb_list_t; + typedef std::map sensor_removed_cb_list_t; + + sensor_manager *m_manager; + sensor_added_cb_list_t m_sensor_added_callbacks; + sensor_removed_cb_list_t m_sensor_removed_callbacks; +}; + +} + +#endif /* __SENSOR_MANAGER_HANDLER__ */ -- 2.7.4 From 10e723b80eebce838fd6365ba9184f8a080b298e Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:53:27 +0900 Subject: [PATCH 06/16] sensord: implement URI-based and provider internal APIs Change-Id: Id76f26d41ce124323bc581fd8ceb6820e73f1be7 Signed-off-by: kibak.yoon --- src/client/sensor_internal.cpp | 101 +++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index 28d4e71..da21e1c 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -20,10 +20,12 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -101,29 +103,12 @@ static std::unordered_map listeners; API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *count) { - int ret = OP_ERROR; - - retvm_if((!list || !count), -EINVAL, - "Invalid parameter[%#x, %#x]", list, count); - retvm_if(!manager.connect(), -EIO, "Failed to connect"); - - ret = manager.get_sensors(type, list, count); - retv_if(ret < 0, ret); - - return OP_SUCCESS; + return sensord_get_sensors_by_uri(utils::get_uri(type), list, count); } API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor) { - int ret = OP_ERROR; - - retvm_if(!sensor, -EINVAL, "Invalid parameter[%#x]", sensor); - retvm_if(!manager.connect(), -EIO, "Failed to connect"); - - ret = manager.get_sensor(type, sensor); - retv_if(ret < 0, ret); - - return OP_SUCCESS; + return sensord_get_default_sensor_by_uri(utils::get_uri(type), sensor); } API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) @@ -158,8 +143,8 @@ API const char* sensord_get_vendor(sensor_t sensor) API bool sensord_get_min_range(sensor_t sensor, float *min_range) { - retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!min_range, false, "Invalid parameter[%#x]", min_range); + retvm_if(!manager.connect(), false, "Failed to connect"); retvm_if(!manager.is_supported(sensor), false, "Invalid sensor[%#x]", sensor); @@ -546,7 +531,7 @@ API int sensord_get_default_sensor_by_uri(const char *uri, sensor_t *sensor) retvm_if(!sensor, -EINVAL, "Invalid parameter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); - return OP_SUCCESS; + return manager.get_sensor(uri, sensor); } API int sensord_get_sensors_by_uri(const char *uri, sensor_t **list, int *count) @@ -554,7 +539,7 @@ API int sensord_get_sensors_by_uri(const char *uri, sensor_t **list, int *count) retvm_if((!list || !count), -EINVAL, "Invalid parameter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); - return OP_SUCCESS; + return manager.get_sensors(uri, list, count); } API int sensord_add_sensor_added_cb(sensord_added_cb callback, void *user_data) @@ -562,6 +547,7 @@ API int sensord_add_sensor_added_cb(sensord_added_cb callback, void *user_data) retvm_if(!callback, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + manager.add_sensor_added_cb(callback, user_data); return OP_SUCCESS; } @@ -570,6 +556,7 @@ API int sensord_remove_sensor_added_cb(sensord_added_cb callback) retvm_if(!callback, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + manager.remove_sensor_added_cb(callback); return OP_SUCCESS; } @@ -578,6 +565,7 @@ API int sensord_add_sensor_removed_cb(sensord_removed_cb callback, void *user_da retvm_if(!callback, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + manager.add_sensor_removed_cb(callback, user_data); return OP_SUCCESS; } @@ -586,6 +574,7 @@ API int sensord_remove_sensor_removed_cb(sensord_removed_cb callback) retvm_if(!callback, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + manager.remove_sensor_removed_cb(callback); return OP_SUCCESS; } @@ -594,6 +583,12 @@ API int sensord_create_provider(const char *uri, sensord_provider_h *provider) { retvm_if(!provider, -EINVAL, "Invalid paramter"); + sensor_provider *p; + + p = new(std::nothrow) sensor_provider(uri); + retvm_if(!p, -ENOMEM, "Failed to allocate memory"); + + *provider = static_cast(p); return OP_SUCCESS; } @@ -601,6 +596,7 @@ API int sensord_destroy_provider(sensord_provider_h provider) { retvm_if(!provider, -EINVAL, "Invalid paramter"); + delete static_cast(provider); return OP_SUCCESS; } @@ -609,6 +605,18 @@ API int sensord_add_provider(sensord_provider_h provider) retvm_if(!provider, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + int ret; + sensor_provider *p = static_cast(provider); + + if (!p->connect()) + return OP_ERROR; + + ret = manager.add_sensor(p); + if (ret < 0) { + p->disconnect(); + return OP_ERROR; + } + return OP_SUCCESS; } @@ -617,6 +625,18 @@ API int sensord_remove_provider(sensord_provider_h provider) retvm_if(!provider, -EINVAL, "Invalid paramter"); retvm_if(!manager.connect(), -EIO, "Failed to connect"); + int ret; + sensor_provider *p = static_cast(provider); + + if (!p->disconnect()) + return OP_ERROR; + + ret = manager.remove_sensor(p); + if (ret < 0) { + p->connect(); + return OP_ERROR; + } + return OP_SUCCESS; } @@ -624,6 +644,11 @@ API int sensord_provider_set_name(sensord_provider_h provider, const char *name) { retvm_if(!provider, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + sensor_info *info = p->get_sensor_info(); + info->set_model(name); + return OP_SUCCESS; } @@ -631,6 +656,11 @@ API int sensord_provider_set_vendor(sensord_provider_h provider, const char *ven { retvm_if(!provider, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + sensor_info *info = p->get_sensor_info(); + info->set_vendor(vendor); + return OP_SUCCESS; } @@ -638,6 +668,12 @@ API int sensord_provider_set_range(sensord_provider_h provider, float min_range, { retvm_if(!provider, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + sensor_info *info = p->get_sensor_info(); + info->set_min_range(min_range); + info->set_max_range(max_range); + return OP_SUCCESS; } @@ -645,6 +681,11 @@ API int sensord_provider_set_resolution(sensord_provider_h provider, float resol { retvm_if(!provider, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + sensor_info *info = p->get_sensor_info(); + info->set_resolution(resolution); + return OP_SUCCESS; } @@ -653,6 +694,10 @@ API int sensord_provider_set_start_cb(sensord_provider_h provider, sensord_provi retvm_if(!provider, -EINVAL, "Invalid paramter"); retvm_if(!callback, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + p->set_start_cb(callback, user_data); + return OP_SUCCESS; } @@ -661,6 +706,9 @@ API int sensord_provider_set_stop_cb(sensord_provider_h provider, sensord_provid retvm_if(!provider, -EINVAL, "Invalid paramter"); retvm_if(!callback, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + p->set_stop_cb(callback, user_data); return OP_SUCCESS; } @@ -670,6 +718,10 @@ API int sensord_provider_set_set_interval_cb(sensord_provider_h provider, sensor retvm_if(!provider, -EINVAL, "Invalid paramter"); retvm_if(!callback, -EINVAL, "Invalid paramter"); + sensor_provider *p = static_cast(provider); + + p->set_interval_cb(callback, user_data); + return OP_SUCCESS; } @@ -677,7 +729,10 @@ API int sensord_provider_publish(sensord_provider_h provider, sensor_data_t data { retvm_if(!provider, -EINVAL, "Invalid paramter"); - return OP_SUCCESS; + sensor_provider *p = static_cast(provider); + + /* TODO: synchronous call is enough? */ + return p->publish(&data, sizeof(data)); } /* deperecated */ -- 2.7.4 From 6444aa5e415d65d4385c0cb55aeca9044472cc63 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 15:55:55 +0900 Subject: [PATCH 07/16] sensord: do not let the listener have sensor pointer directly - Since the sensor could be registered or unregistered in runtime, the sensor pointer of sensor listener can be NULL when the sensor is unregistered. - This patch modifies the listener has only the sensor URI and requests a handle to the sensor manager if needed. Change-Id: I7967a332577eb7a0a41c7e678eea85b6b3f55dfd Signed-off-by: kibak.yoon --- src/server/sensor_listener_proxy.cpp | 54 +++++++++++++++++++++++++++-------- src/server/sensor_listener_proxy.h | 8 ++++-- src/server/server_channel_handler.cpp | 9 ++---- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/server/sensor_listener_proxy.cpp b/src/server/sensor_listener_proxy.cpp index b542af8..7c20c90 100644 --- a/src/server/sensor_listener_proxy.cpp +++ b/src/server/sensor_listener_proxy.cpp @@ -25,12 +25,15 @@ #include #include +#include "sensor_handler.h" + using namespace sensor; -sensor_listener_proxy::sensor_listener_proxy( - uint32_t id, sensor_handler *sensor, ipc::channel *ch) +sensor_listener_proxy::sensor_listener_proxy(uint32_t id, + std::string uri, sensor_manager *manager, ipc::channel *ch) : m_id(id) -, m_sensor(sensor) +, m_uri(uri) +, m_manager(manager) , m_ch(ch) , m_passive(false) , m_pause_policy(SENSORD_PAUSE_ALL) @@ -63,16 +66,22 @@ int sensor_listener_proxy::update(const char *uri, ipc::message *msg) int sensor_listener_proxy::start(void) { + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + /* TODO: listen pause policy */ - return m_sensor->start(this); + return sensor->start(this); } int sensor_listener_proxy::stop(void) { + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + /* TODO: listen pause policy */ int ret; - ret = m_sensor->stop(this); + ret = sensor->stop(this); retv_if(ret < 0, OP_ERROR); /* unset attributes */ @@ -84,12 +93,18 @@ int sensor_listener_proxy::stop(void) int sensor_listener_proxy::set_interval(unsigned int interval) { - return m_sensor->set_interval(this, interval); + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + return sensor->set_interval(this, interval); } int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency) { - return m_sensor->set_batch_latency(this, max_batch_latency); + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + return sensor->set_batch_latency(this, max_batch_latency); } int sensor_listener_proxy::set_passive_mode(bool passive) @@ -101,6 +116,9 @@ int sensor_listener_proxy::set_passive_mode(bool passive) int sensor_listener_proxy::set_attribute(int attribute, int value) { + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) { m_pause_policy = value; return OP_SUCCESS; @@ -109,27 +127,39 @@ int sensor_listener_proxy::set_attribute(int attribute, int value) return OP_SUCCESS; } - return m_sensor->set_attribute(this, attribute, value); + return sensor->set_attribute(this, attribute, value); } int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len) { - return m_sensor->set_attribute(this, attribute, value, len); + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + return sensor->set_attribute(this, attribute, value, len); } int sensor_listener_proxy::flush(void) { - return m_sensor->flush(this); + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + return sensor->flush(this); } int sensor_listener_proxy::get_data(sensor_data_t **data, int *len) { + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + /* TODO : caching the last data & retry logic if there is no data */ - return m_sensor->get_data(data, len); + return sensor->get_data(data, len); } std::string sensor_listener_proxy::get_required_privileges(void) { - sensor_info info = m_sensor->get_sensor_info(); + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, ""); + + sensor_info info = sensor->get_sensor_info(); return info.get_privilege(); } diff --git a/src/server/sensor_listener_proxy.h b/src/server/sensor_listener_proxy.h index 583cf36..05bc453 100644 --- a/src/server/sensor_listener_proxy.h +++ b/src/server/sensor_listener_proxy.h @@ -23,14 +23,15 @@ #include #include -#include "sensor_handler.h" +#include "sensor_manager.h" #include "sensor_observer.h" namespace sensor { class sensor_listener_proxy : public sensor_observer { public: - sensor_listener_proxy(uint32_t id, sensor_handler *sensor, ipc::channel *ch); + sensor_listener_proxy(uint32_t id, + std::string uri, sensor_manager *manager, ipc::channel *ch); ~sensor_listener_proxy(); uint32_t get_id(void); @@ -52,8 +53,9 @@ public: private: uint32_t m_id; + std::string m_uri; - sensor_handler *m_sensor; + sensor_manager *m_manager; ipc::channel *m_ch; bool m_passive; diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index d5ad86f..3a0fb44 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -116,16 +116,13 @@ int server_channel_handler::manager_get_sensor_list(channel *ch, message &msg) int server_channel_handler::listener_connect(channel *ch, message &msg) { static uint32_t listener_id = 1; - sensor_handler *sensor; cmd_listener_connect_t buf; msg.disclose((char *)&buf); - sensor = m_manager->get_sensor(buf.sensor); - retv_if(!sensor, OP_ERROR); - - sensor_listener_proxy *listener = - new(std::nothrow) sensor_listener_proxy(listener_id, sensor, ch); + sensor_listener_proxy *listener; + listener = new(std::nothrow) sensor_listener_proxy(listener_id, + buf.sensor, m_manager, ch); retvm_if(!listener, OP_ERROR, "Failed to allocate memory"); retvm_if(!has_privileges(ch->get_fd(), listener->get_required_privileges()), -EACCES, "Permission denied"); -- 2.7.4 From 54276985c8d4d7ba48df658b1ac005b3ecff2056 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 16:13:00 +0900 Subject: [PATCH 08/16] sensord: implement application sensor handler Change-Id: If415a7268adf9d7cd5870ba62a0f4bd7497baccf Signed-off-by: kibak.yoon --- src/server/application_sensor_handler.cpp | 63 ++++++++++++++++++++++++- src/server/application_sensor_handler.h | 12 ++++- src/server/server_channel_handler.cpp | 78 ++++++++++++++++++++++++++----- src/server/server_channel_handler.h | 12 ++--- 4 files changed, 144 insertions(+), 21 deletions(-) diff --git a/src/server/application_sensor_handler.cpp b/src/server/application_sensor_handler.cpp index 1070156..a6bd0bd 100644 --- a/src/server/application_sensor_handler.cpp +++ b/src/server/application_sensor_handler.cpp @@ -19,13 +19,18 @@ #include "application_sensor_handler.h" +#include +#include #include #include using namespace sensor; -application_sensor_handler::application_sensor_handler(const sensor_info &info) +application_sensor_handler::application_sensor_handler(const sensor_info &info, ipc::channel *ch) : m_info(info) +, m_ch(ch) +, m_started(false) +, m_prev_interval(0) { } @@ -33,7 +38,7 @@ application_sensor_handler::~application_sensor_handler() { } -int application_sensor_handler::post(sensor_data_t *data, int len) +int application_sensor_handler::publish(sensor_data_t *data, int len) { std::string uri = m_info.get_uri(); return notify(uri.c_str(), data, len); @@ -48,6 +53,14 @@ int application_sensor_handler::start(sensor_observer *ob) { add_observer(ob); + if (observer_count() > 1 || m_started.load()) + return OP_SUCCESS; /* already started */ + + ipc::message msg; + msg.set_type(CMD_PROVIDER_START); + m_ch->send_sync(&msg); + m_started.store(true); + return OP_SUCCESS; } @@ -55,11 +68,57 @@ int application_sensor_handler::stop(sensor_observer *ob) { remove_observer(ob); + if (observer_count() > 0 || !m_started.load()) + return OP_SUCCESS; /* already started */ + + ipc::message msg; + msg.set_type(CMD_PROVIDER_STOP); + m_ch->send_sync(&msg); + m_started.store(false); + return OP_SUCCESS; } +int application_sensor_handler::get_min_interval(void) +{ + int interval; + std::vector temp; + + for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it) + if (it->second > 0) + temp.push_back(it->second); + + if (temp.empty()) + return m_info.get_min_interval(); + + interval = *std::min_element(temp.begin(), temp.end()); + + if (interval < m_info.get_min_interval()) + return m_info.get_min_interval(); + + return interval; +} + int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) { + retv_if(interval == m_prev_interval, OP_SUCCESS); + + int32_t cur_interval = interval; + + m_interval_map[ob] = cur_interval; + cur_interval = get_min_interval(); + + ipc::message msg; + cmd_provider_attr_int_t buf; + buf.attribute = SENSORD_ATTRIBUTE_INTERVAL; + buf.value = cur_interval; + + msg.set_type(CMD_PROVIDER_ATTR_INT); + msg.enclose((const char *)&buf, sizeof(cmd_provider_attr_int_t)); + m_ch->send_sync(&msg); + + m_prev_interval = cur_interval; + return OP_SUCCESS; } diff --git a/src/server/application_sensor_handler.h b/src/server/application_sensor_handler.h index 87d07fd..2d2801e 100644 --- a/src/server/application_sensor_handler.h +++ b/src/server/application_sensor_handler.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "sensor_handler.h" #include "sensor_observer.h" @@ -31,11 +32,11 @@ namespace sensor { class application_sensor_handler : public sensor_handler { public: - application_sensor_handler(const sensor_info &info); + application_sensor_handler(const sensor_info &info, ipc::channel *ch); ~application_sensor_handler(); /* TODO: const */ - int post(sensor_data_t *data, int len); + int publish(sensor_data_t *data, int len); /* sensor interface */ const sensor_info &get_sensor_info(void); @@ -52,7 +53,14 @@ public: private: sensor_info m_info; + ipc::channel *m_ch; + std::atomic m_started; + int32_t m_prev_interval; + + int get_min_interval(void); + std::vector m_required_sensors; + std::unordered_map m_interval_map; }; } diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index 3a0fb44..5ad196e 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -26,12 +26,18 @@ #include #include "permission_checker.h" +#include "application_sensor_handler.h" #define PRIV_DELIMINATOR ";" using namespace sensor; using namespace ipc; +/* TODO */ +std::unordered_map server_channel_handler::m_listeners; +std::unordered_map server_channel_handler::m_listener_ids; +std::unordered_map server_channel_handler::m_app_sensors; + server_channel_handler::server_channel_handler(sensor_manager *manager) : m_manager(manager) { @@ -47,14 +53,25 @@ void server_channel_handler::connected(channel *ch) void server_channel_handler::disconnected(channel *ch) { - auto it = m_listener_ids.find(ch); - ret_if(it == m_listener_ids.end()); + auto it_asensor = m_app_sensors.find(ch); + if (it_asensor != m_app_sensors.end()) { + sensor_info info = it_asensor->second->get_sensor_info(); - _I("Disconnected listener[%u]", it->second); + _I("Disconnected provider[%s]", info.get_uri().c_str()); - delete m_listeners[it->second]; - m_listeners.erase(it->second); - m_listener_ids.erase(ch); + /* TODO: if sensor is unregistered, related listeners has to be handled */ + m_manager->deregister_sensor(info.get_uri()); + m_app_sensors.erase(ch); + } + + auto it_listener = m_listener_ids.find(ch); + if (it_listener != m_listener_ids.end()) { + _I("Disconnected listener[%u]", it_listener->second); + + delete m_listeners[it_listener->second]; + m_listeners.erase(it_listener->second); + m_listener_ids.erase(ch); + } } void server_channel_handler::read(channel *ch, message &msg) @@ -83,7 +100,7 @@ void server_channel_handler::read(channel *ch, message &msg) case CMD_PROVIDER_DISCONNECT: err = provider_disconnect(ch, msg); break; case CMD_PROVIDER_PUBLISH: - err = provider_post(ch, msg); break; + err = provider_publish(ch, msg); break; case CMD_HAS_PRIVILEGE: err = has_privileges(ch, msg); break; default: break; @@ -281,17 +298,56 @@ int server_channel_handler::listener_get_data(channel *ch, message &msg) int server_channel_handler::provider_connect(channel *ch, message &msg) { - return send_reply(ch, OP_ERROR); + retvm_if(!has_privileges(ch->get_fd(), PRIV_APPLICATION_SENSOR_WRITE), + -EACCES, "Permission denied"); + + sensor_info info; + info.clear(); + info.deserialize(msg.body(), msg.size()); + + info.show(); + + application_sensor_handler *sensor; + sensor = new(std::nothrow) application_sensor_handler(info, ch); + retvm_if(!sensor, -ENOMEM, "Failed to allocate memory"); + + if (!m_manager->register_sensor(sensor)) { + delete sensor; + return -EINVAL; + } + + /* temporarily */ + m_app_sensors[ch] = sensor; + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::provider_disconnect(channel *ch, message &msg) { - return send_reply(ch, OP_ERROR); + auto it = m_app_sensors.find(ch); + retv_if(it == m_app_sensors.end(), -EINVAL); + + sensor_info info = it->second->get_sensor_info(); + + m_manager->deregister_sensor(info.get_uri()); + m_app_sensors.erase(ch); + + return send_reply(ch, OP_SUCCESS); } -int server_channel_handler::provider_post(channel *ch, message &msg) +int server_channel_handler::provider_publish(channel *ch, message &msg) { - return send_reply(ch, OP_ERROR); + auto it = m_app_sensors.find(ch); + retv_if(it == m_app_sensors.end(), -EINVAL); + + sensor_data_t *data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!data, -ENOMEM, "Failed to allocate memory"); + + msg.disclose((char *)data); + + it->second->publish(data, sizeof(sensor_data_t)); + + return OP_SUCCESS; } int server_channel_handler::has_privileges(channel *ch, message &msg) diff --git a/src/server/server_channel_handler.h b/src/server/server_channel_handler.h index a2d9fce..9c912be 100644 --- a/src/server/server_channel_handler.h +++ b/src/server/server_channel_handler.h @@ -56,7 +56,7 @@ private: int provider_connect(ipc::channel *ch, ipc::message &msg); int provider_disconnect(ipc::channel *ch, ipc::message &msg); - int provider_post(ipc::channel *ch, ipc::message &msg); + int provider_publish(ipc::channel *ch, ipc::message &msg); int has_privileges(ipc::channel *ch, ipc::message &msg); @@ -68,14 +68,14 @@ private: sensor_manager *m_manager; /* {id, listener} */ - std::unordered_map m_listeners; + static std::unordered_map m_listeners; /* {channel, id} */ - std::unordered_map m_listener_ids; + static std::unordered_map m_listener_ids; - /* {name, application_sensor_handler} */ - /* TODO: move it to sensor_manager */ - std::unordered_map m_sensors; + /* {channel, application_sensor_handler} */ + /* it should move to sensor_manager */ + static std::unordered_map m_app_sensors; }; } -- 2.7.4 From 17a6546e46224544fbe3416c27586b878707018f Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 16:15:47 +0900 Subject: [PATCH 09/16] sensord: notify that the sensor is registered or unregistered Change-Id: I606dd37f3ad045db4ac0d9517b7c87469b9ac37e Signed-off-by: kibak.yoon --- src/server/sensor_manager.cpp | 72 +++++++++++++++++++++++++++++++++++ src/server/sensor_manager.h | 14 ++++++- src/server/server_channel_handler.cpp | 19 ++++++++- src/server/server_channel_handler.h | 3 ++ 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index 9a06489..8d79ed0 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include #include @@ -105,6 +107,53 @@ bool sensor_manager::is_supported(std::string uri) return false; } +int sensor_manager::serialize(sensor_info *info, char **bytes) +{ + int size; + raw_data_t *raw = new(std::nothrow) raw_data_t; + retvm_if(!raw, -ENOMEM, "Failed to allocated memory"); + + info->serialize(*raw); + + *bytes = new(std::nothrow) char[raw->size()]; + retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory"); + + std::copy(raw->begin(), raw->end(), *bytes); + + size = raw->size(); + delete raw; + + return size; +} + +void sensor_manager::send(ipc::message &msg) +{ + for (auto it = m_channels.begin(); it != m_channels.end(); ++it) + (*it)->send_sync(&msg); +} + +void sensor_manager::send_added_msg(sensor_info *info) +{ + char *bytes; + int size; + + size = serialize(info, &bytes); + + ipc::message msg((const char *)bytes, size); + msg.set_type(CMD_MANAGER_SENSOR_ADDED); + + send(msg); +} + +void sensor_manager::send_removed_msg(const std::string &uri) +{ + ipc::message msg; + msg.set_type(CMD_MANAGER_SENSOR_REMOVED); + msg.enclose(uri.c_str(), uri.size()); + + send(msg); +} + bool sensor_manager::register_sensor(sensor_handler *sensor) { retvm_if(!sensor, false, "Invalid sensor"); @@ -116,6 +165,8 @@ bool sensor_manager::register_sensor(sensor_handler *sensor) m_sensors[info.get_uri()] = sensor; + send_added_msg(&info); + _I("Registered[%s]", info.get_uri().c_str()); return true; @@ -129,9 +180,29 @@ void sensor_manager::deregister_sensor(const std::string uri) delete it->second; m_sensors.erase(it); + send_removed_msg(uri); + _I("Deregistered[%s]", uri.c_str()); } +void sensor_manager::register_channel(ipc::channel *ch) +{ + ret_if(!ch); + m_channels.push_back(ch); +} + +void sensor_manager::deregister_channel(ipc::channel *ch) +{ + ret_if(!ch); + + for (auto it = m_channels.begin(); it != m_channels.end(); ++it) { + if (*it == ch) { + m_channels.erase(it); + return; + } + } +} + sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri) { auto it = m_sensors.begin(); @@ -247,6 +318,7 @@ static void put_int_to_vec(std::vector &data, int value) std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data)); } +/* TODO: remove socket fd parameter */ /* packet format : * [count:4] {[size:4] [info:n] [size:4] [info:n] ...} */ diff --git a/src/server/sensor_manager.h b/src/server/sensor_manager.h index 5c09b0e..78692a8 100644 --- a/src/server/sensor_manager.h +++ b/src/server/sensor_manager.h @@ -24,7 +24,8 @@ #include #include -#include "event_loop.h" +#include +#include #include "sensor_handler.h" #include "sensor_observer.h" @@ -49,6 +50,9 @@ public: bool register_sensor(sensor_handler *sensor); void deregister_sensor(const std::string uri); + void register_channel(ipc::channel *ch); + void deregister_channel(ipc::channel *ch); + sensor_handler *get_sensor_by_type(const std::string uri); sensor_handler *get_sensor(const std::string uri); std::vector get_sensors(void); @@ -67,11 +71,19 @@ private: void init_sensors(void); void register_handler(physical_sensor_handler *sensor); + int serialize(sensor_info *info, char **bytes); + + void send(ipc::message &msg); + void send_added_msg(sensor_info *info); + void send_removed_msg(const std::string &uri); + void show(void); ipc::event_loop *m_loop; sensor_loader m_loader; sensor_map_t m_sensors; + + std::vector m_channels; }; } diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index 5ad196e..429013a 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -53,13 +53,14 @@ void server_channel_handler::connected(channel *ch) void server_channel_handler::disconnected(channel *ch) { + m_manager->deregister_channel(ch); + auto it_asensor = m_app_sensors.find(ch); if (it_asensor != m_app_sensors.end()) { sensor_info info = it_asensor->second->get_sensor_info(); _I("Disconnected provider[%s]", info.get_uri().c_str()); - /* TODO: if sensor is unregistered, related listeners has to be handled */ m_manager->deregister_sensor(info.get_uri()); m_app_sensors.erase(ch); } @@ -79,6 +80,10 @@ void server_channel_handler::read(channel *ch, message &msg) int err = -EINVAL; switch (msg.type()) { + case CMD_MANAGER_CONNECT: + err = manager_connect(ch, msg); break; + case CMD_MANAGER_DISCONNECT: + err = manager_disconnect(ch, msg); break; case CMD_MANAGER_SENSOR_LIST: err = manager_get_sensor_list(ch, msg); break; case CMD_LISTENER_CONNECT: @@ -112,6 +117,18 @@ void server_channel_handler::read(channel *ch, message &msg) } } +int server_channel_handler::manager_connect(channel *ch, message &msg) +{ + m_manager->register_channel(ch); + return send_reply(ch, OP_SUCCESS); +} + +int server_channel_handler::manager_disconnect(channel *ch, message &msg) +{ + m_manager->deregister_channel(ch); + return send_reply(ch, OP_SUCCESS); +} + int server_channel_handler::manager_get_sensor_list(channel *ch, message &msg) { ipc::message reply; diff --git a/src/server/server_channel_handler.h b/src/server/server_channel_handler.h index 9c912be..befa663 100644 --- a/src/server/server_channel_handler.h +++ b/src/server/server_channel_handler.h @@ -44,8 +44,11 @@ public: void error_caught(ipc::channel *ch, int error) {} private: + int manager_connect(ipc::channel *ch, ipc::message &msg); + int manager_disconnect(ipc::channel *ch, ipc::message &msg); int manager_get_sensor_list(ipc::channel *ch, ipc::message &msg); + int listener_connect(ipc::channel *ch, ipc::message &msg); int listener_disconnect(ipc::channel *ch, ipc::message &msg); int listener_start(ipc::channel *ch, ipc::message &msg); -- 2.7.4 From e76501a283287b9e87766b2e44065dba83dae313 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 19:18:15 +0900 Subject: [PATCH 10/16] sensord: add internal API to get URI of sensor Change-Id: I95f68aa8fefe6a04f42f621de813801859b84884 Signed-off-by: kibak.yoon --- include/sensor_internal.h | 8 ++++++++ src/client-dummy/client_dummy.cpp | 5 +++++ src/client/sensor_internal.cpp | 11 ++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/sensor_internal.h b/include/sensor_internal.h index 9911949..49609b6 100644 --- a/include/sensor_internal.h +++ b/include/sensor_internal.h @@ -95,6 +95,14 @@ int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor); bool sensord_get_type(sensor_t sensor, sensor_type_t *type); /** + * @brief Get the URI string of this sensor. + * + * @param[in] sensor a sensor to get uri. + * @return the name string of this sensor on success, otherwise NULL. + */ +const char* sensord_get_uri(sensor_t sensor); + +/** * @brief Get the name string of this sensor. * * @param[in] sensor a sensor to get name. diff --git a/src/client-dummy/client_dummy.cpp b/src/client-dummy/client_dummy.cpp index ee2de11..63ae2ea 100644 --- a/src/client-dummy/client_dummy.cpp +++ b/src/client-dummy/client_dummy.cpp @@ -48,6 +48,11 @@ API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) return false; } +API const char* sensord_get_uri(sensor_t sensor) +{ + return NULL; +} + API const char* sensord_get_name(sensor_t sensor) { return NULL; diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index da21e1c..1a5b0b7 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -123,7 +123,7 @@ API bool sensord_get_type(sensor_t sensor, sensor_type_t *type) return true; } -API const char* sensord_get_name(sensor_t sensor) +API const char* sensord_get_uri(sensor_t sensor) { retvm_if(!manager.connect(), NULL, "Failed to connect"); retvm_if(!manager.is_supported(sensor), NULL, @@ -132,6 +132,15 @@ API const char* sensord_get_name(sensor_t sensor) return static_cast(sensor)->get_uri().c_str(); } +API const char* sensord_get_name(sensor_t sensor) +{ + retvm_if(!manager.connect(), NULL, "Failed to connect"); + retvm_if(!manager.is_supported(sensor), NULL, + "Invalid sensor[%#x]", sensor); + + return static_cast(sensor)->get_model().c_str(); +} + API const char* sensord_get_vendor(sensor_t sensor) { retvm_if(!manager.connect(), NULL, "Failed to connect"); -- 2.7.4 From 3a53d78d7d4d509d85e415de42f697eb7f14193b Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 19:27:19 +0900 Subject: [PATCH 11/16] sensord: initialize class members of sensor_provider_handler Change-Id: I39bd1d2f4b73cfcca23132fb65aa3f95b0872d59 Signed-off-by: kibak.yoon --- src/client/sensor_provider_handler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/sensor_provider_handler.cpp b/src/client/sensor_provider_handler.cpp index 7f3f366..14fa3a1 100644 --- a/src/client/sensor_provider_handler.cpp +++ b/src/client/sensor_provider_handler.cpp @@ -27,6 +27,12 @@ using namespace sensor; sensor_provider_handler::sensor_provider_handler(sensor_provider *provider) : m_provider(provider) +, m_start_cb(NULL) +, m_stop_cb(NULL) +, m_set_interval_cb(NULL) +, m_start_user_data(NULL) +, m_stop_user_data(NULL) +, m_set_interval_user_data(NULL) { } -- 2.7.4 From 6972932bf043cc56a041a90c07048c77b58cc8c2 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Mon, 24 Apr 2017 21:48:55 +0900 Subject: [PATCH 12/16] sensord: rename URIs of sensor type and support get_sensor(uri) properly - compare string before last slash Change-Id: I2e5f6a317298b96db8d712d5545e17f89f8408e5 Signed-off-by: kibak.yoon --- src/client/sensor_manager.cpp | 45 ++++++++++++++++------- src/sensor/linear_accel/linear_accel_sensor.cpp | 2 +- src/sensor/rotation_vector/gyro_rv_sensor.cpp | 2 +- src/sensor/rotation_vector/magnetic_rv_sensor.cpp | 2 +- src/server/sensor_manager.cpp | 13 +++++-- src/shared/sensor_utils.cpp | 18 ++++----- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index 11c61d9..b2917b2 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -98,9 +98,14 @@ bool sensor_manager::is_supported(const char *uri) return true; for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { - std::size_t found = (*it).get_uri().rfind(uri); + if ((*it).get_uri() == uri) + return true; + + std::size_t found = (*it).get_uri().find_last_of("/"); + if (found == std::string::npos) + continue; - if (found != std::string::npos) + if ((*it).get_uri().substr(0, found) == uri) return true; } @@ -331,15 +336,23 @@ sensor_info *sensor_manager::get_info(const char *uri) return &m_sensors[0]; for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { - std::size_t found = (*it).get_uri().rfind(uri); - - if (found == std::string::npos) + if ((*it).get_uri() != uri) continue; - if ((*it).get_privilege().empty()) + if ((*it).get_privilege().empty() || has_privilege((*it).get_uri())) return &*it; - if (has_privilege((*it).get_uri())) + 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; } @@ -355,17 +368,23 @@ std::vector sensor_manager::get_infos(const char *uri) all = true; for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { - std::size_t found = (*it).get_uri().rfind(uri); - - if (!all && found == std::string::npos) + if ((*it).get_uri() != uri) continue; - if ((*it).get_privilege().empty()) { + 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 (has_privilege((*it).get_uri())) + if ((*it).get_privilege().empty() || has_privilege((*it).get_uri())) infos.push_back(&*it); } diff --git a/src/sensor/linear_accel/linear_accel_sensor.cpp b/src/sensor/linear_accel/linear_accel_sensor.cpp index 27cce33..00e0bb7 100644 --- a/src/sensor/linear_accel/linear_accel_sensor.cpp +++ b/src/sensor/linear_accel/linear_accel_sensor.cpp @@ -23,7 +23,7 @@ #include #include -#define NAME_SENSOR "http://tizen.org/sensor/linear_accel/tizen_default" +#define NAME_SENSOR "http://tizen.org/sensor/linear_acceleration/tizen_default" #define NAME_VENDOR "tizen.org" #define SRC_ID_ACC 0x1 diff --git a/src/sensor/rotation_vector/gyro_rv_sensor.cpp b/src/sensor/rotation_vector/gyro_rv_sensor.cpp index ccafce8..74e0e27 100644 --- a/src/sensor/rotation_vector/gyro_rv_sensor.cpp +++ b/src/sensor/rotation_vector/gyro_rv_sensor.cpp @@ -23,7 +23,7 @@ #include #include -#define NAME_SENSOR "http://tizen.org/sensor/gyro_rotation_vector/tizen_default" +#define NAME_SENSOR "http://tizen.org/sensor/gyroscope_rotation_vector/tizen_default" #define NAME_VENDOR "tizen.org" #define SRC_ID_ACC 0x1 diff --git a/src/sensor/rotation_vector/magnetic_rv_sensor.cpp b/src/sensor/rotation_vector/magnetic_rv_sensor.cpp index fd9ad36..2d480d2 100644 --- a/src/sensor/rotation_vector/magnetic_rv_sensor.cpp +++ b/src/sensor/rotation_vector/magnetic_rv_sensor.cpp @@ -23,7 +23,7 @@ #include #include -#define NAME_SENSOR "http://tizen.org/sensor/mag_rotation_vector/tizen_default" +#define NAME_SENSOR "http://tizen.org/sensor/geomagnetic_rotation_vector/tizen_default" #define NAME_VENDOR "tizen.org" #define SRC_ID_ACC 0x1 diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index 8d79ed0..601c5ef 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -205,10 +205,15 @@ void sensor_manager::deregister_channel(ipc::channel *ch) sensor_handler *sensor_manager::get_sensor_by_type(const std::string uri) { - auto it = m_sensors.begin(); - for (; it != m_sensors.end(); ++it) { - std::size_t found = it->first.rfind(uri); - if (found != std::string::npos) + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { + if (it->first == uri) + return it->second; + + std::size_t found = it->first.find_last_of("/"); + if (found == std::string::npos) + continue; + + if (it->first.substr(0, found) == uri) return it->second; } diff --git a/src/shared/sensor_utils.cpp b/src/shared/sensor_utils.cpp index 817eb31..8313c4d 100644 --- a/src/shared/sensor_utils.cpp +++ b/src/shared/sensor_utils.cpp @@ -40,8 +40,8 @@ static std::map types = { {ALL_SENSOR, "http://tizen.org/sensor/all"}, {ACCELEROMETER_SENSOR, "http://tizen.org/sensor/accelerometer"}, {GRAVITY_SENSOR, "http://tizen.org/sensor/gravity"}, - {LINEAR_ACCEL_SENSOR, "http://tizen.org/sensor/linear_accel"}, - {GEOMAGNETIC_SENSOR, "http://tizen.org/sensor/geomagnetic"}, + {LINEAR_ACCEL_SENSOR, "http://tizen.org/sensor/linear_acceleration"}, + {GEOMAGNETIC_SENSOR, "http://tizen.org/sensor/magnetic"}, {ROTATION_VECTOR_SENSOR, "http://tizen.org/sensor/rotation_vector"}, {ORIENTATION_SENSOR, "http://tizen.org/sensor/orientation"}, {GYROSCOPE_SENSOR, "http://tizen.org/sensor/gyroscope"}, @@ -52,13 +52,13 @@ static std::map types = { {TEMPERATURE_SENSOR, "http://tizen.org/sensor/temperature"}, {HUMIDITY_SENSOR, "http://tizen.org/sensor/humidity"}, {HRM_SENSOR, "http://tizen.org/sensor/heart_rate_monitor"}, - {HRM_LED_GREEN_SENSOR, "http://tizen.org/sensor/hrm_led_green"}, - {HRM_LED_IR_SENSOR, "http://tizen.org/sensor/hrm_led_ir"}, - {HRM_LED_RED_SENSOR, "http://tizen.org/sensor/hrm_led_red"}, - {GYROSCOPE_UNCAL_SENSOR, "http://tizen.org/sensor/gyro_uncalibrated"}, - {GEOMAGNETIC_UNCAL_SENSOR, "http://tizen.org/sensor/mag_uncalibrated"}, - {GYROSCOPE_RV_SENSOR, "http://tizen.org/sensor/gyro_rotation_vector"}, - {GEOMAGNETIC_RV_SENSOR, "http://tizen.org/sensor/mag_rotation_vector"}, + {HRM_LED_GREEN_SENSOR, "http://tizen.org/sensor/heart_rate_monitor.led_green"}, + {HRM_LED_IR_SENSOR, "http://tizen.org/sensor/heart_rate_monitor.led_ir"}, + {HRM_LED_RED_SENSOR, "http://tizen.org/sensor/heart_rate_monitor.led_red"}, + {GYROSCOPE_UNCAL_SENSOR, "http://tizen.org/sensor/gyroscope.uncalibrated"}, + {GEOMAGNETIC_UNCAL_SENSOR, "http://tizen.org/sensor/geomagnetic.uncalibrated"}, + {GYROSCOPE_RV_SENSOR, "http://tizen.org/sensor/gyroscope_rotation_vector"}, + {GEOMAGNETIC_RV_SENSOR, "http://tizen.org/sensor/geomagnetic_rotation_vector"}, {HUMAN_PEDOMETER_SENSOR, "http://tizen.org/sensor/human_pedometer"}, {HUMAN_SLEEP_MONITOR_SENSOR, "http://tizen.org/sensor/human_sleep_monitor"}, -- 2.7.4 From cf0b43f2c0e1e5c3c706acf01141efc6a093781c Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 25 Apr 2017 13:53:18 +0900 Subject: [PATCH 13/16] sensord: assign a category number to enum of commands manager : 0x100 listener : 0x200 provider : 0x300 etc : 0x1000 Change-Id: I137dd95bc523ac591775b743f9cc6e16234a78c6 Signed-off-by: kibak.yoon --- src/shared/command_types.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/shared/command_types.h b/src/shared/command_types.h index 0ada031..6ea1523 100644 --- a/src/shared/command_types.h +++ b/src/shared/command_types.h @@ -32,14 +32,14 @@ enum cmd_type_e { CMD_NONE = 0, /* Manager */ - CMD_MANAGER_CONNECT, + CMD_MANAGER_CONNECT = 0x100, CMD_MANAGER_DISCONNECT, CMD_MANAGER_SENSOR_LIST, CMD_MANAGER_SENSOR_ADDED, CMD_MANAGER_SENSOR_REMOVED, /* Listener */ - CMD_LISTENER_EVENT, + CMD_LISTENER_EVENT = 0x200, CMD_LISTENER_ACC_EVENT, CMD_LISTENER_CONNECT, CMD_LISTENER_DISCONNECT, @@ -50,14 +50,15 @@ enum cmd_type_e { CMD_LISTENER_GET_DATA, /* Provider */ - CMD_PROVIDER_CONNECT, + CMD_PROVIDER_CONNECT = 0x300, CMD_PROVIDER_DISCONNECT, CMD_PROVIDER_START, CMD_PROVIDER_STOP, CMD_PROVIDER_ATTR_INT, CMD_PROVIDER_PUBLISH, - CMD_HAS_PRIVILEGE, + /* Etc */ + CMD_HAS_PRIVILEGE = 0x1000, CMD_CNT, }; -- 2.7.4 From 0caaba1caf776b90daf11be00d874ac7180f930a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 25 Apr 2017 18:10:59 +0900 Subject: [PATCH 14/16] sensord: change unclear classes to inner class - sensor_manager_handler -> sensor_manager::channel_handler - sensor_provider_handler -> sensor_provider::channel_handler Change-Id: I07637cd51e8075ca7ba7271e4b8fb4a6b2c02230 Signed-off-by: kibak.yoon --- src/client/sensor_manager.cpp | 4 +++- src/client/sensor_manager.h | 7 +++--- ...dler.cpp => sensor_manager_channel_handler.cpp} | 26 +++++++++++----------- ..._handler.h => sensor_manager_channel_handler.h} | 14 ++++++------ src/client/sensor_provider.cpp | 4 ++-- src/client/sensor_provider.h | 6 ++--- ...ler.cpp => sensor_provider_channel_handler.cpp} | 20 ++++++++--------- ...handler.h => sensor_provider_channel_handler.h} | 15 ++++++------- 8 files changed, 48 insertions(+), 48 deletions(-) rename src/client/{sensor_manager_handler.cpp => sensor_manager_channel_handler.cpp} (63%) rename src/client/{sensor_manager_handler.h => sensor_manager_channel_handler.h} (85%) rename src/client/{sensor_provider_handler.cpp => sensor_provider_channel_handler.cpp} (66%) rename src/client/{sensor_provider_handler.h => sensor_provider_channel_handler.h} (83%) diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index b2917b2..ee783f0 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -27,6 +27,8 @@ #include #include +#include "sensor_manager_channel_handler.h" + #define SIZE_STR_SENSOR_ALL 27 using namespace sensor; @@ -170,7 +172,7 @@ bool sensor_manager::init(void) m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) sensor_manager_handler(this); + m_handler = new(std::nothrow) channel_handler(this); if (!m_handler) { delete m_client; m_client = NULL; diff --git a/src/client/sensor_manager.h b/src/client/sensor_manager.h index d0fabf8..a2409b4 100644 --- a/src/client/sensor_manager.h +++ b/src/client/sensor_manager.h @@ -29,7 +29,6 @@ #include "sensor_internal.h" #include "sensor_provider.h" -#include "sensor_manager_handler.h" namespace sensor { @@ -61,7 +60,7 @@ public: void remove_sensor_removed_cb(sensord_removed_cb cb); private: - typedef std::vector sensor_list_t; + class channel_handler; bool init(void); void deinit(void); @@ -80,9 +79,9 @@ private: ipc::channel *m_channel; ipc::event_loop m_loop; std::atomic m_connected; - sensor_manager_handler *m_handler; + channel_handler *m_handler; - sensor_list_t m_sensors; + std::vector m_sensors; }; } diff --git a/src/client/sensor_manager_handler.cpp b/src/client/sensor_manager_channel_handler.cpp similarity index 63% rename from src/client/sensor_manager_handler.cpp rename to src/client/sensor_manager_channel_handler.cpp index 5ba2acb..97ffd7b 100644 --- a/src/client/sensor_manager_handler.cpp +++ b/src/client/sensor_manager_channel_handler.cpp @@ -17,7 +17,7 @@ * */ -#include "sensor_manager_handler.h" +#include "sensor_manager_channel_handler.h" #include #include @@ -25,22 +25,22 @@ using namespace sensor; -sensor_manager_handler::sensor_manager_handler(sensor_manager *manager) +sensor_manager::channel_handler::channel_handler(sensor_manager *manager) : m_manager(manager) { } -void sensor_manager_handler::connected(ipc::channel *ch) +void sensor_manager::channel_handler::connected(ipc::channel *ch) { } -void sensor_manager_handler::disconnected(ipc::channel *ch) +void sensor_manager::channel_handler::disconnected(ipc::channel *ch) { /* If channel->disconnect() is not explicitly called, it will be restored */ m_manager->restore(); } -void sensor_manager_handler::read(ipc::channel *ch, ipc::message &msg) +void sensor_manager::channel_handler::read(ipc::channel *ch, ipc::message &msg) { switch (msg.header()->type) { case CMD_MANAGER_SENSOR_ADDED: @@ -52,15 +52,15 @@ void sensor_manager_handler::read(ipc::channel *ch, ipc::message &msg) } } -void sensor_manager_handler::read_complete(ipc::channel *ch) +void sensor_manager::channel_handler::read_complete(ipc::channel *ch) { } -void sensor_manager_handler::error_caught(ipc::channel *ch, int error) +void sensor_manager::channel_handler::error_caught(ipc::channel *ch, int error) { } -void sensor_manager_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg) +void sensor_manager::channel_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg) { ret_if(msg.header()->err < OP_SUCCESS); @@ -77,7 +77,7 @@ void sensor_manager_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg } } -void sensor_manager_handler::on_sensor_removed(ipc::channel *ch, ipc::message &msg) +void sensor_manager::channel_handler::on_sensor_removed(ipc::channel *ch, ipc::message &msg) { ret_if(msg.header()->err < 0); char uri[NAME_MAX] = {0, }; @@ -92,22 +92,22 @@ void sensor_manager_handler::on_sensor_removed(ipc::channel *ch, ipc::message &m } } -void sensor_manager_handler::add_sensor_added_cb(sensord_added_cb cb, void *user_data) +void sensor_manager::channel_handler::add_sensor_added_cb(sensord_added_cb cb, void *user_data) { m_sensor_added_callbacks.emplace(cb, user_data); } -void sensor_manager_handler::remove_sensor_added_cb(sensord_added_cb cb) +void sensor_manager::channel_handler::remove_sensor_added_cb(sensord_added_cb cb) { m_sensor_added_callbacks.erase(cb); } -void sensor_manager_handler::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data) +void sensor_manager::channel_handler::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data) { m_sensor_removed_callbacks.emplace(cb, user_data); } -void sensor_manager_handler::remove_sensor_removed_cb(sensord_removed_cb cb) +void sensor_manager::channel_handler::remove_sensor_removed_cb(sensord_removed_cb cb) { m_sensor_removed_callbacks.erase(cb); } diff --git a/src/client/sensor_manager_handler.h b/src/client/sensor_manager_channel_handler.h similarity index 85% rename from src/client/sensor_manager_handler.h rename to src/client/sensor_manager_channel_handler.h index 4f1ac73..2bbc4c2 100644 --- a/src/client/sensor_manager_handler.h +++ b/src/client/sensor_manager_channel_handler.h @@ -17,21 +17,21 @@ * */ -#ifndef __SENSOR_MANAGER_HANDLER__ -#define __SENSOR_MANAGER_HANDLER__ +#ifndef __SENSOR_MANAGER_CHANNEL_HANDLER__ +#define __SENSOR_MANAGER_CHANNEL_HANDLER__ #include +#include #include #include namespace sensor { -class sensor_manager; - -class sensor_manager_handler : public ipc::channel_handler +class sensor_manager::channel_handler : public ipc::channel_handler { public: - sensor_manager_handler(sensor_manager *manager); + channel_handler(sensor_manager *manager); + void connected(ipc::channel *ch); void disconnected(ipc::channel *ch); void read(ipc::channel *ch, ipc::message &msg); @@ -58,4 +58,4 @@ private: } -#endif /* __SENSOR_MANAGER_HANDLER__ */ +#endif /* __SENSOR_MANAGER_CHANNEL_HANDLER__ */ diff --git a/src/client/sensor_provider.cpp b/src/client/sensor_provider.cpp index b07fec5..d38049a 100644 --- a/src/client/sensor_provider.cpp +++ b/src/client/sensor_provider.cpp @@ -26,7 +26,7 @@ #include #include -#include "sensor_provider_handler.h" +#include "sensor_provider_channel_handler.h" using namespace sensor; @@ -49,7 +49,7 @@ bool sensor_provider::init(const char *uri) m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) sensor_provider_handler(this); + m_handler = new(std::nothrow) channel_handler(this); if (!m_handler) { delete m_client; return false; diff --git a/src/client/sensor_provider.h b/src/client/sensor_provider.h index 7fc4429..e30e617 100644 --- a/src/client/sensor_provider.h +++ b/src/client/sensor_provider.h @@ -30,8 +30,6 @@ #include #include -#include "sensor_provider_handler.h" - namespace sensor { class sensor_provider { @@ -53,6 +51,8 @@ public: int publish(sensor_data_t *data, int len); private: + class channel_handler; + bool init(const char *uri); void deinit(void); @@ -66,7 +66,7 @@ private: ipc::ipc_client *m_client; ipc::channel *m_channel; ipc::event_loop m_loop; - sensor_provider_handler *m_handler; + channel_handler *m_handler; std::atomic m_connected; }; diff --git a/src/client/sensor_provider_handler.cpp b/src/client/sensor_provider_channel_handler.cpp similarity index 66% rename from src/client/sensor_provider_handler.cpp rename to src/client/sensor_provider_channel_handler.cpp index 14fa3a1..a537546 100644 --- a/src/client/sensor_provider_handler.cpp +++ b/src/client/sensor_provider_channel_handler.cpp @@ -17,7 +17,7 @@ * */ -#include "sensor_provider_handler.h" +#include "sensor_provider_channel_handler.h" #include #include @@ -25,7 +25,7 @@ using namespace sensor; -sensor_provider_handler::sensor_provider_handler(sensor_provider *provider) +sensor_provider::channel_handler::channel_handler(sensor_provider *provider) : m_provider(provider) , m_start_cb(NULL) , m_stop_cb(NULL) @@ -36,18 +36,18 @@ sensor_provider_handler::sensor_provider_handler(sensor_provider *provider) { } -void sensor_provider_handler::connected(ipc::channel *ch) +void sensor_provider::channel_handler::connected(ipc::channel *ch) { _I("Connected"); } -void sensor_provider_handler::disconnected(ipc::channel *ch) +void sensor_provider::channel_handler::disconnected(ipc::channel *ch) { /* TODO */ /* m_provider->restore(); */ } -void sensor_provider_handler::read(ipc::channel *ch, ipc::message &msg) +void sensor_provider::channel_handler::read(ipc::channel *ch, ipc::message &msg) { switch (msg.type()) { case CMD_PROVIDER_START: @@ -66,27 +66,27 @@ void sensor_provider_handler::read(ipc::channel *ch, ipc::message &msg) } } -void sensor_provider_handler::read_complete(ipc::channel *ch) +void sensor_provider::channel_handler::read_complete(ipc::channel *ch) { } -void sensor_provider_handler::error_caught(ipc::channel *ch, int error) +void sensor_provider::channel_handler::error_caught(ipc::channel *ch, int error) { } -void sensor_provider_handler::set_start_cb(sensord_provider_start_cb cb, void *user_data) +void sensor_provider::channel_handler::set_start_cb(sensord_provider_start_cb cb, void *user_data) { m_start_cb = cb; m_start_user_data = user_data; } -void sensor_provider_handler::set_stop_cb(sensord_provider_stop_cb cb, void *user_data) +void sensor_provider::channel_handler::set_stop_cb(sensord_provider_stop_cb cb, void *user_data) { m_stop_cb = cb; m_stop_user_data = user_data; } -void sensor_provider_handler::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data) +void sensor_provider::channel_handler::set_interval_cb(sensord_provider_set_interval_cb cb, void *user_data) { m_set_interval_cb = cb; m_set_interval_user_data = user_data; diff --git a/src/client/sensor_provider_handler.h b/src/client/sensor_provider_channel_handler.h similarity index 83% rename from src/client/sensor_provider_handler.h rename to src/client/sensor_provider_channel_handler.h index 16bf479..efe5ce5 100644 --- a/src/client/sensor_provider_handler.h +++ b/src/client/sensor_provider_channel_handler.h @@ -17,20 +17,19 @@ * */ -#ifndef __SENSOR_PROVIDER_HANDLER__ -#define __SENSOR_PROVIDER_HANDLER__ +#ifndef __SENSOR_PROVIDER_CHANNEL_HANDLER__ +#define __SENSOR_PROVIDER_CHANNEL_HANDLER__ -#include #include +#include +#include "sensor_provider.h" namespace sensor { -class sensor_provider; - -class sensor_provider_handler : public ipc::channel_handler +class sensor_provider::channel_handler : public ipc::channel_handler { public: - sensor_provider_handler(sensor_provider *provider); + channel_handler(sensor_provider *provider); void connected(ipc::channel *ch); void disconnected(ipc::channel *ch); @@ -57,4 +56,4 @@ private: } -#endif /* __SENSOR_PROVIDER_HANDLER__ */ +#endif /* __SENSOR_PROVIDER_CHANNEL_HANDLER__ */ -- 2.7.4 From 6a16921a7f899af99fb8034e8e4373cc0e69437f Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 26 Apr 2017 16:21:57 +0900 Subject: [PATCH 15/16] sensord: change return type from bool to int for checking errors Change-Id: I0b232061df9ed8dd0a4297713e887c7f345a7de4 Signed-off-by: kibak.yoon --- src/client/sensor_internal.cpp | 6 +++--- src/client/sensor_provider.cpp | 6 +++--- src/client/sensor_provider.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index 1a5b0b7..31af7d6 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -617,13 +617,13 @@ API int sensord_add_provider(sensord_provider_h provider) int ret; sensor_provider *p = static_cast(provider); - if (!p->connect()) - return OP_ERROR; + ret = p->connect(); + retv_if(ret < 0, ret); ret = manager.add_sensor(p); if (ret < 0) { p->disconnect(); - return OP_ERROR; + return ret; } return OP_SUCCESS; diff --git a/src/client/sensor_provider.cpp b/src/client/sensor_provider.cpp index d38049a..abfd9e1 100644 --- a/src/client/sensor_provider.cpp +++ b/src/client/sensor_provider.cpp @@ -119,10 +119,10 @@ int sensor_provider::send_sensor_info(sensor_info *info) return OP_SUCCESS; } -bool sensor_provider::connect(void) +int sensor_provider::connect(void) { m_channel = m_client->connect(m_handler, &m_loop); - retvm_if(!m_channel, false, "Failed to connect to server"); + retvm_if(!m_channel, -EIO, "Failed to connect to server"); /* serialize and send sensor info */ send_sensor_info(get_sensor_info()); @@ -136,7 +136,7 @@ bool sensor_provider::connect(void) _I("Provider URI[%s]", get_uri()); - return true; + return OP_SUCCESS; } bool sensor_provider::disconnect(void) diff --git a/src/client/sensor_provider.h b/src/client/sensor_provider.h index e30e617..b499995 100644 --- a/src/client/sensor_provider.h +++ b/src/client/sensor_provider.h @@ -40,7 +40,7 @@ public: const char *get_uri(void); sensor_info *get_sensor_info(void); - bool connect(void); + int connect(void); bool disconnect(void); void restore(void); -- 2.7.4 From 572ea8b3b6cf756c22a980e297c8412ae62385dd Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 26 Apr 2017 19:54:08 +0900 Subject: [PATCH 16/16] sensord: add NULL check when callbacks is called Change-Id: Ib2937c0e7ba4656c246b29a3addb1163d4494ef5 Signed-off-by: kibak.yoon --- src/client/sensor_provider_channel_handler.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/client/sensor_provider_channel_handler.cpp b/src/client/sensor_provider_channel_handler.cpp index a537546..52cc6bf 100644 --- a/src/client/sensor_provider_channel_handler.cpp +++ b/src/client/sensor_provider_channel_handler.cpp @@ -51,16 +51,18 @@ void sensor_provider::channel_handler::read(ipc::channel *ch, ipc::message &msg) { switch (msg.type()) { case CMD_PROVIDER_START: - m_start_cb(m_provider, m_start_user_data); + if (m_start_cb) + m_start_cb(m_provider, m_start_user_data); break; case CMD_PROVIDER_STOP: - m_stop_cb(m_provider, m_stop_user_data); + if (m_stop_cb) + m_stop_cb(m_provider, m_stop_user_data); break; case CMD_PROVIDER_ATTR_INT: cmd_provider_attr_int_t buf; msg.disclose((char *)&buf); - if (buf.attribute == SENSORD_ATTRIBUTE_INTERVAL) + if (buf.attribute == SENSORD_ATTRIBUTE_INTERVAL && m_set_interval_cb) m_set_interval_cb(m_provider, buf.value, m_set_interval_user_data); break; } -- 2.7.4