blob: 35a9a087b199a6533fb74f63c347c380f8db65f2 [file] [log] [blame]
Norman Jamesb4914ad2015-10-26 07:14:29 -05001#!/usr/bin/python
2
3import sys
Norman Jamesfacbca42015-11-09 15:58:14 -06004#import subprocess
Norman Jamesb4914ad2015-10-26 07:14:29 -05005import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import xml.etree.ElementTree as ET
Norman Jamesfacbca42015-11-09 15:58:14 -060010import json
Norman Jamesb4914ad2015-10-26 07:14:29 -050011
Norman James8cda6de2015-11-17 19:35:45 -060012
13def fix_byte(it,key,parent):
14 if (isinstance(it,dbus.Array)):
15 for i in range(0,len(it)):
Norman James9e9eba12015-11-19 16:05:57 -060016 fix_byte(it[i],i,it)
Norman James8cda6de2015-11-17 19:35:45 -060017 elif (isinstance(it, dict)):
18 for key in it.keys():
Norman James9e9eba12015-11-19 16:05:57 -060019 fix_byte(it[key],key,it)
Norman James8cda6de2015-11-17 19:35:45 -060020 elif (isinstance(it,dbus.Byte)):
21 if (key != None):
22 parent[key] = int(it)
23 else:
24 pass
25
Norman Jamesb4914ad2015-10-26 07:14:29 -050026
27def printDict(name,data):
Norman James8cda6de2015-11-17 19:35:45 -060028 if (isinstance(data, dict)):
Norman Jamesb4914ad2015-10-26 07:14:29 -050029 print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
30 print name
Norman James9e9eba12015-11-19 16:05:57 -060031 for p in sorted(data.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -050032 printDict(p,data[p])
33 else:
34 print name+" = "+str(data)
35
36def 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
57dbus_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 Jamesa23e7b82015-10-28 19:00:07 -050065 'object_name' : '/org/openbmc/control/led/IDENTIFY',
Norman Jamesb4914ad2015-10-26 07:14:29 -050066 '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 James9e9eba12015-11-19 16:05:57 -060085 'state' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -050086 '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 James9e9eba12015-11-19 16:05:57 -060096 'biosupdate' : {
Norman Jamesb4914ad2015-10-26 07:14:29 -050097 '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 James9e9eba12015-11-19 16:05:57 -0600102 'biosflash' : {
Norman Jamesfacbca42015-11-09 15:58:14 -0600103 'bus_name' : 'org.openbmc.control.Flash',
104 'object_name' : '/org/openbmc/control/flash/bios',
105 'interface_name' : 'org.openbmc.Flash',
106 },
Norman James9e9eba12015-11-19 16:05:57 -0600107 '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 Jamesb4914ad2015-10-26 07:14:29 -0500118 '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 Jamesb4914ad2015-10-26 07:14:29 -0500130}
131
132bus = dbus.SystemBus()
133
134
Norman James9e9eba12015-11-19 16:05:57 -0600135if (len(sys.argv) == 1 or sys.argv[1] == "-h"):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500136 print "Usage: obmcutil [command] [[method] [*args]]"
137 print "\tIf [method] is blank, then all properties are printed\n"
138 print "Available commands:"
Norman James9e9eba12015-11-19 16:05:57 -0600139 for name in sorted(dbus_objects.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500140 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
146method_name = ""
147
148sys.argv.pop(0)
149objinfo = dbus_objects[sys.argv.pop(0)]
150
151if (objinfo.has_key('method')):
152 method_name = objinfo['method']
153elif (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
158bus_name = objinfo['bus_name']
159obj_path = objinfo['object_name']
160intf_name = objinfo['interface_name']
161obj = bus.get_object(bus_name,obj_path)
162
163if (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
169else:
170 methd = obj.get_dbus_method(method_name,intf_name)
171 try:
Norman James9e9eba12015-11-19 16:05:57 -0600172 data = methd(*sys.argv)
173 fix_byte(data,None,None)
174 pydata = json.loads(json.dumps(data))
175 printDict("",pydata)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500176 except Exception as e:
Norman James26f7dca2015-10-26 12:15:09 -0500177 print e
Norman Jamesb4914ad2015-10-26 07:14:29 -0500178 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