Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 1 | #include <openbmc_intf.h> |
| 2 | #include <openbmc.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <string.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 6 | #include <gpio.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 7 | |
| 8 | #define NUM_SLOTS 8 |
| 9 | GPIO slots[NUM_SLOTS] = { |
| 10 | { "SLOT0_RISER_PRESENT" }, |
| 11 | { "SLOT1_RISER_PRESENT" }, |
| 12 | { "SLOT2_RISER_PRESENT" }, |
| 13 | { "SLOT0_PRESENT" }, |
| 14 | { "SLOT1_PRESENT" }, |
| 15 | { "SLOT2_PRESENT" }, |
| 16 | { "MEZZ0_PRESENT" }, |
| 17 | { "MEZZ1_PRESENT" }, |
| 18 | }; |
| 19 | |
| 20 | typedef struct { |
| 21 | const char* bus_name; |
| 22 | const char* path; |
| 23 | const char* intf_name; |
| 24 | } object_info; |
| 25 | |
| 26 | |
| 27 | |
| 28 | /* ------------------------------------------------------------------------- */ |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 29 | void |
| 30 | get_service(GDBusConnection *connection, const char *obj, |
| 31 | const char **service, GError **error) |
| 32 | { |
| 33 | GDBusProxy *proxy; |
| 34 | GVariant *result = NULL; |
| 35 | GVariantIter *iter; |
| 36 | |
| 37 | *error = NULL; |
| 38 | proxy = g_dbus_proxy_new_sync(connection, |
| 39 | G_DBUS_PROXY_FLAGS_NONE, |
| 40 | NULL, /* GDBusInterfaceInfo* */ |
Brad Bishop | e87cc29 | 2017-02-25 15:39:33 -0500 | [diff] [blame] | 41 | "xyz.openbmc_project.ObjectMapper", /* name */ |
| 42 | "/xyz/openbmc_project/ObjectMapper", /* object path */ |
| 43 | "xyz.openbmc_project.ObjectMapper", /* interface name */ |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 44 | NULL, /* GCancellable */ |
| 45 | error); |
| 46 | |
| 47 | result = g_dbus_proxy_call_sync(proxy, |
| 48 | "GetObject", |
| 49 | g_variant_new("(s)", obj), |
| 50 | G_DBUS_CALL_FLAGS_NONE, |
| 51 | -1, |
| 52 | NULL, |
| 53 | error); |
| 54 | if(*error) |
| 55 | goto exit; |
| 56 | |
| 57 | g_variant_get(result, "(a{sas})", &iter); |
| 58 | g_variant_iter_next(iter, "{sas}", service, NULL); |
| 59 | |
| 60 | exit: |
| 61 | if(result) |
| 62 | g_variant_unref(result); |
| 63 | } |
| 64 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 65 | int |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 66 | get_object(GDBusConnection *connection, GDBusProxy *proxy, |
| 67 | GPIO* gpio, object_info* obj_info) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 68 | { |
| 69 | g_print("Checking Presence: %s\n",gpio->name); |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 70 | const char *gpio_bus = NULL; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 71 | GError *error; |
| 72 | GVariant *parm; |
| 73 | GVariant *result; |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 74 | int rc=0; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 75 | |
| 76 | error = NULL; |
| 77 | parm = g_variant_new("(ss)","GPIO_PRESENT",gpio->name); |
| 78 | result = g_dbus_proxy_call_sync(proxy, |
| 79 | "getObjectFromId", |
| 80 | parm, |
| 81 | G_DBUS_CALL_FLAGS_NONE, |
| 82 | -1, |
| 83 | NULL, |
| 84 | &error); |
| 85 | g_assert_no_error(error); |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 86 | if(error) |
| 87 | goto exit; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 88 | |
| 89 | GVariantIter *iter = g_variant_iter_new(result); |
| 90 | GVariant* v_result = g_variant_iter_next_value(iter); |
| 91 | |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 92 | g_variant_get(v_result,"(ss)",&obj_info->path,&obj_info->intf_name); |
| 93 | |
| 94 | get_service(connection, obj_info->path, &gpio_bus, &error); |
| 95 | if(error) |
| 96 | goto exit; |
| 97 | |
| 98 | obj_info->bus_name = gpio_bus; |
| 99 | |
| 100 | exit: |
| 101 | if(!gpio_bus || strlen(gpio_bus) == 0) { |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 102 | rc = 1; |
| 103 | } |
| 104 | g_variant_unref(v_result); |
| 105 | g_variant_unref(result); |
| 106 | |
| 107 | return rc; |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present) |
| 112 | { |
| 113 | int rc = GPIO_OK; |
| 114 | do { |
| 115 | rc = gpio_init(connection,gpio); |
| 116 | if(rc != GPIO_OK) { break; } |
| 117 | uint8_t gpio_val; |
| 118 | rc = gpio_open(gpio); |
| 119 | if(rc != GPIO_OK) { break; } |
| 120 | rc = gpio_read(gpio,&gpio_val); |
| 121 | if(rc != GPIO_OK) { gpio_close(gpio); break; } |
| 122 | gpio_close(gpio); |
| 123 | *present = gpio_val; |
| 124 | } while(0); |
| 125 | if(rc != GPIO_OK) |
| 126 | { |
| 127 | printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc); |
| 128 | } |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | update_fru_obj(GDBusConnection* connection, object_info* obj_info, const char* present) |
| 134 | { |
| 135 | GDBusProxy *proxy; |
| 136 | GError *error; |
| 137 | GVariant *parm; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 138 | |
| 139 | error = NULL; |
| 140 | proxy = g_dbus_proxy_new_sync(connection, |
| 141 | G_DBUS_PROXY_FLAGS_NONE, |
| 142 | NULL, /* GDBusInterfaceInfo* */ |
| 143 | obj_info->bus_name, /* name */ |
| 144 | obj_info->path, /* object path */ |
| 145 | obj_info->intf_name, /* interface name */ |
| 146 | NULL, /* GCancellable */ |
| 147 | &error); |
| 148 | g_assert_no_error(error); |
| 149 | |
| 150 | error = NULL; |
| 151 | parm = g_variant_new("(s)",present); |
| 152 | |
Brad Bishop | 0c82c60 | 2016-04-13 13:36:49 -0400 | [diff] [blame] | 153 | g_dbus_proxy_call_sync(proxy, |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 154 | "setPresent", |
| 155 | parm, |
| 156 | G_DBUS_CALL_FLAGS_NONE, |
| 157 | -1, |
| 158 | NULL, |
| 159 | &error); |
| 160 | |
| 161 | g_assert_no_error(error); |
| 162 | } |
| 163 | |
| 164 | gint |
| 165 | main(gint argc, gchar *argv[]) |
| 166 | { |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 167 | const char *sysmgr_path = "/org/openbmc/managers/System"; |
| 168 | const char *sysmgr_bus = NULL; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 169 | GMainLoop *loop; |
| 170 | GDBusConnection *c; |
| 171 | GDBusProxy *sys_proxy; |
| 172 | GError *error; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 173 | |
| 174 | loop = g_main_loop_new(NULL, FALSE); |
| 175 | |
| 176 | error = NULL; |
| 177 | c = g_bus_get_sync(DBUS_TYPE, NULL, &error); |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 178 | if(error) |
| 179 | goto exit; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 180 | |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 181 | get_service(c, sysmgr_path, &sysmgr_bus, &error); |
| 182 | if(error) |
| 183 | goto exit; |
| 184 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 185 | sys_proxy = g_dbus_proxy_new_sync(c, |
| 186 | G_DBUS_PROXY_FLAGS_NONE, |
| 187 | NULL, /* GDBusInterfaceInfo* */ |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 188 | sysmgr_bus, /* name */ |
| 189 | sysmgr_path, /* object path */ |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 190 | "org.openbmc.managers.System", /* interface name */ |
| 191 | NULL, /* GCancellable */ |
| 192 | &error); |
| 193 | g_assert_no_error(error); |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 194 | if(error) |
| 195 | goto exit; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 196 | |
| 197 | int i = 0; |
| 198 | int rc = 0; |
| 199 | for(i=0;i<NUM_SLOTS;i++) |
| 200 | { |
| 201 | object_info obj_info; |
| 202 | uint8_t present; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 203 | do { |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 204 | rc = get_object(c,sys_proxy,&slots[i],&obj_info); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 205 | if(rc) { break; } |
| 206 | rc = get_presence(c,&slots[i],&present); |
| 207 | //if (rc) { break; } |
| 208 | // TODO: send correct state |
| 209 | if(present == 0) { |
| 210 | update_fru_obj(c,&obj_info,"True"); |
| 211 | } else { |
| 212 | update_fru_obj(c,&obj_info,"False"); |
| 213 | } |
| 214 | } while(0); |
| 215 | } |
| 216 | |
Brad Bishop | eb228b5 | 2016-07-26 15:03:04 -0400 | [diff] [blame] | 217 | exit: |
| 218 | if(sysmgr_bus) |
| 219 | g_free((char *)sysmgr_bus); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 220 | g_object_unref(c); |
| 221 | g_main_loop_unref(loop); |
| 222 | return 0; |
| 223 | } |