Refactor watchdog API 21/279221/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 4 Aug 2022 01:46:46 +0000 (10:46 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 4 Aug 2022 02:02:23 +0000 (11:02 +0900)
The watchdog API is implemented using C++ language.

Change-Id: Ia2e5cf910ca9cfab6f7926ea17a29cfe3265297d
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_watchdog.c [deleted file]
src/aul_watchdog.cc [new file with mode: 0644]

diff --git a/src/aul_watchdog.c b/src/aul_watchdog.c
deleted file mode 100644 (file)
index 7c1d084..0000000
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdbool.h>
-#include <glib.h>
-
-#include "aul_api.h"
-#include "aul_util.h"
-#include "aul_sock.h"
-#include "aul_error.h"
-#include "aul_watchdog.h"
-#include "aul.h"
-
-typedef struct watchdog_context_s {
-       bool enabled;
-       unsigned int interval;
-       guint timer;
-} watchdog_context;
-
-static watchdog_context __context;
-
-API int aul_watchdog_enable(void)
-{
-       int ret;
-
-       if (__context.enabled) {
-               _W("Watchdog is already enabled");
-               return AUL_R_OK;
-       }
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       WATCHDOG_ENABLE, NULL, 0, AUL_SOCK_NOREPLY);
-       if (ret < 0) {
-               _E("Failed to send the watchdog request. ret(%d)", ret);
-               return aul_error_convert(ret);
-       }
-
-       __context.enabled = true;
-       _D("[__WATCHDOG__] enabled, result(%d)", ret);
-       return AUL_R_OK;
-}
-
-API int aul_watchdog_disable(void)
-{
-       int ret;
-
-       if (!__context.enabled) {
-               _W("Watchdog is not enabled");
-               return AUL_R_ERROR;
-       }
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       WATCHDOG_DISABLE, NULL, 0, AUL_SOCK_NOREPLY);
-       if (ret < 0) {
-               _E("Failed to send the watchdog request. ret(%d)", ret);
-               return aul_error_convert(ret);
-       }
-
-       __context.enabled = false;
-       _D("[__WATCHDOG__] disabled, result(%d)", ret);
-       return AUL_R_OK;
-}
-
-API int aul_watchdog_kick(void)
-{
-       int ret;
-
-       if (!__context.enabled) {
-               _W("Watchdog is not enabled");
-               return AUL_R_ERROR;
-       }
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       WATCHDOG_KICK, NULL, 0, AUL_SOCK_NOREPLY);
-       if (ret < 0) {
-               _E("Failed to send the watchdog request. ret(%d)", ret);
-               return aul_error_convert(ret);
-       }
-
-       aul_watchdog_stop();
-       aul_watchdog_start(__context.interval);
-
-       _D("[__WATCHDOG__] kicked, result(%d)", ret);
-       return AUL_R_OK;
-}
-
-static int __watchdog_ping(void)
-{
-       int ret;
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       WATCHDOG_PING, NULL, 0, AUL_SOCK_NOREPLY);
-       if (ret < 0) {
-               _E("Failed to send watchdog ping. ret(%d)", ret);
-               return aul_error_convert(ret);
-       }
-
-       return AUL_R_OK;
-}
-
-static gboolean __watchdog_notify_handler(gpointer data)
-{
-       int ret;
-
-       ret = __watchdog_ping();
-       _W("[__WATCHDOG__] Ping(%d). result(%d)", getpid(), ret);
-
-       return G_SOURCE_CONTINUE;
-}
-
-void aul_watchdog_start(unsigned int interval)
-{
-       if (interval == 0) {
-               _E("Invalid parameter");
-               return;
-       }
-
-       if (__context.timer) {
-               _W("Timer already exists");
-               return;
-       }
-
-       __context.enabled = true;
-       __context.interval = interval;
-       __context.timer = g_timeout_add(interval,
-                       __watchdog_notify_handler, NULL);
-
-       __watchdog_notify_handler(NULL);
-}
-
-void aul_watchdog_stop(void)
-{
-       if (!__context.timer)
-               return;
-
-       g_source_remove(__context.timer);
-       __context.timer = 0;
-}
diff --git a/src/aul_watchdog.cc b/src/aul_watchdog.cc
new file mode 100644 (file)
index 0000000..ca6f748
--- /dev/null
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2018 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/aul_watchdog.h"
+
+#include <glib.h>
+
+#include "app_request.h"
+#include "aul_api.h"
+#include "aul_util.h"
+#include "include/aul.h"
+#include "include/aul_error.h"
+#include "aul/common/exception.hh"
+
+using namespace aul;
+
+namespace {
+using namespace aul::internal;
+
+class WatchdogContext {
+ public:
+  WatchdogContext() = default;
+
+  ~WatchdogContext() {
+    Stop();
+
+    if (enabled_)
+      Disable();
+  }
+
+  void Enable() {
+    if (enabled_) {
+      _W("Already enabled");
+      return;
+    }
+
+    int ret = AppRequest(WATCHDOG_ENABLE, getuid())
+        .SendCmdOnly(AUL_SOCK_NOREPLY);
+    if (ret < 0)
+      THROW(aul_error_convert(ret));
+
+    enabled_ = true;
+    _D("Enabled");
+  }
+
+  void Disable() {
+    if (!enabled_)
+      THROW(AUL_R_ERROR);
+
+    int ret = AppRequest(WATCHDOG_DISABLE, getuid())
+        .SendCmdOnly(AUL_SOCK_NOREPLY);
+    if (ret < 0)
+      THROW(aul_error_convert(ret));
+
+    enabled_ = false;
+    _D("Disabled");
+  }
+
+  void Kick() {
+    if (!enabled_)
+      THROW(AUL_R_ERROR);
+
+    int ret = AppRequest(WATCHDOG_KICK, getuid())
+        .SendCmdOnly(AUL_SOCK_NOREPLY);
+    if (ret < 0)
+      THROW(aul_error_convert(ret));
+
+    Stop();
+    Start(interval_);
+    _D("Kicked");
+  }
+
+  void Start(unsigned int interval) {
+    _D("Interval: %u", interval);
+    if (interval == 0) {
+      _E("Invalid parameter");
+      return;
+    }
+
+    if (timer_ != 0) {
+      _E("Already started");
+      return;
+    }
+
+    enabled_ = true;
+    interval_ = interval;
+    timer_ = g_timeout_add(interval,
+        [](gpointer user_data) -> gboolean {
+          auto* handle = static_cast<WatchdogContext*>(user_data);
+          try {
+            handle->Ping();
+          } catch (const Exception& e) {
+            _E("Exception occurs. error(%d)", e.GetErrorCode());
+          }
+
+          return G_SOURCE_CONTINUE;
+        }, this);
+  }
+
+  void Stop() {
+    if (timer_ == 0)
+      return;
+
+    g_source_remove(timer_);
+    timer_ = 0;
+  }
+
+  bool IsEnabled() const {
+    return enabled_;
+  }
+
+ private:
+  void Ping() {
+    int ret = AppRequest(WATCHDOG_PING, getuid())
+        .SendCmdOnly(AUL_SOCK_NOREPLY);
+    if (ret < 0)
+      THROW(aul_error_convert(ret));
+
+    _D("Ping");
+  }
+
+ private:
+  bool enabled_ = false;
+  unsigned int interval_ = 0;
+  guint timer_ = 0;
+};
+
+WatchdogContext context;
+
+}  // namespace
+
+extern "C" API int aul_watchdog_enable(void) {
+  try {
+    context.Enable();
+  } catch (const Exception& e) {
+    _E("Exception occurs. error(%d)", e.GetErrorCode());
+    return e.GetErrorCode();
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_watchdog_disable(void) {
+  try {
+    context.Disable();
+  } catch (const Exception& e) {
+    if (context.IsEnabled())
+      _E("Exception occurs. error(%d)", e.GetErrorCode());
+
+    return e.GetErrorCode();
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_watchdog_kick(void) {
+  try {
+    context.Kick();
+  } catch (const Exception& e) {
+    _E("Exception occurs. error(%d)", e.GetErrorCode());
+    return e.GetErrorCode();
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" void aul_watchdog_start(unsigned int interval) {
+  context.Start(interval);
+}
+
+extern "C" void aul_watchdog_stop(void) {
+  context.Stop();
+}