blob: f0868989e525d0c2e7cc49658a2120d9d006c01c [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 James969227c2015-10-08 15:10:47 -050051def getSystemName():
52 #use filename as system name, strip off path and ext
53 parts = __file__.replace('.pyc','').replace('.py','').split('/')
54 return parts[len(parts)-1]
55
56
Norman James5e792e32015-10-07 17:36:17 -050057def getDBus():
58 bus = None
59 if (BUS == "session"):
60 bus = dbus.SessionBus()
61 else:
62 bus = dbus.SystemBus()
63 return bus
64
Norman James5d78b4d2015-09-05 13:34:34 -050065
Norman James362a80f2015-09-14 14:04:39 -050066def getManagerInterface(bus,manager):
67 bus_name = "org.openbmc.managers."+manager
68 obj_name = "/org/openbmc/managers/"+manager
69 obj = bus.get_object(bus_name,obj_name)
70 return dbus.Interface(obj,bus_name)
Norman James5d78b4d2015-09-05 13:34:34 -050071
Norman James5d78b4d2015-09-05 13:34:34 -050072
Norman James362a80f2015-09-14 14:04:39 -050073def get_objs(bus,bus_name,path,objects):
Norman James19e45912015-10-04 20:19:41 -050074 tmp_path = path
75 if (path == ""):
76 tmp_path="/"
Norman James91dac392015-10-14 09:38:55 -050077
Norman James19e45912015-10-04 20:19:41 -050078 obj = bus.get_object(bus_name,tmp_path)
Norman James9e6acf92015-09-08 07:00:04 -050079 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
80 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
81 root = tree.getroot()
Norman James91dac392015-10-14 09:38:55 -050082
Norman James19e45912015-10-04 20:19:41 -050083 parent = True
Norman James362a80f2015-09-14 14:04:39 -050084 for node in root.iter('node'):
Norman James19e45912015-10-04 20:19:41 -050085 for intf in node.iter('interface'):
86 intf_name = intf.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050087 parts=path.split('/')
88 instance = parts[len(parts)-1]
89 if (objects.has_key(instance) == False):
90 objects[instance] = {}
91 objects[instance]['PATH'] = path
92 objects[instance]['INIT'] = []
93 for method in intf.iter('method'):
94 if (method.attrib['name'] == "init"):
95 objects[instance]['INIT'].append(intf_name)
Norman James362a80f2015-09-14 14:04:39 -050096
Norman James19e45912015-10-04 20:19:41 -050097 if (node.attrib.has_key('name') == True):
Norman James362a80f2015-09-14 14:04:39 -050098 node_name = node.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050099 if (parent == False):
100 get_objs(bus,bus_name,path+"/"+node_name,objects)
101 else:
102 if (node_name != "" and node_name != path):
103 get_objs(bus,bus_name,node_name,objects)
104
105 parent = False
Norman James362a80f2015-09-14 14:04:39 -0500106
107
Norman James90baede2015-09-02 20:32:49 -0500108class DbusProperty:
109 def __init__(self,name,value):
Norman James90baede2015-09-02 20:32:49 -0500110 self.name = str(name)
111 self.dbusType = str(type(value)).split("'")[1]
Norman James362a80f2015-09-14 14:04:39 -0500112 self.variant_level = 2
Norman James90baede2015-09-02 20:32:49 -0500113 self.value = None
Norman James362a80f2015-09-14 14:04:39 -0500114 if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False):
115 self.variant_level = value.variant_level
116 try:
117 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
118 except:
119 raise Exception("Unknown dbus type: "+self.dbusType)
120 else:
121 self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType]
122 self.value = value
Norman James90baede2015-09-02 20:32:49 -0500123
Norman James5d78b4d2015-09-05 13:34:34 -0500124 def setValue(self,value):
Norman James90baede2015-09-02 20:32:49 -0500125 try:
Norman James362a80f2015-09-14 14:04:39 -0500126 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
Norman James90baede2015-09-02 20:32:49 -0500127 except:
128 raise Exception("Unknown dbus type: "+self.dbusType)
129
Norman James5d78b4d2015-09-05 13:34:34 -0500130 def setVariant(self,variant_level):
131 self.variant_level = variant_level
Norman James90baede2015-09-02 20:32:49 -0500132
133 def getName(self):
134 return self.name
Norman James5d78b4d2015-09-05 13:34:34 -0500135
Norman James90baede2015-09-02 20:32:49 -0500136 def getValue(self):
Norman James5d78b4d2015-09-05 13:34:34 -0500137 e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")"
Norman James90baede2015-09-02 20:32:49 -0500138 return eval(e)
139
Norman James362a80f2015-09-14 14:04:39 -0500140 def getBaseValue(self):
141 return self.value
Norman James90baede2015-09-02 20:32:49 -0500142
143 def __str__(self):
144 return self.dbusType+":"+str(self.value)