blob: 3f4b5a8574224caa6eee98dcaca797b74b0a14fd [file] [log] [blame]
Xo Wang3f87de82016-09-22 11:17:01 -07001/**
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
25gboolean read_power_gpio(GDBusConnection *connection, PowerGpio *power_gpio)
26{
27 GDBusProxy *proxy;
28 GError *error;
29 GVariant *value;
30 gchar *power_good_in_name;
31 GVariantIter *power_up_outs_iter;
32 GVariantIter *reset_outs_iter;
33 gchar *power_up_out_name;
34 gchar *reset_out_name;
35 gboolean power_up_polarity;
36 gboolean reset_out_polarity;
37 int i;
38
39 proxy = g_dbus_proxy_new_sync(connection,
40 G_DBUS_PROXY_FLAGS_NONE,
41 NULL, /* GDBusInterfaceInfo */
42 "org.openbmc.managers.System", /* name */
43 "/org/openbmc/managers/System", /* object path */
44 "org.openbmc.managers.System", /* interface */
45 NULL, /* GCancellable */
46 &error);
47 if(error != NULL) {
48 fprintf(stderr, "Unable to create proxy: %s\n", error->message);
49 g_error_free(error);
50 return FALSE;
51 }
52
53 value = g_dbus_proxy_call_sync(proxy,
54 "getPowerConfiguration",
55 NULL,
56 G_DBUS_CALL_FLAGS_NONE,
57 -1,
58 NULL,
59 &error);
60 if(error != NULL) {
61 fprintf(stderr, "Power GPIO: call to getPowerConfiguration failed: %s\n",
62 error->message);
63 g_error_free(error);
64 return FALSE;
65 }
66
67 g_assert(value != NULL);
68 memset(power_gpio, 0, sizeof(*power_gpio));
69 g_variant_get(value, "(&sa(sb)a(sb))", &power_good_in_name,
70 &power_up_outs_iter, &reset_outs_iter);
71
72 g_print("Power GPIO power good input %s\n", power_good_in_name);
73 power_gpio->power_good_in.name = g_strdup(power_good_in_name);
74 power_gpio->num_power_up_outs = g_variant_iter_n_children(
75 power_up_outs_iter);
76 g_print("Power GPIO %d power_up outputs\n",
77 power_gpio->num_power_up_outs);
78 power_gpio->power_up_outs = g_malloc0_n(power_gpio->num_power_up_outs,
79 sizeof(GPIO));
80 power_gpio->power_up_pols = g_malloc0_n(power_gpio->num_power_up_outs,
81 sizeof(gboolean));
82 for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)",
83 &power_up_out_name, &power_up_polarity);
84 i++) {
85 g_print("Power GPIO power_up[%d] = %s active %s\n", i,
86 power_up_out_name, power_up_polarity ? "HIGH" : "LOW");
87 power_gpio->power_up_outs[i].name = g_strdup(power_up_out_name);
88 power_gpio->power_up_pols[i] = power_up_polarity;
89 }
90 power_gpio->num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
91 g_print("Power GPIO %d reset outputs\n", power_gpio->num_reset_outs);
92 power_gpio->reset_outs = g_malloc0_n(power_gpio->num_reset_outs, sizeof(GPIO));
93 power_gpio->reset_pols = g_malloc0_n(power_gpio->num_reset_outs,
94 sizeof(gboolean));
95 for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name,
96 &reset_out_polarity); i++) {
97 g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name,
98 reset_out_polarity ? "HIGH" : "LOW");
99 power_gpio->reset_outs[i].name = g_strdup(reset_out_name);
100 power_gpio->reset_pols[i] = reset_out_polarity;
101 }
102
103 g_variant_iter_free(power_up_outs_iter);
104 g_variant_iter_free(reset_outs_iter);
105 g_variant_unref(value);
106
107 return TRUE;
108}
109
110void free_power_gpio(PowerGpio *power_gpio) {
111 int i;
112 g_free(power_gpio->power_good_in.name);
113 for(i = 0; i < power_gpio->num_power_up_outs; i++) {
114 g_free(power_gpio->power_up_outs[i].name);
115 }
116 g_free(power_gpio->power_up_outs);
117 g_free(power_gpio->power_up_pols);
118 for(i = 0; i < power_gpio->num_reset_outs; i++) {
119 g_free(power_gpio->reset_outs[i].name);
120 }
121 g_free(power_gpio->reset_outs);
122 g_free(power_gpio->reset_pols);
123}