blob: 545f780bceee7a20019c85b35f9ccb99fba085d8 [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
Williamf784d752016-01-19 12:28:49 +080014static gboolean
Brad Bishop77390492016-04-13 10:47:19 -040015on_init(Control *control,
16 GDBusMethodInvocation *invocation,
17 gpointer user_data)
18{
Brad Bishop77390492016-04-13 10:47:19 -040019 control_complete_init(control,invocation);
Brad Bishop77390492016-04-13 10:47:19 -040020 return TRUE;
21}
22
23static gboolean
24on_warm_reset(ControlBmc *bmc,
25 GDBusMethodInvocation *invocation,
26 gpointer user_data)
Williamf784d752016-01-19 12:28:49 +080027{
28 GError *err = NULL;
29 /* Wait a while before reboot, so the caller can be responded.
30 * Note that g_spawn_command_line_async() cannot parse ';' as
31 * a command separator. Need to use 'sh -c' to let shell parse it.
32 */
33 gchar *reboot_command = "/bin/sh -c 'sleep 3;reboot'";
34
35 g_spawn_command_line_async(reboot_command, &err);
Brad Bishop77390492016-04-13 10:47:19 -040036 if(err != NULL) {
Williamf784d752016-01-19 12:28:49 +080037 fprintf(stderr, "warmReset() error: %s\n", err->message);
38 g_error_free(err);
39 }
40
41 control_bmc_complete_warm_reset(bmc, invocation);
42 return TRUE;
43}
44
Williamf784d752016-01-19 12:28:49 +080045static void
Brad Bishop77390492016-04-13 10:47:19 -040046on_bus_acquired(GDBusConnection *connection,
47 const gchar *name,
48 gpointer user_data)
49{
50 ObjectSkeleton *object;
51 cmdline *cmd = user_data;
52 manager = g_dbus_object_manager_server_new(dbus_object_path);
Williamf784d752016-01-19 12:28:49 +080053
Brad Bishop77390492016-04-13 10:47:19 -040054 gchar *s;
55 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
56 object = object_skeleton_new(s);
57 g_free(s);
Williamf784d752016-01-19 12:28:49 +080058
Brad Bishop77390492016-04-13 10:47:19 -040059 ControlBmc* control_bmc = control_bmc_skeleton_new();
60 object_skeleton_set_control_bmc(object, control_bmc);
61 g_object_unref(control_bmc);
Williamf784d752016-01-19 12:28:49 +080062
Brad Bishop77390492016-04-13 10:47:19 -040063 Control* control = control_skeleton_new();
64 object_skeleton_set_control(object, control);
65 g_object_unref(control);
66
67 //define method callbacks here
68 g_signal_connect(control,
69 "handle-init",
70 G_CALLBACK(on_init),
71 NULL); /* user_data */
72
73
74 g_signal_connect(control_bmc,
75 "handle-warm-reset",
76 G_CALLBACK(on_warm_reset),
77 NULL); /* user_data */
78
79 /* Export the object (@manager takes its own reference to @object) */
80 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
81 g_object_unref(object);
82
83 /* Export all objects */
84 g_dbus_object_manager_server_set_connection(manager, connection);
85
Brad Bishop77390492016-04-13 10:47:19 -040086 cmd->user_data = object;
Brad Bishop77390492016-04-13 10:47:19 -040087}
88
89static void
90on_name_acquired(GDBusConnection *connection,
91 const gchar *name,
92 gpointer user_data)
93{
94}
95
96static void
97on_name_lost(GDBusConnection *connection,
98 const gchar *name,
99 gpointer user_data)
100{
101}
102
103/*----------------------------------------------------------------*/
104/* Main Event Loop */
105
106gint
107main(gint argc, gchar *argv[])
108{
109 GMainLoop *loop;
110 cmdline cmd;
111 cmd.argc = argc;
112 cmd.argv = argv;
113
114 guint id;
115 loop = g_main_loop_new(NULL, FALSE);
116 cmd.loop = loop;
117
118 id = g_bus_own_name(DBUS_TYPE,
119 dbus_name,
120 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
121 G_BUS_NAME_OWNER_FLAGS_REPLACE,
122 on_bus_acquired,
123 on_name_acquired,
124 on_name_lost,
125 &cmd,
126 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}