blob: a7a3e7345a2da6cd2d13fc4edf49041b8a091281 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04004#include <openbmc_intf.h>
5#include <openbmc.h>
Brad Bishop77390492016-04-13 10:47:19 -04006
7/* ------------------------------------------------------------------------- */
8static const gchar* dbus_object_path = "/org/openbmc/control";
9static const gchar* instance_name = "bmc0";
10static const gchar* dbus_name = "org.openbmc.control.Bmc";
11
Brad Bishop77390492016-04-13 10:47:19 -040012static GDBusObjectManagerServer *manager = NULL;
13
Andrew Geisslereb27fa92016-09-29 16:59:36 -050014static gboolean on_init(Control *control, GDBusMethodInvocation *invocation,
15 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040016{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050017 control_complete_init(control, invocation);
18 return TRUE;
Brad Bishop77390492016-04-13 10:47:19 -040019}
20
Andrew Geisslereb27fa92016-09-29 16:59:36 -050021static gboolean on_warm_reset(ControlBmc *bmc,
22 GDBusMethodInvocation *invocation,
23 gpointer user_data)
Williamf784d752016-01-19 12:28:49 +080024{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050025 /* Wait a while before reboot, so the caller can be responded.
Andrew Geisslereb27fa92016-09-29 16:59:36 -050026 */
Andrew Geisslerdfda9362016-11-17 13:43:37 -060027 const char *reboot_command = "/bin/sh -c 'sleep 3;reboot'&";
28 system(reboot_command);
Williamf784d752016-01-19 12:28:49 +080029
Andrew Geisslereb27fa92016-09-29 16:59:36 -050030 control_bmc_complete_warm_reset(bmc, invocation);
31 return TRUE;
Williamf784d752016-01-19 12:28:49 +080032}
33
Nan Li44dd5a62016-08-24 17:13:06 +080034static 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 Geisslereb27fa92016-09-29 16:59:36 -050055static void on_bus_acquired(GDBusConnection *connection, const gchar *name,
56 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040057{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050058 ObjectSkeleton *object;
59 cmdline *cmd = user_data;
60 manager = g_dbus_object_manager_server_new(dbus_object_path);
Williamf784d752016-01-19 12:28:49 +080061
Andrew Geisslereb27fa92016-09-29 16:59:36 -050062 gchar *s;
63 s = g_strdup_printf("%s/%s", dbus_object_path, instance_name);
64 object = object_skeleton_new(s);
65 g_free(s);
Williamf784d752016-01-19 12:28:49 +080066
Andrew Geisslereb27fa92016-09-29 16:59:36 -050067 ControlBmc* control_bmc = control_bmc_skeleton_new();
68 object_skeleton_set_control_bmc(object, control_bmc);
69 g_object_unref(control_bmc);
Williamf784d752016-01-19 12:28:49 +080070
Andrew Geisslereb27fa92016-09-29 16:59:36 -050071 Control* control = control_skeleton_new();
72 object_skeleton_set_control(object, control);
73 g_object_unref(control);
Brad Bishop77390492016-04-13 10:47:19 -040074
Andrew Geisslereb27fa92016-09-29 16:59:36 -050075 //define method callbacks here
76 g_signal_connect(control, "handle-init", G_CALLBACK(on_init), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040077
Andrew Geisslereb27fa92016-09-29 16:59:36 -050078 g_signal_connect(control_bmc, "handle-warm-reset",
79 G_CALLBACK(on_warm_reset), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040080
Nan Li44dd5a62016-08-24 17:13:06 +080081 g_signal_connect(control_bmc, "handle-cold-reset",
82 G_CALLBACK(on_cold_reset), NULL); /* user_data */
83
Andrew Geisslereb27fa92016-09-29 16:59:36 -050084 /* 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 Bishop77390492016-04-13 10:47:19 -040088
Andrew Geisslereb27fa92016-09-29 16:59:36 -050089 /* Export all objects */
90 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -040091
Andrew Geisslereb27fa92016-09-29 16:59:36 -050092 cmd->user_data = object;
Brad Bishop77390492016-04-13 10:47:19 -040093}
94
Andrew Geisslereb27fa92016-09-29 16:59:36 -050095static void on_name_acquired(GDBusConnection *connection, const gchar *name,
96 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040097{
98}
99
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500100static void on_name_lost(GDBusConnection *connection, const gchar *name,
101 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -0400102{
103}
104
105/*----------------------------------------------------------------*/
106/* Main Event Loop */
107
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500108gint main(gint argc, gchar *argv[])
Brad Bishop77390492016-04-13 10:47:19 -0400109{
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500110 GMainLoop *loop;
111 cmdline cmd;
112 cmd.argc = argc;
113 cmd.argv = argv;
Brad Bishop77390492016-04-13 10:47:19 -0400114
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500115 guint id;
116 loop = g_main_loop_new(NULL, FALSE);
117 cmd.loop = loop;
Brad Bishop77390492016-04-13 10:47:19 -0400118
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500119 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 Bishop77390492016-04-13 10:47:19 -0400125
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500126 g_main_loop_run(loop);
Brad Bishop77390492016-04-13 10:47:19 -0400127
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500128 g_bus_unown_name(id);
129 g_main_loop_unref(loop);
130 return 0;
Brad Bishop77390492016-04-13 10:47:19 -0400131}