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