1#!/usr/bin/env python
2
3import dbus
4
5dbus_objects = {
6    "power": {
7        "bus_name": "org.openbmc.control.Power",
8        "object_name": "/org/openbmc/control/power0",
9        "interface_name": "org.openbmc.control.Power",
10    },
11    "occstatus0": {
12        "bus_name": "org.openbmc.Sensors",
13        "object_name": "/org/openbmc/sensors/host/cpu0/OccStatus",
14        "interface_name": "org.openbmc.SensorValue",
15    },
16    "occstatus1": {
17        "bus_name": "org.openbmc.Sensors",
18        "object_name": "/org/openbmc/sensors/host/cpu1/OccStatus",
19        "interface_name": "org.openbmc.SensorValue",
20    },
21    "bootprogress": {
22        "bus_name": "org.openbmc.Sensors",
23        "object_name": "/org/openbmc/sensors/host/BootProgress",
24        "interface_name": "org.openbmc.SensorValue",
25    },
26    "host": {
27        "bus_name": "xyz.openbmc_project.State.Host",
28        "object_name": "/xyz/openbmc_project/state/host0",
29        "interface_name": "xyz.openbmc_project.State.Host",
30    },
31    "settings": {
32        "bus_name": "org.openbmc.settings.Host",
33        "object_name": "/org/openbmc/settings/host0",
34        "interface_name": "org.freedesktop.DBus.Properties",
35    },
36    "system": {
37        "bus_name": "org.openbmc.managers.System",
38        "object_name": "/org/openbmc/managers/System",
39        "interface_name": "org.freedesktop.DBus.Properties",
40    },
41    "powersupplyredundancy": {
42        "bus_name": "org.openbmc.Sensors",
43        "object_name": "/org/openbmc/sensors/host/PowerSupplyRedundancy",
44        "interface_name": "org.openbmc.SensorValue",
45    },
46    "turboallowed": {
47        "bus_name": "org.openbmc.Sensors",
48        "object_name": "/org/openbmc/sensors/host/TurboAllowed",
49        "interface_name": "org.openbmc.SensorValue",
50    },
51    "powersupplyderating": {
52        "bus_name": "org.openbmc.Sensors",
53        "object_name": "/org/openbmc/sensors/host/PowerSupplyDerating",
54        "interface_name": "org.openbmc.SensorValue",
55    },
56}
57
58
59def getInterface(bus, objs, key):
60    obj = bus.get_object(
61        objs[key]["bus_name"], objs[key]["object_name"], introspect=False
62    )
63    return dbus.Interface(obj, objs[key]["interface_name"])
64
65
66def getProperty(bus, objs, key, prop):
67    obj = bus.get_object(
68        objs[key]["bus_name"], objs[key]["object_name"], introspect=False
69    )
70    intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
71    return intf.Get(objs[key]["interface_name"], prop)
72
73
74def setProperty(bus, objs, key, prop, prop_value):
75    obj = bus.get_object(objs[key]["bus_name"], objs[key]["object_name"])
76    intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
77    return intf.Set(objs[key]["interface_name"], prop, prop_value)
78
79
80bus = dbus.SystemBus()
81pgood = getProperty(bus, dbus_objects, "power", "pgood")
82
83if pgood == 1:
84    intf = getInterface(bus, dbus_objects, "bootprogress")
85    intf.setValue("FW Progress, Starting OS")
86    intf = getInterface(bus, dbus_objects, "occstatus0")
87    intf.setValue("Enabled")
88    intf = getInterface(bus, dbus_objects, "occstatus1")
89    intf.setValue("Enabled")
90else:
91    # Power is off, so check power policy
92    settings_intf = getInterface(bus, dbus_objects, "settings")
93    system_intf = getInterface(bus, dbus_objects, "system")
94    system_last_state = system_intf.Get(
95        "org.openbmc.managers.System", "system_last_state"
96    )
97    power_policy = settings_intf.Get(
98        "org.openbmc.settings.Host", "power_policy"
99    )
100    print(
101        "Last System State:"
102        + system_last_state
103        + "Power Policy:"
104        + power_policy
105    )
106
107    if power_policy == "ALWAYS_POWER_ON" or (
108        power_policy == "RESTORE_LAST_STATE"
109        and system_last_state == "HOST_POWERED_ON"
110    ):
111        setProperty(
112            bus,
113            dbus_objects,
114            "host",
115            "RequestedHostTransition",
116            "xyz.openbmc_project.State.Host.Transition.On",
117        )
118
119# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
120