Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 1 | import dbus |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 2 | import xml.etree.ElementTree as ET |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 3 | |
| 4 | BUS_PREFIX = 'org.openbmc' |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 5 | OBJ_PREFIX = "/org/openbmc" |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 6 | GPIO_DEV = '/sys/class/gpio' |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 7 | BUS = "system" |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 8 | |
| 9 | FRU_TYPES = { |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 10 | 'SYSTEM' : 0, |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 11 | 'CPU' : 1, |
| 12 | 'DIMM' : 2, |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 13 | 'MAIN_PLANAR' : 3, |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 14 | 'RISER_CARD' : 4, |
Norman James | c0c5aad | 2015-10-19 11:12:30 -0500 | [diff] [blame] | 15 | 'DAUGHTER_CARD' : 5, |
| 16 | 'FAN' : 6, |
| 17 | 'BMC' : 7, |
| 18 | 'CORE' : 8, |
| 19 | 'PCIE_CARD' : 9, |
| 20 | 'MEMORY_BUFFER' : 10, |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 21 | } |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 22 | FRU_STATES = { |
| 23 | 'NORMAL' : 0, |
| 24 | 'RECOVERABLE_ERROR' : 1, |
| 25 | 'FATAL_ERROR' : 2, |
| 26 | 'NOT_PRESENT' : 3, |
| 27 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 28 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 29 | |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 30 | ENUMS = { |
| 31 | 'org.openbmc.SensorIntegerThreshold.state' : |
| 32 | ['NOT_SET','NORMAL','LOWER_CRITICAL','LOWER_WARNING','UPPER_WARNING','UPPER_CRITICAL'], |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | DBUS_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 | |
| 45 | BASE_TO_DBUS_TYPES = { |
| 46 | 'int' : 'dbus.Int32', |
| 47 | 'float' : 'dbus.Double', |
| 48 | 'str' : 'dbus.String', |
| 49 | 'long' : 'dbus.Int64', |
| 50 | 'bool' : 'dbus.Boolean' |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 51 | } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 52 | |
Norman James | 969227c | 2015-10-08 15:10:47 -0500 | [diff] [blame] | 53 | def 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 James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 59 | def getDBus(): |
| 60 | bus = None |
| 61 | if (BUS == "session"): |
| 62 | bus = dbus.SessionBus() |
| 63 | else: |
| 64 | bus = dbus.SystemBus() |
| 65 | return bus |
| 66 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 67 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 68 | def 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 James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 73 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 74 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 75 | def get_objs(bus,bus_name,path,objects): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 76 | tmp_path = path |
| 77 | if (path == ""): |
| 78 | tmp_path="/" |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 79 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 80 | obj = bus.get_object(bus_name,tmp_path) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 81 | introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") |
| 82 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 83 | root = tree.getroot() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 84 | parent = True |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 85 | for node in root.iter('node'): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 86 | for intf in node.iter('interface'): |
| 87 | intf_name = intf.attrib['name'] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 88 | 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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 97 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 98 | if (node.attrib.has_key('name') == True): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 99 | node_name = node.attrib['name'] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 100 | 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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 107 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 108 | class DbusProperties(dbus.service.Object): |
| 109 | def __init__(self): |
| 110 | dbus.service.Object.__init__(self) |
| 111 | self.properties = {} |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 112 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 113 | @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 | |
| 156 | class DbusVariable: |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 157 | def __init__(self,name,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 158 | self.name = str(name) |
| 159 | self.dbusType = str(type(value)).split("'")[1] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 160 | self.variant_level = 2 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 161 | self.value = None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 162 | 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 James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 171 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 172 | def setValue(self,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 173 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 174 | self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)") |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 175 | except: |
| 176 | raise Exception("Unknown dbus type: "+self.dbusType) |
| 177 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 178 | def setVariant(self,variant_level): |
| 179 | self.variant_level = variant_level |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 180 | |
| 181 | def getName(self): |
| 182 | return self.name |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 183 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 184 | def getValue(self): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 185 | e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")" |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 186 | return eval(e) |
| 187 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 188 | def getBaseValue(self): |
| 189 | return self.value |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 190 | |
| 191 | def __str__(self): |
| 192 | return self.dbusType+":"+str(self.value) |