#include <unistd.h>
#include <sensor_log.h>
+#include <message.h>
+#include <command_types.h>
#include <string>
#include <vector>
#include <memory>
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");
+ retvm_if(devices.empty() && external_sensors.empty(), false, "There is no sensors");
/* TODO: support dynamic sensor */
create_physical_sensors(devices, physical_sensors);
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);
+ retm_if(size == -ENOMEM, "Failed to serialize");
+
+ ipc::message msg((const char *)bytes, size);
+ msg.set_type(CMD_MANAGER_SENSOR_ADDED);
+
+ send(msg);
+ delete []bytes;
+}
+
+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");
m_sensors[info.get_uri()] = sensor;
+ send_added_msg(&info);
+
_I("Registered[%s]", info.get_uri().c_str());
return true;
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();
- 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;
}
m_sensors[sinfo.get_uri()] = psensor;
}
}
-
}
void sensor_manager::create_fusion_sensors(fusion_sensor_registry_t &fsensors)
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] ...}
*/