blob: 485525c88e438de690713bc0d9d25b2bde905454 [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="/"
77 obj = bus.get_object(bus_name,tmp_path)
Norman James9e6acf92015-09-08 07:00:04 -050078 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
79 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
80 root = tree.getroot()
Norman James19e45912015-10-04 20:19:41 -050081 parent = True
Norman James362a80f2015-09-14 14:04:39 -050082 for node in root.iter('node'):
Norman James19e45912015-10-04 20:19:41 -050083 for intf in node.iter('interface'):
84 intf_name = intf.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050085 parts=path.split('/')
86 instance = parts[len(parts)-1]
87 if (objects.has_key(instance) == False):
88 objects[instance] = {}
89 objects[instance]['PATH'] = path
90 objects[instance]['INIT'] = []
91 for method in intf.iter('method'):
92 if (method.attrib['name'] == "init"):
93 objects[instance]['INIT'].append(intf_name)
Norman James362a80f2015-09-14 14:04:39 -050094
Norman James19e45912015-10-04 20:19:41 -050095 if (node.attrib.has_key('name') == True):
Norman James362a80f2015-09-14 14:04:39 -050096 node_name = node.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050097 if (parent == False):
98 get_objs(bus,bus_name,path+"/"+node_name,objects)
99 else:
100 if (node_name != "" and node_name != path):
101 get_objs(bus,bus_name,node_name,objects)
102
103 parent = False
Norman James362a80f2015-09-14 14:04:39 -0500104
105
Norman James90baede2015-09-02 20:32:49 -0500106class DbusProperty:
107 def __init__(self,name,value):
Norman James90baede2015-09-02 20:32:49 -0500108 self.name = str(name)
109 self.dbusType = str(type(value)).split("'")[1]
Norman James362a80f2015-09-14 14:04:39 -0500110 self.variant_level = 2
Norman James90baede2015-09-02 20:32:49 -0500111 self.value = None
Norman James362a80f2015-09-14 14:04:39 -0500112 if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False):
113 self.variant_level = value.variant_level
114 try:
115 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
116 except:
117 raise Exception("Unknown dbus type: "+self.dbusType)
118 else:
119 self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType]
120 self.value = value
Norman James90baede2015-09-02 20:32:49 -0500121
Norman James5d78b4d2015-09-05 13:34:34 -0500122 def setValue(self,value):
Norman James90baede2015-09-02 20:32:49 -0500123 try:
Norman James362a80f2015-09-14 14:04:39 -0500124 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
Norman James90baede2015-09-02 20:32:49 -0500125 except:
126 raise Exception("Unknown dbus type: "+self.dbusType)
127
Norman James5d78b4d2015-09-05 13:34:34 -0500128 def setVariant(self,variant_level):
129 self.variant_level = variant_level
Norman James90baede2015-09-02 20:32:49 -0500130
131 def getName(self):
132 return self.name
Norman James5d78b4d2015-09-05 13:34:34 -0500133
Norman James90baede2015-09-02 20:32:49 -0500134 def getValue(self):
Norman James5d78b4d2015-09-05 13:34:34 -0500135 e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")"
Norman James90baede2015-09-02 20:32:49 -0500136 return eval(e)
137
Norman James362a80f2015-09-14 14:04:39 -0500138 def getBaseValue(self):
139 return self.value
Norman James90baede2015-09-02 20:32:49 -0500140
141 def __str__(self):
142 return self.dbusType+":"+str(self.value)