blob: 9cfeffb01e35c53741df3143ba045ec07c55c522 [file] [log] [blame]
Norman Jamesce46e3e2015-08-30 22:25:55 -05001import dbus
Norman James9e6acf92015-09-08 07:00:04 -05002import xml.etree.ElementTree as ET
Norman Jamesce46e3e2015-08-30 22:25:55 -05003
4BUS_PREFIX = 'org.openbmc'
Norman James362a80f2015-09-14 14:04:39 -05005OBJ_PREFIX = "/org/openbmc"
Norman Jamesce46e3e2015-08-30 22:25:55 -05006GPIO_DEV = '/sys/class/gpio'
7
8
9FRU_TYPES = {
Norman James90baede2015-09-02 20:32:49 -050010 'CPU' : 1,
11 'DIMM' : 2,
12 'BACKPLANE' : 3,
13 'RISER_CARD' : 4,
14 'FAN' : 5
Norman Jamesce46e3e2015-08-30 22:25:55 -050015}
Norman James362a80f2015-09-14 14:04:39 -050016FRU_STATES = {
17 'NORMAL' : 0,
18 'RECOVERABLE_ERROR' : 1,
19 'FATAL_ERROR' : 2,
20 'NOT_PRESENT' : 3,
21}
Norman James32e74e22015-09-15 21:28:06 -050022
Norman James32e74e22015-09-15 21:28:06 -050023
Norman Jamesce46e3e2015-08-30 22:25:55 -050024ENUMS = {
25 'org.openbmc.SensorIntegerThreshold.state' :
26 ['NOT_SET','NORMAL','LOWER_CRITICAL','LOWER_WARNING','UPPER_WARNING','UPPER_CRITICAL'],
Norman James362a80f2015-09-14 14:04:39 -050027}
28
29DBUS_TO_BASE_TYPES = {
30 'dbus.Byte' : 'int',
31 'dbus.Double' : 'float',
32 'dbus.Int32' : 'int',
33 'dbus.UInt32' : 'long',
34 'dbus.String' : 'str',
35 'dbus.UInt64' : 'long',
36 'dbus.Boolean' : 'bool',
37}
38
39BASE_TO_DBUS_TYPES = {
40 'int' : 'dbus.Int32',
41 'float' : 'dbus.Double',
42 'str' : 'dbus.String',
43 'long' : 'dbus.Int64',
44 'bool' : 'dbus.Boolean'
Norman Jamesce46e3e2015-08-30 22:25:55 -050045}
Norman James90baede2015-09-02 20:32:49 -050046
Norman James5d78b4d2015-09-05 13:34:34 -050047
Norman James362a80f2015-09-14 14:04:39 -050048def getManagerInterface(bus,manager):
49 bus_name = "org.openbmc.managers."+manager
50 obj_name = "/org/openbmc/managers/"+manager
51 obj = bus.get_object(bus_name,obj_name)
52 return dbus.Interface(obj,bus_name)
Norman James5d78b4d2015-09-05 13:34:34 -050053
Norman James5d78b4d2015-09-05 13:34:34 -050054
Norman James362a80f2015-09-14 14:04:39 -050055def get_objs(bus,bus_name,path,objects):
56 obj = bus.get_object(bus_name,path)
Norman James9e6acf92015-09-08 07:00:04 -050057 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
Norman James362a80f2015-09-14 14:04:39 -050058 #print introspect_iface.Introspect()
Norman James9e6acf92015-09-08 07:00:04 -050059 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
60 root = tree.getroot()
Norman James362a80f2015-09-14 14:04:39 -050061 for node in root.iter('node'):
62 if (node.attrib.has_key('name') == False):
63 for intf in node.iter('interface'):
64 intf_name = intf.attrib['name']
65 if (intf_name.find(BUS_PREFIX)==0):
66 parts=path.split('/')
67 instance = parts[len(parts)-1]
68 if (objects.has_key(instance) == False):
69 objects[instance] = {}
70 objects[instance]['PATH'] = path
71 objects[instance]['INIT'] = []
72 for method in intf.iter('method'):
73 if (method.attrib['name'] == "init"):
74 objects[instance]['INIT'].append(intf_name)
75
76 else:
77 node_name = node.attrib['name']
78 if (node_name != path):
79 get_objs(bus,bus_name,path+"/"+node.attrib['name'],objects)
80
81
82
83#def get_methods(obj):
84# methods = {}
85# introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
86 # tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
87 # root = tree.getroot()
88# for intf in root.iter('interface'):
89 # intf_name = intf.attrib['name']
90# if (intf_name.find(BUS_PREFIX)==0):
91# methods[intf_name] = {}
92# for method in intf.iter('method'):
93# methods[intf_name][method.attrib['name']] = True
94#
95# return methods
Norman James5d78b4d2015-09-05 13:34:34 -050096
Norman James90baede2015-09-02 20:32:49 -050097class DbusProperty:
98 def __init__(self,name,value):
Norman James90baede2015-09-02 20:32:49 -050099 self.name = str(name)
100 self.dbusType = str(type(value)).split("'")[1]
Norman James362a80f2015-09-14 14:04:39 -0500101 self.variant_level = 2
Norman James90baede2015-09-02 20:32:49 -0500102 self.value = None
Norman James362a80f2015-09-14 14:04:39 -0500103 if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False):
104 self.variant_level = value.variant_level
105 try:
106 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
107 except:
108 raise Exception("Unknown dbus type: "+self.dbusType)
109 else:
110 self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType]
111 self.value = value
Norman James90baede2015-09-02 20:32:49 -0500112
Norman James5d78b4d2015-09-05 13:34:34 -0500113 def setValue(self,value):
Norman James90baede2015-09-02 20:32:49 -0500114 try:
Norman James362a80f2015-09-14 14:04:39 -0500115 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
Norman James90baede2015-09-02 20:32:49 -0500116 except:
117 raise Exception("Unknown dbus type: "+self.dbusType)
118
Norman James5d78b4d2015-09-05 13:34:34 -0500119 def setVariant(self,variant_level):
120 self.variant_level = variant_level
Norman James90baede2015-09-02 20:32:49 -0500121
122 def getName(self):
123 return self.name
Norman James5d78b4d2015-09-05 13:34:34 -0500124
Norman James90baede2015-09-02 20:32:49 -0500125 def getValue(self):
Norman James5d78b4d2015-09-05 13:34:34 -0500126 e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")"
Norman James90baede2015-09-02 20:32:49 -0500127 return eval(e)
128
Norman James362a80f2015-09-14 14:04:39 -0500129 def getBaseValue(self):
130 return self.value
Norman James90baede2015-09-02 20:32:49 -0500131
132 def __str__(self):
133 return self.dbusType+":"+str(self.value)