cpusensor: detect if cpu is available

Add CPU inventory item detection via gpio.

Tested: inventory item is exposed

Change-Id: Ia26a4bed5534edec6a7f5c7cf0965f84fa0fc23a
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/src/CPUSensorMain.cpp b/src/CPUSensorMain.cpp
index d16cf8a..44c5178 100644
--- a/src/CPUSensorMain.cpp
+++ b/src/CPUSensorMain.cpp
@@ -515,9 +515,14 @@
     });
 }
 
-bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
-                  boost::container::flat_set<CPUConfig>& cpuConfigs,
-                  ManagedObjectType& sensorConfigs)
+bool getCpuConfig(
+    const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
+    boost::container::flat_set<CPUConfig>& cpuConfigs,
+    ManagedObjectType& sensorConfigs,
+    sdbusplus::asio::object_server& objectServer,
+    boost::container::flat_map<
+        std::string, std::shared_ptr<sdbusplus::asio::dbus_interface>>&
+        inventoryIfaces)
 {
     bool useCache = false;
     sensorConfigs.clear();
@@ -559,6 +564,28 @@
                 std::string name =
                     std::regex_replace(nameRaw, illegalDbusRegex, "_");
 
+                auto findCpuGpio = config.second.find("PresenceGpio");
+                if (findCpuGpio != config.second.end())
+                {
+                    size_t gpio = std::visit(VariantToUnsignedIntVisitor(),
+                                             findCpuGpio->second);
+                    bool present = hostIsPresent(gpio);
+                    if (inventoryIfaces.find(name) == inventoryIfaces.end())
+                    {
+                        auto iface = objectServer.add_interface(
+                            cpuInventoryPath + std::string("/") + name,
+                            "xyz.openbmc_project.Inventory.Item");
+                        iface->register_property("PrettyName", name);
+                        iface->register_property("Present", present);
+                        iface->initialize();
+                        inventoryIfaces[name] = std::move(iface);
+                    }
+                    if (!present)
+                    {
+                        continue; // no reason to look for non present cpu
+                    }
+                }
+
                 auto findBus = config.second.find("Bus");
                 if (findBus == config.second.end())
                 {
@@ -614,6 +641,9 @@
     boost::asio::deadline_timer creationTimer(io);
     boost::asio::deadline_timer filterTimer(io);
     ManagedObjectType sensorConfigs;
+    boost::container::flat_map<std::string,
+                               std::shared_ptr<sdbusplus::asio::dbus_interface>>
+        inventoryIfaces;
 
     filterTimer.expires_from_now(boost::posix_time::seconds(1));
     filterTimer.async_wait([&](const boost::system::error_code& ec) {
@@ -622,7 +652,8 @@
             return; // we're being canceled
         }
 
-        if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
+        if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer,
+                         inventoryIfaces))
         {
             detectCpuAsync(pingTimer, creationTimer, io, objectServer,
                            systemBus, cpuConfigs, sensorConfigs);
@@ -650,7 +681,8 @@
                     return; // we're being canceled
                 }
 
-                if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
+                if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
+                                 objectServer, inventoryIfaces))
                 {
                     detectCpuAsync(pingTimer, creationTimer, io, objectServer,
                                    systemBus, cpuConfigs, sensorConfigs);
diff --git a/src/TachSensor.cpp b/src/TachSensor.cpp
index 295c7e6..bbd382f 100644
--- a/src/TachSensor.cpp
+++ b/src/TachSensor.cpp
@@ -14,10 +14,12 @@
 // limitations under the License.
 */
 
+#include "TachSensor.hpp"
+
+#include "Utils.hpp"
+
 #include <unistd.h>
 
-#include <TachSensor.hpp>
-#include <Utils.hpp>
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/algorithm/string/replace.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 88a8390..d90813d 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -31,6 +31,7 @@
 
 static bool powerStatusOn = false;
 static bool biosHasPost = false;
+
 static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
 
 bool getSensorConfiguration(