blob: 1518f74dec4490a0bbbcdb45d0df18bde3b16508 [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 Jamesc0c5aad2015-10-19 11:12:30 -050015 'DAUGHTER_CARD' : 5,
16 'FAN' : 6,
17 'BMC' : 7,
18 'CORE' : 8,
19 'PCIE_CARD' : 9,
20 'MEMORY_BUFFER' : 10,
Norman Jamesce46e3e2015-08-30 22:25:55 -050021}
Norman James362a80f2015-09-14 14:04:39 -050022FRU_STATES = {
23 'NORMAL' : 0,
24 'RECOVERABLE_ERROR' : 1,
25 'FATAL_ERROR' : 2,
26 'NOT_PRESENT' : 3,
27}
Norman James32e74e22015-09-15 21:28:06 -050028
Norman James32e74e22015-09-15 21:28:06 -050029
Norman Jamesce46e3e2015-08-30 22:25:55 -050030ENUMS = {
31 'org.openbmc.SensorIntegerThreshold.state' :
32 ['NOT_SET','NORMAL','LOWER_CRITICAL','LOWER_WARNING','UPPER_WARNING','UPPER_CRITICAL'],
Norman James362a80f2015-09-14 14:04:39 -050033}
34
35DBUS_TO_BASE_TYPES = {
36 'dbus.Byte' : 'int',
37 'dbus.Double' : 'float',
38 'dbus.Int32' : 'int',
39 'dbus.UInt32' : 'long',
40 'dbus.String' : 'str',
41 'dbus.UInt64' : 'long',
42 'dbus.Boolean' : 'bool',
43}
44
45BASE_TO_DBUS_TYPES = {
46 'int' : 'dbus.Int32',
47 'float' : 'dbus.Double',
48 'str' : 'dbus.String',
49 'long' : 'dbus.Int64',
50 'bool' : 'dbus.Boolean'
Norman Jamesce46e3e2015-08-30 22:25:55 -050051}
Norman James90baede2015-09-02 20:32:49 -050052
Norman James969227c2015-10-08 15:10:47 -050053def getSystemName():
54 #use filename as system name, strip off path and ext
55 parts = __file__.replace('.pyc','').replace('.py','').split('/')
56 return parts[len(parts)-1]
57
58
Norman James5e792e32015-10-07 17:36:17 -050059def getDBus():
60 bus = None
61 if (BUS == "session"):
62 bus = dbus.SessionBus()
63 else:
64 bus = dbus.SystemBus()
65 return bus
66
Norman James5d78b4d2015-09-05 13:34:34 -050067
Norman James362a80f2015-09-14 14:04:39 -050068def getManagerInterface(bus,manager):
69 bus_name = "org.openbmc.managers."+manager
70 obj_name = "/org/openbmc/managers/"+manager
71 obj = bus.get_object(bus_name,obj_name)
72 return dbus.Interface(obj,bus_name)
Norman James5d78b4d2015-09-05 13:34:34 -050073
Norman James5d78b4d2015-09-05 13:34:34 -050074
Norman James362a80f2015-09-14 14:04:39 -050075def get_objs(bus,bus_name,path,objects):
Norman James19e45912015-10-04 20:19:41 -050076 tmp_path = path
77 if (path == ""):
78 tmp_path="/"
Norman James91dac392015-10-14 09:38:55 -050079
Norman James19e45912015-10-04 20:19:41 -050080 obj = bus.get_object(bus_name,tmp_path)
Norman James9e6acf92015-09-08 07:00:04 -050081 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
82 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
83 root = tree.getroot()
Norman James19e45912015-10-04 20:19:41 -050084 parent = True
Norman James362a80f2015-09-14 14:04:39 -050085 for node in root.iter('node'):
Norman James19e45912015-10-04 20:19:41 -050086 for intf in node.iter('interface'):
87 intf_name = intf.attrib['name']
Norman James19e45912015-10-04 20:19:41 -050088 parts=path.split('/')
89 instance = parts[len(parts)-1]
90 if (objects.has_key(instance) == False):
91 objects[instance] = {}
92 objects[instance]['PATH'] = path
93 objects[instance]['INIT'] = []
94 for method in intf.iter('method'):
95 if (method.attrib['name'] == "init"):
96 objects[instance]['INIT'].append(intf_name)
Norman James362a80f2015-09-14 14:04:39 -050097
Norman James19e45912015-10-04 20:19:41 -050098 if (node.attrib.has_key('name') == True):
Norman James362a80f2015-09-14 14:04:39 -050099 node_name = node.attrib['name']
Norman James19e45912015-10-04 20:19:41 -0500100 if (parent == False):
101 get_objs(bus,bus_name,path+"/"+node_name,objects)
102 else:
103 if (node_name != "" and node_name != path):
104 get_objs(bus,bus_name,node_name,objects)
105
106 parent = False
Norman James362a80f2015-09-14 14:04:39 -0500107
Norman Jamesa3e47c42015-10-18 14:43:10 -0500108class DbusProperties(dbus.service.Object):
109 def __init__(self):
110 dbus.service.Object.__init__(self)
111 self.properties = {}
Norman James362a80f2015-09-14 14:04:39 -0500112
Norman Jamesa3e47c42015-10-18 14:43:10 -0500113 @dbus.service.method(dbus.PROPERTIES_IFACE,
114 in_signature='ss', out_signature='v')
115 def Get(self, interface_name, property_name):
116 d = self.GetAll(interface_name)
117 try:
118 v = d[property_name]
119 return v
120 except:
121 raise dbus.exceptions.DBusException(
122 "org.freedesktop.UnknownPropery: "+property_name)
123
124 @dbus.service.method(dbus.PROPERTIES_IFACE,
125 in_signature='s', out_signature='a{sv}')
126 def GetAll(self, interface_name):
127 try:
128 d = self.properties[interface_name]
129 return d
130 except:
131 raise dbus.exceptions.DBusException(
132 "org.freedesktop.UnknownInterface: "+interface_name)
133
134 @dbus.service.method(dbus.PROPERTIES_IFACE,
135 in_signature='ssv')
136 def Set(self, interface_name, property_name, new_value):
137 if (self.properties.has_key(interface_name) == False):
138 self.properties[interface_name] = {}
139 try:
140 old_value = self.properties[interface_name][property_name]
141 if (old_value != new_value):
142 self.properties[interface_name][property_name] = new_value
143 self.PropertiesChanged(interface_name,{ property_name: new_value }, [])
144
145 except:
146 self.properties[interface_name][property_name] = new_value
147
148 @dbus.service.signal(dbus.PROPERTIES_IFACE,
149 signature='sa{sv}as')
150 def PropertiesChanged(self, interface_name, changed_properties,
151 invalidated_properties):
152 pass
153
154
155
156class DbusVariable:
Norman James90baede2015-09-02 20:32:49 -0500157 def __init__(self,name,value):
Norman James90baede2015-09-02 20:32:49 -0500158 self.name = str(name)
159 self.dbusType = str(type(value)).split("'")[1]
Norman James362a80f2015-09-14 14:04:39 -0500160 self.variant_level = 2
Norman James90baede2015-09-02 20:32:49 -0500161 self.value = None
Norman James362a80f2015-09-14 14:04:39 -0500162 if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False):
163 self.variant_level = value.variant_level
164 try:
165 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
166 except:
167 raise Exception("Unknown dbus type: "+self.dbusType)
168 else:
169 self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType]
170 self.value = value
Norman James90baede2015-09-02 20:32:49 -0500171
Norman James5d78b4d2015-09-05 13:34:34 -0500172 def setValue(self,value):
Norman James90baede2015-09-02 20:32:49 -0500173 try:
Norman James362a80f2015-09-14 14:04:39 -0500174 self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)")
Norman James90baede2015-09-02 20:32:49 -0500175 except:
176 raise Exception("Unknown dbus type: "+self.dbusType)
177
Norman James5d78b4d2015-09-05 13:34:34 -0500178 def setVariant(self,variant_level):
179 self.variant_level = variant_level
Norman James90baede2015-09-02 20:32:49 -0500180
181 def getName(self):
182 return self.name
Norman James5d78b4d2015-09-05 13:34:34 -0500183
Norman James90baede2015-09-02 20:32:49 -0500184 def getValue(self):
Norman James5d78b4d2015-09-05 13:34:34 -0500185 e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")"
Norman James90baede2015-09-02 20:32:49 -0500186 return eval(e)
187
Norman James362a80f2015-09-14 14:04:39 -0500188 def getBaseValue(self):
189 return self.value
Norman James90baede2015-09-02 20:32:49 -0500190
191 def __str__(self):
192 return self.dbusType+":"+str(self.value)