Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
Yong Li | 1acfd75 | 2018-04-04 13:01:23 +0800 | [diff] [blame] | 2 | #include <stdlib.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 3 | #include <openbmc_intf.h> |
| 4 | #include <gpio.h> |
| 5 | #include <openbmc.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 6 | |
| 7 | /* ------------------------------------------------------------------------- */ |
| 8 | static const gchar* dbus_object_path = "/org/openbmc/buttons"; |
| 9 | static const gchar* instance_name = "power0"; |
| 10 | static const gchar* dbus_name = "org.openbmc.buttons.Power"; |
| 11 | static const int LONG_PRESS_SECONDS = 3; |
| 12 | static GDBusObjectManagerServer *manager = NULL; |
| 13 | |
| 14 | //This object will use these GPIOs |
| 15 | GPIO gpio_button = (GPIO){ "POWER_BUTTON" }; |
| 16 | |
| 17 | static gboolean |
| 18 | on_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 | static gboolean |
| 28 | on_button_press(Button *btn, |
| 29 | GDBusMethodInvocation *invocation, |
| 30 | gpointer user_data) |
| 31 | { |
| 32 | button_emit_pressed(btn); |
| 33 | button_complete_sim_press(btn,invocation); |
| 34 | return TRUE; |
| 35 | } |
| 36 | |
| 37 | static gboolean |
| 38 | on_button_interrupt( GIOChannel *channel, |
| 39 | GIOCondition condition, |
| 40 | gpointer user_data ) |
| 41 | { |
| 42 | GError *error = 0; |
| 43 | gsize bytes_read = 0; |
| 44 | gchar buf[2]; |
| 45 | buf[1] = '\0'; |
| 46 | g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 ); |
Brad Bishop | 0c82c60 | 2016-04-13 13:36:49 -0400 | [diff] [blame] | 47 | g_io_channel_read_chars(channel, |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 48 | buf, 1, |
| 49 | &bytes_read, |
| 50 | &error ); |
| 51 | printf("%s\n",buf); |
| 52 | |
| 53 | time_t current_time = time(NULL); |
| 54 | if(gpio_button.irq_inited) |
| 55 | { |
| 56 | Button* button = object_get_button((Object*)user_data); |
| 57 | if(buf[0] == '0') |
| 58 | { |
| 59 | printf("Power 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("Power Button released, held for %ld seconds\n",press_time); |
| 67 | if(press_time > LONG_PRESS_SECONDS) |
| 68 | { |
| 69 | button_emit_pressed_long(button); |
| 70 | } else { |
| 71 | button_emit_released(button); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | else { gpio_button.irq_inited = true; } |
| 76 | |
| 77 | return TRUE; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | on_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); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 87 | manager = g_dbus_object_manager_server_new(dbus_object_path); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 88 | gchar *s; |
| 89 | s = g_strdup_printf("%s/%s",dbus_object_path,instance_name); |
| 90 | object = object_skeleton_new(s); |
| 91 | g_free(s); |
| 92 | |
| 93 | Button* button = button_skeleton_new(); |
| 94 | object_skeleton_set_button(object, button); |
| 95 | g_object_unref(button); |
| 96 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 97 | //define method callbacks |
| 98 | 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 */ |
| 106 | |
| 107 | |
| 108 | /* Export the object (@manager takes its own reference to @object) */ |
Brad Bishop | 58e694d | 2016-04-13 16:04:32 -0400 | [diff] [blame] | 109 | g_dbus_object_manager_server_set_connection(manager, connection); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 110 | g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object)); |
| 111 | g_object_unref(object); |
| 112 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 113 | // get gpio device paths |
| 114 | int rc = GPIO_OK; |
| 115 | do { |
Matt Spinler | 0f3fd5a | 2018-08-08 11:15:26 -0500 | [diff] [blame] | 116 | rc = gpio_init(&gpio_button); |
Matt Spinler | 0f0946b | 2018-08-07 16:26:55 -0500 | [diff] [blame] | 117 | gpio_inits_done(); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 118 | if(rc != GPIO_OK) { break; } |
| 119 | rc = gpio_open_interrupt(&gpio_button,on_button_interrupt,object); |
| 120 | if(rc != GPIO_OK) { break; } |
| 121 | } while(0); |
| 122 | if(rc != GPIO_OK) |
| 123 | { |
| 124 | printf("ERROR PowerButton: GPIO setup (rc=%d)\n",rc); |
Yong Li | 1acfd75 | 2018-04-04 13:01:23 +0800 | [diff] [blame] | 125 | exit(-1); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 126 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void |
| 130 | on_name_acquired(GDBusConnection *connection, |
| 131 | const gchar *name, |
| 132 | gpointer user_data) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | on_name_lost(GDBusConnection *connection, |
| 138 | const gchar *name, |
| 139 | gpointer user_data) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | gint |
| 144 | main(gint argc, gchar *argv[]) |
| 145 | { |
| 146 | GMainLoop *loop; |
| 147 | |
| 148 | cmdline cmd; |
| 149 | cmd.argc = argc; |
| 150 | cmd.argv = argv; |
| 151 | |
| 152 | guint id; |
| 153 | loop = g_main_loop_new(NULL, FALSE); |
| 154 | |
| 155 | id = g_bus_own_name(DBUS_TYPE, |
| 156 | dbus_name, |
| 157 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 158 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 159 | on_bus_acquired, |
| 160 | on_name_acquired, |
| 161 | on_name_lost, |
| 162 | &cmd, |
| 163 | NULL); |
| 164 | |
| 165 | g_main_loop_run(loop); |
| 166 | |
| 167 | g_bus_unown_name(id); |
| 168 | g_main_loop_unref(loop); |
| 169 | return 0; |
| 170 | } |