Tizen 2.0 Release
[framework/connectivity/bluez.git] / test / test-attrib
1 #!/usr/bin/python
2
3 from __future__ import absolute_import, print_function, unicode_literals
4 # Script for testing the Attribute D-Bus API
5
6 import sys
7 from optparse import OptionParser, OptionValueError
8 from binascii import hexlify, unhexlify
9
10 import gobject
11
12 import sys
13 import dbus
14 import dbus.mainloop.glib
15 from optparse import OptionParser, make_option
16
17 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
18 bus = dbus.SystemBus()
19 mainloop = gobject.MainLoop()
20
21 manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
22
23 option_list = [
24                 make_option("-i", "--device", action="store",
25                                 type="string", dest="dev_id"),
26                 ]
27 parser = OptionParser(option_list=option_list)
28
29 (options, args) = parser.parse_args()
30
31 if options.dev_id:
32         adapter_path = manager.FindAdapter(options.dev_id)
33 else:
34         adapter_path = manager.DefaultAdapter()
35
36 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
37                                                         "org.bluez.Adapter")
38
39 if (len(args) < 1):
40         print("Usage: %s <command>" % (sys.argv[0]))
41         print("")
42         print("  list")
43         print("  services <address>")
44         print("  discover <service path>")
45         print("  chars <service path>")
46         sys.exit(1)
47
48 if (args[0] == "list"):
49         for path in adapter.ListDevices():
50                 device = dbus.Interface(bus.get_object("org.bluez", path),
51                                                         "org.bluez.Device")
52                 devprop = device.GetProperties()
53                 print("[ %s ]" % devprop["Address"])
54                 for path in devprop["Services"]:
55
56                         service = dbus.Interface(bus.get_object("org.bluez", path),
57                                                                          "org.bluez.Characteristic")
58                         srvprop = service.GetProperties()
59                         print(" * %s" % (path))
60                         print(" UUID: %s" % srvprop["UUID"])
61                         print(" Chars: ",)
62                         for char in srvprop["Characteristics"]:
63                                 print("%s " % char,)
64                         print()
65                         print()
66                 print()
67         sys.exit(0)
68
69 if (args[0] == "services"):
70         if (len(args) < 2):
71                 print("Need address parameter")
72         else:
73                 path = adapter.FindDevice(args[1])
74                 device = dbus.Interface(bus.get_object("org.bluez", path),
75                                                         "org.bluez.Device")
76                 properties = device.GetProperties()
77                 for path in properties["Services"]:
78                         print(path)
79         sys.exit(0)
80
81 if (args[0] == "discover"):
82         if (len(args) < 2):
83                 print("Need service path parameter")
84         else:
85                 service = dbus.Interface(bus.get_object("org.bluez", args[1]),
86                                                         "org.bluez.Characteristic")
87                 for path in service.DiscoverCharacteristics():
88                         print(path)
89         sys.exit(0)
90
91 if (args[0] == "chars"):
92         if (len(args) < 2):
93                 print("Need service path parameter")
94         else:
95                 service = dbus.Interface(bus.get_object("org.bluez", args[1]),
96                                                                  "org.bluez.Characteristic")
97                 srvprop = service.GetProperties()
98                 for path in srvprop["Characteristics"]:
99                         print("[ %s ]" % (path))
100                         char = dbus.Interface(bus.get_object("org.bluez", path),
101                                                                  "org.bluez.Characteristic")
102                         charprop = char.GetProperties()
103                         print(" Name: %s" % charprop["Name"])
104                         print(" UUID: %s" % charprop["UUID"])
105                         print()
106                 print()
107         sys.exit(0)
108
109 print("Unknown command")
110 sys.exit(1)