Clarification on aioble's use of ThreadSafeFlag class. Flags accumulate or is it a bool? #14131
Unanswered
brianreinhold
asked this question in
Libraries & Drivers
Replies: 1 comment 3 replies
-
See micropython/extmod/asyncio/event.py Lines 45 to 64 in 9d27183 |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am not sure how this ThreadSafeFlag class is supposed to work. Is it like a posix semaphore? IN other words, if make a call like this several times:
does it take three
connection._pair_event.set()
calls to clear it?I should also ask - if cumulative, is it FIFO or PUSH/POP? If bool and two calls are made, does the last override the first?
Why do I care? Because of bonding and bonded reconnects. It turns out that the btstack (used on the PICO-W port) handles security requests internally. In other words, aioble and the application using aioble have no idea pairing/bonding happened. On the other hand, insufficient authentication/encryption errors have to be handled by the application (calling the aioble pair method). Some devices issue the security request on a first time connect but not a bonded reconnect, and some devices issue a security request instead of an insufficient authentication/encryption error. (Some devices dont pair at all.) Need to handle all cases.
So how to deal with this? First of all, I need to know that pairing/encryption happened under the hood (due to a security request). So I added a method
security_request_check
to aioble's security module that was identical to itspair
method except it did not call theBLE gap_pair()
method. All it did was call theconnection._pair_event = asyncio.ThreadSafeFlag()
. Then I needed to add the RE-ENCRYPTION event in the low level C code to the list of events that triggered theIRQ_ENCRYPTION_UPDATE
event in aioble.Now at the start of every connection, this
security_request_check
is run as a task and if pairing/encryption is done by a security request, both aioble and the application will know about. The task will terminate should pairing/encryption happen. It works.But now I have a problem if the device sends an authentication failure error, In this case I have to invoke pairing. So I call the aioble pair method and it also sets the
connection._pair_event = asyncio.ThreadSafeFlag()
. It works (pairing or encryption ensues at the low levels), but the background security task gets the event when the pairing/encryption is done. For the application, the pairing call remains in theawait
state until a timeout is reached. Not good. The application is locked until the timeout. So it looks like the events are 'stacked'. I have added another method to aioble's security modulewhich does nothing but set the event again, releasing the pair method. It SEEMS to work but it is a hard thing to test.
In any case, that is why I want to know if repeated calls to
asyncio.ThreadSafeFlag()
accumulate or is it just a bool?Beta Was this translation helpful? Give feedback.
All reactions