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 | |
| 9 | def fix_byte(it,key,parent): |
| 10 | if (isinstance(it,dbus.Array)): |
| 11 | for i in range(0,len(it)): |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 12 | fix_byte(it[i],i,it) |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 13 | elif (isinstance(it, dict)): |
| 14 | for key in it.keys(): |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 15 | fix_byte(it[key],key,it) |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 16 | elif (isinstance(it,dbus.Byte)): |
| 17 | if (key != None): |
| 18 | parent[key] = int(it) |
| 19 | else: |
| 20 | pass |
Brad Bishop | 38da62a | 2016-06-21 21:39:57 -0400 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def merge_interfaces(objs): |
| 24 | for op in objs: |
| 25 | merged = {} |
| 26 | for interface, properties in objs[op].items(): |
| 27 | merged.update(properties) |
| 28 | |
| 29 | del objs[op] |
| 30 | objs[op] = merged |
| 31 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 32 | |
| 33 | def printDict(name,data): |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 34 | if (isinstance(data, dict)): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 35 | print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 36 | print name |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 37 | for p in sorted(data.keys()): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 38 | printDict(p,data[p]) |
| 39 | else: |
| 40 | print name+" = "+str(data) |
| 41 | |
| 42 | def introspect(bus_name,obj_path,intf_name,method_name): |
| 43 | obj = bus.get_object(bus_name,obj_path) |
| 44 | introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") |
| 45 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 46 | #print method_name |
| 47 | #print introspect_iface.Introspect() |
| 48 | root = tree.getroot() |
| 49 | found = False |
| 50 | for node in root.iter('node'): |
| 51 | for intf in node.iter('interface'): |
| 52 | if (intf.attrib['name'] == intf_name): |
| 53 | for method in intf.iter('method'): |
| 54 | if (method.attrib['name'] == method_name): |
| 55 | for ar in method.iter('arg'): |
| 56 | if (ar.attrib['direction'] == "in"): |
| 57 | print "\t"+ar.attrib['name']+" ("+ar.attrib['type']+")" |
| 58 | found = True |
| 59 | |
| 60 | return found |
| 61 | |
| 62 | |
| 63 | dbus_objects = { |
| 64 | 'power' : { |
| 65 | 'bus_name' : 'org.openbmc.control.Power', |
| 66 | 'object_name' : '/org/openbmc/control/power0', |
| 67 | 'interface_name' : 'org.openbmc.control.Power' |
| 68 | }, |
| 69 | 'identify_led' : { |
| 70 | 'bus_name' : 'org.openbmc.control.led', |
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 71 | 'object_name' : '/org/openbmc/control/led/identify', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 72 | 'interface_name' : 'org.openbmc.Led' |
| 73 | }, |
| 74 | 'chassis' : { |
| 75 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 76 | 'object_name' : '/org/openbmc/control/chassis0', |
| 77 | 'interface_name' : 'org.openbmc.control.Chassis' |
| 78 | }, |
| 79 | 'poweron' : { |
| 80 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 81 | 'object_name' : '/org/openbmc/control/chassis0', |
| 82 | 'interface_name' : 'org.openbmc.control.Chassis', |
| 83 | 'method' : 'powerOn', |
| 84 | }, |
| 85 | 'poweroff' : { |
| 86 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 87 | 'object_name' : '/org/openbmc/control/chassis0', |
| 88 | 'interface_name' : 'org.openbmc.control.Chassis', |
| 89 | 'method' : 'powerOff', |
| 90 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 91 | 'state' : { |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 92 | 'bus_name' : 'org.openbmc.managers.System', |
| 93 | 'object_name' : '/org/openbmc/managers/System', |
| 94 | 'interface_name' : 'org.openbmc.managers.System', |
| 95 | 'method' : 'getSystemState', |
| 96 | }, |
| 97 | 'bootprogress' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 98 | 'bus_name' : 'org.openbmc.Sensors', |
| 99 | 'object_name' : '/org/openbmc/sensors/host/BootProgress', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 100 | 'interface_name' : 'org.openbmc.SensorValue' |
| 101 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 102 | 'biosupdate' : { |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 103 | 'bus_name' : 'org.openbmc.control.Flash', |
| 104 | 'object_name' : '/org/openbmc/control/flash/bios', |
| 105 | 'interface_name' : 'org.openbmc.Flash', |
| 106 | 'method' : 'updateViaTftp', |
| 107 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 108 | 'biosflash' : { |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 109 | 'bus_name' : 'org.openbmc.control.Flash', |
| 110 | 'object_name' : '/org/openbmc/control/flash/bios', |
| 111 | 'interface_name' : 'org.openbmc.Flash', |
| 112 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 113 | 'bmcupdate' : { |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 114 | 'bus_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 115 | 'object_name' : '/org/openbmc/control/flash/bmc', |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 116 | 'interface_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 117 | 'method' : 'updateViaTftp', |
| 118 | }, |
| 119 | 'bmcflash' : { |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 120 | 'bus_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 121 | 'object_name' : '/org/openbmc/control/flash/bmc', |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 122 | 'interface_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 123 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 124 | 'getinventory' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 125 | 'bus_name' : 'org.openbmc.Inventory', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 126 | 'object_name' : '/org/openbmc/inventory', |
Brad Bishop | 38da62a | 2016-06-21 21:39:57 -0400 | [diff] [blame] | 127 | 'interface_name' : 'org.freedesktop.DBus.ObjectManager', |
| 128 | 'method' : 'GetManagedObjects', |
| 129 | 'transform' : merge_interfaces |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 130 | }, |
| 131 | 'getsensors' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 132 | 'bus_name' : 'org.openbmc.Sensors', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 133 | 'object_name' : '/org/openbmc/sensors', |
Brad Bishop | 38da62a | 2016-06-21 21:39:57 -0400 | [diff] [blame] | 134 | 'interface_name' : 'org.freedesktop.DBus.ObjectManager', |
| 135 | 'method' : 'GetManagedObjects', |
| 136 | 'transform' : merge_interfaces |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 137 | }, |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 138 | 'host' : { |
| 139 | 'bus_name' : 'org.openbmc.control.Host', |
| 140 | 'object_name' : '/org/openbmc/control/host0', |
| 141 | 'interface_name' : 'org.openbmc.control.Host', |
| 142 | }, |
| 143 | 'setdebugmode' : { |
| 144 | 'bus_name' : 'org.openbmc.control.Host', |
| 145 | 'object_name' : '/org/openbmc/control/host0', |
| 146 | 'interface_name' : 'org.openbmc.control.Host', |
| 147 | 'property' : 'debug_mode' |
| 148 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bus = dbus.SystemBus() |
| 152 | |
| 153 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 154 | if (len(sys.argv) == 1 or sys.argv[1] == "-h" or dbus_objects.has_key(sys.argv[1])==False): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 155 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 156 | print "\tIf [method] is blank, then all properties are printed\n" |
| 157 | print "Available commands:" |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 158 | for name in sorted(dbus_objects.keys()): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 159 | m = "" |
| 160 | if (dbus_objects[name].has_key('method') == True): |
| 161 | m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 162 | elif (dbus_objects[name].has_key('property') == True): |
| 163 | m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")" |
| 164 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 165 | print "\t"+name+m |
| 166 | exit(0) |
| 167 | |
| 168 | method_name = "" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 169 | property_name = "" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 170 | |
| 171 | sys.argv.pop(0) |
| 172 | objinfo = dbus_objects[sys.argv.pop(0)] |
| 173 | |
| 174 | if (objinfo.has_key('method')): |
| 175 | method_name = objinfo['method'] |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 176 | elif (objinfo.has_key('property')): |
| 177 | property_name = objinfo['property'] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 178 | elif (len(sys.argv)>0): |
| 179 | ## if command line args left and method not specified |
| 180 | ## then next arg must be method name |
| 181 | method_name = sys.argv.pop(0) |
| 182 | |
| 183 | bus_name = objinfo['bus_name'] |
| 184 | obj_path = objinfo['object_name'] |
| 185 | intf_name = objinfo['interface_name'] |
Norman James | 481a5dd | 2016-01-20 10:36:15 -0600 | [diff] [blame] | 186 | obj = bus.get_object(bus_name,obj_path) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 187 | |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 188 | if (method_name != ""): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 189 | methd = obj.get_dbus_method(method_name,intf_name) |
| 190 | try: |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 191 | data = methd(*sys.argv) |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 192 | fix_byte(data,None,None) |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 193 | pydata = json.loads(json.dumps(data)) |
Brad Bishop | 38da62a | 2016-06-21 21:39:57 -0400 | [diff] [blame] | 194 | if 'transform' in objinfo: |
| 195 | objinfo['transform'](pydata) |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 196 | printDict("",pydata) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 197 | except Exception as e: |
Norman James | 26f7dca | 2015-10-26 12:15:09 -0500 | [diff] [blame] | 198 | print e |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 199 | r = introspect(bus_name,obj_path,intf_name,method_name) |
| 200 | if (r == False): |
| 201 | print "ERROR: Invalid method: "+method_name |
| 202 | else: |
| 203 | print "ERROR: Incorrect arguments passed to method" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 204 | elif (property_name != ""): |
| 205 | intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") |
| 206 | property_value = eval(sys.argv.pop(0)) |
| 207 | intf.Set(intf_name,property_name,property_value) |
| 208 | else: |
| 209 | intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") |
| 210 | props = intf.GetAll(intf_name) |
| 211 | for p in props: |
| 212 | print p+" = "+str(props[p]) |
| 213 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 214 | |
| 215 | |