Fix wathcdog macro name
[platform/core/system/argos_watchdog.git] / src / argos_dbus.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common.h"
18 #include <stdlib.h>
19 #include <glib.h>
20 #include <gio/gio.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #define RESOURCED_DBUS_BUS_NAME        "org.tizen.resourced"
25 #define RESOURCED_PATH_WATCHDOG         "/Org/Tizen/ResourceD/Watchdog"
26 #define RESOURCED_INTERFACE_WATCHDOG    "org.tizen.resourced.watchdog"
27 #define WATCHDOG_START                 "Start"
28
29 static unsigned int dbus_watchdog_timeout=0;
30
31 static int aw_send_watchdog(unsigned int timeout)
32 {
33         int ret = 0;
34         static GDBusConnection *g_dbus_conn = NULL;
35         GVariant *param = NULL;
36
37         if (!g_dbus_conn)
38                 g_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
39
40         if (!g_dbus_conn)
41                 return -EINVAL;
42
43         param = g_variant_new("(iu)", getpid(), timeout);
44         g_dbus_connection_call(g_dbus_conn, RESOURCED_DBUS_BUS_NAME,
45                         RESOURCED_PATH_WATCHDOG, RESOURCED_INTERFACE_WATCHDOG, WATCHDOG_START,
46                         param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
47
48         return ret;
49 }
50
51 int aw_register_dbus(unsigned int timeout)
52 {
53         int ret = 0;
54
55         if (timeout == 0)
56                 return -EINVAL;
57
58         aw_send_watchdog(timeout);
59         dbus_watchdog_timeout = timeout;
60         return ret;
61 }
62
63 int aw_control_dbus(aw_op_e op, void *data)
64 {
65         int ret = 0;
66         unsigned int* timeout;
67
68         switch(op){
69         case AW_OP_DISABLE:
70                 ret = aw_send_watchdog(0);
71                 break;
72
73         case AW_OP_ENABLE:
74                 ret = aw_send_watchdog(dbus_watchdog_timeout);
75                 break;
76
77         case AW_OP_CHANGE_TIMEOUT:
78                 timeout = (unsigned int*)(data);
79                 if(timeout == NULL){
80                         ret = -1;
81                         break;
82                 }
83                 dbus_watchdog_timeout = *timeout;
84                 ret = aw_send_watchdog(*timeout);
85                 break;
86         default:
87                 ret = -1;
88                 break;
89         }
90
91         return ret;
92 }
93
94 int aw_notify_dbus(void)
95 {
96         return aw_send_watchdog(dbus_watchdog_timeout);
97 }
98