Use error message thrown by timedate1 service

If NTP is set, SetTime from timedate1 service throws
a standard error. Current code was throwing a custom error
and it was not much helpful. Correct thing is to re-throw
what timedate1 throws.

Also, current code was gating the SetTime request if the
time mode was NTP. This has been removed now and the decision
is left to timedate1 service now on whether to allow setting
the time or not when NTP is enabled.

Change-Id: I2b132bcea57f5181198dfe0c3635666bc8d7c070
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/time-manager.cpp b/time-manager.cpp
index f9e307e..0279e4a 100644
--- a/time-manager.cpp
+++ b/time-manager.cpp
@@ -199,7 +199,8 @@
     return sd_bus_reply_method_return(m, "i", 0);
 }
 
-int Time::setTimeOfDay(const std::chrono::microseconds& timeOfDayUsec)
+int Time::setTimeOfDay(const std::chrono::microseconds& timeOfDayUsec,
+                       sd_bus_error *retError)
 {
     // These 2 are for bypassing some policy
     // checking in the timedate1 service
@@ -211,7 +212,7 @@
                               "/org/freedesktop/timedate1",
                               "org.freedesktop.timedate1",
                               "SetTime",
-                              nullptr,
+                              retError,
                               nullptr,            // timedate1 does not return response
                               "xbb",
                               (int64_t)timeOfDayUsec.count(), //newTimeUsec,
@@ -297,15 +298,6 @@
               << " Curr_Owner: " << TimeConfig::ownerStr(config.getCurrTimeOwner())
               << std::endl;
 
-    if (config.getCurrTimeMode() == TimeConfig::timeModes::NTP)
-    {
-        std::cerr << "Can not set time. Mode is NTP" << std::endl;
-        *retError = SD_BUS_ERROR_MAKE_CONST(
-                        SD_BUS_ERROR_FAILED, "Current Mode is NTP");
-
-        return -1;
-    }
-
     if(config.getCurrTimeOwner() == TimeConfig::timeOwners::HOST)
     {
         std::cerr << "Can not set time. Owner is HOST" << std::endl;
@@ -354,15 +346,7 @@
     // Set REALTIME and also update hwclock
     auto timeInUsec = std::chrono::microseconds(
                           std::chrono::seconds(timeOfDay));
-    r = setTimeOfDay(timeInUsec);
-    if (r < 0)
-    {
-        std::cerr <<"Error: " << strerror(-r)
-                  << "setting time on BMC" << std::endl;
-        *retError = SD_BUS_ERROR_MAKE_CONST(
-                        SD_BUS_ERROR_FAILED, "Error setting time on BMC");
-    }
-    return r < 0 ? r : 0;
+    return setTimeOfDay(timeInUsec, retError);
 }
 
 // Gets the time string from IPMI ( which is currently in seconds since epoch )
@@ -422,16 +406,7 @@
     }
 
     // We are okay to update time in as long as BMC is not the owner
-    r = setTimeOfDay(hostTimeUsec);
-    if (r < 0)
-    {
-        std::cerr <<"Error: " << strerror(-r)
-                  << "setting HOST time" << std::endl;
-        *retError = SD_BUS_ERROR_MAKE_CONST(
-                        SD_BUS_ERROR_FAILED, "Error setting time");
-    }
-
-    return r < 0 ? r : 0;
+    return setTimeOfDay(hostTimeUsec, retError);
 }
 
 // Gets called into by sd_event on an activity seen on sd_bus