blob: 6db589e47da02c837218a66170e90c0b544e23b3 [file] [log] [blame]
Norman Jamesb4914ad2015-10-26 07:14:29 -05001#!/usr/bin/python
2
3import sys
Norman Jamesb4914ad2015-10-26 07:14:29 -05004import dbus
Norman Jamesfacbca42015-11-09 15:58:14 -06005import json
Brad Bishop3cec2962016-06-21 20:51:54 -04006import xml.etree.ElementTree as ET
Norman Jamesb4914ad2015-10-26 07:14:29 -05007
Norman James8cda6de2015-11-17 19:35:45 -06008
9def fix_byte(it,key,parent):
10 if (isinstance(it,dbus.Array)):
11 for i in range(0,len(it)):
Norman James9e9eba12015-11-19 16:05:57 -060012 fix_byte(it[i],i,it)
Norman James8cda6de2015-11-17 19:35:45 -060013 elif (isinstance(it, dict)):
14 for key in it.keys():
Norman James9e9eba12015-11-19 16:05:57 -060015 fix_byte(it[key],key,it)
Norman James8cda6de2015-11-17 19:35:45 -060016 elif (isinstance(it,dbus.Byte)):
17 if (key != None):
18 parent[key] = int(it)
19 else:
20 pass
Brad Bishop38da62a2016-06-21 21:39:57 -040021
22
23def 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 Jamesb4914ad2015-10-26 07:14:29 -050032
33def printDict(name,data):
Norman James8cda6de2015-11-17 19:35:45 -060034 if (isinstance(data, dict)):
Norman Jamesb4914ad2015-10-26 07:14:29 -050035 print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
36 print name
Norman James9e9eba12015-11-19 16:05:57 -060037 for p in sorted(data.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -050038 printDict(p,data[p])
39 else:
40 print name+" = "+str(data)
41
42def 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
63dbus_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 Kobylak9c751042016-02-09 13:44:32 -060071 'object_name' : '/org/openbmc/control/led/identify',
Norman Jamesb4914ad2015-10-26 07:14:29 -050072 '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 James9e9eba12015-11-19 16:05:57 -060091 'state' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -050092 '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 James323ed972015-12-09 09:06:37 -060098 'bus_name' : 'org.openbmc.Sensors',
99 'object_name' : '/org/openbmc/sensors/host/BootProgress',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500100 'interface_name' : 'org.openbmc.SensorValue'
101 },
Norman James9e9eba12015-11-19 16:05:57 -0600102 'biosupdate' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -0500103 '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 James9e9eba12015-11-19 16:05:57 -0600108 'biosflash' : {
Norman Jamesfacbca42015-11-09 15:58:14 -0600109 'bus_name' : 'org.openbmc.control.Flash',
110 'object_name' : '/org/openbmc/control/flash/bios',
111 'interface_name' : 'org.openbmc.Flash',
112 },
Norman James9e9eba12015-11-19 16:05:57 -0600113 'bmcupdate' : {
Norman James8a65a542016-02-03 19:04:44 -0600114 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600115 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600116 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600117 'method' : 'updateViaTftp',
118 },
119 'bmcflash' : {
Norman James8a65a542016-02-03 19:04:44 -0600120 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600121 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600122 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600123 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500124 'getinventory' : {
Norman James323ed972015-12-09 09:06:37 -0600125 'bus_name' : 'org.openbmc.Inventory',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500126 'object_name' : '/org/openbmc/inventory',
Brad Bishop38da62a2016-06-21 21:39:57 -0400127 'interface_name' : 'org.freedesktop.DBus.ObjectManager',
128 'method' : 'GetManagedObjects',
129 'transform' : merge_interfaces
Norman Jamesb4914ad2015-10-26 07:14:29 -0500130 },
131 'getsensors' : {
Norman James323ed972015-12-09 09:06:37 -0600132 'bus_name' : 'org.openbmc.Sensors',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500133 'object_name' : '/org/openbmc/sensors',
Brad Bishop38da62a2016-06-21 21:39:57 -0400134 'interface_name' : 'org.freedesktop.DBus.ObjectManager',
135 'method' : 'GetManagedObjects',
136 'transform' : merge_interfaces
Norman Jamesb4914ad2015-10-26 07:14:29 -0500137 },
Norman Jamesc9415752015-11-30 11:55:14 -0600138 '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 Jamesb4914ad2015-10-26 07:14:29 -0500149}
150
151bus = dbus.SystemBus()
152
153
Norman James323ed972015-12-09 09:06:37 -0600154if (len(sys.argv) == 1 or sys.argv[1] == "-h" or dbus_objects.has_key(sys.argv[1])==False):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500155 print "Usage: obmcutil [command] [[method] [*args]]"
156 print "\tIf [method] is blank, then all properties are printed\n"
157 print "Available commands:"
Norman James9e9eba12015-11-19 16:05:57 -0600158 for name in sorted(dbus_objects.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500159 m = ""
160 if (dbus_objects[name].has_key('method') == True):
161 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")"
Norman Jamesc9415752015-11-30 11:55:14 -0600162 elif (dbus_objects[name].has_key('property') == True):
163 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")"
164
Norman Jamesb4914ad2015-10-26 07:14:29 -0500165 print "\t"+name+m
166 exit(0)
167
168method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600169property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500170
171sys.argv.pop(0)
172objinfo = dbus_objects[sys.argv.pop(0)]
173
174if (objinfo.has_key('method')):
175 method_name = objinfo['method']
Norman Jamesc9415752015-11-30 11:55:14 -0600176elif (objinfo.has_key('property')):
177 property_name = objinfo['property']
Norman Jamesb4914ad2015-10-26 07:14:29 -0500178elif (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
183bus_name = objinfo['bus_name']
184obj_path = objinfo['object_name']
185intf_name = objinfo['interface_name']
Norman James481a5dd2016-01-20 10:36:15 -0600186obj = bus.get_object(bus_name,obj_path)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500187
Norman Jamesc9415752015-11-30 11:55:14 -0600188if (method_name != ""):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500189 methd = obj.get_dbus_method(method_name,intf_name)
190 try:
Norman James9e9eba12015-11-19 16:05:57 -0600191 data = methd(*sys.argv)
Norman Jamesc9415752015-11-30 11:55:14 -0600192 fix_byte(data,None,None)
Norman James9e9eba12015-11-19 16:05:57 -0600193 pydata = json.loads(json.dumps(data))
Brad Bishop38da62a2016-06-21 21:39:57 -0400194 if 'transform' in objinfo:
195 objinfo['transform'](pydata)
Norman James9e9eba12015-11-19 16:05:57 -0600196 printDict("",pydata)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500197 except Exception as e:
Norman James26f7dca2015-10-26 12:15:09 -0500198 print e
Norman Jamesb4914ad2015-10-26 07:14:29 -0500199 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 Jamesc9415752015-11-30 11:55:14 -0600204elif (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)
208else:
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 Jamesb4914ad2015-10-26 07:14:29 -0500214
215