Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
andete committed Mar 2, 2014
1 parent bdd01e5 commit 6e7a557
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
15 changes: 14 additions & 1 deletion ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

primary = ([0x28, 0x00], "Primary" ),
secundary = ([0x28, 0x01], "Secundary"),
include = ([0x28, 0x02], "Include"),
char = ([0x28, 0x03], "Characteristic"),

chr_u_dsc = ([0x29, 0x01], "Characteristic User Description"),
chr_c_cnf = ([0x29, 0x02], "Client Characteristic Configuration"),
Expand Down Expand Up @@ -69,6 +71,7 @@ class BLE(QtCore.QObject):
timeout = QtCore.Signal()
service_result = QtCore.Signal(int, list, int, int)
procedure_completed = QtCore.Signal(int)
info_found = QtCore.Signal(int, int, list)

CONNECTED = 0

Expand Down Expand Up @@ -153,6 +156,7 @@ def start(self, port = None):
ble.check_activity(ser, 1)
print "local device:", self.address
self.ble.ble_evt_attclient_procedure_completed += self.handle_attclient_procedure_completed
self.ble.ble_evt_attclient_find_information_found += self.handle_attclient_information_found

def send_command(self, cmd):
return self.ble.send_command(self.ser, cmd)
Expand Down Expand Up @@ -185,11 +189,17 @@ def handle_connection_status(self, sender, args):
def handle_attclient_group_found(self, sender, args):
#uuid = ''.join(["%02X" % c for c in reversed(args['uuid'])])
#print "Found attribute group for service: %s start=%d, end=%d" % (uuid, args['start'], args['end'])
print args
handle = args['connection']
uuid = args['uuid'][::-1]
self.service_result.emit(handle, uuid, args['start'], args['end'])

def handle_attclient_information_found(self, sender, args):
print args
handle = args['connection']
char = args['chrhandle']
uuid = args['uuid'][::-1]
self.info_found.emit(handle, char, uuid)

def handle_attclient_procedure_completed(self, sender, args):
handle = args['connection']
self.procedure_completed.emit(handle)
Expand All @@ -213,3 +223,6 @@ def connect_direct(self, target):
def primary_service_discovery(self, handle):
# print "service discovery for %d ..." % handle
self.send_command(self.ble.ble_cmd_attclient_read_by_group_type(handle, 0x0001, 0xFFFF, list(reversed(UUID['primary'][0]))))

def find_information(self, handle, start, end):
self.send_command(self.ble.ble_cmd_attclient_find_information(handle, start, end))
76 changes: 69 additions & 7 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

class Device:

def __init__(self, handle, mac):
def __init__(self, ble, handle, mac):
self.ble = ble
self.handle = handle
self.mac = mac
self.view = QtGui.QTreeView()
self.view.setRootIsDecorated(False)
self.model = QtGui.QStandardItemModel()
self.model.setColumnCount(5)
self.model.setHorizontalHeaderLabels(['type', 'service','name','handle-start', 'handle-end'])
self.model.setHorizontalHeaderLabels(['type', 'service/type','name','handle-start', 'handle-end'])
self.root = self.model.invisibleRootItem()
self.view.setModel(self.model)

def primary(self):
self.type = 'primary'

def service_result(self, uuid, start, end):
def s(x):
y = QtGui.QStandardItem(x)
Expand All @@ -36,16 +40,57 @@ def s(x):
break
uuids = ''.join(["%02X" % c for c in uuid])
print uuids
self.model.appendRow([s('primary'), s(uuids), s(service), s(str(start)), s(str(end))])
p = s(self.type)
p.setData(uuid)
self.model.appendRow([p, s(uuids), s(service), s(str(start)), s(str(end))])
self.view.resizeColumnToContents(0)
self.view.resizeColumnToContents(1)
self.view.resizeColumnToContents(2)
self.view.resizeColumnToContents(3)
self.view.resizeColumnToContents(4)

def scan(self):
self.scan_pos = 0
i = 0
uuid = self.model.item(i, 0).data()
start = int(self.model.item(i, 3).data(Qt.DisplayRole))
end = int(self.model.item(i, 4).data(Qt.DisplayRole))
print uuid, start, end
self.ble.find_information(self.handle, start, end)

def continue_scan(self):
self.scan_pos += 1
if self.scan_pos == self.model.rowCount():
return False
i = self.scan_pos
uuid = self.model.item(i, 0).data()
start = int(self.model.item(i, 3).data(Qt.DisplayRole))
end = int(self.model.item(i, 4).data(Qt.DisplayRole))
print uuid, start, end
self.ble.find_information(self.handle, start, end)
return True

def info_found(self, char, uuid):
#print "info_found", char, uuid
def s(x):
y = QtGui.QStandardItem(x)
y.setEditable(False)
return y
uuids = ''.join(["%02X" % c for c in uuid])
name = ''
for (i, n) in ble.UUID.values():
if i == uuid:
name = n
break
self.model.item(self.scan_pos).appendRow([s("char"), s(uuids), s(name), s(str(char)), s('')])
#self.view.expandAll()

class MainWin(QtGui.QMainWindow):

PROC_IDLE = 0
PROC_PRIMARY = 1
PROC_ATTR = 2

def __init__(self):
super(MainWin, self).__init__()
menuBar = self.menuBar()
Expand All @@ -65,6 +110,7 @@ def __init__(self):
self.setWindowTitle("BTLE tool using device "+self.ble.address)
self.handle_to_mac = {}
self.handle_to_device = {}
self.running = self.PROC_IDLE
self.run_collection()

def add_action(self, menu, text, slot, shortcut=None, checkable=False, checked=False):
Expand Down Expand Up @@ -116,9 +162,9 @@ def _add(text, slot = None):
return self.collect_view

def make_device_widget(self, handle, mac):
device = Device(handle, mac)
device = Device(self.ble, handle, mac)
self.handle_to_device[handle] = device
return device.view
return device

def tab_changed(self, i):
pass
Expand All @@ -133,6 +179,7 @@ def run_collection(self):
self.ble.connection_status.connect(self.connection_status)
self.ble.service_result.connect(self.service_result)
self.ble.procedure_completed.connect(self.procedure_completed)
self.ble.info_found.connect(self.info_found)
self.activity_thread.start()

def scan_response(self, args):
Expand Down Expand Up @@ -174,9 +221,12 @@ def tab_exists(self, mac):
def connection_status(self, handle, mac, status):
print "connection_status called", status
if status == BLE.CONNECTED:
idx = self.qtab.addTab(self.make_device_widget(handle, mac), mac)
device = self.make_device_widget(handle, mac)
idx = self.qtab.addTab(device.view, mac)
self.handle_to_mac[handle] = mac
self.qtab.setCurrentIndex(idx)
self.running = self.PROC_PRIMARY
device.primary()
self.ble.primary_service_discovery(handle)

def connect(self):
Expand All @@ -191,8 +241,20 @@ def service_result(self, handle, uuid, start, end):
device = self.handle_to_device[handle]
device.service_result(uuid, start, end)

def info_found(self, handle, char, uuid):
device = self.handle_to_device[handle]
device.info_found(char, uuid)

def procedure_completed(self, handle):
print "procedure completed", handle
print "procedure completed", handle, self.running
device = self.handle_to_device[handle]
if self.running == self.PROC_PRIMARY:
self.running = self.PROC_ATTR
device.scan()
elif self.running == self.PROC_ATTR:
if not device.continue_scan():
self.running = self.PROC_IDLE


def main():
QtCore.QCoreApplication.setOrganizationName("productize")
Expand Down

0 comments on commit 6e7a557

Please sign in to comment.