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 | 969227c | 2015-10-08 15:10:47 -0500 | [diff] [blame] | 51 | def 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 James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 57 | def getDBus(): |
| 58 | bus = None |
| 59 | if (BUS == "session"): |
| 60 | bus = dbus.SessionBus() |
| 61 | else: |
| 62 | bus = dbus.SystemBus() |
| 63 | return bus |
| 64 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 65 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 66 | def 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 James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 71 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 72 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 73 | def get_objs(bus,bus_name,path,objects): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 74 | tmp_path = path |
| 75 | if (path == ""): |
| 76 | tmp_path="/" |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 77 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | obj = bus.get_object(bus_name,tmp_path) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 79 | introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") |
| 80 | tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) |
| 81 | root = tree.getroot() |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 82 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 83 | parent = True |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 84 | for node in root.iter('node'): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 85 | for intf in node.iter('interface'): |
| 86 | intf_name = intf.attrib['name'] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 87 | parts=path.split('/') |
| 88 | instance = parts[len(parts)-1] |
| 89 | if (objects.has_key(instance) == False): |
| 90 | objects[instance] = {} |
| 91 | objects[instance]['PATH'] = path |
| 92 | objects[instance]['INIT'] = [] |
| 93 | for method in intf.iter('method'): |
| 94 | if (method.attrib['name'] == "init"): |
| 95 | objects[instance]['INIT'].append(intf_name) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 96 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 97 | if (node.attrib.has_key('name') == True): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 98 | node_name = node.attrib['name'] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 99 | if (parent == False): |
| 100 | get_objs(bus,bus_name,path+"/"+node_name,objects) |
| 101 | else: |
| 102 | if (node_name != "" and node_name != path): |
| 103 | get_objs(bus,bus_name,node_name,objects) |
| 104 | |
| 105 | parent = False |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 106 | |
| 107 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 108 | class DbusProperty: |
| 109 | def __init__(self,name,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 110 | self.name = str(name) |
| 111 | self.dbusType = str(type(value)).split("'")[1] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 112 | self.variant_level = 2 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 113 | self.value = None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 114 | if (BASE_TO_DBUS_TYPES.has_key(self.dbusType) == False): |
| 115 | self.variant_level = value.variant_level |
| 116 | try: |
| 117 | self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)") |
| 118 | except: |
| 119 | raise Exception("Unknown dbus type: "+self.dbusType) |
| 120 | else: |
| 121 | self.dbusType = BASE_TO_DBUS_TYPES[self.dbusType] |
| 122 | self.value = value |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 123 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 124 | def setValue(self,value): |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 125 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 126 | self.value = eval(DBUS_TO_BASE_TYPES[self.dbusType]+"(value)") |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 127 | except: |
| 128 | raise Exception("Unknown dbus type: "+self.dbusType) |
| 129 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 130 | def setVariant(self,variant_level): |
| 131 | self.variant_level = variant_level |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 132 | |
| 133 | def getName(self): |
| 134 | return self.name |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 135 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 136 | def getValue(self): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 137 | e = self.dbusType+"(self.value, variant_level="+str(self.variant_level)+")" |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 138 | return eval(e) |
| 139 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 140 | def getBaseValue(self): |
| 141 | return self.value |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 142 | |
| 143 | def __str__(self): |
| 144 | return self.dbusType+":"+str(self.value) |