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";
|
| 8 | static const gchar* dbus_name = "org.openbmc.watchdog.Host";
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 9 |
|
| 10 | static GDBusObjectManagerServer *manager = NULL;
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 11 | static guint watchdogid = 0;
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 12 |
|
| 13 | static gboolean
|
| 14 | poll_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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 23 | //watchdog error, emit error and stop watchdog
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 24 | watchdogid = 0;
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 25 | watchdog_emit_watchdog_error(watchdog);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 26 | return FALSE;
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 27 | }
|
| 28 |
|
| 29 | //reset watchdog
|
| 30 | watchdog_set_watchdog(watchdog,0);
|
| 31 | return TRUE;
|
| 32 | }
|
| 33 |
|
| 34 | static gboolean
|
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 35 | set_poll_interval (Watchdog *wd,
|
| 36 | GDBusMethodInvocation *invocation,
|
| 37 | guint interval,
|
| 38 | gpointer user_data)
|
| 39 | {
|
| 40 | g_print("Setting watchdog poll interval to: %d\n", interval);
|
| 41 | watchdog_set_poll_interval(wd, interval);
|
| 42 | watchdog_complete_set(wd,invocation);
|
| 43 | }
|
| 44 |
|
| 45 | static gboolean
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 46 | on_start (Watchdog *wd,
|
| 47 | GDBusMethodInvocation *invocation,
|
| 48 | gpointer user_data)
|
| 49 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 50 | watchdog_set_watchdog(wd,1);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 51 | guint poll_interval = watchdog_get_poll_interval(wd);
|
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 52 | g_print("Starting watchdog with poll interval: %d\n", poll_interval);
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 53 | watchdogid = g_timeout_add(poll_interval, poll_watchdog, user_data);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 54 | watchdog_complete_start(wd,invocation);
|
| 55 | return TRUE;
|
| 56 | }
|
| 57 |
|
| 58 | static gboolean
|
| 59 | on_poke (Watchdog *wd,
|
| 60 | GDBusMethodInvocation *invocation,
|
| 61 | gpointer user_data)
|
| 62 | {
|
| 63 | watchdog_set_watchdog(wd,1);
|
| 64 | watchdog_complete_poke(wd,invocation);
|
| 65 | return TRUE;
|
| 66 | }
|
| 67 |
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 68 | static gboolean
|
| 69 | on_stop (Watchdog *wd,
|
| 70 | GDBusMethodInvocation *invocation,
|
| 71 | gpointer user_data)
|
| 72 | {
|
| 73 | g_print("Stopping watchdog\n");
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 74 |
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 75 | if (watchdogid)
|
| 76 | {
|
| 77 | g_source_remove(watchdogid);
|
| 78 | watchdogid = 0;
|
| 79 | }
|
| 80 |
|
| 81 | watchdog_complete_stop(wd,invocation);
|
| 82 | return TRUE;
|
| 83 | }
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 84 |
|
| 85 | static void
|
| 86 | on_bus_acquired (GDBusConnection *connection,
|
| 87 | const gchar *name,
|
| 88 | gpointer user_data)
|
| 89 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 90 | // g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 91 |
|
| 92 | cmdline *cmd = user_data;
|
| 93 | if (cmd->argc < 2)
|
| 94 | {
|
| 95 | g_print("No objects created. Put object name(s) on command line\n");
|
| 96 | return;
|
| 97 | }
|
| 98 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 99 | int i=0;
|
| 100 | for (i=1;i<cmd->argc;i++)
|
| 101 | {
|
| 102 | gchar *s;
|
| 103 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
|
| 104 | ObjectSkeleton *object = object_skeleton_new (s);
|
| 105 | g_free (s);
|
| 106 |
|
| 107 | Watchdog *wd = watchdog_skeleton_new ();
|
| 108 | object_skeleton_set_watchdog (object, wd);
|
| 109 | g_object_unref (wd);
|
| 110 |
|
| 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 Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 125 | g_signal_connect (wd,
|
Adriana Kobylak | daea21d | 2015-10-22 12:21:56 -0500 | [diff] [blame] | 126 | "handle-stop",
|
| 127 | G_CALLBACK (on_stop),
|
| 128 | object); /* user_data */
|
| 129 |
|
| 130 | g_signal_connect (wd,
|
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 131 | "handle-set",
|
| 132 | G_CALLBACK (set_poll_interval),
|
| 133 | object); /* user_data */
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 134 |
|
| 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);
|
| 138 | }
|
| 139 |
|
| 140 | /* Export all objects */
|
| 141 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 142 | }
|
| 143 |
|
| 144 | static void
|
| 145 | on_name_acquired (GDBusConnection *connection,
|
| 146 | const gchar *name,
|
| 147 | gpointer user_data)
|
| 148 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 149 | //g_print ("Acquired the name %s\n", name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 150 | }
|
| 151 |
|
| 152 | static void
|
| 153 | on_name_lost (GDBusConnection *connection,
|
| 154 | const gchar *name,
|
| 155 | gpointer user_data)
|
| 156 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 157 | //g_print ("Lost the name %s\n", name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 158 | }
|
| 159 |
|
| 160 |
|
| 161 | gint
|
| 162 | main (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 James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 171 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 172 | 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 | }
|