blob: e922dc304722bcb5677c411e281e7af01704dacc [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include "interfaces/openbmc_intf.h"
2#include "openbmc.h"
Brad Bishop77390492016-04-13 10:47:19 -04003
4/* ------------------------------------------------------------------------- */
5
6static const gchar* dbus_object_path = "/org/openbmc/watchdog";
7static const gchar* instance_name = "host0";
8static const gchar* dbus_name = "org.openbmc.watchdog.Host";
9
10static GDBusObjectManagerServer *manager = NULL;
11static guint watchdogid = 0;
12
13static gboolean
14poll_watchdog(gpointer user_data)
15{
16 Watchdog *watchdog = object_get_watchdog((Object*)user_data);
17
18 guint count = watchdog_get_watchdog(watchdog);
19 g_print("Polling watchdog: %d\n",count);
20
21 if(count == 0)
22 {
23 //watchdog error, emit error and stop watchdog
24 watchdogid = 0;
25 watchdog_emit_watchdog_error(watchdog);
26 return FALSE;
27 }
28
29 //reset watchdog
30 watchdog_set_watchdog(watchdog,0);
31 return TRUE;
32}
33
34static gboolean
35remove_watchdog(void)
36{
37 if(watchdogid)
38 {
39 g_source_remove(watchdogid);
40 watchdogid = 0;
41 }
Brad Bishop0c82c602016-04-13 13:36:49 -040042 return TRUE;
Brad Bishop77390492016-04-13 10:47:19 -040043}
44
45static gboolean
46set_poll_interval(Watchdog *wd,
47 GDBusMethodInvocation *invocation,
48 guint interval,
49 gpointer user_data)
50{
51 g_print("Setting watchdog poll interval to: %d\n", interval);
52 watchdog_set_poll_interval(wd, interval);
53 watchdog_complete_set(wd,invocation);
Brad Bishop0c82c602016-04-13 13:36:49 -040054 return TRUE;
Brad Bishop77390492016-04-13 10:47:19 -040055}
56
57static gboolean
58on_start(Watchdog *wd,
59 GDBusMethodInvocation *invocation,
60 gpointer user_data)
61{
62 remove_watchdog();
63 watchdog_set_watchdog(wd,0);
64 guint poll_interval = watchdog_get_poll_interval(wd);
65 g_print("Starting watchdog with poll interval: %d\n", poll_interval);
66 watchdogid = g_timeout_add(poll_interval, poll_watchdog, user_data);
67 watchdog_complete_start(wd,invocation);
68 return TRUE;
69}
70
71static gboolean
72on_poke(Watchdog *wd,
73 GDBusMethodInvocation *invocation,
74 gpointer user_data)
75{
76 watchdog_set_watchdog(wd,1);
77 watchdog_complete_poke(wd,invocation);
78 return TRUE;
79}
80
81static gboolean
82on_stop(Watchdog *wd,
83 GDBusMethodInvocation *invocation,
84 gpointer user_data)
85{
86 g_print("Stopping watchdog\n");
87 remove_watchdog();
88 watchdog_complete_stop(wd,invocation);
89 return TRUE;
90}
91
92static void
93on_bus_acquired(GDBusConnection *connection,
94 const gchar *name,
95 gpointer user_data)
96{
Brad Bishop77390492016-04-13 10:47:19 -040097 manager = g_dbus_object_manager_server_new(dbus_object_path);
98 gchar *s;
99 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
100 ObjectSkeleton *object = object_skeleton_new(s);
101 g_free(s);
102
103 Watchdog *wd = watchdog_skeleton_new();
104 object_skeleton_set_watchdog(object, wd);
105 g_object_unref(wd);
106
Brad Bishop77390492016-04-13 10:47:19 -0400107 // set properties
108 watchdog_set_watchdog(wd,1);
109
110 //define method callbacks here
111 g_signal_connect(wd,
112 "handle-start",
113 G_CALLBACK(on_start),
114 object); /* user_data */
115
116 g_signal_connect(wd,
117 "handle-poke",
118 G_CALLBACK(on_poke),
119 object); /* user_data */
120
121 g_signal_connect(wd,
122 "handle-stop",
123 G_CALLBACK(on_stop),
124 object); /* user_data */
125
126 g_signal_connect(wd,
127 "handle-set",
128 G_CALLBACK(set_poll_interval),
129 object); /* user_data */
130
131 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400132 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400133 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
134 g_object_unref(object);
Brad Bishop77390492016-04-13 10:47:19 -0400135}
136
137static void
138on_name_acquired(GDBusConnection *connection,
139 const gchar *name,
140 gpointer user_data)
141{
142 //g_print ("Acquired the name %s\n", name);
143}
144
145static void
146on_name_lost(GDBusConnection *connection,
147 const gchar *name,
148 gpointer user_data)
149{
150 //g_print ("Lost the name %s\n", name);
151}
152
153
154gint
155main(gint argc, gchar *argv[])
156{
157 GMainLoop *loop;
158 cmdline cmd;
159 cmd.argc = argc;
160 cmd.argv = argv;
161 guint id;
162 loop = g_main_loop_new(NULL, FALSE);
163
164 id = g_bus_own_name(DBUS_TYPE,
165 dbus_name,
166 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
167 G_BUS_NAME_OWNER_FLAGS_REPLACE,
168 on_bus_acquired,
169 on_name_acquired,
170 on_name_lost,
171 &cmd,
172 NULL);
173
174 g_main_loop_run(loop);
175
176 g_bus_unown_name(id);
177 g_main_loop_unref(loop);
178 return 0;
179}