Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 Google Inc. |
| 3 | * Copyright © 2016 IBM Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "power_gpio.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <glib.h> |
| 24 | |
| 25 | gboolean read_power_gpio(GDBusConnection *connection, PowerGpio *power_gpio) |
| 26 | { |
| 27 | GDBusProxy *proxy; |
| 28 | GError *error; |
| 29 | GVariant *value; |
| 30 | gchar *power_good_in_name; |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame^] | 31 | gchar *latch_out_name; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 32 | GVariantIter *power_up_outs_iter; |
| 33 | GVariantIter *reset_outs_iter; |
| 34 | gchar *power_up_out_name; |
| 35 | gchar *reset_out_name; |
| 36 | gboolean power_up_polarity; |
| 37 | gboolean reset_out_polarity; |
| 38 | int i; |
| 39 | |
| 40 | proxy = g_dbus_proxy_new_sync(connection, |
| 41 | G_DBUS_PROXY_FLAGS_NONE, |
| 42 | NULL, /* GDBusInterfaceInfo */ |
| 43 | "org.openbmc.managers.System", /* name */ |
| 44 | "/org/openbmc/managers/System", /* object path */ |
| 45 | "org.openbmc.managers.System", /* interface */ |
| 46 | NULL, /* GCancellable */ |
| 47 | &error); |
| 48 | if(error != NULL) { |
| 49 | fprintf(stderr, "Unable to create proxy: %s\n", error->message); |
| 50 | g_error_free(error); |
| 51 | return FALSE; |
| 52 | } |
| 53 | |
| 54 | value = g_dbus_proxy_call_sync(proxy, |
| 55 | "getPowerConfiguration", |
| 56 | NULL, |
| 57 | G_DBUS_CALL_FLAGS_NONE, |
| 58 | -1, |
| 59 | NULL, |
| 60 | &error); |
| 61 | if(error != NULL) { |
| 62 | fprintf(stderr, "Power GPIO: call to getPowerConfiguration failed: %s\n", |
| 63 | error->message); |
| 64 | g_error_free(error); |
| 65 | return FALSE; |
| 66 | } |
| 67 | |
| 68 | g_assert(value != NULL); |
| 69 | memset(power_gpio, 0, sizeof(*power_gpio)); |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame^] | 70 | g_variant_get(value, "(&s&sa(sb)a(sb))", &power_good_in_name, &latch_out_name, |
| 71 | &power_up_outs_iter, &reset_outs_iter); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 72 | |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame^] | 73 | g_print("Power GPIO latch output %s\n", latch_out_name); |
| 74 | if(*latch_out_name != '\0') { /* latch is optional */ |
| 75 | power_gpio->latch_out.name = strdup(latch_out_name); |
| 76 | } |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 77 | g_print("Power GPIO power good input %s\n", power_good_in_name); |
| 78 | power_gpio->power_good_in.name = g_strdup(power_good_in_name); |
| 79 | power_gpio->num_power_up_outs = g_variant_iter_n_children( |
| 80 | power_up_outs_iter); |
| 81 | g_print("Power GPIO %d power_up outputs\n", |
| 82 | power_gpio->num_power_up_outs); |
| 83 | power_gpio->power_up_outs = g_malloc0_n(power_gpio->num_power_up_outs, |
| 84 | sizeof(GPIO)); |
| 85 | power_gpio->power_up_pols = g_malloc0_n(power_gpio->num_power_up_outs, |
| 86 | sizeof(gboolean)); |
| 87 | for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)", |
| 88 | &power_up_out_name, &power_up_polarity); |
| 89 | i++) { |
| 90 | g_print("Power GPIO power_up[%d] = %s active %s\n", i, |
| 91 | power_up_out_name, power_up_polarity ? "HIGH" : "LOW"); |
| 92 | power_gpio->power_up_outs[i].name = g_strdup(power_up_out_name); |
| 93 | power_gpio->power_up_pols[i] = power_up_polarity; |
| 94 | } |
| 95 | power_gpio->num_reset_outs = g_variant_iter_n_children(reset_outs_iter); |
| 96 | g_print("Power GPIO %d reset outputs\n", power_gpio->num_reset_outs); |
| 97 | power_gpio->reset_outs = g_malloc0_n(power_gpio->num_reset_outs, sizeof(GPIO)); |
| 98 | power_gpio->reset_pols = g_malloc0_n(power_gpio->num_reset_outs, |
| 99 | sizeof(gboolean)); |
| 100 | for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name, |
| 101 | &reset_out_polarity); i++) { |
| 102 | g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name, |
| 103 | reset_out_polarity ? "HIGH" : "LOW"); |
| 104 | power_gpio->reset_outs[i].name = g_strdup(reset_out_name); |
| 105 | power_gpio->reset_pols[i] = reset_out_polarity; |
| 106 | } |
| 107 | |
| 108 | g_variant_iter_free(power_up_outs_iter); |
| 109 | g_variant_iter_free(reset_outs_iter); |
| 110 | g_variant_unref(value); |
| 111 | |
| 112 | return TRUE; |
| 113 | } |
| 114 | |
| 115 | void free_power_gpio(PowerGpio *power_gpio) { |
| 116 | int i; |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame^] | 117 | g_free(power_gpio->latch_out.name); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 118 | g_free(power_gpio->power_good_in.name); |
| 119 | for(i = 0; i < power_gpio->num_power_up_outs; i++) { |
| 120 | g_free(power_gpio->power_up_outs[i].name); |
| 121 | } |
| 122 | g_free(power_gpio->power_up_outs); |
| 123 | g_free(power_gpio->power_up_pols); |
| 124 | for(i = 0; i < power_gpio->num_reset_outs; i++) { |
| 125 | g_free(power_gpio->reset_outs[i].name); |
| 126 | } |
| 127 | g_free(power_gpio->reset_outs); |
| 128 | g_free(power_gpio->reset_pols); |
| 129 | } |