blob: 408bab0a3520642dabff71355c905e3baacab7df [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
Andrew Geisslercae038e2016-12-19 15:29:10 -060026def printDict(name, data):
27 if (isinstance(data, dict)):
28 print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
29 print name
30 for p in sorted(data.keys()):
31 printDict(p, data[p])
32 else:
33 print name + " = " + str(data)
Norman Jamesb4914ad2015-10-26 07:14:29 -050034
Norman Jamesb4914ad2015-10-26 07:14:29 -050035
Andrew Geisslercae038e2016-12-19 15:29:10 -060036def introspect(bus_name, obj_path, intf_name, method_name):
37 obj = bus.get_object(bus_name, obj_path)
38 introspect_iface = dbus.Interface(obj,
39 "org.freedesktop.DBus.Introspectable")
40 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
41 # print method_name
42 # print introspect_iface.Introspect()
43 root = tree.getroot()
44 found = False
45 for node in root.iter('node'):
46 for intf in node.iter('interface'):
47 if (intf.attrib['name'] == intf_name):
48 for method in intf.iter('method'):
49 if (method.attrib['name'] == method_name):
50 for ar in method.iter('arg'):
51 if (ar.attrib['direction'] == "in"):
52 print "\t" + ar.attrib['name'] + " (" + \
53 ar.attrib['type'] + ")"
54 found = True
55
56 return found
Norman Jamesb4914ad2015-10-26 07:14:29 -050057
58
59dbus_objects = {
Andrew Geisslercae038e2016-12-19 15:29:10 -060060 'power': {
61 'bus_name': 'org.openbmc.control.Power',
62 'object_name': '/org/openbmc/control/power0',
63 'interface_name': 'org.openbmc.control.Power'
64 },
Andrew Geissler2cbbb672017-05-17 10:52:49 -050065 'chassison': {
66 'bus_name': 'xyz.openbmc_project.State.Chassis',
67 'object_name': '/xyz/openbmc_project/state/chassis0',
68 'interface_name': 'xyz.openbmc_project.State.Chassis',
69 'property': 'RequestedPowerTransition',
70 'value': 'xyz.openbmc_project.State.Chassis.Transition.On'
71 },
72 'chassisoff': {
73 'bus_name': 'xyz.openbmc_project.State.Chassis',
74 'object_name': '/xyz/openbmc_project/state/chassis0',
75 'interface_name': 'xyz.openbmc_project.State.Chassis',
76 'property': 'RequestedPowerTransition',
77 'value': 'xyz.openbmc_project.State.Chassis.Transition.Off'
78 },
Andrew Geisslercae038e2016-12-19 15:29:10 -060079 'poweron': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060080 'bus_name': 'xyz.openbmc_project.State.Host',
81 'object_name': '/xyz/openbmc_project/state/host0',
82 'interface_name': 'xyz.openbmc_project.State.Host',
83 'property': 'RequestedHostTransition',
84 'value': 'xyz.openbmc_project.State.Host.Transition.On'
Andrew Geisslercae038e2016-12-19 15:29:10 -060085 },
86 'poweroff': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060087 'bus_name': 'xyz.openbmc_project.State.Host',
88 'object_name': '/xyz/openbmc_project/state/host0',
89 'interface_name': 'xyz.openbmc_project.State.Host',
90 'property': 'RequestedHostTransition',
91 'value': 'xyz.openbmc_project.State.Host.Transition.Off'
Andrew Geisslercae038e2016-12-19 15:29:10 -060092 },
Andrew Geissler8f8cbce2017-02-20 16:10:26 -060093 'bmcstate': {
94 'bus_name': 'xyz.openbmc_project.State.BMC',
95 'object_name': '/xyz/openbmc_project/state/bmc0',
96 'interface_name': 'xyz.openbmc_project.State.BMC',
97 'property': 'CurrentBMCState',
98 },
99 'chassisstate': {
100 'bus_name': 'xyz.openbmc_project.State.Chassis',
101 'object_name': '/xyz/openbmc_project/state/chassis0',
102 'interface_name': 'xyz.openbmc_project.State.Chassis',
103 'property': 'CurrentPowerState',
104 },
105 'hoststate': {
106 'bus_name': 'xyz.openbmc_project.State.Host',
107 'object_name': '/xyz/openbmc_project/state/host0',
108 'interface_name': 'xyz.openbmc_project.State.Host',
109 'property': 'CurrentHostState',
Andrew Geisslercae038e2016-12-19 15:29:10 -0600110 },
111 'bootprogress': {
George Keishing3be09952017-08-18 09:48:03 -0500112 'bus_name': 'xyz.openbmc_project.State.Host',
113 'object_name': '/xyz/openbmc_project/state/host0',
114 'interface_name': 'xyz.openbmc_project.State.Boot.Progress',
115 'property': 'BootProgress',
Andrew Geisslercae038e2016-12-19 15:29:10 -0600116 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500117}
118
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600119# Commands that need to run multiple objects above
120multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] }
121
Norman Jamesb4914ad2015-10-26 07:14:29 -0500122bus = dbus.SystemBus()
123
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600124if (len(sys.argv) == 1 or sys.argv[1] == "-h" or
125 (not(dbus_objects.has_key(sys.argv[1])) and
126 (not(multicmd_objects.has_key(sys.argv[1]))))):
Andrew Geisslercae038e2016-12-19 15:29:10 -0600127 print "Usage: obmcutil [command] [[method] [*args]]"
128 print "\tIf [method] is blank, then all properties are printed\n"
129 print "Available commands:"
130 for name in sorted(dbus_objects.keys()):
131 m = ""
132 if (dbus_objects[name].has_key('method') == True):
133 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
134 dbus_objects[name]['method'] + ")"
135 elif (dbus_objects[name].has_key('property') == True):
136 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
137 dbus_objects[name]['property'] + ")"
Norman Jamesb4914ad2015-10-26 07:14:29 -0500138
Andrew Geisslercae038e2016-12-19 15:29:10 -0600139 print "\t" + name + m
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600140 print "Multi-Commands:"
141 for name in sorted(multicmd_objects.keys()):
142 print "\t" + name + " -> " + ",".join(multicmd_objects[name])
Andrew Geisslercae038e2016-12-19 15:29:10 -0600143 exit(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500144
145method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600146property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500147
148sys.argv.pop(0)
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600149cmd = [sys.argv.pop(0)]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500150
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600151# Check if this is a multicmd command and update if it is
152if(multicmd_objects.has_key(cmd[0])):
153 cmd = multicmd_objects[cmd[0]]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500154
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600155for c in cmd:
156 objinfo = dbus_objects[c]
157 if (objinfo.has_key('method')):
158 method_name = objinfo['method']
159 elif (objinfo.has_key('property')):
160 property_name = objinfo['property']
161 elif (len(sys.argv) > 0):
162 ## if command line args left and method not specified
163 ## then next arg must be method name
164 method_name = sys.argv.pop(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500165
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600166 bus_name = objinfo['bus_name']
167 obj_path = objinfo['object_name']
168 intf_name = objinfo['interface_name']
169 obj = bus.get_object(bus_name, obj_path)
170
171 if (method_name != ""):
172 methd = obj.get_dbus_method(method_name, intf_name)
173 try:
174 data = methd(*sys.argv)
175 fix_byte(data, None, None)
176 pydata = json.loads(json.dumps(data))
177 if 'transform' in objinfo:
178 objinfo['transform'](pydata)
179 printDict("", pydata)
180 except Exception as e:
181 print e
182 r = introspect(bus_name, obj_path, intf_name, method_name)
183 if (r == False):
184 print "ERROR: Invalid method: " + method_name
185 else:
186 print "ERROR: Incorrect arguments passed to method"
187 elif (property_name != ""):
188 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
189 if (objinfo.has_key('value')):
190 property_value = objinfo['value']
Andrew Geisslercae038e2016-12-19 15:29:10 -0600191 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600192 if(len(sys.argv) > 0):
193 property_value = eval(sys.argv.pop(0))
194 else:
195 property_value = None
196 if(property_value is not None):
197 intf.Set(intf_name, property_name, property_value)
198 else:
199 prop = intf.Get(intf_name,property_name)
200 print '{:<20}'.format(property_name+": ") + str(prop)
Andrew Geissler25b9a852016-12-19 16:14:09 -0600201 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600202 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
203 props = intf.GetAll(intf_name)
204 for p in props:
Vishwanatha Subbannacf951db2017-03-23 18:46:06 +0530205 print p + " = " + str(props[p])