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