Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 1 | #include "interfaces/openbmc_intf.h"
|
| 2 | #include "openbmc.h"
|
Norman James | 3d3b7fb | 2015-10-06 16:54:06 -0500 | [diff] [blame] | 3 | #include <stdio.h>
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 4 | #include <stdbool.h>
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 5 | #include <string.h>
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 6 | #include "gpio.h"
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 7 |
|
| 8 | #define NUM_SLOTS 4
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 9 | GPIO slots[NUM_SLOTS] = {
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 10 | // { "SLOT0_RISER_PRESENT" },
|
| 11 | // { "SLOT1_RISER_PRESENT" },
|
| 12 | // { "SLOT2_RISER_PRESENT" },
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 13 | { "SLOT0_PRESENT" },
|
| 14 | { "SLOT1_PRESENT" },
|
| 15 | { "SLOT2_PRESENT" },
|
| 16 | { "MEZZ0_PRESENT" },
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 17 | // { "MEZZ1_PRESENT" },
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 18 | };
|
| 19 |
|
| 20 | typedef struct {
|
| 21 | const char* bus_name;
|
| 22 | const char* path;
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 23 | const char* intf_name;
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 24 | } object_info;
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 | /* ---------------------------------------------------------------------------------------------------- */
|
| 29 | int get_object(GDBusProxy *proxy, GPIO* gpio, object_info* obj_info)
|
| 30 | {
|
Norman James | 3d3b7fb | 2015-10-06 16:54:06 -0500 | [diff] [blame] | 31 | g_print("Checking Presence: %s\n",gpio->name);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 32 | 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 James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 46 |
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 47 | GVariantIter *iter = g_variant_iter_new(result);
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 48 | GVariant* v_result = g_variant_iter_next_value(iter);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 49 |
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 50 | g_variant_get(v_result,"(sss)",&obj_info->bus_name,&obj_info->path,&obj_info->intf_name);
|
| 51 | int rc=0;
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 52 | if (strlen(obj_info->bus_name) == 0) {
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 53 | rc = 1;
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 54 | }
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 55 | g_variant_unref(v_result);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 56 | g_variant_unref(result);
|
| 57 |
|
| 58 | return rc;
|
| 59 | }
|
| 60 |
|
| 61 | int 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 James | 3d3b7fb | 2015-10-06 16:54:06 -0500 | [diff] [blame] | 77 | printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | }
|
| 79 | return rc;
|
| 80 | }
|
| 81 |
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 82 | void update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present)
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 83 | {
|
| 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 James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 95 | obj_info->intf_name, /* interface name */
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 96 | NULL, /* GCancellable */
|
| 97 | &error);
|
| 98 | g_assert_no_error (error);
|
| 99 |
|
| 100 | error = NULL;
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 101 | parm = g_variant_new("(s)",present);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 102 |
|
| 103 | result = g_dbus_proxy_call_sync (proxy,
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 104 | "setPresent",
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 105 | parm,
|
| 106 | G_DBUS_CALL_FLAGS_NONE,
|
| 107 | -1,
|
| 108 | NULL,
|
| 109 | &error);
|
| 110 |
|
| 111 | g_assert_no_error (error);
|
| 112 | }
|
| 113 |
|
| 114 | gint
|
| 115 | main (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 James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 127 | c = g_bus_get_sync (DBUS_TYPE, NULL, &error);
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 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;
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 146 | char* chr_present;
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 147 | do {
|
| 148 | rc = get_object(sys_proxy,&slots[i],&obj_info);
|
| 149 | if (rc) { break; }
|
| 150 | rc = get_presence(c,&slots[i],&present);
|
Norman James | 1d30992 | 2015-10-15 10:23:58 -0500 | [diff] [blame] | 151 | //if (rc) { break; }
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 152 | // TODO: send correct state
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 153 | if (present == 1) {
|
| 154 | update_fru_obj(c,&obj_info,"PRESENT");
|
| 155 | } else {
|
| 156 | update_fru_obj(c,&obj_info,"NOT PRESENT");
|
| 157 | }
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 158 | } while(0);
|
| 159 | }
|
| 160 |
|
| 161 | g_object_unref(c);
|
| 162 | g_main_loop_unref (loop);
|
| 163 | return 0;
|
| 164 | }
|