apple / HomeKitADK

Apache License 2.0
2.55k stars 231 forks source link

LightBulb sample application lost connection after a while. #54

Open ht-hieu opened 4 years ago

ht-hieu commented 4 years ago

Hi everyone,

I am having an issue with the sample LightBulb application. The HomeKit app is disconnected from the accessory server after idling a few minutes. After that, the HomeKit app cannot connect to the accessory server again. I tried to restart the app but it still not working (I tried to restart the Iphone too). If I restart the accessory server, it can work again.

Here is my log:

   0000  47455420 2f636861 72616374 65726973 74696373 3f69643d 312e3531 20485454    GET /characteristics?id=1.51 HTT
    0020  502f312e 310d0a48 6f73743a 2041636d 655c3033 324c6967 68745c30 33324275    P/1.1..Host: Acme\032Light\032Bu
    0040  6c622e5f 6861702e 5f746370 2e6c6f63 616c0d0a 0d0a                          lb._hap._tcp.local....
2020-05-11'T'11:14:08'Z'        Info    [com.apple.mfi.HomeKit.Core:Characteristic] [0000000000000001 Acme Light Bulb] [0000000000000033 on] Calling read handler.
2020-05-11'T'11:14:08'Z'        Info    HandleLightBulbOnRead: false
2020-05-11'T'11:14:08'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:<
    0000  48545450 2f312e31 20323030 204f4b0d 0a436f6e 74656e74 2d547970 653a2061    HTTP/1.1 200 OK..Content-Type: a
    0020  70706c69 63617469 6f6e2f68 61702b6a 736f6e0d 0a436f6e 74656e74 2d4c656e    pplication/hap+json..Content-Len
    0040  6774683a 2035300d 0a0d0a7b 22636861 72616374 65726973 74696373 223a5b7b    gth: 50....{"characteristics":[{
    0060  22616964 223a312c 22696964 223a3531 2c227661 6c756522 3a307d5d 7d          "aid":1,"iid":51,"value":0}]}
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:input closed
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:closing
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:closing security context
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] Closing HAP session.
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:Session] HAPSessionRelease
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:Session] HAPSessionInvalidate
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:closing TCP stream
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Platform:TCPStreamManager] shutdown(11, SHUT_RDWR);
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Platform:TCPStreamManager] close(11);
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:closed
2020-05-11'T'11:21:32'Z'        Debug   [com.apple.mfi.HomeKit.Core:IPAccessoryServer] session:0x69b300:releasing session

I am running Ubuntu 18.04 in a virtual machine (VirtualBox). Could you please help me with this problem? Thank you in advance.

ghost commented 4 years ago

@gochit, could you please try to browse and see if bonjour is working or not on your iPhone when this happens? With this tool: https://itunes.apple.com/us/app/discovery-dns-sd-browser/id305441017?mt=8

ht-hieu commented 4 years ago

Hi @hao-gao, when this happens, the Discovery app stuck in scanning (I waited for more than 30 minutes). I tried with an Android equivalent, but I got the same result.

I also tried scanning without the lightbulb application, but it stuck in the scanning process too. It only works normally if I run an instance of the lightbulb app.

danilvpetrov commented 4 years ago

Hi all,

I am having exactly the same problem using Espressif ESP32-S2-Saola-1 Dev Board. I got with the same logs telling me that the input is closed, shutting down the session.

Instead of using the app mentioned above, I was able to debug service discovery by running the following:

dns-sd -B _hap._tcp

with the following output:

Timestamp     A/R    Flags  if Domain               Service Type         Instance Name
23:40:06.700  Add        3   5 local.               _hap._tcp.           Acme Light Bulb

Please note the accessory still advertises itself without any problem even after it becomes irresponsive to the homekit on the iPhone or macbook.

danilvpetrov commented 4 years ago

Ok, actually it stops advertising after about 10 minutes of the session end and while the accessory is still powered.

crasmussen commented 3 years ago

Hey @danilvpetrov , I'm seeing the exact same issue. Did you make any progress diagnosing the issue?

izmmisha commented 3 years ago

Hi, can’t say anything about Linux platform in this case, but with esp32 platform I managed to same problem with my code based on lightbulb example.

The problem is how esp32 platform handle wifi connection. WiFi session require to update encryption keys from time to time. This process on esp32 platform is not seamless, it will raise disconnect event with reason deauth (https://docs.espressif.com/projects/esp-idf/en/stable/api-guides/wifi.html#wifi-event-sta-disconnected) and you should connect to wifi again.