Skip to content

Commit

Permalink
Merge branch 'task/multiple_fixes' into 'master'
Browse files Browse the repository at this point in the history
Multiple fixes

See merge request app-frameworks/esp-homekit-sdk!8
  • Loading branch information
shahpiyushv committed Jan 11, 2021
2 parents 040b0f3 + ccb8737 commit 389189a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 19 deletions.
11 changes: 11 additions & 0 deletions components/homekit/esp_hap_core/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ menu "HomeKit"
If you have the MFi variant of the SDK, set this to true to use features like
HW authentication, software token authentication and WAC.

config HAP_SESSION_KEEP_ALIVE_ENABLE
bool "Enable Keep Alive on sessions"
default n
depends on !HAP_MFI_ENABLE
help
Enable Keep Alive on Controller Sessions so that stale connections do not stay
open indefinitely. Note that the HomeKit Specs do not allow this and so, this
should be enabled at your own risk. Even without this option, the HomeKit core
will close stale session using the HTTP Server's Least Recently Used (LRU) purge
logic.

endmenu
22 changes: 22 additions & 0 deletions components/homekit/esp_hap_core/src/esp_hap_ip_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ static int hap_http_pair_verify_handler(httpd_req_t *req)
sizeof(timeout)) < 0) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "setsockopt on pair verified socket failed for SO_SNDTIMEO");
}
#ifdef CONFIG_HAP_SESSION_KEEP_ALIVE_ENABLE
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Enabling Keep-Alive on Pair Verify Session");
const int yes = 1; /* enable sending keepalive probes for socket */
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) < 0 ) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "setsockopt on pair verified socket failed for SO_KEEPALIVE");
}

const int idle = 180; /* 180 sec idle before start sending probes */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) < 0) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "setsockopt on pair verified socket failed for TCP_KEEPIDLE");
}

const int interval = 30; /* 30 sec between probes */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)) < 0) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "setsockopt on pair verified socket failed for TCP_KEEPINTVL");
}

const int maxpkt = 4; /* Drop connection after 4 probes without response */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &maxpkt, sizeof(maxpkt)) < 0) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "setsockopt on pair verified socket failed for TCP_KEEPCNT");
}
#endif
hap_platform_httpd_set_sess_ctx(req, ctx, hap_free_session, true);
httpd_sess_set_send_override(hap_priv.server, fd, hap_httpd_send);
httpd_sess_set_recv_override(hap_priv.server, fd, hap_httpd_recv);
Expand Down
10 changes: 5 additions & 5 deletions components/homekit/esp_hap_core/src/esp_hap_network_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ int hap_encrypt_data(hap_encrypt_frame_t *frame, hap_secure_session_t *session,
return 2 + buflen + 16; /* Total length of the encrypted data */
}

int hap_decrypt_error(hap_secure_session_t *session)
static int hap_session_error(hap_secure_session_t *session)
{
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Decryption error/Connection lost. Marking session as invalid");
if (session) {
session->state = STATE_INVALID;
hap_close_ctrl_sessions(session->ctrl);
hap_close_session(session);
}
return HAP_FAIL;
}
Expand All @@ -116,7 +116,7 @@ int hap_decrypt_data(hap_decrypt_frame_t *frame, hap_secure_session_t *session,
}
if ((frame->pkt_size - frame->bytes_read) == 0) {
if (read_fn(frame->data, 2, context) < 2)
return hap_decrypt_error(session);
return hap_session_error(session);

frame->pkt_size = get_u16_le(frame->data);
frame->bytes_read = 0;
Expand All @@ -125,7 +125,7 @@ int hap_decrypt_data(hap_decrypt_frame_t *frame, hap_secure_session_t *session,
int num_bytes = read_fn(&frame->data[frame->bytes_read],
bytes_to_read, context);
if (num_bytes <= 0)
return hap_decrypt_error(session);
return hap_session_error(session);
bytes_to_read -= num_bytes;
frame->bytes_read += num_bytes;
}
Expand All @@ -140,7 +140,7 @@ int hap_decrypt_data(hap_decrypt_frame_t *frame, hap_secure_session_t *session,
&frame->data[frame->bytes_read], aad, 2, newnonce, session->decrypt_key);
if (ret != 0) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "AEAD decryption failure");
return hap_decrypt_error(session);
return hap_session_error(session);
}
frame->bytes_read = 0;
/* Increment nonce after every frame */
Expand Down
20 changes: 18 additions & 2 deletions components/homekit/esp_hap_core/src/esp_hap_pair_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,23 @@ typedef struct {
hap_secure_session_t *session;
} pair_verify_ctx_t;

void hap_close_ctrl_sessions(hap_ctrl_data_t *ctrl)
void hap_close_session(hap_secure_session_t *session)
{
if (!session)
return;
int i;
for (i = 0; i < HAP_MAX_SESSIONS; i++) {
if (!hap_priv.sessions[i])
continue;
if (hap_priv.sessions[i] == session) {
hap_report_event(HAP_EVENT_CTRL_DISCONNECTED, (session->ctrl->info.id),
sizeof((session->ctrl->info.id)));
httpd_sess_trigger_close(hap_priv.server, hap_priv.sessions[i]->conn_identifier);
}
}
}

void hap_close_sessions_of_ctrl(hap_ctrl_data_t *ctrl)
{
if (!ctrl)
return;
Expand Down Expand Up @@ -95,7 +111,7 @@ void hap_close_all_sessions()
for (i = 0; i < HAP_MAX_SESSIONS; i++) {
if (hap_priv.sessions[i]) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Closing Session");
hap_close_ctrl_sessions(hap_priv.sessions[i]->ctrl);
hap_close_session(hap_priv.sessions[i]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/homekit/esp_hap_core/src/esp_hap_pairings.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void hap_remove_all_controllers()
int i;
for (i = 0; i < HAP_MAX_CONTROLLERS; i++) {
if (hap_priv.controllers[i].valid) {
hap_close_ctrl_sessions(&hap_priv.controllers[i]);
hap_close_sessions_of_ctrl(&hap_priv.controllers[i]);
hap_controller_remove(&hap_priv.controllers[i]);
}
}
Expand All @@ -91,7 +91,7 @@ static int hap_process_pair_remove(uint8_t *buf, int inlen, int bufsize, int *ou

ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Removing Controller %s", ctrl_id);
hap_ctrl_data_t *ctrl = hap_get_controller(ctrl_id);
hap_close_ctrl_sessions(ctrl);
hap_close_sessions_of_ctrl(ctrl);
hap_controller_remove(ctrl);

if (!is_admin_paired()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int hap_pair_verify_process(void **ctx, uint8_t *buf, int inlen, int bufsize, in
uint8_t hap_pair_verify_get_state(void *ctx);
void hap_free_session(void *session);
int hap_get_ctrl_session_index(hap_secure_session_t *session);
void hap_close_ctrl_sessions(hap_ctrl_data_t *ctrl);
int hap_close_session(hap_secure_session_t *session);
void hap_close_sessions_of_ctrl(hap_ctrl_data_t *ctrl);
void hap_close_all_sessions();
#endif /* _HAP_PAIR_VERIFY_H_ */
8 changes: 6 additions & 2 deletions examples/aws-iot/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ static void lightbulb_thread_entry(void *p)
ESP_LOGI(TAG, "Accessory is paired with %d controllers",
hap_get_paired_controller_count());

esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, aws_iot_ip_event_cb, NULL);

/* TODO: Do the actual hardware initialization here */

/* For production accessories, the setup code shouldn't be programmed on to
Expand Down Expand Up @@ -214,6 +212,12 @@ static void lightbulb_thread_entry(void *p)
/* Initialize Wi-Fi */
app_wifi_init();

/* Register an event handler so that AWS IoT can be started after the station
* interface gets an IP Address.
* All event handlers should be registered only after app_wifi_init()
*/
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, aws_iot_ip_event_cb, NULL);

/* After all the initializations are done, start the HAP core */
hap_start();
/* Start Wi-Fi */
Expand Down
8 changes: 4 additions & 4 deletions examples/emulator/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ static void emulator_thread_entry(void *p)
/* Register an event handler for HomeKit specific events */
hap_register_event_handler(emulator_hap_event_handler);

/* Register system event handler */
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &hap_emulator_system_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &hap_emulator_system_event_handler, NULL);

/* Query the controller count (just for information) */
ESP_LOGI(TAG, "Accessory is paired with %d controllers",
hap_get_paired_controller_count());
Expand Down Expand Up @@ -285,6 +281,10 @@ static void emulator_thread_entry(void *p)
/* Initialize Wi-Fi */
app_wifi_init();

/* Register system event handler */
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &hap_emulator_system_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &hap_emulator_system_event_handler, NULL);

/* After all the initializations are done, start the HAP core */
if (hap_start() != ESP_OK) {
printf("Failed to start HAP\n");
Expand Down
8 changes: 5 additions & 3 deletions examples/fan/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ static void fan_thread_entry(void *p)
*/
reset_key_init(RESET_GPIO);

/* Register an event handler for HomeKit specific events */
esp_event_handler_register(HAP_EVENT, ESP_EVENT_ANY_ID, &fan_hap_event_handler, NULL);

/* Query the controller count (just for information) */
ESP_LOGI(TAG, "Accessory is paired with %d controllers",
hap_get_paired_controller_count());
Expand Down Expand Up @@ -310,6 +307,11 @@ static void fan_thread_entry(void *p)
/* Initialize Wi-Fi */
app_wifi_init();

/* Register an event handler for HomeKit specific events.
* All event handlers should be registered only after app_wifi_init()
*/
esp_event_handler_register(HAP_EVENT, ESP_EVENT_ANY_ID, &fan_hap_event_handler, NULL);

/* After all the initializations are done, start the HAP core */
hap_start();
/* Start Wi-Fi */
Expand Down

0 comments on commit 389189a

Please sign in to comment.