dbusconfiguration: Fix and simplify zone calculation
This code was duplicated throughout the file and the index
calculation was wrong as .size() gave us greater than the
last index.
Tested-by: Added two zones and set DEBUG = true to notice
all pids in the correct zone.
Change-Id: I8eaff89ffcbd75e68abca1e79ab9cfd67db0c03c
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/dbus/dbusconfiguration.cpp b/dbus/dbusconfiguration.cpp
index 78cc4e4..31742f5 100644
--- a/dbus/dbusconfiguration.cpp
+++ b/dbus/dbusconfiguration.cpp
@@ -140,6 +140,18 @@
return 1;
}
+size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
+{
+ auto it = std::find(zones.begin(), zones.end(), name);
+ if (it == zones.end())
+ {
+ zones.emplace_back(name);
+ it = zones.end() - 1;
+ }
+
+ return it - zones.begin();
+}
+
void init(sdbusplus::bus::bus& bus)
{
using DbusVariantType =
@@ -270,7 +282,7 @@
// on dbus having an index field is a bit strange, so randomly
// assign index based on name property
- std::vector<std::string> zoneIndex;
+ std::vector<std::string> foundZones;
for (const auto& configuration : configurations)
{
auto findZone =
@@ -278,19 +290,10 @@
if (findZone != configuration.second.end())
{
const auto& zone = findZone->second;
- size_t index = 1;
+
const std::string& name =
variant_ns::get<std::string>(zone.at("Name"));
- auto it = std::find(zoneIndex.begin(), zoneIndex.end(), name);
- if (it == zoneIndex.end())
- {
- zoneIndex.emplace_back(name);
- index = zoneIndex.size();
- }
- else
- {
- index = zoneIndex.end() - it;
- }
+ size_t index = getZoneIndex(name, foundZones);
auto& details = zoneDetailsConfig[index];
details.minthermalrpm = variant_ns::apply_visitor(
@@ -308,17 +311,7 @@
variant_ns::get<std::vector<std::string>>(base.at("Zones"));
for (const std::string& zone : zones)
{
- auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
- size_t index = 1;
- if (it == zoneIndex.end())
- {
- zoneIndex.emplace_back(zone);
- index = zoneIndex.size();
- }
- else
- {
- index = zoneIndex.end() - it;
- }
+ size_t index = getZoneIndex(zone, foundZones);
PIDConf& conf = zoneConfig[index];
std::vector<std::string> sensorNames =
@@ -436,17 +429,7 @@
variant_ns::get<std::vector<std::string>>(base.at("Zones"));
for (const std::string& zone : zones)
{
- auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
- size_t index = 1;
- if (it == zoneIndex.end())
- {
- zoneIndex.emplace_back(zone);
- index = zoneIndex.size();
- }
- else
- {
- index = zoneIndex.end() - it;
- }
+ size_t index = getZoneIndex(zone, foundZones);
PIDConf& conf = zoneConfig[index];
std::vector<std::string> inputs;