maximkulkin / esp-homekit

Apple HomeKit accessory server library for ESP-OPEN-RTOS
MIT License
1.1k stars 169 forks source link

Led status example #72

Closed GermanSheepDog closed 5 years ago

GermanSheepDog commented 5 years ago

I found a little challenge at led_status example (line 6):

void on_event(homekit_event_t event) {
    if (event == HOMEKIT_EVENT_SERVER_INITIALIZED) {
        led_status_set(led_status, paired ? &normal_mode : &unpaired);
    }
    else if (event == HOMEKIT_EVENT_CLIENT_CONNECTED) {
        if (!paired) //<- Here is the little unexpected comparison
            led_status_set(led_status, &pairing);
    }
    else if (event == HOMEKIT_EVENT_CLIENT_DISCONNECTED) {
        if (!paired)
            led_status_set(led_status, &unpaired);
    }
...
}

I would have expected at line 6:

  if (paired) // not (!paired)
      led_status_set(led_status, &pairing);
maximkulkin commented 5 years ago

No, the code is correct: when client connects, you get HOMEKIT_EVENT_CLIENT_CONNECTED event. By that time it's just socket connection, but if there are no pairings yet, there is a big chance that client has connected to pair (and thus transitioning to pairing state). If client has connected and there is a pairing already, then do not do anything: by that time pairing is disabled and client will try pairing and fail or it will do pair verification and proceed managing accessory as usual.