blob: 29698fe9d3ebc534286f304e9d7edd56f09954aa [file] [log] [blame]
Norman James772232e2015-10-18 14:43:28 -05001#include "interfaces/openbmc_intf.h"
2#include <stdio.h>
3#include "openbmc.h"
4#include "gpio.h"
5
6/* ---------------------------------------------------------------------------------------------------- */
7static const gchar* dbus_object_path = "/org/openbmc/control/led";
8static const gchar* dbus_name = "org.openbmc.control.led";
9
10static GDBusObjectManagerServer *manager = NULL;
11
12#define NUM_GPIO 2
13
14GPIO led_gpio[NUM_GPIO] = {
15 (GPIO){"IDENTIFY"},
16 (GPIO){"BMC_READY"}
17};
18
19
20static gboolean
21on_set_on (Led *led,
22 GDBusMethodInvocation *invocation,
23 gpointer user_data)
24{
Norman James772232e2015-10-18 14:43:28 -050025 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050026 g_print("Turn on LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050027 led_complete_set_on(led,invocation);
28 int rc = GPIO_OK;
29 do {
30 rc = gpio_open(mygpio);
31 if (rc != GPIO_OK) { break; }
Norman James3791d9c2015-10-28 18:58:59 -050032 rc = gpio_write(mygpio,0);
Norman James772232e2015-10-18 14:43:28 -050033 if (rc != GPIO_OK) { break; }
34 } while(0);
35 gpio_close(mygpio);
36 if (rc != GPIO_OK)
37 {
38 printf("ERROR ledcontrol: GPIO error %s (rc=%d)\n",mygpio->name,rc);
39 }
40
41 return TRUE;
42
43}
44
45static gboolean
46on_set_off (Led *led,
47 GDBusMethodInvocation *invocation,
48 gpointer user_data)
49{
Norman James772232e2015-10-18 14:43:28 -050050 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050051 g_print("Turn off LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050052 led_complete_set_off(led,invocation);
53 int rc = GPIO_OK;
54 do {
55 rc = gpio_open(mygpio);
56 if (rc != GPIO_OK) { break; }
Norman James3791d9c2015-10-28 18:58:59 -050057 rc = gpio_write(mygpio,1);
Norman James772232e2015-10-18 14:43:28 -050058 if (rc != GPIO_OK) { break; }
59 } while(0);
60 gpio_close(mygpio);
61 if (rc != GPIO_OK)
62 {
63 printf("ERROR ChassisIdentify: GPIO error %s (rc=%d)\n",mygpio->name,rc);
64 }
65 return TRUE;
66}
67
68static void
69on_bus_acquired (GDBusConnection *connection,
70 const gchar *name,
71 gpointer user_data)
72{
73 ObjectSkeleton *object;
74
75 cmdline *cmd = user_data;
Norman James772232e2015-10-18 14:43:28 -050076
77 manager = g_dbus_object_manager_server_new (dbus_object_path);
78 int i = 0;
79 for (i=0;i<NUM_GPIO;i++)
80 {
81 gchar *s;
82 s = g_strdup_printf ("%s/%s",dbus_object_path,led_gpio[i].name);
83 object = object_skeleton_new (s);
84 g_free (s);
85
86 Led *led = led_skeleton_new ();
87 object_skeleton_set_led (object, led);
88 g_object_unref (led);
89
90 //define method callbacks
91 g_signal_connect (led,
92 "handle-set-on",
93 G_CALLBACK (on_set_on),
94 &led_gpio[i]); /* user_data */
95 g_signal_connect (led,
96 "handle-set-off",
97 G_CALLBACK (on_set_off),
98 &led_gpio[i]);
99
100 led_set_color(led,0);
101 led_set_function(led,led_gpio[i].name);
102
103 gpio_init(connection,&led_gpio[i]);
104 /* Export the object (@manager takes its own reference to @object) */
105 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
106 g_object_unref (object);
107 }
108 /* Export all objects */
109 g_dbus_object_manager_server_set_connection (manager, connection);
110
111}
112
113static void
114on_name_acquired (GDBusConnection *connection,
115 const gchar *name,
116 gpointer user_data)
117{
118}
119
120static void
121on_name_lost (GDBusConnection *connection,
122 const gchar *name,
123 gpointer user_data)
124{
125}
126
127
128gint
129main (gint argc, gchar *argv[])
130{
131 GMainLoop *loop;
132 cmdline cmd;
133 cmd.argc = argc;
134 cmd.argv = argv;
135
136 guint id;
137 loop = g_main_loop_new (NULL, FALSE);
138
139 id = g_bus_own_name (DBUS_TYPE,
140 dbus_name,
141 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
142 G_BUS_NAME_OWNER_FLAGS_REPLACE,
143 on_bus_acquired,
144 on_name_acquired,
145 on_name_lost,
146 &cmd,
147 NULL);
148
149 g_main_loop_run (loop);
150
151 g_bus_unown_name (id);
152 g_main_loop_unref (loop);
153 return 0;
154}