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