Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 1 | #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>
|
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 9 | #include <syslog.h>
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 10 | #include "interfaces/openbmc_intf.h"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 11 | #include "openbmc.h"
|
| 12 | #include "gpio.h"
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 13 |
|
| 14 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 15 | static const gchar* dbus_object_path = "/org/openbmc/control";
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 16 | static const gchar* dbus_name = "org.openbmc.control.Power";
|
| 17 |
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 18 | //This object will use these GPIOs
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 19 | GPIO power_pin = (GPIO){ "POWER_PIN" };
|
| 20 | GPIO pgood = (GPIO){ "PGOOD" };
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 21 |
|
| 22 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 23 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 24 | time_t pgood_timeout_start = 0;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 25 |
|
Norman James | 3d3b7fb | 2015-10-06 16:54:06 -0500 | [diff] [blame] | 26 | // TODO: Change to interrupt driven instead of polling
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 27 | static gboolean poll_pgood(gpointer user_data)
|
| 28 | {
|
| 29 | ControlPower *control_power = object_get_control_power((Object*)user_data);
|
| 30 | Control* control = object_get_control((Object*)user_data);
|
Norman James | bd4abd1 | 2015-09-16 22:43:46 -0500 | [diff] [blame] | 31 |
|
| 32 | //send the heartbeat
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 33 | control_emit_heartbeat(control,dbus_name);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 34 | const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 35 |
|
Norman James | 6a1283c | 2015-09-16 22:21:11 -0500 | [diff] [blame] | 36 | guint poll_int = control_get_poll_interval(control);
|
| 37 | if (poll_int == 0)
|
| 38 | {
|
Norman James | 2891c53 | 2015-10-06 08:01:56 -0500 | [diff] [blame] | 39 | printf("ERROR PowerControl: Poll interval cannot be 0\n");
|
Norman James | 6a1283c | 2015-09-16 22:21:11 -0500 | [diff] [blame] | 40 | return FALSE;
|
| 41 | }
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 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)
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 46 | {
|
Norman James | 2891c53 | 2015-10-06 08:01:56 -0500 | [diff] [blame] | 47 | printf("ERROR PowerControl: Pgood poll timeout\n");
|
Norman James | bd4abd1 | 2015-09-16 22:43:46 -0500 | [diff] [blame] | 48 | // set timeout to 0 so timeout doesn't happen again
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 49 | control_power_set_pgood_timeout(control_power,0);
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 50 | pgood_timeout_start = 0;
|
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 51 | return TRUE;
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 52 | }
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 53 | uint8_t gpio;
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 54 |
|
| 55 | int rc = gpio_open(&pgood);
|
| 56 | rc = gpio_read(&pgood,&gpio);
|
| 57 | gpio_close(&pgood);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 58 | if (rc == GPIO_OK)
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 59 | {
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 60 | //if changed, set property and emit signal
|
| 61 | if (gpio != control_power_get_pgood(control_power))
|
| 62 | {
|
| 63 | control_power_set_pgood(control_power,gpio);
|
| 64 | if (gpio==0)
|
| 65 | {
|
| 66 | control_power_emit_power_lost(control_power);
|
| 67 | control_emit_goto_system_state(control,"POWERED_OFF");
|
| 68 | }
|
| 69 | else
|
| 70 | {
|
| 71 | control_power_emit_power_good(control_power);
|
| 72 | control_emit_goto_system_state(control,"POWERED_ON");
|
| 73 | }
|
| 74 | }
|
| 75 | } else {
|
Norman James | 2891c53 | 2015-10-06 08:01:56 -0500 | [diff] [blame] | 76 | printf("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",pgood.name,rc);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 77 | }
|
| 78 | //pgood is not at desired state yet
|
| 79 | if (gpio != control_power_get_state(control_power) &&
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 80 | control_power_get_pgood_timeout(control_power) > 0)
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 81 | {
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 82 | if (pgood_timeout_start == 0 ) {
|
| 83 | pgood_timeout_start = current_time;
|
| 84 | }
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 85 | }
|
| 86 | else
|
| 87 | {
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 88 | pgood_timeout_start = 0;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 89 | }
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 90 | return TRUE;
|
| 91 | }
|
| 92 |
|
| 93 |
|
| 94 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 95 | static gboolean
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 96 | on_set_power_state (ControlPower *pwr,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 97 | GDBusMethodInvocation *invocation,
|
| 98 | guint state,
|
| 99 | gpointer user_data)
|
| 100 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 101 | Control* control = object_get_control((Object*)user_data);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 102 | const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 103 | if (state > 1)
|
| 104 | {
|
| 105 | g_dbus_method_invocation_return_dbus_error (invocation,
|
| 106 | "org.openbmc.ControlPower.Error.Failed",
|
| 107 | "Invalid power state");
|
| 108 | return TRUE;
|
| 109 | }
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 110 | // return from method call
|
| 111 | control_power_complete_set_power_state(pwr,invocation);
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 112 | if (state == control_power_get_state(pwr))
|
| 113 | {
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 114 | g_print("Power already at requested state: %d\n",state);
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 115 | }
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 116 | else
|
| 117 | {
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 118 | int error = 0;
|
| 119 | do {
|
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 120 | if (state == 1) {
|
| 121 | control_emit_goto_system_state(control,"POWERING_ON");
|
| 122 | } else {
|
| 123 | control_emit_goto_system_state(control,"POWERING_OFF");
|
| 124 | }
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 125 | error = gpio_open(&power_pin);
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 126 | if (error != GPIO_OK) { break; }
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 127 | error = gpio_write(&power_pin,!state);
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 128 | if (error != GPIO_OK) { break; }
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 129 | gpio_close(&power_pin);
|
| 130 | control_power_set_state(pwr,state);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 131 | } while(0);
|
| 132 | if (error != GPIO_OK)
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 133 | {
|
Norman James | 2891c53 | 2015-10-06 08:01:56 -0500 | [diff] [blame] | 134 | printf("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 135 | }
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 136 | }
|
| 137 | return TRUE;
|
| 138 | }
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 139 |
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 140 | static gboolean
|
| 141 | on_init (Control *control,
|
| 142 | GDBusMethodInvocation *invocation,
|
| 143 | gpointer user_data)
|
| 144 | {
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 145 | pgood_timeout_start = 0;
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 146 | guint poll_interval = control_get_poll_interval(control);
|
| 147 | g_timeout_add(poll_interval, poll_pgood, user_data);
|
| 148 | control_complete_init(control,invocation);
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 149 | return TRUE;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 150 | }
|
| 151 |
|
| 152 | static gboolean
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 153 | on_get_power_state (ControlPower *pwr,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 154 | GDBusMethodInvocation *invocation,
|
| 155 | gpointer user_data)
|
| 156 | {
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 157 | guint pgood = control_power_get_pgood(pwr);
|
| 158 | control_power_complete_get_power_state(pwr,invocation,pgood);
|
| 159 | return TRUE;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 160 | }
|
| 161 |
|
| 162 | static void
|
| 163 | on_bus_acquired (GDBusConnection *connection,
|
| 164 | const gchar *name,
|
| 165 | gpointer user_data)
|
| 166 | {
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 167 | ObjectSkeleton *object;
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 168 | cmdline *cmd = user_data;
|
| 169 | if (cmd->argc < 2)
|
| 170 | {
|
| 171 | g_print("No objects created. Put object name(s) on command line\n");
|
| 172 | return;
|
| 173 | }
|
| 174 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 175 | gchar *s;
|
| 176 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[1]);
|
| 177 | g_print("%s\n",s);
|
| 178 | object = object_skeleton_new (s);
|
| 179 | g_free (s);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 180 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 181 | ControlPower* control_power = control_power_skeleton_new ();
|
| 182 | object_skeleton_set_control_power (object, control_power);
|
| 183 | g_object_unref (control_power);
|
| 184 |
|
| 185 | Control* control = control_skeleton_new ();
|
| 186 | object_skeleton_set_control (object, control);
|
| 187 | g_object_unref (control);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 188 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 189 | //define method callbacks here
|
| 190 | g_signal_connect (control_power,
|
| 191 | "handle-set-power-state",
|
| 192 | G_CALLBACK (on_set_power_state),
|
| 193 | object); /* user_data */
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 194 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 195 | g_signal_connect (control_power,
|
| 196 | "handle-get-power-state",
|
| 197 | G_CALLBACK (on_get_power_state),
|
| 198 | NULL); /* user_data */
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 199 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 200 | g_signal_connect (control,
|
| 201 | "handle-init",
|
| 202 | G_CALLBACK (on_init),
|
| 203 | object); /* user_data */
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 204 |
|
| 205 |
|
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 206 | /* Export the object (@manager takes its own reference to @object) */
|
| 207 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 208 | g_object_unref (object);
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 209 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 210 | /* Export all objects */
|
| 211 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 212 |
|
| 213 | // get gpio device paths
|
Norman James | bd4abd1 | 2015-09-16 22:43:46 -0500 | [diff] [blame] | 214 | int rc = GPIO_OK;
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 215 | do {
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 216 | rc = gpio_init(connection,&power_pin);
|
| 217 | if (rc != GPIO_OK) { break; }
|
| 218 | rc = gpio_init(connection,&pgood);
|
| 219 | if (rc != GPIO_OK) { break; }
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 220 | uint8_t gpio;
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 221 | rc = gpio_open(&pgood);
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 222 | if (rc != GPIO_OK) { break; }
|
| 223 | rc = gpio_read(&pgood,&gpio);
|
| 224 | if (rc != GPIO_OK) { break; }
|
| 225 | gpio_close(&pgood);
|
| 226 | control_power_set_pgood(control_power,gpio);
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 227 | } while(0);
|
Norman James | bd4abd1 | 2015-09-16 22:43:46 -0500 | [diff] [blame] | 228 | if (rc != GPIO_OK)
|
| 229 | {
|
Norman James | 2891c53 | 2015-10-06 08:01:56 -0500 | [diff] [blame] | 230 | printf("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 231 | }
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 232 | }
|
| 233 |
|
| 234 | static void
|
| 235 | on_name_acquired (GDBusConnection *connection,
|
| 236 | const gchar *name,
|
| 237 | gpointer user_data)
|
| 238 | {
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 239 | }
|
| 240 |
|
| 241 | static void
|
| 242 | on_name_lost (GDBusConnection *connection,
|
| 243 | const gchar *name,
|
| 244 | gpointer user_data)
|
| 245 | {
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 246 | }
|
| 247 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 248 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 249 |
|
| 250 |
|
| 251 | /*----------------------------------------------------------------*/
|
| 252 | /* Main Event Loop */
|
| 253 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 254 | gint
|
| 255 | main (gint argc, gchar *argv[])
|
| 256 | {
|
| 257 | GMainLoop *loop;
|
Norman James | 90caa3c | 2015-08-27 21:28:48 -0500 | [diff] [blame] | 258 | cmdline cmd;
|
| 259 | cmd.argc = argc;
|
| 260 | cmd.argv = argv;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 261 |
|
| 262 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 263 | loop = g_main_loop_new (NULL, FALSE);
|
| 264 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 265 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 266 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 267 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 268 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 269 | on_bus_acquired,
|
| 270 | on_name_acquired,
|
| 271 | on_name_lost,
|
Norman James | 90caa3c | 2015-08-27 21:28:48 -0500 | [diff] [blame] | 272 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 273 | NULL);
|
| 274 |
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 275 | g_main_loop_run (loop);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 276 |
|
| 277 | g_bus_unown_name (id);
|
| 278 | g_main_loop_unref (loop);
|
| 279 | return 0;
|
| 280 | }
|