Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 4 | #include <openbmc_intf.h> |
| 5 | #include <openbmc.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 6 | |
| 7 | /* ------------------------------------------------------------------------- */ |
| 8 | static const gchar* dbus_object_path = "/org/openbmc/control"; |
| 9 | static const gchar* instance_name = "bmc0"; |
| 10 | static const gchar* dbus_name = "org.openbmc.control.Bmc"; |
| 11 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 12 | static GDBusObjectManagerServer *manager = NULL; |
| 13 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 14 | static gboolean on_init(Control *control, GDBusMethodInvocation *invocation, |
| 15 | gpointer user_data) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 16 | { |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 17 | control_complete_init(control, invocation); |
| 18 | return TRUE; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 19 | } |
| 20 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 21 | static gboolean on_warm_reset(ControlBmc *bmc, |
| 22 | GDBusMethodInvocation *invocation, |
| 23 | gpointer user_data) |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 24 | { |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 25 | /* Wait a while before reboot, so the caller can be responded. |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 26 | */ |
Andrew Geissler | dfda936 | 2016-11-17 13:43:37 -0600 | [diff] [blame] | 27 | const char *reboot_command = "/bin/sh -c 'sleep 3;reboot'&"; |
Ed Tanous | 81100ae | 2021-01-28 15:25:26 -0800 | [diff] [blame] | 28 | if(system(reboot_command) < 0){ |
| 29 | return FALSE; |
| 30 | } |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 31 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 32 | control_bmc_complete_warm_reset(bmc, invocation); |
| 33 | return TRUE; |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 34 | } |
| 35 | |
Nan Li | 44dd5a6 | 2016-08-24 17:13:06 +0800 | [diff] [blame] | 36 | static gboolean on_cold_reset(ControlBmc *bmc, |
| 37 | GDBusMethodInvocation *invocation, |
| 38 | gpointer user_data) |
| 39 | { |
| 40 | GError *err = NULL; |
| 41 | /* Wait a while before reboot, so the caller can be responded. |
| 42 | * Note that g_spawn_command_line_async() cannot parse ';' as |
| 43 | * a command separator. Need to use 'sh -c' to let shell parse it. |
| 44 | */ |
| 45 | gchar *reboot_command = "/bin/sh -c 'sleep 3;reboot'"; |
| 46 | |
| 47 | g_spawn_command_line_async(reboot_command, &err); |
| 48 | if(err != NULL) { |
| 49 | fprintf(stderr, "coldReset() error: %s\n", err->message); |
| 50 | g_error_free(err); |
| 51 | } |
| 52 | |
| 53 | control_bmc_complete_cold_reset(bmc, invocation); |
| 54 | return TRUE; |
| 55 | } |
| 56 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 57 | static void on_bus_acquired(GDBusConnection *connection, const gchar *name, |
| 58 | gpointer user_data) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 59 | { |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 60 | ObjectSkeleton *object; |
| 61 | cmdline *cmd = user_data; |
| 62 | manager = g_dbus_object_manager_server_new(dbus_object_path); |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 63 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 64 | gchar *s; |
| 65 | s = g_strdup_printf("%s/%s", dbus_object_path, instance_name); |
| 66 | object = object_skeleton_new(s); |
| 67 | g_free(s); |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 68 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 69 | ControlBmc* control_bmc = control_bmc_skeleton_new(); |
| 70 | object_skeleton_set_control_bmc(object, control_bmc); |
| 71 | g_object_unref(control_bmc); |
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 72 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 73 | Control* control = control_skeleton_new(); |
| 74 | object_skeleton_set_control(object, control); |
| 75 | g_object_unref(control); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 76 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 77 | //define method callbacks here |
| 78 | g_signal_connect(control, "handle-init", G_CALLBACK(on_init), NULL); /* user_data */ |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 79 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 80 | g_signal_connect(control_bmc, "handle-warm-reset", |
| 81 | G_CALLBACK(on_warm_reset), NULL); /* user_data */ |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 82 | |
Nan Li | 44dd5a6 | 2016-08-24 17:13:06 +0800 | [diff] [blame] | 83 | g_signal_connect(control_bmc, "handle-cold-reset", |
| 84 | G_CALLBACK(on_cold_reset), NULL); /* user_data */ |
| 85 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 86 | /* Export the object (@manager takes its own reference to @object) */ |
| 87 | g_dbus_object_manager_server_export(manager, |
| 88 | G_DBUS_OBJECT_SKELETON(object)); |
| 89 | g_object_unref(object); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 90 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 91 | /* Export all objects */ |
| 92 | g_dbus_object_manager_server_set_connection(manager, connection); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 93 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 94 | cmd->user_data = object; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 97 | static void on_name_acquired(GDBusConnection *connection, const gchar *name, |
| 98 | gpointer user_data) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 102 | static void on_name_lost(GDBusConnection *connection, const gchar *name, |
| 103 | gpointer user_data) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
| 107 | /*----------------------------------------------------------------*/ |
| 108 | /* Main Event Loop */ |
| 109 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 110 | gint main(gint argc, gchar *argv[]) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 111 | { |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 112 | GMainLoop *loop; |
| 113 | cmdline cmd; |
| 114 | cmd.argc = argc; |
| 115 | cmd.argv = argv; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 116 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 117 | guint id; |
| 118 | loop = g_main_loop_new(NULL, FALSE); |
| 119 | cmd.loop = loop; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 120 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 121 | id = g_bus_own_name( |
| 122 | DBUS_TYPE, |
| 123 | dbus_name, |
| 124 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
| 125 | | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 126 | on_bus_acquired, on_name_acquired, on_name_lost, &cmd, NULL); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 127 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 128 | g_main_loop_run(loop); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 129 | |
Andrew Geissler | eb27fa9 | 2016-09-29 16:59:36 -0500 | [diff] [blame] | 130 | g_bus_unown_name(id); |
| 131 | g_main_loop_unref(loop); |
| 132 | return 0; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 133 | } |