blob: 9207eca55a0e6bf59204e15e766f4cf13d3ea07c [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)
Brad Bishop8e5354e2016-06-24 09:09:29 -040019 elif (isinstance(it,dbus.Double)):
20 if (key != None):
21 parent[key] = float(it)
Norman James8cda6de2015-11-17 19:35:45 -060022 else:
23 pass
Brad Bishop38da62a2016-06-21 21:39:57 -040024
25
26def 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 Jamesb4914ad2015-10-26 07:14:29 -050035
36def printDict(name,data):
Norman James8cda6de2015-11-17 19:35:45 -060037 if (isinstance(data, dict)):
Norman Jamesb4914ad2015-10-26 07:14:29 -050038 print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
39 print name
Norman James9e9eba12015-11-19 16:05:57 -060040 for p in sorted(data.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -050041 printDict(p,data[p])
42 else:
43 print name+" = "+str(data)
44
45def 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
66dbus_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 Kobylak9c751042016-02-09 13:44:32 -060074 'object_name' : '/org/openbmc/control/led/identify',
Norman Jamesb4914ad2015-10-26 07:14:29 -050075 '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 James9e9eba12015-11-19 16:05:57 -060094 'state' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -050095 '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 James323ed972015-12-09 09:06:37 -0600101 'bus_name' : 'org.openbmc.Sensors',
102 'object_name' : '/org/openbmc/sensors/host/BootProgress',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500103 'interface_name' : 'org.openbmc.SensorValue'
104 },
Norman James9e9eba12015-11-19 16:05:57 -0600105 'biosupdate' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -0500106 '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 James9e9eba12015-11-19 16:05:57 -0600111 'biosflash' : {
Norman Jamesfacbca42015-11-09 15:58:14 -0600112 'bus_name' : 'org.openbmc.control.Flash',
113 'object_name' : '/org/openbmc/control/flash/bios',
114 'interface_name' : 'org.openbmc.Flash',
115 },
Norman James9e9eba12015-11-19 16:05:57 -0600116 'bmcupdate' : {
Norman James8a65a542016-02-03 19:04:44 -0600117 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600118 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600119 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600120 'method' : 'updateViaTftp',
121 },
122 'bmcflash' : {
Norman James8a65a542016-02-03 19:04:44 -0600123 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600124 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600125 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600126 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500127 'getinventory' : {
Norman James323ed972015-12-09 09:06:37 -0600128 'bus_name' : 'org.openbmc.Inventory',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500129 'object_name' : '/org/openbmc/inventory',
Brad Bishop38da62a2016-06-21 21:39:57 -0400130 'interface_name' : 'org.freedesktop.DBus.ObjectManager',
131 'method' : 'GetManagedObjects',
132 'transform' : merge_interfaces
Norman Jamesb4914ad2015-10-26 07:14:29 -0500133 },
134 'getsensors' : {
Norman James323ed972015-12-09 09:06:37 -0600135 'bus_name' : 'org.openbmc.Sensors',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500136 'object_name' : '/org/openbmc/sensors',
Brad Bishop38da62a2016-06-21 21:39:57 -0400137 'interface_name' : 'org.freedesktop.DBus.ObjectManager',
138 'method' : 'GetManagedObjects',
139 'transform' : merge_interfaces
Norman Jamesb4914ad2015-10-26 07:14:29 -0500140 },
Norman Jamesc9415752015-11-30 11:55:14 -0600141 '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 Jamesb4914ad2015-10-26 07:14:29 -0500152}
153
154bus = dbus.SystemBus()
155
156
Norman James323ed972015-12-09 09:06:37 -0600157if (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 -0500158 print "Usage: obmcutil [command] [[method] [*args]]"
159 print "\tIf [method] is blank, then all properties are printed\n"
160 print "Available commands:"
Norman James9e9eba12015-11-19 16:05:57 -0600161 for name in sorted(dbus_objects.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500162 m = ""
163 if (dbus_objects[name].has_key('method') == True):
164 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")"
Norman Jamesc9415752015-11-30 11:55:14 -0600165 elif (dbus_objects[name].has_key('property') == True):
166 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")"
167
Norman Jamesb4914ad2015-10-26 07:14:29 -0500168 print "\t"+name+m
169 exit(0)
170
171method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600172property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500173
174sys.argv.pop(0)
175objinfo = dbus_objects[sys.argv.pop(0)]
176
177if (objinfo.has_key('method')):
178 method_name = objinfo['method']
Norman Jamesc9415752015-11-30 11:55:14 -0600179elif (objinfo.has_key('property')):
180 property_name = objinfo['property']
Norman Jamesb4914ad2015-10-26 07:14:29 -0500181elif (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
186bus_name = objinfo['bus_name']
187obj_path = objinfo['object_name']
188intf_name = objinfo['interface_name']
Norman James481a5dd2016-01-20 10:36:15 -0600189obj = bus.get_object(bus_name,obj_path)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500190
Norman Jamesc9415752015-11-30 11:55:14 -0600191if (method_name != ""):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500192 methd = obj.get_dbus_method(method_name,intf_name)
193 try:
Norman James9e9eba12015-11-19 16:05:57 -0600194 data = methd(*sys.argv)
Norman Jamesc9415752015-11-30 11:55:14 -0600195 fix_byte(data,None,None)
Norman James9e9eba12015-11-19 16:05:57 -0600196 pydata = json.loads(json.dumps(data))
Brad Bishop38da62a2016-06-21 21:39:57 -0400197 if 'transform' in objinfo:
198 objinfo['transform'](pydata)
Norman James9e9eba12015-11-19 16:05:57 -0600199 printDict("",pydata)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500200 except Exception as e:
Norman James26f7dca2015-10-26 12:15:09 -0500201 print e
Norman Jamesb4914ad2015-10-26 07:14:29 -0500202 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 Jamesc9415752015-11-30 11:55:14 -0600207elif (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)
211else:
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 Jamesb4914ad2015-10-26 07:14:29 -0500217
218