blob: dedcbe204d36fded0d8afa07e4b9827edf7f3af6 [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
Lei YU75a18a22016-11-22 01:47:47 +080055 int rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -070056 if(rc != GPIO_OK) {
57 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +080058 g_gpio_configs.power_gpio.power_good_in.name, rc);
Xo Wang20a19412016-09-22 11:26:14 -070059 return FALSE;
60 }
Lei YU75a18a22016-11-22 01:47:47 +080061 rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state);
62 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Brad Bishop77390492016-04-13 10:47:19 -040063 if(rc == GPIO_OK)
64 {
65 //if changed, set property and emit signal
Xo Wang20a19412016-09-22 11:26:14 -070066 if(pgood_state != control_power_get_pgood(control_power))
Brad Bishop77390492016-04-13 10:47:19 -040067 {
Xo Wang20a19412016-09-22 11:26:14 -070068 int i;
69 uint8_t reset_state;
70 control_power_set_pgood(control_power, pgood_state);
71 if(pgood_state == 0)
Brad Bishop77390492016-04-13 10:47:19 -040072 {
73 control_power_emit_power_lost(control_power);
Yi Li0475f652016-10-25 13:19:59 +080074 g_pci_reset_held = 1;
Brad Bishop77390492016-04-13 10:47:19 -040075 }
76 else
77 {
78 control_power_emit_power_good(control_power);
Xo Wang20a19412016-09-22 11:26:14 -070079 }
Brad Bishop77390492016-04-13 10:47:19 -040080
Lei YU75a18a22016-11-22 01:47:47 +080081 for(i = 0; i < g_gpio_configs.power_gpio.num_reset_outs; i++)
Xo Wang20a19412016-09-22 11:26:14 -070082 {
Lei YU75a18a22016-11-22 01:47:47 +080083 GPIO *reset_out = &g_gpio_configs.power_gpio.reset_outs[i];
Xo Wang20a19412016-09-22 11:26:14 -070084 rc = gpio_open(reset_out);
85 if(rc != GPIO_OK)
86 {
87 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
88 reset_out->name, rc);
89 continue;
90 }
91
Lei YU75a18a22016-11-22 01:47:47 +080092 reset_state = pgood_state ^ g_gpio_configs.power_gpio.reset_pols[i];
Yi Li39df4032016-12-16 16:06:50 +080093 g_print("PowerControl: pgood: %d, setting reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +080094 (int)pgood_state, reset_out->name, (int)reset_state);
Xo Wang20a19412016-09-22 11:26:14 -070095 gpio_write(reset_out, reset_state);
96 gpio_close(reset_out);
Brad Bishop77390492016-04-13 10:47:19 -040097 }
Yi Li0475f652016-10-25 13:19:59 +080098
Lei YU75a18a22016-11-22 01:47:47 +080099 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800100 {
Lei YU75a18a22016-11-22 01:47:47 +0800101 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800102 if(pgood_state == 1)
103 {
104 /*
105 * When powering on, hold PCI reset until
106 * the processor can forward clocks and control reset.
107 */
Lei YU75a18a22016-11-22 01:47:47 +0800108 if(g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800109 {
110 g_print("Holding pci reset: %s\n", pci_reset_out->name);
111 continue;
112 }
113 }
114 rc = gpio_open(pci_reset_out);
115 if(rc != GPIO_OK)
116 {
117 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
118 pci_reset_out->name, rc);
119 continue;
120 }
121
Lei YU75a18a22016-11-22 01:47:47 +0800122 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
Yi Li39df4032016-12-16 16:06:50 +0800123 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800124 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800125 gpio_write(pci_reset_out, reset_state);
126 gpio_close(pci_reset_out);
127 }
Brad Bishop77390492016-04-13 10:47:19 -0400128 }
129 } else {
Xo Wang20a19412016-09-22 11:26:14 -0700130 g_print("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800131 g_gpio_configs.power_gpio.power_good_in.name, rc);
Brad Bishop77390492016-04-13 10:47:19 -0400132 //return false so poll won't get called anymore
133 return FALSE;
134 }
135 //pgood is not at desired state yet
Xo Wang20a19412016-09-22 11:26:14 -0700136 if(pgood_state != control_power_get_state(control_power) &&
Brad Bishop77390492016-04-13 10:47:19 -0400137 control_power_get_pgood_timeout(control_power) > 0)
138 {
139 if(pgood_timeout_start == 0 ) {
140 pgood_timeout_start = current_time;
141 }
142 }
143 else
144 {
145 pgood_timeout_start = 0;
146 }
147 return TRUE;
148}
149
Yi Li0475f652016-10-25 13:19:59 +0800150/* Handler for BootProgress signal from BootProgress sensor */
151static void
152on_boot_progress(GDBusConnection *connection,
153 const gchar *sender_name,
154 const gchar *object_path,
155 const gchar *interface_name,
156 const gchar *signal_name,
157 GVariant *parameters,
158 gpointer user_data)
159{
Lei YU93b84e42017-10-11 15:06:20 +0800160 gchar *interface;
161 GVariantIter *properties;
162 GVariantIter *dummy;
163 gchar *boot_progress = NULL;
164 gchar *property;
165 GVariant *value;
Yi Li0475f652016-10-25 13:19:59 +0800166 uint8_t pgood_state;
167 uint8_t reset_state;
168 int rc;
169 int i;
Lei YU93b84e42017-10-11 15:06:20 +0800170 int ignore;
Yi Li0475f652016-10-25 13:19:59 +0800171
172 if(!parameters)
173 return;
174
175 /* prevent release again */
176 if(!g_pci_reset_held)
177 return;
178
Lei YU93b84e42017-10-11 15:06:20 +0800179 g_variant_get(parameters, "(&sa{sv}as)", &interface, &properties, &dummy);
180 for(i = 0; g_variant_iter_next(properties, "{&sv}", &property, &value); i++)
181 {
182 if (strcmp(property, "BootProgress") == 0)
183 {
184 gchar* tmp;
185 g_variant_get(value, "&s", &tmp);
186 boot_progress = g_strdup(tmp);
187 g_print("BootProgress: %s\n", boot_progress);
188 g_variant_unref(value);
189 }
190 }
191
192 g_variant_iter_free(properties);
193 g_variant_iter_free(dummy);
194 if (boot_progress == NULL)
195 return;
196
Yi Li0475f652016-10-25 13:19:59 +0800197 /* Release PCI reset when FW boot progress goes beyond 'Baseboard Init' */
Lei YU93b84e42017-10-11 15:06:20 +0800198 ignore = strcmp(boot_progress,
199 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit") == 0;
200 g_free(boot_progress);
201
202 if (ignore)
Yi Li0475f652016-10-25 13:19:59 +0800203 return;
204
Lei YU75a18a22016-11-22 01:47:47 +0800205 rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in);
Yi Li0475f652016-10-25 13:19:59 +0800206 if(rc != GPIO_OK)
207 {
208 g_print("ERROR PowerControl: on_boot_progress(): GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800209 g_gpio_configs.power_gpio.power_good_in.name, rc);
Yi Li0475f652016-10-25 13:19:59 +0800210 return;
211 }
Lei YU75a18a22016-11-22 01:47:47 +0800212 rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state);
213 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Yi Li0475f652016-10-25 13:19:59 +0800214 if(rc != GPIO_OK || pgood_state != 1)
215 return;
216
Lei YU75a18a22016-11-22 01:47:47 +0800217 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800218 {
Lei YU75a18a22016-11-22 01:47:47 +0800219 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800220
Lei YU75a18a22016-11-22 01:47:47 +0800221 if(!g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800222 continue;
223 rc = gpio_open(pci_reset_out);
224 if(rc != GPIO_OK)
225 {
226 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
227 pci_reset_out->name, rc);
228 continue;
229 }
230
Lei YU75a18a22016-11-22 01:47:47 +0800231 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
Yi Li39df4032016-12-16 16:06:50 +0800232 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800233 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800234 gpio_write(pci_reset_out, reset_state);
235 gpio_close(pci_reset_out);
Lei YU93b84e42017-10-11 15:06:20 +0800236 g_print("Released pci reset: %s\n", pci_reset_out->name);
Yi Li0475f652016-10-25 13:19:59 +0800237 }
238 g_pci_reset_held = 0;
239}
240
Brad Bishop77390492016-04-13 10:47:19 -0400241static gboolean
242on_set_power_state(ControlPower *pwr,
243 GDBusMethodInvocation *invocation,
244 guint state,
245 gpointer user_data)
246{
247 Control* control = object_get_control((Object*)user_data);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800248 PowerGpio *power_gpio = &g_gpio_configs.power_gpio;
Brad Bishop77390492016-04-13 10:47:19 -0400249 if(state > 1)
250 {
251 g_dbus_method_invocation_return_dbus_error(invocation,
252 "org.openbmc.ControlPower.Error.Failed",
253 "Invalid power state");
254 return TRUE;
255 }
256 // return from method call
257 control_power_complete_set_power_state(pwr,invocation);
258 if(state == control_power_get_state(pwr))
259 {
260 g_print("Power already at requested state: %d\n",state);
261 }
262 else
263 {
264 int error = 0;
265 do {
Xo Wang20a19412016-09-22 11:26:14 -0700266 int i;
267 uint8_t power_up_out;
Xo Wangaa6c76f2017-03-03 14:25:01 -0800268 for (i = 0; i < power_gpio->num_power_up_outs; i++) {
269 GPIO *power_pin = &power_gpio->power_up_outs[i];
Xo Wang20a19412016-09-22 11:26:14 -0700270 error = gpio_open(power_pin);
271 if(error != GPIO_OK) {
272 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800273 power_gpio->power_up_outs[i].name, error);
Xo Wang20a19412016-09-22 11:26:14 -0700274 continue;
275 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800276 power_up_out = state ^ !power_gpio->power_up_pols[i];
Xo Wang20a19412016-09-22 11:26:14 -0700277 g_print("PowerControl: setting power up %s to %d\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800278 power_gpio->power_up_outs[i].name, (int)power_up_out);
Xo Wang20a19412016-09-22 11:26:14 -0700279 error = gpio_write(power_pin, power_up_out);
280 if(error != GPIO_OK) {
281 continue;
282 }
283 gpio_close(power_pin);
284 }
Brad Bishop77390492016-04-13 10:47:19 -0400285 if(error != GPIO_OK) { break; }
Brad Bishop77390492016-04-13 10:47:19 -0400286 control_power_set_state(pwr,state);
287 } while(0);
288 if(error != GPIO_OK)
289 {
Xo Wang20a19412016-09-22 11:26:14 -0700290 g_print("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
Brad Bishop77390492016-04-13 10:47:19 -0400291 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800292
293 /* If there's a latch, it should be enabled following changes to the
294 * power pins' states. This commits the changes to the latch states. */
295 if (power_gpio->latch_out.name != NULL) {
296 int rc;
297 uint8_t latch_value = 0;
298 rc = gpio_open(&power_gpio->latch_out);
299 if (rc != GPIO_OK) {
300 /* Failures are non-fatal. */
301 g_print("PowerControl ERROR failed to open latch %s rc=%d\n",
302 power_gpio->latch_out.name, rc);
303 return TRUE;
304 }
305 /* Make the latch transparent for as brief of a time as possible. */
306 rc = gpio_write(&power_gpio->latch_out, 1);
307 if (rc != GPIO_OK) {
308 g_print("PowerControl ERROR failed to assert latch %s rc=%d\n",
309 power_gpio->latch_out.name, rc);
310 } else {
311 g_print("PowerControl asserted latch %s\n",
312 power_gpio->latch_out.name);
313 }
314 rc = gpio_write(&power_gpio->latch_out, 0);
315 if (rc != GPIO_OK) {
316 g_print("PowerControl ERROR failed to clear latch %s rc=%d\n",
317 power_gpio->latch_out.name, rc);
318 }
319 gpio_close(&power_gpio->latch_out);
320 }
Brad Bishop77390492016-04-13 10:47:19 -0400321 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800322
Brad Bishop77390492016-04-13 10:47:19 -0400323 return TRUE;
324}
325
326static gboolean
327on_init(Control *control,
328 GDBusMethodInvocation *invocation,
329 gpointer user_data)
330{
331 pgood_timeout_start = 0;
332 //guint poll_interval = control_get_poll_interval(control);
333 //g_timeout_add(poll_interval, poll_pgood, user_data);
334 control_complete_init(control,invocation);
335 return TRUE;
336}
337
338static gboolean
339on_get_power_state(ControlPower *pwr,
340 GDBusMethodInvocation *invocation,
341 gpointer user_data)
342{
343 guint pgood = control_power_get_pgood(pwr);
344 control_power_complete_get_power_state(pwr,invocation,pgood);
345 return TRUE;
346}
347
Xo Wang20a19412016-09-22 11:26:14 -0700348static int
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500349set_up_gpio(PowerGpio *power_gpio, ControlPower* control_power)
Xo Wang20a19412016-09-22 11:26:14 -0700350{
351 int error = GPIO_OK;
352 int rc;
353 int i;
354 uint8_t pgood_state;
355
356 // get gpio device paths
Xo Wangc1ce68b2016-09-22 16:34:37 -0700357 if(power_gpio->latch_out.name != NULL) { /* latch is optional */
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500358 rc = gpio_init(&power_gpio->latch_out);
Xo Wangc1ce68b2016-09-22 16:34:37 -0700359 if(rc != GPIO_OK) {
360 error = rc;
361 }
362 }
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500363 rc = gpio_init(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700364 if(rc != GPIO_OK) {
365 error = rc;
366 }
367 for(int i = 0; i < power_gpio->num_power_up_outs; i++) {
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500368 rc = gpio_init(&power_gpio->power_up_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700369 if(rc != GPIO_OK) {
370 error = rc;
371 }
372 }
373 for(int i = 0; i < power_gpio->num_reset_outs; i++) {
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500374 rc = gpio_init(&power_gpio->reset_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700375 if(rc != GPIO_OK) {
376 error = rc;
377 }
378 }
Yi Li0475f652016-10-25 13:19:59 +0800379 for(int i = 0; i < power_gpio->num_pci_reset_outs; i++) {
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500380 rc = gpio_init(&power_gpio->pci_reset_outs[i]);
Yi Li0475f652016-10-25 13:19:59 +0800381 if(rc != GPIO_OK) {
382 error = rc;
383 }
384 }
Xo Wang20a19412016-09-22 11:26:14 -0700385
Matt Spinler0f0946b2018-08-07 16:26:55 -0500386 gpio_inits_done();
387
Xo Wang20a19412016-09-22 11:26:14 -0700388 rc = gpio_open(&power_gpio->power_good_in);
389 if(rc != GPIO_OK) {
390 return rc;
391 }
392 rc = gpio_read(&power_gpio->power_good_in, &pgood_state);
393 if(rc != GPIO_OK) {
394 return rc;
395 }
396 gpio_close(&power_gpio->power_good_in);
397 control_power_set_pgood(control_power, pgood_state);
398 control_power_set_state(control_power, pgood_state);
399 g_print("Pgood state: %d\n", pgood_state);
400
401 return error;
402}
403
Brad Bishop77390492016-04-13 10:47:19 -0400404static void
405on_bus_acquired(GDBusConnection *connection,
406 const gchar *name,
407 gpointer user_data)
408{
409 ObjectSkeleton *object;
410 cmdline *cmd = user_data;
411 if(cmd->argc < 3)
412 {
413 g_print("Usage: power_control.exe [poll interval] [timeout]\n");
414 return;
415 }
416 manager = g_dbus_object_manager_server_new(dbus_object_path);
417 gchar *s;
418 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
419 object = object_skeleton_new(s);
420 g_free(s);
421
422 ControlPower* control_power = control_power_skeleton_new();
423 object_skeleton_set_control_power(object, control_power);
424 g_object_unref(control_power);
425
426 Control* control = control_skeleton_new();
427 object_skeleton_set_control(object, control);
428 g_object_unref(control);
429
Brad Bishop77390492016-04-13 10:47:19 -0400430 //define method callbacks here
431 g_signal_connect(control_power,
432 "handle-set-power-state",
433 G_CALLBACK(on_set_power_state),
434 object); /* user_data */
435
436 g_signal_connect(control_power,
437 "handle-get-power-state",
438 G_CALLBACK(on_get_power_state),
439 NULL); /* user_data */
440
441 g_signal_connect(control,
442 "handle-init",
443 G_CALLBACK(on_init),
444 object); /* user_data */
445
Yi Li0475f652016-10-25 13:19:59 +0800446 /* Listen for BootProgress signal from BootProgress sensor */
447 g_dbus_connection_signal_subscribe(connection,
448 NULL, /* service */
Lei YU93b84e42017-10-11 15:06:20 +0800449 "org.freedesktop.DBus.Properties", /* interface_name */
450 "PropertiesChanged", /* member: name of the signal */
451 "/xyz/openbmc_project/state/host0", /* obj path */
Yi Li0475f652016-10-25 13:19:59 +0800452 NULL, /* arg0 */
453 G_DBUS_SIGNAL_FLAGS_NONE,
454 (GDBusSignalCallback) on_boot_progress,
455 object, /* user data */
456 NULL );
Brad Bishop77390492016-04-13 10:47:19 -0400457
458 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400459 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400460 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
461 g_object_unref(object);
462
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500463 if(read_gpios(&g_gpio_configs) != TRUE) {
Xo Wang20a19412016-09-22 11:26:14 -0700464 g_print("ERROR PowerControl: could not read power GPIO configuration\n");
Yong Li1acfd752018-04-04 13:01:23 +0800465 exit(-1);
Xo Wang20a19412016-09-22 11:26:14 -0700466 }
Brad Bishop77390492016-04-13 10:47:19 -0400467
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500468 int rc = set_up_gpio(&g_gpio_configs.power_gpio, control_power);
Xo Wang20a19412016-09-22 11:26:14 -0700469 if(rc != GPIO_OK) {
470 g_print("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
Yong Li1acfd752018-04-04 13:01:23 +0800471 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400472 }
473 //start poll
474 pgood_timeout_start = 0;
475 int poll_interval = atoi(cmd->argv[1]);
476 int pgood_timeout = atoi(cmd->argv[2]);
Matt Spinler89e6c912017-12-14 09:55:13 -0600477 if(poll_interval < 500 || pgood_timeout <5) {
478 g_print("ERROR PowerControl: poll_interval < 500 or pgood_timeout < 5\n");
Yong Li1acfd752018-04-04 13:01:23 +0800479 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400480 } else {
481 control_set_poll_interval(control,poll_interval);
482 control_power_set_pgood_timeout(control_power,pgood_timeout);
483 g_timeout_add(poll_interval, poll_pgood, object);
484 }
Brad Bishop77390492016-04-13 10:47:19 -0400485}
486
487static void
488on_name_acquired(GDBusConnection *connection,
489 const gchar *name,
490 gpointer user_data)
491{
492}
493
494static void
495on_name_lost(GDBusConnection *connection,
496 const gchar *name,
497 gpointer user_data)
498{
Lei YU75a18a22016-11-22 01:47:47 +0800499 free_gpios(&g_gpio_configs);
Brad Bishop77390492016-04-13 10:47:19 -0400500}
501
502/*----------------------------------------------------------------*/
503/* Main Event Loop */
504
505gint
506main(gint argc, gchar *argv[])
507{
508 GMainLoop *loop;
509 cmdline cmd;
510 cmd.argc = argc;
511 cmd.argv = argv;
512
513 guint id;
514 loop = g_main_loop_new(NULL, FALSE);
515
516 id = g_bus_own_name(DBUS_TYPE,
517 dbus_name,
518 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
519 G_BUS_NAME_OWNER_FLAGS_REPLACE,
520 on_bus_acquired,
521 on_name_acquired,
522 on_name_lost,
523 &cmd,
524 NULL);
525
526 g_main_loop_run(loop);
527
528 g_bus_unown_name(id);
529 g_main_loop_unref(loop);
530 return 0;
531}