summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorHunt, Preston <preston.hunt@intel.com>2018-09-24 15:46:41 +0000
committerhimanshu <h.himanshu@samsung.com>2020-02-11 14:27:47 +0530
commit8f30911332f69fc8bfad98abdf5527c266ac4450 (patch)
tree2bca6234e270c0396d2a8429e6a1712dd66ef462 /test
parent57ffe837b91a38afdead06db86fb6df29919d416 (diff)
downloadbluez-8f30911332f69fc8bfad98abdf5527c266ac4450.tar.gz
bluez-8f30911332f69fc8bfad98abdf5527c266ac4450.tar.bz2
bluez-8f30911332f69fc8bfad98abdf5527c266ac4450.zip
test/example-advertisement: add shutdown code
The previous sample code did not release all resources when shutting down. This is fine when it's a standalone program since Python will free all resources automatically when the process terminates. However, in a long-running process, this will eventually cause problems. This changeset shows how to properly release all resources, if an optional command line "--timeout" argument is used. The default is no timeout to maintain behavior of the previous implementation (advertisements will run forever). Change-Id: Ica1bbbf1f9a78625e2a6d5f9ac5570609c5376e5 Signed-off-by: himanshu <h.himanshu@samsung.com>
Diffstat (limited to 'test')
-rwxr-xr-xtest/example-advertisement42
1 files changed, 32 insertions, 10 deletions
diff --git a/test/example-advertisement b/test/example-advertisement
index fd84eacf..88a27ab3 100755
--- a/test/example-advertisement
+++ b/test/example-advertisement
@@ -2,19 +2,18 @@
from __future__ import print_function
+import argparse
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service
-
-import array
+import time
+import threading
try:
- from gi.repository import GObject # python3
+ from gi.repository import GObject # python3
except ImportError:
- import gobject as GObject # python2
-
-from random import randint
+ import gobject as GObject # python2
mainloop = None
@@ -136,6 +135,7 @@ class Advertisement(dbus.service.Object):
def Release(self):
print('%s: Released!' % self.path)
+
class TestAdvertisement(Advertisement):
def __init__(self, bus, index):
@@ -170,7 +170,13 @@ def find_adapter(bus):
return None
-def main():
+def shutdown(timeout):
+ print('Advertising for {} seconds...'.format(timeout))
+ time.sleep(timeout)
+ mainloop.quit()
+
+
+def main(timeout=0):
global mainloop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
@@ -183,7 +189,7 @@ def main():
return
adapter_props = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter),
- "org.freedesktop.DBus.Properties");
+ "org.freedesktop.DBus.Properties")
adapter_props.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1))
@@ -198,7 +204,23 @@ def main():
reply_handler=register_ad_cb,
error_handler=register_ad_error_cb)
- mainloop.run()
+ if timeout > 0:
+ threading.Thread(target=shutdown, args=(timeout,)).start()
+ else:
+ print('Advertising forever...')
+
+ mainloop.run() # blocks until mainloop.quit() is called
+
+ ad_manager.UnregisterAdvertisement(test_advertisement)
+ print('Advertisement unregistered')
+ dbus.service.Object.remove_from_connection(test_advertisement)
+
if __name__ == '__main__':
- main()
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--timeout', default=0, type=int, help="advertise " +
+ "for this many seconds then stop, 0=run forever " +
+ "(default: 0)")
+ args = parser.parse_args()
+
+ main(args.timeout)