From d5a83bb3924e79727fce9891c10e0df0bb510ae4 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 12 Apr 2017 18:54:08 +0900 Subject: [PATCH] sensord: modify sensor plugin interface - rename from get_sensors to get_sensor_info() - change the parameter of update() from id to URI * [TBD] the name of sensor_notifier Change-Id: Ie50afe75a3517847d17e259a6bf317095a827cc7 Signed-off-by: kibak.yoon --- include/external_sensor.h | 4 +-- include/fusion_sensor.h | 11 +++++-- src/server/external_sensor_handler.cpp | 3 +- src/server/fusion_sensor_handler.cpp | 51 ++++++++++++++++--------------- src/server/fusion_sensor_handler.h | 15 +++++++-- src/server/sensor_event_handler.cpp | 2 +- src/server/sensor_manager.cpp | 56 +++++++++++++++------------------- 7 files changed, 77 insertions(+), 65 deletions(-) diff --git a/include/external_sensor.h b/include/external_sensor.h index d34cf96..d029419 100644 --- a/include/external_sensor.h +++ b/include/external_sensor.h @@ -68,8 +68,8 @@ public: inline uint32_t get_version(void) { return EXTERNAL_SENSOR_VERSION(1, 0); } - virtual int get_sensors(const sensor_info2_t **sensors) = 0; - virtual void set_notifier(sensor_notifier *notifier) = 0; + virtual int get_sensor_info(const sensor_info2_t **info) = 0; + virtual int set_notifier(sensor_notifier *notifier) = 0; virtual int get_data(sensor_data_t **data, int *len) = 0; virtual int start(observer_h ob) diff --git a/include/fusion_sensor.h b/include/fusion_sensor.h index 6fa6d09..f772e3d 100644 --- a/include/fusion_sensor.h +++ b/include/fusion_sensor.h @@ -49,6 +49,11 @@ typedef int (*create_fusion_t)(fusion_sensor_t **sensors); typedef void *observer_h; +typedef struct required_sensor_s { + uint32_t id; + const char *uri; +} required_sensor_s; + /* * Sensor interface */ @@ -58,11 +63,11 @@ public: inline uint32_t get_version(void) { return FUSION_SENSOR_VERSION(1, 0); } - virtual int get_sensors(const sensor_info2_t **sensors) = 0; - virtual void get_required_sensors(std::vector &sensors) = 0; + virtual int get_sensor_info(const sensor_info2_t **info) = 0; + virtual int get_required_sensors(const required_sensor_s **info) = 0; /* if update() returns positive value, then get_data() is called */ - virtual int update(const char *uri, sensor_data_t *data, int len) = 0; + virtual int update(uint32_t id, sensor_data_t *data, int len) = 0; virtual int get_data(sensor_data_t **data, int *len) = 0; virtual int start(observer_h ob) diff --git a/src/server/external_sensor_handler.cpp b/src/server/external_sensor_handler.cpp index 7ace9a5..15b8bae 100644 --- a/src/server/external_sensor_handler.cpp +++ b/src/server/external_sensor_handler.cpp @@ -52,9 +52,8 @@ int external_sensor_notifier::notify(void) /* 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); + return m_sensor->notify(info.get_uri().c_str(), data, len); } external_sensor_handler::external_sensor_handler(const sensor_info &info, diff --git a/src/server/fusion_sensor_handler.cpp b/src/server/fusion_sensor_handler.cpp index ace60b8..05ab314 100644 --- a/src/server/fusion_sensor_handler.cpp +++ b/src/server/fusion_sensor_handler.cpp @@ -34,18 +34,23 @@ fusion_sensor_handler::fusion_sensor_handler(const sensor_info &info, fusion_sensor_handler::~fusion_sensor_handler() { + m_required_sensors.clear(); } -void fusion_sensor_handler::add_required_sensor(sensor_handler *sensor) +void fusion_sensor_handler::add_required_sensor(uint32_t id, sensor_handler *sensor) { - m_required_sensors.push_back(sensor); + sensor_info info = sensor->get_sensor_info(); + m_required_sensors.emplace(info.get_uri(), required_sensor(id, 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) + auto it = m_required_sensors.find(uri); + retv_if(it == m_required_sensors.end(), OP_SUCCESS); + + if (m_sensor->update(it->second.id, (sensor_data_t *)msg->body(), msg->size()) < 0) return OP_SUCCESS; sensor_data_t *data; @@ -54,9 +59,7 @@ int fusion_sensor_handler::update(const char *uri, ipc::message *msg) 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); + return notify(m_info.get_uri().c_str(), data, len); } const sensor_info &fusion_sensor_handler::get_sensor_info(void) @@ -226,9 +229,9 @@ int fusion_sensor_handler::flush(sensor_observer *ob) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->start(this) < 0) return OP_ERROR; } @@ -237,9 +240,9 @@ int fusion_sensor_handler::start_internal(void) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->stop(this) < 0) return OP_ERROR; } @@ -248,9 +251,9 @@ int fusion_sensor_handler::stop_internal(void) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->set_interval(this, interval) < 0) return OP_ERROR; } @@ -259,9 +262,9 @@ int fusion_sensor_handler::set_interval_internal(int32_t interval) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->set_batch_latency(this, latency) < 0) return OP_ERROR; } @@ -270,9 +273,9 @@ int fusion_sensor_handler::set_batch_latency_internal(int32_t latency) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->set_attribute(this, attr, value) < 0) return OP_ERROR; } @@ -281,9 +284,9 @@ int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value) 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) + auto it = m_required_sensors.begin(); + for (; it != m_required_sensors.end(); ++it) { + if (it->second.sensor->set_attribute(this, attr, value, len) < 0) return OP_ERROR; } diff --git a/src/server/fusion_sensor_handler.h b/src/server/fusion_sensor_handler.h index 151bb16..0bd1fa6 100644 --- a/src/server/fusion_sensor_handler.h +++ b/src/server/fusion_sensor_handler.h @@ -30,13 +30,24 @@ namespace sensor { +class required_sensor { +public: + required_sensor(uint32_t _id, sensor_handler *_sensor) + : id(_id) + , sensor(_sensor) + {} + + uint32_t id; + sensor_handler *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); + void add_required_sensor(uint32_t id, sensor_handler *sensor); /* subscriber */ int update(const char *uri, ipc::message *msg); @@ -67,7 +78,7 @@ private: sensor_info m_info; fusion_sensor *m_sensor; - std::vector m_required_sensors; + std::unordered_map m_required_sensors; std::unordered_map m_interval_map; std::unordered_map m_batch_latency_map; diff --git a/src/server/sensor_event_handler.cpp b/src/server/sensor_event_handler.cpp index de13006..4fac18d 100644 --- a/src/server/sensor_event_handler.cpp +++ b/src/server/sensor_event_handler.cpp @@ -62,7 +62,7 @@ bool sensor_event_handler::handle(int fd, ipc::event_condition condition) info = m_sensor->get_sensor_info(); //_I("[Data] allocate %p", data); - if (m_sensor->notify(info.get_type_uri().c_str(), data, length) < 0) { + if (m_sensor->notify(info.get_uri().c_str(), data, length) < 0) { free(data); } info.clear(); diff --git a/src/server/sensor_manager.cpp b/src/server/sensor_manager.cpp index 7aa04fe..9a06489 100644 --- a/src/server/sensor_manager.cpp +++ b/src/server/sensor_manager.cpp @@ -187,41 +187,37 @@ void sensor_manager::create_physical_sensors(device_sensor_registry_t &devices, void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors) { const sensor_info2_t *info; + const required_sensor_s *required_sensors; fusion_sensor_handler *fsensor; - std::vector req_names; + sensor_handler *sensor = NULL; 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(); + bool support = true; - fsensor = new(std::nothrow) fusion_sensor_handler(info[i], it->get()); - retm_if(!fsensor, "Failed to allocate memory"); + (*it)->get_sensor_info(&info); - (*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]); + fsensor = new(std::nothrow) fusion_sensor_handler(info[0], it->get()); + retm_if(!fsensor, "Failed to allocate memory"); - if (sensor == NULL) { - support = false; - break; - } + int count = (*it)->get_required_sensors(&required_sensors); + for (int i = 0; i < count; ++i) { + sensor = get_sensor_by_type(required_sensors[i].uri); - fsensor->add_required_sensor(sensor); + if (sensor == NULL) { + support = false; + break; } - if (!support) { - delete fsensor; - /* TODO: remove plugin */ - continue; - } + fsensor->add_required_sensor(required_sensors[i].id, sensor); + } - sensor_info sinfo = fsensor->get_sensor_info(); - m_sensors[sinfo.get_uri()] = fsensor; + if (!support) { + delete fsensor; + continue; } + + sensor_info sinfo = fsensor->get_sensor_info(); + m_sensors[sinfo.get_uri()] = fsensor; } } @@ -231,15 +227,13 @@ void sensor_manager::create_external_sensors(external_sensor_registry_t &esensor external_sensor_handler *esensor; for (auto it = esensors.begin(); it != esensors.end(); ++it) { - int count = (*it)->get_sensors(&info); + (*it)->get_sensor_info(&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"); + esensor = new(std::nothrow) external_sensor_handler(info[0], it->get()); + retm_if(!esensor, "Failed to allocate memory"); - sensor_info sinfo = esensor->get_sensor_info(); - m_sensors[sinfo.get_uri()] = esensor; - } + sensor_info sinfo = esensor->get_sensor_info(); + m_sensors[sinfo.get_uri()] = esensor; } } -- 2.7.4