functions: bios-attr: Check that PLDM is running

The BIOS attribute table can only be updated if PLDM is running. Check
that the service is running, and if it's not, return. A subsequent
commit will add D-Bus matches to trigger if PLDM starts after the bios
attr function.

Change-Id: Id64bffc3b6465091f2df60304a0c95c9e34312dc
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/functions.cpp b/functions.cpp
index 40bfc83..7e684f1 100644
--- a/functions.cpp
+++ b/functions.cpp
@@ -32,6 +32,37 @@
 using namespace phosphor::logging;
 
 /**
+ * @brief GetObject function to find the service given an object path.
+ *        It is used to determine if a service is running, so there is no need
+ *        to specify interfaces as a parameter to constrain the search.
+ */
+std::string getObject(sdbusplus::bus::bus& bus, const std::string& path)
+{
+    auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
+                                      MAPPER_BUSNAME, "GetObject");
+    method.append(path);
+    std::vector<std::string> interfaces;
+    method.append(interfaces);
+
+    std::vector<std::pair<std::string, std::vector<std::string>>> response;
+
+    try
+    {
+        auto reply = bus.call(method);
+        reply.read(response);
+        if (response.empty())
+        {
+            return std::string{};
+        }
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        return std::string{};
+    }
+    return response[0].first;
+}
+
+/**
  * @brief Issue callbacks safely
  *
  * std::function can be empty, so this wrapper method checks for that prior to
@@ -643,6 +674,16 @@
         std::make_shared<decltype(elementsJsonFilePath)>(
             std::move(elementsJsonFilePath));
 
+    // The BIOS attribute table can only be updated if PLDM is running because
+    // PLDM is the one that exposes this property. Return if it's not running.
+    constexpr auto pldmPath = "/xyz/openbmc_project/pldm";
+    auto pldmObject = getObject(bus, pldmPath);
+    if (pldmObject.empty())
+    {
+        loop.exit(0);
+        return nullptr;
+    }
+
     auto getManagedObjects = bus.new_method_call(
         "xyz.openbmc_project.EntityManager", "/",
         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");