blob: a71ba2d2e13dc0184c58f91c7060ec7d2c219f08 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include "interfaces/openbmc_intf.h"
2#include "openbmc.h"
3#include <stdio.h>
4#include <stdbool.h>
5#include <string.h>
6#include "gpio.h"
7
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/* ------------------------------------------------------------------------- */
29int
30get_object(GDBusProxy *proxy, GPIO* gpio, object_info* obj_info)
31{
32 g_print("Checking Presence: %s\n",gpio->name);
33 GError *error;
34 GVariant *parm;
35 GVariant *result;
36
37 error = NULL;
38 parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name);
39 result = g_dbus_proxy_call_sync(proxy,
40 "getObjectFromId",
41 parm,
42 G_DBUS_CALL_FLAGS_NONE,
43 -1,
44 NULL,
45 &error);
46 g_assert_no_error(error);
47
48 GVariantIter *iter = g_variant_iter_new(result);
49 GVariant* v_result = g_variant_iter_next_value(iter);
50
51 g_variant_get(v_result,"(sss)",&obj_info->bus_name,&obj_info->path,&obj_info->intf_name);
52 int rc=0;
53 if(strlen(obj_info->bus_name) == 0) {
54 rc = 1;
55 }
56 g_variant_unref(v_result);
57 g_variant_unref(result);
58
59 return rc;
60}
61
62int
63get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
64{
65 int rc = GPIO_OK;
66 do {
67 rc = gpio_init(connection,gpio);
68 if(rc != GPIO_OK) { break; }
69 uint8_t gpio_val;
70 rc = gpio_open(gpio);
71 if(rc != GPIO_OK) { break; }
72 rc = gpio_read(gpio,&gpio_val);
73 if(rc != GPIO_OK) { gpio_close(gpio); break; }
74 gpio_close(gpio);
75 *present = gpio_val;
76 } while(0);
77 if(rc != GPIO_OK)
78 {
79 printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
80 }
81 return rc;
82}
83
84void
85update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
86{
87 GDBusProxy *proxy;
88 GError *error;
89 GVariant *parm;
90 GVariant *result;
91
92 error = NULL;
93 proxy = g_dbus_proxy_new_sync(connection,
94 G_DBUS_PROXY_FLAGS_NONE,
95 NULL, /* GDBusInterfaceInfo* */
96 obj_info->bus_name, /* name */
97 obj_info->path, /* object path */
98 obj_info->intf_name, /* interface name */
99 NULL, /* GCancellable */
100 &error);
101 g_assert_no_error(error);
102
103 error = NULL;
104 parm = g_variant_new("(s)",present);
105
106 result = g_dbus_proxy_call_sync(proxy,
107 "setPresent",
108 parm,
109 G_DBUS_CALL_FLAGS_NONE,
110 -1,
111 NULL,
112 &error);
113
114 g_assert_no_error(error);
115}
116
117gint
118main(gint argc, gchar *argv[])
119{
120 GMainLoop *loop;
121 GDBusConnection *c;
122 GDBusProxy *sys_proxy;
123 GError *error;
124 GVariant *parm;
125 GVariant *result;
126
127 loop = g_main_loop_new(NULL, FALSE);
128
129 error = NULL;
130 c = g_bus_get_sync(DBUS_TYPE, NULL, &error);
131
132 error = NULL;
133 sys_proxy = g_dbus_proxy_new_sync(c,
134 G_DBUS_PROXY_FLAGS_NONE,
135 NULL, /* GDBusInterfaceInfo* */
136 "org.openbmc.managers.System", /* name */
137 "/org/openbmc/managers/System", /* object path */
138 "org.openbmc.managers.System", /* interface name */
139 NULL, /* GCancellable */
140 &error);
141 g_assert_no_error(error);
142
143 int i = 0;
144 int rc = 0;
145 for(i=0;i<NUM_SLOTS;i++)
146 {
147 object_info obj_info;
148 uint8_t present;
149 char* chr_present;
150 do {
151 rc = get_object(sys_proxy,&slots[i],&obj_info);
152 if(rc) { break; }
153 rc = get_presence(c,&slots[i],&present);
154 //if (rc) { break; }
155 // TODO: send correct state
156 if(present == 0) {
157 update_fru_obj(c,&obj_info,"True");
158 } else {
159 update_fru_obj(c,&obj_info,"False");
160 }
161 } while(0);
162 }
163
164 g_object_unref(c);
165 g_main_loop_unref(loop);
166 return 0;
167}