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)
typedef void *observer_h;
+typedef struct required_sensor_s {
+ uint32_t id;
+ const char *uri;
+} required_sensor_s;
+
/*
* Sensor interface
*/
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<std::string> &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)
/* 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,
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;
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)
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
sensor_info m_info;
fusion_sensor *m_sensor;
- std::vector<sensor_handler *> m_required_sensors;
+ std::unordered_map<std::string, required_sensor> m_required_sensors;
std::unordered_map<sensor_observer *, int> m_interval_map;
std::unordered_map<sensor_observer *, int> m_batch_latency_map;
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();
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<std::string> 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;
}
}
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;
}
}