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 | 93d0cf0 | 2016-09-08 21:37:27 -0400 | [diff] [blame] | 21 | OBJ_PREFIX = '/org/openbmc' |
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): |
| 33 | def __init__(self): |
| 34 | dbus.service.Object.__init__(self) |
| 35 | self.properties = {} |
| 36 | self.object_path = "" |
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( |
| 61 | "org.freedesktop.UnknownProperty: "+property_name) |
| 62 | |
| 63 | @dbus.service.method( |
| 64 | dbus.PROPERTIES_IFACE, |
| 65 | in_signature='s', out_signature='a{sv}') |
| 66 | def GetAll(self, interface_name): |
| 67 | try: |
| 68 | d = self.properties[interface_name] |
| 69 | return d |
| 70 | except: |
| 71 | raise dbus.exceptions.DBusException( |
| 72 | "org.freedesktop.UnknownInterface: "+interface_name) |
| 73 | |
| 74 | @dbus.service.method( |
| 75 | dbus.PROPERTIES_IFACE, |
| 76 | in_signature='ssv') |
| 77 | def Set(self, interface_name, property_name, new_value): |
| 78 | if (interface_name not in self.properties): |
| 79 | self.properties[interface_name] = {} |
| 80 | try: |
| 81 | old_value = self.properties[interface_name][property_name] |
| 82 | if (old_value != new_value): |
| 83 | self.properties[interface_name][property_name] = new_value |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 84 | if self._export: |
| 85 | self.PropertiesChanged( |
| 86 | interface_name, {property_name: new_value}, []) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 87 | |
| 88 | except: |
| 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 | @dbus.service.method( |
| 95 | "org.openbmc.Object.Properties", in_signature='sa{sv}') |
| 96 | def SetMultiple(self, interface_name, prop_dict): |
Brad Bishop | d0827b1 | 2016-05-12 16:48:57 -0400 | [diff] [blame] | 97 | if (interface_name not in self.properties): |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 98 | self.properties[interface_name] = {} |
| 99 | |
| 100 | value_changed = False |
| 101 | for property_name in prop_dict: |
| 102 | new_value = prop_dict[property_name] |
| 103 | try: |
| 104 | old_value = self.properties[interface_name][property_name] |
| 105 | if (old_value != new_value): |
| 106 | self.properties[interface_name][property_name] = new_value |
| 107 | value_changed = True |
| 108 | |
| 109 | except: |
| 110 | self.properties[interface_name][property_name] = new_value |
| 111 | value_changed = True |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 112 | if (value_changed is True and self._export): |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 113 | self.PropertiesChanged(interface_name, prop_dict, []) |
| 114 | |
| 115 | @dbus.service.signal( |
| 116 | dbus.PROPERTIES_IFACE, signature='sa{sv}as') |
| 117 | def PropertiesChanged( |
| 118 | self, interface_name, changed_properties, invalidated_properties): |
| 119 | pass |
| 120 | |
| 121 | |
| 122 | class DbusObjectManager(dbus.service.Object): |
| 123 | def __init__(self): |
| 124 | dbus.service.Object.__init__(self) |
| 125 | self.objects = {} |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 126 | self._export = False |
| 127 | |
| 128 | def unmask_signals(self): |
| 129 | self._export = True |
| 130 | inst = super(DbusObjectManager, self) |
| 131 | if hasattr(inst, 'unmask_signals'): |
| 132 | inst.unmask_signals() |
| 133 | |
| 134 | def mask_signals(self): |
| 135 | self._export = False |
| 136 | inst = super(DbusObjectManager, self) |
| 137 | if hasattr(inst, 'mask_signals'): |
| 138 | inst.mask_signals() |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 139 | |
| 140 | def add(self, object_path, obj): |
| 141 | self.objects[object_path] = obj |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 142 | if self._export: |
| 143 | self.InterfacesAdded(object_path, obj.properties) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 144 | |
| 145 | def remove(self, object_path): |
| 146 | obj = self.objects.pop(object_path, None) |
| 147 | obj.remove_from_connection() |
Brad Bishop | 6835b67 | 2016-06-27 21:55:29 -0400 | [diff] [blame] | 148 | if self._export: |
| 149 | self.InterfacesRemoved(object_path, obj.properties.keys()) |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 150 | |
| 151 | def get(self, object_path, default=None): |
| 152 | return self.objects.get(object_path, default) |
| 153 | |
| 154 | @dbus.service.method( |
| 155 | "org.freedesktop.DBus.ObjectManager", |
| 156 | in_signature='', out_signature='a{oa{sa{sv}}}') |
| 157 | def GetManagedObjects(self): |
| 158 | data = {} |
| 159 | for objpath in self.objects.keys(): |
| 160 | data[objpath] = self.objects[objpath].properties |
| 161 | return data |
| 162 | |
| 163 | @dbus.service.signal( |
| 164 | "org.freedesktop.DBus.ObjectManager", signature='oa{sa{sv}}') |
| 165 | def InterfacesAdded(self, object_path, properties): |
Brad Bishop | 0b30e97 | 2016-06-21 14:58:14 -0400 | [diff] [blame] | 166 | pass |
Brad Bishop | 8ffe1e4 | 2016-02-11 16:15:40 -0500 | [diff] [blame] | 167 | |
| 168 | @dbus.service.signal( |
| 169 | "org.freedesktop.DBus.ObjectManager", signature='oas') |
| 170 | def InterfacesRemoved(self, object_path, interfaces): |
| 171 | pass |