blob: 5752505f497de820f85f57da227c66b2725ba716 [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'&";
Ed Tanous81100ae2021-01-28 15:25:26 -080028 if(system(reboot_command) < 0){
29 return FALSE;
30 }
Williamf784d752016-01-19 12:28:49 +080031
Andrew Geisslereb27fa92016-09-29 16:59:36 -050032 control_bmc_complete_warm_reset(bmc, invocation);
33 return TRUE;
Williamf784d752016-01-19 12:28:49 +080034}
35
Nan Li44dd5a62016-08-24 17:13:06 +080036static 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
Andrew Geisslereb27fa92016-09-29 16:59:36 -050057static void on_bus_acquired(GDBusConnection *connection, const gchar *name,
58 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040059{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050060 ObjectSkeleton *object;
61 cmdline *cmd = user_data;
62 manager = g_dbus_object_manager_server_new(dbus_object_path);
Williamf784d752016-01-19 12:28:49 +080063
Andrew Geisslereb27fa92016-09-29 16:59:36 -050064 gchar *s;
65 s = g_strdup_printf("%s/%s", dbus_object_path, instance_name);
66 object = object_skeleton_new(s);
67 g_free(s);
Williamf784d752016-01-19 12:28:49 +080068
Andrew Geisslereb27fa92016-09-29 16:59:36 -050069 ControlBmc* control_bmc = control_bmc_skeleton_new();
70 object_skeleton_set_control_bmc(object, control_bmc);
71 g_object_unref(control_bmc);
Williamf784d752016-01-19 12:28:49 +080072
Andrew Geisslereb27fa92016-09-29 16:59:36 -050073 Control* control = control_skeleton_new();
74 object_skeleton_set_control(object, control);
75 g_object_unref(control);
Brad Bishop77390492016-04-13 10:47:19 -040076
Andrew Geisslereb27fa92016-09-29 16:59:36 -050077 //define method callbacks here
78 g_signal_connect(control, "handle-init", G_CALLBACK(on_init), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040079
Andrew Geisslereb27fa92016-09-29 16:59:36 -050080 g_signal_connect(control_bmc, "handle-warm-reset",
81 G_CALLBACK(on_warm_reset), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040082
Nan Li44dd5a62016-08-24 17:13:06 +080083 g_signal_connect(control_bmc, "handle-cold-reset",
84 G_CALLBACK(on_cold_reset), NULL); /* user_data */
85
Andrew Geisslereb27fa92016-09-29 16:59:36 -050086 /* 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);
Brad Bishop77390492016-04-13 10:47:19 -040090
Andrew Geisslereb27fa92016-09-29 16:59:36 -050091 /* Export all objects */
92 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -040093
Andrew Geisslereb27fa92016-09-29 16:59:36 -050094 cmd->user_data = object;
Brad Bishop77390492016-04-13 10:47:19 -040095}
96
Andrew Geisslereb27fa92016-09-29 16:59:36 -050097static void on_name_acquired(GDBusConnection *connection, const gchar *name,
98 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040099{
100}
101
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500102static void on_name_lost(GDBusConnection *connection, const gchar *name,
103 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -0400104{
105}
106
107/*----------------------------------------------------------------*/
108/* Main Event Loop */
109
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500110gint main(gint argc, gchar *argv[])
Brad Bishop77390492016-04-13 10:47:19 -0400111{
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500112 GMainLoop *loop;
113 cmdline cmd;
114 cmd.argc = argc;
115 cmd.argv = argv;
Brad Bishop77390492016-04-13 10:47:19 -0400116
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500117 guint id;
118 loop = g_main_loop_new(NULL, FALSE);
119 cmd.loop = loop;
Brad Bishop77390492016-04-13 10:47:19 -0400120
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500121 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);
Brad Bishop77390492016-04-13 10:47:19 -0400127
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500128 g_main_loop_run(loop);
Brad Bishop77390492016-04-13 10:47:19 -0400129
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500130 g_bus_unown_name(id);
131 g_main_loop_unref(loop);
132 return 0;
Brad Bishop77390492016-04-13 10:47:19 -0400133}