Skip to content

Commit

Permalink
GAP: Add a event for encryption failed
Browse files Browse the repository at this point in the history
Add a event `GAP_EV_ENCRYPTION_FAILED=91` for encryption failed.

Handle the event `GAP_EV_ENCRYPTION_FAILED`.
Put the received data to `gap.encryption_failed_rcvd.data`.

Add a function `gap_wait_for_encryption_fail` to wait the encryption
failed event.

Signed-off-by: Lyle Zhu <[email protected]>
  • Loading branch information
lylezhu2012 committed Nov 28, 2024
1 parent 81bdfec commit 78ca880
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions autopts/ptsprojects/stack/layers/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self, name, manufacturer_data, appearance, svc_data, flags,
self.passkey = Property(None)
self.conn_params = Property(None)
self.pairing_failed_rcvd = Property(None)
self.encryption_failed_rcvd = Property(None)

# bond_lost data (addr_type, addr)
self.bond_lost_ev_data = Property(None)
Expand Down Expand Up @@ -224,6 +225,12 @@ def gap_wait_for_pairing_fail(self, timeout=5):

return self.pairing_failed_rcvd.data

def gap_wait_for_encryption_fail(self, timeout=5):
if self.encryption_failed_rcvd.data is None:
wait_for_event(timeout, lambda: self.encryption_failed_rcvd.data)

return self.encryption_failed_rcvd.data

def gap_wait_for_lost_bond(self, timeout=5):
if self.bond_lost_ev_data.data is None:
wait_for_event(timeout, lambda: self.bond_lost_ev_data.data)
Expand Down
27 changes: 26 additions & 1 deletion autopts/pybtp/btp/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ def gap_pairing_failed_ev_(gap, data, data_len):
stack.gap.pairing_failed_rcvd.data = (_addr_t, _addr, _reason)


def gap_encryption_failed_ev_(gap, data, data_len):
stack = get_stack()
logging.debug("%s", gap_encryption_failed_ev_.__name__)

logging.debug("received %r", data)

fmt = '<B6sB'
if len(data) != struct.calcsize(fmt):
raise BTPError("Invalid data length")

_addr_t, _addr, _error = struct.unpack_from(fmt, data)
_addr = binascii.hexlify(_addr[::-1]).decode()

logging.debug("received %r", (_addr_t, _addr, _error))

stack.gap.encryption_failed_rcvd.data = (_addr_t, _addr, _error)


def gap_bond_lost_ev_(gap, data, data_len):
logging.debug("%s", gap_bond_lost_ev_.__name__)

Expand Down Expand Up @@ -394,7 +412,8 @@ def gap_passkey_entry_req_ev_(gap, data, data_len):
defs.GAP_EV_PERIODIC_SYNC_ESTABLISHED: gap_padv_sync_established_ev_,
defs.GAP_EV_PERIODIC_SYNC_LOST: gap_padv_sync_lost_ev_,
defs.GAP_EV_PERIODIC_REPORT: gap_padv_report_ev_,
defs.GAP_EV_PERIODIC_TRANSFER_RECEIVED: gap_padv_transfer_received_ev_
defs.GAP_EV_PERIODIC_TRANSFER_RECEIVED: gap_padv_transfer_received_ev_,
defs.GAP_EV_ENCRYPTION_FAILED: gap_encryption_failed_ev_
}


Expand Down Expand Up @@ -435,6 +454,12 @@ def gap_wait_for_pairing_fail(timeout=30):
return stack.gap.gap_wait_for_pairing_fail(timeout)


def gap_wait_for_encryption_fail(timeout=30):
stack = get_stack()

return stack.gap.gap_wait_for_encryption_fail(timeout)


def gap_wait_for_lost_bond(timeout=30):
stack = get_stack()

Expand Down
1 change: 1 addition & 0 deletions autopts/pybtp/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def BIT(bit):
GAP_EV_PERIODIC_SYNC_LOST = 0x8e
GAP_EV_PERIODIC_REPORT = 0x8f
GAP_EV_PERIODIC_TRANSFER_RECEIVED = 0x90
GAP_EV_ENCRYPTION_FAILED = 0x91
GATT_READ_SUPPORTED_COMMANDS = 0x01
GATT_SERVICE_PRIMARY = 0x00
GATT_SERVICE_SECONDARY = 0x01
Expand Down
10 changes: 10 additions & 0 deletions doc/btp_gap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,13 @@ Events:

This event can be sent after IUT received Periodic Advertising Sync
Transfer was received.

Opcode 0x91 - Encryption Failed event

Controller Index: <controller id>
Event parameters: Address_Type (1 octet)
Address (6 octets)
Error (1 octet)

This event can be sent when IUT encryption procedure fails with
specified error.

0 comments on commit 78ca880

Please sign in to comment.