Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ble pairing strategy #222

Merged
merged 20 commits into from
Feb 4, 2022
Merged

Conversation

kvaellning
Copy link

Though not the best solution, this implements an "pairing strategy" which, depending on the user preferences, tries to pair automatically (standard), pairs only when an Apple authentication coprocessor is available, or only via software.

Note that the supported pairing mehtod can only be found out after stage M4, that means that at this point a restart of the process is necessary anyway.

Improvement: directly raise an error on verification of M4, but I am not familiar with the implementation, so I'll do it in a brute force way (retry if anything fails).

@Jc2k
Copy link
Collaborator

Jc2k commented Jan 28, 2022

Some thoughts:

  • This PR creates an asymmetry between the IP and BLE code paths (to be more clear, IP has the same bug)
  • In this comment Unable to Pair Level Bolt #211 (comment), @los93sol says they are able to read the supported pairing modes without being paired. Maybe they can help you do this without needing a retry at all.
  • If they can't, I think the retry logic needs to be better. We should handle the error that is returned at M4 and raise an exception that you can handle. Whitelisting some exceptions and then retrying for everything else seems the wrong way round. This would retry for all sorts of random cases where the retrying wouldn't help.

@kvaellning
Copy link
Author

I also don't like the solution, it's just a "the best I can do for now" ;)

  • asymmetry I can cope for,
  • @los93sol, you'll have any idea how I can access the ff without having IP enabled?
  • Whitelisting was just an afterthought. The problem lies specifically that I have not enough insight in the code on how to proper implement all the steps stated in the spec. However, the error will result in an empty TLV on M5, which causes an attribute error. My first idea was to handle it there, then threw it out, then added the whitelisting and totally forget about it. I will add the specific exception and invite everyone to fix the verifciation of M4.

@Jc2k
Copy link
Collaborator

Jc2k commented Jan 28, 2022

If you don't like it either, maybe we leave out auto for now? And just have a yes/no boolean for start_pairing. But at it to both IP and BLE.

@kvaellning kvaellning force-pushed the ble_pairing_strategy branch 2 times, most recently from 1252836 to 17db6d0 Compare January 28, 2022 11:39
@kvaellning
Copy link
Author

Good point. Will remove the Auto option for now.

For the sake of convenience, I made it symmetrical for IP accessories, though this can be simplified by evaluating the FF flag. Since I don't have an IP accessory to check against, that's something I wouldn't do.

@kvaellning
Copy link
Author

@jlusiardi If you are okay with the solution for now, I would consider it finished. The improvements with the FeatureFlag may be part of another request. I will now take more time on #223

@los93sol
Copy link

I’ll check on the feature stuff tonight or tomorrow morning in my app. The HAP permissions on the characteristic definitely allow unpaired read to it, but I have not yet verified you can actually do that

@jlusiardi
Copy link
Owner

read the supported pairing modes without being paired. Maybe they can help you do this without needing a retry at all.

From an Apple User's point of view I would also expect that we can read all information to pair an unpaired accessory before pairing. Hence there should be no need for a retry.

But since you introduce a new parameter (with a default value though) to start_pairing and start_pairing_ble I would prefer those new parameters exposed in homekit/pair.py and homekit/pair_ble.py as well. Could you @kvaellning add this please?

At best @los93sol finds out, which piece of information from the discover replies indicates which authenication method. I think I could have gotten the feature flag from discover wrong from the beginning...

@kvaellning
Copy link
Author

I will change it asap, but this may take until Monday.
Currently, I only found the feature flag on the IP implementation. For BLE, flags maps the status flag. I‘ll take a look if I find some more information regarding this.

@los93sol
Copy link

Good news, I actually did confirm this previously and confirmed again just now. You can read the value out from the characteristic unpaired.

You want the Pairing service (00000055-0000-1000-8000-0026BB765291) and Pairing Feature Flags characteristic (0000004F-0000-1000-8000-0026BB765291).

What I did is write a HAP Read request to it and my device gave me back

CTRL, TID, STATUS, LEN (2), TLV (3)
0x02, 0x69, 0x00, 0x03, 0x00, 0x01, 0x01, 0x02

So once you strip off the PDU, the TLV value is 0x01, 0x01, 0x02 which translates to...
AdditionalParameterTypes.Value = 1, Length = 1, Value = 2 (SupportsSoftwareAuthentication)

Also want to give a big thanks to everyone who's contributed code to understanding this protocol, it has been extremely helpful for me!

@kvaellning
Copy link
Author

Nice! Will update the stuff on Monday! Thanks guys, and have a nice weekend!

@jlusiardi
Copy link
Owner

This is great news! Thanks @los93sol and i look forward to the updates!

@kvaellning
Copy link
Author

kvaellning commented Jan 31, 2022

I'll added the service and the characteristic, as well as the CLI implementation (currently only local).
I currently try to get the readout running, but somehow I get a None returned when I use read_value on the characteristic.

@kvaellning kvaellning force-pushed the ble_pairing_strategy branch 3 times, most recently from 8f4732b to 613cce9 Compare February 1, 2022 11:05
@kvaellning
Copy link
Author

kvaellning commented Feb 1, 2022

@jlusiardi @Jc2k I finally got it to run. BLE is working fine, IP I cannot check here. Would be nice if someone could comment on this.

Note that I discovered by checking that no set "HwAuth" and "SwAuth" feature flags mean that the accessory is not certified. I left this to be treated as software based authentication, since that appears to be the strategy of Apples Home App.

Robert Schulze added 11 commits February 1, 2022 13:36
As defined in the open source HomeKit ADK, both PairSetup and PairSetupWithAuth
are available options. The main difference lies in the necessary hardware:
PairSetupWithAuth needs an Apple co-processor for the authentication in
pairing step M4.
Though not the best solution, the user may specify if a specific strategy
for pairing should be used (either try both pairing options, or try one and
cope with a possible exception).
Based on the feature flags, the best method for authentication is determined.
Note that no supported authentication method (both flags are not set) is
treated as "supports software authentication".
Having both flags not set may be the case for uncertified accessories, which
have neither co-processor nor the MFi authentication token.
The feature flags are now also extracted during the connection process, to
avoid unnecessary additional calls to the discovery function.
@kvaellning kvaellning force-pushed the ble_pairing_strategy branch from 613cce9 to 1a323d0 Compare February 1, 2022 12:36
@jlusiardi
Copy link
Owner

jlusiardi commented Feb 1, 2022

Did you run the tests? find_device_ip_and_port was tested before but no tests were changed. I just updated my Master Branch to make sure the tests run ok (which they do now).

@kvaellning
Copy link
Author

Good point, forgot about the tests since I only added the implementation folder to my workspace. D'Oh!

@jlusiardi
Copy link
Owner

I am already also looking into them

@kvaellning
Copy link
Author

I've got the feature flag and BLE test to run again, but currently it's not checking against authentication method errors.

@kvaellning kvaellning force-pushed the ble_pairing_strategy branch from 52c09e5 to 37ccf59 Compare February 3, 2022 08:22
@jlusiardi jlusiardi merged commit 6fc86d6 into jlusiardi:master Feb 4, 2022
@kvaellning kvaellning deleted the ble_pairing_strategy branch February 4, 2022 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants