blob: e3da8cbb1937456e3625e3b90438e6daae9de743 [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -05001#include "interfaces/openbmc_intf.h"
Norman James9e6acf92015-09-08 07:00:04 -05002#include "openbmc.h"
Norman Jamescfc2b442015-10-31 17:31:46 -05003#include "object_mapper.h"
Norman James9e6acf92015-09-08 07:00:04 -05004
5/* ---------------------------------------------------------------------------------------------------- */
6
Norman James362a80f2015-09-14 14:04:39 -05007static const gchar* dbus_object_path = "/org/openbmc/watchdog";
Norman James8fee6f22015-10-28 12:48:43 -05008static const gchar* instance_name = "host0";
Norman James362a80f2015-09-14 14:04:39 -05009static const gchar* dbus_name = "org.openbmc.watchdog.Host";
Norman James9e6acf92015-09-08 07:00:04 -050010
11static GDBusObjectManagerServer *manager = NULL;
Adriana Kobylakdaea21d2015-10-22 12:21:56 -050012static guint watchdogid = 0;
Norman James9e6acf92015-09-08 07:00:04 -050013
14static gboolean
15poll_watchdog(gpointer user_data)
16{
17 Watchdog *watchdog = object_get_watchdog((Object*)user_data);
18
19 guint count = watchdog_get_watchdog(watchdog);
20 g_print("Polling watchdog: %d\n",count);
21
22 if (count == 0)
23 {
Norman James362a80f2015-09-14 14:04:39 -050024 //watchdog error, emit error and stop watchdog
Adriana Kobylakdaea21d2015-10-22 12:21:56 -050025 watchdogid = 0;
Norman James9e6acf92015-09-08 07:00:04 -050026 watchdog_emit_watchdog_error(watchdog);
Norman James362a80f2015-09-14 14:04:39 -050027 return FALSE;
Norman James9e6acf92015-09-08 07:00:04 -050028 }
29
30 //reset watchdog
31 watchdog_set_watchdog(watchdog,0);
32 return TRUE;
33}
34
35static gboolean
Adriana Kobylak5f7fe662016-02-04 12:39:06 -060036remove_watchdog(void)
37{
38 if (watchdogid)
39 {
40 g_source_remove(watchdogid);
41 watchdogid = 0;
42 }
43}
44
45static gboolean
Adriana Kobylak2cb27752015-10-19 16:23:14 -050046set_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);
54}
55
56static gboolean
Norman James9e6acf92015-09-08 07:00:04 -050057on_start (Watchdog *wd,
58 GDBusMethodInvocation *invocation,
59 gpointer user_data)
60{
Adriana Kobylak5f7fe662016-02-04 12:39:06 -060061 remove_watchdog();
Norman James362a80f2015-09-14 14:04:39 -050062 watchdog_set_watchdog(wd,1);
Norman James9e6acf92015-09-08 07:00:04 -050063 guint poll_interval = watchdog_get_poll_interval(wd);
Adriana Kobylak2cb27752015-10-19 16:23:14 -050064 g_print("Starting watchdog with poll interval: %d\n", poll_interval);
Adriana Kobylakdaea21d2015-10-22 12:21:56 -050065 watchdogid = g_timeout_add(poll_interval, poll_watchdog, user_data);
Norman James9e6acf92015-09-08 07:00:04 -050066 watchdog_complete_start(wd,invocation);
67 return TRUE;
68}
69
70static gboolean
71on_poke (Watchdog *wd,
72 GDBusMethodInvocation *invocation,
73 gpointer user_data)
74{
75 watchdog_set_watchdog(wd,1);
76 watchdog_complete_poke(wd,invocation);
77 return TRUE;
78}
79
Adriana Kobylakdaea21d2015-10-22 12:21:56 -050080static gboolean
81on_stop (Watchdog *wd,
82 GDBusMethodInvocation *invocation,
83 gpointer user_data)
84{
85 g_print("Stopping watchdog\n");
Adriana Kobylak5f7fe662016-02-04 12:39:06 -060086 remove_watchdog();
Adriana Kobylakdaea21d2015-10-22 12:21:56 -050087 watchdog_complete_stop(wd,invocation);
88 return TRUE;
89}
Norman James9e6acf92015-09-08 07:00:04 -050090
91static void
92on_bus_acquired (GDBusConnection *connection,
93 const gchar *name,
94 gpointer user_data)
95{
Norman James9e6acf92015-09-08 07:00:04 -050096 cmdline *cmd = user_data;
Norman James9e6acf92015-09-08 07:00:04 -050097 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman James9e6acf92015-09-08 07:00:04 -050098 gchar *s;
Norman James8fee6f22015-10-28 12:48:43 -050099 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
Norman James9e6acf92015-09-08 07:00:04 -0500100 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);
Norman Jamescfc2b442015-10-31 17:31:46 -0500106
107 ObjectMapper* mapper = object_mapper_skeleton_new ();
108 object_skeleton_set_object_mapper (object, mapper);
109 g_object_unref (mapper);
Norman James9e6acf92015-09-08 07:00:04 -0500110
111 // set properties
112 watchdog_set_watchdog(wd,1);
113
114 //define method callbacks here
115 g_signal_connect (wd,
116 "handle-start",
117 G_CALLBACK (on_start),
118 object); /* user_data */
119
120 g_signal_connect (wd,
121 "handle-poke",
122 G_CALLBACK (on_poke),
123 object); /* user_data */
124
Adriana Kobylak2cb27752015-10-19 16:23:14 -0500125 g_signal_connect (wd,
Adriana Kobylakdaea21d2015-10-22 12:21:56 -0500126 "handle-stop",
127 G_CALLBACK (on_stop),
128 object); /* user_data */
129
130 g_signal_connect (wd,
Adriana Kobylak2cb27752015-10-19 16:23:14 -0500131 "handle-set",
132 G_CALLBACK (set_poll_interval),
133 object); /* user_data */
Norman James9e6acf92015-09-08 07:00:04 -0500134
135 /* Export the object (@manager takes its own reference to @object) */
136 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
137 g_object_unref (object);
Norman James9e6acf92015-09-08 07:00:04 -0500138
139 /* Export all objects */
140 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamescfc2b442015-10-31 17:31:46 -0500141 emit_object_added((GDBusObjectManager*)manager);
Norman James9e6acf92015-09-08 07:00:04 -0500142}
143
144static void
145on_name_acquired (GDBusConnection *connection,
146 const gchar *name,
147 gpointer user_data)
148{
Norman James362a80f2015-09-14 14:04:39 -0500149 //g_print ("Acquired the name %s\n", name);
Norman James9e6acf92015-09-08 07:00:04 -0500150}
151
152static void
153on_name_lost (GDBusConnection *connection,
154 const gchar *name,
155 gpointer user_data)
156{
Norman James362a80f2015-09-14 14:04:39 -0500157 //g_print ("Lost the name %s\n", name);
Norman James9e6acf92015-09-08 07:00:04 -0500158}
159
160
161gint
162main (gint argc, gchar *argv[])
163{
164 GMainLoop *loop;
165 cmdline cmd;
166 cmd.argc = argc;
167 cmd.argv = argv;
168 guint id;
169 loop = g_main_loop_new (NULL, FALSE);
170
Norman James5e792e32015-10-07 17:36:17 -0500171 id = g_bus_own_name (DBUS_TYPE,
Norman James9e6acf92015-09-08 07:00:04 -0500172 dbus_name,
173 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
174 G_BUS_NAME_OWNER_FLAGS_REPLACE,
175 on_bus_acquired,
176 on_name_acquired,
177 on_name_lost,
178 &cmd,
179 NULL);
180
181 g_main_loop_run (loop);
182
183 g_bus_unown_name (id);
184 g_main_loop_unref (loop);
185 return 0;
186}