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