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