psu-ng: Add Availability support to power supplies

Add support for determining if a power supply is considered 'available'
or not, and set the Available D-Bus property on the
xyz.openbmc_project.State.Decorator.Availability interface with the
result.

A power supply is considered unavailable if any of the following are
true:
 - it isn't present
 - there is a input fault active
 - there is a Vin UV fault active
 - there is a PS KILL fault active
 - there is a Iout OC fault active

The latter four faults are the faults where it can't provide good
output power, hence the PS is unavailable.  These faults also don't
call out the power supply directly, so the Functional property won't
get set to false, unlike other faults.

The Available D-Bus property is then able to be used in Redfish
responses for the heath and state properties in the power supply
schema.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I349eddb8c052cf238276c832af0dde9e49f6d3f0
diff --git a/phosphor-power-supply/power_supply.cpp b/phosphor-power-supply/power_supply.cpp
index 27bc502..e5fa9c8 100644
--- a/phosphor-power-supply/power_supply.cpp
+++ b/phosphor-power-supply/power_supply.cpp
@@ -186,6 +186,7 @@
         std::string prettyName = invpath.substr(lastSlashPos + 1);
         setPresence(bus, invpath, present, prettyName);
         updateInventory();
+        checkAvailability();
     }
 }
 
@@ -563,6 +564,8 @@
                         .c_str());
                 clearFaults();
             }
+
+            checkAvailability();
         }
         catch (const ReadFailure& e)
         {
@@ -616,6 +619,7 @@
     if (present)
     {
         clearFaultFlags();
+        checkAvailability();
         readFail = 0;
 
         try
@@ -662,6 +666,7 @@
             // Clear out the now outdated inventory properties
             updateInventory();
         }
+        checkAvailability();
     }
 }
 
@@ -690,6 +695,7 @@
                                      .c_str());
 
                 updateInventory();
+                checkAvailability();
             }
         }
     }
@@ -905,4 +911,17 @@
     }
 }
 
+void PowerSupply::checkAvailability()
+{
+    bool origAvailability = available;
+    available = present && !hasInputFault() && !hasVINUVFault() &&
+                !hasPSKillFault() && !hasIoutOCFault();
+
+    if (origAvailability != available)
+    {
+        auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
+        phosphor::power::psu::setAvailable(bus, invpath, available);
+    }
+}
+
 } // namespace phosphor::power::psu