blob: b0523b37949d9d0d403ea6b253183736b6d4ee07 [file] [log] [blame]
Brad Bishop9b1417f2015-09-21 17:16:16 -04001#!/usr/bin/env python
2
3# Contributors Listed Below - COPYRIGHT 2015
4# [+] International Business Machines Corp.
5#
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16# implied. See the License for the specific language governing
17# permissions and limitations under the License.
18
19import sys
20import dbus
21import dbus.service
22import dbus.mainloop.glib
23import gobject
24
25SERVICE_PREFIX = 'org.openbmc.examples'
26IFACE_PREFIX = 'org.openbmc.examples'
27BASE_OBJ_PATH = '/org/openbmc/examples/'
28
29class SampleObjectOne(dbus.service.Object):
30 def __init__(self, bus, name):
31 super(SampleObjectOne, self).__init__(bus, name)
32
33 @dbus.service.method(IFACE_PREFIX + '.Echo', 's', 's')
34 def Echo(self, val):
35 self.MethodInvoked("Echo method was invoked", self._object_path)
36 return self._object_path + " says " + val
37
38 @dbus.service.signal(IFACE_PREFIX + '.Echo', 'ss')
39 def MethodInvoked(self, message, path):
40 pass
41
42class SampleObjectTwo(SampleObjectOne):
43 def __init__(self, bus, name):
44 super(SampleObjectTwo, self).__init__(bus, name)
45 self.map = {}
46
47 @dbus.service.method(IFACE_PREFIX + '.Dict', 'ss', '')
48 def SetAValueInTheDict(self, key, value):
49 self.map[key] = value
50
51 @dbus.service.method(IFACE_PREFIX + '.Dict', 's', 's')
52 def GetAValueFromTheDict(self, key):
53 return self.map.get(key, "set a value first")
54
55 @dbus.service.method(IFACE_PREFIX + '.Dict', '', 's')
56 def GetAllValuesFromTheDict(self):
57 return " ".join( [ x+ ':' + self.map[x] for x in self.map.keys() ] )
58
59
60if __name__ == '__main__':
61 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
62
63 bus = dbus.SystemBus()
64
65 services = []
66 services.append(dbus.service.BusName(SERVICE_PREFIX + '.PythonService0', bus))
67 services.append(dbus.service.BusName(SERVICE_PREFIX + '.PythonService1', bus))
68
69 objs = []
70 objs.append(SampleObjectOne(bus, BASE_OBJ_PATH + 'path0/PythonObj'))
71 objs.append(SampleObjectTwo(bus, BASE_OBJ_PATH + 'path1/PythonObj'))
72
73 mainloop = gobject.MainLoop()
74
75 print "obmc-phosphor-example-pydbus starting..."
76 mainloop.run()
77