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