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