Use cJSON to read the GPIO configuration

Change the read_gpios function to get the power GPIO
configuration out of JSON instead of D-Bus.

Note that the Host Control GPIOs are no longer used and will
be removed from the GpioConfigs structure in a future commit.

Change-Id: If95f9b4f11af052148332aec436abd23e2e5819d
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/libopenbmc_intf/gpio_configs.c b/libopenbmc_intf/gpio_configs.c
index a1cce59..4de9d5c 100644
--- a/libopenbmc_intf/gpio_configs.c
+++ b/libopenbmc_intf/gpio_configs.c
@@ -16,158 +16,187 @@
  */
 
 #include "gpio_configs.h"
+#include "gpio_json.h"
+#include <cjson/cJSON.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
 
-gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios)
+
+/**
+ * Loads the GPIO information into the gpios->power_gpio structure
+ * from the JSON.
+ *
+ * @param gpios - the structure where GpioConfigs.power_gpio will
+ *                be filled in.
+ * @param gpio_configs - cJSON pointer to the GPIO JSON
+ */
+void read_power_gpios(GpioConfigs* gpios, const cJSON* gpio_configs)
 {
-	GDBusProxy *proxy;
-	GError *error = NULL;
-	GVariant *value;
+	size_t i = 0;
 
-	GVariantIter *power_up_outs_iter;
-	GVariantIter *reset_outs_iter;
-	GVariantIter *pci_reset_outs_iter;
-	gchar *power_up_out_name;
-	gchar *reset_out_name;
-	gchar *pci_reset_out_name;
-	gboolean power_up_polarity;
-	gboolean reset_out_polarity;
-	gboolean pci_reset_out_polarity;
-	gboolean pci_reset_out_hold;
+	const cJSON* power_config = cJSON_GetObjectItem(
+			gpio_configs, "power_config");
+	g_assert(power_config != NULL);
 
-	GVariantIter *optionals_iter;
-	gchar *optional_name;
-	gboolean optional_polarity;
-	int i;
+	/* PGOOD - required */
 
-	proxy = g_dbus_proxy_new_sync(connection,
-			G_DBUS_PROXY_FLAGS_NONE,
-			NULL,							/* GDBusInterfaceInfo */
-			"org.openbmc.managers.System",	/* name */
-			"/org/openbmc/managers/System",	/* object path */
-			"org.openbmc.managers.System",	/* interface */
-			NULL,							/* GCancellable */
-			&error);
-	if(error != NULL) {
-		fprintf(stderr, "Unable to create proxy: %s\n", error->message);
-		g_error_free(error);
-		return FALSE;
+	const cJSON* pgood = cJSON_GetObjectItem(power_config, "power_good_in");
+	g_assert(pgood != NULL);
+
+	gpios->power_gpio.power_good_in.name = g_strdup(pgood->valuestring);
+
+	g_print("Power GPIO power good input: %s\n",
+			gpios->power_gpio.power_good_in.name);
+
+	/* Latch out - optional */
+
+	const cJSON* latch = cJSON_GetObjectItem(power_config, "latch_out");
+	if (latch != NULL)
+	{
+		gpios->power_gpio.latch_out.name = g_strdup(latch->valuestring);
+		g_print("Power GPIO latch output: %s\n",
+				gpios->power_gpio.latch_out.name);
 	}
-
-	value = g_dbus_proxy_call_sync(proxy,
-			"getGpioConfiguration",
-			NULL,
-			G_DBUS_CALL_FLAGS_NONE,
-			-1,
-			NULL,
-			&error);
-	if(error != NULL) {
-		fprintf(stderr, "Power GPIO: call to getGpioConfiguration failed: %s\n",
-				error->message);
-		g_error_free(error);
-		return FALSE;
-	}
-
-	g_assert(value != NULL);
-	memset(gpios, 0, sizeof(*gpios));
-	g_variant_get(
-		value, "(&s&sa(sb)a(sb)a(sbb)&s&s&s&sa(sb))",
-		&gpios->power_gpio.power_good_in.name, &gpios->power_gpio.latch_out.name,
-		&power_up_outs_iter, &reset_outs_iter, &pci_reset_outs_iter,
-		&gpios->hostctl_gpio.fsi_data.name, &gpios->hostctl_gpio.fsi_clk.name,
-		&gpios->hostctl_gpio.fsi_enable.name, &gpios->hostctl_gpio.cronus_sel.name,
-		&optionals_iter);
-
-	g_print("Power GPIO latch output: %s\n", gpios->power_gpio.latch_out.name);
-	if(*gpios->power_gpio.latch_out.name != '\0') {  /* latch is optional */
-		gpios->power_gpio.latch_out.name = strdup(gpios->power_gpio.latch_out.name);
-	}
-	else {
+	else
+	{
+		//Must be NULL if not there
 		gpios->power_gpio.latch_out.name = NULL;
 	}
-	g_print("Power GPIO power good input: %s\n", gpios->power_gpio.power_good_in.name);
-	gpios->power_gpio.power_good_in.name = g_strdup(gpios->power_gpio.power_good_in.name);
-	gpios->power_gpio.num_power_up_outs = g_variant_iter_n_children(
-			power_up_outs_iter);
+
+	/* Power Up Outs - required */
+
+	const cJSON* power_up_outs = cJSON_GetObjectItem(
+			power_config, "power_up_outs");
+	g_assert(power_up_outs != NULL);
+
+	gpios->power_gpio.num_power_up_outs = cJSON_GetArraySize(power_up_outs);
 	g_print("Power GPIO %zu power_up outputs\n",
 			gpios->power_gpio.num_power_up_outs);
-	gpios->power_gpio.power_up_outs = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
-			sizeof(GPIO));
-	gpios->power_gpio.power_up_pols = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
-			sizeof(gboolean));
-	for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)",
-				&power_up_out_name, &power_up_polarity);
-			i++) {
-		g_print("Power GPIO power_up[%d] = %s active %s\n", i,
-				power_up_out_name, power_up_polarity ? "HIGH" : "LOW");
-		gpios->power_gpio.power_up_outs[i].name = g_strdup(power_up_out_name);
-		gpios->power_gpio.power_up_pols[i] = power_up_polarity;
-	}
-	gpios->power_gpio.num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
-	g_print("Power GPIO %zu reset outputs\n", gpios->power_gpio.num_reset_outs);
-	gpios->power_gpio.reset_outs = g_malloc0_n(gpios->power_gpio.num_reset_outs, sizeof(GPIO));
-	gpios->power_gpio.reset_pols = g_malloc0_n(gpios->power_gpio.num_reset_outs,
-			sizeof(gboolean));
-	for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name,
-				&reset_out_polarity); i++) {
-		g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name,
-				reset_out_polarity ? "HIGH" : "LOW");
-		gpios->power_gpio.reset_outs[i].name = g_strdup(reset_out_name);
-		gpios->power_gpio.reset_pols[i] = reset_out_polarity;
+
+	if (gpios->power_gpio.num_power_up_outs != 0)
+	{
+		gpios->power_gpio.power_up_outs =
+			g_malloc0_n(gpios->power_gpio.num_power_up_outs, sizeof(GPIO));
+		gpios->power_gpio.power_up_pols =
+			g_malloc0_n(gpios->power_gpio.num_power_up_outs, sizeof(gboolean));
+
+		const cJSON* power_out;
+		cJSON_ArrayForEach(power_out, power_up_outs)
+		{
+			cJSON* name = cJSON_GetObjectItem(power_out, "name");
+			g_assert(name != NULL);
+			gpios->power_gpio.power_up_outs[i].name =
+				g_strdup(name->valuestring);
+
+			const cJSON* polarity = cJSON_GetObjectItem(power_out, "polarity");
+			g_assert(polarity != NULL);
+			gpios->power_gpio.power_up_pols[i] = polarity->valueint;
+
+			g_print("Power GPIO power_up[%d] = %s active %s\n",
+					i, gpios->power_gpio.power_up_outs[i].name,
+					gpios->power_gpio.power_up_pols[i] ? "HIGH" : "LOW");
+			i++;
+		}
 	}
 
-	gpios->power_gpio.num_pci_reset_outs = g_variant_iter_n_children(pci_reset_outs_iter);
-	g_print("Power GPIO %zd pci reset outputs\n", gpios->power_gpio.num_pci_reset_outs);
-	gpios->power_gpio.pci_reset_outs = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
-			sizeof(GPIO));
-	gpios->power_gpio.pci_reset_pols = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
-			sizeof(gboolean));
-	gpios->power_gpio.pci_reset_holds = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
-			sizeof(gboolean));
-	for(i = 0; g_variant_iter_next(pci_reset_outs_iter, "(&sbb)", &pci_reset_out_name,
-				&pci_reset_out_polarity, &pci_reset_out_hold); i++) {
-		g_print("Power GPIO pci reset[%d] = %s active %s, hold - %s\n", i,
-				pci_reset_out_name,
-				pci_reset_out_polarity ? "HIGH" : "LOW",
-				pci_reset_out_hold ? "Yes" : "No");
-		gpios->power_gpio.pci_reset_outs[i].name = g_strdup(pci_reset_out_name);
-		gpios->power_gpio.pci_reset_pols[i] = pci_reset_out_polarity;
-		gpios->power_gpio.pci_reset_holds[i] = pci_reset_out_hold;
+	/* Resets - optional */
+
+	const cJSON* reset_outs = cJSON_GetObjectItem(power_config, "reset_outs");
+	gpios->power_gpio.num_reset_outs = cJSON_GetArraySize(reset_outs);
+
+	g_print("Power GPIO %zu reset outputs\n",
+			gpios->power_gpio.num_reset_outs);
+
+	if (gpios->power_gpio.num_reset_outs != 0)
+	{
+		gpios->power_gpio.reset_outs =
+			g_malloc0_n(gpios->power_gpio.num_reset_outs, sizeof(GPIO));
+		gpios->power_gpio.reset_pols =
+			g_malloc0_n(gpios->power_gpio.num_reset_outs, sizeof(gboolean));
+
+		i = 0;
+		const cJSON* reset_out;
+		cJSON_ArrayForEach(reset_out, reset_outs)
+		{
+			cJSON* name = cJSON_GetObjectItem(reset_out, "name");
+			g_assert(name != NULL);
+			gpios->power_gpio.reset_outs[i].name = g_strdup(name->valuestring);
+
+			const cJSON* polarity = cJSON_GetObjectItem(reset_out, "polarity");
+			g_assert(polarity != NULL);
+			gpios->power_gpio.reset_pols[i] = polarity->valueint;
+
+			g_print("Power GPIO reset[%d] = %s active %s\n", i,
+					gpios->power_gpio.reset_outs[i].name,
+					gpios->power_gpio.reset_pols[i] ? "HIGH" : "LOW");
+			i++;
+		}
 	}
 
+	/* PCI Resets - optional */
 
-	g_print("FSI DATA GPIO: %s\n", gpios->hostctl_gpio.fsi_data.name);
-	gpios->hostctl_gpio.fsi_data.name = strdup(gpios->hostctl_gpio.fsi_data.name);
+	const cJSON* pci_reset_outs = cJSON_GetObjectItem(
+			power_config, "pci_reset_outs");
 
-	g_print("FSI CLK GPIO: %s\n", gpios->hostctl_gpio.fsi_clk.name);
-	gpios->hostctl_gpio.fsi_clk.name = strdup(gpios->hostctl_gpio.fsi_clk.name);
+	gpios->power_gpio.num_pci_reset_outs =
+		cJSON_GetArraySize(pci_reset_outs);
 
-	g_print("FSI ENABLE GPIO: %s\n", gpios->hostctl_gpio.fsi_enable.name);
-	gpios->hostctl_gpio.fsi_enable.name = strdup(gpios->hostctl_gpio.fsi_enable.name);
+	g_print("Power GPIO %zd pci reset outputs\n",
+			gpios->power_gpio.num_pci_reset_outs);
 
-	g_print("CRONUS SEL GPIO: %s\n", gpios->hostctl_gpio.cronus_sel.name);
-	gpios->hostctl_gpio.cronus_sel.name = strdup(gpios->hostctl_gpio.cronus_sel.name);
+	if (gpios->power_gpio.num_pci_reset_outs != 0)
+	{
+		gpios->power_gpio.pci_reset_outs =
+			g_malloc0_n(gpios->power_gpio.num_pci_reset_outs, sizeof(GPIO));
+		gpios->power_gpio.pci_reset_pols =
+			g_malloc0_n(gpios->power_gpio.num_pci_reset_outs, sizeof(gboolean));
+		gpios->power_gpio.pci_reset_holds =
+			g_malloc0_n(gpios->power_gpio.num_pci_reset_outs, sizeof(gboolean));
 
-	gpios->hostctl_gpio.num_optionals = g_variant_iter_n_children(optionals_iter);
-	g_print("Hostctl GPIO optionals: %zu\n", gpios->hostctl_gpio.num_optionals);
-	gpios->hostctl_gpio.optionals = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(GPIO));
-	gpios->hostctl_gpio.optional_pols = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(gboolean));
-	for (i = 0; g_variant_iter_next(optionals_iter, "(&sb)", &optional_name, &optional_polarity); ++i) {
-		g_print("Hostctl optional GPIO[%d] = %s active %s\n", i, optional_name, optional_polarity ? "HIGH" : "LOW");
-		gpios->hostctl_gpio.optionals[i].name = g_strdup(optional_name);
-		gpios->hostctl_gpio.optional_pols[i] = optional_polarity;
+		i = 0;
+		const cJSON* pci_reset_out;
+		cJSON_ArrayForEach(pci_reset_out, pci_reset_outs)
+		{
+			cJSON* name = cJSON_GetObjectItem(pci_reset_out, "name");
+			g_assert(name != NULL);
+            gpios->power_gpio.pci_reset_outs[i].name =
+                g_strdup(name->valuestring);
+
+			const cJSON* polarity = cJSON_GetObjectItem(
+                    pci_reset_out, "polarity");
+			g_assert(polarity != NULL);
+			gpios->power_gpio.pci_reset_pols[i] = polarity->valueint;
+
+			const cJSON* hold = cJSON_GetObjectItem(pci_reset_out, "hold");
+			g_assert(hold != NULL);
+			gpios->power_gpio.pci_reset_holds[i] = polarity->valueint;
+
+			g_print("Power GPIO pci reset[%d] = %s active %s, hold %s\n", i,
+					gpios->power_gpio.pci_reset_outs[i].name,
+					gpios->power_gpio.pci_reset_pols[i] ? "HIGH" : "LOW",
+					gpios->power_gpio.pci_reset_holds[i] ? "Yes" : "No");
+			i++;
+		}
+	}
+}
+
+gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios)
+{
+	cJSON* json = load_json();
+	if (json == NULL)
+	{
+		return FALSE;
 	}
 
-	g_variant_iter_free(power_up_outs_iter);
-	g_variant_iter_free(reset_outs_iter);
-	g_variant_iter_free(pci_reset_outs_iter);
-	g_variant_iter_free(optionals_iter);
-	g_variant_unref(value);
+	const cJSON* configs = cJSON_GetObjectItem(json, "gpio_configs");
+	g_assert(configs != NULL);
 
+	read_power_gpios(gpios, configs);
+
+	cJSON_Delete(json);
 	return TRUE;
 }