Hook up UUID changes to be written in the config file

Make the uuid property a GObject.Property so we can listen to it. Notify about
the uuid change at the end of the pairing process, then write that value into
the device's config file.
This commit is contained in:
Peter Hutterer 2018-01-19 16:04:05 +10:00
parent 0deb5f8e49
commit 4ee07a18b6
3 changed files with 50 additions and 8 deletions

11
tuhi.py
View File

@ -77,13 +77,15 @@ class TuhiDevice(GObject.Object):
over Tuhi's DBus interface
"""
def __init__(self, bluez_device, tuhi_dbus_device, uuid=None, paired=True):
def __init__(self, bluez_device, tuhi_dbus_device, config, uuid=None, paired=True):
GObject.Object.__init__(self)
self.config = config
self._tuhi_dbus_device = tuhi_dbus_device
self._wacom_device = WacomDevice(bluez_device, uuid)
self._wacom_device.connect('drawing', self._on_drawing_received)
self._wacom_device.connect('done', self._on_fetching_finished, bluez_device)
self._wacom_device.connect('button-press-required', self._on_button_press_required)
self._wacom_device.connect('notify::uuid', self._on_uuid_updated, bluez_device)
self.drawings = []
# We need either uuid or paired as false
assert uuid is not None or paired is False
@ -147,6 +149,9 @@ class TuhiDevice(GObject.Object):
def _on_button_press_required(self, device):
self._tuhi_dbus_device.notify_button_press_required()
def _on_uuid_updated(self, wacom_device, pspec, bluez_device):
self.config.new_device(bluez_device.address, wacom_device.uuid)
class Tuhi(GObject.Object):
__gsignals__ = {
@ -213,7 +218,7 @@ class Tuhi(GObject.Object):
logger.debug('{}: uuid {}'.format(bluez_device.address, uuid))
tuhi_dbus_device = self.server.create_device(bluez_device)
d = TuhiDevice(bluez_device, tuhi_dbus_device, uuid=uuid)
d = TuhiDevice(bluez_device, tuhi_dbus_device, self.config, uuid=uuid)
self.devices[bluez_device.address] = d
def _on_bluez_discovery_started(self, manager):
@ -232,7 +237,7 @@ class Tuhi(GObject.Object):
if Tuhi._is_pairing_device(bluez_device):
tuhi_dbus_device = self.server.create_device(bluez_device, paired=False)
d = TuhiDevice(bluez_device, tuhi_dbus_device, paired=False)
d = TuhiDevice(bluez_device, tuhi_dbus_device, self.config, paired=False)
self.devices[bluez_device.address] = d
logger.debug('{}: call Pair() on device'.format(bluez_device.objpath))

View File

@ -24,6 +24,10 @@ logger = logging.getLogger('tuhi.config')
ROOT_PATH = os.path.join(xdg.BaseDirectory.xdg_data_home, 'tuhi')
def is_btaddr(addr):
return re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$', addr) is not None
class TuhiConfig(GObject.Object):
def __init__(self):
GObject.Object.__init__(self)
@ -48,7 +52,7 @@ class TuhiConfig(GObject.Object):
if entry.is_file():
continue
if not re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$', entry.name):
if not is_btaddr(entry.name):
continue
path = os.path.join(ROOT_PATH, entry.name, 'settings.ini')
@ -62,3 +66,31 @@ class TuhiConfig(GObject.Object):
assert config['Device']['Address'] == entry.name
self._devices[entry.name] = config['Device']
def new_device(self, address, uuid):
assert is_btaddr(address)
assert len(uuid) == 12
logger.debug("{}: adding new config, uuid {}".format(address, uuid))
path = os.path.join(ROOT_PATH, address)
try:
os.mkdir(path)
except FileExistsError:
pass
# The ConfigParser default is to write out options as lowercase, but
# the ini standard is Capitalized. But it's convenient to have
# write-out nice but read-in flexible. So have two different config
# parsers for writing and then for handling the reads later
path = os.path.join(path, 'settings.ini')
config = configparser.ConfigParser()
config.optionxform = str
config.read(path)
config['Device']['Address'] = address
config['Device']['UUID'] = uuid
with open(path, 'w') as configfile:
config.write(configfile)
config = configparser.ConfigParser()
config.read(path)
self._devices[address] = config['Device']

View File

@ -136,7 +136,7 @@ class WacomDevice(GObject.Object):
self.width = WACOM_SLATE_WIDTH
self.height = WACOM_SLATE_HEIGHT
self.name = device.name
self.uuid = uuid
self._uuid = uuid
self._is_running = False
@ -147,6 +147,11 @@ class WacomDevice(GObject.Object):
device.connect_gatt_value(NORDIC_UART_CHRC_RX_UUID,
self._on_nordic_data_received)
@GObject.Property
def uuid(self):
assert self._uuid is not None
return self._uuid
def is_slate(self):
return self.name == "Bamboo Slate"
@ -595,7 +600,6 @@ class WacomDevice(GObject.Object):
fw_high = self.get_firmware_version(0)
fw_low = self.get_firmware_version(1)
logger.info(f'firmware is {fw_high}-{fw_low}')
logger.info("pairing completed")
def pair_device_spark(self):
try:
@ -621,15 +625,16 @@ class WacomDevice(GObject.Object):
fw_high = self.get_firmware_version(0)
fw_low = self.get_firmware_version(1)
logger.info(f'firmware is {fw_high}-{fw_low}')
logger.info("pairing completed")
def pair_device(self):
self.uuid = uuid.uuid4().hex[:12]
self._uuid = uuid.uuid4().hex[:12]
logger.debug("{}: pairing device, assigned {}".format(self.device.address, self.uuid))
if self.is_slate():
self.pair_device_slate()
else:
self.pair_device_spark()
logger.info('pairing completed')
self.notify('uuid')
def run(self):
if self._is_running: