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