PLDM : Changes in setDateTime command

The BMC will have the correct time when in NTP mode, so we
don't need to try and set the time again which may cause
potential time drifts.

Tested: Using pldmtool

Change-Id: I86b2c056ea6628ed4dffeb7ad754bdd1ba295f42
Signed-off-by: Pavithra Barithaya <pavithra.b@ibm.com>
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 2306ad9..9aa44ea 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -17,13 +17,10 @@
 
 namespace pldm
 {
-
 namespace responder
 {
-
 namespace utils
 {
-
 void epochToBCDTime(uint64_t timeSec, uint8_t& seconds, uint8_t& minutes,
                     uint8_t& hours, uint8_t& day, uint8_t& month,
                     uint16_t& year)
@@ -64,7 +61,6 @@
 
 namespace bios
 {
-
 using EpochTimeUS = uint64_t;
 
 DBusHandler dbusHandler;
@@ -161,6 +157,32 @@
     uint16_t year = 0;
     std::time_t timeSec;
 
+    constexpr auto timeSyncPath = "/xyz/openbmc_project/time/sync_method";
+    constexpr auto timeSyncInterface =
+        "xyz.openbmc_project.Time.Synchronization";
+    constexpr auto timeSyncProperty = "TimeSyncMethod";
+
+    // The time is correct on BMC when in NTP mode, so we do not want to
+    // try and set the time again and cause potential time drifts.
+    try
+    {
+        auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
+            timeSyncPath, timeSyncProperty, timeSyncInterface);
+        const auto& mode = std::get<std::string>(propVal);
+
+        if (mode == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
+        {
+            return ccOnlyResponse(request, PLDM_SUCCESS);
+        }
+    }
+    catch (const std::exception& e)
+    {
+        std::cerr << "Error getting the time sync property, PATH="
+                  << timeSyncPath << "INTERFACE=" << timeSyncInterface
+                  << "PROPERTY=" << timeSyncProperty << "ERROR=" << e.what()
+                  << "\n";
+    }
+
     constexpr auto setTimeInterface = "xyz.openbmc_project.Time.EpochTime";
     constexpr auto setTimePath = "/xyz/openbmc_project/time/bmc";
     constexpr auto timeSetPro = "Elapsed";