blob: 731ecce36b80856a470cbfcd870e7a5c3806f410 [file] [log] [blame]
Kenc95eccd2015-12-19 07:02:34 +08001#include <stdio.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04002#include <openbmc_intf.h>
3#include <gpio.h>
4#include <openbmc.h>
Kenc95eccd2015-12-19 07:02:34 +08005
Brad Bishop77390492016-04-13 10:47:19 -04006/* ------------------------------------------------------------------------- */
Kenc95eccd2015-12-19 07:02:34 +08007static const gchar* dbus_object_path = "/org/openbmc/buttons";
8static const gchar* instance_name = "reset0";
Brad Bishop77390492016-04-13 10:47:19 -04009static const gchar* dbus_name = "org.openbmc.buttons.reset";
Kenc95eccd2015-12-19 07:02:34 +080010static const int LONG_PRESS_SECONDS = 3;
11static GDBusObjectManagerServer *manager = NULL;
12
13//This object will use these GPIOs
Brad Bishop77390492016-04-13 10:47:19 -040014GPIO gpio_button = (GPIO){ "RESET_BUTTON" };
Kenc95eccd2015-12-19 07:02:34 +080015
16static gboolean
Brad Bishop77390492016-04-13 10:47:19 -040017on_is_on(Button *btn,
18 GDBusMethodInvocation *invocation,
19 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080020{
Brad Bishop77390492016-04-13 10:47:19 -040021 gboolean btn_state=button_get_state(btn);
22 button_complete_is_on(btn,invocation,btn_state);
23 return TRUE;
Kenc95eccd2015-12-19 07:02:34 +080024}
25
26static gboolean
Brad Bishop77390492016-04-13 10:47:19 -040027on_button_press(Button *btn,
28 GDBusMethodInvocation *invocation,
29 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080030{
31 button_emit_pressed(btn);
32 button_complete_sim_press(btn,invocation);
33 return TRUE;
34}
Brad Bishop77390492016-04-13 10:47:19 -040035
Kenc95eccd2015-12-19 07:02:34 +080036static gboolean
37on_button_interrupt( GIOChannel *channel,
Brad Bishop77390492016-04-13 10:47:19 -040038 GIOCondition condition,
39 gpointer user_data )
Kenc95eccd2015-12-19 07:02:34 +080040{
Kenc95eccd2015-12-19 07:02:34 +080041 GError *error = 0;
42 gsize bytes_read = 0;
Brad Bishop77390492016-04-13 10:47:19 -040043 gchar buf[2];
Kenc95eccd2015-12-19 07:02:34 +080044 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 );
Kenc95eccd2015-12-19 07:02:34 +080050 printf("%s\n",buf);
Brad Bishop77390492016-04-13 10:47:19 -040051
Kenc95eccd2015-12-19 07:02:34 +080052 time_t current_time = time(NULL);
Brad Bishop77390492016-04-13 10:47:19 -040053 if(gpio_button.irq_inited)
Kenc95eccd2015-12-19 07:02:34 +080054 {
55 Button* button = object_get_button((Object*)user_data);
Brad Bishop77390492016-04-13 10:47:19 -040056 if(buf[0] == '0')
Kenc95eccd2015-12-19 07:02:34 +080057 {
58 printf("reset 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("reset Button released, held for %ld seconds\n",press_time);
Brad Bishop77390492016-04-13 10:47:19 -040066 if(press_time > LONG_PRESS_SECONDS)
Kenc95eccd2015-12-19 07:02:34 +080067 {
68 button_emit_pressed_long(button);
69 } else {
70 button_emit_released(button);
71 }
72 }
Brad Bishop77390492016-04-13 10:47:19 -040073 }
Kenc95eccd2015-12-19 07:02:34 +080074 else { gpio_button.irq_inited = true; }
75
76 return TRUE;
77}
Brad Bishop77390492016-04-13 10:47:19 -040078
79static void
80on_bus_acquired(GDBusConnection *connection,
81 const gchar *name,
82 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080083{
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);
Kenc95eccd2015-12-19 07:02:34 +080087 gchar *s;
Brad Bishop77390492016-04-13 10:47:19 -040088 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
89 object = object_skeleton_new(s);
90 g_free(s);
Kenc95eccd2015-12-19 07:02:34 +080091
Brad Bishop77390492016-04-13 10:47:19 -040092 Button* button = button_skeleton_new();
93 object_skeleton_set_button(object, button);
94 g_object_unref(button);
Kenc95eccd2015-12-19 07:02:34 +080095
Kenc95eccd2015-12-19 07:02:34 +080096 //define method callbacks
Brad Bishop77390492016-04-13 10:47:19 -040097 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 */
Kenc95eccd2015-12-19 07:02:34 +0800105
Brad Bishop77390492016-04-13 10:47:19 -0400106
Kenc95eccd2015-12-19 07:02:34 +0800107 /* 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);
Kenc95eccd2015-12-19 07:02:34 +0800111
Kenc95eccd2015-12-19 07:02:34 +0800112 // get gpio device paths
113 int rc = GPIO_OK;
114 do {
115 rc = gpio_init(connection,&gpio_button);
Brad Bishop77390492016-04-13 10:47:19 -0400116 if(rc != GPIO_OK) { break; }
Kenc95eccd2015-12-19 07:02:34 +0800117 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
Brad Bishop77390492016-04-13 10:47:19 -0400118 if(rc != GPIO_OK) { break; }
Kenc95eccd2015-12-19 07:02:34 +0800119 } while(0);
Brad Bishop77390492016-04-13 10:47:19 -0400120 if(rc != GPIO_OK)
Kenc95eccd2015-12-19 07:02:34 +0800121 {
122 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
123 }
Kenc95eccd2015-12-19 07:02:34 +0800124}
125
126static void
Brad Bishop77390492016-04-13 10:47:19 -0400127on_name_acquired(GDBusConnection *connection,
128 const gchar *name,
129 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +0800130{
131}
132
133static void
Brad Bishop77390492016-04-13 10:47:19 -0400134on_name_lost(GDBusConnection *connection,
135 const gchar *name,
136 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +0800137{
138}
139
Kenc95eccd2015-12-19 07:02:34 +0800140gint
Brad Bishop77390492016-04-13 10:47:19 -0400141main(gint argc, gchar *argv[])
Kenc95eccd2015-12-19 07:02:34 +0800142{
Brad Bishop77390492016-04-13 10:47:19 -0400143 GMainLoop *loop;
Kenc95eccd2015-12-19 07:02:34 +0800144
Brad Bishop77390492016-04-13 10:47:19 -0400145 cmdline cmd;
146 cmd.argc = argc;
147 cmd.argv = argv;
Kenc95eccd2015-12-19 07:02:34 +0800148
Brad Bishop77390492016-04-13 10:47:19 -0400149 guint id;
150 loop = g_main_loop_new(NULL, FALSE);
Kenc95eccd2015-12-19 07:02:34 +0800151
Brad Bishop77390492016-04-13 10:47:19 -0400152 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);
Kenc95eccd2015-12-19 07:02:34 +0800161
Brad Bishop77390492016-04-13 10:47:19 -0400162 g_main_loop_run(loop);
163
164 g_bus_unown_name(id);
165 g_main_loop_unref(loop);
166 return 0;
Kenc95eccd2015-12-19 07:02:34 +0800167}