blob: 8c3fa3c4a56ab1a6d4109c69fc15c7cf00cfe4d7 [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
13#define NUM_GPIO 2
14
15GPIO led_gpio[NUM_GPIO] = {
16 (GPIO){"IDENTIFY"},
17 (GPIO){"BMC_READY"}
18};
19
20
21static gboolean
22on_set_on (Led *led,
23 GDBusMethodInvocation *invocation,
24 gpointer user_data)
25{
Norman James772232e2015-10-18 14:43:28 -050026 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050027 g_print("Turn on LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050028 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 James3791d9c2015-10-28 18:58:59 -050033 rc = gpio_write(mygpio,0);
Norman James772232e2015-10-18 14:43:28 -050034 if (rc != GPIO_OK) { break; }
Norman James877dd412015-10-31 17:33:25 -050035 led_set_state(led,"on");
36
Norman James772232e2015-10-18 14:43:28 -050037 } while(0);
38 gpio_close(mygpio);
39 if (rc != GPIO_OK)
40 {
Norman James877dd412015-10-31 17:33:25 -050041 g_print("ERROR ledcontrol: GPIO error %s (rc=%d)\n",mygpio->name,rc);
Norman James772232e2015-10-18 14:43:28 -050042 }
Norman James772232e2015-10-18 14:43:28 -050043 return TRUE;
Norman James772232e2015-10-18 14:43:28 -050044}
45
46static gboolean
47on_set_off (Led *led,
48 GDBusMethodInvocation *invocation,
49 gpointer user_data)
50{
Norman James772232e2015-10-18 14:43:28 -050051 GPIO* mygpio = (GPIO*)user_data;
Norman James3791d9c2015-10-28 18:58:59 -050052 g_print("Turn off LED: %s\n",mygpio->name);
Norman James772232e2015-10-18 14:43:28 -050053 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 James3791d9c2015-10-28 18:58:59 -050058 rc = gpio_write(mygpio,1);
Norman James772232e2015-10-18 14:43:28 -050059 if (rc != GPIO_OK) { break; }
Norman James877dd412015-10-31 17:33:25 -050060 led_set_state(led,"off");
Norman James772232e2015-10-18 14:43:28 -050061 } while(0);
62 gpio_close(mygpio);
63 if (rc != GPIO_OK)
64 {
Norman James877dd412015-10-31 17:33:25 -050065 g_print("ERROR led controller: GPIO error %s (rc=%d)\n",mygpio->name,rc);
Norman James772232e2015-10-18 14:43:28 -050066 }
67 return TRUE;
68}
69
Norman James877dd412015-10-31 17:33:25 -050070void 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 James772232e2015-10-18 14:43:28 -050092static void
93on_bus_acquired (GDBusConnection *connection,
94 const gchar *name,
95 gpointer user_data)
96{
97 ObjectSkeleton *object;
98
99 cmdline *cmd = user_data;
Norman James772232e2015-10-18 14:43:28 -0500100
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 James877dd412015-10-31 17:33:25 -0500114 ObjectMapper* mapper = object_mapper_skeleton_new ();
115 object_skeleton_set_object_mapper (object, mapper);
116 g_object_unref (mapper);
117
Norman James772232e2015-10-18 14:43:28 -0500118 //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 James877dd412015-10-31 17:33:25 -0500132 init_led(led, &led_gpio[i]);
Norman James772232e2015-10-18 14:43:28 -0500133 /* 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 James877dd412015-10-31 17:33:25 -0500139 emit_object_added((GDBusObjectManager*)manager);
Norman James772232e2015-10-18 14:43:28 -0500140}
141
142static void
143on_name_acquired (GDBusConnection *connection,
144 const gchar *name,
145 gpointer user_data)
146{
147}
148
149static void
150on_name_lost (GDBusConnection *connection,
151 const gchar *name,
152 gpointer user_data)
153{
154}
155
156
157gint
158main (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}