micropython / micropython/micropython-lib

Handling the # TODO: Handle failure in the _IRQ_ENCRYPTION_UPDATE event

Open
#868 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

I am using the btstack for the PICO_W. We have needed to do significant work to add pairing/encryption support from the BtStack but for the most part I think we have got the basics working. What I need to know is what to do with the aioble objects when errors occur. I have modified the code a little (slowly adding typing as I learn what things are) to look as follows:

pair_status: int = -1
def _security_irq(event: int, data: tuple[int, bool, bool, bool, int, int]):
    global _modified
    global pair_status

    if event == _IRQ_ENCRYPTION_UPDATE:
        pair_status = -1
        # Connection has updated (usually due to pairing).
        conn_handle, encrypted, authenticated, bonded, key_size, status = data
        pair_status = status
        logger.info("LNI: AIOBLE: encryption update") # type: ignore
        if connection := DeviceConnection._connected.get(conn_handle, None):
            logger.info(f'LNI: AIOBLE: Event triggered. Pairing status: {pair_status}') # type: ignore
            connection.encrypted = encrypted
            connection.authenticated = authenticated
            connection.bonded = bonded
            connection.key_size = key_size
            if pair_status == 19: #ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION (in Bt Stack code - not so in MP code)
                del DeviceConnection._connected[conn_handle]  # Signal as disconnected

            # TODO: Handle failure.
            #if encrypted and connection._pair_event:
            connection._pair_event.set()

I am assuming the status values are those defined in the btstack file bluetooth.h such that status value 0x13 is, for example, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION

What I dont know is exactly what to do in each case. Is the del DeviceConnection._connected[conn_handle] sufficient for the ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION case? Should I always set the event if the connection is not None?

What should be done if if connection := DeviceConnection._connected.get(conn_handle, None) gives None?

Are there certain exceptions I should raise to inform the application or do I need to create one?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing _security_irq and DeviceConnection._connected, then compare the reported status values with bluetooth.h and the existing connection and pairing event handling. Determine the expected behavior for encryption failures, disconnections, missing connections, and pairing events; done means the application receives consistent, documented outcomes for each case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.