Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 1 | import dbus |
| 2 | |
| 3 | BUS_PREFIX = 'org.openbmc' |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 4 | OBJ_PREFIX = "/org/openbmc" |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 5 | GPIO_DEV = '/sys/class/gpio' |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 6 | BUS = "system" |
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 7 | |
Norman James | 969227c | 2015-10-08 15:10:47 -0500 | [diff] [blame] | 8 | def getSystemName(): |
| 9 | #use filename as system name, strip off path and ext |
| 10 | parts = __file__.replace('.pyc','').replace('.py','').split('/') |
| 11 | return parts[len(parts)-1] |
| 12 | |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 13 | def getDBus(): |
| 14 | bus = None |
| 15 | if (BUS == "session"): |
| 16 | bus = dbus.SessionBus() |
| 17 | else: |
| 18 | bus = dbus.SystemBus() |
| 19 | return bus |
| 20 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 21 | class DbusProperties(dbus.service.Object): |
| 22 | def __init__(self): |
| 23 | dbus.service.Object.__init__(self) |
| 24 | self.properties = {} |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 25 | self.object_path = "" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 26 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 27 | @dbus.service.method(dbus.PROPERTIES_IFACE, |
| 28 | in_signature='ss', out_signature='v') |
| 29 | def Get(self, interface_name, property_name): |
| 30 | d = self.GetAll(interface_name) |
| 31 | try: |
| 32 | v = d[property_name] |
| 33 | return v |
| 34 | except: |
| 35 | raise dbus.exceptions.DBusException( |
| 36 | "org.freedesktop.UnknownPropery: "+property_name) |
| 37 | |
| 38 | @dbus.service.method(dbus.PROPERTIES_IFACE, |
| 39 | in_signature='s', out_signature='a{sv}') |
| 40 | def GetAll(self, interface_name): |
| 41 | try: |
| 42 | d = self.properties[interface_name] |
| 43 | return d |
| 44 | except: |
| 45 | raise dbus.exceptions.DBusException( |
| 46 | "org.freedesktop.UnknownInterface: "+interface_name) |
| 47 | |
| 48 | @dbus.service.method(dbus.PROPERTIES_IFACE, |
| 49 | in_signature='ssv') |
| 50 | def Set(self, interface_name, property_name, new_value): |
| 51 | if (self.properties.has_key(interface_name) == False): |
| 52 | self.properties[interface_name] = {} |
| 53 | try: |
| 54 | old_value = self.properties[interface_name][property_name] |
| 55 | if (old_value != new_value): |
| 56 | self.properties[interface_name][property_name] = new_value |
| 57 | self.PropertiesChanged(interface_name,{ property_name: new_value }, []) |
| 58 | |
| 59 | except: |
| 60 | self.properties[interface_name][property_name] = new_value |
Norman James | f81d498 | 2015-10-26 17:12:14 -0500 | [diff] [blame] | 61 | self.PropertiesChanged(interface_name,{ property_name: new_value }, []) |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 62 | |
Norman James | f81d498 | 2015-10-26 17:12:14 -0500 | [diff] [blame] | 63 | @dbus.service.method("org.openbmc.Object.Properties", |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 64 | in_signature='sa{sv}') |
Norman James | f81d498 | 2015-10-26 17:12:14 -0500 | [diff] [blame] | 65 | def SetMultiple(self, interface_name, prop_dict): |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 66 | if (self.properties.has_key(interface_name) == False): |
| 67 | self.properties[interface_name] = {} |
| 68 | |
| 69 | value_changed = False |
| 70 | for property_name in prop_dict: |
| 71 | new_value = prop_dict[property_name] |
| 72 | try: |
| 73 | old_value = self.properties[interface_name][property_name] |
| 74 | if (old_value != new_value): |
| 75 | self.properties[interface_name][property_name] = new_value |
| 76 | value_changed = True |
| 77 | |
| 78 | except: |
| 79 | self.properties[interface_name][property_name] = new_value |
| 80 | value_changed = True |
| 81 | if (value_changed == True): |
| 82 | self.PropertiesChanged(interface_name, prop_dict, []) |
| 83 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 84 | @dbus.service.signal(dbus.PROPERTIES_IFACE, |
| 85 | signature='sa{sv}as') |
| 86 | def PropertiesChanged(self, interface_name, changed_properties, |
| 87 | invalidated_properties): |
| 88 | pass |
| 89 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 90 | class DbusObjectManager(dbus.service.Object): |
| 91 | def __init__(self): |
| 92 | dbus.service.Object.__init__(self) |
| 93 | self.objects = {} |
| 94 | |
| 95 | def add(self,object_path,obj): |
| 96 | self.objects[object_path] = obj |
| 97 | self.InterfacesAdded(object_path,obj.properties) |
| 98 | |
| 99 | def remove(self,object_path): |
| 100 | obj = self.objects.pop(object_path,None) |
| 101 | obj.remove_from_connection() |
| 102 | self.InterfacesRemoved(object_path,obj.properties.keys()) |
| 103 | |
| 104 | def get(self,object_path): |
| 105 | return self.objects[object_path] |
| 106 | |
| 107 | @dbus.service.method("org.freedesktop.DBus.ObjectManager", |
| 108 | in_signature='', out_signature='a{oa{sa{sv}}}') |
| 109 | def GetManagedObjects(self): |
| 110 | data = {} |
| 111 | for objpath in self.objects.keys(): |
| 112 | data[objpath] = self.objects[objpath].properties |
| 113 | return data |
| 114 | |
| 115 | @dbus.service.signal("org.freedesktop.DBus.ObjectManager", |
| 116 | signature='oa{sa{sv}}') |
| 117 | def InterfacesAdded(self,object_path,properties): |
| 118 | self.ObjectAdded(object_path,"") |
| 119 | |
| 120 | @dbus.service.signal("org.freedesktop.DBus.ObjectManager", |
| 121 | signature='oas') |
| 122 | def InterfacesRemoved(self,object_path,interfaces): |
| 123 | pass |
| 124 | |
| 125 | ## Legacy support, need to eventually refactor out |
Norman James | 633f04d | 2015-10-31 17:34:34 -0500 | [diff] [blame] | 126 | @dbus.service.signal("org.openbmc.Object.ObjectMapper", |
| 127 | signature='ss') |
| 128 | def ObjectAdded(self,object_path,interface_name): |
| 129 | pass |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 130 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 131 | ## flattens interfaces |
| 132 | @dbus.service.method('org.openbmc.Object.Enumerate', |
| 133 | in_signature='', out_signature='a{sa{sv}}') |
| 134 | def enumerate(self): |
| 135 | data = {} |
| 136 | for objpath in self.objects.keys(): |
| 137 | props = self.objects[objpath].properties |
| 138 | data[objpath] = { } |
| 139 | for iface in props.keys(): |
| 140 | data[objpath].update(props[iface]) |
| 141 | |
| 142 | return data |
| 143 | |
| 144 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 145 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 146 | |