#!/usr/bin/python

import sys
import dbus
import json
import xml.etree.ElementTree as ET

dbus_objects = {
    'power': {
        'bus_name': 'org.openbmc.control.Power',
        'object_name': '/org/openbmc/control/power0',
        'interface_name': 'org.openbmc.control.Power'
    },
    'chassison': {
        'bus_name': 'xyz.openbmc_project.State.Chassis',
        'object_name': '/xyz/openbmc_project/state/chassis0',
        'interface_name': 'xyz.openbmc_project.State.Chassis',
        'property': 'RequestedPowerTransition',
        'value': 'xyz.openbmc_project.State.Chassis.Transition.On'
    },
    'chassisoff': {
        'bus_name': 'xyz.openbmc_project.State.Chassis',
        'object_name': '/xyz/openbmc_project/state/chassis0',
        'interface_name': 'xyz.openbmc_project.State.Chassis',
        'property': 'RequestedPowerTransition',
        'value': 'xyz.openbmc_project.State.Chassis.Transition.Off'
    },
    'poweron': {
        'bus_name': 'xyz.openbmc_project.State.Host',
        'object_name': '/xyz/openbmc_project/state/host0',
        'interface_name': 'xyz.openbmc_project.State.Host',
        'property': 'RequestedHostTransition',
        'value': 'xyz.openbmc_project.State.Host.Transition.On'
    },
    'poweroff': {
        'bus_name': 'xyz.openbmc_project.State.Host',
        'object_name': '/xyz/openbmc_project/state/host0',
        'interface_name': 'xyz.openbmc_project.State.Host',
        'property': 'RequestedHostTransition',
        'value': 'xyz.openbmc_project.State.Host.Transition.Off'
    },
    'bmcstate': {
        'bus_name': 'xyz.openbmc_project.State.BMC',
        'object_name': '/xyz/openbmc_project/state/bmc0',
        'interface_name': 'xyz.openbmc_project.State.BMC',
        'property': 'CurrentBMCState',
    },
    'chassisstate': {
        'bus_name': 'xyz.openbmc_project.State.Chassis',
        'object_name': '/xyz/openbmc_project/state/chassis0',
        'interface_name': 'xyz.openbmc_project.State.Chassis',
        'property': 'CurrentPowerState',
    },
    'hoststate': {
        'bus_name': 'xyz.openbmc_project.State.Host',
        'object_name': '/xyz/openbmc_project/state/host0',
        'interface_name': 'xyz.openbmc_project.State.Host',
        'property': 'CurrentHostState',
    },
    'bootprogress': {
        'bus_name': 'xyz.openbmc_project.State.Host',
        'object_name': '/xyz/openbmc_project/state/host0',
        'interface_name': 'xyz.openbmc_project.State.Boot.Progress',
        'property': 'BootProgress',
    },
}

def main():
    # Commands that need to run multiple objects above
    multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] }

    dbus_bus = dbus.SystemBus()

    if (len(sys.argv) == 1 or sys.argv[1] == "-h" or
            (not(dbus_objects.has_key(sys.argv[1])) and
            (not(multicmd_objects.has_key(sys.argv[1]))))):
        print "Usage: obmcutil [command]"
        print "Available commands:"
        for name in sorted(dbus_objects.keys()):
            m = ""
            if (dbus_objects[name].has_key('property') == True):
                m = " (" + dbus_objects[name]['interface_name'] + "->" + \
                    dbus_objects[name]['property'] + ")"

            print "\t" + name + m
        print "Multi-Commands:"
        for name in sorted(multicmd_objects.keys()):
            print "\t" + name + " -> " + ",".join(multicmd_objects[name])
        exit(0)

    property_name = ""

    sys.argv.pop(0)
    cmd = [sys.argv.pop(0)]

    # Check if this is a multicmd command and update if it is
    if(multicmd_objects.has_key(cmd[0])):
        cmd = multicmd_objects[cmd[0]]

    for c in cmd:
        objinfo = dbus_objects[c]
        if (objinfo.has_key('property')):
            property_name = objinfo['property']

        bus_name = objinfo['bus_name']
        obj = objinfo['object_name']
        iface = objinfo['interface_name']
        dbus_obj = dbus_bus.get_object(bus_name, obj)

        if (property_name != ""):
            dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties")
            if (objinfo.has_key('value')):
                property_value = objinfo['value']
            else:
                if(len(sys.argv) > 0):
                    property_value = eval(sys.argv.pop(0))
                else:
                    property_value = None
            if(property_value is not None):
                dbus_iface.Set(iface, property_name, property_value)
            else:
                prop = dbus_iface.Get(iface, property_name)
                print '{:<20}'.format(property_name+": ") + str(prop)
        else:
            dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties")
            props = dbus_iface.GetAll(iface)
            for p in props:
                print p + " = " + str(props[p])

if __name__ == "__main__":
    main()
