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