2 * Copyright (c) 2018 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "include/aul_watchdog.h"
21 #include "app_request.h"
24 #include "include/aul.h"
25 #include "include/aul_error.h"
26 #include "aul/common/exception.hh"
31 using namespace aul::internal;
33 class WatchdogContext {
35 WatchdogContext() = default;
43 } catch (const Exception& e) {
44 _E("Exception occurs. error(%d)", e.GetErrorCode());
51 _W("Already enabled");
55 int ret = AppRequest(WATCHDOG_ENABLE, getuid())
56 .SendCmdOnly(AUL_SOCK_NOREPLY);
58 THROW(aul_error_convert(ret));
68 int ret = AppRequest(WATCHDOG_DISABLE, getuid())
69 .SendCmdOnly(AUL_SOCK_NOREPLY);
71 THROW(aul_error_convert(ret));
81 int ret = AppRequest(WATCHDOG_KICK, getuid())
82 .SendCmdOnly(AUL_SOCK_NOREPLY);
84 THROW(aul_error_convert(ret));
91 void Start(unsigned int interval) {
92 _D("Interval: %u", interval);
94 _E("Invalid parameter");
99 _E("Already started");
104 interval_ = interval;
105 timer_ = g_timeout_add(interval,
106 [](gpointer user_data) -> gboolean {
107 auto* handle = static_cast<WatchdogContext*>(user_data);
110 } catch (const Exception& e) {
111 _E("Exception occurs. error(%d)", e.GetErrorCode());
114 return G_SOURCE_CONTINUE;
122 g_source_remove(timer_);
126 bool IsEnabled() const {
132 int ret = AppRequest(WATCHDOG_PING, getuid())
133 .SendCmdOnly(AUL_SOCK_NOREPLY);
135 THROW(aul_error_convert(ret));
141 bool enabled_ = false;
142 unsigned int interval_ = 0;
146 WatchdogContext context;
150 extern "C" API int aul_watchdog_enable(void) {
153 } catch (const Exception& e) {
154 _E("Exception occurs. error(%d)", e.GetErrorCode());
155 return e.GetErrorCode();
161 extern "C" API int aul_watchdog_disable(void) {
164 } catch (const Exception& e) {
165 if (context.IsEnabled())
166 _E("Exception occurs. error(%d)", e.GetErrorCode());
168 return e.GetErrorCode();
174 extern "C" API int aul_watchdog_kick(void) {
177 } catch (const Exception& e) {
178 _E("Exception occurs. error(%d)", e.GetErrorCode());
179 return e.GetErrorCode();
185 extern "C" void aul_watchdog_start(unsigned int interval) {
186 context.Start(interval);
189 extern "C" void aul_watchdog_stop(void) {