blob: 01aeff4dec231124aa25c6c8130a233598acf87a [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
111get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
112{
113 int rc = GPIO_OK;
114 do {
115 rc = gpio_init(connection,gpio);
116 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 }
129 return rc;
130}
131
132void
133update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
134{
135 GDBusProxy *proxy;
136 GError *error;
137 GVariant *parm;
Brad Bishop77390492016-04-13 10:47:19 -0400138
139 error = NULL;
140 proxy = g_dbus_proxy_new_sync(connection,
141 G_DBUS_PROXY_FLAGS_NONE,
142 NULL, /* GDBusInterfaceInfo* */
143 obj_info->bus_name, /* name */
144 obj_info->path, /* object path */
145 obj_info->intf_name, /* interface name */
146 NULL, /* GCancellable */
147 &error);
148 g_assert_no_error(error);
149
150 error = NULL;
151 parm = g_variant_new("(s)",present);
152
Brad Bishop0c82c602016-04-13 13:36:49 -0400153 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -0400154 "setPresent",
155 parm,
156 G_DBUS_CALL_FLAGS_NONE,
157 -1,
158 NULL,
159 &error);
160
161 g_assert_no_error(error);
162}
163
164gint
165main(gint argc, gchar *argv[])
166{
Brad Bishopeb228b52016-07-26 15:03:04 -0400167 const char *sysmgr_path = "/org/openbmc/managers/System";
168 const char *sysmgr_bus = NULL;
Brad Bishop77390492016-04-13 10:47:19 -0400169 GMainLoop *loop;
170 GDBusConnection *c;
171 GDBusProxy *sys_proxy;
172 GError *error;
Brad Bishop77390492016-04-13 10:47:19 -0400173
174 loop = g_main_loop_new(NULL, FALSE);
175
176 error = NULL;
177 c = g_bus_get_sync(DBUS_TYPE, NULL, &error);
Brad Bishopeb228b52016-07-26 15:03:04 -0400178 if(error)
179 goto exit;
Brad Bishop77390492016-04-13 10:47:19 -0400180
Brad Bishopeb228b52016-07-26 15:03:04 -0400181 get_service(c, sysmgr_path, &sysmgr_bus, &error);
182 if(error)
183 goto exit;
184
Brad Bishop77390492016-04-13 10:47:19 -0400185 sys_proxy = g_dbus_proxy_new_sync(c,
186 G_DBUS_PROXY_FLAGS_NONE,
187 NULL, /* GDBusInterfaceInfo* */
Brad Bishopeb228b52016-07-26 15:03:04 -0400188 sysmgr_bus, /* name */
189 sysmgr_path, /* object path */
Brad Bishop77390492016-04-13 10:47:19 -0400190 "org.openbmc.managers.System", /* interface name */
191 NULL, /* GCancellable */
192 &error);
193 g_assert_no_error(error);
Brad Bishopeb228b52016-07-26 15:03:04 -0400194 if(error)
195 goto exit;
Brad Bishop77390492016-04-13 10:47:19 -0400196
197 int i = 0;
198 int rc = 0;
199 for(i=0;i<NUM_SLOTS;i++)
200 {
201 object_info obj_info;
202 uint8_t present;
Brad Bishop77390492016-04-13 10:47:19 -0400203 do {
Brad Bishopeb228b52016-07-26 15:03:04 -0400204 rc = get_object(c,sys_proxy,&slots[i],&obj_info);
Brad Bishop77390492016-04-13 10:47:19 -0400205 if(rc) { break; }
206 rc = get_presence(c,&slots[i],&present);
207 //if (rc) { break; }
208 // TODO: send correct state
209 if(present == 0) {
210 update_fru_obj(c,&obj_info,"True");
211 } else {
212 update_fru_obj(c,&obj_info,"False");
213 }
214 } while(0);
215 }
216
Brad Bishopeb228b52016-07-26 15:03:04 -0400217exit:
218 if(sysmgr_bus)
219 g_free((char *)sysmgr_bus);
Brad Bishop77390492016-04-13 10:47:19 -0400220 g_object_unref(c);
221 g_main_loop_unref(loop);
222 return 0;
223}