blob: 93005a14828ddd1ef088b9f9582810ea61aa250a [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";
Norman James9e2750b2015-10-28 20:26:06 -05008static const gchar* instance_name = "power0";
Norman James362a80f2015-09-14 14:04:39 -05009static const gchar* dbus_name = "org.openbmc.buttons.Power";
Norman Jamese2765102015-08-19 22:00:55 -050010
11static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050012
Norman James19e45912015-10-04 20:19:41 -050013//This object will use these GPIOs
Norman James2c9efb52015-10-06 12:31:40 -050014GPIO gpio_button = (GPIO){ "POWER_BUTTON" };
Norman James19e45912015-10-04 20:19:41 -050015
Norman Jamese2765102015-08-19 22:00:55 -050016static gboolean
17on_is_on (Button *btn,
18 GDBusMethodInvocation *invocation,
19 gpointer user_data)
20{
21 gboolean btn_state=button_get_state(btn);
22 button_complete_is_on(btn,invocation,btn_state);
23 return TRUE;
24
25}
26
27static gboolean
Norman James19e45912015-10-04 20:19:41 -050028on_button_press (Button *btn,
Norman Jamese2765102015-08-19 22:00:55 -050029 GDBusMethodInvocation *invocation,
30 gpointer user_data)
31{
Norman James19e45912015-10-04 20:19:41 -050032 button_emit_button_pressed(btn);
33 button_complete_sim_button_press(btn,invocation);
34 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050035}
Norman James2c9efb52015-10-06 12:31:40 -050036static gboolean
37on_button_interrupt( GIOChannel *channel,
38 GIOCondition condition,
39 gpointer user_data )
40{
Norman James9e2750b2015-10-28 20:26:06 -050041
42 GError *error = 0;
43 gsize bytes_read = 0;
44 gchar buf[2];
45 buf[1] = '\0';
46 g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 );
47 GIOStatus rc = g_io_channel_read_chars( channel,
48 buf, 1,
49 &bytes_read,
50 &error );
51 printf("%s\n",buf);
52
53 if (gpio_button.irq_inited)
54 {
55 Button* button = object_get_button((Object*)user_data);
56 if (buf[0] == '0')
57 {
58 printf("Power Button pressed\n");
59 button_emit_button_pressed(button);
60 }
61 else
62 {
63 printf("Power Button released\n");
64 }
65 }
66 else { gpio_button.irq_inited = true; }
67
68 return TRUE;
Norman James2c9efb52015-10-06 12:31:40 -050069}
Norman Jamese2765102015-08-19 22:00:55 -050070
71static void
72on_bus_acquired (GDBusConnection *connection,
73 const gchar *name,
74 gpointer user_data)
75{
Norman James10ff6a32015-08-27 14:24:17 -050076 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050077 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050078 cmdline *cmd = user_data;
Norman James10ff6a32015-08-27 14:24:17 -050079 manager = g_dbus_object_manager_server_new (dbus_object_path);
80 int i=0;
Norman James2c9efb52015-10-06 12:31:40 -050081 gchar *s;
Norman James9e2750b2015-10-28 20:26:06 -050082 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
Norman James2c9efb52015-10-06 12:31:40 -050083 object = object_skeleton_new (s);
84 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050085
Norman James2c9efb52015-10-06 12:31:40 -050086 Button* button = button_skeleton_new ();
87 object_skeleton_set_button (object, button);
88 g_object_unref (button);
Norman Jamese2765102015-08-19 22:00:55 -050089
Norman James2c9efb52015-10-06 12:31:40 -050090 //define method callbacks
91 g_signal_connect (button,
92 "handle-is-on",
93 G_CALLBACK (on_is_on),
94 NULL); /* user_data */
95 g_signal_connect (button,
Norman Jamese2765102015-08-19 22:00:55 -050096 "handle-sim-button-press",
Norman James19e45912015-10-04 20:19:41 -050097 G_CALLBACK (on_button_press),
Norman Jamese2765102015-08-19 22:00:55 -050098 NULL); /* user_data */
99
Norman James19e45912015-10-04 20:19:41 -0500100
Norman James2c9efb52015-10-06 12:31:40 -0500101 /* Export the object (@manager takes its own reference to @object) */
102 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
103 g_object_unref (object);
Norman James19e45912015-10-04 20:19:41 -0500104
Norman James10ff6a32015-08-27 14:24:17 -0500105 /* Export all objects */
106 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James2c9efb52015-10-06 12:31:40 -0500107
108 // get gpio device paths
109 int rc = GPIO_OK;
110 do {
111 rc = gpio_init(connection,&gpio_button);
112 if (rc != GPIO_OK) { break; }
113 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
114 if (rc != GPIO_OK) { break; }
115 } while(0);
116 if (rc != GPIO_OK)
117 {
118 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
119 }
120
Norman Jamese2765102015-08-19 22:00:55 -0500121}
122
123static void
124on_name_acquired (GDBusConnection *connection,
125 const gchar *name,
126 gpointer user_data)
127{
Norman Jamese2765102015-08-19 22:00:55 -0500128}
129
130static void
131on_name_lost (GDBusConnection *connection,
132 const gchar *name,
133 gpointer user_data)
134{
Norman Jamese2765102015-08-19 22:00:55 -0500135}
136
137
138gint
139main (gint argc, gchar *argv[])
140{
141 GMainLoop *loop;
142
Norman James952b38d2015-08-27 21:30:06 -0500143 cmdline cmd;
144 cmd.argc = argc;
145 cmd.argv = argv;
146
Norman Jamese2765102015-08-19 22:00:55 -0500147 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500148 loop = g_main_loop_new (NULL, FALSE);
149
Norman James5e792e32015-10-07 17:36:17 -0500150 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500151 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500152 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
153 G_BUS_NAME_OWNER_FLAGS_REPLACE,
154 on_bus_acquired,
155 on_name_acquired,
156 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500157 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500158 NULL);
159
160 g_main_loop_run (loop);
161
162 g_bus_unown_name (id);
163 g_main_loop_unref (loop);
164 return 0;
165}