blob: 203b56f1b69e15c869e0d8ec9aad47fa4af98cb5 [file] [log] [blame]
Brad Bishop8ffe1e42016-02-11 16:15:40 -05001# 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
Brad Bishop84ed6e12016-09-08 21:39:34 -040018import dbus.service
19import dbus.exceptions
Brad Bishop8ffe1e42016-02-11 16:15:40 -050020
Brad Bishop93d0cf02016-09-08 21:37:27 -040021OBJ_PREFIX = '/org/openbmc'
Brad Bishop8ffe1e42016-02-11 16:15:40 -050022
23
24def is_unique(connection):
25 return connection[0] == ':'
26
27
Brad Bishop8ffe1e42016-02-11 16:15:40 -050028def get_dbus():
Brad Bishop93d0cf02016-09-08 21:37:27 -040029 return dbus.SystemBus()
Brad Bishop8ffe1e42016-02-11 16:15:40 -050030
31
32class DbusProperties(dbus.service.Object):
Brad Bishopd874f0b2016-09-08 22:16:58 -040033 def __init__(self, **kw):
34 super(DbusProperties, self).__init__(**kw)
Brad Bishop8ffe1e42016-02-11 16:15:40 -050035 self.properties = {}
Brad Bishop6835b672016-06-27 21:55:29 -040036 self._export = False
37
38 def unmask_signals(self):
39 self._export = True
40 inst = super(DbusProperties, self)
41 if hasattr(inst, 'unmask_signals'):
42 inst.unmask_signals()
43
44 def mask_signals(self):
45 self._export = False
46 inst = super(DbusProperties, self)
47 if hasattr(inst, 'mask_signals'):
48 inst.mask_signals()
Brad Bishop8ffe1e42016-02-11 16:15:40 -050049
50 @dbus.service.method(
51 dbus.PROPERTIES_IFACE,
52 in_signature='ss', out_signature='v')
53 def Get(self, interface_name, property_name):
54 d = self.GetAll(interface_name)
55 try:
56 v = d[property_name]
57 return v
58 except:
59 raise dbus.exceptions.DBusException(
60 "org.freedesktop.UnknownProperty: "+property_name)
61
62 @dbus.service.method(
63 dbus.PROPERTIES_IFACE,
64 in_signature='s', out_signature='a{sv}')
65 def GetAll(self, interface_name):
66 try:
67 d = self.properties[interface_name]
68 return d
69 except:
70 raise dbus.exceptions.DBusException(
71 "org.freedesktop.UnknownInterface: "+interface_name)
72
73 @dbus.service.method(
74 dbus.PROPERTIES_IFACE,
75 in_signature='ssv')
76 def Set(self, interface_name, property_name, new_value):
77 if (interface_name not in self.properties):
78 self.properties[interface_name] = {}
79 try:
80 old_value = self.properties[interface_name][property_name]
81 if (old_value != new_value):
82 self.properties[interface_name][property_name] = new_value
Brad Bishop6835b672016-06-27 21:55:29 -040083 if self._export:
84 self.PropertiesChanged(
85 interface_name, {property_name: new_value}, [])
Brad Bishop8ffe1e42016-02-11 16:15:40 -050086
87 except:
88 self.properties[interface_name][property_name] = new_value
Brad Bishop6835b672016-06-27 21:55:29 -040089 if self._export:
90 self.PropertiesChanged(
91 interface_name, {property_name: new_value}, [])
Brad Bishop8ffe1e42016-02-11 16:15:40 -050092
93 @dbus.service.method(
94 "org.openbmc.Object.Properties", in_signature='sa{sv}')
95 def SetMultiple(self, interface_name, prop_dict):
Brad Bishopd0827b12016-05-12 16:48:57 -040096 if (interface_name not in self.properties):
Brad Bishop8ffe1e42016-02-11 16:15:40 -050097 self.properties[interface_name] = {}
98
99 value_changed = False
100 for property_name in prop_dict:
101 new_value = prop_dict[property_name]
102 try:
103 old_value = self.properties[interface_name][property_name]
104 if (old_value != new_value):
105 self.properties[interface_name][property_name] = new_value
106 value_changed = True
107
108 except:
109 self.properties[interface_name][property_name] = new_value
110 value_changed = True
Brad Bishop6835b672016-06-27 21:55:29 -0400111 if (value_changed is True and self._export):
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500112 self.PropertiesChanged(interface_name, prop_dict, [])
113
114 @dbus.service.signal(
115 dbus.PROPERTIES_IFACE, signature='sa{sv}as')
116 def PropertiesChanged(
117 self, interface_name, changed_properties, invalidated_properties):
118 pass
119
120
121class DbusObjectManager(dbus.service.Object):
Brad Bishopd874f0b2016-09-08 22:16:58 -0400122 def __init__(self, **kw):
123 super(DbusObjectManager, self).__init__(**kw)
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500124 self.objects = {}
Brad Bishop6835b672016-06-27 21:55:29 -0400125 self._export = False
126
127 def unmask_signals(self):
128 self._export = True
129 inst = super(DbusObjectManager, self)
130 if hasattr(inst, 'unmask_signals'):
131 inst.unmask_signals()
132
133 def mask_signals(self):
134 self._export = False
135 inst = super(DbusObjectManager, self)
136 if hasattr(inst, 'mask_signals'):
137 inst.mask_signals()
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500138
139 def add(self, object_path, obj):
140 self.objects[object_path] = obj
Brad Bishop6835b672016-06-27 21:55:29 -0400141 if self._export:
142 self.InterfacesAdded(object_path, obj.properties)
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500143
144 def remove(self, object_path):
145 obj = self.objects.pop(object_path, None)
146 obj.remove_from_connection()
Brad Bishop6835b672016-06-27 21:55:29 -0400147 if self._export:
148 self.InterfacesRemoved(object_path, obj.properties.keys())
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500149
150 def get(self, object_path, default=None):
151 return self.objects.get(object_path, default)
152
153 @dbus.service.method(
154 "org.freedesktop.DBus.ObjectManager",
155 in_signature='', out_signature='a{oa{sa{sv}}}')
156 def GetManagedObjects(self):
157 data = {}
158 for objpath in self.objects.keys():
159 data[objpath] = self.objects[objpath].properties
160 return data
161
162 @dbus.service.signal(
163 "org.freedesktop.DBus.ObjectManager", signature='oa{sa{sv}}')
164 def InterfacesAdded(self, object_path, properties):
Brad Bishop0b30e972016-06-21 14:58:14 -0400165 pass
Brad Bishop8ffe1e42016-02-11 16:15:40 -0500166
167 @dbus.service.signal(
168 "org.freedesktop.DBus.ObjectManager", signature='oas')
169 def InterfacesRemoved(self, object_path, interfaces):
170 pass