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