blob: b8bca38e99ee3929c167326f45b0043ac295b3c6 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include <stdio.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04002#include <openbmc_intf.h>
3#include <gpio.h>
4#include <openbmc.h>
Brad Bishop77390492016-04-13 10:47:19 -04005
6/* ------------------------------------------------------------------------- */
7static const gchar* dbus_object_path = "/org/openbmc/buttons";
8static const gchar* instance_name = "power0";
9static const gchar* dbus_name = "org.openbmc.buttons.Power";
10static const int LONG_PRESS_SECONDS = 3;
11static GDBusObjectManagerServer *manager = NULL;
12
13//This object will use these GPIOs
14GPIO gpio_button = (GPIO){ "POWER_BUTTON" };
15
16static 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
26static gboolean
27on_button_press(Button *btn,
28 GDBusMethodInvocation *invocation,
29 gpointer user_data)
30{
31 button_emit_pressed(btn);
32 button_complete_sim_press(btn,invocation);
33 return TRUE;
34}
35
36static gboolean
37on_button_interrupt( GIOChannel *channel,
38 GIOCondition condition,
39 gpointer user_data )
40{
41 GError *error = 0;
42 gsize bytes_read = 0;
43 gchar buf[2];
44 buf[1] = '\0';
45 g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 );
Brad Bishop0c82c602016-04-13 13:36:49 -040046 g_io_channel_read_chars(channel,
Brad Bishop77390492016-04-13 10:47:19 -040047 buf, 1,
48 &bytes_read,
49 &error );
50 printf("%s\n",buf);
51
52 time_t current_time = time(NULL);
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_pressed(button);
60 button_set_timer(button,(long)current_time);
61 }
62 else
63 {
64 long press_time = current_time-button_get_timer(button);
65 printf("Power Button released, held for %ld seconds\n",press_time);
66 if(press_time > LONG_PRESS_SECONDS)
67 {
68 button_emit_pressed_long(button);
69 } else {
70 button_emit_released(button);
71 }
72 }
73 }
74 else { gpio_button.irq_inited = true; }
75
76 return TRUE;
77}
78
79static void
80on_bus_acquired(GDBusConnection *connection,
81 const gchar *name,
82 gpointer user_data)
83{
84 ObjectSkeleton *object;
85 //g_print ("Acquired a message bus connection: %s\n",name);
Brad Bishop77390492016-04-13 10:47:19 -040086 manager = g_dbus_object_manager_server_new(dbus_object_path);
Brad Bishop77390492016-04-13 10:47:19 -040087 gchar *s;
88 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
89 object = object_skeleton_new(s);
90 g_free(s);
91
92 Button* button = button_skeleton_new();
93 object_skeleton_set_button(object, button);
94 g_object_unref(button);
95
Brad Bishop77390492016-04-13 10:47:19 -040096 //define method callbacks
97 g_signal_connect(button,
98 "handle-is-on",
99 G_CALLBACK(on_is_on),
100 NULL); /* user_data */
101 g_signal_connect(button,
102 "handle-sim-press",
103 G_CALLBACK(on_button_press),
104 NULL); /* user_data */
105
106
107 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400108 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400109 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
110 g_object_unref(object);
111
Brad Bishop77390492016-04-13 10:47:19 -0400112 // get gpio device paths
113 int rc = GPIO_OK;
114 do {
115 rc = gpio_init(connection,&gpio_button);
116 if(rc != GPIO_OK) { break; }
117 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
118 if(rc != GPIO_OK) { break; }
119 } while(0);
120 if(rc != GPIO_OK)
121 {
122 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
123 }
Brad Bishop77390492016-04-13 10:47:19 -0400124}
125
126static void
127on_name_acquired(GDBusConnection *connection,
128 const gchar *name,
129 gpointer user_data)
130{
131}
132
133static void
134on_name_lost(GDBusConnection *connection,
135 const gchar *name,
136 gpointer user_data)
137{
138}
139
140gint
141main(gint argc, gchar *argv[])
142{
143 GMainLoop *loop;
144
145 cmdline cmd;
146 cmd.argc = argc;
147 cmd.argv = argv;
148
149 guint id;
150 loop = g_main_loop_new(NULL, FALSE);
151
152 id = g_bus_own_name(DBUS_TYPE,
153 dbus_name,
154 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
155 G_BUS_NAME_OWNER_FLAGS_REPLACE,
156 on_bus_acquired,
157 on_name_acquired,
158 on_name_lost,
159 &cmd,
160 NULL);
161
162 g_main_loop_run(loop);
163
164 g_bus_unown_name(id);
165 g_main_loop_unref(loop);
166 return 0;
167}