Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 4 | import dbus |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 5 | import json |
Brad Bishop | 3cec296 | 2016-06-21 20:51:54 -0400 | [diff] [blame] | 6 | import xml.etree.ElementTree as ET |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 7 | |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 8 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 9 | def fix_byte(it, key, parent): |
| 10 | if (isinstance(it, dbus.Array)): |
| 11 | for i in range(0, len(it)): |
| 12 | fix_byte(it[i], i, it) |
| 13 | elif (isinstance(it, dict)): |
| 14 | for key in it.keys(): |
| 15 | fix_byte(it[key], key, it) |
| 16 | elif (isinstance(it, dbus.Byte)): |
Brad Bishop | 8e5354e | 2016-06-24 09:09:29 -0400 | [diff] [blame] | 17 | if (key != None): |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 18 | parent[key] = int(it) |
| 19 | elif (isinstance(it, dbus.Double)): |
| 20 | if (key != None): |
| 21 | parent[key] = float(it) |
| 22 | else: |
| 23 | pass |
Brad Bishop | 38da62a | 2016-06-21 21:39:57 -0400 | [diff] [blame] | 24 | |
| 25 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 26 | def printDict(name, data): |
| 27 | if (isinstance(data, dict)): |
| 28 | print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 29 | print name |
| 30 | for p in sorted(data.keys()): |
| 31 | printDict(p, data[p]) |
| 32 | else: |
| 33 | print name + " = " + str(data) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 34 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 35 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 36 | def introspect(bus_name, obj_path, intf_name, method_name): |
| 37 | obj = bus.get_object(bus_name, obj_path) |
| 38 | introspect_iface = dbus.Interface(obj, |
| 39 | "org.freedesktop.DBus.Introspectable") |
| 40 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 41 | # print method_name |
| 42 | # print introspect_iface.Introspect() |
| 43 | root = tree.getroot() |
| 44 | found = False |
| 45 | for node in root.iter('node'): |
| 46 | for intf in node.iter('interface'): |
| 47 | if (intf.attrib['name'] == intf_name): |
| 48 | for method in intf.iter('method'): |
| 49 | if (method.attrib['name'] == method_name): |
| 50 | for ar in method.iter('arg'): |
| 51 | if (ar.attrib['direction'] == "in"): |
| 52 | print "\t" + ar.attrib['name'] + " (" + \ |
| 53 | ar.attrib['type'] + ")" |
| 54 | found = True |
| 55 | |
| 56 | return found |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 57 | |
| 58 | |
| 59 | dbus_objects = { |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 60 | 'power': { |
| 61 | 'bus_name': 'org.openbmc.control.Power', |
| 62 | 'object_name': '/org/openbmc/control/power0', |
| 63 | 'interface_name': 'org.openbmc.control.Power' |
| 64 | }, |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 65 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 66 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 67 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 68 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 69 | 'property': 'RequestedHostTransition', |
| 70 | 'value': 'xyz.openbmc_project.State.Host.Transition.On' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 71 | }, |
| 72 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 73 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 74 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 75 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 76 | 'property': 'RequestedHostTransition', |
| 77 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 78 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 79 | 'bmcstate': { |
| 80 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 81 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 82 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 83 | 'property': 'CurrentBMCState', |
| 84 | }, |
| 85 | 'chassisstate': { |
| 86 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 87 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 88 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 89 | 'property': 'CurrentPowerState', |
| 90 | }, |
| 91 | 'hoststate': { |
| 92 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 93 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 94 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 95 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 96 | }, |
| 97 | 'bootprogress': { |
| 98 | 'bus_name': 'org.openbmc.Sensors', |
| 99 | 'object_name': '/org/openbmc/sensors/host/BootProgress', |
| 100 | 'interface_name': 'org.openbmc.SensorValue' |
| 101 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 104 | # Commands that need to run multiple objects above |
| 105 | multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] } |
| 106 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 107 | bus = dbus.SystemBus() |
| 108 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 109 | if (len(sys.argv) == 1 or sys.argv[1] == "-h" or |
| 110 | (not(dbus_objects.has_key(sys.argv[1])) and |
| 111 | (not(multicmd_objects.has_key(sys.argv[1]))))): |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 112 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 113 | print "\tIf [method] is blank, then all properties are printed\n" |
| 114 | print "Available commands:" |
| 115 | for name in sorted(dbus_objects.keys()): |
| 116 | m = "" |
| 117 | if (dbus_objects[name].has_key('method') == True): |
| 118 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 119 | dbus_objects[name]['method'] + ")" |
| 120 | elif (dbus_objects[name].has_key('property') == True): |
| 121 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 122 | dbus_objects[name]['property'] + ")" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 123 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 124 | print "\t" + name + m |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 125 | print "Multi-Commands:" |
| 126 | for name in sorted(multicmd_objects.keys()): |
| 127 | print "\t" + name + " -> " + ",".join(multicmd_objects[name]) |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 128 | exit(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 129 | |
| 130 | method_name = "" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 131 | property_name = "" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 132 | |
| 133 | sys.argv.pop(0) |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 134 | cmd = [sys.argv.pop(0)] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 135 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 136 | # Check if this is a multicmd command and update if it is |
| 137 | if(multicmd_objects.has_key(cmd[0])): |
| 138 | cmd = multicmd_objects[cmd[0]] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 139 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 140 | for c in cmd: |
| 141 | objinfo = dbus_objects[c] |
| 142 | if (objinfo.has_key('method')): |
| 143 | method_name = objinfo['method'] |
| 144 | elif (objinfo.has_key('property')): |
| 145 | property_name = objinfo['property'] |
| 146 | elif (len(sys.argv) > 0): |
| 147 | ## if command line args left and method not specified |
| 148 | ## then next arg must be method name |
| 149 | method_name = sys.argv.pop(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 150 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 151 | bus_name = objinfo['bus_name'] |
| 152 | obj_path = objinfo['object_name'] |
| 153 | intf_name = objinfo['interface_name'] |
| 154 | obj = bus.get_object(bus_name, obj_path) |
| 155 | |
| 156 | if (method_name != ""): |
| 157 | methd = obj.get_dbus_method(method_name, intf_name) |
| 158 | try: |
| 159 | data = methd(*sys.argv) |
| 160 | fix_byte(data, None, None) |
| 161 | pydata = json.loads(json.dumps(data)) |
| 162 | if 'transform' in objinfo: |
| 163 | objinfo['transform'](pydata) |
| 164 | printDict("", pydata) |
| 165 | except Exception as e: |
| 166 | print e |
| 167 | r = introspect(bus_name, obj_path, intf_name, method_name) |
| 168 | if (r == False): |
| 169 | print "ERROR: Invalid method: " + method_name |
| 170 | else: |
| 171 | print "ERROR: Incorrect arguments passed to method" |
| 172 | elif (property_name != ""): |
| 173 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 174 | if (objinfo.has_key('value')): |
| 175 | property_value = objinfo['value'] |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 176 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 177 | if(len(sys.argv) > 0): |
| 178 | property_value = eval(sys.argv.pop(0)) |
| 179 | else: |
| 180 | property_value = None |
| 181 | if(property_value is not None): |
| 182 | intf.Set(intf_name, property_name, property_value) |
| 183 | else: |
| 184 | prop = intf.Get(intf_name,property_name) |
| 185 | print '{:<20}'.format(property_name+": ") + str(prop) |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 186 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 187 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 188 | props = intf.GetAll(intf_name) |
| 189 | for p in props: |
Vishwanatha Subbanna | cf951db | 2017-03-23 18:46:06 +0530 | [diff] [blame] | 190 | print p + " = " + str(props[p]) |