summaryrefslogtreecommitdiffstats
path: root/pciedetect/pcie_slot_present_obj.c
blob: 4828051e67189ddd4b02a54921152477650ed566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <openbmc_intf.h>
#include <openbmc.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <gpio.h>

#define NUM_SLOTS 8
GPIO slots[NUM_SLOTS] = {
	{ "SLOT0_RISER_PRESENT" },
	{ "SLOT1_RISER_PRESENT" },
	{ "SLOT2_RISER_PRESENT" },
	{ "SLOT0_PRESENT" },
	{ "SLOT1_PRESENT" },
	{ "SLOT2_PRESENT" },
	{ "MEZZ0_PRESENT" },
	{ "MEZZ1_PRESENT" },
};

typedef struct {
	const char* bus_name;
	const char* path;
	const char* intf_name;
} object_info;



/* ------------------------------------------------------------------------- */
void
get_service(GDBusConnection *connection, const char *obj,
		const char **service, GError **error)
{
	GDBusProxy *proxy;
	GVariant *result = NULL;
	GVariantIter *iter;

	*error = NULL;
	proxy = g_dbus_proxy_new_sync(connection,
			G_DBUS_PROXY_FLAGS_NONE,
			NULL, /* GDBusInterfaceInfo* */
			"xyz.openbmc_project.ObjectMapper", /* name */
			"/xyz/openbmc_project/object_mapper", /* object path */
			"xyz.openbmc_project.ObjectMapper", /* interface name */
			NULL, /* GCancellable */
			error);

	result = g_dbus_proxy_call_sync(proxy,
			"GetObject",
			g_variant_new("(s)", obj),
			G_DBUS_CALL_FLAGS_NONE,
			-1,
			NULL,
			error);
	if(*error)
		goto exit;

	g_variant_get(result, "(a{sas})", &iter);
	g_variant_iter_next(iter, "{sas}", service, NULL);

exit:
	if(result)
		g_variant_unref(result);
}

int
get_object(GDBusConnection *connection, GDBusProxy *proxy,
		GPIO* gpio, object_info* obj_info)
{
	g_print("Checking Presence: %s\n",gpio->name);
	const char *gpio_bus = NULL;
	GError *error;
	GVariant *parm;
	GVariant *result;
	int rc=0;

	error = NULL;
	parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name);
	result = g_dbus_proxy_call_sync(proxy,
			"getObjectFromId",
			parm,
			G_DBUS_CALL_FLAGS_NONE,
			-1,
			NULL,
			&error);
	g_assert_no_error(error);
	if(error)
		goto exit;

	GVariantIter *iter = g_variant_iter_new(result);
	GVariant* v_result = g_variant_iter_next_value(iter);

	g_variant_get(v_result,"(ss)",&obj_info->path,&obj_info->intf_name);

	get_service(connection, obj_info->path, &gpio_bus, &error);
	if(error)
		goto exit;

	obj_info->bus_name = gpio_bus;

exit:
	if(!gpio_bus || strlen(gpio_bus) == 0) {
		rc = 1;
	}
	g_variant_unref(v_result);
	g_variant_unref(result);

	return rc;
}

int
get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
{
	int rc = GPIO_OK;
	do {
		rc = gpio_init(connection,gpio);
		if(rc != GPIO_OK) { break; }
		uint8_t gpio_val;
		rc = gpio_open(gpio);
		if(rc != GPIO_OK) { break; }
		rc = gpio_read(gpio,&gpio_val);
		if(rc != GPIO_OK) { gpio_close(gpio); break; }
		gpio_close(gpio);
		*present = gpio_val;
	} while(0);
	if(rc != GPIO_OK)
	{
		printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
	}
	gpio_inits_done();
	return rc;
}

void
update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
{
	GDBusProxy *proxy;
	GError *error;
	GVariant *parm;

	error = NULL;
	proxy = g_dbus_proxy_new_sync(connection,
			G_DBUS_PROXY_FLAGS_NONE,
			NULL, /* GDBusInterfaceInfo* */
			obj_info->bus_name, /* name */
			obj_info->path, /* object path */
			obj_info->intf_name, /* interface name */
			NULL, /* GCancellable */
			&error);
	g_assert_no_error(error);

	error = NULL;
	parm = g_variant_new("(s)",present);

	g_dbus_proxy_call_sync(proxy,
			"setPresent",
			parm,
			G_DBUS_CALL_FLAGS_NONE,
			-1,
			NULL,
			&error);

	g_assert_no_error(error);
}

gint
main(gint argc, gchar *argv[])
{
	const char *sysmgr_path = "/org/openbmc/managers/System";
	const char *sysmgr_bus = NULL;
	GMainLoop *loop;
	GDBusConnection *c;
	GDBusProxy *sys_proxy;
	GError *error;

	loop = g_main_loop_new(NULL, FALSE);

	error = NULL;
	c = g_bus_get_sync(DBUS_TYPE, NULL, &error);
	if(error)
		goto exit;

	get_service(c, sysmgr_path, &sysmgr_bus, &error);
	if(error)
		goto exit;

	sys_proxy = g_dbus_proxy_new_sync(c,
			G_DBUS_PROXY_FLAGS_NONE,
			NULL, /* GDBusInterfaceInfo* */
			sysmgr_bus, /* name */
			sysmgr_path, /* object path */
			"org.openbmc.managers.System", /* interface name */
			NULL, /* GCancellable */
			&error);
	g_assert_no_error(error);
	if(error)
		goto exit;

	int i = 0;
	int rc = 0;
	for(i=0;i<NUM_SLOTS;i++)
	{
		object_info obj_info;
		uint8_t present;
		do {
			rc = get_object(c,sys_proxy,&slots[i],&obj_info);
			if(rc) { break; }
			rc = get_presence(c,&slots[i],&present);
			//if (rc) { break; }
			// TODO: send correct state
			if(present == 0) {
				update_fru_obj(c,&obj_info,"True");
			} else {
				update_fru_obj(c,&obj_info,"False");
			}
		} while(0);
	}

exit:
	if(sysmgr_bus)
		g_free((char *)sysmgr_bus);
	g_object_unref(c);
	g_main_loop_unref(loop);
	return 0;
}
OpenPOWER on IntegriCloud