From 048cd2a887fb2c585e26edd63bec3c581e8f8c57 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Thu, 14 Mar 2019 17:28:26 +0530 Subject: [PATCH] esp_prov : Catch DBus exception when reading/writing to BLE GATT characteristic This is useful in the context of provisioning when server initiates disconnection if secure session establishment fails. --- tools/esp_prov/transport/ble_cli.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/esp_prov/transport/ble_cli.py b/tools/esp_prov/transport/ble_cli.py index b6ea225c66..04d2921aca 100644 --- a/tools/esp_prov/transport/ble_cli.py +++ b/tools/esp_prov/transport/ble_cli.py @@ -162,8 +162,17 @@ class BLE_Bluez_Client: path = self.characteristics[characteristic_uuid] except KeyError: raise RuntimeError("Invalid characteristic : " + characteristic_uuid) - path.WriteValue([ord(c) for c in data], {}, dbus_interface='org.bluez.GattCharacteristic1') - return ''.join(chr(b) for b in path.ReadValue({}, dbus_interface='org.bluez.GattCharacteristic1')) + + try: + path.WriteValue([ord(c) for c in data], {}, dbus_interface='org.bluez.GattCharacteristic1') + except dbus.exceptions.DBusException as e: + raise RuntimeError("Failed to write value to characteristic " + characteristic_uuid + ": " + str(e)) + + try: + readval = path.ReadValue({}, dbus_interface='org.bluez.GattCharacteristic1') + except dbus.exceptions.DBusException as e: + raise RuntimeError("Failed to read value from characteristic " + characteristic_uuid + ": " + str(e)) + return ''.join(chr(b) for b in readval) # --------------------------------------------------------------------