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