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 | |
| 26 | def merge_interfaces(objs): |
| 27 | for op in objs: |
| 28 | merged = {} |
| 29 | for interface, properties in objs[op].items(): |
| 30 | merged.update(properties) |
| 31 | |
| 32 | del objs[op] |
| 33 | objs[op] = merged |
| 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 printDict(name, data): |
| 37 | if (isinstance(data, dict)): |
| 38 | print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 39 | print name |
| 40 | for p in sorted(data.keys()): |
| 41 | printDict(p, data[p]) |
| 42 | else: |
| 43 | print name + " = " + str(data) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 44 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 45 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 46 | def introspect(bus_name, obj_path, intf_name, method_name): |
| 47 | obj = bus.get_object(bus_name, obj_path) |
| 48 | introspect_iface = dbus.Interface(obj, |
| 49 | "org.freedesktop.DBus.Introspectable") |
| 50 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 51 | # print method_name |
| 52 | # print introspect_iface.Introspect() |
| 53 | root = tree.getroot() |
| 54 | found = False |
| 55 | for node in root.iter('node'): |
| 56 | for intf in node.iter('interface'): |
| 57 | if (intf.attrib['name'] == intf_name): |
| 58 | for method in intf.iter('method'): |
| 59 | if (method.attrib['name'] == method_name): |
| 60 | for ar in method.iter('arg'): |
| 61 | if (ar.attrib['direction'] == "in"): |
| 62 | print "\t" + ar.attrib['name'] + " (" + \ |
| 63 | ar.attrib['type'] + ")" |
| 64 | found = True |
| 65 | |
| 66 | return found |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 67 | |
| 68 | |
| 69 | dbus_objects = { |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 70 | 'power': { |
| 71 | 'bus_name': 'org.openbmc.control.Power', |
| 72 | 'object_name': '/org/openbmc/control/power0', |
| 73 | 'interface_name': 'org.openbmc.control.Power' |
| 74 | }, |
| 75 | 'identify_led': { |
| 76 | 'bus_name': 'org.openbmc.control.led', |
| 77 | 'object_name': '/org/openbmc/control/led/identify', |
| 78 | 'interface_name': 'org.openbmc.Led' |
| 79 | }, |
| 80 | 'chassis': { |
| 81 | 'bus_name': 'org.openbmc.control.Chassis', |
| 82 | 'object_name': '/org/openbmc/control/chassis0', |
| 83 | 'interface_name': 'org.openbmc.control.Chassis' |
| 84 | }, |
| 85 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 86 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 87 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 88 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 89 | 'property': 'RequestedHostTransition', |
| 90 | 'value': 'xyz.openbmc_project.State.Host.Transition.On' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 91 | }, |
| 92 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 93 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 94 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 95 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 96 | 'property': 'RequestedHostTransition', |
| 97 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 98 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 99 | 'bmcstate': { |
| 100 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 101 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 102 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 103 | 'property': 'CurrentBMCState', |
| 104 | }, |
| 105 | 'chassisstate': { |
| 106 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 107 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 108 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 109 | 'property': 'CurrentPowerState', |
| 110 | }, |
| 111 | 'hoststate': { |
| 112 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 113 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 114 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 115 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 116 | }, |
| 117 | 'bootprogress': { |
| 118 | 'bus_name': 'org.openbmc.Sensors', |
| 119 | 'object_name': '/org/openbmc/sensors/host/BootProgress', |
| 120 | 'interface_name': 'org.openbmc.SensorValue' |
| 121 | }, |
| 122 | 'biosupdate': { |
| 123 | 'bus_name': 'org.openbmc.control.Flash', |
| 124 | 'object_name': '/org/openbmc/control/flash/bios', |
| 125 | 'interface_name': 'org.openbmc.Flash', |
| 126 | 'method': 'updateViaTftp', |
| 127 | }, |
| 128 | 'biosflash': { |
| 129 | 'bus_name': 'org.openbmc.control.Flash', |
| 130 | 'object_name': '/org/openbmc/control/flash/bios', |
| 131 | 'interface_name': 'org.openbmc.Flash', |
| 132 | }, |
| 133 | 'bmcupdate': { |
| 134 | 'bus_name': 'org.openbmc.control.BmcFlash', |
| 135 | 'object_name': '/org/openbmc/control/flash/bmc', |
| 136 | 'interface_name': 'org.openbmc.control.BmcFlash', |
| 137 | 'method': 'updateViaTftp', |
| 138 | }, |
| 139 | 'bmcflash': { |
| 140 | 'bus_name': 'org.openbmc.control.BmcFlash', |
| 141 | 'object_name': '/org/openbmc/control/flash/bmc', |
| 142 | 'interface_name': 'org.openbmc.control.BmcFlash', |
| 143 | }, |
| 144 | 'getinventory': { |
| 145 | 'bus_name': 'org.openbmc.Inventory', |
| 146 | 'object_name': '/org/openbmc/inventory', |
| 147 | 'interface_name': 'org.freedesktop.DBus.ObjectManager', |
| 148 | 'method': 'GetManagedObjects', |
| 149 | 'transform': merge_interfaces |
| 150 | }, |
| 151 | 'getsensors': { |
| 152 | 'bus_name': 'org.openbmc.Sensors', |
| 153 | 'object_name': '/org/openbmc/sensors', |
| 154 | 'interface_name': 'org.freedesktop.DBus.ObjectManager', |
| 155 | 'method': 'GetManagedObjects', |
| 156 | 'transform': merge_interfaces |
| 157 | }, |
| 158 | 'host': { |
| 159 | 'bus_name': 'org.openbmc.control.Host', |
| 160 | 'object_name': '/org/openbmc/control/host0', |
| 161 | 'interface_name': 'org.openbmc.control.Host', |
| 162 | }, |
| 163 | 'setdebugmode': { |
| 164 | 'bus_name': 'org.openbmc.control.Host', |
| 165 | 'object_name': '/org/openbmc/control/host0', |
| 166 | 'interface_name': 'org.openbmc.control.Host', |
| 167 | 'property': 'debug_mode' |
| 168 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 171 | # Commands that need to run multiple objects above |
| 172 | multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] } |
| 173 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 174 | bus = dbus.SystemBus() |
| 175 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 176 | if (len(sys.argv) == 1 or sys.argv[1] == "-h" or |
| 177 | (not(dbus_objects.has_key(sys.argv[1])) and |
| 178 | (not(multicmd_objects.has_key(sys.argv[1]))))): |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 179 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 180 | print "\tIf [method] is blank, then all properties are printed\n" |
| 181 | print "Available commands:" |
| 182 | for name in sorted(dbus_objects.keys()): |
| 183 | m = "" |
| 184 | if (dbus_objects[name].has_key('method') == True): |
| 185 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 186 | dbus_objects[name]['method'] + ")" |
| 187 | elif (dbus_objects[name].has_key('property') == True): |
| 188 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 189 | dbus_objects[name]['property'] + ")" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 190 | |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 191 | print "\t" + name + m |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 192 | print "Multi-Commands:" |
| 193 | for name in sorted(multicmd_objects.keys()): |
| 194 | print "\t" + name + " -> " + ",".join(multicmd_objects[name]) |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 195 | exit(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 196 | |
| 197 | method_name = "" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 198 | property_name = "" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 199 | |
| 200 | sys.argv.pop(0) |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 201 | cmd = [sys.argv.pop(0)] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 202 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 203 | # Check if this is a multicmd command and update if it is |
| 204 | if(multicmd_objects.has_key(cmd[0])): |
| 205 | cmd = multicmd_objects[cmd[0]] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 206 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 207 | for c in cmd: |
| 208 | objinfo = dbus_objects[c] |
| 209 | if (objinfo.has_key('method')): |
| 210 | method_name = objinfo['method'] |
| 211 | elif (objinfo.has_key('property')): |
| 212 | property_name = objinfo['property'] |
| 213 | elif (len(sys.argv) > 0): |
| 214 | ## if command line args left and method not specified |
| 215 | ## then next arg must be method name |
| 216 | method_name = sys.argv.pop(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 217 | |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 218 | bus_name = objinfo['bus_name'] |
| 219 | obj_path = objinfo['object_name'] |
| 220 | intf_name = objinfo['interface_name'] |
| 221 | obj = bus.get_object(bus_name, obj_path) |
| 222 | |
| 223 | if (method_name != ""): |
| 224 | methd = obj.get_dbus_method(method_name, intf_name) |
| 225 | try: |
| 226 | data = methd(*sys.argv) |
| 227 | fix_byte(data, None, None) |
| 228 | pydata = json.loads(json.dumps(data)) |
| 229 | if 'transform' in objinfo: |
| 230 | objinfo['transform'](pydata) |
| 231 | printDict("", pydata) |
| 232 | except Exception as e: |
| 233 | print e |
| 234 | r = introspect(bus_name, obj_path, intf_name, method_name) |
| 235 | if (r == False): |
| 236 | print "ERROR: Invalid method: " + method_name |
| 237 | else: |
| 238 | print "ERROR: Incorrect arguments passed to method" |
| 239 | elif (property_name != ""): |
| 240 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 241 | if (objinfo.has_key('value')): |
| 242 | property_value = objinfo['value'] |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 243 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 244 | if(len(sys.argv) > 0): |
| 245 | property_value = eval(sys.argv.pop(0)) |
| 246 | else: |
| 247 | property_value = None |
| 248 | if(property_value is not None): |
| 249 | intf.Set(intf_name, property_name, property_value) |
| 250 | else: |
| 251 | prop = intf.Get(intf_name,property_name) |
| 252 | print '{:<20}'.format(property_name+": ") + str(prop) |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 253 | else: |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 254 | intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 255 | props = intf.GetAll(intf_name) |
| 256 | for p in props: |
| 257 | print p + " = " + str(props[p]) |