Skip to content

Commit

Permalink
add disconnect command; add Productize/count service; add Int32 print…
Browse files Browse the repository at this point in the history
…er class
  • Loading branch information
andete committed Apr 17, 2014
1 parent 18da649 commit e804a16
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
11 changes: 11 additions & 0 deletions ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BLE(QtCore.QObject):
attr_value = QtCore.Signal(int, int, int, list)

CONNECTED = 0
DISCONNECTED = 1

def __init__(self, baud_rate, packet_mode = False):
import data
Expand Down Expand Up @@ -119,6 +120,7 @@ def start(self, port = None):
self.send_command(ble.ble_cmd_hardware_io_port_write(0, 1, 0))
# handle connections
self.ble.ble_evt_connection_status += self.handle_connection_status
self.ble.ble_evt_connection_disconnected += self.handle_connection_disconnected
# handle service info
self.ble.ble_evt_attclient_group_found += self.handle_attclient_group_found
# get local address
Expand Down Expand Up @@ -157,6 +159,12 @@ def handle_connection_status(self, sender, args):
print "Connected to %d %s" % (h, f)
self.connection_status.emit(h, f, self.CONNECTED)

def handle_connection_disconnected(self, sender, args):
h = args['connection']
reason = args['reason']
print "Disconnected to %d because %s" % (h, reason)
self.connection_status.emit(h, "", self.DISCONNECTED)

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'])
Expand Down Expand Up @@ -191,6 +199,9 @@ def connect_direct(self, target):
address, addr_type, conn_interval_min,
conn_interval_max, timeout, slave_latency))

def disconnect(self, handle):
return self.send_command(self.ble.ble_cmd_connection_disconnect(handle))

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(self.uuid.attr_by_name['Primary'].uuid))))
Expand Down
2 changes: 1 addition & 1 deletion collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def print_scan_response(ble, args):
(name, data) = ble.data_to_string(args['data'])
if not 'Hum' in name: return
#if not 'Hum' in name: return
print "gap_scan_response",
t = datetime.datetime.now()
disp_list = []
Expand Down
13 changes: 11 additions & 2 deletions data/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

import printers

import uuid

def uuid_to_list(uuidx):
u = uuid.UUID(uuidx)
return [ord(x) for x in u.bytes]

class Attr:

def __init__(self, uuid, name, printer=printers.Default):
self.uuid = uuid
def __init__(self, uuidx, name, printer=printers.Default):
if type(uuidx) == type("hello"):
self.uuid = uuid_to_list(uuidx)
else:
self.uuid = uuidx
self.name = name
self.printer = printer()
self.suuid = printers.print_uuid(self.uuid)
Expand Down
5 changes: 5 additions & 0 deletions data/printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class String:
def printv(self, val):
return ''.join(["%c" % x for x in val])

class Int32:

def printv(self, val):
return str(val[0] + 256*val[1] + 256*256*val[2] + 256*256*256*val[3])

class Char(Default):

def printv(self, val):
Expand Down
6 changes: 5 additions & 1 deletion data/productize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# (c) 2014 Productize <[email protected]>

from attr import Attr
import printers

attrs = []
attrs = [
Attr("ac842717-f24c-46f7-bf49-5393f0124f8d", "Count Service"),
Attr("5b97ab09-9065-478c-9ce8-9c9dcb2c5922", "Count", printers.Int32),
]

class Vendor:

Expand Down
20 changes: 20 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def __init__(self):
self.add_action(fileMenu, '&Quit', self.close, 'Ctrl+Q')
deviceMenu = menuBar.addMenu('&Device')
self.add_action(deviceMenu, '&Connect', self.double_click)
self.add_action(deviceMenu, '&Disconnect', self.disconnect)
helpMenu = menuBar.addMenu('&Help')
self.add_action(helpMenu, '&About', self.about)
self.qtab = QtGui.QTabWidget()
Expand All @@ -228,6 +229,7 @@ def __init__(self):
self.setWindowTitle("BTLE-explorer using device "+self.ble.address)
self.handle_to_mac = {}
self.handle_to_device = {}
self.mac_to_handle = {}
self.running = self.PROC_IDLE
self.status("Ready.")
self.run_collection()
Expand Down Expand Up @@ -286,6 +288,7 @@ def _add(text, slot = None):
def make_device_widget(self, handle, mac):
device = Device(self.ble, handle, mac)
self.handle_to_device[handle] = device
self.handle_to_device[handle] = device
return device

def tab_changed(self, i):
Expand Down Expand Up @@ -348,12 +351,21 @@ def connection_status(self, handle, mac, status):
device = self.make_device_widget(handle, mac)
idx = self.qtab.addTab(device.view, mac)
self.handle_to_mac[handle] = mac
self.mac_to_handle[mac] = handle
self.qtab.setCurrentIndex(idx)
self.running = self.PROC_PRIMARY
QtGui.QApplication.setOverrideCursor(Qt.WaitCursor)
device.primary()
self.status("service discovery running")
self.ble.primary_service_discovery(handle)
elif status == BLE.DISCONNECTED:
try:
mac = self.handle_to_mac[handle]
idx = self.tab_exists(mac)
if not idx is None:
self.qtab.removeTab(idx)
except KeyError: # old stale connection
pass

def double_click(self):
idx = self.tab_exists(self.selected_device)
Expand All @@ -364,6 +376,14 @@ def double_click(self):
else:
self.qtab.setCurrentIndex(idx)

def disconnect(self):
idx = self.tab_exists(self.selected_device)
if not idx is None:
if not self.selected_device is None:
self.status("disconnecting")
self.qtab.setCurrentIndex(idx)
self.ble.disconnect(self.mac_to_handle[self.selected_device])

def service_result(self, handle, uuid, start, end):
device = self.handle_to_device[handle]
device.service_result(uuid, start, end)
Expand Down

0 comments on commit e804a16

Please sign in to comment.