blob: ada9b3b5fec64b1ff39b086c7f1b3f12e2be5c67 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <sys/stat.h>
8#include <sys/mman.h>
9#include <syslog.h>
Brad Bishopf6c85682016-06-27 11:56:39 -040010#include <openbmc_intf.h>
11#include <openbmc.h>
12#include <gpio.h>
Lei YU45cb4fc2016-11-29 01:48:26 +080013#include <gpio_configs.h>
Brad Bishop77390492016-04-13 10:47:19 -040014
15/* ------------------------------------------------------------------------- */
16static const gchar* dbus_object_path = "/org/openbmc/control";
17static const gchar* instance_name = "power0";
18static const gchar* dbus_name = "org.openbmc.control.Power";
19
Yi Li0475f652016-10-25 13:19:59 +080020static int g_pci_reset_held = 1;
21
Lei YU75a18a22016-11-22 01:47:47 +080022static GpioConfigs g_gpio_configs;
Brad Bishop77390492016-04-13 10:47:19 -040023
24static GDBusObjectManagerServer *manager = NULL;
25
26time_t pgood_timeout_start = 0;
27
28// TODO: Change to interrupt driven instead of polling
29static gboolean
30poll_pgood(gpointer user_data)
31{
32 ControlPower *control_power = object_get_control_power((Object*)user_data);
33 Control* control = object_get_control((Object*)user_data);
34
35 //send the heartbeat
Brad Bishop77390492016-04-13 10:47:19 -040036 guint poll_int = control_get_poll_interval(control);
37 if(poll_int == 0)
38 {
Xo Wang20a19412016-09-22 11:26:14 -070039 g_print("ERROR PowerControl: Poll interval cannot be 0\n");
Brad Bishop77390492016-04-13 10:47:19 -040040 return FALSE;
41 }
42 //handle timeout
43 time_t current_time = time(NULL);
44 if(difftime(current_time,pgood_timeout_start) > control_power_get_pgood_timeout(control_power)
45 && pgood_timeout_start != 0)
46 {
Xo Wang20a19412016-09-22 11:26:14 -070047 g_print("ERROR PowerControl: Pgood poll timeout\n");
Brad Bishop77390492016-04-13 10:47:19 -040048 // set timeout to 0 so timeout doesn't happen again
49 control_power_set_pgood_timeout(control_power,0);
50 pgood_timeout_start = 0;
Patrick Williams27eaf902017-03-21 15:46:40 -050051 return TRUE;
Brad Bishop77390492016-04-13 10:47:19 -040052 }
Xo Wang20a19412016-09-22 11:26:14 -070053 uint8_t pgood_state;
Brad Bishop77390492016-04-13 10:47:19 -040054
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060055 int rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0);
Xo Wang20a19412016-09-22 11:26:14 -070056 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060057 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -070058 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +080059 g_gpio_configs.power_gpio.power_good_in.name, rc);
Xo Wang20a19412016-09-22 11:26:14 -070060 return FALSE;
61 }
Lei YU75a18a22016-11-22 01:47:47 +080062 rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state);
63 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Brad Bishop77390492016-04-13 10:47:19 -040064 if(rc == GPIO_OK)
65 {
66 //if changed, set property and emit signal
Xo Wang20a19412016-09-22 11:26:14 -070067 if(pgood_state != control_power_get_pgood(control_power))
Brad Bishop77390492016-04-13 10:47:19 -040068 {
Xo Wang20a19412016-09-22 11:26:14 -070069 int i;
70 uint8_t reset_state;
71 control_power_set_pgood(control_power, pgood_state);
72 if(pgood_state == 0)
Brad Bishop77390492016-04-13 10:47:19 -040073 {
74 control_power_emit_power_lost(control_power);
Yi Li0475f652016-10-25 13:19:59 +080075 g_pci_reset_held = 1;
Brad Bishop77390492016-04-13 10:47:19 -040076 }
77 else
78 {
79 control_power_emit_power_good(control_power);
Xo Wang20a19412016-09-22 11:26:14 -070080 }
Brad Bishop77390492016-04-13 10:47:19 -040081
Lei YU75a18a22016-11-22 01:47:47 +080082 for(i = 0; i < g_gpio_configs.power_gpio.num_reset_outs; i++)
Xo Wang20a19412016-09-22 11:26:14 -070083 {
Lei YU75a18a22016-11-22 01:47:47 +080084 GPIO *reset_out = &g_gpio_configs.power_gpio.reset_outs[i];
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060085 reset_state = pgood_state ^ g_gpio_configs.power_gpio.reset_pols[i];
86 rc = gpio_open(reset_out, reset_state);
Xo Wang20a19412016-09-22 11:26:14 -070087 if(rc != GPIO_OK)
88 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060089 gpio_close(reset_out);
Xo Wang20a19412016-09-22 11:26:14 -070090 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
91 reset_out->name, rc);
92 continue;
93 }
94
Yi Li39df4032016-12-16 16:06:50 +080095 g_print("PowerControl: pgood: %d, setting reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +080096 (int)pgood_state, reset_out->name, (int)reset_state);
Xo Wang20a19412016-09-22 11:26:14 -070097 gpio_write(reset_out, reset_state);
98 gpio_close(reset_out);
Brad Bishop77390492016-04-13 10:47:19 -040099 }
Yi Li0475f652016-10-25 13:19:59 +0800100
Lei YU75a18a22016-11-22 01:47:47 +0800101 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800102 {
Lei YU75a18a22016-11-22 01:47:47 +0800103 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800104 if(pgood_state == 1)
105 {
106 /*
107 * When powering on, hold PCI reset until
108 * the processor can forward clocks and control reset.
109 */
Lei YU75a18a22016-11-22 01:47:47 +0800110 if(g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800111 {
112 g_print("Holding pci reset: %s\n", pci_reset_out->name);
113 continue;
114 }
115 }
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600116 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
117 rc = gpio_open(pci_reset_out, reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800118 if(rc != GPIO_OK)
119 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600120 gpio_close(pci_reset_out);
Yi Li0475f652016-10-25 13:19:59 +0800121 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
122 pci_reset_out->name, rc);
123 continue;
124 }
125
Yi Li39df4032016-12-16 16:06:50 +0800126 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800127 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800128 gpio_write(pci_reset_out, reset_state);
129 gpio_close(pci_reset_out);
130 }
Brad Bishop77390492016-04-13 10:47:19 -0400131 }
132 } else {
Xo Wang20a19412016-09-22 11:26:14 -0700133 g_print("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800134 g_gpio_configs.power_gpio.power_good_in.name, rc);
Brad Bishop77390492016-04-13 10:47:19 -0400135 //return false so poll won't get called anymore
136 return FALSE;
137 }
138 //pgood is not at desired state yet
Xo Wang20a19412016-09-22 11:26:14 -0700139 if(pgood_state != control_power_get_state(control_power) &&
Brad Bishop77390492016-04-13 10:47:19 -0400140 control_power_get_pgood_timeout(control_power) > 0)
141 {
142 if(pgood_timeout_start == 0 ) {
143 pgood_timeout_start = current_time;
144 }
145 }
146 else
147 {
148 pgood_timeout_start = 0;
149 }
150 return TRUE;
151}
152
Yi Li0475f652016-10-25 13:19:59 +0800153/* Handler for BootProgress signal from BootProgress sensor */
154static void
155on_boot_progress(GDBusConnection *connection,
156 const gchar *sender_name,
157 const gchar *object_path,
158 const gchar *interface_name,
159 const gchar *signal_name,
160 GVariant *parameters,
161 gpointer user_data)
162{
Lei YU93b84e42017-10-11 15:06:20 +0800163 gchar *interface;
164 GVariantIter *properties;
165 GVariantIter *dummy;
166 gchar *boot_progress = NULL;
167 gchar *property;
168 GVariant *value;
Yi Li0475f652016-10-25 13:19:59 +0800169 uint8_t pgood_state;
170 uint8_t reset_state;
171 int rc;
172 int i;
Lei YU93b84e42017-10-11 15:06:20 +0800173 int ignore;
Yi Li0475f652016-10-25 13:19:59 +0800174
175 if(!parameters)
176 return;
177
178 /* prevent release again */
179 if(!g_pci_reset_held)
180 return;
181
Lei YU93b84e42017-10-11 15:06:20 +0800182 g_variant_get(parameters, "(&sa{sv}as)", &interface, &properties, &dummy);
183 for(i = 0; g_variant_iter_next(properties, "{&sv}", &property, &value); i++)
184 {
185 if (strcmp(property, "BootProgress") == 0)
186 {
187 gchar* tmp;
188 g_variant_get(value, "&s", &tmp);
189 boot_progress = g_strdup(tmp);
190 g_print("BootProgress: %s\n", boot_progress);
191 g_variant_unref(value);
192 }
193 }
194
195 g_variant_iter_free(properties);
196 g_variant_iter_free(dummy);
197 if (boot_progress == NULL)
198 return;
199
Yi Li0475f652016-10-25 13:19:59 +0800200 /* Release PCI reset when FW boot progress goes beyond 'Baseboard Init' */
Lei YU93b84e42017-10-11 15:06:20 +0800201 ignore = strcmp(boot_progress,
202 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit") == 0;
203 g_free(boot_progress);
204
205 if (ignore)
Yi Li0475f652016-10-25 13:19:59 +0800206 return;
207
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600208 rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0);
Yi Li0475f652016-10-25 13:19:59 +0800209 if(rc != GPIO_OK)
210 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600211 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Yi Li0475f652016-10-25 13:19:59 +0800212 g_print("ERROR PowerControl: on_boot_progress(): GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800213 g_gpio_configs.power_gpio.power_good_in.name, rc);
Yi Li0475f652016-10-25 13:19:59 +0800214 return;
215 }
Lei YU75a18a22016-11-22 01:47:47 +0800216 rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state);
217 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Yi Li0475f652016-10-25 13:19:59 +0800218 if(rc != GPIO_OK || pgood_state != 1)
219 return;
220
Lei YU75a18a22016-11-22 01:47:47 +0800221 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800222 {
Lei YU75a18a22016-11-22 01:47:47 +0800223 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800224
Lei YU75a18a22016-11-22 01:47:47 +0800225 if(!g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800226 continue;
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600227 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
228 rc = gpio_open(pci_reset_out, reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800229 if(rc != GPIO_OK)
230 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600231 gpio_close(pci_reset_out);
Yi Li0475f652016-10-25 13:19:59 +0800232 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
233 pci_reset_out->name, rc);
234 continue;
235 }
236
Yi Li39df4032016-12-16 16:06:50 +0800237 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800238 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800239 gpio_write(pci_reset_out, reset_state);
240 gpio_close(pci_reset_out);
Lei YU93b84e42017-10-11 15:06:20 +0800241 g_print("Released pci reset: %s\n", pci_reset_out->name);
Yi Li0475f652016-10-25 13:19:59 +0800242 }
243 g_pci_reset_held = 0;
244}
245
Brad Bishop77390492016-04-13 10:47:19 -0400246static gboolean
247on_set_power_state(ControlPower *pwr,
248 GDBusMethodInvocation *invocation,
249 guint state,
250 gpointer user_data)
251{
252 Control* control = object_get_control((Object*)user_data);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800253 PowerGpio *power_gpio = &g_gpio_configs.power_gpio;
Brad Bishop77390492016-04-13 10:47:19 -0400254 if(state > 1)
255 {
256 g_dbus_method_invocation_return_dbus_error(invocation,
257 "org.openbmc.ControlPower.Error.Failed",
258 "Invalid power state");
259 return TRUE;
260 }
261 // return from method call
262 control_power_complete_set_power_state(pwr,invocation);
263 if(state == control_power_get_state(pwr))
264 {
265 g_print("Power already at requested state: %d\n",state);
266 }
267 else
268 {
269 int error = 0;
270 do {
Xo Wang20a19412016-09-22 11:26:14 -0700271 int i;
272 uint8_t power_up_out;
Xo Wangaa6c76f2017-03-03 14:25:01 -0800273 for (i = 0; i < power_gpio->num_power_up_outs; i++) {
274 GPIO *power_pin = &power_gpio->power_up_outs[i];
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600275 power_up_out = state ^ !power_gpio->power_up_pols[i];
276 error = gpio_open(power_pin, power_up_out);
Xo Wang20a19412016-09-22 11:26:14 -0700277 if(error != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600278 gpio_close(power_pin);
Xo Wang20a19412016-09-22 11:26:14 -0700279 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800280 power_gpio->power_up_outs[i].name, error);
Xo Wang20a19412016-09-22 11:26:14 -0700281 continue;
282 }
Xo Wang20a19412016-09-22 11:26:14 -0700283 g_print("PowerControl: setting power up %s to %d\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800284 power_gpio->power_up_outs[i].name, (int)power_up_out);
Xo Wang20a19412016-09-22 11:26:14 -0700285 error = gpio_write(power_pin, power_up_out);
286 if(error != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600287 gpio_close(power_pin);
Xo Wang20a19412016-09-22 11:26:14 -0700288 continue;
289 }
290 gpio_close(power_pin);
291 }
Brad Bishop77390492016-04-13 10:47:19 -0400292 if(error != GPIO_OK) { break; }
Brad Bishop77390492016-04-13 10:47:19 -0400293 control_power_set_state(pwr,state);
294 } while(0);
295 if(error != GPIO_OK)
296 {
Xo Wang20a19412016-09-22 11:26:14 -0700297 g_print("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
Brad Bishop77390492016-04-13 10:47:19 -0400298 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800299
300 /* If there's a latch, it should be enabled following changes to the
301 * power pins' states. This commits the changes to the latch states. */
302 if (power_gpio->latch_out.name != NULL) {
303 int rc;
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600304 uint8_t latch_value = 1;
305 rc = gpio_open(&power_gpio->latch_out, latch_value);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800306 if (rc != GPIO_OK) {
307 /* Failures are non-fatal. */
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600308 gpio_close(&power_gpio->latch_out);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800309 g_print("PowerControl ERROR failed to open latch %s rc=%d\n",
310 power_gpio->latch_out.name, rc);
311 return TRUE;
312 }
313 /* Make the latch transparent for as brief of a time as possible. */
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600314 rc = gpio_write(&power_gpio->latch_out, latch_value--);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800315 if (rc != GPIO_OK) {
316 g_print("PowerControl ERROR failed to assert latch %s rc=%d\n",
317 power_gpio->latch_out.name, rc);
318 } else {
319 g_print("PowerControl asserted latch %s\n",
320 power_gpio->latch_out.name);
321 }
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600322 rc = gpio_write(&power_gpio->latch_out, latch_value);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800323 if (rc != GPIO_OK) {
324 g_print("PowerControl ERROR failed to clear latch %s rc=%d\n",
325 power_gpio->latch_out.name, rc);
326 }
327 gpio_close(&power_gpio->latch_out);
328 }
Brad Bishop77390492016-04-13 10:47:19 -0400329 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800330
Brad Bishop77390492016-04-13 10:47:19 -0400331 return TRUE;
332}
333
334static gboolean
335on_init(Control *control,
336 GDBusMethodInvocation *invocation,
337 gpointer user_data)
338{
339 pgood_timeout_start = 0;
340 //guint poll_interval = control_get_poll_interval(control);
341 //g_timeout_add(poll_interval, poll_pgood, user_data);
342 control_complete_init(control,invocation);
343 return TRUE;
344}
345
346static gboolean
347on_get_power_state(ControlPower *pwr,
348 GDBusMethodInvocation *invocation,
349 gpointer user_data)
350{
351 guint pgood = control_power_get_pgood(pwr);
352 control_power_complete_get_power_state(pwr,invocation,pgood);
353 return TRUE;
354}
355
Xo Wang20a19412016-09-22 11:26:14 -0700356static int
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500357set_up_gpio(PowerGpio *power_gpio, ControlPower* control_power)
Xo Wang20a19412016-09-22 11:26:14 -0700358{
359 int error = GPIO_OK;
360 int rc;
361 int i;
362 uint8_t pgood_state;
363
364 // get gpio device paths
Xo Wangc1ce68b2016-09-22 16:34:37 -0700365 if(power_gpio->latch_out.name != NULL) { /* latch is optional */
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600366 rc = gpio_get_params(&power_gpio->latch_out);
Xo Wangc1ce68b2016-09-22 16:34:37 -0700367 if(rc != GPIO_OK) {
368 error = rc;
369 }
370 }
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600371 rc = gpio_get_params(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700372 if(rc != GPIO_OK) {
373 error = rc;
374 }
375 for(int i = 0; i < power_gpio->num_power_up_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600376 rc = gpio_get_params(&power_gpio->power_up_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700377 if(rc != GPIO_OK) {
378 error = rc;
379 }
380 }
381 for(int i = 0; i < power_gpio->num_reset_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600382 rc = gpio_get_params(&power_gpio->reset_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700383 if(rc != GPIO_OK) {
384 error = rc;
385 }
386 }
Yi Li0475f652016-10-25 13:19:59 +0800387 for(int i = 0; i < power_gpio->num_pci_reset_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600388 rc = gpio_get_params(&power_gpio->pci_reset_outs[i]);
Yi Li0475f652016-10-25 13:19:59 +0800389 if(rc != GPIO_OK) {
390 error = rc;
391 }
392 }
Xo Wang20a19412016-09-22 11:26:14 -0700393
Matt Spinler0f0946b2018-08-07 16:26:55 -0500394 gpio_inits_done();
395
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600396 rc = gpio_open(&power_gpio->power_good_in, 0);
Xo Wang20a19412016-09-22 11:26:14 -0700397 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600398 gpio_close(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700399 return rc;
400 }
401 rc = gpio_read(&power_gpio->power_good_in, &pgood_state);
402 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600403 gpio_close(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700404 return rc;
405 }
406 gpio_close(&power_gpio->power_good_in);
407 control_power_set_pgood(control_power, pgood_state);
408 control_power_set_state(control_power, pgood_state);
409 g_print("Pgood state: %d\n", pgood_state);
410
411 return error;
412}
413
Brad Bishop77390492016-04-13 10:47:19 -0400414static void
415on_bus_acquired(GDBusConnection *connection,
416 const gchar *name,
417 gpointer user_data)
418{
419 ObjectSkeleton *object;
420 cmdline *cmd = user_data;
421 if(cmd->argc < 3)
422 {
423 g_print("Usage: power_control.exe [poll interval] [timeout]\n");
424 return;
425 }
426 manager = g_dbus_object_manager_server_new(dbus_object_path);
427 gchar *s;
428 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
429 object = object_skeleton_new(s);
430 g_free(s);
431
432 ControlPower* control_power = control_power_skeleton_new();
433 object_skeleton_set_control_power(object, control_power);
434 g_object_unref(control_power);
435
436 Control* control = control_skeleton_new();
437 object_skeleton_set_control(object, control);
438 g_object_unref(control);
439
Brad Bishop77390492016-04-13 10:47:19 -0400440 //define method callbacks here
441 g_signal_connect(control_power,
442 "handle-set-power-state",
443 G_CALLBACK(on_set_power_state),
444 object); /* user_data */
445
446 g_signal_connect(control_power,
447 "handle-get-power-state",
448 G_CALLBACK(on_get_power_state),
449 NULL); /* user_data */
450
451 g_signal_connect(control,
452 "handle-init",
453 G_CALLBACK(on_init),
454 object); /* user_data */
455
Yi Li0475f652016-10-25 13:19:59 +0800456 /* Listen for BootProgress signal from BootProgress sensor */
457 g_dbus_connection_signal_subscribe(connection,
458 NULL, /* service */
Lei YU93b84e42017-10-11 15:06:20 +0800459 "org.freedesktop.DBus.Properties", /* interface_name */
460 "PropertiesChanged", /* member: name of the signal */
461 "/xyz/openbmc_project/state/host0", /* obj path */
Yi Li0475f652016-10-25 13:19:59 +0800462 NULL, /* arg0 */
463 G_DBUS_SIGNAL_FLAGS_NONE,
464 (GDBusSignalCallback) on_boot_progress,
465 object, /* user data */
466 NULL );
Brad Bishop77390492016-04-13 10:47:19 -0400467
468 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400469 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400470 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
471 g_object_unref(object);
472
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500473 if(read_gpios(&g_gpio_configs) != TRUE) {
Xo Wang20a19412016-09-22 11:26:14 -0700474 g_print("ERROR PowerControl: could not read power GPIO configuration\n");
Yong Li1acfd752018-04-04 13:01:23 +0800475 exit(-1);
Xo Wang20a19412016-09-22 11:26:14 -0700476 }
Brad Bishop77390492016-04-13 10:47:19 -0400477
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500478 int rc = set_up_gpio(&g_gpio_configs.power_gpio, control_power);
Xo Wang20a19412016-09-22 11:26:14 -0700479 if(rc != GPIO_OK) {
480 g_print("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
Yong Li1acfd752018-04-04 13:01:23 +0800481 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400482 }
483 //start poll
484 pgood_timeout_start = 0;
485 int poll_interval = atoi(cmd->argv[1]);
486 int pgood_timeout = atoi(cmd->argv[2]);
Matt Spinler89e6c912017-12-14 09:55:13 -0600487 if(poll_interval < 500 || pgood_timeout <5) {
488 g_print("ERROR PowerControl: poll_interval < 500 or pgood_timeout < 5\n");
Yong Li1acfd752018-04-04 13:01:23 +0800489 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400490 } else {
491 control_set_poll_interval(control,poll_interval);
492 control_power_set_pgood_timeout(control_power,pgood_timeout);
493 g_timeout_add(poll_interval, poll_pgood, object);
494 }
Brad Bishop77390492016-04-13 10:47:19 -0400495}
496
497static void
498on_name_acquired(GDBusConnection *connection,
499 const gchar *name,
500 gpointer user_data)
501{
502}
503
504static void
505on_name_lost(GDBusConnection *connection,
506 const gchar *name,
507 gpointer user_data)
508{
Lei YU75a18a22016-11-22 01:47:47 +0800509 free_gpios(&g_gpio_configs);
Brad Bishop77390492016-04-13 10:47:19 -0400510}
511
512/*----------------------------------------------------------------*/
513/* Main Event Loop */
514
515gint
516main(gint argc, gchar *argv[])
517{
518 GMainLoop *loop;
519 cmdline cmd;
520 cmd.argc = argc;
521 cmd.argv = argv;
522
523 guint id;
524 loop = g_main_loop_new(NULL, FALSE);
525
526 id = g_bus_own_name(DBUS_TYPE,
527 dbus_name,
528 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
529 G_BUS_NAME_OWNER_FLAGS_REPLACE,
530 on_bus_acquired,
531 on_name_acquired,
532 on_name_lost,
533 &cmd,
534 NULL);
535
536 g_main_loop_run(loop);
537
538 g_bus_unown_name(id);
539 g_main_loop_unref(loop);
540 return 0;
541}