xref: /openbmc/pyphosphor/obmc/dbuslib/bindings.py (revision 8ffe1e44)
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
49    @dbus.service.method(
50        dbus.PROPERTIES_IFACE,
51        in_signature='ss', out_signature='v')
52    def Get(self, interface_name, property_name):
53        d = self.GetAll(interface_name)
54        try:
55            v = d[property_name]
56            return v
57        except:
58            raise dbus.exceptions.DBusException(
59                "org.freedesktop.UnknownProperty: "+property_name)
60
61    @dbus.service.method(
62        dbus.PROPERTIES_IFACE,
63        in_signature='s', out_signature='a{sv}')
64    def GetAll(self, interface_name):
65        try:
66            d = self.properties[interface_name]
67            return d
68        except:
69            raise dbus.exceptions.DBusException(
70                "org.freedesktop.UnknownInterface: "+interface_name)
71
72    @dbus.service.method(
73        dbus.PROPERTIES_IFACE,
74        in_signature='ssv')
75    def Set(self, interface_name, property_name, new_value):
76        if (interface_name not in self.properties):
77            self.properties[interface_name] = {}
78        try:
79            old_value = self.properties[interface_name][property_name]
80            if (old_value != new_value):
81                self.properties[interface_name][property_name] = new_value
82                self.PropertiesChanged(
83                    interface_name, {property_name: new_value}, [])
84
85        except:
86            self.properties[interface_name][property_name] = new_value
87            self.PropertiesChanged(
88                interface_name, {property_name: new_value}, [])
89
90    @dbus.service.method(
91        "org.openbmc.Object.Properties", in_signature='sa{sv}')
92    def SetMultiple(self, interface_name, prop_dict):
93        if (interface_name in self.properties):
94            self.properties[interface_name] = {}
95
96        value_changed = False
97        for property_name in prop_dict:
98            new_value = prop_dict[property_name]
99            try:
100                old_value = self.properties[interface_name][property_name]
101                if (old_value != new_value):
102                    self.properties[interface_name][property_name] = new_value
103                    value_changed = True
104
105            except:
106                self.properties[interface_name][property_name] = new_value
107                value_changed = True
108        if (value_changed is True):
109            self.PropertiesChanged(interface_name, prop_dict, [])
110
111    @dbus.service.signal(
112        dbus.PROPERTIES_IFACE, signature='sa{sv}as')
113    def PropertiesChanged(
114            self, interface_name, changed_properties, invalidated_properties):
115        pass
116
117
118class DbusObjectManager(dbus.service.Object):
119    def __init__(self):
120        dbus.service.Object.__init__(self)
121        self.objects = {}
122
123    def add(self, object_path, obj):
124        self.objects[object_path] = obj
125        self.InterfacesAdded(object_path, obj.properties)
126
127    def remove(self, object_path):
128        obj = self.objects.pop(object_path, None)
129        obj.remove_from_connection()
130        self.InterfacesRemoved(object_path, obj.properties.keys())
131
132    def get(self, object_path, default=None):
133        return self.objects.get(object_path, default)
134
135    @dbus.service.method(
136        "org.freedesktop.DBus.ObjectManager",
137        in_signature='', out_signature='a{oa{sa{sv}}}')
138    def GetManagedObjects(self):
139        data = {}
140        for objpath in self.objects.keys():
141            data[objpath] = self.objects[objpath].properties
142        return data
143
144    @dbus.service.signal(
145        "org.freedesktop.DBus.ObjectManager", signature='oa{sa{sv}}')
146    def InterfacesAdded(self, object_path, properties):
147        self.ObjectAdded(object_path, "")
148
149    @dbus.service.signal(
150        "org.freedesktop.DBus.ObjectManager", signature='oas')
151    def InterfacesRemoved(self, object_path, interfaces):
152        pass
153
154    ## Legacy support, need to eventually refactor out
155    @dbus.service.signal(
156        "org.openbmc.Object.ObjectMapper", signature='ss')
157    def ObjectAdded(self, object_path, interface_name):
158        pass
159
160    ## flattens interfaces
161    @dbus.service.method(
162        'org.openbmc.Object.Enumerate', in_signature='',
163        out_signature='a{sa{sv}}')
164    def enumerate(self):
165        data = {}
166        for objpath in self.objects.keys():
167            props = self.objects[objpath].properties
168            data[objpath] = {}
169            for iface in props.keys():
170                data[objpath].update(props[iface])
171
172        return data
173