Merge pull request #45 from liuct/add-tizen-localmode
[test/tools/testkit-lite.git] / testkit-lite-dbus
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2012 Intel Corporation
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # Authors:
16 #              ChengTao.Liu  <chengtaox.liu@intel.com>
17 #              Yuanyuan,Zou  <yuanyuanx.zou@intel.com>
18 """ testkit dbus service"""
19
20 import sys
21 import dbus
22 import dbus.service
23 from dbus.mainloop.glib import DBusGMainLoop
24 import gobject
25
26
27 class DeviceKeeper(dbus.service.Object):
28     """
29     testkit dbus service implementation
30     """
31
32     def init(self):
33         self._devices = []
34
35     @dbus.service.method(dbus_interface="com.intel.testkit", in_signature="s", out_signature="b")
36     def addDevice(self, device_id):
37         if device_id in self._devices:
38             return False
39         else:
40             self._devices.append(device_id)
41             return True
42
43     @dbus.service.method(dbus_interface="com.intel.testkit", in_signature="s", out_signature="")
44     def removeDevice(self, device_id):
45         if device_id in self._devices:
46             self._devices.remove(device_id)
47         return None
48
49
50 if __name__ == '__main__':
51     DBusGMainLoop(set_as_default=True)
52     gobject.threads_init()
53     bus = dbus.SessionBus()
54     if bus.name_has_owner('com.intel.testkit'):
55         print 'An instance of testkit service is running!'
56         sys.exit()
57     bus_name = dbus.service.BusName('com.intel.testkit', bus)
58     t_object = DeviceKeeper(bus_name, "/com/intel/testkit/devices")
59     t_object.init()
60     loop = gobject.MainLoop()
61     loop.run()