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