blob: bd753caf2f5b847da7465b2d541ff7f178d184d5 [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',
Adriana Kobylak9c751042016-02-09 13:44:32 -060065 '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' : {
Norman James323ed972015-12-09 09:06:37 -060092 'bus_name' : 'org.openbmc.Sensors',
93 'object_name' : '/org/openbmc/sensors/host/BootProgress',
Norman Jamesb4914ad2015-10-26 07:14:29 -050094 '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' : {
Norman James8a65a542016-02-03 19:04:44 -0600108 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600109 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600110 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600111 'method' : 'updateViaTftp',
112 },
113 'bmcflash' : {
Norman James8a65a542016-02-03 19:04:44 -0600114 'bus_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600115 'object_name' : '/org/openbmc/control/flash/bmc',
Norman James8a65a542016-02-03 19:04:44 -0600116 'interface_name' : 'org.openbmc.control.BmcFlash',
Norman James9e9eba12015-11-19 16:05:57 -0600117 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500118 'getinventory' : {
Norman James323ed972015-12-09 09:06:37 -0600119 'bus_name' : 'org.openbmc.Inventory',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500120 'object_name' : '/org/openbmc/inventory',
121 'interface_name' : 'org.openbmc.Object.Enumerate',
122 'method' : 'enumerate'
123 },
124 'getsensors' : {
Norman James323ed972015-12-09 09:06:37 -0600125 'bus_name' : 'org.openbmc.Sensors',
Norman Jamesb4914ad2015-10-26 07:14:29 -0500126 'object_name' : '/org/openbmc/sensors',
127 'interface_name' : 'org.openbmc.Object.Enumerate',
128 'method' : 'enumerate'
129 },
Norman Jamesc9415752015-11-30 11:55:14 -0600130 'host' : {
131 'bus_name' : 'org.openbmc.control.Host',
132 'object_name' : '/org/openbmc/control/host0',
133 'interface_name' : 'org.openbmc.control.Host',
134 },
135 'setdebugmode' : {
136 'bus_name' : 'org.openbmc.control.Host',
137 'object_name' : '/org/openbmc/control/host0',
138 'interface_name' : 'org.openbmc.control.Host',
139 'property' : 'debug_mode'
140 },
Norman Jamesb4914ad2015-10-26 07:14:29 -0500141}
142
143bus = dbus.SystemBus()
144
145
Norman James323ed972015-12-09 09:06:37 -0600146if (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 -0500147 print "Usage: obmcutil [command] [[method] [*args]]"
148 print "\tIf [method] is blank, then all properties are printed\n"
149 print "Available commands:"
Norman James9e9eba12015-11-19 16:05:57 -0600150 for name in sorted(dbus_objects.keys()):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500151 m = ""
152 if (dbus_objects[name].has_key('method') == True):
153 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")"
Norman Jamesc9415752015-11-30 11:55:14 -0600154 elif (dbus_objects[name].has_key('property') == True):
155 m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")"
156
Norman Jamesb4914ad2015-10-26 07:14:29 -0500157 print "\t"+name+m
158 exit(0)
159
160method_name = ""
Norman Jamesc9415752015-11-30 11:55:14 -0600161property_name = ""
Norman Jamesb4914ad2015-10-26 07:14:29 -0500162
163sys.argv.pop(0)
164objinfo = dbus_objects[sys.argv.pop(0)]
165
166if (objinfo.has_key('method')):
167 method_name = objinfo['method']
Norman Jamesc9415752015-11-30 11:55:14 -0600168elif (objinfo.has_key('property')):
169 property_name = objinfo['property']
Norman Jamesb4914ad2015-10-26 07:14:29 -0500170elif (len(sys.argv)>0):
171 ## if command line args left and method not specified
172 ## then next arg must be method name
173 method_name = sys.argv.pop(0)
174
175bus_name = objinfo['bus_name']
176obj_path = objinfo['object_name']
177intf_name = objinfo['interface_name']
Norman James481a5dd2016-01-20 10:36:15 -0600178obj = bus.get_object(bus_name,obj_path)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500179
Norman Jamesc9415752015-11-30 11:55:14 -0600180if (method_name != ""):
Norman Jamesb4914ad2015-10-26 07:14:29 -0500181 methd = obj.get_dbus_method(method_name,intf_name)
182 try:
Norman James9e9eba12015-11-19 16:05:57 -0600183 data = methd(*sys.argv)
Norman Jamesc9415752015-11-30 11:55:14 -0600184 fix_byte(data,None,None)
Norman James9e9eba12015-11-19 16:05:57 -0600185 pydata = json.loads(json.dumps(data))
186 printDict("",pydata)
Norman Jamesb4914ad2015-10-26 07:14:29 -0500187 except Exception as e:
Norman James26f7dca2015-10-26 12:15:09 -0500188 print e
Norman Jamesb4914ad2015-10-26 07:14:29 -0500189 r = introspect(bus_name,obj_path,intf_name,method_name)
190 if (r == False):
191 print "ERROR: Invalid method: "+method_name
192 else:
193 print "ERROR: Incorrect arguments passed to method"
Norman Jamesc9415752015-11-30 11:55:14 -0600194elif (property_name != ""):
195 intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties")
196 property_value = eval(sys.argv.pop(0))
197 intf.Set(intf_name,property_name,property_value)
198else:
199 intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties")
200 props = intf.GetAll(intf_name)
201 for p in props:
202 print p+" = "+str(props[p])
203
Norman Jamesb4914ad2015-10-26 07:14:29 -0500204
205