Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 1 | #include "interfaces/button.h"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 2 | #include "gpio.h"
|
| 3 | #include "openbmc.h"
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 4 |
|
| 5 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 6 | static const gchar* dbus_object_path = "/org/openbmc/buttons/ButtonPower";
|
| 7 | static const gchar* dbus_name = "org.openbmc.buttons.ButtonPower";
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 8 |
|
| 9 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 10 |
|
| 11 | static gboolean
|
| 12 | on_is_on (Button *btn,
|
| 13 | GDBusMethodInvocation *invocation,
|
| 14 | gpointer user_data)
|
| 15 | {
|
| 16 | gboolean btn_state=button_get_state(btn);
|
| 17 | button_complete_is_on(btn,invocation,btn_state);
|
| 18 | return TRUE;
|
| 19 |
|
| 20 | }
|
| 21 |
|
| 22 | static gboolean
|
| 23 | on_sim_button_press (Button *btn,
|
| 24 | GDBusMethodInvocation *invocation,
|
| 25 | gpointer user_data)
|
| 26 | {
|
| 27 | g_print("Simulating button pressed\n");
|
| 28 | button_emit_button_pressed(btn);
|
| 29 | button_complete_sim_button_press(btn,invocation);
|
| 30 | return TRUE;
|
| 31 |
|
| 32 | }
|
| 33 |
|
| 34 | static void
|
| 35 | on_bus_acquired (GDBusConnection *connection,
|
| 36 | const gchar *name,
|
| 37 | gpointer user_data)
|
| 38 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 39 | ObjectSkeleton *object;
|
| 40 | g_print ("Acquired a message bus connection: %s\n",name);
|
| 41 | cmdline *cmd = user_data;
|
| 42 | if (cmd->argc < 2)
|
| 43 | {
|
| 44 | g_print("No objects created. Put object name(s) on command line\n");
|
| 45 | return;
|
| 46 | }
|
| 47 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 48 | int i=0;
|
| 49 | for (i=1;i<cmd->argc;i++)
|
| 50 | {
|
| 51 | gchar *s;
|
| 52 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
|
| 53 | object = object_skeleton_new (s);
|
| 54 | g_free (s);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 55 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 56 | Button* button = button_skeleton_new ();
|
| 57 | object_skeleton_set_button (object, button);
|
| 58 | g_object_unref (button);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 59 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 60 | //define method callbacks
|
| 61 | g_signal_connect (button,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 62 | "handle-is-on",
|
| 63 | G_CALLBACK (on_is_on),
|
| 64 | NULL); /* user_data */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 65 | g_signal_connect (button,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 66 | "handle-sim-button-press",
|
| 67 | G_CALLBACK (on_sim_button_press),
|
| 68 | NULL); /* user_data */
|
| 69 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 70 | /* Export the object (@manager takes its own reference to @object) */
|
| 71 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 72 | g_object_unref (object);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 73 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 74 | }
|
| 75 | /* Export all objects */
|
| 76 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 77 | }
|
| 78 |
|
| 79 | static void
|
| 80 | on_name_acquired (GDBusConnection *connection,
|
| 81 | const gchar *name,
|
| 82 | gpointer user_data)
|
| 83 | {
|
| 84 | g_print ("Acquired the name %s\n", name);
|
| 85 | }
|
| 86 |
|
| 87 | static void
|
| 88 | on_name_lost (GDBusConnection *connection,
|
| 89 | const gchar *name,
|
| 90 | gpointer user_data)
|
| 91 | {
|
| 92 | g_print ("Lost the name %s\n", name);
|
| 93 | }
|
| 94 |
|
| 95 |
|
| 96 | gint
|
| 97 | main (gint argc, gchar *argv[])
|
| 98 | {
|
| 99 | GMainLoop *loop;
|
| 100 |
|
Norman James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 101 | cmdline cmd;
|
| 102 | cmd.argc = argc;
|
| 103 | cmd.argv = argv;
|
| 104 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 105 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 106 | loop = g_main_loop_new (NULL, FALSE);
|
| 107 |
|
| 108 | id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 109 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 110 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 111 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 112 | on_bus_acquired,
|
| 113 | on_name_acquired,
|
| 114 | on_name_lost,
|
Norman James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 115 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 116 | NULL);
|
| 117 |
|
| 118 | g_main_loop_run (loop);
|
| 119 |
|
| 120 | g_bus_unown_name (id);
|
| 121 | g_main_loop_unref (loop);
|
| 122 | return 0;
|
| 123 | }
|