blob: 0cbc2d711a5f1b6b9575dd3009723b84c45f9c51 [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 Jamesa3e47c42015-10-18 14:43:10 -05005#include <string.h>
Norman James19e45912015-10-04 20:19:41 -05006#include "gpio.h"
Norman James1d309922015-10-15 10:23:58 -05007
Norman James627961a2015-11-09 10:58:00 -06008#define NUM_SLOTS 8
Norman James19e45912015-10-04 20:19:41 -05009GPIO slots[NUM_SLOTS] = {
Norman James627961a2015-11-09 10:58:00 -060010 { "SLOT0_RISER_PRESENT" },
11 { "SLOT1_RISER_PRESENT" },
12 { "SLOT2_RISER_PRESENT" },
Norman James19e45912015-10-04 20:19:41 -050013 { "SLOT0_PRESENT" },
14 { "SLOT1_PRESENT" },
15 { "SLOT2_PRESENT" },
16 { "MEZZ0_PRESENT" },
Norman James627961a2015-11-09 10:58:00 -060017 { "MEZZ1_PRESENT" },
Norman James19e45912015-10-04 20:19:41 -050018};
19
20typedef struct {
21 const char* bus_name;
22 const char* path;
Norman James1d309922015-10-15 10:23:58 -050023 const char* intf_name;
Norman James19e45912015-10-04 20:19:41 -050024} object_info;
25
26
27
28/* ---------------------------------------------------------------------------------------------------- */
29int get_object(GDBusProxy *proxy, GPIO* gpio, object_info* obj_info)
30{
Norman James3d3b7fb2015-10-06 16:54:06 -050031 g_print("Checking Presence: %s\n",gpio->name);
Norman James19e45912015-10-04 20:19:41 -050032 GError *error;
33 GVariant *parm;
34 GVariant *result;
35
36 error = NULL;
37 parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name);
38 result = g_dbus_proxy_call_sync (proxy,
39 "getObjectFromId",
40 parm,
41 G_DBUS_CALL_FLAGS_NONE,
42 -1,
43 NULL,
44 &error);
45 g_assert_no_error (error);
Norman James1d309922015-10-15 10:23:58 -050046
Norman James19e45912015-10-04 20:19:41 -050047 GVariantIter *iter = g_variant_iter_new(result);
Norman James1d309922015-10-15 10:23:58 -050048 GVariant* v_result = g_variant_iter_next_value(iter);
Norman James19e45912015-10-04 20:19:41 -050049
Norman James1d309922015-10-15 10:23:58 -050050 g_variant_get(v_result,"(sss)",&obj_info->bus_name,&obj_info->path,&obj_info->intf_name);
51int rc=0;
Norman Jamesa3e47c42015-10-18 14:43:10 -050052 if (strlen(obj_info->bus_name) == 0) {
Norman James19e45912015-10-04 20:19:41 -050053 rc = 1;
Norman James19e45912015-10-04 20:19:41 -050054 }
Norman James1d309922015-10-15 10:23:58 -050055 g_variant_unref(v_result);
Norman James19e45912015-10-04 20:19:41 -050056 g_variant_unref(result);
57
58 return rc;
59}
60
61int get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
62{
63 int rc = GPIO_OK;
64 do {
65 rc = gpio_init(connection,gpio);
66 if (rc != GPIO_OK) { break; }
67 uint8_t gpio_val;
68 rc = gpio_open(gpio);
69 if (rc != GPIO_OK) { break; }
70 rc = gpio_read(gpio,&gpio_val);
71 if (rc != GPIO_OK) { gpio_close(gpio); break; }
72 gpio_close(gpio);
73 *present = gpio_val;
74 } while(0);
75 if (rc != GPIO_OK)
76 {
Norman James3d3b7fb2015-10-06 16:54:06 -050077 printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
Norman James19e45912015-10-04 20:19:41 -050078 }
79 return rc;
80}
81
Norman Jamesa3e47c42015-10-18 14:43:10 -050082void update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
Norman James19e45912015-10-04 20:19:41 -050083{
84 GDBusProxy *proxy;
85 GError *error;
86 GVariant *parm;
87 GVariant *result;
88
89 error = NULL;
90 proxy = g_dbus_proxy_new_sync (connection,
91 G_DBUS_PROXY_FLAGS_NONE,
92 NULL, /* GDBusInterfaceInfo* */
93 obj_info->bus_name, /* name */
94 obj_info->path, /* object path */
Norman James1d309922015-10-15 10:23:58 -050095 obj_info->intf_name, /* interface name */
Norman James19e45912015-10-04 20:19:41 -050096 NULL, /* GCancellable */
97 &error);
98 g_assert_no_error (error);
99
100 error = NULL;
Norman Jamesa3e47c42015-10-18 14:43:10 -0500101 parm = g_variant_new("(s)",present);
Norman James19e45912015-10-04 20:19:41 -0500102
103 result = g_dbus_proxy_call_sync (proxy,
Norman James1d309922015-10-15 10:23:58 -0500104 "setPresent",
Norman James19e45912015-10-04 20:19:41 -0500105 parm,
106 G_DBUS_CALL_FLAGS_NONE,
107 -1,
108 NULL,
109 &error);
110
111 g_assert_no_error (error);
112}
113
114gint
115main (gint argc, gchar *argv[])
116{
117 GMainLoop *loop;
118 GDBusConnection *c;
119 GDBusProxy *sys_proxy;
120 GError *error;
121 GVariant *parm;
122 GVariant *result;
123
124 loop = g_main_loop_new (NULL, FALSE);
125
126 error = NULL;
Norman James5e792e32015-10-07 17:36:17 -0500127 c = g_bus_get_sync (DBUS_TYPE, NULL, &error);
Norman James19e45912015-10-04 20:19:41 -0500128
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;
Norman Jamesa3e47c42015-10-18 14:43:10 -0500146 char* chr_present;
Norman James19e45912015-10-04 20:19:41 -0500147 do {
148 rc = get_object(sys_proxy,&slots[i],&obj_info);
149 if (rc) { break; }
150 rc = get_presence(c,&slots[i],&present);
Norman James1d309922015-10-15 10:23:58 -0500151 //if (rc) { break; }
Norman James19e45912015-10-04 20:19:41 -0500152 // TODO: send correct state
Norman James3814ec82015-11-17 19:35:19 -0600153 if (present == 0) {
Norman James14caa522015-12-03 17:57:56 -0600154 update_fru_obj(c,&obj_info,"True");
Norman Jamesa3e47c42015-10-18 14:43:10 -0500155 } else {
Norman James14caa522015-12-03 17:57:56 -0600156 update_fru_obj(c,&obj_info,"False");
Norman Jamesa3e47c42015-10-18 14:43:10 -0500157 }
Norman James19e45912015-10-04 20:19:41 -0500158 } while(0);
159 }
160
161 g_object_unref(c);
162 g_main_loop_unref (loop);
163 return 0;
164}