Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 1 | # Contributors Listed Below - COPYRIGHT 2016 |
| 2 | # [+] International Business Machines Corp. |
| 3 | # |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | import dbus |
Brad Bishop | 84ed6e1 | 2016-09-08 21:39:34 -0400 | [diff] [blame] | 18 | import dbus.service |
| 19 | import dbus.exceptions |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 20 | |
Brad Bishop | c88b095 | 2017-07-29 23:03:30 -0400 | [diff] [blame] | 21 | OBJ_PREFIX = '/xyz/openbmc_project' |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def is_unique(connection): |
| 25 | return connection[0] == ':' |
| 26 | |
| 27 | |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 28 | def get_dbus(): |
Brad Bishop | 93d0cf0 | 2016-09-08 21:37:27 -0400 | [diff] [blame] | 29 | return dbus.SystemBus() |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class DbusProperties(dbus.service.Object): |
Brad Bishop | d874f0b | 2016-09-08 22:16:58 -0400 | [diff] [blame] | 33 | def __init__(self, **kw): |
Patrick Williams | 4fed868 | 2016-09-28 13:19:14 -0500 | [diff] [blame] | 34 | self.validator = kw.pop('validator', None) |
Brad Bishop | d874f0b | 2016-09-08 22:16:58 -0400 | [diff] [blame] | 35 | super(DbusProperties, self).__init__(**kw) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 36 | self.properties = {} |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 37 | self._export = False |
| 38 | |
| 39 | def unmask_signals(self): |
| 40 | self._export = True |
| 41 | inst = super(DbusProperties, self) |
| 42 | if hasattr(inst, 'unmask_signals'): |
| 43 | inst.unmask_signals() |
| 44 | |
| 45 | def mask_signals(self): |
| 46 | self._export = False |
| 47 | inst = super(DbusProperties, self) |
| 48 | if hasattr(inst, 'mask_signals'): |
| 49 | inst.mask_signals() |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 50 | |
| 51 | @dbus.service.method( |
| 52 | dbus.PROPERTIES_IFACE, |
| 53 | in_signature='ss', out_signature='v') |
| 54 | def Get(self, interface_name, property_name): |
| 55 | d = self.GetAll(interface_name) |
| 56 | try: |
| 57 | v = d[property_name] |
| 58 | return v |
| 59 | except: |
| 60 | raise dbus.exceptions.DBusException( |
Adriana Kobylak | dc7f067 | 2017-12-13 10:36:53 -0600 | [diff] [blame^] | 61 | "Unknown property: '{}'".format(property_name), |
| 62 | name="org.freedesktop.DBus.Error.UnknownProperty") |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 63 | |
| 64 | @dbus.service.method( |
| 65 | dbus.PROPERTIES_IFACE, |
| 66 | in_signature='s', out_signature='a{sv}') |
| 67 | def GetAll(self, interface_name): |
| 68 | try: |
| 69 | d = self.properties[interface_name] |
| 70 | return d |
| 71 | except: |
| 72 | raise dbus.exceptions.DBusException( |
Adriana Kobylak | dc7f067 | 2017-12-13 10:36:53 -0600 | [diff] [blame^] | 73 | "Unknown interface: '{}'".format(interface_name), |
| 74 | name="org.freedesktop.DBus.Error.UnknownInterface") |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 75 | |
| 76 | @dbus.service.method( |
| 77 | dbus.PROPERTIES_IFACE, |
| 78 | in_signature='ssv') |
| 79 | def Set(self, interface_name, property_name, new_value): |
| 80 | if (interface_name not in self.properties): |
| 81 | self.properties[interface_name] = {} |
Vishwanatha Subbanna | 0722564 | 2016-09-20 20:41:15 +0530 | [diff] [blame] | 82 | |
| 83 | if self.validator: |
| 84 | self.validator(interface_name, property_name, new_value) |
| 85 | |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 86 | try: |
| 87 | old_value = self.properties[interface_name][property_name] |
| 88 | if (old_value != new_value): |
| 89 | self.properties[interface_name][property_name] = new_value |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 90 | if self._export: |
| 91 | self.PropertiesChanged( |
| 92 | interface_name, {property_name: new_value}, []) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 93 | |
| 94 | except: |
| 95 | self.properties[interface_name][property_name] = new_value |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 96 | if self._export: |
| 97 | self.PropertiesChanged( |
| 98 | interface_name, {property_name: new_value}, []) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 99 | |
| 100 | @dbus.service.method( |
| 101 | "org.openbmc.Object.Properties", in_signature='sa{sv}') |
| 102 | def SetMultiple(self, interface_name, prop_dict): |
Brad Bishop | d0827b1 | 2016-05-12 16:48:57 -0400 | [diff] [blame] | 103 | if (interface_name not in self.properties): |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 104 | self.properties[interface_name] = {} |
| 105 | |
| 106 | value_changed = False |
| 107 | for property_name in prop_dict: |
| 108 | new_value = prop_dict[property_name] |
| 109 | try: |
| 110 | old_value = self.properties[interface_name][property_name] |
| 111 | if (old_value != new_value): |
| 112 | self.properties[interface_name][property_name] = new_value |
| 113 | value_changed = True |
| 114 | |
| 115 | except: |
| 116 | self.properties[interface_name][property_name] = new_value |
| 117 | value_changed = True |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 118 | if (value_changed is True and self._export): |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 119 | self.PropertiesChanged(interface_name, prop_dict, []) |
| 120 | |
| 121 | @dbus.service.signal( |
| 122 | dbus.PROPERTIES_IFACE, signature='sa{sv}as') |
| 123 | def PropertiesChanged( |
| 124 | self, interface_name, changed_properties, invalidated_properties): |
| 125 | pass |
| 126 | |
| 127 | |
Brad Bishop | 2b05434 | 2017-10-27 15:33:02 -0400 | [diff] [blame] | 128 | def add_interfaces_to_class(cls, ifaces): |
| 129 | """ |
| 130 | The built-in Introspect method in dbus-python doesn't find |
| 131 | interfaces if the @method or @signal decorators aren't used |
| 132 | (property-only interfaces). Use this method on a class |
| 133 | derived from dbus.service.Object to help the dbus-python provided |
| 134 | Introspect method find these interfaces. |
| 135 | |
| 136 | Arguments: |
| 137 | cls -- The dbus.service.Object superclass to add interfaces to. |
| 138 | ifaces -- The property-only interfaces to add to the class. |
| 139 | """ |
| 140 | |
| 141 | for iface in ifaces: |
| 142 | class_table_key = '{}.{}'.format(cls.__module__, cls.__name__) |
| 143 | cls._dbus_class_table[class_table_key].setdefault(iface, {}) |
| 144 | |
| 145 | |
| 146 | def add_interfaces(ifaces): |
| 147 | """ |
| 148 | A class decorator for add_interfaces_to_class. |
| 149 | """ |
| 150 | |
| 151 | def decorator(cls): |
| 152 | undecorated = cls.__init__ |
| 153 | |
| 154 | def ctor(obj, *a, **kw): |
| 155 | undecorated(obj, *a, **kw) |
| 156 | add_interfaces_to_class(cls, ifaces) |
| 157 | |
| 158 | cls.__init__ = ctor |
| 159 | return cls |
| 160 | return decorator |
| 161 | |
| 162 | |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 163 | class DbusObjectManager(dbus.service.Object): |
Brad Bishop | d874f0b | 2016-09-08 22:16:58 -0400 | [diff] [blame] | 164 | def __init__(self, **kw): |
| 165 | super(DbusObjectManager, self).__init__(**kw) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 166 | self.objects = {} |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 167 | self._export = False |
| 168 | |
| 169 | def unmask_signals(self): |
| 170 | self._export = True |
| 171 | inst = super(DbusObjectManager, self) |
| 172 | if hasattr(inst, 'unmask_signals'): |
| 173 | inst.unmask_signals() |
| 174 | |
| 175 | def mask_signals(self): |
| 176 | self._export = False |
| 177 | inst = super(DbusObjectManager, self) |
| 178 | if hasattr(inst, 'mask_signals'): |
| 179 | inst.mask_signals() |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 180 | |
| 181 | def add(self, object_path, obj): |
| 182 | self.objects[object_path] = obj |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 183 | if self._export: |
| 184 | self.InterfacesAdded(object_path, obj.properties) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 185 | |
| 186 | def remove(self, object_path): |
| 187 | obj = self.objects.pop(object_path, None) |
| 188 | obj.remove_from_connection() |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 189 | if self._export: |
| 190 | self.InterfacesRemoved(object_path, obj.properties.keys()) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 191 | |
| 192 | def get(self, object_path, default=None): |
| 193 | return self.objects.get(object_path, default) |
| 194 | |
| 195 | @dbus.service.method( |
| 196 | "org.freedesktop.DBus.ObjectManager", |
| 197 | in_signature='', out_signature='a{oa{sa{sv}}}') |
| 198 | def GetManagedObjects(self): |
| 199 | data = {} |
| 200 | for objpath in self.objects.keys(): |
| 201 | data[objpath] = self.objects[objpath].properties |
| 202 | return data |
| 203 | |
| 204 | @dbus.service.signal( |
| 205 | "org.freedesktop.DBus.ObjectManager", signature='oa{sa{sv}}') |
| 206 | def InterfacesAdded(self, object_path, properties): |
Brad Bishop | 0b30e97 | 2016-06-21 14:58:14 -0400 | [diff] [blame] | 207 | pass |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 208 | |
| 209 | @dbus.service.signal( |
| 210 | "org.freedesktop.DBus.ObjectManager", signature='oas') |
| 211 | def InterfacesRemoved(self, object_path, interfaces): |
| 212 | pass |