blob: 30078bbc4fa0ee6a42b64fa3373a38d80db70438 [file] [log] [blame]
Norman James2c9efb52015-10-06 12:31:40 -05001#include <stdio.h>
Norman James362a80f2015-09-14 14:04:39 -05002#include "interfaces/openbmc_intf.h"
Norman James10ff6a32015-08-27 14:24:17 -05003#include "gpio.h"
4#include "openbmc.h"
Norman Jamese2765102015-08-19 22:00:55 -05005
6/* ---------------------------------------------------------------------------------------------------- */
Norman James362a80f2015-09-14 14:04:39 -05007static const gchar* dbus_object_path = "/org/openbmc/buttons";
8static const gchar* dbus_name = "org.openbmc.buttons.Power";
Norman Jamese2765102015-08-19 22:00:55 -05009
10static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050011
Norman James19e45912015-10-04 20:19:41 -050012//This object will use these GPIOs
Norman James2c9efb52015-10-06 12:31:40 -050013GPIO gpio_button = (GPIO){ "POWER_BUTTON" };
Norman James19e45912015-10-04 20:19:41 -050014
Norman Jamese2765102015-08-19 22:00:55 -050015static gboolean
16on_is_on (Button *btn,
17 GDBusMethodInvocation *invocation,
18 gpointer user_data)
19{
20 gboolean btn_state=button_get_state(btn);
21 button_complete_is_on(btn,invocation,btn_state);
22 return TRUE;
23
24}
25
26static gboolean
Norman James19e45912015-10-04 20:19:41 -050027on_button_press (Button *btn,
Norman Jamese2765102015-08-19 22:00:55 -050028 GDBusMethodInvocation *invocation,
29 gpointer user_data)
30{
Norman James19e45912015-10-04 20:19:41 -050031 button_emit_button_pressed(btn);
32 button_complete_sim_button_press(btn,invocation);
33 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050034}
Norman James2c9efb52015-10-06 12:31:40 -050035static gboolean
36on_button_interrupt( GIOChannel *channel,
37 GIOCondition condition,
38 gpointer user_data )
39{
40 Button* button = object_get_button((Object*)user_data);
41 printf("Power Button pressed\n");
42 button_emit_button_pressed(button);
43}
Norman Jamese2765102015-08-19 22:00:55 -050044
45static void
46on_bus_acquired (GDBusConnection *connection,
47 const gchar *name,
48 gpointer user_data)
49{
Norman James10ff6a32015-08-27 14:24:17 -050050 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050051 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050052 cmdline *cmd = user_data;
53 if (cmd->argc < 2)
54 {
55 g_print("No objects created. Put object name(s) on command line\n");
56 return;
57 }
58 manager = g_dbus_object_manager_server_new (dbus_object_path);
59 int i=0;
Norman James2c9efb52015-10-06 12:31:40 -050060 gchar *s;
61 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[1]);
62 object = object_skeleton_new (s);
63 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050064
Norman James2c9efb52015-10-06 12:31:40 -050065 Button* button = button_skeleton_new ();
66 object_skeleton_set_button (object, button);
67 g_object_unref (button);
Norman Jamese2765102015-08-19 22:00:55 -050068
Norman James2c9efb52015-10-06 12:31:40 -050069 //define method callbacks
70 g_signal_connect (button,
71 "handle-is-on",
72 G_CALLBACK (on_is_on),
73 NULL); /* user_data */
74 g_signal_connect (button,
Norman Jamese2765102015-08-19 22:00:55 -050075 "handle-sim-button-press",
Norman James19e45912015-10-04 20:19:41 -050076 G_CALLBACK (on_button_press),
Norman Jamese2765102015-08-19 22:00:55 -050077 NULL); /* user_data */
78
Norman James19e45912015-10-04 20:19:41 -050079
Norman James2c9efb52015-10-06 12:31:40 -050080 /* Export the object (@manager takes its own reference to @object) */
81 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
82 g_object_unref (object);
Norman James19e45912015-10-04 20:19:41 -050083
Norman James10ff6a32015-08-27 14:24:17 -050084 /* Export all objects */
85 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James2c9efb52015-10-06 12:31:40 -050086
87 // get gpio device paths
88 int rc = GPIO_OK;
89 do {
90 rc = gpio_init(connection,&gpio_button);
91 if (rc != GPIO_OK) { break; }
92 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
93 if (rc != GPIO_OK) { break; }
94 } while(0);
95 if (rc != GPIO_OK)
96 {
97 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
98 }
99
Norman Jamese2765102015-08-19 22:00:55 -0500100}
101
102static void
103on_name_acquired (GDBusConnection *connection,
104 const gchar *name,
105 gpointer user_data)
106{
Norman Jamese2765102015-08-19 22:00:55 -0500107}
108
109static void
110on_name_lost (GDBusConnection *connection,
111 const gchar *name,
112 gpointer user_data)
113{
Norman Jamese2765102015-08-19 22:00:55 -0500114}
115
116
117gint
118main (gint argc, gchar *argv[])
119{
120 GMainLoop *loop;
121
Norman James952b38d2015-08-27 21:30:06 -0500122 cmdline cmd;
123 cmd.argc = argc;
124 cmd.argv = argv;
125
Norman Jamese2765102015-08-19 22:00:55 -0500126 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500127 loop = g_main_loop_new (NULL, FALSE);
128
Norman James5e792e32015-10-07 17:36:17 -0500129 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500130 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500131 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
132 G_BUS_NAME_OWNER_FLAGS_REPLACE,
133 on_bus_acquired,
134 on_name_acquired,
135 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500136 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500137 NULL);
138
139 g_main_loop_run (loop);
140
141 g_bus_unown_name (id);
142 g_main_loop_unref (loop);
143 return 0;
144}