blob: f762c5906534a709e5e65e9e5482419ac4a200e0 [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
Andrew Geisslercae038e2016-12-19 15:29:10 -06009def fix_byte(it, key, parent):
10 if (isinstance(it, dbus.Array)):
11 for i in range(0, len(it)):
12 fix_byte(it[i], i, it)
13 elif (isinstance(it, dict)):
14 for key in it.keys():
15 fix_byte(it[key], key, it)
16 elif (isinstance(it, dbus.Byte)):
Brad Bishop8e5354e2016-06-24 09:09:29 -040017 if (key != None):
Andrew Geisslercae038e2016-12-19 15:29:10 -060018 parent[key] = int(it)
19 elif (isinstance(it, dbus.Double)):
20 if (key != None):
21 parent[key] = float(it)
22 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
Andrew Geisslercae038e2016-12-19 15:29:10 -060036def printDict(name, data):
37 if (isinstance(data, dict)):
38 print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
39 print name
40 for p in sorted(data.keys()):
41 printDict(p, data[p])
42 else:
43 print name + " = " + str(data)
Norman Jamesb4914ad2015-10-26 07:14:29 -050044
Norman Jamesb4914ad2015-10-26 07:14:29 -050045
Andrew Geisslercae038e2016-12-19 15:29:10 -060046def introspect(bus_name, obj_path, intf_name, method_name):
47 obj = bus.get_object(bus_name, obj_path)
48 introspect_iface = dbus.Interface(obj,
49 "org.freedesktop.DBus.Introspectable")
50 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
51 # print method_name
52 # print introspect_iface.Introspect()
53 root = tree.getroot()
54 found = False
55 for node in root.iter('node'):
56 for intf in node.iter('interface'):
57 if (intf.attrib['name'] == intf_name):
58 for method in intf.iter('method'):
59 if (method.attrib['name'] == method_name):
60 for ar in method.iter('arg'):
61 if (ar.attrib['direction'] == "in"):
62 print "\t" + ar.attrib['name'] + " (" + \
63 ar.attrib['type'] + ")"
64 found = True
65
66 return found
Norman Jamesb4914ad2015-10-26 07:14:29 -050067
68
69dbus_objects = {
Andrew Geisslercae038e2016-12-19 15:29:10 -060070 'power': {
71 'bus_name': 'org.openbmc.control.Power',
72 'object_name': '/org/openbmc/control/power0',
73 'interface_name': 'org.openbmc.control.Power'
74 },
75 'identify_led': {
76 'bus_name': 'org.openbmc.control.led',
77 'object_name': '/org/openbmc/control/led/identify',
78 'interface_name': 'org.openbmc.Led'
79 },
80 'chassis': {
81 'bus_name': 'org.openbmc.control.Chassis',
82 'object_name': '/org/openbmc/control/chassis0',
83 'interface_name': 'org.openbmc.control.Chassis'
84 },
85 'poweron': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060086 'bus_name': 'xyz.openbmc_project.State.Host',
87 'object_name': '/xyz/openbmc_project/state/host0',
88 'interface_name': 'xyz.openbmc_project.State.Host',
89 'property': 'RequestedHostTransition',
90 'value': 'xyz.openbmc_project.State.Host.Transition.On'
Andrew Geisslercae038e2016-12-19 15:29:10 -060091 },
92 'poweroff': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060093 'bus_name': 'xyz.openbmc_project.State.Host',
94 'object_name': '/xyz/openbmc_project/state/host0',
95 'interface_name': 'xyz.openbmc_project.State.Host',
96 'property': 'RequestedHostTransition',
97 'value': 'xyz.openbmc_project.State.Host.Transition.Off'
Andrew Geisslercae038e2016-12-19 15:29:10 -060098 },
Andrew Geissler8f8cbce2017-02-20 16:10:26 -060099 'bmcstate': {
100 'bus_name': 'xyz.openbmc_project.State.BMC',
101 'object_name': '/xyz/openbmc_project/state/bmc0',
102 'interface_name': 'xyz.openbmc_project.State.BMC',
103 'property': 'CurrentBMCState',
104 },
105 'chassisstate': {
106 'bus_name': 'xyz.openbmc_project.State.Chassis',
107 'object_name': '/xyz/openbmc_project/state/chassis0',
108 'interface_name': 'xyz.openbmc_project.State.Chassis',
109 'property': 'CurrentPowerState',
110 },
111 'hoststate': {
112 'bus_name': 'xyz.openbmc_project.State.Host',
113 'object_name': '/xyz/openbmc_project/state/host0',
114 'interface_name': 'xyz.openbmc_project.State.Host',
115 'property': 'CurrentHostState',
Andrew Geisslercae038e2016-12-19 15:29:10 -0600116 },
117 'bootprogress': {
118 'bus_name': 'org.openbmc.Sensors',
119 'object_name': '/org/openbmc/sensors/host/BootProgress',
120 'interface_name': 'org.openbmc.SensorValue'
121 },
122 'biosupdate': {
123 'bus_name': 'org.openbmc.control.Flash',
124 'object_name': '/org/openbmc/control/flash/bios',
125 'interface_name': 'org.openbmc.Flash',
126 'method': 'updateViaTftp',
127 },
128 'biosflash': {
129 'bus_name': 'org.openbmc.control.Flash',
130 'object_name': '/org/openbmc/control/flash/bios',
131 'interface_name': 'org.openbmc.Flash',
132 },
133 'bmcupdate': {
134 'bus_name': 'org.openbmc.control.BmcFlash',
135 'object_name': '/org/openbmc/control/flash/bmc',
136 'interface_name': 'org.openbmc.control.BmcFlash',
137 'method': 'updateViaTftp',
138 },
139 'bmcflash': {
140 'bus_name': 'org.openbmc.control.BmcFlash',
141 'object_name': '/org/openbmc/control/flash/bmc',
142 'interface_name': 'org.openbmc.control.BmcFlash',
143 },
144 'getinventory': {
145 'bus_name': 'org.openbmc.Inventory',
146 'object_name': '/org/openbmc/inventory',
147 'interface_name': 'org.freedesktop.DBus.ObjectManager',
148 'method': 'GetManagedObjects',
149 'transform': merge_interfaces
150 },
151 'getsensors': {
152 'bus_name': 'org.openbmc.Sensors',
153 'object_name': '/org/openbmc/sensors',
154 'interface_name': 'org.freedesktop.DBus.ObjectManager',
155 'method': 'GetManagedObjects',
156 'transform': merge_interfaces
157 },
158 'host': {
159 'bus_name': 'org.openbmc.control.Host',
160 'object_name': '/org/openbmc/control/host0',
161 'interface_name': 'org.openbmc.control.Host',
162 },
163 'setdebugmode': {
164 'bus_name': 'org.openbmc.control.Host',
165 'object_name': '/org/openbmc/control/host0',
166 'interface_name': 'org.openbmc.control.Host',
167 'property': 'debug_mode'
168 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500169}
170
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600171# Commands that need to run multiple objects above
172multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] }
173
Norman Jamesb4914ad2015-10-26 07:14:29 -0500174bus = dbus.SystemBus()
175
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600176if (len(sys.argv) == 1 or sys.argv[1] == "-h" or
177 (not(dbus_objects.has_key(sys.argv[1])) and
178 (not(multicmd_objects.has_key(sys.argv[1]))))):
Andrew Geisslercae038e2016-12-19 15:29:10 -0600179 print "Usage: obmcutil [command] [[method] [*args]]"
180 print "\tIf [method] is blank, then all properties are printed\n"
181 print "Available commands:"
182 for name in sorted(dbus_objects.keys()):
183 m = ""
184 if (dbus_objects[name].has_key('method') == True):
185 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
186 dbus_objects[name]['method'] + ")"
187 elif (dbus_objects[name].has_key('property') == True):
188 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
189 dbus_objects[name]['property'] + ")"
Norman Jamesb4914ad2015-10-26 07:14:29 -0500190
Andrew Geisslercae038e2016-12-19 15:29:10 -0600191 print "\t" + name + m
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600192 print "Multi-Commands:"
193 for name in sorted(multicmd_objects.keys()):
194 print "\t" + name + " -> " + ",".join(multicmd_objects[name])
Andrew Geisslercae038e2016-12-19 15:29:10 -0600195 exit(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500196
197method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600198property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500199
200sys.argv.pop(0)
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600201cmd = [sys.argv.pop(0)]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500202
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600203# Check if this is a multicmd command and update if it is
204if(multicmd_objects.has_key(cmd[0])):
205 cmd = multicmd_objects[cmd[0]]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500206
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600207for c in cmd:
208 objinfo = dbus_objects[c]
209 if (objinfo.has_key('method')):
210 method_name = objinfo['method']
211 elif (objinfo.has_key('property')):
212 property_name = objinfo['property']
213 elif (len(sys.argv) > 0):
214 ## if command line args left and method not specified
215 ## then next arg must be method name
216 method_name = sys.argv.pop(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500217
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600218 bus_name = objinfo['bus_name']
219 obj_path = objinfo['object_name']
220 intf_name = objinfo['interface_name']
221 obj = bus.get_object(bus_name, obj_path)
222
223 if (method_name != ""):
224 methd = obj.get_dbus_method(method_name, intf_name)
225 try:
226 data = methd(*sys.argv)
227 fix_byte(data, None, None)
228 pydata = json.loads(json.dumps(data))
229 if 'transform' in objinfo:
230 objinfo['transform'](pydata)
231 printDict("", pydata)
232 except Exception as e:
233 print e
234 r = introspect(bus_name, obj_path, intf_name, method_name)
235 if (r == False):
236 print "ERROR: Invalid method: " + method_name
237 else:
238 print "ERROR: Incorrect arguments passed to method"
239 elif (property_name != ""):
240 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
241 if (objinfo.has_key('value')):
242 property_value = objinfo['value']
Andrew Geisslercae038e2016-12-19 15:29:10 -0600243 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600244 if(len(sys.argv) > 0):
245 property_value = eval(sys.argv.pop(0))
246 else:
247 property_value = None
248 if(property_value is not None):
249 intf.Set(intf_name, property_name, property_value)
250 else:
251 prop = intf.Get(intf_name,property_name)
252 print '{:<20}'.format(property_name+": ") + str(prop)
Andrew Geissler25b9a852016-12-19 16:14:09 -0600253 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600254 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
255 props = intf.GetAll(intf_name)
256 for p in props:
257 print p + " = " + str(props[p])