Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame^] | 1 | #include "interfaces/power_control.h"
|
| 2 |
|
| 3 | /* ---------------------------------------------------------------------------------------------------- */
|
| 4 |
|
| 5 | static GDBusObjectManagerServer *manager = NULL;
|
| 6 | static PowerControl *power_control = NULL;
|
| 7 |
|
| 8 | static guint gpio_power = 26;
|
| 9 | static guint gpio_fsiclk = 27;
|
| 10 | static guint gpio_fsidat = 28;
|
| 11 | static guint gpio_pgood = 21;
|
| 12 |
|
| 13 | static guint tmp_pgood = 0;
|
| 14 |
|
| 15 | static gboolean
|
| 16 | on_set_power_state (PowerControl *pwr,
|
| 17 | GDBusMethodInvocation *invocation,
|
| 18 | guint state,
|
| 19 | gpointer user_data)
|
| 20 | {
|
| 21 | if (state > 1)
|
| 22 | {
|
| 23 | g_dbus_method_invocation_return_dbus_error (invocation,
|
| 24 | "org.openbmc.PowerControl.Error.Failed",
|
| 25 | "Invalid power state");
|
| 26 | return TRUE;
|
| 27 | }
|
| 28 | if (state == power_control_get_state(pwr))
|
| 29 | {
|
| 30 | g_dbus_method_invocation_return_dbus_error (invocation,
|
| 31 | "org.openbmc.PowerControl.Error.Failed",
|
| 32 | "Power Control is already at requested state");
|
| 33 | return TRUE;
|
| 34 | }
|
| 35 |
|
| 36 | // TODO: Implement gpio toggling
|
| 37 | g_print("Set power state: %d\n",state);
|
| 38 | if (state==1)
|
| 39 | {
|
| 40 | g_print("Turn on\n");
|
| 41 | tmp_pgood=1;
|
| 42 | }
|
| 43 | else
|
| 44 | {
|
| 45 | g_print("Turn off\n");
|
| 46 | tmp_pgood=0;
|
| 47 | }
|
| 48 | power_control_set_state(pwr,state);
|
| 49 | power_control_complete_set_power_state(pwr,invocation);
|
| 50 | return TRUE;
|
| 51 | }
|
| 52 |
|
| 53 | static gboolean
|
| 54 | on_get_power_state (PowerControl *pwr,
|
| 55 | GDBusMethodInvocation *invocation,
|
| 56 | gpointer user_data)
|
| 57 | {
|
| 58 | guint pgood = power_control_get_pgood(pwr);
|
| 59 | power_control_complete_get_power_state(pwr,invocation,pgood);
|
| 60 | return TRUE;
|
| 61 | }
|
| 62 |
|
| 63 | static void
|
| 64 | on_bus_acquired (GDBusConnection *connection,
|
| 65 | const gchar *name,
|
| 66 | gpointer user_data)
|
| 67 | {
|
| 68 | ObjectSkeleton *object;
|
| 69 | guint n;
|
| 70 |
|
| 71 | g_print ("Acquired a message bus connection: %s\n",name);
|
| 72 |
|
| 73 | manager = g_dbus_object_manager_server_new ("/org/openbmc/PowerControl");
|
| 74 |
|
| 75 | gchar *s;
|
| 76 | s = g_strdup_printf ("/org/openbmc/PowerControl/0");
|
| 77 | object = object_skeleton_new (s);
|
| 78 | g_free (s);
|
| 79 |
|
| 80 | power_control = power_control_skeleton_new ();
|
| 81 | object_skeleton_set_power_control (object, power_control);
|
| 82 | g_object_unref (power_control);
|
| 83 |
|
| 84 | //define method callbacks here
|
| 85 | g_signal_connect (power_control,
|
| 86 | "handle-set-power-state",
|
| 87 | G_CALLBACK (on_set_power_state),
|
| 88 | NULL); /* user_data */
|
| 89 |
|
| 90 | g_signal_connect (power_control,
|
| 91 | "handle-get-power-state",
|
| 92 | G_CALLBACK (on_get_power_state),
|
| 93 | NULL); /* user_data */
|
| 94 |
|
| 95 | /* Export the object (@manager takes its own reference to @object) */
|
| 96 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 97 | g_object_unref (object);
|
| 98 |
|
| 99 | /* Export all objects */
|
| 100 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 101 | }
|
| 102 |
|
| 103 | static void
|
| 104 | on_name_acquired (GDBusConnection *connection,
|
| 105 | const gchar *name,
|
| 106 | gpointer user_data)
|
| 107 | {
|
| 108 | g_print ("Acquired the name %s\n", name);
|
| 109 | }
|
| 110 |
|
| 111 | static void
|
| 112 | on_name_lost (GDBusConnection *connection,
|
| 113 | const gchar *name,
|
| 114 | gpointer user_data)
|
| 115 | {
|
| 116 | g_print ("Lost the name %s\n", name);
|
| 117 | }
|
| 118 |
|
| 119 | static gboolean
|
| 120 | poll_pgood()
|
| 121 | {
|
| 122 | guint pgood = power_control_get_pgood(power_control);
|
| 123 | //TOOD: Change to actually read gpio
|
| 124 | guint gpio = tmp_pgood;
|
| 125 |
|
| 126 | g_print("Polling pgood: %d\n",gpio);
|
| 127 |
|
| 128 | //if changed, set property and emit signal
|
| 129 | if (gpio != power_control_get_pgood(power_control))
|
| 130 | {
|
| 131 | power_control_set_pgood(power_control,gpio);
|
| 132 | if (gpio==0)
|
| 133 | {
|
| 134 | power_control_emit_power_lost(power_control);
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | power_control_emit_power_good(power_control);
|
| 139 | }
|
| 140 | }
|
| 141 | return TRUE;
|
| 142 | }
|
| 143 |
|
| 144 | gint
|
| 145 | main (gint argc, gchar *argv[])
|
| 146 | {
|
| 147 | GMainLoop *loop;
|
| 148 |
|
| 149 | guint id;
|
| 150 | //g_type_init ();
|
| 151 | loop = g_main_loop_new (NULL, FALSE);
|
| 152 |
|
| 153 | id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
| 154 | "org.openbmc.PowerControl",
|
| 155 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 156 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 157 | on_bus_acquired,
|
| 158 | on_name_acquired,
|
| 159 | on_name_lost,
|
| 160 | loop,
|
| 161 | NULL);
|
| 162 |
|
| 163 | g_timeout_add(5000, poll_pgood, NULL);
|
| 164 | g_main_loop_run (loop);
|
| 165 |
|
| 166 | g_bus_unown_name (id);
|
| 167 | g_main_loop_unref (loop);
|
| 168 | return 0;
|
| 169 | }
|