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

Unknown error on initial pair setup #12

Closed
mjg59 opened this issue Mar 11, 2018 · 14 comments
Closed

Unknown error on initial pair setup #12

mjg59 opened this issue Mar 11, 2018 · 14 comments
Labels

Comments

@mjg59
Copy link
Contributor

mjg59 commented Mar 11, 2018

Trying to pair with an Ikea Tradfri running the latest (Homekit-compatible) firmware:

Step #3: unkown error

Packet dump of the transaction attached
pairing.zip

@mjg59
Copy link
Contributor Author

mjg59 commented Mar 11, 2018

I've previously played with a couple of Homekit devices that were very picky about header ordering, so I don't know if that could be contributing. I don't have a convenient iOS device - I'll try to find one so I can see what the functioning pairing looks like

@mjg59
Copy link
Contributor Author

mjg59 commented Mar 12, 2018

Ok, that doesn't seem to be it - sending an apparently identical request using curl (mocking up the same headers and ordering gives a more reasonable response.
pairing3.zip

@jlusiardi
Copy link
Owner

i just looked into your dump of pairing3.
Apple's spec says TLV items for state (M1) and method (Pair Setup) must be
transmitted. I am not sure if the ordering of the TLV items is relevant. Can
you try to send
0x06 0x01 0x01 0x00 0x01 0x01

instead of

0x00 0x01 0x01 0x06 0x01 0x01?

Perhaps i missed that in the documentation or IKEA Tradfi just relies on it.

@mjg59
Copy link
Contributor Author

mjg59 commented Mar 12, 2018

The problem is because http.client (in 3.5, at least) sends the headers and the body as separate packets, and this device mishandles the case. Monkey-patching http.client to send in a single packet fixes it.

@jlusiardi
Copy link
Owner

is this something to be fixed within this project?

@mjg59
Copy link
Contributor Author

mjg59 commented Mar 18, 2018

I don't think it can easily be fixed there, but I'll think about it some more

@nickpl456
Copy link

@mjg59 I'm running into this issue as well on a Leviton switch, could you give some detail on what you had to "monkey-patch"? (can't say I've heard that term before, I'm not much of a programer though :) )

@mjg59
Copy link
Contributor Author

mjg59 commented Apr 30, 2018

@nickpl456 Monkey Patching in Python refers to replacing a method in an imported module with your own method. In Home Assistant I have this:

def homekit_http_send(self, message_body=None, encode_chunked=False):
    r"""Send the currently buffered request and clear the buffer.

    Appends an extra \r\n to the buffer.
    A message_body may be specified, to be appended to the request.
    """
    self._buffer.extend((b"", b""))
    msg = b"\r\n".join(self._buffer)
    del self._buffer[:]

    if message_body is not None:
        msg = msg + message_body

    self.send(msg)

and then later do:

        http.client.HTTPConnection._send_output = homekit_http_send

to replace the _send_output function with my own. If you do that at the start of your code you ought to be able to get further.

jlusiardi added a commit that referenced this issue May 1, 2018
@jlusiardi
Copy link
Owner

jlusiardi commented May 1, 2018

I just tried to fix the issue without changing the behavior of the original http.client.HTTPConnection for all the code running in the interpreter. what do you think about 61bf4e9 ?

1_paket.pcapng.zip

2_pakets.pcapng.zip

@nickpl456
Copy link

Just tested out 61bf4e9 and it works great, I'm able to successfully pair with and control my Leviton switch, thanks guys!

@mjg59
Copy link
Contributor Author

mjg59 commented May 1, 2018

I think you need to add an extra (and unused) named argument for 3.6 compatibility - see mjg59/home-assistant@705b430

@mjg59
Copy link
Contributor Author

mjg59 commented May 1, 2018

(But otherwise this looks good!)

@jlusiardi
Copy link
Owner

thanks for the hint for python 3.6 @mjg59. So either way of patching this will be an ongoing process, I guess.

@jlusiardi
Copy link
Owner

I would close this as done then.

@jlusiardi jlusiardi added the bug label Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants