Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 4 | #import subprocess |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import xml.etree.ElementTree as ET |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 10 | import json |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 11 | |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 12 | |
| 13 | def fix_byte(it,key,parent): |
| 14 | if (isinstance(it,dbus.Array)): |
| 15 | for i in range(0,len(it)): |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 16 | fix_byte(it[i],i,it) |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 17 | elif (isinstance(it, dict)): |
| 18 | for key in it.keys(): |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 19 | fix_byte(it[key],key,it) |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 20 | elif (isinstance(it,dbus.Byte)): |
| 21 | if (key != None): |
| 22 | parent[key] = int(it) |
| 23 | else: |
| 24 | pass |
| 25 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 26 | |
| 27 | def printDict(name,data): |
Norman James | 8cda6de | 2015-11-17 19:35:45 -0600 | [diff] [blame] | 28 | if (isinstance(data, dict)): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 29 | print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 30 | print name |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 31 | for p in sorted(data.keys()): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 32 | printDict(p,data[p]) |
| 33 | else: |
| 34 | print name+" = "+str(data) |
| 35 | |
| 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,"org.freedesktop.DBus.Introspectable") |
| 39 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 40 | #print method_name |
| 41 | #print introspect_iface.Introspect() |
| 42 | root = tree.getroot() |
| 43 | found = False |
| 44 | for node in root.iter('node'): |
| 45 | for intf in node.iter('interface'): |
| 46 | if (intf.attrib['name'] == intf_name): |
| 47 | for method in intf.iter('method'): |
| 48 | if (method.attrib['name'] == method_name): |
| 49 | for ar in method.iter('arg'): |
| 50 | if (ar.attrib['direction'] == "in"): |
| 51 | print "\t"+ar.attrib['name']+" ("+ar.attrib['type']+")" |
| 52 | found = True |
| 53 | |
| 54 | return found |
| 55 | |
| 56 | |
| 57 | dbus_objects = { |
| 58 | 'power' : { |
| 59 | 'bus_name' : 'org.openbmc.control.Power', |
| 60 | 'object_name' : '/org/openbmc/control/power0', |
| 61 | 'interface_name' : 'org.openbmc.control.Power' |
| 62 | }, |
| 63 | 'identify_led' : { |
| 64 | 'bus_name' : 'org.openbmc.control.led', |
Norman James | a23e7b8 | 2015-10-28 19:00:07 -0500 | [diff] [blame] | 65 | 'object_name' : '/org/openbmc/control/led/IDENTIFY', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 66 | 'interface_name' : 'org.openbmc.Led' |
| 67 | }, |
| 68 | 'chassis' : { |
| 69 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 70 | 'object_name' : '/org/openbmc/control/chassis0', |
| 71 | 'interface_name' : 'org.openbmc.control.Chassis' |
| 72 | }, |
| 73 | 'poweron' : { |
| 74 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 75 | 'object_name' : '/org/openbmc/control/chassis0', |
| 76 | 'interface_name' : 'org.openbmc.control.Chassis', |
| 77 | 'method' : 'powerOn', |
| 78 | }, |
| 79 | 'poweroff' : { |
| 80 | 'bus_name' : 'org.openbmc.control.Chassis', |
| 81 | 'object_name' : '/org/openbmc/control/chassis0', |
| 82 | 'interface_name' : 'org.openbmc.control.Chassis', |
| 83 | 'method' : 'powerOff', |
| 84 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 85 | 'state' : { |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 86 | 'bus_name' : 'org.openbmc.managers.System', |
| 87 | 'object_name' : '/org/openbmc/managers/System', |
| 88 | 'interface_name' : 'org.openbmc.managers.System', |
| 89 | 'method' : 'getSystemState', |
| 90 | }, |
| 91 | 'bootprogress' : { |
| 92 | 'bus_name' : 'org.openbmc.sensor.Power8Virtual', |
| 93 | 'object_name' : '/org/openbmc/sensor/virtual/BootProgress', |
| 94 | 'interface_name' : 'org.openbmc.SensorValue' |
| 95 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 96 | 'biosupdate' : { |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 97 | 'bus_name' : 'org.openbmc.control.Flash', |
| 98 | 'object_name' : '/org/openbmc/control/flash/bios', |
| 99 | 'interface_name' : 'org.openbmc.Flash', |
| 100 | 'method' : 'updateViaTftp', |
| 101 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 102 | 'biosflash' : { |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 103 | 'bus_name' : 'org.openbmc.control.Flash', |
| 104 | 'object_name' : '/org/openbmc/control/flash/bios', |
| 105 | 'interface_name' : 'org.openbmc.Flash', |
| 106 | }, |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 107 | 'bmcupdate' : { |
| 108 | 'bus_name' : 'org.openbmc.control.Flash', |
| 109 | 'object_name' : '/org/openbmc/control/flash/bmc', |
| 110 | 'interface_name' : 'org.openbmc.Flash', |
| 111 | 'method' : 'updateViaTftp', |
| 112 | }, |
| 113 | 'bmcflash' : { |
| 114 | 'bus_name' : 'org.openbmc.control.Flash', |
| 115 | 'object_name' : '/org/openbmc/control/flash/bmc', |
| 116 | 'interface_name' : 'org.openbmc.Flash', |
| 117 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 118 | 'getinventory' : { |
| 119 | 'bus_name' : 'org.openbmc.managers.Inventory', |
| 120 | 'object_name' : '/org/openbmc/inventory', |
| 121 | 'interface_name' : 'org.openbmc.Object.Enumerate', |
| 122 | 'method' : 'enumerate' |
| 123 | }, |
| 124 | 'getsensors' : { |
| 125 | 'bus_name' : 'org.openbmc.managers.Sensors', |
| 126 | 'object_name' : '/org/openbmc/sensors', |
| 127 | 'interface_name' : 'org.openbmc.Object.Enumerate', |
| 128 | 'method' : 'enumerate' |
| 129 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bus = dbus.SystemBus() |
| 133 | |
| 134 | |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 135 | if (len(sys.argv) == 1 or sys.argv[1] == "-h"): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 136 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 137 | print "\tIf [method] is blank, then all properties are printed\n" |
| 138 | print "Available commands:" |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 139 | for name in sorted(dbus_objects.keys()): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 140 | m = "" |
| 141 | if (dbus_objects[name].has_key('method') == True): |
| 142 | m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")" |
| 143 | print "\t"+name+m |
| 144 | exit(0) |
| 145 | |
| 146 | method_name = "" |
| 147 | |
| 148 | sys.argv.pop(0) |
| 149 | objinfo = dbus_objects[sys.argv.pop(0)] |
| 150 | |
| 151 | if (objinfo.has_key('method')): |
| 152 | method_name = objinfo['method'] |
| 153 | elif (len(sys.argv)>0): |
| 154 | ## if command line args left and method not specified |
| 155 | ## then next arg must be method name |
| 156 | method_name = sys.argv.pop(0) |
| 157 | |
| 158 | bus_name = objinfo['bus_name'] |
| 159 | obj_path = objinfo['object_name'] |
| 160 | intf_name = objinfo['interface_name'] |
| 161 | obj = bus.get_object(bus_name,obj_path) |
| 162 | |
| 163 | if (method_name == ""): |
| 164 | intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") |
| 165 | props = intf.GetAll(intf_name) |
| 166 | for p in props: |
| 167 | print p+" = "+str(props[p]) |
| 168 | |
| 169 | else: |
| 170 | methd = obj.get_dbus_method(method_name,intf_name) |
| 171 | try: |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 172 | data = methd(*sys.argv) |
| 173 | fix_byte(data,None,None) |
| 174 | pydata = json.loads(json.dumps(data)) |
| 175 | printDict("",pydata) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 176 | except Exception as e: |
Norman James | 26f7dca | 2015-10-26 12:15:09 -0500 | [diff] [blame] | 177 | print e |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 178 | r = introspect(bus_name,obj_path,intf_name,method_name) |
| 179 | if (r == False): |
| 180 | print "ERROR: Invalid method: "+method_name |
| 181 | else: |
| 182 | print "ERROR: Incorrect arguments passed to method" |
| 183 | |
| 184 | |
| 185 | |