1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <openbmc_intf.h> 5 #include <openbmc.h> 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 12 static GDBusObjectManagerServer *manager = NULL; 13 14 static gboolean on_init(Control *control, GDBusMethodInvocation *invocation, 15 gpointer user_data) 16 { 17 control_complete_init(control, invocation); 18 return TRUE; 19 } 20 21 static gboolean on_warm_reset(ControlBmc *bmc, 22 GDBusMethodInvocation *invocation, 23 gpointer user_data) 24 { 25 /* Wait a while before reboot, so the caller can be responded. 26 */ 27 const char *reboot_command = "/bin/sh -c 'sleep 3;reboot'&"; 28 system(reboot_command); 29 30 control_bmc_complete_warm_reset(bmc, invocation); 31 return TRUE; 32 } 33 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 55 static void on_bus_acquired(GDBusConnection *connection, const gchar *name, 56 gpointer user_data) 57 { 58 ObjectSkeleton *object; 59 cmdline *cmd = user_data; 60 manager = g_dbus_object_manager_server_new(dbus_object_path); 61 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); 66 67 ControlBmc* control_bmc = control_bmc_skeleton_new(); 68 object_skeleton_set_control_bmc(object, control_bmc); 69 g_object_unref(control_bmc); 70 71 Control* control = control_skeleton_new(); 72 object_skeleton_set_control(object, control); 73 g_object_unref(control); 74 75 //define method callbacks here 76 g_signal_connect(control, "handle-init", G_CALLBACK(on_init), NULL); /* user_data */ 77 78 g_signal_connect(control_bmc, "handle-warm-reset", 79 G_CALLBACK(on_warm_reset), NULL); /* user_data */ 80 81 g_signal_connect(control_bmc, "handle-cold-reset", 82 G_CALLBACK(on_cold_reset), NULL); /* user_data */ 83 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); 88 89 /* Export all objects */ 90 g_dbus_object_manager_server_set_connection(manager, connection); 91 92 cmd->user_data = object; 93 } 94 95 static void on_name_acquired(GDBusConnection *connection, const gchar *name, 96 gpointer user_data) 97 { 98 } 99 100 static void on_name_lost(GDBusConnection *connection, const gchar *name, 101 gpointer user_data) 102 { 103 } 104 105 /*----------------------------------------------------------------*/ 106 /* Main Event Loop */ 107 108 gint main(gint argc, gchar *argv[]) 109 { 110 GMainLoop *loop; 111 cmdline cmd; 112 cmd.argc = argc; 113 cmd.argv = argv; 114 115 guint id; 116 loop = g_main_loop_new(NULL, FALSE); 117 cmd.loop = loop; 118 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); 125 126 g_main_loop_run(loop); 127 128 g_bus_unown_name(id); 129 g_main_loop_unref(loop); 130 return 0; 131 } 132