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