Add timer use field support in watchdog command

Tested:
ipmitool raw 0x06 0x24 1 0 0 0 0 0
ipmitool mc watchdog get

Change-Id: Iaffd6622821d33183a52f54a4e2e52a36aa17dde
Signed-off-by: Yong Li <yong.b.li@linux.intel.com>
diff --git a/app/watchdog.cpp b/app/watchdog.cpp
index 7ca465c..abdf8e4 100644
--- a/app/watchdog.cpp
+++ b/app/watchdog.cpp
@@ -88,6 +88,8 @@
 static constexpr uint8_t wd_dont_stop = 0x1 << 6;
 static constexpr uint8_t wd_timeout_action_mask = 0x3;
 
+static constexpr uint8_t wdTimerUseMask = 0x7;
+
 enum class IpmiAction : uint8_t
 {
     None = 0x0,
@@ -127,6 +129,51 @@
     }
 }
 
+enum class IpmiTimerUse : uint8_t
+{
+    Reserved = 0x0,
+    BIOSFRB2 = 0x1,
+    BIOSPOST = 0x2,
+    OSLoad = 0x3,
+    SMSOS = 0x4,
+    OEM = 0x5,
+};
+
+WatchdogService::TimerUse ipmiTimerUseToWdTimerUse(IpmiTimerUse ipmiTimerUse)
+{
+    switch (ipmiTimerUse)
+    {
+        case IpmiTimerUse::Reserved:
+        {
+            return WatchdogService::TimerUse::Reserved;
+        }
+        case IpmiTimerUse::BIOSFRB2:
+        {
+            return WatchdogService::TimerUse::BIOSFRB2;
+        }
+        case IpmiTimerUse::BIOSPOST:
+        {
+            return WatchdogService::TimerUse::BIOSPOST;
+        }
+        case IpmiTimerUse::OSLoad:
+        {
+            return WatchdogService::TimerUse::OSLoad;
+        }
+        case IpmiTimerUse::SMSOS:
+        {
+            return WatchdogService::TimerUse::SMSOS;
+        }
+        case IpmiTimerUse::OEM:
+        {
+            return WatchdogService::TimerUse::OEM;
+        }
+        default:
+        {
+            return WatchdogService::TimerUse::Reserved;
+        }
+    }
+}
+
 struct wd_set_req
 {
     uint8_t timer_use;
@@ -170,6 +217,10 @@
             static_cast<IpmiAction>(req.timer_action & wd_timeout_action_mask);
         wd_service.setExpireAction(ipmiActionToWdAction(ipmi_action));
 
+        const auto ipmiTimerUse =
+            static_cast<IpmiTimerUse>(req.timer_use & wdTimerUseMask);
+        wd_service.setTimerUse(ipmiTimerUseToWdTimerUse(ipmiTimerUse));
+
         // Set the new interval and the time remaining deci -> mill seconds
         const uint64_t interval = req.initial_countdown * 100;
         wd_service.setInterval(interval);
@@ -239,6 +290,42 @@
     }
 }
 
+IpmiTimerUse wdTimerUseToIpmiTimerUse(WatchdogService::TimerUse wdTimerUse)
+{
+    switch (wdTimerUse)
+    {
+        case WatchdogService::TimerUse::Reserved:
+        {
+            return IpmiTimerUse::Reserved;
+        }
+        case WatchdogService::TimerUse::BIOSFRB2:
+        {
+            return IpmiTimerUse::BIOSFRB2;
+        }
+        case WatchdogService::TimerUse::BIOSPOST:
+        {
+            return IpmiTimerUse::BIOSPOST;
+        }
+        case WatchdogService::TimerUse::OSLoad:
+        {
+            return IpmiTimerUse::OSLoad;
+        }
+
+        case WatchdogService::TimerUse::SMSOS:
+        {
+            return IpmiTimerUse::SMSOS;
+        }
+        case WatchdogService::TimerUse::OEM:
+        {
+            return IpmiTimerUse::OEM;
+        }
+        default:
+        {
+            return IpmiTimerUse::Reserved;
+        }
+    }
+}
+
 struct wd_get_res
 {
     uint8_t timer_use;
@@ -278,6 +365,10 @@
         {
             res.timer_use |= wd_running;
         }
+
+        res.timer_use |=
+            static_cast<uint8_t>(wdTimerUseToIpmiTimerUse(wd_prop.timerUse));
+
         // TODO: Do something about having pretimeout support
         res.pretimeout = 0;
         res.expire_flags = 0;
diff --git a/app/watchdog_service.cpp b/app/watchdog_service.cpp
index 1deb58b..e65ea63 100644
--- a/app/watchdog_service.cpp
+++ b/app/watchdog_service.cpp
@@ -80,6 +80,9 @@
         wd_prop.enabled = get<bool>(properties.at("Enabled"));
         wd_prop.expireAction = Watchdog::convertActionFromString(
             get<std::string>(properties.at("ExpireAction")));
+        wd_prop.timerUse = Watchdog::convertTimerUseFromString(
+            get<std::string>(properties.at("CurrentTimerUse")));
+
         wd_prop.interval = get<uint64_t>(properties.at("Interval"));
         wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
         return wd_prop;
@@ -179,6 +182,11 @@
     setProperty("ExpireAction", convertForMessage(expireAction));
 }
 
+void WatchdogService::setTimerUse(TimerUse timerUse)
+{
+    setProperty("CurrentTimerUse", convertForMessage(timerUse));
+}
+
 void WatchdogService::setInterval(uint64_t interval)
 {
     setProperty("Interval", interval);
diff --git a/app/watchdog_service.hpp b/app/watchdog_service.hpp
index 8056fb7..75afc1e 100644
--- a/app/watchdog_service.hpp
+++ b/app/watchdog_service.hpp
@@ -16,6 +16,8 @@
 
     using Action =
         sdbusplus::xyz::openbmc_project::State::server::Watchdog::Action;
+    using TimerUse =
+        sdbusplus::xyz::openbmc_project::State::server::Watchdog::TimerUse;
 
     /** @brief Resets the time remaining on the watchdog.
      *         Equivalent to setTimeRemaining(getInterval()).
@@ -33,6 +35,7 @@
         bool initialized;
         bool enabled;
         Action expireAction;
+        TimerUse timerUse;
         uint64_t interval;
         uint64_t timeRemaining;
     };
@@ -70,6 +73,12 @@
      */
     void setExpireAction(Action expireAction);
 
+    /** @brief Sets the value of the timerUse property on the host watchdog
+     *
+     *  @param[in] timerUse - The new timerUse value
+     */
+    void setTimerUse(TimerUse timerUse);
+
     /** @brief Sets the value of the interval property on the host watchdog
      *
      *  @param[in] interval - The new interval value