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