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