diff options
| author | James Feist <james.feist@linux.intel.com> | 2018-11-15 14:46:36 -0800 |
|---|---|---|
| committer | Patrick Venture <venture@google.com> | 2018-11-16 02:26:41 +0000 |
| commit | ffd418bb5212a959150a66215fe428161d59e9c8 (patch) | |
| tree | 7688505b3459de7a66341178e08e75ab268389be /dbus | |
| parent | 734f953519fbb21a277313152399971744df158f (diff) | |
| download | phosphor-pid-control-ffd418bb5212a959150a66215fe428161d59e9c8.tar.gz phosphor-pid-control-ffd418bb5212a959150a66215fe428161d59e9c8.zip | |
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>
Diffstat (limited to 'dbus')
| -rw-r--r-- | dbus/dbusconfiguration.cpp | 51 |
1 files changed, 17 insertions, 34 deletions
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 @@ int eventHandler(sd_bus_message*, void*, sd_bus_error*) 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 @@ void init(sdbusplus::bus::bus& bus) // 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 @@ void init(sdbusplus::bus::bus& bus) 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 @@ void init(sdbusplus::bus::bus& bus) 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 @@ void init(sdbusplus::bus::bus& bus) 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; |

