blob: 36104e2cb4f4a54e4d6662aa2ba2536dd0d5b87d [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;
Brad Bishop77390492016-04-13 10:47:19 -040090
91 error = NULL;
92 proxy = g_dbus_proxy_new_sync(connection,
93 G_DBUS_PROXY_FLAGS_NONE,
94 NULL, /* GDBusInterfaceInfo* */
95 obj_info->bus_name, /* name */
96 obj_info->path, /* object path */
97 obj_info->intf_name, /* interface name */
98 NULL, /* GCancellable */
99 &error);
100 g_assert_no_error(error);
101
102 error = NULL;
103 parm = g_variant_new("(s)",present);
104
Brad Bishop0c82c602016-04-13 13:36:49 -0400105 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -0400106 "setPresent",
107 parm,
108 G_DBUS_CALL_FLAGS_NONE,
109 -1,
110 NULL,
111 &error);
112
113 g_assert_no_error(error);
114}
115
116gint
117main(gint argc, gchar *argv[])
118{
119 GMainLoop *loop;
120 GDBusConnection *c;
121 GDBusProxy *sys_proxy;
122 GError *error;
Brad Bishop77390492016-04-13 10:47:19 -0400123
124 loop = g_main_loop_new(NULL, FALSE);
125
126 error = NULL;
127 c = g_bus_get_sync(DBUS_TYPE, NULL, &error);
128
129 error = NULL;
130 sys_proxy = g_dbus_proxy_new_sync(c,
131 G_DBUS_PROXY_FLAGS_NONE,
132 NULL, /* GDBusInterfaceInfo* */
133 "org.openbmc.managers.System", /* name */
134 "/org/openbmc/managers/System", /* object path */
135 "org.openbmc.managers.System", /* interface name */
136 NULL, /* GCancellable */
137 &error);
138 g_assert_no_error(error);
139
140 int i = 0;
141 int rc = 0;
142 for(i=0;i<NUM_SLOTS;i++)
143 {
144 object_info obj_info;
145 uint8_t present;
Brad Bishop77390492016-04-13 10:47:19 -0400146 do {
147 rc = get_object(sys_proxy,&slots[i],&obj_info);
148 if(rc) { break; }
149 rc = get_presence(c,&slots[i],&present);
150 //if (rc) { break; }
151 // TODO: send correct state
152 if(present == 0) {
153 update_fru_obj(c,&obj_info,"True");
154 } else {
155 update_fru_obj(c,&obj_info,"False");
156 }
157 } while(0);
158 }
159
160 g_object_unref(c);
161 g_main_loop_unref(loop);
162 return 0;
163}