blob: 94f892207eb1bacbd2a80e50cbee0b6c3115c30c [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 James5d793682015-10-31 17:28:12 -05005#include "object_mapper.h"
Norman Jamese2765102015-08-19 22:00:55 -05006
7/* ---------------------------------------------------------------------------------------------------- */
Norman James362a80f2015-09-14 14:04:39 -05008static const gchar* dbus_object_path = "/org/openbmc/buttons";
Norman James9e2750b2015-10-28 20:26:06 -05009static const gchar* instance_name = "power0";
Norman James362a80f2015-09-14 14:04:39 -050010static const gchar* dbus_name = "org.openbmc.buttons.Power";
Norman James5d793682015-10-31 17:28:12 -050011static const int LONG_PRESS_SECONDS = 3;
Norman Jamese2765102015-08-19 22:00:55 -050012static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050013
Norman James19e45912015-10-04 20:19:41 -050014//This object will use these GPIOs
Norman James2c9efb52015-10-06 12:31:40 -050015GPIO gpio_button = (GPIO){ "POWER_BUTTON" };
Norman James19e45912015-10-04 20:19:41 -050016
Norman Jamese2765102015-08-19 22:00:55 -050017static gboolean
18on_is_on (Button *btn,
19 GDBusMethodInvocation *invocation,
20 gpointer user_data)
21{
22 gboolean btn_state=button_get_state(btn);
23 button_complete_is_on(btn,invocation,btn_state);
24 return TRUE;
25
26}
27
28static gboolean
Norman James19e45912015-10-04 20:19:41 -050029on_button_press (Button *btn,
Norman Jamese2765102015-08-19 22:00:55 -050030 GDBusMethodInvocation *invocation,
31 gpointer user_data)
32{
Norman James5d793682015-10-31 17:28:12 -050033 button_emit_pressed(btn);
34 button_complete_sim_press(btn,invocation);
Norman James19e45912015-10-04 20:19:41 -050035 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050036}
Norman James2c9efb52015-10-06 12:31:40 -050037static gboolean
38on_button_interrupt( GIOChannel *channel,
39 GIOCondition condition,
40 gpointer user_data )
41{
Norman James9e2750b2015-10-28 20:26:06 -050042
43 GError *error = 0;
44 gsize bytes_read = 0;
45 gchar buf[2];
46 buf[1] = '\0';
47 g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 );
48 GIOStatus rc = g_io_channel_read_chars( channel,
49 buf, 1,
50 &bytes_read,
51 &error );
52 printf("%s\n",buf);
53
Norman James5d793682015-10-31 17:28:12 -050054 time_t current_time = time(NULL);
Norman James9e2750b2015-10-28 20:26:06 -050055 if (gpio_button.irq_inited)
56 {
57 Button* button = object_get_button((Object*)user_data);
58 if (buf[0] == '0')
59 {
60 printf("Power Button pressed\n");
Norman James5d793682015-10-31 17:28:12 -050061 button_emit_pressed(button);
62 button_set_timer(button,(long)current_time);
Norman James9e2750b2015-10-28 20:26:06 -050063 }
64 else
65 {
Norman James5d793682015-10-31 17:28:12 -050066 long press_time = current_time-button_get_timer(button);
67 printf("Power Button released, held for %ld seconds\n",press_time);
68 if (press_time > LONG_PRESS_SECONDS)
69 {
70 button_emit_pressed_long(button);
71 } else {
72 button_emit_released(button);
73 }
Norman James9e2750b2015-10-28 20:26:06 -050074 }
75 }
76 else { gpio_button.irq_inited = true; }
77
78 return TRUE;
Norman James2c9efb52015-10-06 12:31:40 -050079}
Norman Jamese2765102015-08-19 22:00:55 -050080static void
81on_bus_acquired (GDBusConnection *connection,
82 const gchar *name,
83 gpointer user_data)
84{
Norman James10ff6a32015-08-27 14:24:17 -050085 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050086 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050087 cmdline *cmd = user_data;
Norman James10ff6a32015-08-27 14:24:17 -050088 manager = g_dbus_object_manager_server_new (dbus_object_path);
89 int i=0;
Norman James2c9efb52015-10-06 12:31:40 -050090 gchar *s;
Norman James9e2750b2015-10-28 20:26:06 -050091 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
Norman James2c9efb52015-10-06 12:31:40 -050092 object = object_skeleton_new (s);
93 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050094
Norman James2c9efb52015-10-06 12:31:40 -050095 Button* button = button_skeleton_new ();
96 object_skeleton_set_button (object, button);
97 g_object_unref (button);
Norman Jamese2765102015-08-19 22:00:55 -050098
Norman James5d793682015-10-31 17:28:12 -050099 ObjectMapper* mapper = object_mapper_skeleton_new ();
100 object_skeleton_set_object_mapper (object, mapper);
101 g_object_unref (mapper);
102
Norman James2c9efb52015-10-06 12:31:40 -0500103 //define method callbacks
104 g_signal_connect (button,
105 "handle-is-on",
106 G_CALLBACK (on_is_on),
107 NULL); /* user_data */
108 g_signal_connect (button,
Norman James641465e2015-11-09 11:06:05 -0600109 "handle-sim-press",
Norman James19e45912015-10-04 20:19:41 -0500110 G_CALLBACK (on_button_press),
Norman Jamese2765102015-08-19 22:00:55 -0500111 NULL); /* user_data */
112
Norman James19e45912015-10-04 20:19:41 -0500113
Norman James2c9efb52015-10-06 12:31:40 -0500114 /* Export the object (@manager takes its own reference to @object) */
115 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
116 g_object_unref (object);
Norman James19e45912015-10-04 20:19:41 -0500117
Norman James10ff6a32015-08-27 14:24:17 -0500118 /* Export all objects */
119 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James2c9efb52015-10-06 12:31:40 -0500120
121 // get gpio device paths
122 int rc = GPIO_OK;
123 do {
124 rc = gpio_init(connection,&gpio_button);
125 if (rc != GPIO_OK) { break; }
126 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
127 if (rc != GPIO_OK) { break; }
128 } while(0);
129 if (rc != GPIO_OK)
130 {
131 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
Norman James5d793682015-10-31 17:28:12 -0500132 }
133 emit_object_added((GDBusObjectManager*)manager);
Norman Jamese2765102015-08-19 22:00:55 -0500134}
135
136static void
137on_name_acquired (GDBusConnection *connection,
138 const gchar *name,
139 gpointer user_data)
140{
Norman Jamese2765102015-08-19 22:00:55 -0500141}
142
143static void
144on_name_lost (GDBusConnection *connection,
145 const gchar *name,
146 gpointer user_data)
147{
Norman Jamese2765102015-08-19 22:00:55 -0500148}
149
150
151gint
152main (gint argc, gchar *argv[])
153{
154 GMainLoop *loop;
155
Norman James952b38d2015-08-27 21:30:06 -0500156 cmdline cmd;
157 cmd.argc = argc;
158 cmd.argv = argv;
159
Norman Jamese2765102015-08-19 22:00:55 -0500160 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500161 loop = g_main_loop_new (NULL, FALSE);
162
Norman James5e792e32015-10-07 17:36:17 -0500163 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500164 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500165 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
166 G_BUS_NAME_OWNER_FLAGS_REPLACE,
167 on_bus_acquired,
168 on_name_acquired,
169 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500170 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500171 NULL);
172
173 g_main_loop_run (loop);
174
175 g_bus_unown_name (id);
176 g_main_loop_unref (loop);
177 return 0;
178}