Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / test / test-health-sink
1 #!/usr/bin/python
2
3 from __future__ import absolute_import, print_function, unicode_literals
4 # -*- coding: utf-8 -*-
5
6 import sys
7 import dbus
8 import dbus.service
9 from dbus.mainloop.glib import DBusGMainLoop
10 try:
11   from gi.repository import GObject
12 except ImportError:
13   import gobject as GObject
14
15 BUS_NAME = 'org.bluez'
16 PATH = '/org/bluez'
17 ADAPTER_INTERFACE = 'org.bluez.Adapter1'
18 HEALTH_MANAGER_INTERFACE = 'org.bluez.HealthManager1'
19 HEALTH_DEVICE_INTERFACE = 'org.bluez.HealthDevice1'
20
21 DBusGMainLoop(set_as_default=True)
22 loop = GObject.MainLoop()
23
24 bus = dbus.SystemBus()
25
26 type = 4103
27 if len(sys.argv) > 1:
28         type = int(sys.argv[1])
29
30 hdp_manager = dbus.Interface(bus.get_object(BUS_NAME, PATH),
31                                                 HEALTH_MANAGER_INTERFACE)
32 app_path = hdp_manager.CreateApplication({"DataType": dbus.types.UInt16(type),
33                                         "Role": "sink"})
34
35 print(app_path)
36
37 manager = dbus.Interface(bus.get_object(BUS_NAME, "/"),
38                                         "org.freedesktop.DBus.ObjectManager")
39
40 objects = manager.GetManagedObjects()
41 adapters = []
42
43 for path, ifaces in objects.iteritems():
44         if ifaces.has_key(ADAPTER_INTERFACE):
45                 adapters.append(path)
46
47 i = 1
48 for ad in adapters:
49         print("%d. %s" % (i, ad))
50         i = i + 1
51
52 print("Select an adapter: ",)
53 select = None
54 while select == None:
55         try:
56                 pos = int(sys.stdin.readline()) - 1
57                 if pos < 0:
58                         raise TypeError
59                 select = adapters[pos]
60         except (TypeError, IndexError, ValueError):
61                 print("Wrong selection, try again: ",)
62         except KeyboardInterrupt:
63                 sys.exit()
64
65 adapter =  dbus.Interface(bus.get_object(BUS_NAME, select),
66                                                 ADAPTER_INTERFACE)
67
68 devices = []
69 for path, interfaces in objects.iteritems():
70         if "org.bluez.Device1" not in interfaces:
71                 continue
72         properties = interfaces["org.bluez.Device1"]
73         if properties["Adapter"] != select:
74                 continue;
75
76         if HEALTH_DEVICE_INTERFACE not in interfaces:
77                 continue
78         devices.append(path)
79
80 if len(devices) == 0:
81         print("No devices available")
82         sys.exit()
83
84 i = 1
85 for dev in devices:
86         print("%d. %s" % (i, dev))
87         i = i + 1
88
89 print("Select a device: ",)
90 select = None
91 while select == None:
92         try:
93                 pos = int(sys.stdin.readline()) - 1
94                 if pos < 0:
95                         raise TypeError
96                 select = devices[pos]
97         except (TypeError, IndexError, ValueError):
98                 print("Wrong selection, try again: ",)
99         except KeyboardInterrupt:
100                 sys.exit()
101
102 print("Connecting to %s" % (select))
103 device = dbus.Interface(bus.get_object(BUS_NAME, select),
104                                                 HEALTH_DEVICE_INTERFACE)
105
106 chan = device.CreateChannel(app_path, "Any")
107
108 print(chan)
109
110 print("Push Enter for finishing")
111 sys.stdin.readline()
112
113 hdp_manager.DestroyApplication(app_path)