blob: c3ef7236405c99226cad2c00ef1a940c38b5b577 [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 GError *err = NULL;
26 /* Wait a while before reboot, so the caller can be responded.
27 * Note that g_spawn_command_line_async() cannot parse ';' as
28 * a command separator. Need to use 'sh -c' to let shell parse it.
29 */
30 gchar *reboot_command = "/bin/sh -c 'sleep 3;reboot'";
Williamf784d752016-01-19 12:28:49 +080031
Andrew Geisslereb27fa92016-09-29 16:59:36 -050032 g_spawn_command_line_async(reboot_command, &err);
33 if (err != NULL)
34 {
35 fprintf(stderr, "warmReset() error: %s\n", err->message);
36 g_error_free(err);
37 }
Williamf784d752016-01-19 12:28:49 +080038
Andrew Geisslereb27fa92016-09-29 16:59:36 -050039 control_bmc_complete_warm_reset(bmc, invocation);
40 return TRUE;
Williamf784d752016-01-19 12:28:49 +080041}
42
Andrew Geisslereb27fa92016-09-29 16:59:36 -050043static void on_bus_acquired(GDBusConnection *connection, const gchar *name,
44 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040045{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050046 ObjectSkeleton *object;
47 cmdline *cmd = user_data;
48 manager = g_dbus_object_manager_server_new(dbus_object_path);
Williamf784d752016-01-19 12:28:49 +080049
Andrew Geisslereb27fa92016-09-29 16:59:36 -050050 gchar *s;
51 s = g_strdup_printf("%s/%s", dbus_object_path, instance_name);
52 object = object_skeleton_new(s);
53 g_free(s);
Williamf784d752016-01-19 12:28:49 +080054
Andrew Geisslereb27fa92016-09-29 16:59:36 -050055 ControlBmc* control_bmc = control_bmc_skeleton_new();
56 object_skeleton_set_control_bmc(object, control_bmc);
57 g_object_unref(control_bmc);
Williamf784d752016-01-19 12:28:49 +080058
Andrew Geisslereb27fa92016-09-29 16:59:36 -050059 Control* control = control_skeleton_new();
60 object_skeleton_set_control(object, control);
61 g_object_unref(control);
Brad Bishop77390492016-04-13 10:47:19 -040062
Andrew Geisslereb27fa92016-09-29 16:59:36 -050063 //define method callbacks here
64 g_signal_connect(control, "handle-init", G_CALLBACK(on_init), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040065
Andrew Geisslereb27fa92016-09-29 16:59:36 -050066 g_signal_connect(control_bmc, "handle-warm-reset",
67 G_CALLBACK(on_warm_reset), NULL); /* user_data */
Brad Bishop77390492016-04-13 10:47:19 -040068
Andrew Geisslereb27fa92016-09-29 16:59:36 -050069 /* Export the object (@manager takes its own reference to @object) */
70 g_dbus_object_manager_server_export(manager,
71 G_DBUS_OBJECT_SKELETON(object));
72 g_object_unref(object);
Brad Bishop77390492016-04-13 10:47:19 -040073
Andrew Geisslereb27fa92016-09-29 16:59:36 -050074 /* Export all objects */
75 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -040076
Andrew Geisslereb27fa92016-09-29 16:59:36 -050077 cmd->user_data = object;
Brad Bishop77390492016-04-13 10:47:19 -040078}
79
Andrew Geisslereb27fa92016-09-29 16:59:36 -050080static void on_name_acquired(GDBusConnection *connection, const gchar *name,
81 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040082{
83}
84
Andrew Geisslereb27fa92016-09-29 16:59:36 -050085static void on_name_lost(GDBusConnection *connection, const gchar *name,
86 gpointer user_data)
Brad Bishop77390492016-04-13 10:47:19 -040087{
88}
89
90/*----------------------------------------------------------------*/
91/* Main Event Loop */
92
Andrew Geisslereb27fa92016-09-29 16:59:36 -050093gint main(gint argc, gchar *argv[])
Brad Bishop77390492016-04-13 10:47:19 -040094{
Andrew Geisslereb27fa92016-09-29 16:59:36 -050095 GMainLoop *loop;
96 cmdline cmd;
97 cmd.argc = argc;
98 cmd.argv = argv;
Brad Bishop77390492016-04-13 10:47:19 -040099
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500100 guint id;
101 loop = g_main_loop_new(NULL, FALSE);
102 cmd.loop = loop;
Brad Bishop77390492016-04-13 10:47:19 -0400103
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500104 id = g_bus_own_name(
105 DBUS_TYPE,
106 dbus_name,
107 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT
108 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
109 on_bus_acquired, on_name_acquired, on_name_lost, &cmd, NULL);
Brad Bishop77390492016-04-13 10:47:19 -0400110
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500111 g_main_loop_run(loop);
Brad Bishop77390492016-04-13 10:47:19 -0400112
Andrew Geisslereb27fa92016-09-29 16:59:36 -0500113 g_bus_unown_name(id);
114 g_main_loop_unref(loop);
115 return 0;
Brad Bishop77390492016-04-13 10:47:19 -0400116}