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