blob: d038e15854c678c4b14a6f24e7ab46f5027f5730 [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"
3
4
5/* ---------------------------------------------------------------------------------------------------- */
6
Norman James362a80f2015-09-14 14:04:39 -05007static const gchar* dbus_object_path = "/org/openbmc/watchdog";
8static const gchar* dbus_name = "org.openbmc.watchdog.Host";
Norman James9e6acf92015-09-08 07:00:04 -05009
10static GDBusObjectManagerServer *manager = NULL;
11
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 {
Norman James362a80f2015-09-14 14:04:39 -050023 //watchdog error, emit error and stop watchdog
Norman James9e6acf92015-09-08 07:00:04 -050024 watchdog_emit_watchdog_error(watchdog);
Norman James362a80f2015-09-14 14:04:39 -050025 return FALSE;
Norman James9e6acf92015-09-08 07:00:04 -050026 }
27
28 //reset watchdog
29 watchdog_set_watchdog(watchdog,0);
30 return TRUE;
31}
32
33static gboolean
34on_start (Watchdog *wd,
35 GDBusMethodInvocation *invocation,
36 gpointer user_data)
37{
Norman James362a80f2015-09-14 14:04:39 -050038 watchdog_set_watchdog(wd,1);
Norman James9e6acf92015-09-08 07:00:04 -050039 guint poll_interval = watchdog_get_poll_interval(wd);
40 g_timeout_add(poll_interval, poll_watchdog, user_data);
41 watchdog_complete_start(wd,invocation);
42 return TRUE;
43}
44
45static gboolean
46on_poke (Watchdog *wd,
47 GDBusMethodInvocation *invocation,
48 gpointer user_data)
49{
50 watchdog_set_watchdog(wd,1);
51 watchdog_complete_poke(wd,invocation);
52 return TRUE;
53}
54
55
56
57static void
58on_bus_acquired (GDBusConnection *connection,
59 const gchar *name,
60 gpointer user_data)
61{
Norman James362a80f2015-09-14 14:04:39 -050062 // g_print ("Acquired a message bus connection: %s\n",name);
Norman James9e6acf92015-09-08 07:00:04 -050063
64 cmdline *cmd = user_data;
65 if (cmd->argc < 2)
66 {
67 g_print("No objects created. Put object name(s) on command line\n");
68 return;
69 }
70 manager = g_dbus_object_manager_server_new (dbus_object_path);
71 int i=0;
72 for (i=1;i<cmd->argc;i++)
73 {
74 gchar *s;
75 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
76 ObjectSkeleton *object = object_skeleton_new (s);
77 g_free (s);
78
79 Watchdog *wd = watchdog_skeleton_new ();
80 object_skeleton_set_watchdog (object, wd);
81 g_object_unref (wd);
82
83 // set properties
84 watchdog_set_watchdog(wd,1);
85
86 //define method callbacks here
87 g_signal_connect (wd,
88 "handle-start",
89 G_CALLBACK (on_start),
90 object); /* user_data */
91
92 g_signal_connect (wd,
93 "handle-poke",
94 G_CALLBACK (on_poke),
95 object); /* user_data */
96
97
98 /* Export the object (@manager takes its own reference to @object) */
99 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
100 g_object_unref (object);
101 }
102
103 /* Export all objects */
104 g_dbus_object_manager_server_set_connection (manager, connection);
105}
106
107static void
108on_name_acquired (GDBusConnection *connection,
109 const gchar *name,
110 gpointer user_data)
111{
Norman James362a80f2015-09-14 14:04:39 -0500112 //g_print ("Acquired the name %s\n", name);
Norman James9e6acf92015-09-08 07:00:04 -0500113}
114
115static void
116on_name_lost (GDBusConnection *connection,
117 const gchar *name,
118 gpointer user_data)
119{
Norman James362a80f2015-09-14 14:04:39 -0500120 //g_print ("Lost the name %s\n", name);
Norman James9e6acf92015-09-08 07:00:04 -0500121}
122
123
124gint
125main (gint argc, gchar *argv[])
126{
127 GMainLoop *loop;
128 cmdline cmd;
129 cmd.argc = argc;
130 cmd.argv = argv;
131 guint id;
132 loop = g_main_loop_new (NULL, FALSE);
133
134 id = g_bus_own_name (G_BUS_TYPE_SESSION,
135 dbus_name,
136 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
137 G_BUS_NAME_OWNER_FLAGS_REPLACE,
138 on_bus_acquired,
139 on_name_acquired,
140 on_name_lost,
141 &cmd,
142 NULL);
143
144 g_main_loop_run (loop);
145
146 g_bus_unown_name (id);
147 g_main_loop_unref (loop);
148 return 0;
149}