jlusiardi / homekit_python

A python implementation to work as both HomeKit controller and accessory.
Apache License 2.0
221 stars 41 forks source link

Bluetooth LE Accessories #56

Closed der-punkt closed 5 years ago

der-punkt commented 5 years ago

Hey,

what is needed to implement the support for Bluetooth LE Accessories?

I don't have much free time, but I'm motivated to contribute here.

Best regards

jlusiardi commented 5 years ago

I guess there are a few points unclear to me:

der-punkt commented 5 years ago

Hi jlusiardi,

first of all, sorry, it took me a whole week to write back.

  • How does python work with bluetooth LE?

I found that there is a Bluetooth LE library for python: https://github.com/IanHarvey/bluepy

  • How to discover Accessories?

Here is some general code for discovering BTLE devices:

from bluepy.btle import Scanner, DefaultDelegate

class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print "Discovered device", dev.addr
        elif isNewData:
            print "Received new data from", dev.addr

scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)

for dev in devices:
    print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
    for (adtype, desc, value) in dev.getScanData():
        print "  %s = %s" % (desc, value)
  • How to communicate with the accessories?

I had chance to take only a short look at the HAP specification. However, this may be a good starting point:

from bluepy.btle import Peripheral, ADDR_TYPE_RANDOM

addr = "xx:xx:xx:xx:xx:xx"
dev = Peripheral(addr, ADDR_TYPE_RANDOM)

srvc_pairing = "00000055-0000-1000-8000-0026BB765291"
ch_pair_setup = "0000004C-0000-1000-8000-0026BB765291"
ch_pair_verify = "0000004E-0000-1000-8000-0026BB765291"
ch_pairing_features = "0000004F-0000-1000-8000-0026BB765291"
ch_pairing_pairings = "00000050-0000-1000-8000-0026BB765291"

service = dev.getServiceByUUID(srvc_pairing)
print "Service: ", service.uuid.getCommonName()
for characteristic in service.getCharacteristics():
    print "  Characteristic: ", characteristic.uuid.getCommonName()
    print "    Properties: ", characteristic.propertiesToString()
    if characteristic.supportsRead():
        print "    Value: ", (characteristic.read(),)

All the best!

jlusiardi commented 5 years ago

Hey,

that helps a lot, thanks! I already started tinkering with gatt-python, but i will also consider bluepy. Both seem to have one downside: linux with bluez only? Lets have a look :)

Regards Joachim

jlusiardi commented 5 years ago

Hey again,

https://github.com/markrages/ble states that bluepy uses the hci socket interface to bluez. This works well but it is deprecated and requires root access.

So this doesn't look so good :/

jlusiardi commented 5 years ago

Also interesting: which library allows access to the advertisement packages?

jlusiardi commented 5 years ago

Success so far (with some pretty ugly hacks):

Discovery

Using subprocess to use hcitool & hcidump

Name: Koogeek-DW1-8ca86c
MAC: e0:44:6a:b8:05:e0
Configuration number (c#): 1
Device ID (id): da9ae07f2e4e
Compatible Version (cv): 2
State Number (s#): 2
Status Flags (sf): unpaired
Category Identifier (ci): (Id: 10)

Reading out services & characteristics

1: >accessory-information<
  1.4:  () >manufacturer< [pr]
  1.14:  () >firmware.revision< [pr]
  1.3:  () >identify< [pw]
  1.6:  () >serial-number< [pr]
  1.2:  () >name< [pr]
  1.15:  () >hardware.revision< [pr]
  1.5:  () >model< [pr]
None: >Unknown Service: 00001801-0000-1000-8000-00805F9B34FB<
1003: >Unknown Service: 4AAAF961-0DEC-11E5-B939-0800200C9A66<
  1003.1005:  (Time_zone) >Unknown Characteristic 4AAAF963-0DEC-11E5-B939-0800200C9A66< [pr,pw]
  1003.1004:  (Time_value) >Unknown Characteristic 4AAAF962-0DEC-11E5-B939-0800200C9A66< [pr,pw]
91: >contact<
  91.1002:  (One_hundred_data) >Unknown Characteristic 4AAAF966-0DEC-11E5-B939-0800200C9A66< [pr]
  91.1001:  (Thirday_one_day_data) >Unknown Characteristic 4AAAF965-0DEC-11E5-B939-0800200C9A66< [pr]
  91.93:  (Contact_sensor_state) >contact-state< [pr,evc,evd]
  91.1000:  (Today_data) >Unknown Characteristic 4AAAF964-0DEC-11E5-B939-0800200C9A66< [pr]
  91.98:  (Name) >name< [pr]
7: >service<
  7.60000:  () >service-signature< [pr]
  7.8:  () >version< [pr]
9: >pairing<
  9.13:  () >pairing.pairings< [pr,pw]
  9.10:  () >pairing.pair-setup< [r,w]
  9.12:  () >pairing.features< [r]
  9.11:  () >pairing.pair-verify< [r,w]
900: >Unknown Service: 00001530-1212-EFDE-1523-785FEABCD123<
  900.901:  (DFU Control Point) >Unknown Characteristic 00001531-1212-EFDE-1523-785FEABCD123< [pw,hd]
  900.902:  (Service Signature) >service-signature< [r,pr,hd]
57: >battery<
  57.60:  (Charging_state) >charging-state< [pr,evc,evd]
  57.59:  (Battery_level) >battery-level< [pr,evc,evd]
  57.61:  (Status_low_battery) >status-lo-batt< [pr,evc,evd]
der-punkt commented 5 years ago

Looking good! Can I contribute with something?

jlusiardi commented 5 years ago

Hey,

you could test the scripts from branch add_bluetooth from the staging directory:

The first should try to discover all BLE HomeKit devices:

sudo python3 staging/homekit_discovery.py

E.g.:

Name: Koogeek-DW1-8ca86c
MAC: e0:44:6a:b8:05:e0
Configuration number (c#): 1
Device ID (id): da9ae07f2e4e
Compatible Version (cv): 2
State Number (s#): 2
Status Flags (sf): unpaired
Category Identifier (ci): (Id: 10)

The second is called with a mac address:

python3 staging/hap_char_sig_read.py e0:44:6a:b8:05:e0

Would be cool to get some input data and feedback!

Regards Joachim

der-punkt commented 5 years ago

Hi Joachim,

thank you for the response. I tried them out with the following results:

$ sudo python3 staging/homekit_discovery.py
Set scan parameters failed: Input/output error
Name: A60 Ce
MAC: c8:ea:1b:6f:b2:10
Configuration number (c#): 1
Device ID (id): 6f21816f8519
Compatible Version (cv): 2
State Number (s#): 1
Status Flags (sf): unpaired
Category Identifier (ci): (Id: 5)

but $ python3 staging/hap_char_sig_read.py c8:ea:1b:6f:b2:10 doesn't produce any output it is just stuck somewhere in manager.run().

Best regards, Vasil

jlusiardi commented 5 years ago

Hey Vasil,

is this some Osram Smart+ product?

It does not even print resolved? That's strange. Some more questions:

Thanks for the support!

Joachim

der-punkt commented 5 years ago

Hi Joachim,

yes, it is a Osram Smart+ LED light bulb.

On the side: Interestingly, after I paired the device and your script homekit_discovery.py got this status also right, but bluetoothhctl still shows it as unpaired.

I am also running bluez 5.43. Below is the test run in bluetoothhctl (PS Sorry for the long log):

Console ```shell [NEW] Controller B8:27:EB:49:44:C9 raspberrypi.local [default] [NEW] Device C8:EA:1B:6F:B2:10 A60 Ce [bluetooth]# info C8:EA:1B:6F:B2:10 Device C8:EA:1B:6F:B2:10 Name: A60 Ce Alias: A60 Ce Paired: no Trusted: no Blocked: no Connected: no LegacyPairing: no ManufacturerData Key: 0x004c ManufacturerData Value: 0x06 ManufacturerData Value: 0x6d ManufacturerData Value: 0x00 ManufacturerData Value: 0x6f ManufacturerData Value: 0x21 ManufacturerData Value: 0x81 ManufacturerData Value: 0x6f ManufacturerData Value: 0x85 ManufacturerData Value: 0x19 ManufacturerData Value: 0x05 ManufacturerData Value: 0x00 ManufacturerData Value: 0x13 ManufacturerData Value: 0x00 ManufacturerData Value: 0x01 ManufacturerData Value: 0x02 [bluetooth]# connect C8:EA:1B:6F:B2:10 Attempting to connect to C8:EA:1B:6F:B2:10 [CHG] Device C8:EA:1B:6F:B2:10 Connected: yes Connection successful [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0001 00001801-0000-1000-8000-00805f9b34fb Generic Attribute Profile [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028 0000003e-0000-1000-8000-0026bb765291 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0029 e604e95d-a759-4817-87d3-aa005083a0d1 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char002b 00000014-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char002b/desc002d dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char002e 00000020-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char002e/desc0030 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0031 00000021-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0031/desc0033 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0034 00000023-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0034/desc0036 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0037 00000030-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char0037/desc0039 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char003a 00000052-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0028/char003a/desc003c dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0040 000000a2-0000-1000-8000-0026bb765291 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0040/char0041 e604e95d-a759-4817-87d3-aa005083a0d1 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0040/char0043 00000037-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0040/char0043/desc0045 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050 00000043-0000-1000-8000-0026bb765291 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0051 e604e95d-a759-4817-87d3-aa005083a0d1 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0053 000000a5-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0053/desc0055 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0056 00000008-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0056/desc0058 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0056/desc0059 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char005e 00000013-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char005e/desc0060 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char005e/desc0061 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0062 0000002f-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0062/desc0064 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0062/desc0065 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0080 00000025-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0080/desc0082 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0080/desc0083 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0084 00000023-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service0050/char0084/desc0086 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0 00000055-0000-1000-8000-0026bb765291 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f1 e604e95d-a759-4817-87d3-aa005083a0d1 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f3 0000004c-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f3/desc00f5 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f6 0000004e-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f6/desc00f8 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f9 0000004f-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00f9/desc00fb dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00fc 00000050-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/service00f0/char00fc/desc00fe dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff00 ae5d1e47-5c13-43a0-8635-82ad38a1381f Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff00/charff01 a3dd50bf-f7a7-4e99-838e-570a086c661b Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff00/charff01/descff03 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff00/charff04 a2e86c7a-d961-4091-b74f-2409e72efe26 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff00/charff06 a47f7608-2e2d-47eb-913b-75d4edc4de4b Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11 b0733e83-8434-4c00-a344-25d1c982a0ef Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff12 e604e95d-a759-4817-87d3-aa005083a0d1 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff14 000000a5-0000-1000-8000-0026bb765291 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff14/descff16 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff17 b176bd7f-4148-47bd-a6c6-9d0796e96183 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff17/descff19 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff17/descff1a 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff1b b2fd7f2d-ead3-4f17-b16c-202ec758c697 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff1b/descff1d dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff1e b31259a5-9acc-45c2-838a-956f57825196 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff11/charff1e/descff20 dc46f0fe-81d2-4616-b5d9-6abdd796939a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff3a 1b7e8251-2877-41c3-b46e-cf057c562023 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff3a/charff3b 5e9bf2a8-f93f-4481-a67e-3b2f4a07891a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff3a/charff3e 8ac32d3f-5cb9-4d44-bec2-ee689169f626 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_C8_EA_1B_6F_B2_10/serviceff3a/charff3e/descff40 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 0000003e-0000-1000-8000-0026bb765291 [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 00000043-0000-1000-8000-0026bb765291 [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 00000055-0000-1000-8000-0026bb765291 [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 000000a2-0000-1000-8000-0026bb765291 [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 00001800-0000-1000-8000-00805f9b34fb [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 00001801-0000-1000-8000-00805f9b34fb [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: 1b7e8251-2877-41c3-b46e-cf057c562023 [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: ae5d1e47-5c13-43a0-8635-82ad38a1381f [CHG] Device C8:EA:1B:6F:B2:10 UUIDs: b0733e83-8434-4c00-a344-25d1c982a0ef [CHG] Device C8:EA:1B:6F:B2:10 ServicesResolved: yes [A60 Ce]# ```

Thank you for spending time on this!

Vasil

jlusiardi commented 5 years ago

Hi,

my script prints the homekit pairing status not the bluetooth pairing (as in bluetoothctrl) status.

The bluetoothctrl looks ok wondering...

Regards Joachim

jlusiardi commented 5 years ago

Hey Vasil,

I think we had a misunderstanding. Sorry :/

Pushed some changes to hap_char_sig_read.py.

I meant: can you start bluetoothctl in one terminal, do nothing with it and run PYTHONPATH=. python3 staging/hap_char_sig_read.py c8:ea:1b:6f:b2:10 --log=DEBUG in an other terminal and send me this output?

Regards Joachim

der-punkt commented 5 years ago

Hey Joachim,

Sorry I was offline so long :/

I understand now :)

Output ```shell $ PYTHONPATH=. python3 staging/hap_char_sig_read.py c8:ea:1b:6f:b2:10 --log=DEBUG 2018-11-18 22:14:35,795 hap_char_sig_read.py:214 DEBUG using adapter hci0 2018-11-18 22:14:36,017 hap_char_sig_read.py:136 DEBUG discovered 6c:40:08:a6:e6:c9 2018-11-18 22:14:36,044 hap_char_sig_read.py:136 DEBUG discovered 71:82:05:83:4b:51 2018-11-18 22:14:36,336 hap_char_sig_read.py:136 DEBUG discovered 48:80:c2:3f:d3:ec 2018-11-18 22:14:36,340 hap_char_sig_read.py:136 DEBUG discovered 16:81:d3:a9:fe:8d 2018-11-18 22:14:36,429 hap_char_sig_read.py:136 DEBUG discovered 6c:40:08:a6:e6:c9 2018-11-18 22:14:36,661 hap_char_sig_read.py:136 DEBUG discovered c8:ea:1b:6f:b2:10 2018-11-18 22:14:36,662 hap_char_sig_read.py:218 DEBUG discovered 2018-11-18 22:14:36,678 hap_char_sig_read.py:222 DEBUG connecting to device 2018-11-18 22:14:46,704 hap_char_sig_read.py:224 DEBUG connected to device 2018-11-18 22:14:46,705 hap_char_sig_read.py:227 DEBUG start manager 2018-11-18 22:14:49,556 hap_char_sig_read.py:144 DEBUG resolved 8 services 2018-11-18 22:14:49,560 hap_char_sig_read.py:146 DEBUG stopped manager 2018-11-18 22:14:49,561 hap_char_sig_read.py:152 DEBUG found service with UUID 1b7e8251-2877-41c3-b46e-cf057c562023 (Unknown Service: 1B7E8251-2877-41C3-B46E-CF057C562023) 2018-11-18 22:14:49,564 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 5e9bf2a8-f93f-4481-a67e-3b2f4a07891a (Unknown Characteristic 5E9BF2A8-F93F-4481-A67E-3B2F4A07891A) 2018-11-18 22:14:49,565 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 8ac32d3f-5cb9-4d44-bec2-ee689169f626 (Unknown Characteristic 8AC32D3F-5CB9-4D44-BEC2-EE689169F626) 2018-11-18 22:14:49,718 hap_char_sig_read.py:152 DEBUG found service with UUID 00000055-0000-1000-8000-0026bb765291 (pairing) 2018-11-18 22:14:49,719 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000050-0000-1000-8000-0026bb765291 (pairing.pairings) 2018-11-18 22:14:49,815 hap_char_sig_read.py:178 DEBUG read characteristic id 34 2018-11-18 22:14:50,206 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb26000080001000005000000007021e000610915276bb2600008000100000550000000a0230000c071b000027010000 2018-11-18 22:14:50,208 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 0000004f-0000-1000-8000-0026bb765291 (pairing.features) 2018-11-18 22:14:50,350 hap_char_sig_read.py:178 DEBUG read characteristic id 33 2018-11-18 22:14:50,596 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb26000080001000004f00000007021e000610915276bb2600008000100000550000000a0201000c0704000027010000 2018-11-18 22:14:50,598 hap_char_sig_read.py:160 DEBUG found characteristic with UUID e604e95d-a759-4817-87d3-aa005083a0d1 (protocol.service-id) 2018-11-18 22:14:50,740 hap_char_sig_read.py:164 DEBUG read service id 30 2018-11-18 22:14:50,741 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 0000004e-0000-1000-8000-0026bb765291 (pairing.pair-verify) 2018-11-18 22:14:50,887 hap_char_sig_read.py:178 DEBUG read characteristic id 32 2018-11-18 22:14:51,132 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb26000080001000004e00000007021e000610915276bb2600008000100000550000000a0203000c071b000027010000 2018-11-18 22:14:51,134 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 0000004c-0000-1000-8000-0026bb765291 (pairing.pair-setup) 2018-11-18 22:14:51,326 hap_char_sig_read.py:178 DEBUG read characteristic id 31 2018-11-18 22:14:51,571 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb26000080001000004c00000007021e000610915276bb2600008000100000550000000a0203000c071b000027010000 2018-11-18 22:14:51,573 hap_char_sig_read.py:152 DEBUG found service with UUID 00001801-0000-1000-8000-00805f9b34fb (Unknown Service: 00001801-0000-1000-8000-00805F9B34FB) 2018-11-18 22:14:51,574 hap_char_sig_read.py:152 DEBUG found service with UUID 00000043-0000-1000-8000-0026bb765291 (lightbulb) 2018-11-18 22:14:51,575 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000008-0000-1000-8000-0026bb765291 (brightness) 2018-11-18 22:14:51,671 hap_char_sig_read.py:178 DEBUG read characteristic id 23 2018-11-18 22:14:52,254 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0045000410915276bb260000800010000008000000070214000610915276bb2600008000100000430000000a02b0010c071000ad270100000d0800000000640000000e0401000000 2018-11-18 22:14:52,256 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000025-0000-1000-8000-0026bb765291 (on) 2018-11-18 22:14:52,545 hap_char_sig_read.py:178 DEBUG read characteristic id 15 2018-11-18 22:14:52,936 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000025000000070214000610915276bb2600008000100000430000000a02b0010c0701000027010000 2018-11-18 22:14:52,938 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000013-0000-1000-8000-0026bb765291 (hue) 2018-11-18 22:14:53,031 hap_char_sig_read.py:178 DEBUG read characteristic id 24 2018-11-18 22:14:53,327 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0045000410915276bb260000800010000013000000070214000610915276bb2600008000100000430000000a02b0010c07140063270100000d08000000000000b4430e040000803f 2018-11-18 22:14:53,329 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000023-0000-1000-8000-0026bb765291 (name) 2018-11-18 22:14:53,422 hap_char_sig_read.py:178 DEBUG read characteristic id 16 2018-11-18 22:14:53,618 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000023000000070214000610915276bb2600008000100000430000000a0210000c0719000027010000 2018-11-18 22:14:53,620 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 000000a5-0000-1000-8000-0026bb765291 (service-signature) 2018-11-18 22:14:53,714 hap_char_sig_read.py:178 DEBUG read characteristic id 19 2018-11-18 22:14:54,105 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb2600008000100000a5000000070214000610915276bb2600008000100000430000000a0201000c071b000027010000 2018-11-18 22:14:54,106 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 0000002f-0000-1000-8000-0026bb765291 (saturation) 2018-11-18 22:14:54,202 hap_char_sig_read.py:178 DEBUG read characteristic id 25 2018-11-18 22:14:54,644 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0045000410915276bb26000080001000002f000000070214000610915276bb2600008000100000430000000a02b0010c071400ad270100000d08000000000000c8420e040000803f 2018-11-18 22:14:54,645 hap_char_sig_read.py:160 DEBUG found characteristic with UUID e604e95d-a759-4817-87d3-aa005083a0d1 (protocol.service-id) 2018-11-18 22:14:54,787 hap_char_sig_read.py:164 DEBUG read service id 20 2018-11-18 22:14:54,787 hap_char_sig_read.py:152 DEBUG found service with UUID ae5d1e47-5c13-43a0-8635-82ad38a1381f (Unknown Service: AE5D1E47-5C13-43A0-8635-82AD38A1381F) 2018-11-18 22:14:54,788 hap_char_sig_read.py:160 DEBUG found characteristic with UUID a3dd50bf-f7a7-4e99-838e-570a086c661b (Unknown Characteristic A3DD50BF-F7A7-4E99-838E-570A086C661B) 2018-11-18 22:14:54,884 hap_char_sig_read.py:160 DEBUG found characteristic with UUID a2e86c7a-d961-4091-b74f-2409e72efe26 (Unknown Characteristic A2E86C7A-D961-4091-B74F-2409E72EFE26) 2018-11-18 22:14:54,885 hap_char_sig_read.py:160 DEBUG found characteristic with UUID a47f7608-2e2d-47eb-913b-75d4edc4de4b (Unknown Characteristic A47F7608-2E2D-47EB-913B-75D4EDC4DE4B) 2018-11-18 22:14:54,888 hap_char_sig_read.py:152 DEBUG found service with UUID 000000a2-0000-1000-8000-0026bb765291 (service) 2018-11-18 22:14:54,889 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000037-0000-1000-8000-0026bb765291 (version) 2018-11-18 22:14:54,981 hap_char_sig_read.py:178 DEBUG read characteristic id 11 2018-11-18 22:14:55,227 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb26000080001000003700000007020a000610915276bb2600008000100000a20000000a0210000c0719000027010000 2018-11-18 22:14:55,229 hap_char_sig_read.py:160 DEBUG found characteristic with UUID e604e95d-a759-4817-87d3-aa005083a0d1 (protocol.service-id) 2018-11-18 22:14:55,323 hap_char_sig_read.py:164 DEBUG read service id 10 2018-11-18 22:14:55,324 hap_char_sig_read.py:152 DEBUG found service with UUID 0000003e-0000-1000-8000-0026bb765291 (accessory-information) 2018-11-18 22:14:55,325 hap_char_sig_read.py:160 DEBUG found characteristic with UUID e604e95d-a759-4817-87d3-aa005083a0d1 (protocol.service-id) 2018-11-18 22:14:55,420 hap_char_sig_read.py:164 DEBUG read service id 1 2018-11-18 22:14:55,421 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000030-0000-1000-8000-0026bb765291 (serial-number) 2018-11-18 22:14:55,518 hap_char_sig_read.py:178 DEBUG read characteristic id 6 2018-11-18 22:14:55,715 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000030000000070201000610915276bb26000080001000003e0000000a0210000c0719000027010000 2018-11-18 22:14:55,716 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000021-0000-1000-8000-0026bb765291 (model) 2018-11-18 22:14:55,810 hap_char_sig_read.py:178 DEBUG read characteristic id 4 2018-11-18 22:14:56,008 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000021000000070201000610915276bb26000080001000003e0000000a0210000c0719000027010000 2018-11-18 22:14:56,009 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000020-0000-1000-8000-0026bb765291 (manufacturer) 2018-11-18 22:14:56,151 hap_char_sig_read.py:178 DEBUG read characteristic id 3 2018-11-18 22:14:56,348 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000020000000070201000610915276bb26000080001000003e0000000a0210000c0719000027010000 2018-11-18 22:14:56,350 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000023-0000-1000-8000-0026bb765291 (name) 2018-11-18 22:14:56,444 hap_char_sig_read.py:178 DEBUG read characteristic id 5 2018-11-18 22:14:56,690 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000023000000070201000610915276bb26000080001000003e0000000a0210000c0719000027010000 2018-11-18 22:14:56,691 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000052-0000-1000-8000-0026bb765291 (firmware.revision) 2018-11-18 22:14:56,785 hap_char_sig_read.py:178 DEBUG read characteristic id 7 2018-11-18 22:14:57,031 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000052000000070201000610915276bb26000080001000003e0000000a0210000c0719000027010000 2018-11-18 22:14:57,033 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 00000014-0000-1000-8000-0026bb765291 (identify) 2018-11-18 22:14:57,126 hap_char_sig_read.py:178 DEBUG read characteristic id 2 2018-11-18 22:14:57,323 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb260000800010000014000000070201000610915276bb26000080001000003e0000000a0202000c0701000027010000 2018-11-18 22:14:57,325 hap_char_sig_read.py:152 DEBUG found service with UUID b0733e83-8434-4c00-a344-25d1c982a0ef (Unknown Service: B0733E83-8434-4C00-A344-25D1C982A0EF) 2018-11-18 22:14:57,325 hap_char_sig_read.py:160 DEBUG found characteristic with UUID b176bd7f-4148-47bd-a6c6-9d0796e96183 (Unknown Characteristic B176BD7F-4148-47BD-A6C6-9D0796E96183) 2018-11-18 22:14:57,419 hap_char_sig_read.py:178 DEBUG read characteristic id 1002 2018-11-18 22:14:57,713 hap_char_sig_read.py:38 DEBUG parse sig read response 022a00350004108361e996079dc6a6bd4748417fbd76b10702e8030610efa082c9d12544a3004c3484833e73b00a02b0010c071b000027010000 2018-11-18 22:14:57,715 hap_char_sig_read.py:160 DEBUG found characteristic with UUID b2fd7f2d-ead3-4f17-b16c-202ec758c697 (Unknown Characteristic B2FD7F2D-EAD3-4F17-B16C-202EC758C697) 2018-11-18 22:14:57,809 hap_char_sig_read.py:178 DEBUG read characteristic id 1003 2018-11-18 22:14:58,055 hap_char_sig_read.py:38 DEBUG parse sig read response 022a003500041097c658c72e206cb1174fd3ea2d7ffdb20702e8030610efa082c9d12544a3004c3484833e73b00a0230000c071b000027010000 2018-11-18 22:14:58,056 hap_char_sig_read.py:160 DEBUG found characteristic with UUID e604e95d-a759-4817-87d3-aa005083a0d1 (protocol.service-id) 2018-11-18 22:14:58,150 hap_char_sig_read.py:164 DEBUG read service id 1000 2018-11-18 22:14:58,151 hap_char_sig_read.py:160 DEBUG found characteristic with UUID 000000a5-0000-1000-8000-0026bb765291 (service-signature) 2018-11-18 22:14:58,248 hap_char_sig_read.py:178 DEBUG read characteristic id 1001 2018-11-18 22:14:58,445 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410915276bb2600008000100000a50000000702e8030610efa082c9d12544a3004c3484833e73b00a0201000c071b000027010000 2018-11-18 22:14:58,446 hap_char_sig_read.py:160 DEBUG found characteristic with UUID b31259a5-9acc-45c2-838a-956f57825196 (Unknown Characteristic B31259A5-9ACC-45C2-838A-956F57825196) 2018-11-18 22:14:58,541 hap_char_sig_read.py:178 DEBUG read characteristic id 1004 2018-11-18 22:14:58,737 hap_char_sig_read.py:38 DEBUG parse sig read response 022a0035000410965182576f958a83c245cc9aa55912b30702e8030610efa082c9d12544a3004c3484833e73b00a0210000c071b000027010000 2018-11-18 22:14:58,739 hap_char_sig_read.py:192 DEBUG disconnecting from device 2018-11-18 22:15:00,832 hap_char_sig_read.py:194 DEBUG disconnected from device 2018-11-18 22:15:00,833 hap_char_sig_read.py:196 DEBUG manager stopped -------------------- human readable -------------------- None: >Unknown Service: 1B7E8251-2877-41C3-B46E-CF057C562023< (1B7E8251-2877-41C3-B46E-CF057C562023) None.None: () >Unknown Characteristic 5E9BF2A8-F93F-4481-A67E-3B2F4A07891A< [] None.None: () >Unknown Characteristic 8AC32D3F-5CB9-4D44-BEC2-EE689169F626< [] 30: >pairing< (00000055-0000-1000-8000-0026BB765291) 30.34: () >pairing.pairings< [pr,pw] 30.33: () >pairing.features< [r] 30.32: () >pairing.pair-verify< [r,w] 30.31: () >pairing.pair-setup< [r,w] None: >Unknown Service: 00001801-0000-1000-8000-00805F9B34FB< (00001801-0000-1000-8000-00805F9B34FB) 20: >lightbulb< (00000043-0000-1000-8000-0026BB765291) 20.23: () >brightness< [pr,pw,evc,evd] 20.15: () >on< [pr,pw,evc,evd] 20.24: () >hue< [pr,pw,evc,evd] 20.16: () >name< [pr] 20.19: () >service-signature< [r] 20.25: () >saturation< [pr,pw,evc,evd] None: >Unknown Service: AE5D1E47-5C13-43A0-8635-82AD38A1381F< (AE5D1E47-5C13-43A0-8635-82AD38A1381F) None.None: () >Unknown Characteristic A3DD50BF-F7A7-4E99-838E-570A086C661B< [] None.None: () >Unknown Characteristic A2E86C7A-D961-4091-B74F-2409E72EFE26< [] None.None: () >Unknown Characteristic A47F7608-2E2D-47EB-913B-75D4EDC4DE4B< [] 10: >service< (000000A2-0000-1000-8000-0026BB765291) 10.11: () >version< [pr] 1: >accessory-information< (0000003E-0000-1000-8000-0026BB765291) 1.6: () >serial-number< [pr] 1.4: () >model< [pr] 1.3: () >manufacturer< [pr] 1.5: () >name< [pr] 1.7: () >firmware.revision< [pr] 1.2: () >identify< [w] 1000: >Unknown Service: B0733E83-8434-4C00-A344-25D1C982A0EF< (B0733E83-8434-4C00-A344-25D1C982A0EF) 1000.1002: () >Unknown Characteristic B176BD7F-4148-47BD-A6C6-9D0796E96183< [pr,pw,evc,evd] 1000.1003: () >Unknown Characteristic B2FD7F2D-EAD3-4F17-B16C-202EC758C697< [pr,pw] 1000.1001: () >service-signature< [r] 1000.1004: () >Unknown Characteristic B31259A5-9ACC-45C2-838A-956F57825196< [pr] -------------------- json -------------------- Traceback (most recent call last): File "staging/hap_char_sig_read.py", line 269, in 'format': characteristic['format'], KeyError: 'format' ```

All the best Vasil

jlusiardi commented 5 years ago

Hey Vasil,

can u give it another try with the new code an post back the debug log?

I ordered one of the bulbs myself, too.

Joachim

der-punkt commented 5 years ago

Hi Joachim,

Nice, congrats on the new bulb 😉

Unfortunately, I’m on a business trip. I’ll be able to try some thing not before next week. Sorry. 😐

Best wishes Vasil

Am 19.11.2018 um 22:10 schrieb Joachim Lusiardi notifications@github.com:

Hey Vasil,

can u give it another try with the new code an post back the debug log?

I ordered one of the bulbs myself, too.

Joachim

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jlusiardi commented 5 years ago

Hey Vasil,

Bulb arrived, and i was able to do some improvements:

10: >service< (000000A2-0000-1000-8000-0026BB765291)
  10.11:  () >version< [pr]
1: >accessory-information< (0000003E-0000-1000-8000-0026BB765291)
  1.3:  () >manufacturer< [pr]
  1.7:  () >firmware.revision< [pr]
  1.2:  () >identify< [w]
  1.5:  () >name< [pr]
  1.4:  () >model< [pr]
  1.6:  () >serial-number< [pr]
30: >pairing< (00000055-0000-1000-8000-0026BB765291)
  30.33:  () >pairing.features< [r]
  30.31:  () >pairing.pair-setup< [r,w]
  30.34:  () >pairing.pairings< [pr,pw]
  30.32:  () >pairing.pair-verify< [r,w]
20: >lightbulb< (00000043-0000-1000-8000-0026BB765291)
  20.15:  () >on< [pr,pw,evc,evd]
  20.19:  () >service-signature< [r]
  20.25:  () >saturation< [pr,pw,evc,evd]
  20.24:  () >hue< [pr,pw,evc,evd]
  20.16:  () >name< [pr]
  20.23:  () >brightness< [pr,pw,evc,evd]
1000: >Unknown Service: B0733E83-8434-4C00-A344-25D1C982A0EF< (B0733E83-8434-4C00-A344-25D1C982A0EF)
  1000.1001:  () >service-signature< [r]
  1000.1003:  () >Unknown Characteristic B2FD7F2D-EAD3-4F17-B16C-202EC758C697< [pr,pw]
  1000.1004:  () >Unknown Characteristic B31259A5-9ACC-45C2-838A-956F57825196< [pr]
  1000.1002:  () >Unknown Characteristic B176BD7F-4148-47BD-A6C6-9D0796E96183< [pr,pw,evc,evd]

Try to check when you have the time ;)

Joachim

Jc2k commented 5 years ago

Hey, very interested in this branch. Heres what my eve energy looks like:

sudo ./staging/homekit_discovery.py 
Name: Eve Energy XXXX
MAC: c6:87:8d:xx:xx:xx
Configuration number (c#): 2
Device ID (id): xxxxxxxxxxxx
Compatible Version (cv): 2
State Number (s#): 1009
Status Flags (sf): unpaired
Category Identifier (ci): (Id: 7)
john@ubuntu:~/homekit_python$ sudo ./staging/hap_char_sig_read.py --log=DEBUG c6:87:8d:xx:xx:xx
2018-11-28 02:44:58,651 hap_char_sig_read.py:0214 DEBUG Running version 20181122
2018-11-28 02:44:58,652 hap_char_sig_read.py:0215 DEBUG using adapter hci0
2018-11-28 02:44:59,604 hap_char_sig_read.py:0131 DEBUG discovered c6:87:8d:xx:xx:xx
2018-11-28 02:44:59,605 hap_char_sig_read.py:0219 DEBUG discovered
2018-11-28 02:44:59,608 hap_char_sig_read.py:0223 DEBUG connecting to device
2018-11-28 02:45:00,788 hap_char_sig_read.py:0225 DEBUG connected to device
2018-11-28 02:45:00,788 hap_char_sig_read.py:0228 DEBUG start manager

And it never returns

And if i try to pair:

$ sudo ./staging/hap_pair.py --log=DEBUG -p xxxxxxxx c6:87:8d:xx:xx:xx
2018-11-28 02:46:59,593 hap_pair.py:103 DEBUG using adapter hci0
2018-11-28 02:47:00,543 hap_pair.py:23 DEBUG discovered c6:87:8d:xx:xx:xx
2018-11-28 02:47:00,549 hap_pair.py:110 DEBUG connecting to device
2018-11-28 02:47:01,477 hap_pair.py:112 DEBUG connected to device

And it never returns.

Running bluetoothctl in another terminal i see lots of RSSI measurements. And a few seconds after the script has connected to my device, i see a Connected: no fly by.

Jc2k commented 5 years ago

Added a:

    def connect_failed(self, error):
        super().connect_failed(error)
        print("[%s] Connection failed: %s" % (self.mac_address, str(error)))

And got:

[c6:87:8d:xx:xx:xx] Connection failed: No such property 'ServicesResolved'

Which lead me to this. But I can see the device is discovered so I don't think its that. Looking at the docs, bluez must be >= 5.38. I used xenial vm i already had lying around, so actually only on 5.37. Upgrading.

Jc2k commented 5 years ago

Ok now it works:

-------------------- human readable --------------------
28: >outlet< (00000047-0000-1000-8000-0026BB765291)
  28.36:  () >service-signature< [pr]
  28.35:  () >Unknown Characteristic E863F10C-079E-48FF-8F27-9C2605A29F52< [pr]
  28.34:  () >Unknown Characteristic E863F10D-079E-48FF-8F27-9C2605A29F52< [pr,evc,evd]
  28.33:  () >Unknown Characteristic E863F126-079E-48FF-8F27-9C2605A29F52< [pr]
  28.32:  () >Unknown Characteristic E863F10A-079E-48FF-8F27-9C2605A29F52< [pr]
  28.31:  () >outlet-in-use< [pr,evc,evd]
  28.30:  () >on< [pr,pw,evc,evd]
  28.29:  () >name< [pr]
16: >Unknown Service: E863F007-079E-48FF-8F27-9C2605A29F52< (E863F007-079E-48FF-8F27-9C2605A29F52)
  16.27:  () >service-signature< [pr]
  16.26:  () >Unknown Characteristic E863F11D-079E-48FF-8F27-9C2605A29F52< [pw]
  16.25:  () >Unknown Characteristic E863F131-079E-48FF-8F27-9C2605A29F52< [pr]
  16.24:  () >Unknown Characteristic E863F117-079E-48FF-8F27-9C2605A29F52< [pr]
  16.23:  () >Unknown Characteristic E863F116-079E-48FF-8F27-9C2605A29F52< [pr]
  16.22:  () >Unknown Characteristic E863F121-079E-48FF-8F27-9C2605A29F52< [pw]
  16.21:  () >Unknown Characteristic E863F11C-079E-48FF-8F27-9C2605A29F52< [pw]
  16.20:  () >Unknown Characteristic E863F112-079E-48FF-8F27-9C2605A29F52< [pr,pw]
  16.19:  () >Unknown Characteristic E863F11E-079E-48FF-8F27-9C2605A29F52< [pr,pw]
  16.17:  () >name< [pr]
11: >pairing< (00000055-0000-1000-8000-0026BB765291)
  11.15:  () >pairing.pairings< [pr,pw]
  11.14:  () >pairing.features< [r]
  11.13:  () >pairing.pair-verify< [r,w]
  11.12:  () >pairing.pair-setup< [r,w]
9: >service< (000000A2-0000-1000-8000-0026BB765291)
  9.37:  () >service-signature< [pr]
  9.10:  () >version< [pr]
1: >accessory-information< (0000003E-0000-1000-8000-0026BB765291)
  1.8:  () >hardware.revision< [pr]
  1.7:  () >firmware.revision< [pr]
  1.6:  () >serial-number< [pr]
  1.5:  () >model< [pr]
  1.4:  () >manufacturer< [pr]
  1.3:  () >identify< [pw]
  1.2:  () >name< [pr]
-------------------- json --------------------
[
    {
        "aid": 1,
        "services": [
            {
                "type": "00000047-0000-1000-8000-0026BB765291",
                "iid": 28,
                "characteristics": [
                    {
                        "type": "000000a5-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 28,
                        "iid": 36,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f10c-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 28,
                        "iid": 35,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "float",
                        "unit": "unitless",
                        "range": "(0.0, inf)",
                        "step": "None"
                    },
                    {
                        "type": "e863f10d-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 28,
                        "iid": 34,
                        "value": "",
                        "perms": [
                            "pr",
                            "evc",
                            "evd"
                        ],
                        "format": "float",
                        "unit": "unitless",
                        "range": "(0.0, 5000.0)",
                        "step": "None"
                    },
                    {
                        "type": "e863f126-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 28,
                        "iid": 33,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "float",
                        "unit": "unitless",
                        "range": "(0.0, 16.0)",
                        "step": "None"
                    },
                    {
                        "type": "e863f10a-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 28,
                        "iid": 32,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "float",
                        "unit": "unitless",
                        "range": "(0.0, 380.0)",
                        "step": "None"
                    },
                    {
                        "type": "00000026-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 28,
                        "iid": 31,
                        "value": "",
                        "perms": [
                            "pr",
                            "evc",
                            "evd"
                        ],
                        "format": "bool",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000025-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 28,
                        "iid": 30,
                        "value": "",
                        "perms": [
                            "pr",
                            "pw",
                            "evc",
                            "evd"
                        ],
                        "format": "bool",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000023-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 28,
                        "iid": 29,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    }
                ]
            },
            {
                "type": "E863F007-079E-48FF-8F27-9C2605A29F52",
                "iid": 16,
                "characteristics": [
                    {
                        "type": "000000a5-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 16,
                        "iid": 27,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f11d-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 26,
                        "value": "",
                        "perms": [
                            "pw"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f131-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 25,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f117-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 24,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f116-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 23,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f121-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 22,
                        "value": "",
                        "perms": [
                            "pw"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f11c-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 21,
                        "value": "",
                        "perms": [
                            "pw"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "e863f112-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 20,
                        "value": "",
                        "perms": [
                            "pr",
                            "pw"
                        ],
                        "format": "uint32",
                        "unit": "unitless",
                        "range": "(None, None)",
                        "step": "None"
                    },
                    {
                        "type": "e863f11e-079e-48ff-8f27-9c2605a29f52",
                        "description": "",
                        "aid": 16,
                        "iid": 19,
                        "value": "",
                        "perms": [
                            "pr",
                            "pw"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000023-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 16,
                        "iid": 17,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    }
                ]
            },
            {
                "type": "00000055-0000-1000-8000-0026BB765291",
                "iid": 11,
                "characteristics": [
                    {
                        "type": "00000050-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 11,
                        "iid": 15,
                        "value": "",
                        "perms": [
                            "pr",
                            "pw"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "0000004f-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 11,
                        "iid": 14,
                        "value": "",
                        "perms": [
                            "r"
                        ],
                        "format": "uint8",
                        "unit": "unitless",
                        "range": "(0, 1)",
                        "step": "1"
                    },
                    {
                        "type": "0000004e-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 11,
                        "iid": 13,
                        "value": "",
                        "perms": [
                            "r",
                            "w"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "0000004c-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 11,
                        "iid": 12,
                        "value": "",
                        "perms": [
                            "r",
                            "w"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    }
                ]
            },
            {
                "type": "000000A2-0000-1000-8000-0026BB765291",
                "iid": 9,
                "characteristics": [
                    {
                        "type": "000000a5-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 9,
                        "iid": 37,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "data",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000037-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 9,
                        "iid": 10,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    }
                ]
            },
            {
                "type": "0000003E-0000-1000-8000-0026BB765291",
                "iid": 1,
                "characteristics": [
                    {
                        "type": "00000053-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 8,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000052-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 7,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000030-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 6,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000021-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 5,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000020-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 4,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000014-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 3,
                        "value": "",
                        "perms": [
                            "pw"
                        ],
                        "format": "bool",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    },
                    {
                        "type": "00000023-0000-1000-8000-0026bb765291",
                        "description": "",
                        "aid": 1,
                        "iid": 2,
                        "value": "",
                        "perms": [
                            "pr"
                        ],
                        "format": "string",
                        "unit": "unitless",
                        "range": "None",
                        "step": "None"
                    }
                ]
            }
        ]
    }
]
Jc2k commented 5 years ago

Pairing fails:

2018-11-28 03:38:23,751 hap_pair.py:110 DEBUG connecting to device
2018-11-28 03:38:24,542 hap_pair.py:112 DEBUG connected to device
2018-11-28 03:38:25,378 hap_pair.py:31 DEBUG resolved 6 services
2018-11-28 03:38:25,386 hap_pair.py:119 DEBUG Pairing Service: <gatt.gatt_linux.Service object at 0x7fb73df8a160>
2018-11-28 03:38:25,386 hap_pair.py:124 DEBUG char: e604e95d-a759-4817-87d3-aa005083a0d1 protocol.service-id
2018-11-28 03:38:25,387 hap_pair.py:124 DEBUG char: 00000050-0000-1000-8000-0026bb765291 pairing.pairings
2018-11-28 03:38:25,387 hap_pair.py:124 DEBUG char: 0000004f-0000-1000-8000-0026bb765291 pairing.features
2018-11-28 03:38:25,387 hap_pair.py:124 DEBUG char: 0000004e-0000-1000-8000-0026bb765291 pairing.pair-verify
2018-11-28 03:38:25,387 hap_pair.py:124 DEBUG char: 0000004c-0000-1000-8000-0026bb765291 pairing.pair-setup
2018-11-28 03:38:25,388 hap_pair.py:131 DEBUG setup char: <gatt.gatt_linux.Characteristic object at 0x7fb73b42eb70> <__main__.AnyDevice object at 0x7fb73bce5fd0>
2018-11-28 03:38:25,388 hap_pair.py:132 DEBUG verify char: <gatt.gatt_linux.Characteristic object at 0x7fb73b42edd8>
2018-11-28 03:38:25,388 __init__.py:89 DEBUG #1 ios -> accessory: send SRP start request
2018-11-28 03:38:25,389 hap_pair.py:47 DEBUG entering write function [[6, bytearray(b'\x01')], [0, bytearray(b'\x01')]]
2018-11-28 03:38:25,389 hap_pair.py:57 DEBUG sent 0002d00a000b000901010106060101000101
2018-11-28 03:38:26,391 hap_pair.py:62 DEBUG reading characteristic
2018-11-28 03:38:26,641 hap_pair.py:69 DEBUG control field: 2, tid: d0, status: 4, length: 0
2018-11-28 03:38:26,641 hap_pair.py:77 DEBUG received 02d004
2018-11-28 03:38:26,641 hap_pair.py:78 DEBUG decode 
Traceback (most recent call last):
  File "./staging/hap_pair.py", line 138, in <module>
    pairing = perform_pair_setup(args.pin, str(uuid.uuid4()), write_fun)
  File "/home/john/homekit_python/homekit/protocol/__init__.py", line 96, in perform_pair_setup
    response_tlv = write_fun(request_tlv, step2_expectations)
  File "./staging/hap_pair.py", line 80, in write
    result = TLV.decode_bytes(resp_tlv[0][1], expected)
IndexError: list index out of range
jlusiardi commented 5 years ago

Hey @Jc2k ,

cool that this is working at least for the hap_char_sig_read.py!

I am still working on this but I like your support and will try to get back to the pairing problem soon.

Regards Joachim

jlusiardi commented 5 years ago

Hey @Jc2k can you give it another try after pulling? And please always give the full command line and if a script does not work, enable logging like you already did. Thanks! Joachim

Jc2k commented 5 years ago

Looking good!

So first of all, discovery:

Name: Eve Energy XXXX
MAC: C6:87:8D:XX:XX:XX
Configuration number (c#): 2
Device ID (id): XX:XX:XX:XX:XX:XX
Compatible Version (cv): 2
State Number (s#): 1009
Status Flags (sf): unpaired
Category Identifier (ci): Outlet (Id: 7)

It's now showing as an Outlet!

At a glance the hap_char_sig_read data looks the same.

Unfortunately hap_pair now fails with an import error. The BlePairing class doesnt seem to exist in homekit.controller.

Jc2k commented 5 years ago

I justed used homekit.controller.Pairing and the script now finishes successfully. Device now shows as paired in homekit discovery:

Status Flags (sf): paired

I have a valid looking JSON file from the pairing process.

jlusiardi commented 5 years ago

yeah I forgot to commit/push properly :/

Jc2k commented 5 years ago

Hey - was thinking of having a go at adding a BLEPairing.get_characteristics but wanted to make sure I wasn’t duplicating effort. And it sounds like you might have a partial BLEPairing 🙃

jlusiardi commented 5 years ago

Hey,

contributions welcome, I am still stuck on how to apply session security on the BLE functions.

What exactly to u mean with partial pairing?

Regards Joachim

Jc2k commented 5 years ago

Oh sorry - replied on my phone and wasn't very clear.

I assumed you had some code already for the BLEPairing class that wasn't committed (because the pairing code references it, but it isnt in git). I said partial because I assumed it wasn't finished yet.

The docs around session security for BLE are a bit hard to follow. It wasn't clear to me whether you encrypted the entire PDU or just the PDU body. I think it means just the body, and that the length is the length of the encrypted body and the auth tag.

If you didn't have any more code to share I was going to start out by adding a BlePairing/BleSession and try to write something generic to do a Request/Response with session security. But if you have something part written that I can play with debugging that would save me some time this evening. New to BLE and HAP so might end in disaster!

jlusiardi commented 5 years ago

No Problem!

I will give a quick look to commit & push the latest code now :) I am still working on some kind of working implementation of a BLE version of pair verify (that part works) and the read out of the device name via the name service of the Koogeek DW1:

...
1: >accessory-information< (0000003E-0000-1000-8000-0026BB765291)
...
  1.2:  () >name< [pr]
...

The code is is hap_pair_verify.py. I hope i did not forget anything this time ;)

Jc2k commented 5 years ago

Didn't have as much time to look at this last night as i'd hope.

I think i'm currently in the same position as you - accessory is rejecting my Read-Request packets. From reading the spec everything looks good, but I just get GATT errors.

I found this: https://github.com/grover/homebridge-ranger. It proxies IP homekit to bluetooth homekit accessories (via homebridge). It just seems to corobarate what you are doing session security wise. The crypto is here. It's invoked around here.

The only thing that pops out at me is that the buildPacket function always writes a payload length. But that doesn't match the spec or what homekit_python does, and doesn't fix my GATT errors.

I'm fighting with node now to see if I can see if it can pair with my Eve Energy, but its node build pain all the way down...

jlusiardi commented 5 years ago

hap_read_pairingfeatures.py allows to read a characteristic without pairing for now. Perhaps it gives clues...

Jc2k commented 5 years ago

If you try to read a characteristic do you get a valid HAP reply with a known BLE status code?

I managed to get a little closer to a working homebridge-ranger. Its not working yet but I have seen it return HAP BLE status codes for the read characteristic op. Even sending the read request with no encryption, my device gives me a valid response packet (with status 5).

But I can't get the same with homekit_python so far.

    feature_char, feature_char_id = find_characteristic(
        device,
        ServicesTypes['public.hap.service.outlet'],
        CharacteristicsTypes.ON)

    if not feature_char:
        print('features characteristic not found')
        sys.exit(-1)

    transaction_id = random.randrange(0, 255)
    transaction_id = 0xff

    data = bytearray([0x00, HapBleOpCodes.CHAR_READ, transaction_id])
    data.extend(feature_char_id.to_bytes(length=2, byteorder='little'))
    # to match homebridge-ranger, probably not needed
    data.extend([0, 0])
    print(data.hex())

    result = feature_char.write_value(value=data)
    logging.debug('write resulted in: %s', result)

    data = []
    while not data or len(data) == 0:
        time.sleep(1)
        logging.debug('reading characteristic')
        data = feature_char.read_value()
    resp_data = [b for b in data]
    logging.debug('read: %s', bytearray(resp_data).hex())

It managed to send a Write and a Read to the dbus API, (i checked with dbus-monitor --system). The Read returns None so the code enters a loop. But now subsequent Reads fail - and the GATT API just continues to return None. Instrumentiont the LoggingDevice class tells me it disconnects after the first Read.

Also I think gatt might have a bug in that characteristic_write_value_succeeded is never called?

jlusiardi commented 5 years ago

I got this result for a read on the pairing features characteristic:

> PYTHONPATH=. python3 staging/hap_read_pairingfeatures.py --log=DEBUG -f ble.json -a DW1
2018-12-05 21:47:34,853 hap_read_pairingfeatures.py:0055 DEBUG running version: 20181204
2018-12-05 21:47:34,853 hap_read_pairingfeatures.py:0056 DEBUG using adapter hci0
2018-12-05 21:47:34,865 hap_read_pairingfeatures.py:0074 DEBUG connecting to device
2018-12-05 21:47:35,099 hap_read_pairingfeatures.py:0076 DEBUG connected to device
2018-12-05 21:47:35,099 tools.py:0033 DEBUG services: [<gatt.gatt_linux.Service object at 0x7fa69ec9f9b0>, <gatt.gatt_linux.Service object at 0x7fa69ecb0320>, <gatt.gatt_linux.Service object at 0x7fa69ecb0400>, <gatt.gatt_linux.Service object at 0x7fa69ec87940>, <gatt.gatt_linux.Service object at 0x7fa69ec78d68>, <gatt.gatt_linux.Service object at 0x7fa69ec87c50>, <gatt.gatt_linux.Service object at 0x7fa69ec8e908>, <gatt.gatt_linux.Service object at 0x7fa69ec8e048>]
2018-12-05 21:47:35,099 tools.py:0038 DEBUG searched service: <gatt.gatt_linux.Service object at 0x7fa69ec87c50>
2018-12-05 21:47:35,099 tools.py:0047 DEBUG char: 00000050-0000-1000-8000-0026bb765291 pairing.pairings
2018-12-05 21:47:35,099 tools.py:0047 DEBUG char: 0000004f-0000-1000-8000-0026bb765291 pairing.features
2018-12-05 21:47:35,160 tools.py:0047 DEBUG char: e604e95d-a759-4817-87d3-aa005083a0d1 protocol.service-id
2018-12-05 21:47:35,160 tools.py:0047 DEBUG char: 0000004c-0000-1000-8000-0026bb765291 pairing.pair-setup
2018-12-05 21:47:35,160 tools.py:0047 DEBUG char: 0000004e-0000-1000-8000-0026bb765291 pairing.pair-verify
2018-12-05 21:47:35,161 tools.py:0061 DEBUG searched char: <gatt.gatt_linux.Characteristic object at 0x7fa69ec8eb00> 12
2018-12-05 21:47:35,161 hap_read_pairingfeatures.py:0091 DEBUG write resulted in: None
2018-12-05 21:47:36,162 hap_read_pairingfeatures.py:0096 DEBUG reading characteristic
2018-12-05 21:47:36,319 hap_read_pairingfeatures.py:0099 DEBUG read: 028c000300010101
2018-12-05 21:47:36,319 hap_read_pairingfeatures.py:0023 DEBUG parse sig read response 028c000300010101
2018-12-05 21:47:36,319 hap_read_pairingfeatures.py:0027 DEBUG control field 2
2018-12-05 21:47:36,320 hap_read_pairingfeatures.py:0029 DEBUG transaction id 140 (expected was 140)
2018-12-05 21:47:36,320 hap_read_pairingfeatures.py:0031 DEBUG status code 0 (The request was successful.)
2018-12-05 21:47:36,320 hap_read_pairingfeatures.py:0038 DEBUG expected body length 3 (got 3)
[
  1: (1 bytes) 0x01
]

The value of 1 makes sense (Table 4-1 page 58 of spec).

I'll try homebridge-ranger on my Raspberry Pi Zero W as well.

Also I think gatt might have a bug in that characteristic_write_value_succeeded is never called?

Perhaps this is because of the manager is not running?

Jc2k commented 5 years ago

OK - so if I modify hap_read_pairingfeatures.py to try and read my outlets on char then i get an error 5, like homebridge-ranger.

If i copy and paste that same working code to the end of pair_verify (so that i have the crypto keys) then it stops working. The write seems to work. But the first read returns None. If you fix the loop to handle Nones the others fail too, and it seems the connection has been closed.

If i then comment out the call to get_session_keys (and references to the keys) i'm back to the error 5 state.

I guess after doing a pair_verify the connection is considered 'secure' by the other end and it now only expects encrypted messages, and ignores anything it can't verify the auth mac of?

Jc2k commented 5 years ago

I've managed to read a characteristic on my Eve Energy! At the end of hap_pair_verify.py:

    write_fun = create_ble_pair_setup_write(pair_verify_char, pair_verify_char_id)
    c2a_key, a2c_key = get_session_keys(None, pairing_data, write_fun)
    logging.debug('keys: \n\t\tc2a: %s\n\t\ta2c: %s', c2a_key.hex(), a2c_key.hex())
    print(c2a_key)

    import random

    feature_char, feature_char_id = find_characteristic(device,
                                                        ServicesTypes['public.hap.service.outlet'],
                                                        CharacteristicsTypes.ON)

    if not feature_char:
        print('features characteristic not found')
        sys.exit(-1)

    transaction_id = random.randrange(0, 255)

    data = bytearray([0x00, HapBleOpCodes.CHAR_READ, transaction_id])
    data.extend(feature_char_id.to_bytes(length=2, byteorder='little'))

    c2a_counter = 0
    cnt_bytes = c2a_counter.to_bytes(8, byteorder='little')
    cipher_and_mac = chacha20_aead_encrypt(bytes(), c2a_key, cnt_bytes, bytes([0, 0, 0, 0]), data)
    cipher_and_mac[0].extend(cipher_and_mac[1])
    data = cipher_and_mac[0]
    logging.debug('cipher and mac %s', cipher_and_mac[0].hex())

    result = feature_char.write_value(value=data)
    logging.debug('write resulted in: %s', result)

    data = []
    while not data or len(data) == 0:
        time.sleep(1)
        logging.debug('reading characteristic')
        data = feature_char.read_value()
    resp_data = bytearray([b for b in data])
    logging.debug('read: %s', bytearray(resp_data).hex())

    a2c_counter = 0
    resp_data = chacha20_aead_decrypt(bytes(), a2c_key, a2c_counter.to_bytes(8, byteorder='little'), bytes([0, 0, 0, 0]), resp_data)
    print(resp_data)

    parse_read_response(resp_data, transaction_id)

The difference between this and your earlier PoC code is just this:

len_bytes = len(data).to_bytes(2, byteorder='little')
cipher_and_mac = chacha20_aead_encrypt(len_bytes, c2a_key, cnt_bytes, bytes([0, 0, 0, 0]), data)

vs this

cipher_and_mac = chacha20_aead_encrypt(bytes(), c2a_key, cnt_bytes, bytes([0, 0, 0, 0]), data)

When I got rid of len_bytes it all started working.

Jc2k commented 5 years ago

https://github.com/jlusiardi/homekit_python/pull/57 contains staging/test.py. When I run that against my Eve Energy it turns the switch on and off on a loop, checking the result with get_characteristic after every go. It does eventually fail, but the session security seems to be working.

jlusiardi commented 5 years ago

Hey @Jc2k,

that's awesome, works with the DW1 as well, but i'm not sure why. I'll dig into it!

Jc2k commented 5 years ago

I think I mostly get it now. What bit are you not sure about? The aad being bytes() was a surprise but looking at the HAP spec the AAD is documented as set for HTTP messages, but the AAD isn't mentioned at all for BLE secure messages. I can't see that they specify the params for the ChaCha20 call for secure BLE sessions at all, at least in the copy of the spec I have. But I guess if it works it must be right?

There are some bits of my branch i'm not happy with still:

And obviously my branch is messy and needs factoring into a proper API.

I'll leave you to dig at it - let me know how it goes!

jlusiardi commented 5 years ago

hey,

i started to integrate it! works out well so far. But did you also have issues that the bluetooth le accessories just react on the second attempt?

Jc2k commented 5 years ago

Yes I did - we need to find a way for the background service discovery to complete. It forgets all the characteristics every time a session disconnects it seems.

jlusiardi commented 5 years ago

perhaps an own bluetooth lib based on https://docs.python.org/3.3/library/socket.html would help for that.

Jc2k commented 5 years ago

It was actually working in an earlier version of the code - let me dig out the magic

Jc2k commented 5 years ago

So when i was running this version of the code i could work around the 2-attempt issue by uncommenting this line: https://github.com/jlusiardi/homekit_python/commit/ec0b098393e6973c0d1aeee20bdd41f2f5c03e5c#diff-c69531862ba2a133a077f908538ed170R65

Letting the AnyDevice object run until the services were resolved.

Jc2k commented 5 years ago

Hoping to spend some time on this tomorrow. How is it looking on your end?

jlusiardi commented 5 years ago

i did some commits, but the travis ci checks keep on failing. I am looking into that at the moment

jlusiardi commented 5 years ago

Your proposal with the manager will result in a never returning program if the accessory is not available. That is not a good option, i guess. perhaps there is a better option.

Jc2k commented 5 years ago

Looking at the GATT code we could do something like:

    def setup_session(self):
        mac_address = self.pairing_data['AccessoryMAC']

        # TODO specify adapter by config?
        manager = staging.gatt.DeviceManager(adapter_name='hci0')

        self.device = staging.gatt.Device(manager=manager, mac_address=mac_address)
        logging.debug('connecting to device')
        self.device.connect()
        logging.debug('connected to device')

        if not self.device.services:
            logging.debug('waiting for services to be resolved')
            for i in range(10):
                if self.device.is_services_resolved():
                    break
                time.sleep(1)
            else:
               raise BleSessionError('Unable to resolve device services + characteristics')

            # This is called automatically when the mainloop is running, but we
            # want to avoid running it and blocking for an indeterminate amount of time.
            logging.debug('enumerating resolved services')
            self.device.services_resolved()

EDIT to add the code i've tested.

Jc2k commented 5 years ago

Didn't get to look at this as much as I wanted today but:

https://github.com/jlusiardi/homekit_python/pull/57

Makes put_char / get_char / list_accessories_and_chars more reliable - at least in terms of service enumeration.

Jc2k commented 5 years ago

(FYI I've raised https://github.com/home-assistant/home-assistant/pull/19549 to switch to using Controller in home assistant, ready for adding BLE support there).