blob: c81209fcd4700fe21c49f8cdd94e1d38515767c6 [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'
Norman James5e792e32015-10-07 17:36:17 -05007BUS = "system"
Norman Jamesce46e3e2015-08-30 22:25:55 -05008
9FRU_TYPES = {
Norman James88872672015-09-21 16:51:35 -050010 'SYSTEM' : 0,
Norman James90baede2015-09-02 20:32:49 -050011 'CPU' : 1,
12 'DIMM' : 2,
Norman James88872672015-09-21 16:51:35 -050013 'MAIN_PLANAR' : 3,
Norman James90baede2015-09-02 20:32:49 -050014 'RISER_CARD' : 4,
Norman James88872672015-09-21 16:51:35 -050015 'FAN' : 5,
16 'BMC' : 6,
17 'CORE' : 7,
Norman James19e45912015-10-04 20:19:41 -050018 'PCIE_CARD' : 8,
Norman Jamesce46e3e2015-08-30 22:25:55 -050019}
Norman James362a80f2015-09-14 14:04:39 -050020FRU_STATES = {
21 'NORMAL' : 0,
22 'RECOVERABLE_ERROR' : 1,
23 'FATAL_ERROR' : 2,
24 'NOT_PRESENT' : 3,
25}
Norman James32e74e22015-09-15 21:28:06 -050026
Norman James32e74e22015-09-15 21:28:06 -050027
Norman Jamesce46e3e2015-08-30 22:25:55 -050028ENUMS = {
29 'org.openbmc.SensorIntegerThreshold.state' :
30 ['NOT_SET','NORMAL','LOWER_CRITICAL','LOWER_WARNING','UPPER_WARNING','UPPER_CRITICAL'],
Norman James362a80f2015-09-14 14:04:39 -050031}
32
33DBUS_TO_BASE_TYPES = {
34 'dbus.Byte' : 'int',
35 'dbus.Double' : 'float',
36 'dbus.Int32' : 'int',
37 'dbus.UInt32' : 'long',
38 'dbus.String' : 'str',
39 'dbus.UInt64' : 'long',
40 'dbus.Boolean' : 'bool',
41}
42
43BASE_TO_DBUS_TYPES = {
44 'int' : 'dbus.Int32',
45 'float' : 'dbus.Double',
46 'str' : 'dbus.String',
47 'long' : 'dbus.Int64',
48 'bool' : 'dbus.Boolean'
Norman Jamesce46e3e2015-08-30 22:25:55 -050049}
Norman James90baede2015-09-02 20:32:49 -050050
Norman James5e792e32015-10-07 17:36:17 -050051def getDBus():
52 bus = None
53 if (BUS == "session"):
54 bus = dbus.SessionBus()
55 else:
56 bus = dbus.SystemBus()
57 return bus
58
Norman James5d78b4d2015-09-05 13:34:34 -050059
Norman James362a80f2015-09-14 14:04:39 -050060def getManagerInterface(bus,manager):
61 bus_name = "org.openbmc.managers."+manager
62 obj_name = "/org/openbmc/managers/"+manager
63 obj = bus.get_object(bus_name,obj_name)
64 return dbus.Interface(obj,bus_name)
Norman James5d78b4d2015-09-05 13:34:34 -050065
Norman James5d78b4d2015-09-05 13:34:34 -050066
Norman James362a80f2015-09-14 14:04:39 -050067def get_objs(bus,bus_name,path,objects):
Norman James19e45912015-10-04 20:19:41 -050068 tmp_path = path
69 if (path == ""):
70 tmp_path="/"
71 obj = bus.get_object(bus_name,tmp_path)
Norman James9e6acf92015-09-08 07:00:04 -050072 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
73 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
74 root = tree.getroot()
Norman James19e45912015-10-04 20:19:41 -050075 parent = True
Norman James362a80f2015-09-14 14:04:39 -050076 for node in root.iter('node'):
Norman James19e45912015-10-04 20:19:41 -050077 for intf in node.iter('interface'):
78 intf_name = intf.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050079 parts=path.split('/')
80 instance = parts[len(parts)-1]
81 if (objects.has_key(instance) == False):
82 objects[instance] = {}
83 objects[instance]['PATH'] = path
84 objects[instance]['INIT'] = []
85 for method in intf.iter('method'):
86 if (method.attrib['name'] == "init"):
87 objects[instance]['INIT'].append(intf_name)
Norman James362a80f2015-09-14 14:04:39 -050088
Norman James19e45912015-10-04 20:19:41 -050089 if (node.attrib.has_key('name') == True):
Norman James362a80f2015-09-14 14:04:39 -050090 node_name = node.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050091 if (parent == False):
92 get_objs(bus,bus_name,path+"/"+node_name,objects)
93 else:
94 if (node_name != "" and node_name != path):
95 get_objs(bus,bus_name,node_name,objects)
96
97 parent = False
Norman James362a80f2015-09-14 14:04:39 -050098
99
Norman James90baede2015-09-02 20:32:49 -0500100class DbusProperty:
101 def __init__(self,name,value):
Norman James90baede2015-09-02 20:32:49 -0500102 self.name = str(name)
103 self.dbusType = str(type(value)).split("'")[1]
Norman James362a80f2015-09-14 14:04:39 -0500104 self.variant_level = 2
Norman James90baede2015-09-02 20:32:49 -0500105 self.value = None
Norman James362a80f2015-09-14 14:04:39 -0500106 if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False):
107 self.variant_level = value.variant_level
108 try:
109 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
110 except:
111 raise Exception("Unknown dbus type: "+self.dbusType)
112 else:
113 self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType]
114 self.value = value
Norman James90baede2015-09-02 20:32:49 -0500115
Norman James5d78b4d2015-09-05 13:34:34 -0500116 def setValue(self,value):
Norman James90baede2015-09-02 20:32:49 -0500117 try:
Norman James362a80f2015-09-14 14:04:39 -0500118 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
Norman James90baede2015-09-02 20:32:49 -0500119 except:
120 raise Exception("Unknown dbus type: "+self.dbusType)
121
Norman James5d78b4d2015-09-05 13:34:34 -0500122 def setVariant(self,variant_level):
123 self.variant_level = variant_level
Norman James90baede2015-09-02 20:32:49 -0500124
125 def getName(self):
126 return self.name
Norman James5d78b4d2015-09-05 13:34:34 -0500127
Norman James90baede2015-09-02 20:32:49 -0500128 def getValue(self):
Norman James5d78b4d2015-09-05 13:34:34 -0500129 e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")"
Norman James90baede2015-09-02 20:32:49 -0500130 return eval(e)
131
Norman James362a80f2015-09-14 14:04:39 -0500132 def getBaseValue(self):
133 return self.value
Norman James90baede2015-09-02 20:32:49 -0500134
135 def __str__(self):
136 return self.dbusType+":"+str(self.value)