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;
|
| 11 |
|
| 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
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 24 | watchdog_emit_watchdog_error(watchdog);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 25 | return FALSE;
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 26 | }
|
| 27 |
|
| 28 | //reset watchdog
|
| 29 | watchdog_set_watchdog(watchdog,0);
|
| 30 | return TRUE;
|
| 31 | }
|
| 32 |
|
| 33 | static gboolean
|
| 34 | on_start (Watchdog *wd,
|
| 35 | GDBusMethodInvocation *invocation,
|
| 36 | gpointer user_data)
|
| 37 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 38 | watchdog_set_watchdog(wd,1);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 39 | 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 |
|
| 45 | static gboolean
|
| 46 | on_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 |
|
| 57 | static void
|
| 58 | on_bus_acquired (GDBusConnection *connection,
|
| 59 | const gchar *name,
|
| 60 | gpointer user_data)
|
| 61 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 62 | // g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 63 |
|
| 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 |
|
| 107 | static void
|
| 108 | on_name_acquired (GDBusConnection *connection,
|
| 109 | const gchar *name,
|
| 110 | gpointer user_data)
|
| 111 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 112 | //g_print ("Acquired the name %s\n", name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 113 | }
|
| 114 |
|
| 115 | static void
|
| 116 | on_name_lost (GDBusConnection *connection,
|
| 117 | const gchar *name,
|
| 118 | gpointer user_data)
|
| 119 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 120 | //g_print ("Lost the name %s\n", name);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 121 | }
|
| 122 |
|
| 123 |
|
| 124 | gint
|
| 125 | main (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 | }
|