From 31c592d8d53d9b4bb450d61694a64bc722e3593d Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 18:45:57 +0900 Subject: [PATCH 01/16] sensord: add fusion_sensor_handler class - fusion_sensor_handler provides: - sensor information - controls for operating required sensors - controls for operation policy Change-Id: Id1f6d7d6dce67611a00c5f9e2880f4d2178cb145 Signed-off-by: kibak.yoon --- src/server/fusion_sensor_handler.cpp | 292 +++++++++++++++++++++++++++++++++++ src/server/fusion_sensor_handler.h | 79 ++++++++++ 2 files changed, 371 insertions(+) create mode 100644 src/server/fusion_sensor_handler.cpp create mode 100644 src/server/fusion_sensor_handler.h diff --git a/src/server/fusion_sensor_handler.cpp b/src/server/fusion_sensor_handler.cpp new file mode 100644 index 0000000..44c794a --- /dev/null +++ b/src/server/fusion_sensor_handler.cpp @@ -0,0 +1,292 @@ +/* + * 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 "fusion_sensor_handler.h" + +#include +#include +#include + +using namespace sensor; + +fusion_sensor_handler::fusion_sensor_handler(const sensor_info &info, + fusion_sensor *sensor) +: m_info(info) +, m_sensor(sensor) +, m_policy(OP_DEFAULT) +{ +} + +fusion_sensor_handler::~fusion_sensor_handler() +{ +} + +void fusion_sensor_handler::add_required_sensor(sensor_handler *sensor) +{ + m_required_sensors.push_back(sensor); +} + +int fusion_sensor_handler::update(const char *uri, ipc::message *msg) +{ + retv_if(!m_sensor, -EINVAL); + + if (m_sensor->update(uri, (sensor_data_t *)msg->body(), msg->size()) < 0) + return OP_SUCCESS; + + sensor_data_t *data; + int len; + + if (m_sensor->get_data(&data, &len) < 0) + return OP_ERROR; + + std::string fsensor_uri = m_info.get_type_uri(); + + return notify(fsensor_uri.c_str(), data, len); +} + +const sensor_info &fusion_sensor_handler::get_sensor_info(void) +{ + return m_info; +} + +int fusion_sensor_handler::start(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = OP_DEFAULT; + + policy = m_sensor->start(ob); + retv_if(policy <= OP_ERROR, policy); + + add_observer(ob); + + if (policy == OP_DEFAULT) { + if (observer_count() > 1) + return OP_SUCCESS; + } + + return start_internal(); +} + +int fusion_sensor_handler::stop(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = OP_DEFAULT; + + policy = m_sensor->stop(ob); + retv_if(policy <= OP_ERROR, policy); + + remove_observer(ob); + + if (policy == OP_DEFAULT) { + if (observer_count() >= 1) + return OP_SUCCESS; /* already started */ + } + + return stop_internal(); +} + +int fusion_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 fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) +{ + retv_if(!m_sensor, -EINVAL); + + int _interval = interval; + int policy = OP_DEFAULT; + + policy = m_sensor->set_interval(ob, _interval); + retv_if(policy <= OP_ERROR, policy); + + m_interval_map[ob] = interval; + + if (policy == OP_DEFAULT) + _interval = get_min_interval(); + + return set_interval_internal(_interval); +} + +int fusion_sensor_handler::get_min_batch_latency(void) +{ + int batch_latency; + std::vector temp; + + for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it) + if (it->second > 0) + temp.push_back(it->second); + + if (temp.empty()) + return 0; + + batch_latency = *std::min_element(temp.begin(), temp.end()); + + return batch_latency; +} + +int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency) +{ + retv_if(!m_sensor, -EINVAL); + + int _latency = latency; + int policy = OP_DEFAULT; + + if (m_sensor) { + policy = m_sensor->set_batch_latency(ob, _latency); + retv_if(policy <= OP_ERROR, policy); + } + + m_batch_latency_map[ob] = _latency; + + if (policy == OP_DEFAULT) + _latency = get_min_batch_latency(); + + return set_batch_latency_internal(_latency); +} + +int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = OP_DEFAULT; + + policy = m_sensor->set_attribute(ob, attr, value); + retv_if(policy <= OP_ERROR, policy); + + if (m_policy == OP_DEFAULT) { + /* default logic */ + } + + return set_attribute_internal(attr, value); +} + +int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = OP_DEFAULT; + + policy = m_sensor->set_attribute(ob, attr, value, len); + retv_if(policy <= OP_ERROR, policy); + + if (m_policy == OP_DEFAULT) { + /* default logic */ + } + + return set_attribute_internal(attr, value, len); +} + +int fusion_sensor_handler::get_data(sensor_data_t **data, int *len) +{ + /* TODO */ + return OP_SUCCESS; +} + +int fusion_sensor_handler::flush(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + m_sensor->flush(this); + + return OP_SUCCESS; +} + +int fusion_sensor_handler::start_internal(void) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->start(this) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} + +int fusion_sensor_handler::stop_internal(void) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->stop(this) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} + +int fusion_sensor_handler::set_interval_internal(int32_t interval) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->set_interval(this, interval) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} + +int fusion_sensor_handler::set_batch_latency_internal(int32_t latency) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->set_batch_latency(this, latency) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} + +int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->set_attribute(this, attr, value) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} + +int fusion_sensor_handler::set_attribute_internal(int32_t attr, const char *value, int len) +{ + int size = m_required_sensors.size(); + for (int i = 0; i < size; ++i) { + if (m_required_sensors[i]->set_attribute(this, attr, value, len) < 0) + return OP_ERROR; + } + + return OP_SUCCESS; +} diff --git a/src/server/fusion_sensor_handler.h b/src/server/fusion_sensor_handler.h new file mode 100644 index 0000000..d16278f --- /dev/null +++ b/src/server/fusion_sensor_handler.h @@ -0,0 +1,79 @@ +/* + * 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 __FUSION_SENSOR_HANDLER_H__ +#define __FUSION_SENSOR_HANDLER_H__ + +#include +#include +#include +#include + +#include "sensor_handler.h" +#include "sensor_observer.h" + +namespace sensor { + +class fusion_sensor_handler : public sensor_handler, public sensor_observer { +public: + fusion_sensor_handler(const sensor_info &info, + fusion_sensor *sensor); + ~fusion_sensor_handler(); + + void add_required_sensor(sensor_handler *sensor); + + /* subscriber */ + int update(const char *uri, ipc::message *msg); + + /* sensor interface */ + const sensor_info &get_sensor_info(void); + + int start(sensor_observer *ob); + int stop(sensor_observer *ob); + + int set_interval(sensor_observer *ob, int32_t interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); + int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); + int flush(sensor_observer *ob); + int get_data(sensor_data_t **data, int *len); + +private: + int start_internal(void); + int stop_internal(void); + int set_interval_internal(int32_t interval); + int set_batch_latency_internal(int32_t latency); + int set_attribute_internal(int32_t attr, int32_t value); + int set_attribute_internal(int32_t attr, const char *value, int len); + + int get_min_interval(void); + int get_min_batch_latency(void); + + sensor_info m_info; + fusion_sensor *m_sensor; + std::vector m_required_sensors; + + std::unordered_map m_interval_map; + std::unordered_map m_batch_latency_map; + int m_policy; +}; + +} + +#endif /* __FUSION_SENSOR_HANDLER_H__ */ -- 2.7.4 From 551f945381ec727b27f23b77787635d9670d227b Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 18:47:29 +0900 Subject: [PATCH 02/16] sensord: add external_sensor_handler class - external_sensor_handler provides: - controls for operating external_sensor - controls for operation policy - listening event from notifier and notify it to observers Change-Id: I3213c7798873833b8bb5e0307275fd2a3bd52bf6 Signed-off-by: kibak.yoon --- src/server/external_sensor_handler.cpp | 236 +++++++++++++++++++++++++++++++++ src/server/external_sensor_handler.h | 69 ++++++++++ 2 files changed, 305 insertions(+) create mode 100644 src/server/external_sensor_handler.cpp create mode 100644 src/server/external_sensor_handler.h diff --git a/src/server/external_sensor_handler.cpp b/src/server/external_sensor_handler.cpp new file mode 100644 index 0000000..110e15b --- /dev/null +++ b/src/server/external_sensor_handler.cpp @@ -0,0 +1,236 @@ +/* + * 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 "external_sensor_handler.h" + +#include +#include +#include +#include + +using namespace sensor; + +class external_sensor_notifier : public sensor_notifier { +public: + external_sensor_notifier(external_sensor_handler *sensor); + + int notify(void); + +private: + external_sensor_handler *m_sensor; +}; + +external_sensor_notifier::external_sensor_notifier(external_sensor_handler *sensor) +: m_sensor(sensor) +{ +} + +int external_sensor_notifier::notify(void) +{ + /* TODO: Change thread-safe function(add handler to event-loop) */ + sensor_data_t *data; + int len; + + if (m_sensor->get_data(&data, &len) < 0) + return OP_ERROR; + + /* TODO: pointer would be better */ + sensor_info info = m_sensor->get_sensor_info(); + std::string uri = info.get_type_uri(); + + return m_sensor->notify(uri.c_str(), data, len); +} + +external_sensor_handler::external_sensor_handler(const sensor_info &info, + external_sensor *sensor) +: m_info(info) +, m_sensor(sensor) +, m_notifier(NULL) +{ + init(); +} + +external_sensor_handler::~external_sensor_handler() +{ + deinit(); +} + +bool external_sensor_handler::init(void) +{ + m_notifier = new(std::nothrow) external_sensor_notifier(this); + retvm_if(!m_notifier, false, "Failed to allocate memory"); + + m_sensor->set_notifier(m_notifier); + return true; +} + +void external_sensor_handler::deinit(void) +{ + delete m_notifier; + m_notifier = NULL; +} + +const sensor_info &external_sensor_handler::get_sensor_info(void) +{ + return m_info; +} + +int external_sensor_handler::start(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = m_sensor->start(ob); + retv_if(policy <= OP_ERROR, policy); + + return OP_SUCCESS; +} + +int external_sensor_handler::stop(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + int policy = m_sensor->stop(ob); + retv_if(policy <= OP_ERROR, policy); + + remove_observer(ob); + + return OP_SUCCESS; +} + +int external_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 external_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) +{ + retv_if(!m_sensor, -EINVAL); + + int _interval = interval; + + if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) { + m_policy = m_sensor->set_interval(ob, interval); + retv_if(m_policy <= OP_ERROR, m_policy); + } + + m_interval_map[ob] = interval; + + if (m_policy == OP_DEFAULT && observer_count() > 0) { + _interval = get_min_interval(); + return m_sensor->set_interval(ob, _interval); + } + + return OP_SUCCESS; +} + +int external_sensor_handler::get_min_batch_latency(void) +{ + int batch_latency; + std::vector temp; + + for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it) + if (it->second > 0) + temp.push_back(it->second); + + if (temp.empty()) + return 0; + + batch_latency = *std::min_element(temp.begin(), temp.end()); + + return batch_latency; +} + +int external_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency) +{ + retv_if(!m_sensor, -EINVAL); + + int _latency = latency; + + if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) { + m_policy = m_sensor->set_batch_latency(ob, latency); + retv_if(m_policy <= OP_ERROR, m_policy); + } + + m_batch_latency_map[ob] = _latency; + + if (m_policy == OP_DEFAULT && observer_count() > 0) { + _latency = get_min_batch_latency(); + return m_sensor->set_batch_latency(ob, latency); + } + return OP_SUCCESS; +} + +int external_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value) +{ + retv_if(!m_sensor, -EINVAL); + + if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) { + m_policy = m_sensor->set_attribute(ob, attr, value); + retv_if(m_policy <= OP_ERROR, m_policy); + } + + if (m_policy == OP_DEFAULT) { + /* default logic */ + } + return OP_SUCCESS; +} + +int external_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len) +{ + retv_if(!m_sensor, -EINVAL); + + if ((m_policy == OP_DEFAULT && observer_count() == 0) || m_policy == OP_SUCCESS) { + m_policy = m_sensor->set_attribute(ob, attr, value, len); + retv_if(m_policy <= OP_ERROR, m_policy); + } + + if (m_policy == OP_DEFAULT) { + /* default logic */ + } + return OP_SUCCESS; +} + +int external_sensor_handler::get_data(sensor_data_t **data, int *len) +{ + return m_sensor->get_data(data, len); +} + +int external_sensor_handler::flush(sensor_observer *ob) +{ + retv_if(!m_sensor, -EINVAL); + + m_sensor->flush(this); + + return OP_SUCCESS; +} diff --git a/src/server/external_sensor_handler.h b/src/server/external_sensor_handler.h new file mode 100644 index 0000000..6fe386a --- /dev/null +++ b/src/server/external_sensor_handler.h @@ -0,0 +1,69 @@ +/* + * 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 __EXTERNAL_SENSOR_HANDLER_H__ +#define __EXTERNAL_SENSOR_HANDLER_H__ + +#include +#include +#include +#include + +#include "sensor_handler.h" + +namespace sensor { + +class external_sensor_handler : public sensor_handler { +public: + external_sensor_handler(const sensor_info &info, + external_sensor *sensor); + ~external_sensor_handler(); + + /* sensor interface */ + const sensor_info &get_sensor_info(void); + + int start(sensor_observer *ob); + int stop(sensor_observer *ob); + + int set_interval(sensor_observer *ob, int32_t interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); + int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); + int flush(sensor_observer *ob); + int get_data(sensor_data_t **data, int *len); + +private: + bool init(); + void deinit(); + + int get_min_interval(void); + int get_min_batch_latency(void); + + sensor_info m_info; + external_sensor *m_sensor; + sensor_notifier *m_notifier; + int m_policy; + + std::unordered_map m_interval_map; + std::unordered_map m_batch_latency_map; +}; + +} + +#endif /* __EXTERNAL_SENSOR_HANDLER_H__ */ -- 2.7.4 From 33a86529ceced3ba0b6d69e12523d5dd1fdd0c1a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 18:49:55 +0900 Subject: [PATCH 03/16] sensord: add application_sensor_handler skeleton class - application_sensor_handler will be created by application - application_sensor_handler provides: - events from application to observers(listeners) Change-Id: Ic33e1c164ddfc98766eec6e82b2b537ccf4c5a6a Signed-off-by: kibak.yoon --- src/server/application_sensor_handler.cpp | 90 +++++++++++++++++++++++++++++++ src/server/application_sensor_handler.h | 60 +++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/server/application_sensor_handler.cpp create mode 100644 src/server/application_sensor_handler.h diff --git a/src/server/application_sensor_handler.cpp b/src/server/application_sensor_handler.cpp new file mode 100644 index 0000000..48e50be --- /dev/null +++ b/src/server/application_sensor_handler.cpp @@ -0,0 +1,90 @@ +/* + * 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 "application_sensor_handler.h" + +#include +#include + +using namespace sensor; + +application_sensor_handler::application_sensor_handler(const sensor_info &info) +: m_info(info) +{ +} + +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(); + + return notify(uri.c_str(), data, len); +} + +const sensor_info &application_sensor_handler::get_sensor_info(void) +{ + return m_info; +} + +int application_sensor_handler::start(sensor_observer *ob) +{ + add_observer(ob); + + return OP_SUCCESS; +} + +int application_sensor_handler::stop(sensor_observer *ob) +{ + remove_observer(ob); + + return OP_SUCCESS; +} + +int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) +{ + return OP_SUCCESS; +} + +int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency) +{ + return OP_SUCCESS; +} + +int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value) +{ + return OP_SUCCESS; +} + +int application_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len) +{ + return OP_SUCCESS; +} + +int application_sensor_handler::flush(sensor_observer *ob) +{ + return OP_SUCCESS; +} + +int application_sensor_handler::get_data(sensor_data_t **data, int *len) +{ + return OP_SUCCESS; +} diff --git a/src/server/application_sensor_handler.h b/src/server/application_sensor_handler.h new file mode 100644 index 0000000..87d07fd --- /dev/null +++ b/src/server/application_sensor_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 __APPLICATION_SENSOR_HANDLER_H__ +#define __APPLICATION_SENSOR_HANDLER_H__ + +#include +#include +#include + +#include "sensor_handler.h" +#include "sensor_observer.h" + +namespace sensor { + +class application_sensor_handler : public sensor_handler { +public: + application_sensor_handler(const sensor_info &info); + ~application_sensor_handler(); + + /* TODO: const */ + int post(sensor_data_t *data, int len); + + /* sensor interface */ + const sensor_info &get_sensor_info(void); + + int start(sensor_observer *ob); + int stop(sensor_observer *ob); + + int set_interval(sensor_observer *ob, int32_t interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); + int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); + int flush(sensor_observer *ob); + int get_data(sensor_data_t **data, int *len); + +private: + sensor_info m_info; + std::vector m_required_sensors; +}; + +} + +#endif /* __APPLICATION_SENSOR_HANDLER_H__ */ -- 2.7.4 From 9041f160c57acacf7445e4008ef3a4337c2ef305 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 18:52:33 +0900 Subject: [PATCH 04/16] sensord: add sensor_listener_proxy class - sensor_listener_proxy is a proxy of client's listener - it listens sensor event as observer and controls sensors Change-Id: I40b398f0701e7940a9893f54b896568debb3ed36 Signed-off-by: kibak.yoon --- src/server/sensor_listener_proxy.cpp | 129 +++++++++++++++++++++++++++++++++++ src/server/sensor_listener_proxy.h | 65 ++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 src/server/sensor_listener_proxy.cpp create mode 100644 src/server/sensor_listener_proxy.h diff --git a/src/server/sensor_listener_proxy.cpp b/src/server/sensor_listener_proxy.cpp new file mode 100644 index 0000000..29608b0 --- /dev/null +++ b/src/server/sensor_listener_proxy.cpp @@ -0,0 +1,129 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "sensor_listener_proxy.h" + +#include +#include +#include +#include +#include + +using namespace sensor; + +sensor_listener_proxy::sensor_listener_proxy( + uint32_t id, sensor_handler *sensor, ipc::channel *ch) +: m_id(id) +, m_sensor(sensor) +, m_ch(ch) +, m_passive(false) +, m_pause_policy(SENSORD_PAUSE_ALL) +, m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED) +{ +} + +sensor_listener_proxy::~sensor_listener_proxy() +{ + stop(); +} + +uint32_t sensor_listener_proxy::get_id(void) +{ + return m_id; +} + +int sensor_listener_proxy::update(const char *uri, ipc::message *msg) +{ + retv_if(!m_ch || !m_ch->is_connected(), OP_CONTINUE); + + /* TODO: check axis orientation */ + msg->header()->type = CMD_LISTENER_EVENT; + msg->header()->err = OP_SUCCESS; + + m_ch->send(msg); + + return OP_CONTINUE; +} + +int sensor_listener_proxy::start(void) +{ + /* TODO: listen pause policy */ + return m_sensor->start(this); +} + +int sensor_listener_proxy::stop(void) +{ + /* TODO: listen pause policy */ + int ret; + + ret = m_sensor->stop(this); + retv_if(ret < 0, OP_ERROR); + + /* unset attributes */ + set_interval(POLL_1HZ_MS); + set_max_batch_latency(0); + + return OP_SUCCESS; +} + +int sensor_listener_proxy::set_interval(unsigned int interval) +{ + return m_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); +} + +int sensor_listener_proxy::set_passive_mode(bool passive) +{ + /* TODO: passive mode */ + m_passive = passive; + return OP_SUCCESS; +} + +int sensor_listener_proxy::set_attribute(int attribute, int value) +{ + if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) { + m_pause_policy = value; + return OP_SUCCESS; + } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) { + m_axis_orientation = value; + return OP_SUCCESS; + } + + return m_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); +} + +int sensor_listener_proxy::flush(void) +{ + return m_sensor->flush(this); +} + +int sensor_listener_proxy::get_data(sensor_data_t **data, int *len) +{ + /* TODO : caching the last data & retry logic if there is no data */ + return m_sensor->get_data(data, len); +} diff --git a/src/server/sensor_listener_proxy.h b/src/server/sensor_listener_proxy.h new file mode 100644 index 0000000..709f057 --- /dev/null +++ b/src/server/sensor_listener_proxy.h @@ -0,0 +1,65 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __SENSOR_LISTENER_PROXY_H__ +#define __SENSOR_LISTENER_PROXY_H__ + +#include +#include + +#include "sensor_handler.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 get_id(void); + + /* sensor observer */ + int update(const char *uri, ipc::message *msg); + + int start(void); + int stop(void); + + int set_interval(unsigned int interval); + int set_max_batch_latency(unsigned int max_batch_latency); + int set_passive_mode(bool passive); + int set_attribute(int attribute, int value); + int set_attribute(int attribute, const char *value, int len); + int flush(void); + int get_data(sensor_data_t **data, int *len); + +private: + uint32_t m_id; + + sensor_handler *m_sensor; + ipc::channel *m_ch; + + bool m_passive; + int m_pause_policy; + int m_axis_orientation; +}; + +} + +#endif /* __SENSOR_LISTENER_PROXY_H__ */ -- 2.7.4 From 0b6a81e4e5441f6487f35a274282ae569356bc2d Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 19:04:16 +0900 Subject: [PATCH 05/16] sensord: pass observer handle to flush() Change-Id: I4c95531c5461324c41c1ac5eb63cfb4ed00dea99 Signed-off-by: kibak.yoon --- src/server/sensor_handler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/sensor_handler.h b/src/server/sensor_handler.h index 68672ab..a5e6fd5 100644 --- a/src/server/sensor_handler.h +++ b/src/server/sensor_handler.h @@ -48,8 +48,8 @@ public: virtual int set_batch_latency(sensor_observer *ob, int32_t latency) = 0; virtual int set_attribute(sensor_observer *ob, int32_t attr, int32_t value) = 0; virtual int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len) = 0; + virtual int flush(sensor_observer *ob) = 0; virtual int get_data(sensor_data_t **data, int *len) = 0; - virtual int flush(void) = 0; private: std::list m_observers; -- 2.7.4 From d1c961c50f1dd18eb24072ff3f2f49b5ca84fc54 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 18:55:11 +0900 Subject: [PATCH 06/16] sensord: add sensor_manager to create and manage sensor_handlers - add sensor_event_handler which is to listen events from physical sensor Change-Id: I6854ee874c52c003e4e1daa37a57974ee4b434e4 Signed-off-by: kibak.yoon --- src/server/sensor_event_handler.cpp | 72 +++++++++ src/server/sensor_event_handler.h | 41 ++++++ src/server/sensor_manager.cpp | 283 ++++++++++++++++++++++++++++++++++++ src/server/sensor_manager.h | 31 ++++ 4 files changed, 427 insertions(+) create mode 100644 src/server/sensor_event_handler.cpp create mode 100644 src/server/sensor_event_handler.h diff --git a/src/server/sensor_event_handler.cpp b/src/server/sensor_event_handler.cpp new file mode 100644 index 0000000..de13006 --- /dev/null +++ b/src/server/sensor_event_handler.cpp @@ -0,0 +1,72 @@ +/* + * 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_event_handler.h" + +#include +#include +#include + +using namespace sensor; + +static std::vector ids; + +sensor_event_handler::sensor_event_handler(physical_sensor_handler *sensor) +: m_sensor(sensor) +{ +} + +bool sensor_event_handler::handle(int fd, ipc::event_condition condition) +{ + sensor_info info; + sensor_data_t *data; + int length = 0; + int remains = 1; + + if (m_sensor->read_fd(ids) < 0) + return true; + + auto result = std::find(std::begin(ids), std::end(ids), m_sensor->get_hal_id()); + + if (result == std::end(ids)) + return true; + + while (remains > 0) { + remains = m_sensor->get_data(&data, &length); + if (remains < 0) { + _E("Failed to get sensor data"); + break; + } + + if (m_sensor->on_event(data, length, remains) < 0) { + free(data); + continue; + } + + info = m_sensor->get_sensor_info(); + + //_I("[Data] allocate %p", data); + if (m_sensor->notify(info.get_type_uri().c_str(), data, length) < 0) { + free(data); + } + info.clear(); + } + + return true; +} diff --git a/src/server/sensor_event_handler.h b/src/server/sensor_event_handler.h new file mode 100644 index 0000000..53b5cd6 --- /dev/null +++ b/src/server/sensor_event_handler.h @@ -0,0 +1,41 @@ +/* + * 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_EVENT_HANDLER__ +#define __SENSOR_EVENT_HANDLER__ + +#include +#include "physical_sensor_handler.h" + +namespace sensor { + +class sensor_event_handler : public ipc::event_handler +{ +public: + sensor_event_handler(physical_sensor_handler *sensor); + + bool handle(int fd, ipc::event_condition condition); + +private: + physical_sensor_handler *m_sensor; +}; + +} + +#endif /* __SENSOR_EVENT_DISPATCHER__ */ diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index 6944e86..7a0ccc9 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -26,8 +26,26 @@ #include #include +#include "sensor_event_handler.h" +#include "permission_checker.h" +#include "sensor_loader.h" +#include "physical_sensor_handler.h" +#include "fusion_sensor_handler.h" +#include "external_sensor_handler.h" +#include "fusion_sensor_handler.h" + using namespace sensor; +#define DEVICE_HAL_DIR_PATH LIBDIR "/sensor/hal" +#define PHYSICAL_SENSOR_DIR_PATH LIBDIR "/sensor/physical" +#define VIRTUAL_SENSOR_DIR_PATH LIBDIR "/sensor/fusion" +#define EXTERNAL_SENSOR_DIR_PATH LIBDIR "/sensor/external" + +static device_sensor_registry_t devices; +static physical_sensor_registry_t physical_sensors; +static fusion_sensor_registry_t fusion_sensors; +static external_sensor_registry_t external_sensors; + sensor_manager::sensor_manager(ipc::event_loop *loop) : m_loop(loop) { @@ -39,11 +57,276 @@ sensor_manager::~sensor_manager() bool sensor_manager::init(void) { + m_loader.load_hal(DEVICE_HAL_DIR_PATH, devices); + m_loader.load_physical_sensor(PHYSICAL_SENSOR_DIR_PATH, physical_sensors); + m_loader.load_fusion_sensor(VIRTUAL_SENSOR_DIR_PATH, fusion_sensors); + m_loader.load_external_sensor(EXTERNAL_SENSOR_DIR_PATH, external_sensors); + + retvm_if(devices.empty(), false, "There is no sensors"); + + /* TODO: support dynamic sensor */ + create_physical_sensors(devices, physical_sensors); + create_fusion_sensors(fusion_sensors); + create_external_sensors(external_sensors); + + init_sensors(); + + show(); + return true; } bool sensor_manager::deinit(void) { + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) + delete it->second; + m_sensors.clear(); + + external_sensors.clear(); + fusion_sensors.clear(); + physical_sensors.clear(); + devices.clear(); + + m_loader.unload(); + + return true; +} + +bool sensor_manager::is_supported(std::string uri) +{ + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { + sensor_info info = it->second->get_sensor_info(); + + if (info.get_uri() == uri) + return true; + } + + return false; +} + +bool sensor_manager::register_sensor(sensor_handler *sensor) +{ + retvm_if(!sensor, false, "Invalid sensor"); + + sensor_info info = sensor->get_sensor_info(); + + auto it = m_sensors.find(info.get_uri()); + retvm_if(it != m_sensors.end(), false, "There is already a sensor with the same name"); + + m_sensors[info.get_uri()] = sensor; + + _I("Registered[%s]", info.get_uri().c_str()); + return true; } +void sensor_manager::deregister_sensor(const std::string uri) +{ + auto it = m_sensors.find(uri); + ret_if(it == m_sensors.end()); + + delete it->second; + m_sensors.erase(it); + + _I("Deregistered[%s]", uri.c_str()); +} + +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) + return it->second; + } + + return NULL; +} + +sensor_handler *sensor_manager::get_sensor(const std::string uri) +{ + auto it = m_sensors.find(uri); + retv_if(it == m_sensors.end(), NULL); + + return m_sensors[uri]; +} + +std::vector sensor_manager::get_sensors(void) +{ + std::vector sensors; + + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) + sensors.push_back(it->second); + + return sensors; +} + +void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices, + physical_sensor_registry_t &psensors) +{ + const sensor_info_t *info; + physical_sensor_handler *psensor; + + for (auto it = devices.begin(); it != devices.end(); ++it) { + int count = (*it)->get_sensors(&info); + + for (int i = 0; i < count; ++i) { + /* TODO: psensors */ + psensor = new(std::nothrow) physical_sensor_handler( + info[i], it->get(), info[i].id, NULL); + retm_if(!psensor, "Failed to allocate memory"); + + sensor_info sinfo = psensor->get_sensor_info(); + m_sensors[sinfo.get_uri()] = psensor; + } + } + +} + +void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors) +{ + const sensor_info2_t *info; + fusion_sensor_handler *fsensor; + std::vector req_names; + + for (auto it = fsensors.begin(); it != fsensors.end(); ++it) { + int count = (*it)->get_sensors(&info); + + for (int i = 0; i < count; ++i) { + bool support = true; + req_names.clear(); + + fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get()); + retm_if(!fsensor, "Failed to allocate memory"); + + (*it)->get_required_sensors(req_names); + for (unsigned int j = 0; j < req_names.size(); ++j) { + sensor_handler *sensor; + sensor = get_sensor_by_type(req_names[j]); + + if (sensor == NULL) { + support = false; + break; + } + + fsensor->add_required_sensor(sensor); + } + + if (!support) { + delete fsensor; + /* TODO: remove plugin */ + continue; + } + + sensor_info sinfo = fsensor->get_sensor_info(); + m_sensors[sinfo.get_uri()] = fsensor; + } + } +} + +void sensor_manager::create_external_sensors(external_sensor_registry_t &esensors) +{ + const sensor_info2_t *info; + external_sensor_handler *esensor; + + for (auto it = esensors.begin(); it != esensors.end(); ++it) { + int count = (*it)->get_sensors(&info); + + for (int i = 0; i < count; ++i) { + esensor = new(std::nothrow) external_sensor_handler(info[i], it->get()); + retm_if(!esensor, "Failed to allocate memory"); + + sensor_info sinfo = esensor->get_sensor_info(); + m_sensors[sinfo.get_uri()] = esensor; + } + } +} + +static void put_int_to_vec(std::vector &data, int value) +{ + char buf[sizeof(value)]; + + int *temp = (int *)buf; + *temp = value; + + std::copy(&buf[0], &buf[sizeof(buf)], back_inserter(data)); +} + +/* packet format : + * [count:4] {[size:4] [info:n] [size:4] [info:n] ...} + */ +size_t sensor_manager::serialize(int sock_fd, char **bytes) +{ + static permission_checker checker; + + sensor_info info; + std::vector raw_list; + + put_int_to_vec(raw_list, m_sensors.size()); + + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { + info = it->second->get_sensor_info(); + + if (!checker.has_permission(sock_fd, info.get_permission())) + info.set_uri(SENSOR_URI_PERMISSION_DENIED); + + raw_data_t *raw = new(std::nothrow) raw_data_t(); + retvm_if(!raw, -ENOMEM, "Failed to allocated memory"); + + info.serialize(*raw); + + /* copy size */ + put_int_to_vec(raw_list, raw->size()); + + /* copy info */ + std::copy(raw->begin(), raw->end(), std::back_inserter(raw_list)); + + delete raw; + } + + *bytes = new(std::nothrow) char[raw_list.size()]; + retvm_if(!*bytes, -ENOMEM, "Failed to allocate memory"); + + std::copy(raw_list.begin(), raw_list.end(), *bytes); + + return raw_list.size(); +} + +void sensor_manager::init_sensors(void) +{ + physical_sensor_handler *sensor; + + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { + sensor = dynamic_cast(it->second); + if (sensor == NULL) + continue; + + /* it doesn't need to deregister handlers, they are consumed in event_loop */ + register_handler(sensor); + } +} + +void sensor_manager::register_handler(physical_sensor_handler *sensor) +{ + ret_if(sensor->get_poll_fd() < 0); + + sensor_event_handler *handler = new(std::nothrow) sensor_event_handler(sensor); + retm_if(!handler, "Failed to allocate memory"); + + m_loop->add_event(sensor->get_poll_fd(), + ipc::EVENT_IN | ipc::EVENT_HUP | ipc::EVENT_NVAL, handler); +} + +void sensor_manager::show(void) +{ + int index = 0; + + _I("========== Loaded sensor information ==========\n"); + for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { + sensor_info info = it->second->get_sensor_info(); + + _I("Sensor #%d[%s]: ", ++index, it->first.c_str()); + info.show(); + } + _I("===============================================\n"); +} diff --git a/src/server/sensor_manager.h b/src/server/sensor_manager.h index 31236af..5c09b0e 100644 --- a/src/server/sensor_manager.h +++ b/src/server/sensor_manager.h @@ -26,8 +26,14 @@ #include "event_loop.h" +#include "sensor_handler.h" +#include "sensor_observer.h" #include "sensor_loader.h" +#include "physical_sensor_handler.h" +#include "fusion_sensor_handler.h" +#include "external_sensor_handler.h" + namespace sensor { class sensor_manager { @@ -38,9 +44,34 @@ public: bool init(void); bool deinit(void); + bool is_supported(const std::string uri); + + bool register_sensor(sensor_handler *sensor); + void deregister_sensor(const std::string uri); + + sensor_handler *get_sensor_by_type(const std::string uri); + sensor_handler *get_sensor(const std::string uri); + std::vector get_sensors(void); + + size_t serialize(int sock_fd, char **bytes); + private: + typedef std::map sensor_map_t; + + void create_physical_sensors( + device_sensor_registry_t &devices, + physical_sensor_registry_t &psensors); + void create_fusion_sensors(fusion_sensor_registry_t &vsensors); + void create_external_sensors(external_sensor_registry_t &vsensors); + + void init_sensors(void); + void register_handler(physical_sensor_handler *sensor); + + void show(void); + ipc::event_loop *m_loop; sensor_loader m_loader; + sensor_map_t m_sensors; }; } -- 2.7.4 From a868eee02e8707fccab8db7810df501d3d28b171 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 19:12:15 +0900 Subject: [PATCH 07/16] sensord: add calibration node setting - read calibration file path from configuration file(/etc/sensor_cal.conf) Change-Id: Ic256a0352e1e047dc4d7a3cc091a0b7cdaf106be Signed-off-by: kibak.yoon --- src/server/server.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/server/server.h | 1 - 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/server/server.cpp b/src/server/server.cpp index fee9abf..a7a2f03 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -28,7 +28,13 @@ #include "sensor_manager.h" #include "server_channel_handler.h" +#define MAX_CONFIG_PATH 255 +#define CAL_CONFIG_PATH "/etc/sensor_cal.conf" +#define SET_CAL 1 +//#define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data" + #define TIMEOUT_TERM 10 +#define MAX_CONNECTION 1000 using namespace sensor; @@ -42,10 +48,6 @@ server::server() { } -server::~server() -{ -} - server &server::instance(void) { static server inst; @@ -83,6 +85,7 @@ bool server::init(void) m_handler = new(std::nothrow) server_channel_handler(m_manager); retvm_if(!m_handler, false, "Failed to allocate memory"); + init_calibration(); init_server(); init_termination(); @@ -109,19 +112,52 @@ void server::deinit(void) is_running.store(false); } +static void set_cal_data(const char *path) +{ + FILE *fp = fopen(path, "w"); + retm_if(!fp, "There is no calibration file[%s]", path); + + fprintf(fp, "%d", SET_CAL); + fclose(fp); + + _I("Succeeded to set calibration data"); +} + +void server::init_calibration(void) +{ + char path[MAX_CONFIG_PATH]; + + FILE *fp = fopen(CAL_CONFIG_PATH, "r"); + retm_if(!fp, "There is no config file[%s]", CAL_CONFIG_PATH); + + while (!feof(fp)) { + if (fgets(path, sizeof(path), fp) == NULL) + break; + set_cal_data(path); + } + + fclose(fp); +} + void server::init_server(void) { m_manager->init(); /* TODO: setting socket option */ - m_server->set_option("max_connection", 1000); + m_server->set_option("max_connection", MAX_CONNECTION); m_server->set_option(SO_TYPE, SOCK_STREAM); m_server->bind(m_handler, &m_loop); } static gboolean terminate(gpointer data) { - /* TODO: if there is no sensor, sensord will be terminated */ + sensor_manager *mgr = reinterpret_cast(data); + std::vector sensors = mgr->get_sensors(); + + if (sensors.size() <= 0) { + _I("Terminating.. because there is no sensors"); + server::stop(); + } return FALSE; } diff --git a/src/server/server.h b/src/server/server.h index 98d99c1..99856a1 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -40,7 +40,6 @@ private: static std::atomic is_running; server(); - ~server(); bool init(void); void deinit(void); -- 2.7.4 From 26eff59fb807a5f0a943f3cb96392ae8f7d0485f Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 19:14:20 +0900 Subject: [PATCH 08/16] sensord: implement server_channel_handler - server_channel_handler is a handler that responds to client request. Change-Id: I788010896384f686fa271d61662cd09ae48ca4c2 Signed-off-by: kibak.yoon --- src/server/server_channel_handler.cpp | 128 +++++++++++++++++++++++++++++++--- src/server/server_channel_handler.h | 12 +++- 2 files changed, 128 insertions(+), 12 deletions(-) diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index afcfa4e..a34f58f 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -21,6 +21,7 @@ #include #include +#include #include using namespace sensor; @@ -41,6 +42,13 @@ void server_channel_handler::connected(channel *ch) void server_channel_handler::disconnected(channel *ch) { + auto it = m_listeners.find(ch); + ret_if(it == m_listeners.end()); + + _I("Disconnected listener[%u]", it->second->get_id()); + + delete it->second; + m_listeners.erase(ch); } void server_channel_handler::read(channel *ch, message &msg) @@ -81,55 +89,153 @@ void server_channel_handler::read(channel *ch, message &msg) int server_channel_handler::manager_get_sensor_list(channel *ch, message &msg) { - return OP_ERROR; + ipc::message reply; + char *bytes; + int size; + + size = m_manager->serialize(ch->get_fd(), &bytes); + retv_if(size < 0, size); + + reply.enclose((const char *)bytes, size); + ch->send_sync(&reply); + + delete [] bytes; + + return OP_SUCCESS; } int server_channel_handler::listener_connect(channel *ch, message &msg) { - return OP_ERROR; + 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); + retvm_if(!listener, OP_ERROR, "Failed to allocate memory"); + + buf.listener_id = listener_id; + + message reply; + reply.enclose((const char *)&buf, sizeof(buf)); + reply.header()->err = OP_SUCCESS; + + if (!ch->send_sync(&reply)) + return OP_ERROR; + + _I("Connected sensor_listener[fd(%d) -> id(%u)]", ch->get_fd(), listener_id); + listener_id++; + m_listeners[ch] = listener; + + return OP_SUCCESS; } int server_channel_handler::listener_disconnect(channel *ch, message &msg) { - return OP_ERROR; + auto it = m_listeners.find(ch); + retv_if(it == m_listeners.end(), -EINVAL); + + uint32_t id = m_listeners[ch]->get_id(); + + delete m_listeners[ch]; + m_listeners.erase(ch); + + _D("Disconnected sensor_listener[%u]", id); + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::listener_start(channel *ch, message &msg) { - return OP_ERROR; + auto it = m_listeners.find(ch); + retv_if(it == m_listeners.end(), -EINVAL); + + int ret = m_listeners[ch]->start(); + retv_if(ret < 0, ret); + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::listener_stop(channel *ch, message &msg) { - return OP_ERROR; + auto it = m_listeners.find(ch); + retv_if(it == m_listeners.end(), -EINVAL); + + int ret = m_listeners[ch]->stop(); + retv_if(ret < 0, ret); + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::listener_attr_int(channel *ch, message &msg) { - return OP_ERROR; + int ret = OP_SUCCESS; + + auto it = m_listeners.find(ch); + retv_if(it == m_listeners.end(), -EINVAL); + + cmd_listener_attr_int_t buf; + msg.disclose((char *)&buf); + + switch (buf.attribute) { + case SENSORD_ATTRIBUTE_INTERVAL: + ret = m_listeners[ch]->set_interval(buf.value); break; + case SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY: + ret = m_listeners[ch]->set_max_batch_latency(buf.value); break; + case SENSORD_ATTRIBUTE_PASSIVE_MODE: + ret = m_listeners[ch]->set_passive_mode(buf.value); break; + case SENSORD_ATTRIBUTE_PAUSE_POLICY: + case SENSORD_ATTRIBUTE_AXIS_ORIENTATION: + default: + ret = m_listeners[ch]->set_attribute(buf.attribute, buf.value); + } + retv_if(ret < 0, ret); + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::listener_attr_str(channel *ch, message &msg) { - return OP_ERROR; + auto it = m_listeners.find(ch); + retv_if(it == m_listeners.end(), -EINVAL); + + cmd_listener_attr_str_t buf; + msg.disclose((char *)&buf); + + int ret = m_listeners[ch]->set_attribute(buf.attribute, buf.value, buf.len); + retv_if(ret < 0, ret); + + return send_reply(ch, OP_SUCCESS); } int server_channel_handler::listener_get_data(channel *ch, message &msg) { - return OP_ERROR; + return send_reply(ch, OP_ERROR); } int server_channel_handler::provider_connect(channel *ch, message &msg) { - return OP_ERROR; + return send_reply(ch, OP_ERROR); } int server_channel_handler::provider_disconnect(channel *ch, message &msg) { - return OP_ERROR; + return send_reply(ch, OP_ERROR); } int server_channel_handler::provider_post(channel *ch, message &msg) { - return OP_ERROR; + return send_reply(ch, OP_ERROR); +} + +int server_channel_handler::send_reply(channel *ch, int error) +{ + message reply(error); + retvm_if(!ch->send_sync(&reply), OP_ERROR, "Failed to send reply"); + return OP_SUCCESS; } diff --git a/src/server/server_channel_handler.h b/src/server/server_channel_handler.h index 76ea4a3..d48ff7e 100644 --- a/src/server/server_channel_handler.h +++ b/src/server/server_channel_handler.h @@ -26,10 +26,11 @@ #include #include "sensor_manager.h" +#include "sensor_listener_proxy.h" +#include "application_sensor_handler.h" namespace sensor { -/* TODO: this class seems to be able to split */ class server_channel_handler : public ipc::channel_handler { public: @@ -57,7 +58,16 @@ private: int provider_disconnect(ipc::channel *ch, ipc::message &msg); int provider_post(ipc::channel *ch, ipc::message &msg); + int send_reply(ipc::channel *ch, int error); + sensor_manager *m_manager; + + /* {fd, listener} */ + std::unordered_map m_listeners; + + /* {name, application_sensor_handler} */ + /* TODO: move it to sensor_manager */ + std::unordered_map m_sensors; }; } -- 2.7.4 From 81e3fc9ca45a1b6c21708594e6df8edd6991b85a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Thu, 6 Apr 2017 19:16:45 +0900 Subject: [PATCH 09/16] sensord: add testcase for internal apis - there are just only basic testcases. - [TBD] more test cases will be added. Change-Id: I28e44c0001ef77ce6349e0b03bc6b17bf7dea429 Signed-off-by: kibak.yoon --- src/sensorctl/testcase/unit_client.cpp | 154 +++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/sensorctl/testcase/unit_client.cpp diff --git a/src/sensorctl/testcase/unit_client.cpp b/src/sensorctl/testcase/unit_client.cpp new file mode 100644 index 0000000..1034573 --- /dev/null +++ b/src/sensorctl/testcase/unit_client.cpp @@ -0,0 +1,154 @@ +/* + * sensorctl + * + * 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 +#include +#include + +#include +#include +#include +#include + +#include "log.h" +#include "mainloop.h" +#include "test_bench.h" + +TESTCASE(sensor_api_get_default_sensor, get_sensor_p_1) +{ + int err; + sensor_t sensor; + + err = sensord_get_default_sensor(ACCELEROMETER_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + return true; +} + +TESTCASE(sensor_api_get_sensors, get_sensor_p_2) +{ + int err; + int count; + sensor_t *sensors; + + err = sensord_get_sensors(ACCELEROMETER_SENSOR, &sensors, &count); + ASSERT_EQ(err, 0); + ASSERT_GT(count, 0); + + free(sensors); + + return true; +} + +TESTCASE(sensor_api_connect, connect_p_1) +{ + int err; + int handle; + sensor_t sensor; + + err = sensord_get_default_sensor(ACCELEROMETER_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + ASSERT_GT(handle, 0); + + err = sensord_disconnect(handle); + ASSERT_EQ(err, 1); + + return true; +} + +static bool called = false; + +static void basic_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data) +{ + if (test_option::full_log == false) { + while (true) {} + } + _I("[%llu] %f %f %f\n", data->timestamp, data->values[0], data->values[1], data->values[2]); +} + +TESTCASE(sensor_api_all, all_p_1) +{ + int err; + bool ret; + int handle; + sensor_t sensor; + sensor_t *list; + int count; + + called = false; + + err = sensord_get_default_sensor(ACCELEROMETER_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + err = sensord_get_sensors(ALL_SENSOR, &list, &count); + 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_change_event_interval(handle, 0, 100); + ASSERT_TRUE(ret); + + ret = sensord_change_event_max_batch_latency(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); + + free(list); + + return true; +} + +TESTCASE(sensor_api_provider, provider_base_p_1) +{ + bool ret; + int handle; + + handle = sensord_external_connect("TEST", NULL, NULL); + ASSERT_GE(handle, 0); + + 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); + } + + ret = sensord_external_disconnect(handle); + ASSERT_TRUE(ret); + + return true; +} -- 2.7.4 From 56d7c3a6a1e081dde58ebee6e318a164e9c588ef Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 00:32:56 +0900 Subject: [PATCH 10/16] sensord: check URI to find the matched sensor Change-Id: Ib6d0364fd7036c295129872b7db79f0bd8f40685 Signed-off-by: kibak.yoon --- src/client/sensor_manager.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index 9a93cb5..74ed6c7 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -272,8 +272,12 @@ sensor_info *sensor_manager::get_info(const char *uri) return &m_infos[0]; for (auto it = m_infos.begin(); it != m_infos.end(); ++it) { - if ((*it).get_uri() == uri) - return &*it; + std::size_t found = (*it).get_uri().rfind(uri); + + if (found == std::string::npos) + continue; + + return &*it; } return NULL; @@ -288,8 +292,12 @@ std::vector sensor_manager::get_infos(const char *uri) all = true; for (auto it = m_infos.begin(); it != m_infos.end(); ++it) { - if (all || (*it).get_type_uri() == uri) - infos.push_back(&*it); + std::size_t found = (*it).get_uri().rfind(uri); + + if (!all && found == std::string::npos) + continue; + + infos.push_back(&*it); } return infos; -- 2.7.4 From 7427c10733193b8cfd163353c25b9d7e36d1bd27 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 7 Apr 2017 17:18:00 +0900 Subject: [PATCH 11/16] sensord: remove sensor_permission_t from sensor_info - it just only needs privilege string. Change-Id: I82b25ae10075167781ed21baf62fe868a7b48450 Signed-off-by: kibak.yoon --- src/shared/sensor_info.cpp | 34 +++++++++++++--------------------- src/shared/sensor_info.h | 8 ++++---- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/shared/sensor_info.cpp b/src/shared/sensor_info.cpp index 808c07f..7ff31f4 100644 --- a/src/shared/sensor_info.cpp +++ b/src/shared/sensor_info.cpp @@ -40,7 +40,7 @@ sensor_info::sensor_info() , m_min_interval(0) , m_max_batch_count(0) , m_wakeup_supported(false) -, m_permission(SENSOR_PERMISSION_STANDARD) /* TODO: change it to string */ +, m_privilege("") { } @@ -56,7 +56,7 @@ sensor_info::sensor_info(const sensor_info &info) , m_min_interval(info.m_min_interval) , m_max_batch_count(info.m_max_batch_count) , m_wakeup_supported(info.m_wakeup_supported) -, m_permission(SENSOR_PERMISSION_STANDARD) +, m_privilege(info.m_privilege) { } @@ -78,7 +78,8 @@ sensor_info::sensor_info(const sensor_info_t &info) set_min_interval(info.min_interval); set_max_batch_count(info.max_batch_count); set_wakeup_supported(info.wakeup_supported); - set_permission(SENSOR_PERMISSION_STANDARD); + /* TODO: sensor_info_t should have privilege string */ + set_privilege(""); } sensor_info::sensor_info(const sensor_info2_t &info) @@ -97,13 +98,7 @@ sensor_info::sensor_info(const sensor_info2_t &info) set_min_interval(info.min_interval); set_max_batch_count(info.max_batch_count); set_wakeup_supported(info.wakeup_supported); - - /* TODO : store string just itself */ - std::string privilege = info.privilege; - if (privilege == "http://tizen.org/privilege/healthinfo") - set_permission(SENSOR_PERMISSION_HEALTH_INFO); - else - set_permission(SENSOR_PERMISSION_STANDARD); + set_privilege(info.privilege); } sensor_type_t sensor_info::get_type(void) @@ -161,9 +156,9 @@ bool sensor_info::is_wakeup_supported(void) return m_wakeup_supported; } -sensor_permission_t sensor_info::get_permission(void) +std::string &sensor_info::get_privilege(void) { - return m_permission; + return m_privilege; } void sensor_info::set_type(sensor_type_t type) @@ -221,9 +216,9 @@ void sensor_info::set_wakeup_supported(bool supported) m_wakeup_supported = supported; } -void sensor_info::set_permission(sensor_permission_t permission) +void sensor_info::set_privilege(const char *privilege) { - m_permission = permission; + m_privilege = privilege; } void sensor_info::serialize(raw_data_t &data) @@ -239,12 +234,11 @@ void sensor_info::serialize(raw_data_t &data) put(data, m_min_interval); put(data, m_max_batch_count); put(data, m_wakeup_supported); - put(data, (int)m_permission); + put(data, m_privilege); } void sensor_info::deserialize(const char *data, int data_len) { - int permission; int type; raw_data_t raw_data(&data[0], &data[data_len]); @@ -262,9 +256,7 @@ void sensor_info::deserialize(const char *data, int data_len) it = get(it, m_min_interval); it = get(it, m_max_batch_count); it = get(it, m_wakeup_supported); - - it = get(it, permission); - m_permission = (sensor_permission_t)permission; + it = get(it, m_privilege); } void sensor_info::show(void) @@ -279,7 +271,7 @@ void sensor_info::show(void) _I("Min_interval = %d", m_min_interval); _I("Max_batch_count = %d", m_max_batch_count); _I("Wakeup_supported = %d", m_wakeup_supported); - _I("Privilege = %d", (int)m_permission); + _I("Privilege = %s", m_privilege.c_str()); } void sensor_info::clear(void) @@ -295,7 +287,7 @@ void sensor_info::clear(void) m_min_interval = 0; m_max_batch_count = 0; m_wakeup_supported = false; - m_permission = SENSOR_PERMISSION_STANDARD; + m_privilege.clear(); } void sensor_info::put(raw_data_t &data, int value) diff --git a/src/shared/sensor_info.h b/src/shared/sensor_info.h index 93ae3aa..2611948 100644 --- a/src/shared/sensor_info.h +++ b/src/shared/sensor_info.h @@ -44,14 +44,14 @@ public: std::string &get_type_uri(void); std::string &get_uri(void); std::string &get_model(void); - std::string & get_vendor(void); + std::string &get_vendor(void); float get_min_range(void); float get_max_range(void); float get_resolution(void); int get_min_interval(void); int get_max_batch_count(void); bool is_wakeup_supported(void); - sensor_permission_t get_permission(void); + std::string &get_privilege(void); /* TODO: it would be better to get_type() returns type(URI) */ void set_type(sensor_type_t type); @@ -65,7 +65,7 @@ public: void set_min_interval(int min_interval); void set_max_batch_count(int max_batch_count); void set_wakeup_supported(bool supported); - void set_permission(sensor_permission_t permission); + void set_privilege(const char *privilege); void clear(void); @@ -85,7 +85,7 @@ private: int m_min_interval; int m_max_batch_count; bool m_wakeup_supported; - sensor_permission_t m_permission; + std::string m_privilege; /* TODO: use template */ void put(raw_data_t &data, int value); -- 2.7.4 From ed2ea0fea55c8291d0a4df8dc9ceaff108239ef3 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Fri, 7 Apr 2017 17:25:51 +0900 Subject: [PATCH 12/16] sensor: remove a bad way for hiding sensor handle - from now on, privilege is not checking. Change-Id: I4853b27e2bb7a7c2fc1775974d9df8e199b46ec8 Signed-off-by: kibak.yoon --- src/client/sensor_manager.cpp | 6 ------ src/server/sensor_manager.cpp | 3 --- 2 files changed, 9 deletions(-) diff --git a/src/client/sensor_manager.cpp b/src/client/sensor_manager.cpp index 74ed6c7..5602312 100644 --- a/src/client/sensor_manager.cpp +++ b/src/client/sensor_manager.cpp @@ -89,9 +89,6 @@ int sensor_manager::get_sensor(const char *uri, sensor_t *sensor) return -ENODATA; } - if (info->get_uri() == SENSOR_URI_PERMISSION_DENIED) - return -EACCES; - *sensor = (sensor_t)info; return OP_SUCCESS; } @@ -109,9 +106,6 @@ int sensor_manager::get_sensors(const char *uri, sensor_t **list, int *count) return -ENODATA; } - if (infos[0]->get_uri() == SENSOR_URI_PERMISSION_DENIED) - return -EACCES; - *list = (sensor_t *)malloc(sizeof(sensor_info *) * size); retvm_if(!*list, -ENOMEM, "Failed to allocate memory"); diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index 7a0ccc9..c7a692d 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -267,9 +267,6 @@ size_t sensor_manager::serialize(int sock_fd, char **bytes) for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) { info = it->second->get_sensor_info(); - if (!checker.has_permission(sock_fd, info.get_permission())) - info.set_uri(SENSOR_URI_PERMISSION_DENIED); - raw_data_t *raw = new(std::nothrow) raw_data_t(); retvm_if(!raw, -ENOMEM, "Failed to allocated memory"); -- 2.7.4 From 69dfc88f31ca89c3ede1de88ccfb4d5683cc05f6 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 10:43:29 +0900 Subject: [PATCH 13/16] sensord: move member functions from public to private Change-Id: Ibbf486527e7b41663c6a3948071602d897b890b0 Signed-off-by: kibak.yoon --- src/server/permission_checker.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/server/permission_checker.h b/src/server/permission_checker.h index b307dc0..06499a7 100644 --- a/src/server/permission_checker.h +++ b/src/server/permission_checker.h @@ -32,12 +32,13 @@ public: void init(void); + bool has_permission(int sock_fd, std::string &perm); + bool has_permission(int sock_fd, sensor_permission_t perm); + +private: void init_cynara(void); void deinit_cynara(void); bool has_permission_cynara(int sock_fd, std::string &perm); - - bool has_permission(int sock_fd, std::string &perm); - bool has_permission(int sock_fd, sensor_permission_t perm); }; } -- 2.7.4 From c96ddb24d1a3bebea6ddd5df66c297ac62fc7503 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 10:52:13 +0900 Subject: [PATCH 14/16] sensord: fix a bug which did not check privilege - missing cynara check Change-Id: I3bb6de4babadfd17f45b0b99f2b957942c05cb95 Signed-off-by: kibak.yoon --- src/server/permission_checker.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/server/permission_checker.cpp b/src/server/permission_checker.cpp index 644ce61..e275c30 100644 --- a/src/server/permission_checker.cpp +++ b/src/server/permission_checker.cpp @@ -89,6 +89,7 @@ bool permission_checker::has_permission_cynara(int sock_fd, std::string &perm) { retvm_if(cynara_env == NULL, false, "Cynara not initialized"); + int ret; int pid = -1; char *client = NULL; char *session = NULL; @@ -109,7 +110,13 @@ bool permission_checker::has_permission_cynara(int sock_fd, std::string &perm) return false; } - return true; + ret = cynara_check(cynara_env, client, session, user, perm.c_str()); + + free(client); + free(session); + free(user); + + return (ret == CYNARA_API_ACCESS_ALLOWED); } bool permission_checker::has_permission(int sock_fd, std::string &perm) -- 2.7.4 From 66ab6ae6524109a61092965618a1d178d9f6e410 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 10:56:31 +0900 Subject: [PATCH 15/16] sensord: remove sensor_permission_t in the sensord - SENSOR_PRIVILEGE_PUBLIC should be 0 because of backwards compatibility. Change-Id: I81ff50e0e15459ede107925cc9eedfbabdedc9b8 Signed-off-by: kibak.yoon --- include/sensor_types.h | 10 +--------- src/client/sensor_internal.cpp | 24 ++++++++---------------- src/server/permission_checker.cpp | 15 --------------- src/server/permission_checker.h | 3 --- src/server/sensor_manager.cpp | 3 --- 5 files changed, 9 insertions(+), 46 deletions(-) diff --git a/include/sensor_types.h b/include/sensor_types.h index 3da9cfc..9c9426c 100644 --- a/include/sensor_types.h +++ b/include/sensor_types.h @@ -60,8 +60,6 @@ extern "C" #define SENSOR_UNKNOWN_TYPE "http://tizen.org/sensor/unknown" #define SENSOR_UNKNOWN_NAME "Unknown" -#define SENSOR_URI_PERMISSION_DENIED "http://tizen.org/sensor/EACCES" - typedef int64_t sensor_id_t; typedef void *sensor_t; @@ -179,14 +177,8 @@ typedef struct sensor_info2_t { void *reserved[8]; } sensor_info2_t; -typedef enum sensor_permission_t { - SENSOR_PERMISSION_NONE = 0, - SENSOR_PERMISSION_STANDARD = 1, - SENSOR_PERMISSION_HEALTH_INFO = 2, -} sensor_permission_t; - typedef enum sensor_privilege_t { - SENSOR_PRIVILEGE_PUBLIC = 1, + SENSOR_PRIVILEGE_PUBLIC = 0, } sensor_privilege_t; typedef struct sensor_event_t { diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index f9b2698..364eb92 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -156,22 +156,6 @@ API const char* sensord_get_vendor(sensor_t sensor) return static_cast(sensor)->get_vendor().c_str(); } -API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege) -{ - retvm_if(!manager.connect(), false, "Failed to connect"); - retvm_if(!privilege, false, "Invalid parameter[%#x]", privilege); - retvm_if(!manager.is_supported(sensor), false, - "Invalid sensor[%#x]", sensor); - - /* TODO: - * sensor_permission_t perm = static_cast(sensor)->get_permission(); - * *privilege = static_cast(perm); - */ - *privilege = SENSOR_PRIVILEGE_PUBLIC; - - return true; -} - API bool sensord_get_min_range(sensor_t sensor, float *min_range) { retvm_if(!manager.connect(), false, "Failed to connect"); @@ -650,3 +634,11 @@ API bool sensord_send_command(int handle, const char *command, int command_len) return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS); } +/* deprecated */ +API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege) +{ + *privilege = SENSOR_PRIVILEGE_PUBLIC; + + return true; +} + diff --git a/src/server/permission_checker.cpp b/src/server/permission_checker.cpp index e275c30..05ead10 100644 --- a/src/server/permission_checker.cpp +++ b/src/server/permission_checker.cpp @@ -42,12 +42,6 @@ permission_checker::~permission_checker() deinit_cynara(); } -void permission_checker::init(void) -{ - /* if needed, add privilege to permissions */ - permissions[SENSOR_PERMISSION_HEALTH_INFO] = "http://tizen.org/privilege/healthinfo"; -} - void permission_checker::init_cynara(void) { int err; @@ -125,12 +119,3 @@ bool permission_checker::has_permission(int sock_fd, std::string &perm) return has_permission_cynara(sock_fd, perm); } - -/* TODO: remove sensor_permission_t and this function */ -bool permission_checker::has_permission(int sock_fd, sensor_permission_t perm) -{ - auto it = permissions.find(perm); - retv_if(it == permissions.end(), true); - - return has_permission(sock_fd, permissions[perm]); -} diff --git a/src/server/permission_checker.h b/src/server/permission_checker.h index 06499a7..7549de4 100644 --- a/src/server/permission_checker.h +++ b/src/server/permission_checker.h @@ -30,10 +30,7 @@ public: permission_checker(); ~permission_checker(); - void init(void); - bool has_permission(int sock_fd, std::string &perm); - bool has_permission(int sock_fd, sensor_permission_t perm); private: void init_cynara(void); diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index c7a692d..6b8bc91 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -27,7 +27,6 @@ #include #include "sensor_event_handler.h" -#include "permission_checker.h" #include "sensor_loader.h" #include "physical_sensor_handler.h" #include "fusion_sensor_handler.h" @@ -257,8 +256,6 @@ static void put_int_to_vec(std::vector &data, int value) */ size_t sensor_manager::serialize(int sock_fd, char **bytes) { - static permission_checker checker; - sensor_info info; std::vector raw_list; -- 2.7.4 From 0369befbb6d81edf9b839fffe6b688c807c9a3f3 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 10:59:22 +0900 Subject: [PATCH 16/16] sensord: add tokenize function to sensor::utils Change-Id: I8f646d00a241f23a8ee2ca3725102fecf8ec220c Signed-off-by: kibak.yoon --- src/shared/sensor_utils.cpp | 20 ++++++++++++++++++++ src/shared/sensor_utils.h | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/shared/sensor_utils.cpp b/src/shared/sensor_utils.cpp index 2b2781f..6ef0504 100644 --- a/src/shared/sensor_utils.cpp +++ b/src/shared/sensor_utils.cpp @@ -19,6 +19,7 @@ #include "sensor_utils.h" +#include #include #include #include @@ -29,7 +30,9 @@ #include +#ifndef PATH_MAX #define PATH_MAX 256 +#endif /* TODO: move and define string type to sensor_type.h */ static std::map types = { @@ -199,3 +202,20 @@ const char* sensor::utils::get_client_name(void) return client_name; } + +std::vector sensor::utils::tokenize(const std::string &in, const char *delim) +{ + std::vector tokens; + char *input = g_strdup(in.c_str()); + + char *save = NULL; + char *token = strtok_r(input, delim, &save); + + while (token != NULL) { + tokens.push_back(token); + token = strtok_r(NULL, delim, &save); + } + + g_free(input); + return tokens; +} diff --git a/src/shared/sensor_utils.h b/src/shared/sensor_utils.h index c6ab64d..7e31f6d 100644 --- a/src/shared/sensor_utils.h +++ b/src/shared/sensor_utils.h @@ -22,6 +22,8 @@ #include #include +#include +#include namespace sensor { @@ -33,6 +35,8 @@ namespace utils { const char* get_client_name(void); bool get_proc_name(pid_t pid, char *process_name); + + std::vector tokenize(const std::string &in, const char *delim); } } -- 2.7.4