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' |
| 7 | |
| 8 | |
| 9 | FRU_TYPES = { |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 10 | 'CPU' : 1, |
| 11 | 'DIMM' : 2, |
| 12 | 'BACKPLANE' : 3, |
| 13 | 'RISER_CARD' : 4, |
| 14 | 'FAN' : 5 |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 15 | } |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 16 | FRU_STATES = { |
| 17 | 'NORMAL' : 0, |
| 18 | 'RECOVERABLE_ERROR' : 1, |
| 19 | 'FATAL_ERROR' : 2, |
| 20 | 'NOT_PRESENT' : 3, |
| 21 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 22 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 23 | |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 24 | ENUMS = { |
| 25 | 'org.openbmc.SensorIntegerThreshold.state' : |
| 26 | ['NOT_SET','NORMAL','LOWER_CRITICAL','LOWER_WARNING','UPPER_WARNING','UPPER_CRITICAL'], |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | DBUS_TO_BASE_TYPES = { |
| 30 | 'dbus.Byte' : 'int', |
| 31 | 'dbus.Double' : 'float', |
| 32 | 'dbus.Int32' : 'int', |
| 33 | 'dbus.UInt32' : 'long', |
| 34 | 'dbus.String' : 'str', |
| 35 | 'dbus.UInt64' : 'long', |
| 36 | 'dbus.Boolean' : 'bool', |
| 37 | } |
| 38 | |
| 39 | BASE_TO_DBUS_TYPES = { |
| 40 | 'int' : 'dbus.Int32', |
| 41 | 'float' : 'dbus.Double', |
| 42 | 'str' : 'dbus.String', |
| 43 | 'long' : 'dbus.Int64', |
| 44 | 'bool' : 'dbus.Boolean' |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 45 | } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 46 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 47 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 48 | def getManagerInterface(bus,manager): |
| 49 | bus_name = "org.openbmc.managers."+manager |
| 50 | obj_name = "/org/openbmc/managers/"+manager |
| 51 | obj = bus.get_object(bus_name,obj_name) |
| 52 | return dbus.Interface(obj,bus_name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 53 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 54 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 55 | def get_objs(bus,bus_name,path,objects): |
| 56 | obj = bus.get_object(bus_name,path) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 57 | introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 58 | #print introspect_iface.Introspect() |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 59 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 60 | root = tree.getroot() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 61 | for node in root.iter('node'): |
| 62 | if (node.attrib.has_key('name') == False): |
| 63 | for intf in node.iter('interface'): |
| 64 | intf_name = intf.attrib['name'] |
| 65 | if (intf_name.find(BUS_PREFIX)==0): |
| 66 | parts=path.split('/') |
| 67 | instance = parts[len(parts)-1] |
| 68 | if (objects.has_key(instance) == False): |
| 69 | objects[instance] = {} |
| 70 | objects[instance]['PATH'] = path |
| 71 | objects[instance]['INIT'] = [] |
| 72 | for method in intf.iter('method'): |
| 73 | if (method.attrib['name'] == "init"): |
| 74 | objects[instance]['INIT'].append(intf_name) |
| 75 | |
| 76 | else: |
| 77 | node_name = node.attrib['name'] |
| 78 | if (node_name != path): |
| 79 | get_objs(bus,bus_name,path+"/"+node.attrib['name'],objects) |
| 80 | |
| 81 | |
| 82 | |
| 83 | #def get_methods(obj): |
| 84 | # methods = {} |
| 85 | # introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") |
| 86 | # tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 87 | # root = tree.getroot() |
| 88 | # for intf in root.iter('interface'): |
| 89 | # intf_name = intf.attrib['name'] |
| 90 | # if (intf_name.find(BUS_PREFIX)==0): |
| 91 | # methods[intf_name] = {} |
| 92 | # for method in intf.iter('method'): |
| 93 | # methods[intf_name][method.attrib['name']] = True |
| 94 | # |
| 95 | # return methods |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 96 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 97 | class DbusProperty: |
| 98 | def __init__(self,name,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 99 | self.name = str(name) |
| 100 | self.dbusType = str(type(value)).split("'")[1] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 101 | self.variant_level = 2 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 102 | self.value = None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 103 | if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False): |
| 104 | self.variant_level = value.variant_level |
| 105 | try: |
| 106 | self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)") |
| 107 | except: |
| 108 | raise Exception("Unknown dbus type: "+self.dbusType) |
| 109 | else: |
| 110 | self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType] |
| 111 | self.value = value |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 112 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 113 | def setValue(self,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 114 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 115 | self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)") |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 116 | except: |
| 117 | raise Exception("Unknown dbus type: "+self.dbusType) |
| 118 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 119 | def setVariant(self,variant_level): |
| 120 | self.variant_level = variant_level |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 121 | |
| 122 | def getName(self): |
| 123 | return self.name |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 124 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 125 | def getValue(self): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 126 | e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")" |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 127 | return eval(e) |
| 128 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 129 | def getBaseValue(self): |
| 130 | return self.value |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 131 | |
| 132 | def __str__(self): |
| 133 | return self.dbusType+":"+str(self.value) |