blob: 1240b30e1e8256abe1c098f2b511a4ea5dc8754d [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"
Norman James877dd412015-10-31 17:33:25 -05005#include "object_mapper.h"
Norman James772232e2015-10-18 14:43:28 -05006
7/* ---------------------------------------------------------------------------------------------------- */
8static const gchar* dbus_object_path = "/org/openbmc/control/led";
9static const gchar* dbus_name = "org.openbmc.control.led";
10
11static GDBusObjectManagerServer *manager = NULL;
12
Ken2fd67f92015-12-09 01:41:23 +080013#define NUM_GPIO 4
Norman James772232e2015-10-18 14:43:28 -050014
15GPIO led_gpio[NUM_GPIO] = {
16 (GPIO){"IDENTIFY"},
Ken2fd67f92015-12-09 01:41:23 +080017 (GPIO){"BMC_READY"},
18 (GPIO){"BEEP"},
19 (GPIO){"HEART_BEAT"}
Norman James772232e2015-10-18 14:43:28 -050020};
21
22
23static gboolean
24on_set_on (Led *led,
25 GDBusMethodInvocation *invocation,
26 gpointer user_data)
27{
Norman James772232e2015-10-18 14:43:28 -050028 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050029 g_print("Turn on LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050030 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 James3791d9c2015-10-28 18:58:59 -050035 rc = gpio_write(mygpio,0);
Norman James772232e2015-10-18 14:43:28 -050036 if (rc != GPIO_OK) { break; }
Norman James877dd412015-10-31 17:33:25 -050037 led_set_state(led,"on");
38
Norman James772232e2015-10-18 14:43:28 -050039 } while(0);
40 gpio_close(mygpio);
41 if (rc != GPIO_OK)
42 {
Norman James877dd412015-10-31 17:33:25 -050043 g_print("ERROR ledcontrol: GPIO error %s (rc=%d)\n",mygpio->name,rc);
Norman James772232e2015-10-18 14:43:28 -050044 }
Norman James772232e2015-10-18 14:43:28 -050045 return TRUE;
Norman James772232e2015-10-18 14:43:28 -050046}
47
48static gboolean
49on_set_off (Led *led,
50 GDBusMethodInvocation *invocation,
51 gpointer user_data)
52{
Norman James772232e2015-10-18 14:43:28 -050053 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050054 g_print("Turn off LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050055 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 James3791d9c2015-10-28 18:58:59 -050060 rc = gpio_write(mygpio,1);
Norman James772232e2015-10-18 14:43:28 -050061 if (rc != GPIO_OK) { break; }
Norman James877dd412015-10-31 17:33:25 -050062 led_set_state(led,"off");
Norman James772232e2015-10-18 14:43:28 -050063 } while(0);
64 gpio_close(mygpio);
65 if (rc != GPIO_OK)
66 {
Norman James877dd412015-10-31 17:33:25 -050067 g_print("ERROR led controller: GPIO error %s (rc=%d)\n",mygpio->name,rc);
Norman James772232e2015-10-18 14:43:28 -050068 }
69 return TRUE;
70}
71
Norman James877dd412015-10-31 17:33:25 -050072void 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 James772232e2015-10-18 14:43:28 -050094static void
95on_bus_acquired (GDBusConnection *connection,
96 const gchar *name,
97 gpointer user_data)
98{
99 ObjectSkeleton *object;
100
101 cmdline *cmd = user_data;
Norman James772232e2015-10-18 14:43:28 -0500102
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 James877dd412015-10-31 17:33:25 -0500116 ObjectMapper* mapper = object_mapper_skeleton_new ();
117 object_skeleton_set_object_mapper (object, mapper);
118 g_object_unref (mapper);
119
Norman James772232e2015-10-18 14:43:28 -0500120 //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 James877dd412015-10-31 17:33:25 -0500134 init_led(led, &led_gpio[i]);
Norman James772232e2015-10-18 14:43:28 -0500135 /* 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 James877dd412015-10-31 17:33:25 -0500141 emit_object_added((GDBusObjectManager*)manager);
Norman James772232e2015-10-18 14:43:28 -0500142}
143
144static void
145on_name_acquired (GDBusConnection *connection,
146 const gchar *name,
147 gpointer user_data)
148{
149}
150
151static void
152on_name_lost (GDBusConnection *connection,
153 const gchar *name,
154 gpointer user_data)
155{
156}
157
158
159gint
160main (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}