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 17import dbus 18 19BUS_PREFIX = 'org.openbmc' 20OBJ_PREFIX = "/org/openbmc" 21BUS = "system" 22 23 24def is_unique(connection): 25 return connection[0] == ':' 26 27 28def get_system_name(): 29 #use filename as system name, strip off path and ext 30 parts = __file__.replace('.pyc', '').replace('.py', '').split('/') 31 return parts[len(parts)-1] 32 33 34def get_dbus(): 35 bus = None 36 if (BUS == "session"): 37 bus = dbus.SessionBus() 38 else: 39 bus = dbus.SystemBus() 40 return bus 41 42 43class DbusProperties(dbus.service.Object): 44 def __init__(self): 45 dbus.service.Object.__init__(self) 46 self.properties = {} 47 self.object_path = "" 48 self._export = False 49 50 def unmask_signals(self): 51 self._export = True 52 inst = super(DbusProperties, self) 53 if hasattr(inst, 'unmask_signals'): 54 inst.unmask_signals() 55 56 def mask_signals(self): 57 self._export = False 58 inst = super(DbusProperties, self) 59 if hasattr(inst, 'mask_signals'): 60 inst.mask_signals() 61 62 @dbus.service.method( 63 dbus.PROPERTIES_IFACE, 64 in_signature='ss', out_signature='v') 65 def Get(self, interface_name, property_name): 66 d = self.GetAll(interface_name) 67 try: 68 v = d[property_name] 69 return v 70 except: 71 raise dbus.exceptions.DBusException( 72 "org.freedesktop.UnknownProperty: "+property_name) 73 74 @dbus.service.method( 75 dbus.PROPERTIES_IFACE, 76 in_signature='s', out_signature='a{sv}') 77 def GetAll(self, interface_name): 78 try: 79 d = self.properties[interface_name] 80 return d 81 except: 82 raise dbus.exceptions.DBusException( 83 "org.freedesktop.UnknownInterface: "+interface_name) 84 85 @dbus.service.method( 86 dbus.PROPERTIES_IFACE, 87 in_signature='ssv') 88 def Set(self, interface_name, property_name, new_value): 89 if (interface_name not in self.properties): 90 self.properties[interface_name] = {} 91 try: 92 old_value = self.properties[interface_name][property_name] 93 if (old_value != new_value): 94 self.properties[interface_name][property_name] = new_value 95 if self._export: 96 self.PropertiesChanged( 97 interface_name, {property_name: new_value}, []) 98 99 except: 100 self.properties[interface_name][property_name] = new_value 101 if self._export: 102 self.PropertiesChanged( 103 interface_name, {property_name: new_value}, []) 104 105 @dbus.service.method( 106 "org.openbmc.Object.Properties", in_signature='sa{sv}') 107 def SetMultiple(self, interface_name, prop_dict): 108 if (interface_name not in self.properties): 109 self.properties[interface_name] = {} 110 111 value_changed = False 112 for property_name in prop_dict: 113 new_value = prop_dict[property_name] 114 try: 115 old_value = self.properties[interface_name][property_name] 116 if (old_value != new_value): 117 self.properties[interface_name][property_name] = new_value 118 value_changed = True 119 120 except: 121 self.properties[interface_name][property_name] = new_value 122 value_changed = True 123 if (value_changed is True and self._export): 124 self.PropertiesChanged(interface_name, prop_dict, []) 125 126 @dbus.service.signal( 127 dbus.PROPERTIES_IFACE, signature='sa{sv}as') 128 def PropertiesChanged( 129 self, interface_name, changed_properties, invalidated_properties): 130 pass 131 132 133class DbusObjectManager(dbus.service.Object): 134 def __init__(self): 135 dbus.service.Object.__init__(self) 136 self.objects = {} 137 self._export = False 138 139 def unmask_signals(self): 140 self._export = True 141 inst = super(DbusObjectManager, self) 142 if hasattr(inst, 'unmask_signals'): 143 inst.unmask_signals() 144 145 def mask_signals(self): 146 self._export = False 147 inst = super(DbusObjectManager, self) 148 if hasattr(inst, 'mask_signals'): 149 inst.mask_signals() 150 151 def add(self, object_path, obj): 152 self.objects[object_path] = obj 153 if self._export: 154 self.InterfacesAdded(object_path, obj.properties) 155 156 def remove(self, object_path): 157 obj = self.objects.pop(object_path, None) 158 obj.remove_from_connection() 159 if self._export: 160 self.InterfacesRemoved(object_path, obj.properties.keys()) 161 162 def get(self, object_path, default=None): 163 return self.objects.get(object_path, default) 164 165 @dbus.service.method( 166 "org.freedesktop.DBus.ObjectManager", 167 in_signature='', out_signature='a{oa{sa{sv}}}') 168 def GetManagedObjects(self): 169 data = {} 170 for objpath in self.objects.keys(): 171 data[objpath] = self.objects[objpath].properties 172 return data 173 174 @dbus.service.signal( 175 "org.freedesktop.DBus.ObjectManager", signature='oa{sa{sv}}') 176 def InterfacesAdded(self, object_path, properties): 177 pass 178 179 @dbus.service.signal( 180 "org.freedesktop.DBus.ObjectManager", signature='oas') 181 def InterfacesRemoved(self, object_path, interfaces): 182 pass 183