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