From 8db0f6f96401b9e0598836f761b808b590ce3f38 Mon Sep 17 00:00:00 2001 From: Matthew Barth Date: Thu, 23 Feb 2017 10:16:57 -0600 Subject: Only update inventory when presence state changes When the presence state of a fan enclosure, determined from all the sensors part of that fan enclosure, changes then update inventory. Change-Id: Ie80e83fa7d0200239ced7b9d2ef84664e599e8ca Signed-off-by: Matthew Barth --- fan_enclosure.cpp | 72 +++++++++++++++++++++++++++++++++---------------------- fan_enclosure.hpp | 11 ++++++++- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/fan_enclosure.cpp b/fan_enclosure.cpp index 998bd50..aaf415d 100644 --- a/fan_enclosure.cpp +++ b/fan_enclosure.cpp @@ -21,17 +21,24 @@ constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; -FanEnclosure::ObjectMap FanEnclosure::getObjectMap() +presenceState FanEnclosure::getCurPresState() { - ObjectMap invObj; - InterfaceMap invIntf; - PropertyMap invProp; auto presPred = [](auto const& s) {return s->isPresent();}; // Determine if all sensors show fan is not present auto isPresent = std::any_of(sensors.begin(), sensors.end(), presPred); - invProp.emplace("Present", isPresent); + + return (isPresent) ? PRESENT : NOT_PRESENT; +} + +FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState) +{ + ObjectMap invObj; + InterfaceMap invIntf; + PropertyMap invProp; + + invProp.emplace("Present", curPresState); invProp.emplace("PrettyName", fanDesc); invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp)); Object fanInvPath = invPath; @@ -71,31 +78,38 @@ std::string FanEnclosure::getInvService() void FanEnclosure::updInventory() { - //Get inventory object for this fan - ObjectMap invObj = getObjectMap(); - //Get inventory manager service name from mapper - std::string invService; - try - { - invService = getInvService(); - } - catch (const std::runtime_error& err) - { - log(err.what()); - return; - } - // Update inventory for this fan - auto invMsg = bus.new_method_call(invService.c_str(), - INVENTORY_PATH, - INVENTORY_INTF, - "Notify"); - invMsg.append(std::move(invObj)); - auto invMgrResponseMsg = bus.call(invMsg); - if (invMgrResponseMsg.is_method_error()) + auto curPresState = getCurPresState(); + // Only update inventory when presence state changed + if (presState != curPresState) { - log( - "Error in inventory manager call to update inventory"); - return; + // Get inventory object for this fan + ObjectMap invObj = getObjectMap(curPresState); + // Get inventory manager service name from mapper + std::string invService; + try + { + invService = getInvService(); + } + catch (const std::runtime_error& err) + { + log(err.what()); + return; + } + // Update inventory for this fan + auto invMsg = bus.new_method_call(invService.c_str(), + INVENTORY_PATH, + INVENTORY_INTF, + "Notify"); + invMsg.append(std::move(invObj)); + auto invMgrResponseMsg = bus.call(invMsg); + if (invMgrResponseMsg.is_method_error()) + { + log( + "Error in inventory manager call to update inventory"); + return; + } + // Inventory updated, set presence state to current + presState = curPresState; } } diff --git a/fan_enclosure.hpp b/fan_enclosure.hpp index cd9f689..da6f7e5 100644 --- a/fan_enclosure.hpp +++ b/fan_enclosure.hpp @@ -12,6 +12,13 @@ namespace fan namespace presence { +typedef enum presenceState +{ + NOT_PRESENT, + PRESENT, + UNKNOWN +} presenceState; + class FanEnclosure { using Property = std::string; @@ -52,10 +59,12 @@ class FanEnclosure const std::string invPath; const std::string fanDesc; std::vector> sensors; + presenceState presState = UNKNOWN; + presenceState getCurPresState(); //TODO openbmc/openbmc#1299 - Move getInvService() to a utility file std::string getInvService(); - ObjectMap getObjectMap(); + ObjectMap getObjectMap(bool curPresState); }; -- cgit v1.2.1