blob: 1206d17bb146d55255199dede128d26479ceffe2 [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -05001#include "interfaces/openbmc_intf.h"
Norman James3d3b7fb2015-10-06 16:54:06 -05002#include <stdio.h>
Norman James10ff6a32015-08-27 14:24:17 -05003#include "openbmc.h"
4#include "gpio.h"
Norman Jamese2765102015-08-19 22:00:55 -05005
6/* ---------------------------------------------------------------------------------------------------- */
Norman James362a80f2015-09-14 14:04:39 -05007static const gchar* dbus_object_path = "/org/openbmc/leds";
Norman James3f97c5d2015-08-26 17:44:14 -05008static const gchar* dbus_name = "org.openbmc.leds.ChassisIdentify";
Norman Jamese2765102015-08-19 22:00:55 -05009
10static GDBusObjectManagerServer *manager = NULL;
Norman James10ff6a32015-08-27 14:24:17 -050011
12GPIO led_gpio = (GPIO){"IDENTIFY"};
Norman Jamese2765102015-08-19 22:00:55 -050013
14static gboolean
15on_set_on (Led *led,
16 GDBusMethodInvocation *invocation,
17 gpointer user_data)
18{
Norman James3d3b7fb2015-10-06 16:54:06 -050019 printf("Turn on chassis identify led\n");
Norman James10ff6a32015-08-27 14:24:17 -050020 led_complete_set_on(led,invocation);
Norman James19e45912015-10-04 20:19:41 -050021 int rc = GPIO_OK;
22 do {
23 rc = gpio_open(&led_gpio);
24 if (rc != GPIO_OK) { break; }
25 rc = gpio_write(&led_gpio,1);
26 if (rc != GPIO_OK) { break; }
27 } while(0);
Norman James10ff6a32015-08-27 14:24:17 -050028 gpio_close(&led_gpio);
Norman James19e45912015-10-04 20:19:41 -050029 if (rc != GPIO_OK)
30 {
Norman James3d3b7fb2015-10-06 16:54:06 -050031 printf("ERROR ChassisIdentify: GPIO error %s (rc=%d)\n",led_gpio.name,rc);
Norman James19e45912015-10-04 20:19:41 -050032 }
Norman James10ff6a32015-08-27 14:24:17 -050033
34 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050035
36}
37
38static gboolean
39on_set_off (Led *led,
40 GDBusMethodInvocation *invocation,
41 gpointer user_data)
42{
Norman James10ff6a32015-08-27 14:24:17 -050043 g_print("Turn off chassis identify led\n");
44 led_complete_set_off(led,invocation);
Norman James19e45912015-10-04 20:19:41 -050045 int rc = GPIO_OK;
46 do {
47 rc = gpio_open(&led_gpio);
48 if (rc != GPIO_OK) { break; }
49 rc = gpio_write(&led_gpio,0);
50 if (rc != GPIO_OK) { break; }
51 } while(0);
Norman James10ff6a32015-08-27 14:24:17 -050052 gpio_close(&led_gpio);
Norman James19e45912015-10-04 20:19:41 -050053 if (rc != GPIO_OK)
54 {
Norman James3d3b7fb2015-10-06 16:54:06 -050055 printf("ERROR ChassisIdentify: GPIO error %s (rc=%d)\n",led_gpio.name,rc);
Norman James19e45912015-10-04 20:19:41 -050056 }
Norman James10ff6a32015-08-27 14:24:17 -050057 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050058}
59
60static void
61on_bus_acquired (GDBusConnection *connection,
62 const gchar *name,
63 gpointer user_data)
64{
Norman James10ff6a32015-08-27 14:24:17 -050065 ObjectSkeleton *object;
Norman Jamese2765102015-08-19 22:00:55 -050066
Norman James10ff6a32015-08-27 14:24:17 -050067 cmdline *cmd = user_data;
68 if (cmd->argc < 2)
69 {
70 g_print("No objects created. Put object name(s) on command line\n");
71 return;
72 }
Norman Jamese2765102015-08-19 22:00:55 -050073
Norman James10ff6a32015-08-27 14:24:17 -050074 manager = g_dbus_object_manager_server_new (dbus_object_path);
75 int i = 0;
76 for (i=1;i<cmd->argc;i++)
77 {
78 gchar *s;
79 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
80 object = object_skeleton_new (s);
81 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050082
Norman James10ff6a32015-08-27 14:24:17 -050083 Led *led = led_skeleton_new ();
84 object_skeleton_set_led (object, led);
85 g_object_unref (led);
Norman Jamese2765102015-08-19 22:00:55 -050086
Norman James10ff6a32015-08-27 14:24:17 -050087 //define method callbacks
88 g_signal_connect (led,
Norman Jamese2765102015-08-19 22:00:55 -050089 "handle-set-on",
90 G_CALLBACK (on_set_on),
91 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050092 g_signal_connect (led,
Norman Jamese2765102015-08-19 22:00:55 -050093 "handle-set-off",
94 G_CALLBACK (on_set_off),
95 NULL);
96
Norman James10ff6a32015-08-27 14:24:17 -050097 led_set_color(led,0);
98 led_set_function(led,"CHASSIS_IDENTIFY");
Norman Jamese2765102015-08-19 22:00:55 -050099
Norman James10ff6a32015-08-27 14:24:17 -0500100 /* Export the object (@manager takes its own reference to @object) */
101 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
102 g_object_unref (object);
103 }
104 /* Export all objects */
105 g_dbus_object_manager_server_set_connection (manager, connection);
106 gpio_init(connection,&led_gpio);
Norman Jamese2765102015-08-19 22:00:55 -0500107
Norman Jamese2765102015-08-19 22:00:55 -0500108}
109
110static void
111on_name_acquired (GDBusConnection *connection,
112 const gchar *name,
113 gpointer user_data)
114{
Norman Jamese2765102015-08-19 22:00:55 -0500115}
116
117static void
118on_name_lost (GDBusConnection *connection,
119 const gchar *name,
120 gpointer user_data)
121{
Norman Jamese2765102015-08-19 22:00:55 -0500122}
123
124
125gint
126main (gint argc, gchar *argv[])
127{
128 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500129 cmdline cmd;
130 cmd.argc = argc;
131 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500132
133 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500134 loop = g_main_loop_new (NULL, FALSE);
135
Norman James5e792e32015-10-07 17:36:17 -0500136 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500137 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500138 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
139 G_BUS_NAME_OWNER_FLAGS_REPLACE,
140 on_bus_acquired,
141 on_name_acquired,
142 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500143 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500144 NULL);
145
146 g_main_loop_run (loop);
147
148 g_bus_unown_name (id);
149 g_main_loop_unref (loop);
150 return 0;
151}