blob: 8e1c1ac409135080bdea9f520a26b4a9af8cb979 [file] [log] [blame]
Kenc95eccd2015-12-19 07:02:34 +08001#include <stdio.h>
Yong Li1acfd752018-04-04 13:01:23 +08002#include <stdlib.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04003#include <openbmc_intf.h>
4#include <gpio.h>
5#include <openbmc.h>
Kenc95eccd2015-12-19 07:02:34 +08006
Brad Bishop77390492016-04-13 10:47:19 -04007/* ------------------------------------------------------------------------- */
Kenc95eccd2015-12-19 07:02:34 +08008static const gchar* dbus_object_path = "/org/openbmc/buttons";
9static const gchar* instance_name = "reset0";
Brad Bishop77390492016-04-13 10:47:19 -040010static const gchar* dbus_name = "org.openbmc.buttons.reset";
Kenc95eccd2015-12-19 07:02:34 +080011static const int LONG_PRESS_SECONDS = 3;
12static GDBusObjectManagerServer *manager = NULL;
13
14//This object will use these GPIOs
Brad Bishop77390492016-04-13 10:47:19 -040015GPIO gpio_button = (GPIO){ "RESET_BUTTON" };
Kenc95eccd2015-12-19 07:02:34 +080016
17static gboolean
Brad Bishop77390492016-04-13 10:47:19 -040018on_is_on(Button *btn,
19 GDBusMethodInvocation *invocation,
20 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080021{
Brad Bishop77390492016-04-13 10:47:19 -040022 gboolean btn_state=button_get_state(btn);
23 button_complete_is_on(btn,invocation,btn_state);
24 return TRUE;
Kenc95eccd2015-12-19 07:02:34 +080025}
26
27static gboolean
Brad Bishop77390492016-04-13 10:47:19 -040028on_button_press(Button *btn,
29 GDBusMethodInvocation *invocation,
30 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080031{
32 button_emit_pressed(btn);
33 button_complete_sim_press(btn,invocation);
34 return TRUE;
35}
Brad Bishop77390492016-04-13 10:47:19 -040036
Kenc95eccd2015-12-19 07:02:34 +080037static gboolean
38on_button_interrupt( GIOChannel *channel,
Brad Bishop77390492016-04-13 10:47:19 -040039 GIOCondition condition,
40 gpointer user_data )
Kenc95eccd2015-12-19 07:02:34 +080041{
Kenc95eccd2015-12-19 07:02:34 +080042 GError *error = 0;
43 gsize bytes_read = 0;
Brad Bishop77390492016-04-13 10:47:19 -040044 gchar buf[2];
Kenc95eccd2015-12-19 07:02:34 +080045 buf[1] = '\0';
46 g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 );
Brad Bishop0c82c602016-04-13 13:36:49 -040047 g_io_channel_read_chars( channel,
Brad Bishop77390492016-04-13 10:47:19 -040048 buf, 1,
49 &bytes_read,
50 &error );
Kenc95eccd2015-12-19 07:02:34 +080051 printf("%s\n",buf);
Brad Bishop77390492016-04-13 10:47:19 -040052
Kenc95eccd2015-12-19 07:02:34 +080053 time_t current_time = time(NULL);
Brad Bishop77390492016-04-13 10:47:19 -040054 if(gpio_button.irq_inited)
Kenc95eccd2015-12-19 07:02:34 +080055 {
56 Button* button = object_get_button((Object*)user_data);
Brad Bishop77390492016-04-13 10:47:19 -040057 if(buf[0] == '0')
Kenc95eccd2015-12-19 07:02:34 +080058 {
59 printf("reset Button pressed\n");
60 button_emit_pressed(button);
61 button_set_timer(button,(long)current_time);
62 }
63 else
64 {
65 long press_time = current_time-button_get_timer(button);
66 printf("reset Button released, held for %ld seconds\n",press_time);
Brad Bishop77390492016-04-13 10:47:19 -040067 if(press_time > LONG_PRESS_SECONDS)
Kenc95eccd2015-12-19 07:02:34 +080068 {
69 button_emit_pressed_long(button);
70 } else {
71 button_emit_released(button);
72 }
73 }
Brad Bishop77390492016-04-13 10:47:19 -040074 }
Kenc95eccd2015-12-19 07:02:34 +080075 else { gpio_button.irq_inited = true; }
76
77 return TRUE;
78}
Brad Bishop77390492016-04-13 10:47:19 -040079
80static void
81on_bus_acquired(GDBusConnection *connection,
82 const gchar *name,
83 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +080084{
85 ObjectSkeleton *object;
86 //g_print ("Acquired a message bus connection: %s\n",name);
Brad Bishop77390492016-04-13 10:47:19 -040087 manager = g_dbus_object_manager_server_new(dbus_object_path);
Kenc95eccd2015-12-19 07:02:34 +080088 gchar *s;
Brad Bishop77390492016-04-13 10:47:19 -040089 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
90 object = object_skeleton_new(s);
91 g_free(s);
Kenc95eccd2015-12-19 07:02:34 +080092
Brad Bishop77390492016-04-13 10:47:19 -040093 Button* button = button_skeleton_new();
94 object_skeleton_set_button(object, button);
95 g_object_unref(button);
Kenc95eccd2015-12-19 07:02:34 +080096
Kenc95eccd2015-12-19 07:02:34 +080097 //define method callbacks
Brad Bishop77390492016-04-13 10:47:19 -040098 g_signal_connect(button,
99 "handle-is-on",
100 G_CALLBACK(on_is_on),
101 NULL); /* user_data */
102 g_signal_connect(button,
103 "handle-sim-press",
104 G_CALLBACK(on_button_press),
105 NULL); /* user_data */
Kenc95eccd2015-12-19 07:02:34 +0800106
Brad Bishop77390492016-04-13 10:47:19 -0400107
Kenc95eccd2015-12-19 07:02:34 +0800108 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400109 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400110 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
111 g_object_unref(object);
Kenc95eccd2015-12-19 07:02:34 +0800112
Kenc95eccd2015-12-19 07:02:34 +0800113 // get gpio device paths
114 int rc = GPIO_OK;
115 do {
116 rc = gpio_init(connection,&gpio_button);
Brad Bishop77390492016-04-13 10:47:19 -0400117 if(rc != GPIO_OK) { break; }
Kenc95eccd2015-12-19 07:02:34 +0800118 rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object);
Brad Bishop77390492016-04-13 10:47:19 -0400119 if(rc != GPIO_OK) { break; }
Kenc95eccd2015-12-19 07:02:34 +0800120 } while(0);
Brad Bishop77390492016-04-13 10:47:19 -0400121 if(rc != GPIO_OK)
Kenc95eccd2015-12-19 07:02:34 +0800122 {
123 printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc);
Yong Li1acfd752018-04-04 13:01:23 +0800124 exit(-1);
Kenc95eccd2015-12-19 07:02:34 +0800125 }
Kenc95eccd2015-12-19 07:02:34 +0800126}
127
128static void
Brad Bishop77390492016-04-13 10:47:19 -0400129on_name_acquired(GDBusConnection *connection,
130 const gchar *name,
131 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +0800132{
133}
134
135static void
Brad Bishop77390492016-04-13 10:47:19 -0400136on_name_lost(GDBusConnection *connection,
137 const gchar *name,
138 gpointer user_data)
Kenc95eccd2015-12-19 07:02:34 +0800139{
140}
141
Kenc95eccd2015-12-19 07:02:34 +0800142gint
Brad Bishop77390492016-04-13 10:47:19 -0400143main(gint argc, gchar *argv[])
Kenc95eccd2015-12-19 07:02:34 +0800144{
Brad Bishop77390492016-04-13 10:47:19 -0400145 GMainLoop *loop;
Kenc95eccd2015-12-19 07:02:34 +0800146
Brad Bishop77390492016-04-13 10:47:19 -0400147 cmdline cmd;
148 cmd.argc = argc;
149 cmd.argv = argv;
Kenc95eccd2015-12-19 07:02:34 +0800150
Brad Bishop77390492016-04-13 10:47:19 -0400151 guint id;
152 loop = g_main_loop_new(NULL, FALSE);
Kenc95eccd2015-12-19 07:02:34 +0800153
Brad Bishop77390492016-04-13 10:47:19 -0400154 id = g_bus_own_name(DBUS_TYPE,
155 dbus_name,
156 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
157 G_BUS_NAME_OWNER_FLAGS_REPLACE,
158 on_bus_acquired,
159 on_name_acquired,
160 on_name_lost,
161 &cmd,
162 NULL);
Kenc95eccd2015-12-19 07:02:34 +0800163
Brad Bishop77390492016-04-13 10:47:19 -0400164 g_main_loop_run(loop);
165
166 g_bus_unown_name(id);
167 g_main_loop_unref(loop);
168 return 0;
Kenc95eccd2015-12-19 07:02:34 +0800169}