#include "key.h"
#include "aul_watch_control_internal.h"
#include "aul_worker.h"
+#include "aul_watchdog.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
__invoke_aul_handler(AUL_UPDATE_REQUESTED, req->b);
}
-static void __dispatch_watchdog_ping(aul_request_h req)
-{
- const char *start_time;
- struct timeval tv;
-
- gettimeofday(&tv, NULL);
- start_time = bundle_get_val(req->b, AUL_K_STARTTIME);
- _W("[__WATCHDOG__] Start time: %s, response time: %ld/%ld",
- start_time ? start_time : "Unknown",
- tv.tv_sec, tv.tv_usec);
-}
-
static void __dispatch_app_term_inst(aul_request_h req)
{
__invoke_aul_handler(AUL_TERMINATE_INST, req->b);
__invoke_aul_handler(AUL_TERMINATE_BG_INST, req->b);
}
+static void __dispatch_watchdog_enable(aul_request_h req)
+{
+ const char *interval_str;
+ unsigned int interval;
+
+ interval_str = bundle_get_val(req->b, AUL_K_INTERVAL);
+ if (!interval_str) {
+ _E("Invalid request");
+ return;
+ }
+
+ interval = strtoul(interval_str, NULL, 10);
+ aul_watchdog_start(interval);
+}
+
+static void __dispatch_watchdog_disable(aul_request_h req)
+{
+ aul_watchdog_stop();
+}
+
static dispatcher __dispatcher[] = {
[APP_START] = __dispatch_app_start,
[APP_START_RES] = __dispatch_app_start,
[APP_SUSPEND] = __dispatch_app_suspend,
[WIDGET_GET_CONTENT] = __dispatch_widget_get_content,
[APP_UPDATE_REQUESTED] = __dispatch_app_update_requested,
- [WATCHDOG_PING] = __dispatch_watchdog_ping,
[APP_SEND_LAUNCH_REQUEST] = __dispatch_app_start,
[APP_SEND_LAUNCH_REQUEST_SYNC] = __dispatch_app_start,
[APP_TERM_INSTANCE_ASYNC] = __dispatch_app_term_inst,
[APP_RESUME_INSTANCE] = __dispatch_app_resume_inst,
[APP_PAUSE_INSTANCE] = __dispatch_app_pause_inst,
[APP_TERM_BG_INSTANCE] = __dispatch_app_term_bg_inst,
+ [WATCHDOG_ENABLE] = __dispatch_watchdog_enable,
+ [WATCHDOG_DISABLE] = __dispatch_watchdog_disable,
};
static void __destroy_request(struct aul_request_s *req)
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 r;
+ int ret;
if (__context.enabled) {
_W("Watchdog is already enabled");
return AUL_R_OK;
}
- r = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
- WATCHDOG_ENABLE, NULL, 0, AUL_SOCK_NONE);
- if (r < 0) {
- _E("Failed to send the watchdog request. ret(%d)", r);
- return aul_error_convert(r);
+ 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)", r);
+ _D("[__WATCHDOG__] enabled, result(%d)", ret);
return AUL_R_OK;
}
API int aul_watchdog_disable(void)
{
- int r;
+ int ret;
if (!__context.enabled) {
_W("Watchdog is not enabled");
return AUL_R_ERROR;
}
- r = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
- WATCHDOG_DISABLE, NULL, 0, AUL_SOCK_NONE);
- if (r < 0) {
- _E("Failed to send the watchdog request. ret(%d)", r);
- return aul_error_convert(r);
+ 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)", r);
+ _D("[__WATCHDOG__] disabled, result(%d)", ret);
return AUL_R_OK;
}
API int aul_watchdog_kick(void)
{
- int r;
+ int ret;
if (!__context.enabled) {
_W("Watchdog is not enabled");
return AUL_R_ERROR;
}
- r = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
- WATCHDOG_KICK, NULL, 0, AUL_SOCK_NONE);
- if (r < 0) {
- _E("Failed to send the watchdog request. ret(%d)", r);
- return aul_error_convert(r);
+ 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);
}
- _D("[__WATCHDOG__] kicked, result(%d)", r);
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 (__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;
+}