blob: 0dc4475b134f5793a01ae0636e08f521f1a4a9f3 [file] [log] [blame]
Brad Bishopf6c85682016-06-27 11:56:39 -04001#include <openbmc_intf.h>
2#include <openbmc.h>
Brad Bishop77390492016-04-13 10:47:19 -04003#include <stdio.h>
4#include <stdbool.h>
5#include <string.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04006#include <gpio.h>
Brad Bishop77390492016-04-13 10:47:19 -04007
8#define NUM_SLOTS 8
9GPIO slots[NUM_SLOTS] = {
10 { "SLOT0_RISER_PRESENT" },
11 { "SLOT1_RISER_PRESENT" },
12 { "SLOT2_RISER_PRESENT" },
13 { "SLOT0_PRESENT" },
14 { "SLOT1_PRESENT" },
15 { "SLOT2_PRESENT" },
16 { "MEZZ0_PRESENT" },
17 { "MEZZ1_PRESENT" },
18};
19
20typedef struct {
21 const char* bus_name;
22 const char* path;
23 const char* intf_name;
24} object_info;
25
26
27
28/* ------------------------------------------------------------------------- */
Brad Bishopeb228b52016-07-26 15:03:04 -040029void
30get_service(GDBusConnection *connection, const char *obj,
31 const char **service, GError **error)
32{
33 GDBusProxy *proxy;
34 GVariant *result = NULL;
35 GVariantIter *iter;
36
37 *error = NULL;
38 proxy = g_dbus_proxy_new_sync(connection,
39 G_DBUS_PROXY_FLAGS_NONE,
40 NULL, /* GDBusInterfaceInfo* */
Brad Bishope87cc292017-02-25 15:39:33 -050041 "xyz.openbmc_project.ObjectMapper", /* name */
Leonel Gonzaleze1e2e6d2017-03-16 13:47:46 -050042 "/xyz/openbmc_project/object_mapper", /* object path */
Brad Bishope87cc292017-02-25 15:39:33 -050043 "xyz.openbmc_project.ObjectMapper", /* interface name */
Brad Bishopeb228b52016-07-26 15:03:04 -040044 NULL, /* GCancellable */
45 error);
46
47 result = g_dbus_proxy_call_sync(proxy,
48 "GetObject",
49 g_variant_new("(s)", obj),
50 G_DBUS_CALL_FLAGS_NONE,
51 -1,
52 NULL,
53 error);
54 if(*error)
55 goto exit;
56
57 g_variant_get(result, "(a{sas})", &iter);
58 g_variant_iter_next(iter, "{sas}", service, NULL);
59
60exit:
61 if(result)
62 g_variant_unref(result);
63}
64
Brad Bishop77390492016-04-13 10:47:19 -040065int
Brad Bishopeb228b52016-07-26 15:03:04 -040066get_object(GDBusConnection *connection, GDBusProxy *proxy,
67 GPIO* gpio, object_info* obj_info)
Brad Bishop77390492016-04-13 10:47:19 -040068{
69 g_print("Checking Presence: %s\n",gpio->name);
Brad Bishopeb228b52016-07-26 15:03:04 -040070 const char *gpio_bus = NULL;
Brad Bishop77390492016-04-13 10:47:19 -040071 GError *error;
72 GVariant *parm;
73 GVariant *result;
Brad Bishopeb228b52016-07-26 15:03:04 -040074 int rc=0;
Brad Bishop77390492016-04-13 10:47:19 -040075
76 error = NULL;
77 parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name);
78 result = g_dbus_proxy_call_sync(proxy,
79 "getObjectFromId",
80 parm,
81 G_DBUS_CALL_FLAGS_NONE,
82 -1,
83 NULL,
84 &error);
85 g_assert_no_error(error);
Brad Bishopeb228b52016-07-26 15:03:04 -040086 if(error)
87 goto exit;
Brad Bishop77390492016-04-13 10:47:19 -040088
89 GVariantIter *iter = g_variant_iter_new(result);
90 GVariant* v_result = g_variant_iter_next_value(iter);
91
Brad Bishopeb228b52016-07-26 15:03:04 -040092 g_variant_get(v_result,"(ss)",&obj_info->path,&obj_info->intf_name);
93
94 get_service(connection, obj_info->path, &gpio_bus, &error);
95 if(error)
96 goto exit;
97
98 obj_info->bus_name = gpio_bus;
99
100exit:
101 if(!gpio_bus || strlen(gpio_bus) == 0) {
Brad Bishop77390492016-04-13 10:47:19 -0400102 rc = 1;
103 }
104 g_variant_unref(v_result);
105 g_variant_unref(result);
106
107 return rc;
108}
109
110int
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500111get_presence(GPIO* gpio, uint8_t* present)
Brad Bishop77390492016-04-13 10:47:19 -0400112{
113 int rc = GPIO_OK;
114 do {
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500115 rc = gpio_init(gpio);
Brad Bishop77390492016-04-13 10:47:19 -0400116 if(rc != GPIO_OK) { break; }
117 uint8_t gpio_val;
118 rc = gpio_open(gpio);
119 if(rc != GPIO_OK) { break; }
120 rc = gpio_read(gpio,&gpio_val);
121 if(rc != GPIO_OK) { gpio_close(gpio); break; }
122 gpio_close(gpio);
123 *present = gpio_val;
124 } while(0);
125 if(rc != GPIO_OK)
126 {
127 printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
128 }
Matt Spinler0f0946b2018-08-07 16:26:55 -0500129 gpio_inits_done();
Brad Bishop77390492016-04-13 10:47:19 -0400130 return rc;
131}
132
133void
134update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
135{
136 GDBusProxy *proxy;
137 GError *error;
138 GVariant *parm;
Brad Bishop77390492016-04-13 10:47:19 -0400139
140 error = NULL;
141 proxy = g_dbus_proxy_new_sync(connection,
142 G_DBUS_PROXY_FLAGS_NONE,
143 NULL, /* GDBusInterfaceInfo* */
144 obj_info->bus_name, /* name */
145 obj_info->path, /* object path */
146 obj_info->intf_name, /* interface name */
147 NULL, /* GCancellable */
148 &error);
149 g_assert_no_error(error);
150
151 error = NULL;
152 parm = g_variant_new("(s)",present);
153
Brad Bishop0c82c602016-04-13 13:36:49 -0400154 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -0400155 "setPresent",
156 parm,
157 G_DBUS_CALL_FLAGS_NONE,
158 -1,
159 NULL,
160 &error);
161
162 g_assert_no_error(error);
163}
164
165gint
166main(gint argc, gchar *argv[])
167{
Brad Bishopeb228b52016-07-26 15:03:04 -0400168 const char *sysmgr_path = "/org/openbmc/managers/System";
169 const char *sysmgr_bus = NULL;
Brad Bishop77390492016-04-13 10:47:19 -0400170 GMainLoop *loop;
171 GDBusConnection *c;
172 GDBusProxy *sys_proxy;
173 GError *error;
Brad Bishop77390492016-04-13 10:47:19 -0400174
175 loop = g_main_loop_new(NULL, FALSE);
176
177 error = NULL;
178 c = g_bus_get_sync(DBUS_TYPE, NULL, &error);
Brad Bishopeb228b52016-07-26 15:03:04 -0400179 if(error)
180 goto exit;
Brad Bishop77390492016-04-13 10:47:19 -0400181
Brad Bishopeb228b52016-07-26 15:03:04 -0400182 get_service(c, sysmgr_path, &sysmgr_bus, &error);
183 if(error)
184 goto exit;
185
Brad Bishop77390492016-04-13 10:47:19 -0400186 sys_proxy = g_dbus_proxy_new_sync(c,
187 G_DBUS_PROXY_FLAGS_NONE,
188 NULL, /* GDBusInterfaceInfo* */
Brad Bishopeb228b52016-07-26 15:03:04 -0400189 sysmgr_bus, /* name */
190 sysmgr_path, /* object path */
Brad Bishop77390492016-04-13 10:47:19 -0400191 "org.openbmc.managers.System", /* interface name */
192 NULL, /* GCancellable */
193 &error);
194 g_assert_no_error(error);
Brad Bishopeb228b52016-07-26 15:03:04 -0400195 if(error)
196 goto exit;
Brad Bishop77390492016-04-13 10:47:19 -0400197
198 int i = 0;
199 int rc = 0;
200 for(i=0;i<NUM_SLOTS;i++)
201 {
202 object_info obj_info;
203 uint8_t present;
Brad Bishop77390492016-04-13 10:47:19 -0400204 do {
Brad Bishopeb228b52016-07-26 15:03:04 -0400205 rc = get_object(c,sys_proxy,&slots[i],&obj_info);
Brad Bishop77390492016-04-13 10:47:19 -0400206 if(rc) { break; }
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500207 rc = get_presence(&slots[i],&present);
Brad Bishop77390492016-04-13 10:47:19 -0400208 //if (rc) { break; }
209 // TODO: send correct state
210 if(present == 0) {
211 update_fru_obj(c,&obj_info,"True");
212 } else {
213 update_fru_obj(c,&obj_info,"False");
214 }
215 } while(0);
216 }
217
Brad Bishopeb228b52016-07-26 15:03:04 -0400218exit:
219 if(sysmgr_bus)
220 g_free((char *)sysmgr_bus);
Brad Bishop77390492016-04-13 10:47:19 -0400221 g_object_unref(c);
222 g_main_loop_unref(loop);
223 return 0;
224}