blob: cceb6376d4046d3649eb55616f9dcf4ae2a2239e [file] [log] [blame]
Norman James19e45912015-10-04 20:19:41 -05001#include "interfaces/openbmc_intf.h"
2#include "openbmc.h"
Norman James3d3b7fb2015-10-06 16:54:06 -05003#include <stdio.h>
Norman James1d309922015-10-15 10:23:58 -05004#include <stdbool.h>
Norman James19e45912015-10-04 20:19:41 -05005#include "gpio.h"
Norman James1d309922015-10-15 10:23:58 -05006
7#define NUM_SLOTS 4
Norman James19e45912015-10-04 20:19:41 -05008GPIO slots[NUM_SLOTS] = {
Norman James1d309922015-10-15 10:23:58 -05009// { "SLOT0_RISER_PRESENT" },
10// { "SLOT1_RISER_PRESENT" },
11// { "SLOT2_RISER_PRESENT" },
Norman James19e45912015-10-04 20:19:41 -050012 { "SLOT0_PRESENT" },
13 { "SLOT1_PRESENT" },
14 { "SLOT2_PRESENT" },
15 { "MEZZ0_PRESENT" },
Norman James1d309922015-10-15 10:23:58 -050016// { "MEZZ1_PRESENT" },
Norman James19e45912015-10-04 20:19:41 -050017};
18
19typedef struct {
20 const char* bus_name;
21 const char* path;
Norman James1d309922015-10-15 10:23:58 -050022 const char* intf_name;
Norman James19e45912015-10-04 20:19:41 -050023} object_info;
24
25
26
27/* ---------------------------------------------------------------------------------------------------- */
28int get_object(GDBusProxy *proxy, GPIO* gpio, object_info* obj_info)
29{
Norman James3d3b7fb2015-10-06 16:54:06 -050030 g_print("Checking Presence: %s\n",gpio->name);
Norman James19e45912015-10-04 20:19:41 -050031 GError *error;
32 GVariant *parm;
33 GVariant *result;
34
35 error = NULL;
36 parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name);
37 result = g_dbus_proxy_call_sync (proxy,
38 "getObjectFromId",
39 parm,
40 G_DBUS_CALL_FLAGS_NONE,
41 -1,
42 NULL,
43 &error);
44 g_assert_no_error (error);
Norman James1d309922015-10-15 10:23:58 -050045
Norman James19e45912015-10-04 20:19:41 -050046 GVariantIter *iter = g_variant_iter_new(result);
Norman James1d309922015-10-15 10:23:58 -050047 GVariant* v_result = g_variant_iter_next_value(iter);
Norman James19e45912015-10-04 20:19:41 -050048
Norman James1d309922015-10-15 10:23:58 -050049 g_variant_get(v_result,"(sss)",&obj_info->bus_name,&obj_info->path,&obj_info->intf_name);
50int rc=0;
51 if (strcmp(obj_info->bus_name,"") == 0) {
Norman James19e45912015-10-04 20:19:41 -050052 rc = 1;
Norman James19e45912015-10-04 20:19:41 -050053 }
Norman James1d309922015-10-15 10:23:58 -050054 g_variant_unref(v_result);
Norman James19e45912015-10-04 20:19:41 -050055 g_variant_unref(result);
56
57 return rc;
58}
59
60int get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
61{
62 int rc = GPIO_OK;
63 do {
64 rc = gpio_init(connection,gpio);
65 if (rc != GPIO_OK) { break; }
66 uint8_t gpio_val;
67 rc = gpio_open(gpio);
68 if (rc != GPIO_OK) { break; }
69 rc = gpio_read(gpio,&gpio_val);
70 if (rc != GPIO_OK) { gpio_close(gpio); break; }
71 gpio_close(gpio);
72 *present = gpio_val;
73 } while(0);
74 if (rc != GPIO_OK)
75 {
Norman James3d3b7fb2015-10-06 16:54:06 -050076 printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
Norman James19e45912015-10-04 20:19:41 -050077 }
78 return rc;
79}
80
Norman James1d309922015-10-15 10:23:58 -050081void update_fru_obj(GDBusConnection* connection, object_info* obj_info, bool present)
Norman James19e45912015-10-04 20:19:41 -050082{
83 GDBusProxy *proxy;
84 GError *error;
85 GVariant *parm;
86 GVariant *result;
87
88 error = NULL;
89 proxy = g_dbus_proxy_new_sync (connection,
90 G_DBUS_PROXY_FLAGS_NONE,
91 NULL, /* GDBusInterfaceInfo* */
92 obj_info->bus_name, /* name */
93 obj_info->path, /* object path */
Norman James1d309922015-10-15 10:23:58 -050094 obj_info->intf_name, /* interface name */
Norman James19e45912015-10-04 20:19:41 -050095 NULL, /* GCancellable */
96 &error);
97 g_assert_no_error (error);
98
99 error = NULL;
Norman James1d309922015-10-15 10:23:58 -0500100 parm = g_variant_new("(b)",present);
Norman James19e45912015-10-04 20:19:41 -0500101
102 result = g_dbus_proxy_call_sync (proxy,
Norman James1d309922015-10-15 10:23:58 -0500103 "setPresent",
Norman James19e45912015-10-04 20:19:41 -0500104 parm,
105 G_DBUS_CALL_FLAGS_NONE,
106 -1,
107 NULL,
108 &error);
109
110 g_assert_no_error (error);
111}
112
113gint
114main (gint argc, gchar *argv[])
115{
116 GMainLoop *loop;
117 GDBusConnection *c;
118 GDBusProxy *sys_proxy;
119 GError *error;
120 GVariant *parm;
121 GVariant *result;
122
123 loop = g_main_loop_new (NULL, FALSE);
124
125 error = NULL;
Norman James5e792e32015-10-07 17:36:17 -0500126 c = g_bus_get_sync (DBUS_TYPE, NULL, &error);
Norman James19e45912015-10-04 20:19:41 -0500127
128 error = NULL;
129 sys_proxy = g_dbus_proxy_new_sync (c,
130 G_DBUS_PROXY_FLAGS_NONE,
131 NULL, /* GDBusInterfaceInfo* */
132 "org.openbmc.managers.System", /* name */
133 "/org/openbmc/managers/System", /* object path */
134 "org.openbmc.managers.System", /* interface name */
135 NULL, /* GCancellable */
136 &error);
137 g_assert_no_error (error);
138
139 int i = 0;
140 int rc = 0;
141 for (i=0;i<NUM_SLOTS;i++)
142 {
143 object_info obj_info;
144 uint8_t present;
Norman James1d309922015-10-15 10:23:58 -0500145 bool b_present=false;
Norman James19e45912015-10-04 20:19:41 -0500146 do {
147 rc = get_object(sys_proxy,&slots[i],&obj_info);
148 if (rc) { break; }
149 rc = get_presence(c,&slots[i],&present);
Norman James1d309922015-10-15 10:23:58 -0500150 if (present==1) { b_present=true; }
151 //if (rc) { break; }
Norman James19e45912015-10-04 20:19:41 -0500152 // TODO: send correct state
Norman James1d309922015-10-15 10:23:58 -0500153 update_fru_obj(c,&obj_info,b_present);
Norman James19e45912015-10-04 20:19:41 -0500154 } while(0);
155 }
156
157 g_object_unref(c);
158 g_main_loop_unref (loop);
159 return 0;
160}