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', |
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [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' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 92 | 'bus_name' : 'org.openbmc.Sensors', |
| 93 | 'object_name' : '/org/openbmc/sensors/host/BootProgress', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 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' : { |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 108 | 'bus_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 109 | 'object_name' : '/org/openbmc/control/flash/bmc', |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 110 | 'interface_name' : 'org.openbmc.control.BmcFlash', |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 111 | 'method' : 'updateViaTftp', |
| 112 | }, |
| 113 | 'bmcflash' : { |
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 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 118 | 'getinventory' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 119 | 'bus_name' : 'org.openbmc.Inventory', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 120 | 'object_name' : '/org/openbmc/inventory', |
| 121 | 'interface_name' : 'org.openbmc.Object.Enumerate', |
| 122 | 'method' : 'enumerate' |
| 123 | }, |
| 124 | 'getsensors' : { |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 125 | 'bus_name' : 'org.openbmc.Sensors', |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 126 | 'object_name' : '/org/openbmc/sensors', |
| 127 | 'interface_name' : 'org.openbmc.Object.Enumerate', |
| 128 | 'method' : 'enumerate' |
| 129 | }, |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 130 | 'host' : { |
| 131 | 'bus_name' : 'org.openbmc.control.Host', |
| 132 | 'object_name' : '/org/openbmc/control/host0', |
| 133 | 'interface_name' : 'org.openbmc.control.Host', |
| 134 | }, |
| 135 | 'setdebugmode' : { |
| 136 | 'bus_name' : 'org.openbmc.control.Host', |
| 137 | 'object_name' : '/org/openbmc/control/host0', |
| 138 | 'interface_name' : 'org.openbmc.control.Host', |
| 139 | 'property' : 'debug_mode' |
| 140 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bus = dbus.SystemBus() |
| 144 | |
| 145 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 146 | 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] | 147 | print "Usage: obmcutil [command] [[method] [*args]]" |
| 148 | print "\tIf [method] is blank, then all properties are printed\n" |
| 149 | print "Available commands:" |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 150 | for name in sorted(dbus_objects.keys()): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 151 | m = "" |
| 152 | if (dbus_objects[name].has_key('method') == True): |
| 153 | m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 154 | elif (dbus_objects[name].has_key('property') == True): |
| 155 | m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")" |
| 156 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 157 | print "\t"+name+m |
| 158 | exit(0) |
| 159 | |
| 160 | method_name = "" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 161 | property_name = "" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 162 | |
| 163 | sys.argv.pop(0) |
| 164 | objinfo = dbus_objects[sys.argv.pop(0)] |
| 165 | |
| 166 | if (objinfo.has_key('method')): |
| 167 | method_name = objinfo['method'] |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 168 | elif (objinfo.has_key('property')): |
| 169 | property_name = objinfo['property'] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 170 | elif (len(sys.argv)>0): |
| 171 | ## if command line args left and method not specified |
| 172 | ## then next arg must be method name |
| 173 | method_name = sys.argv.pop(0) |
| 174 | |
| 175 | bus_name = objinfo['bus_name'] |
| 176 | obj_path = objinfo['object_name'] |
| 177 | intf_name = objinfo['interface_name'] |
Norman James | 481a5dd | 2016-01-20 10:36:15 -0600 | [diff] [blame] | 178 | obj = bus.get_object(bus_name,obj_path) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 179 | |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 180 | if (method_name != ""): |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 181 | methd = obj.get_dbus_method(method_name,intf_name) |
| 182 | try: |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 183 | data = methd(*sys.argv) |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 184 | fix_byte(data,None,None) |
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 185 | pydata = json.loads(json.dumps(data)) |
| 186 | printDict("",pydata) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 187 | except Exception as e: |
Norman James | 26f7dca | 2015-10-26 12:15:09 -0500 | [diff] [blame] | 188 | print e |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 189 | r = introspect(bus_name,obj_path,intf_name,method_name) |
| 190 | if (r == False): |
| 191 | print "ERROR: Invalid method: "+method_name |
| 192 | else: |
| 193 | print "ERROR: Incorrect arguments passed to method" |
Norman James | c941575 | 2015-11-30 11:55:14 -0600 | [diff] [blame] | 194 | elif (property_name != ""): |
| 195 | intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") |
| 196 | property_value = eval(sys.argv.pop(0)) |
| 197 | intf.Set(intf_name,property_name,property_value) |
| 198 | else: |
| 199 | intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") |
| 200 | props = intf.GetAll(intf_name) |
| 201 | for p in props: |
| 202 | print p+" = "+str(props[p]) |
| 203 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 204 | |
| 205 | |