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