| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
import dbus |
| 9 |
import dbus.service |
| 10 |
import dbus.mainloop.glib |
| 11 |
try: |
| 12 |
from gi.repository import GObject |
| 13 |
except ImportError: |
| 14 |
import gobject as GObject |
| 15 |
|
| 16 |
BUS_NAME='org.bluez.obex' |
| 17 |
PATH = '/org/bluez/obex' |
| 18 |
CLIENT_INTERFACE = 'org.bluez.obex.Client1' |
| 19 |
SESSION_INTERFACE = 'org.bluez.obex.Session1' |
| 20 |
PHONEBOOK_ACCESS_INTERFACE = 'org.bluez.obex.PhonebookAccess1' |
| 21 |
TRANSFER_INTERFACE = 'org.bluez.obex.Transfer1' |
| 22 |
|
| 23 |
class Transfer: |
| 24 |
def __init__(self, callback_func): |
| 25 |
self.callback_func = callback_func |
| 26 |
self.path = None |
| 27 |
self.filename = None |
| 28 |
|
| 29 |
class PbapClient: |
| 30 |
def __init__(self, session_path): |
| 31 |
self.transfers = 0 |
| 32 |
self.props = dict() |
| 33 |
self.flush_func = None |
| 34 |
bus = dbus.SessionBus() |
| 35 |
obj = bus.get_object(BUS_NAME, session_path) |
| 36 |
self.session = dbus.Interface(obj, SESSION_INTERFACE) |
| 37 |
self.pbap = dbus.Interface(obj, PHONEBOOK_ACCESS_INTERFACE) |
| 38 |
bus.add_signal_receiver(self.properties_changed, |
| 39 |
dbus_interface="org.freedesktop.DBus.Properties", |
| 40 |
signal_name="PropertiesChanged", |
| 41 |
path_keyword="path") |
| 42 |
|
| 43 |
def register(self, path, properties, transfer): |
| 44 |
transfer.path = path |
| 45 |
transfer.filename = properties["Filename"] |
| 46 |
self.props[path] = transfer |
| 47 |
print("Transfer created: %s (file %s)" % (path, |
| 48 |
transfer.filename)) |
| 49 |
|
| 50 |
def error(self, err): |
| 51 |
print(err) |
| 52 |
mainloop.quit() |
| 53 |
|
| 54 |
def transfer_complete(self, path): |
| 55 |
req = self.props.get(path) |
| 56 |
if req == None: |
| 57 |
return |
| 58 |
self.transfers -= 1 |
| 59 |
print("Transfer %s complete" % path) |
| 60 |
try: |
| 61 |
f = open(req.filename, "r") |
| 62 |
os.remove(req.filename) |
| 63 |
lines = f.readlines() |
| 64 |
del self.props[path] |
| 65 |
req.callback_func(lines) |
| 66 |
except: |
| 67 |
pass |
| 68 |
|
| 69 |
if (len(self.props) == 0) and (self.transfers == 0): |
| 70 |
if self.flush_func != None: |
| 71 |
f = self.flush_func |
| 72 |
self.flush_func = None |
| 73 |
f() |
| 74 |
|
| 75 |
def transfer_error(self, path): |
| 76 |
print("Transfer %s error" % path) |
| 77 |
mainloop.quit() |
| 78 |
|
| 79 |
def properties_changed(self, interface, properties, invalidated, path): |
| 80 |
req = self.props.get(path) |
| 81 |
if req == None: |
| 82 |
return |
| 83 |
|
| 84 |
if properties['Status'] == 'complete': |
| 85 |
self.transfer_complete(path) |
| 86 |
return |
| 87 |
|
| 88 |
if properties['Status'] == 'error': |
| 89 |
self.transfer_error(path) |
| 90 |
return |
| 91 |
|
| 92 |
def pull(self, vcard, params, func): |
| 93 |
req = Transfer(func) |
| 94 |
self.pbap.Pull(vcard, "", params, |
| 95 |
reply_handler=lambda o, p: self.register(o, p, req), |
| 96 |
error_handler=self.error) |
| 97 |
self.transfers += 1 |
| 98 |
|
| 99 |
def pull_all(self, params, func): |
| 100 |
req = Transfer(func) |
| 101 |
self.pbap.PullAll("", params, |
| 102 |
reply_handler=lambda o, p: self.register(o, p, req), |
| 103 |
error_handler=self.error) |
| 104 |
self.transfers += 1 |
| 105 |
|
| 106 |
def flush_transfers(self, func): |
| 107 |
if (len(self.props) == 0) and (self.transfers == 0): |
| 108 |
return |
| 109 |
self.flush_func = func |
| 110 |
|
| 111 |
def interface(self): |
| 112 |
return self.pbap |
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
|
| 116 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 117 |
|
| 118 |
bus = dbus.SessionBus() |
| 119 |
mainloop = GObject.MainLoop() |
| 120 |
|
| 121 |
client = dbus.Interface(bus.get_object(BUS_NAME, PATH), |
| 122 |
CLIENT_INTERFACE) |
| 123 |
|
| 124 |
if (len(sys.argv) < 2): |
| 125 |
print("Usage: %s <device>" % (sys.argv[0])) |
| 126 |
sys.exit(1) |
| 127 |
|
| 128 |
print("Creating Session") |
| 129 |
session_path = client.CreateSession(sys.argv[1], { "Target": "PBAP" }) |
| 130 |
|
| 131 |
pbap_client = PbapClient(session_path) |
| 132 |
|
| 133 |
def process_result(lines, header): |
| 134 |
if header != None: |
| 135 |
print(header) |
| 136 |
for line in lines: |
| 137 |
print(line), |
| 138 |
print |
| 139 |
|
| 140 |
def test_paths(paths): |
| 141 |
if len(paths) == 0: |
| 142 |
print |
| 143 |
print("FINISHED") |
| 144 |
mainloop.quit() |
| 145 |
return |
| 146 |
|
| 147 |
path = paths[0] |
| 148 |
|
| 149 |
print("\n--- Select Phonebook %s ---\n" % (path)) |
| 150 |
pbap_client.interface().Select("int", path) |
| 151 |
|
| 152 |
print("\n--- GetSize ---\n") |
| 153 |
ret = pbap_client.interface().GetSize() |
| 154 |
print("Size = %d\n" % (ret)) |
| 155 |
|
| 156 |
print("\n--- List vCard ---\n") |
| 157 |
try: |
| 158 |
ret = pbap_client.interface().List(dbus.Dictionary()) |
| 159 |
except: |
| 160 |
ret = [] |
| 161 |
|
| 162 |
params = dbus.Dictionary({ "Format" : "vcard30", |
| 163 |
"Fields" : ["PHOTO"] }) |
| 164 |
for item in ret: |
| 165 |
print("%s : %s" % (item[0], item[1])) |
| 166 |
pbap_client.pull(item[0], params, |
| 167 |
lambda x: process_result(x, None)) |
| 168 |
|
| 169 |
pbap_client.pull_all(params, lambda x: process_result(x, |
| 170 |
"\n--- PullAll ---\n")) |
| 171 |
|
| 172 |
pbap_client.flush_transfers(lambda: test_paths(paths[1:])) |
| 173 |
|
| 174 |
test_paths(["PB", "ICH", "OCH", "MCH", "CCH", "SPD", "FAV"]) |
| 175 |
|
| 176 |
mainloop.run() |