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 | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 65 | 'chassison': { |
| 66 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 67 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 68 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 69 | 'property': 'RequestedPowerTransition', |
| 70 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.On' |
| 71 | }, |
| 72 | 'chassisoff': { |
| 73 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 74 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 75 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 76 | 'property': 'RequestedPowerTransition', |
| 77 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.Off' |
| 78 | }, |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 79 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 80 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 81 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 82 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 83 | 'property': 'RequestedHostTransition', |
| 84 | 'value': 'xyz.openbmc_project.State.Host.Transition.On' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 85 | }, |
| 86 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 87 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 88 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 89 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 90 | 'property': 'RequestedHostTransition', |
| 91 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 92 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 93 | 'bmcstate': { |
| 94 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 95 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 96 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 97 | 'property': 'CurrentBMCState', |
| 98 | }, |
| 99 | 'chassisstate': { |
| 100 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 101 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 102 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 103 | 'property': 'CurrentPowerState', |
| 104 | }, |
| 105 | 'hoststate': { |
| 106 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 107 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 108 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 109 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 110 | }, |
| 111 | 'bootprogress': { |
George Keishing | 3be0995 | 2017-08-18 09:48:03 -0500 | [diff] [blame] | 112 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 113 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 114 | 'interface_name': 'xyz.openbmc_project.State.Boot.Progress', |
| 115 | 'property': 'BootProgress', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 116 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 119 | # Commands that need to run multiple objects above |
| 120 | multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] } |
| 121 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 122 | bus = dbus.SystemBus() |
| 123 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 124 | if (len(sys.argv) == 1 or sys.argv[1] == "-h" or |
| 125 | (not(dbus_objects.has_key(sys.argv[1])) and |
| 126 | (not(multicmd_objects.has_key(sys.argv[1]))))): |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 127 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 128 | print "\tIf [method] is blank, then all properties are printed\n" |
| 129 | print "Available commands:" |
| 130 | for name in sorted(dbus_objects.keys()): |
| 131 | m = "" |
| 132 | if (dbus_objects[name].has_key('method') == True): |
| 133 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 134 | dbus_objects[name]['method'] + ")" |
| 135 | elif (dbus_objects[name].has_key('property') == True): |
| 136 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 137 | dbus_objects[name]['property'] + ")" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 138 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 139 | print "\t" + name + m |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 140 | print "Multi-Commands:" |
| 141 | for name in sorted(multicmd_objects.keys()): |
| 142 | print "\t" + name + " -> " + ",".join(multicmd_objects[name]) |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 143 | exit(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 144 | |
| 145 | method_name = "" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 146 | property_name = "" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 147 | |
| 148 | sys.argv.pop(0) |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 149 | cmd = [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 | # Check if this is a multicmd command and update if it is |
| 152 | if(multicmd_objects.has_key(cmd[0])): |
| 153 | cmd = multicmd_objects[cmd[0]] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 154 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 155 | for c in cmd: |
| 156 | objinfo = dbus_objects[c] |
| 157 | if (objinfo.has_key('method')): |
| 158 | method_name = objinfo['method'] |
| 159 | elif (objinfo.has_key('property')): |
| 160 | property_name = objinfo['property'] |
| 161 | elif (len(sys.argv) > 0): |
| 162 | ## if command line args left and method not specified |
| 163 | ## then next arg must be method name |
| 164 | method_name = sys.argv.pop(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 165 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 166 | bus_name = objinfo['bus_name'] |
| 167 | obj_path = objinfo['object_name'] |
| 168 | intf_name = objinfo['interface_name'] |
| 169 | obj = bus.get_object(bus_name, obj_path) |
| 170 | |
| 171 | if (method_name != ""): |
| 172 | methd = obj.get_dbus_method(method_name, intf_name) |
| 173 | try: |
| 174 | data = methd(*sys.argv) |
| 175 | fix_byte(data, None, None) |
| 176 | pydata = json.loads(json.dumps(data)) |
| 177 | if 'transform' in objinfo: |
| 178 | objinfo['transform'](pydata) |
| 179 | printDict("", pydata) |
| 180 | except Exception as e: |
| 181 | print e |
| 182 | r = introspect(bus_name, obj_path, intf_name, method_name) |
| 183 | if (r == False): |
| 184 | print "ERROR: Invalid method: " + method_name |
| 185 | else: |
| 186 | print "ERROR: Incorrect arguments passed to method" |
| 187 | elif (property_name != ""): |
| 188 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 189 | if (objinfo.has_key('value')): |
| 190 | property_value = objinfo['value'] |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 191 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 192 | if(len(sys.argv) > 0): |
| 193 | property_value = eval(sys.argv.pop(0)) |
| 194 | else: |
| 195 | property_value = None |
| 196 | if(property_value is not None): |
| 197 | intf.Set(intf_name, property_name, property_value) |
| 198 | else: |
| 199 | prop = intf.Get(intf_name,property_name) |
| 200 | print '{:<20}'.format(property_name+": ") + str(prop) |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 201 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 202 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 203 | props = intf.GetAll(intf_name) |
| 204 | for p in props: |
Vishwanatha Subbanna | cf951db | 2017-03-23 18:46:06 +0530 | [diff] [blame] | 205 | print p + " = " + str(props[p]) |