Add OCC present count detection and watch

Add a Presence child class of Error to handle detecting the number of
OCCs available. Add an instance of this Presence class if the Device
detects that it is the master OCC, since the number of present OCCs is
only reported by the master OCC. When a change to the number of OCCs
reported is detected, compare with the number of OCCs determined to be
active by the Manager, and if there is a mismatch, follow the usual
error path (reset OCC, etc).

Partially resolves openbmc/openbmc#2285
See https://gerrit.openbmc-project.xyz/#/c/7843/

Change-Id: Idbaca52b307992d9b01fe15439ab746ef6d64397
Signed-off-by: Edward A. James <eajames@us.ibm.com>
diff --git a/occ_presence.cpp b/occ_presence.cpp
new file mode 100644
index 0000000..f91521a
--- /dev/null
+++ b/occ_presence.cpp
@@ -0,0 +1,57 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include <org/open_power/OCC/Device/error.hpp>
+#include "occ_presence.hpp"
+#include "occ_manager.hpp"
+#include "elog-errors.hpp"
+
+namespace open_power
+{
+namespace occ
+{
+
+// Reads the occs_present file and analyzes the data
+void Presence::analyzeEvent()
+{
+    using namespace phosphor::logging;
+    using namespace sdbusplus::org::open_power::OCC::Device::Error;
+
+    // Get the number of bytes to read
+    int len = -1;
+    auto r = ioctl(fd, FIONREAD, &len);
+    if (r < 0)
+    {
+        elog<ConfigFailure>(
+            phosphor::logging::org::open_power::OCC::Device::
+                ConfigFailure::CALLOUT_ERRNO(errno),
+            phosphor::logging::org::open_power::OCC::Device::
+                ConfigFailure::CALLOUT_DEVICE_PATH(file.c_str()));
+    }
+
+    auto data = readFile(len);
+    if (data.empty())
+    {
+        return;
+    }
+
+    // Let stoi determine the base
+    auto occsPresent = std::stoi(data, nullptr, 0);
+    if (manager.getNumOCCs() != occsPresent)
+    {
+        log<level::INFO>("OCC presence mismatch",
+                         entry("BMC_OCCS=%d", manager.getNumOCCs(),
+                         entry("OCC_OCCS=%d", occsPresent)));
+        if (callBack)
+        {
+            callBack();
+        }
+    }
+}
+
+} // namespace occ
+} // namespace open_power