Defintion of switchTimer

switchTimer is used to enable or disable the character accumulate
timer or the retry timer.

Change-Id: Ica311650f9c4e723bccdcaeb952d9ec080c06d9a
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/sd_event_loop.cpp b/sd_event_loop.cpp
index d30aafe..14078ea 100644
--- a/sd_event_loop.cpp
+++ b/sd_event_loop.cpp
@@ -217,4 +217,58 @@
     }
 }
 
+void EventLoop::switchTimer(uint8_t payloadInst,
+                            Timers type,
+                            bool status)
+{
+    auto iter = payloadInfo.find(payloadInst);
+    if (iter == payloadInfo.end())
+    {
+        log<level::ERR>("SOL Payload instance not found",
+                entry("payloadInst=%d", payloadInst));
+        throw std::runtime_error("SOL Payload instance not found");
+    }
+
+    int rc = 0;
+    auto source = (std::get<0>(iter->second.at(type))).get();
+    auto interval = std::get<1>(iter->second.at(type));
+
+    // Turn OFF the timer
+    if (!status)
+    {
+        rc = sd_event_source_set_enabled(source, SD_EVENT_OFF);
+        if (rc < 0)
+        {
+            log<level::ERR>("Failed to disable the timer", entry("RC=%d", rc));
+            throw std::runtime_error("Failed to disable timer");
+        }
+        return;
+    }
+
+    // Turn ON the timer
+    uint64_t currentTime = 0;
+    rc = sd_event_now(event, CLOCK_MONOTONIC, &currentTime);
+    if (rc < 0)
+    {
+        log<level::ERR>("Failed to get the current timestamp",
+                entry("RC=%d", rc));
+        throw std::runtime_error("Failed to get current timestamp");
+    }
+
+    rc = sd_event_source_set_time(source, currentTime + interval.count());
+    if (rc < 0)
+    {
+        log<level::ERR>("sd_event_source_set_time function failed",
+                entry("RC=%d", rc));
+        throw std::runtime_error("sd_event_source_set_time function failed");
+    }
+
+    rc = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
+    if (rc < 0)
+    {
+        log<level::ERR>("Failed to enable the timer", entry("RC=%d",rc));
+        throw std::runtime_error("Failed to enable timer");
+    }
+}
+
 } // namespace eventloop