Brad Bishop | 9b1417f | 2015-09-21 17:16:16 -0400 | [diff] [blame] | 1 | #!/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 | |
| 19 | import sys |
| 20 | import dbus |
| 21 | import dbus.service |
| 22 | import dbus.mainloop.glib |
| 23 | import gobject |
| 24 | |
| 25 | SERVICE_PREFIX = 'org.openbmc.examples' |
| 26 | IFACE_PREFIX = 'org.openbmc.examples' |
| 27 | BASE_OBJ_PATH = '/org/openbmc/examples/' |
| 28 | |
| 29 | class 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 | |
| 42 | class SampleObjectTwo(SampleObjectOne): |
| 43 | def __init__(self, bus, name): |
| 44 | super(SampleObjectTwo, self).__init__(bus, name) |
Brad Bishop | b7de8ef | 2015-09-21 22:00:06 -0400 | [diff] [blame] | 45 | self.map = { 'empty' : 'add values to me' } |
| 46 | |
| 47 | @dbus.service.signal(IFACE_PREFIX + '.Dict', 'sss') |
| 48 | def DictMethodCalled(self, message, key, value): |
| 49 | pass |
Brad Bishop | 9b1417f | 2015-09-21 17:16:16 -0400 | [diff] [blame] | 50 | |
| 51 | @dbus.service.method(IFACE_PREFIX + '.Dict', 'ss', '') |
| 52 | def SetAValueInTheDict(self, key, value): |
Brad Bishop | b7de8ef | 2015-09-21 22:00:06 -0400 | [diff] [blame] | 53 | self.DictMethodCalled("Dict method was invoked", key, value) |
Brad Bishop | 9b1417f | 2015-09-21 17:16:16 -0400 | [diff] [blame] | 54 | self.map[key] = value |
| 55 | |
| 56 | @dbus.service.method(IFACE_PREFIX + '.Dict', 's', 's') |
| 57 | def GetAValueFromTheDict(self, key): |
| 58 | return self.map.get(key, "set a value first") |
| 59 | |
| 60 | @dbus.service.method(IFACE_PREFIX + '.Dict', '', 's') |
| 61 | def GetAllValuesFromTheDict(self): |
| 62 | return " ".join( [ x+ ':' + self.map[x] for x in self.map.keys() ] ) |
| 63 | |
Brad Bishop | b7de8ef | 2015-09-21 22:00:06 -0400 | [diff] [blame] | 64 | @dbus.service.method(dbus.PROPERTIES_IFACE, 'ss', 'v') |
| 65 | def Get(self, interface, prop): |
| 66 | return self.GetAll(interface)[prop] |
| 67 | |
| 68 | @dbus.service.method(dbus.PROPERTIES_IFACE, 's', 'a{sv}') |
| 69 | def GetAll(self, interface): |
| 70 | if interface == IFACE_PREFIX + '.Dict': |
| 71 | return { 'Dict': self.map } |
| 72 | |
| 73 | @dbus.service.method(dbus.PROPERTIES_IFACE, 'ssv') |
| 74 | def Set(self, interface, prop, value): |
| 75 | if prop == 'Dict': |
| 76 | self.map = value |
| 77 | self.PropertiesChanged(interface, { prop : value }, []) |
| 78 | |
| 79 | @dbus.service.signal(dbus.PROPERTIES_IFACE, 'sa{sv}as') |
| 80 | def PropertiesChanged(self, interface, properties, invalidated_properties): |
| 81 | pass |
Brad Bishop | 9b1417f | 2015-09-21 17:16:16 -0400 | [diff] [blame] | 82 | |
| 83 | if __name__ == '__main__': |
| 84 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 85 | |
| 86 | bus = dbus.SystemBus() |
| 87 | |
| 88 | services = [] |
| 89 | services.append(dbus.service.BusName(SERVICE_PREFIX + '.PythonService0', bus)) |
| 90 | services.append(dbus.service.BusName(SERVICE_PREFIX + '.PythonService1', bus)) |
| 91 | |
| 92 | objs = [] |
| 93 | objs.append(SampleObjectOne(bus, BASE_OBJ_PATH + 'path0/PythonObj')) |
| 94 | objs.append(SampleObjectTwo(bus, BASE_OBJ_PATH + 'path1/PythonObj')) |
| 95 | |
| 96 | mainloop = gobject.MainLoop() |
| 97 | |
| 98 | print "obmc-phosphor-example-pydbus starting..." |
| 99 | mainloop.run() |
| 100 | |