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