Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 1 | #include "interfaces/openbmc_intf.h"
|
| 2 | #include <stdio.h>
|
| 3 | #include "openbmc.h"
|
| 4 | #include "gpio.h"
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 5 | #include "object_mapper.h"
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 6 |
|
| 7 | /* ---------------------------------------------------------------------------------------------------- */
|
| 8 | static const gchar* dbus_object_path = "/org/openbmc/control/led";
|
| 9 | static const gchar* dbus_name = "org.openbmc.control.led";
|
| 10 |
|
| 11 | static GDBusObjectManagerServer *manager = NULL;
|
| 12 |
|
| 13 | #define NUM_GPIO 2
|
| 14 |
|
| 15 | GPIO led_gpio[NUM_GPIO] = {
|
| 16 | (GPIO){"IDENTIFY"},
|
| 17 | (GPIO){"BMC_READY"}
|
| 18 | };
|
| 19 |
|
| 20 |
|
| 21 | static gboolean
|
| 22 | on_set_on (Led *led,
|
| 23 | GDBusMethodInvocation *invocation,
|
| 24 | gpointer user_data)
|
| 25 | {
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 26 | GPIO* mygpio = (GPIO*)user_data;
|
Norman James | 3791d9c | 2015-10-28 18:58:59 -0500 | [diff] [blame] | 27 | g_print("Turn on LED: %s\n",mygpio->name);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 28 | led_complete_set_on(led,invocation);
|
| 29 | int rc = GPIO_OK;
|
| 30 | do {
|
| 31 | rc = gpio_open(mygpio);
|
| 32 | if (rc != GPIO_OK) { break; }
|
Norman James | 3791d9c | 2015-10-28 18:58:59 -0500 | [diff] [blame] | 33 | rc = gpio_write(mygpio,0);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 34 | if (rc != GPIO_OK) { break; }
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 35 | led_set_state(led,"on");
|
| 36 |
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 37 | } while(0);
|
| 38 | gpio_close(mygpio);
|
| 39 | if (rc != GPIO_OK)
|
| 40 | {
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 41 | g_print("ERROR ledcontrol: GPIO error %s (rc=%d)\n",mygpio->name,rc);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 42 | }
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 43 | return TRUE;
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 44 | }
|
| 45 |
|
| 46 | static gboolean
|
| 47 | on_set_off (Led *led,
|
| 48 | GDBusMethodInvocation *invocation,
|
| 49 | gpointer user_data)
|
| 50 | {
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 51 | GPIO* mygpio = (GPIO*)user_data;
|
Norman James | 3791d9c | 2015-10-28 18:58:59 -0500 | [diff] [blame] | 52 | g_print("Turn off LED: %s\n",mygpio->name);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 53 | led_complete_set_off(led,invocation);
|
| 54 | int rc = GPIO_OK;
|
| 55 | do {
|
| 56 | rc = gpio_open(mygpio);
|
| 57 | if (rc != GPIO_OK) { break; }
|
Norman James | 3791d9c | 2015-10-28 18:58:59 -0500 | [diff] [blame] | 58 | rc = gpio_write(mygpio,1);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 59 | if (rc != GPIO_OK) { break; }
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 60 | led_set_state(led,"off");
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 61 | } while(0);
|
| 62 | gpio_close(mygpio);
|
| 63 | if (rc != GPIO_OK)
|
| 64 | {
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 65 | g_print("ERROR led controller: GPIO error %s (rc=%d)\n",mygpio->name,rc);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 66 | }
|
| 67 | return TRUE;
|
| 68 | }
|
| 69 |
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 70 | void init_led(Led* led, GPIO* mygpio)
|
| 71 | {
|
| 72 | int rc = GPIO_OK;
|
| 73 | do {
|
| 74 | uint8_t val;
|
| 75 | rc = gpio_open(mygpio);
|
| 76 | if (rc != GPIO_OK) { break; }
|
| 77 | rc = gpio_read(mygpio,&val);
|
| 78 | if (rc != GPIO_OK) { break; }
|
| 79 | if (val == 0) {
|
| 80 | led_set_state(led,"on");
|
| 81 | } else {
|
| 82 | led_set_state(led,"off");
|
| 83 | }
|
| 84 | } while(0);
|
| 85 | gpio_close(mygpio);
|
| 86 | if (rc != GPIO_OK) {
|
| 87 | g_print("ERROR led controller: GPIO error %s (rc=%d)\n",
|
| 88 | mygpio->name,rc);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 92 | static void
|
| 93 | on_bus_acquired (GDBusConnection *connection,
|
| 94 | const gchar *name,
|
| 95 | gpointer user_data)
|
| 96 | {
|
| 97 | ObjectSkeleton *object;
|
| 98 |
|
| 99 | cmdline *cmd = user_data;
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 100 |
|
| 101 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 102 | int i = 0;
|
| 103 | for (i=0;i<NUM_GPIO;i++)
|
| 104 | {
|
| 105 | gchar *s;
|
| 106 | s = g_strdup_printf ("%s/%s",dbus_object_path,led_gpio[i].name);
|
| 107 | object = object_skeleton_new (s);
|
| 108 | g_free (s);
|
| 109 |
|
| 110 | Led *led = led_skeleton_new ();
|
| 111 | object_skeleton_set_led (object, led);
|
| 112 | g_object_unref (led);
|
| 113 |
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 114 | ObjectMapper* mapper = object_mapper_skeleton_new ();
|
| 115 | object_skeleton_set_object_mapper (object, mapper);
|
| 116 | g_object_unref (mapper);
|
| 117 |
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 118 | //define method callbacks
|
| 119 | g_signal_connect (led,
|
| 120 | "handle-set-on",
|
| 121 | G_CALLBACK (on_set_on),
|
| 122 | &led_gpio[i]); /* user_data */
|
| 123 | g_signal_connect (led,
|
| 124 | "handle-set-off",
|
| 125 | G_CALLBACK (on_set_off),
|
| 126 | &led_gpio[i]);
|
| 127 |
|
| 128 | led_set_color(led,0);
|
| 129 | led_set_function(led,led_gpio[i].name);
|
| 130 |
|
| 131 | gpio_init(connection,&led_gpio[i]);
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 132 | init_led(led, &led_gpio[i]);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 133 | /* Export the object (@manager takes its own reference to @object) */
|
| 134 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 135 | g_object_unref (object);
|
| 136 | }
|
| 137 | /* Export all objects */
|
| 138 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 139 | emit_object_added((GDBusObjectManager*)manager);
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 140 | }
|
| 141 |
|
| 142 | static void
|
| 143 | on_name_acquired (GDBusConnection *connection,
|
| 144 | const gchar *name,
|
| 145 | gpointer user_data)
|
| 146 | {
|
| 147 | }
|
| 148 |
|
| 149 | static void
|
| 150 | on_name_lost (GDBusConnection *connection,
|
| 151 | const gchar *name,
|
| 152 | gpointer user_data)
|
| 153 | {
|
| 154 | }
|
| 155 |
|
| 156 |
|
| 157 | gint
|
| 158 | main (gint argc, gchar *argv[])
|
| 159 | {
|
| 160 | GMainLoop *loop;
|
| 161 | cmdline cmd;
|
| 162 | cmd.argc = argc;
|
| 163 | cmd.argv = argv;
|
| 164 |
|
| 165 | guint id;
|
| 166 | loop = g_main_loop_new (NULL, FALSE);
|
| 167 |
|
| 168 | id = g_bus_own_name (DBUS_TYPE,
|
| 169 | dbus_name,
|
| 170 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 171 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 172 | on_bus_acquired,
|
| 173 | on_name_acquired,
|
| 174 | on_name_lost,
|
| 175 | &cmd,
|
| 176 | NULL);
|
| 177 |
|
| 178 | g_main_loop_run (loop);
|
| 179 |
|
| 180 | g_bus_unown_name (id);
|
| 181 | g_main_loop_unref (loop);
|
| 182 | return 0;
|
| 183 | }
|