blob: 5ebe70bf933e3a14802f1d4e6782c1a73a2c6e9c [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 Geisslercae038e2016-12-19 15:29:10 -060065 'poweron': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060066 'bus_name': 'xyz.openbmc_project.State.Host',
67 'object_name': '/xyz/openbmc_project/state/host0',
68 'interface_name': 'xyz.openbmc_project.State.Host',
69 'property': 'RequestedHostTransition',
70 'value': 'xyz.openbmc_project.State.Host.Transition.On'
Andrew Geisslercae038e2016-12-19 15:29:10 -060071 },
72 'poweroff': {
Andrew Geissler25b9a852016-12-19 16:14:09 -060073 'bus_name': 'xyz.openbmc_project.State.Host',
74 'object_name': '/xyz/openbmc_project/state/host0',
75 'interface_name': 'xyz.openbmc_project.State.Host',
76 'property': 'RequestedHostTransition',
77 'value': 'xyz.openbmc_project.State.Host.Transition.Off'
Andrew Geisslercae038e2016-12-19 15:29:10 -060078 },
Andrew Geissler8f8cbce2017-02-20 16:10:26 -060079 'bmcstate': {
80 'bus_name': 'xyz.openbmc_project.State.BMC',
81 'object_name': '/xyz/openbmc_project/state/bmc0',
82 'interface_name': 'xyz.openbmc_project.State.BMC',
83 'property': 'CurrentBMCState',
84 },
85 'chassisstate': {
86 'bus_name': 'xyz.openbmc_project.State.Chassis',
87 'object_name': '/xyz/openbmc_project/state/chassis0',
88 'interface_name': 'xyz.openbmc_project.State.Chassis',
89 'property': 'CurrentPowerState',
90 },
91 'hoststate': {
92 'bus_name': 'xyz.openbmc_project.State.Host',
93 'object_name': '/xyz/openbmc_project/state/host0',
94 'interface_name': 'xyz.openbmc_project.State.Host',
95 'property': 'CurrentHostState',
Andrew Geisslercae038e2016-12-19 15:29:10 -060096 },
97 'bootprogress': {
98 'bus_name': 'org.openbmc.Sensors',
99 'object_name': '/org/openbmc/sensors/host/BootProgress',
100 'interface_name': 'org.openbmc.SensorValue'
101 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500102}
103
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600104# Commands that need to run multiple objects above
105multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] }
106
Norman Jamesb4914ad2015-10-26 07:14:29 -0500107bus = dbus.SystemBus()
108
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600109if (len(sys.argv) == 1 or sys.argv[1] == "-h" or
110 (not(dbus_objects.has_key(sys.argv[1])) and
111 (not(multicmd_objects.has_key(sys.argv[1]))))):
Andrew Geisslercae038e2016-12-19 15:29:10 -0600112 print "Usage: obmcutil [command] [[method] [*args]]"
113 print "\tIf [method] is blank, then all properties are printed\n"
114 print "Available commands:"
115 for name in sorted(dbus_objects.keys()):
116 m = ""
117 if (dbus_objects[name].has_key('method') == True):
118 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
119 dbus_objects[name]['method'] + ")"
120 elif (dbus_objects[name].has_key('property') == True):
121 m = " (" + dbus_objects[name]['interface_name'] + "->" + \
122 dbus_objects[name]['property'] + ")"
Norman Jamesb4914ad2015-10-26 07:14:29 -0500123
Andrew Geisslercae038e2016-12-19 15:29:10 -0600124 print "\t" + name + m
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600125 print "Multi-Commands:"
126 for name in sorted(multicmd_objects.keys()):
127 print "\t" + name + " -> " + ",".join(multicmd_objects[name])
Andrew Geisslercae038e2016-12-19 15:29:10 -0600128 exit(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500129
130method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600131property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500132
133sys.argv.pop(0)
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600134cmd = [sys.argv.pop(0)]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500135
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600136# Check if this is a multicmd command and update if it is
137if(multicmd_objects.has_key(cmd[0])):
138 cmd = multicmd_objects[cmd[0]]
Norman Jamesb4914ad2015-10-26 07:14:29 -0500139
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600140for c in cmd:
141 objinfo = dbus_objects[c]
142 if (objinfo.has_key('method')):
143 method_name = objinfo['method']
144 elif (objinfo.has_key('property')):
145 property_name = objinfo['property']
146 elif (len(sys.argv) > 0):
147 ## if command line args left and method not specified
148 ## then next arg must be method name
149 method_name = sys.argv.pop(0)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500150
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600151 bus_name = objinfo['bus_name']
152 obj_path = objinfo['object_name']
153 intf_name = objinfo['interface_name']
154 obj = bus.get_object(bus_name, obj_path)
155
156 if (method_name != ""):
157 methd = obj.get_dbus_method(method_name, intf_name)
158 try:
159 data = methd(*sys.argv)
160 fix_byte(data, None, None)
161 pydata = json.loads(json.dumps(data))
162 if 'transform' in objinfo:
163 objinfo['transform'](pydata)
164 printDict("", pydata)
165 except Exception as e:
166 print e
167 r = introspect(bus_name, obj_path, intf_name, method_name)
168 if (r == False):
169 print "ERROR: Invalid method: " + method_name
170 else:
171 print "ERROR: Incorrect arguments passed to method"
172 elif (property_name != ""):
173 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
174 if (objinfo.has_key('value')):
175 property_value = objinfo['value']
Andrew Geisslercae038e2016-12-19 15:29:10 -0600176 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600177 if(len(sys.argv) > 0):
178 property_value = eval(sys.argv.pop(0))
179 else:
180 property_value = None
181 if(property_value is not None):
182 intf.Set(intf_name, property_name, property_value)
183 else:
184 prop = intf.Get(intf_name,property_name)
185 print '{:<20}'.format(property_name+": ") + str(prop)
Andrew Geissler25b9a852016-12-19 16:14:09 -0600186 else:
Andrew Geissler8f8cbce2017-02-20 16:10:26 -0600187 intf = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
188 props = intf.GetAll(intf_name)
189 for p in props:
Vishwanatha Subbannacf951db2017-03-23 18:46:06 +0530190 print p + " = " + str(props[p])