2 * Copyright (c) 2018 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.
25 #include "aul_error.h"
26 #include "aul_watchdog.h"
29 typedef struct watchdog_context_s {
31 unsigned int interval;
35 static watchdog_context __context;
37 API int aul_watchdog_enable(void)
41 if (__context.enabled) {
42 _W("Watchdog is already enabled");
46 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
47 WATCHDOG_ENABLE, NULL, 0, AUL_SOCK_NOREPLY);
49 _E("Failed to send the watchdog request. ret(%d)", ret);
50 return aul_error_convert(ret);
53 __context.enabled = true;
54 _D("[__WATCHDOG__] enabled, result(%d)", ret);
58 API int aul_watchdog_disable(void)
62 if (!__context.enabled) {
63 _W("Watchdog is not enabled");
67 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
68 WATCHDOG_DISABLE, NULL, 0, AUL_SOCK_NOREPLY);
70 _E("Failed to send the watchdog request. ret(%d)", ret);
71 return aul_error_convert(ret);
74 __context.enabled = false;
75 _D("[__WATCHDOG__] disabled, result(%d)", ret);
79 API int aul_watchdog_kick(void)
83 if (!__context.enabled) {
84 _W("Watchdog is not enabled");
88 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
89 WATCHDOG_KICK, NULL, 0, AUL_SOCK_NOREPLY);
91 _E("Failed to send the watchdog request. ret(%d)", ret);
92 return aul_error_convert(ret);
96 aul_watchdog_start(__context.interval);
98 _D("[__WATCHDOG__] kicked, result(%d)", ret);
102 static int __watchdog_ping(void)
106 ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
107 WATCHDOG_PING, NULL, 0, AUL_SOCK_NOREPLY);
109 _E("Failed to send watchdog ping. ret(%d)", ret);
110 return aul_error_convert(ret);
116 static gboolean __watchdog_notify_handler(gpointer data)
120 ret = __watchdog_ping();
121 _W("[__WATCHDOG__] Ping(%d). result(%d)", getpid(), ret);
123 return G_SOURCE_CONTINUE;
126 void aul_watchdog_start(unsigned int interval)
129 _E("Invalid parameter");
133 if (__context.timer) {
134 _W("Timer already exists");
138 __context.enabled = true;
139 __context.interval = interval;
140 __context.timer = g_timeout_add(interval,
141 __watchdog_notify_handler, NULL);
143 __watchdog_notify_handler(NULL);
146 void aul_watchdog_stop(void)
148 if (!__context.timer)
151 g_source_remove(__context.timer);