xref: /openbmc/skeleton/bmcctl/control_bmc_obj.c (revision 770cdfc8)
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 
14 static gboolean
15 on_init(Control *control,
16 		GDBusMethodInvocation *invocation,
17 		gpointer user_data)
18 {
19 	control_complete_init(control,invocation);
20 	return TRUE;
21 }
22 
23 static gboolean
24 on_warm_reset(ControlBmc *bmc,
25 		GDBusMethodInvocation *invocation,
26 		gpointer user_data)
27 {
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);
36 	if(err != NULL) {
37 		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 
45 static void
46 on_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);
53 
54 	gchar *s;
55 	s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
56 	object = object_skeleton_new(s);
57 	g_free(s);
58 
59 	ControlBmc* control_bmc = control_bmc_skeleton_new();
60 	object_skeleton_set_control_bmc(object, control_bmc);
61 	g_object_unref(control_bmc);
62 
63 	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 
86 	cmd->user_data = object;
87 }
88 
89 static void
90 on_name_acquired(GDBusConnection *connection,
91 		const gchar *name,
92 		gpointer user_data)
93 {
94 }
95 
96 static void
97 on_name_lost(GDBusConnection *connection,
98 		const gchar *name,
99 		gpointer user_data)
100 {
101 }
102 
103 /*----------------------------------------------------------------*/
104 /* Main Event Loop                                                */
105 
106 gint
107 main(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 }
134