blob: 759e83a7dc7dd6f226c50798eb952436f4da9287 [file] [log] [blame]
Kenc95eccd2015-12-19 07:02:34 +08001#include <stdio.h>
2#include "interfaces/openbmc_intf.h"
3#include "gpio.h"
4#include "openbmc.h"
5#include "object_mapper.h"
6
7/* ---------------------------------------------------------------------------------------------------- */
8static const gchar* dbus_object_path = "/org/openbmc/buttons";
9static const gchar* instance_name = "reset0";
10static const gchar* dbus_name = "org.openbmc.buttons.reset";
11static const int LONG_PRESS_SECONDS = 3;
12static GDBusObjectManagerServer *manager = NULL;
13
14//This object will use these GPIOs
15GPIO gpio_button = (GPIO){ "RESET_BUTTON" };
16
17static 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
29on_button_press (Button *btn,
30 GDBusMethodInvocation *invocation,
31 gpointer user_data)
32{
33 button_emit_pressed(btn);
34 button_complete_sim_press(btn,invocation);
35 return TRUE;
36}
37static gboolean
38on_button_interrupt( GIOChannel *channel,
39 GIOCondition condition,
40 gpointer user_data )
41{
42
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
54 time_t current_time = time(NULL);
55 if (gpio_button.irq_inited)
56 {
57 Button* button = object_get_button((Object*)user_data);
58 if (buf[0] == '0')
59 {
60 printf("reset Button pressed\n");
61 button_emit_pressed(button);
62 button_set_timer(button,(long)current_time);
63 }
64 else
65 {
66 long press_time = current_time-button_get_timer(button);
67 printf("reset 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 }
74 }
75 }
76 else { gpio_button.irq_inited = true; }
77
78 return TRUE;
79}
80static void
81on_bus_acquired (GDBusConnection *connection,
82 const gchar *name,
83 gpointer user_data)
84{
85 ObjectSkeleton *object;
86 //g_print ("Acquired a message bus connection: %s\n",name);
87 cmdline *cmd = user_data;
88 manager = g_dbus_object_manager_server_new (dbus_object_path);
89 int i=0;
90 gchar *s;
91 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
92 object = object_skeleton_new (s);
93 g_free (s);
94
95 Button* button = button_skeleton_new ();
96 object_skeleton_set_button (object, button);
97 g_object_unref (button);
98
99 ObjectMapper* mapper = object_mapper_skeleton_new ();
100 object_skeleton_set_object_mapper (object, mapper);
101 g_object_unref (mapper);
102
103 //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,
109 "handle-sim-press",
110 G_CALLBACK (on_button_press),
111 NULL); /* user_data */
112
113
114 /* 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);
117
118 /* Export all objects */
119 g_dbus_object_manager_server_set_connection (manager, connection);
120
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);
132 }
133 emit_object_added((GDBusObjectManager*)manager);
134}
135
136static void
137on_name_acquired (GDBusConnection *connection,
138 const gchar *name,
139 gpointer user_data)
140{
141}
142
143static void
144on_name_lost (GDBusConnection *connection,
145 const gchar *name,
146 gpointer user_data)
147{
148}
149
150
151gint
152main (gint argc, gchar *argv[])
153{
154 GMainLoop *loop;
155
156 cmdline cmd;
157 cmd.argc = argc;
158 cmd.argv = argv;
159
160 guint id;
161 loop = g_main_loop_new (NULL, FALSE);
162
163 id = g_bus_own_name (DBUS_TYPE,
164 dbus_name,
165 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,
170 &cmd,
171 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}
179