Skip to content

Commit

Permalink
Merge branch 'bugfix/pairing-ctx' into 'master'
Browse files Browse the repository at this point in the history
esp_hap_pairings: Manage session context correctly

See merge request app-frameworks/esp-homekit-sdk!28
  • Loading branch information
shahpiyushv committed Feb 15, 2024
2 parents bd236e7 + 367546c commit 68c049e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions components/homekit/esp_hap_core/src/esp_hap_pair_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <esp_hap_ip_services.h>
#include <esp_hap_pair_common.h>
#include <esp_hap_pair_setup.h>
#include <esp_hap_pair_verify.h>
#include <esp_hap_database.h>
#include <esp_hap_main.h>
#include <esp_hap_acc.h>
Expand Down Expand Up @@ -486,6 +487,7 @@ static pair_setup_ctx_t *hap_pair_setup_ctx_action(int action)
else {
ps_ctx = hap_platform_memory_calloc(sizeof(pair_setup_ctx_t), 1);
if (ps_ctx) {
ps_ctx->process = PROCESS_PAIR_SETUP;
ps_ctx->timer = xTimerCreate("hap_setup_timer", HAP_SETUP_TIMEOUT_IN_TICKS,
pdFALSE, (void *) ps_ctx, hap_pair_setup_timeout);
xTimerStart(ps_ctx->timer, 0);
Expand All @@ -512,7 +514,7 @@ static pair_setup_ctx_t *hap_pair_setup_ctx_action(int action)
void hap_pair_setup_ctx_clean(void *sess_ctx)
{
if (sess_ctx) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Cleaning Pair Setup Context");
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Clearing Pair Setup Context");
hap_pair_setup_ctx_action(PS_CTX_DEINIT);
}
}
Expand Down Expand Up @@ -545,9 +547,18 @@ int hap_pair_setup_process(void **ctx, uint8_t *buf, int inlen, int bufsize, int
* that pair setup was restarted. Handle it accordingly
*/
if ((recv_state == STATE_M1) && (ps_ctx->state != STATE_M0)) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Restarting Pair Setup");
int sock_fd = ps_ctx->sock_fd;
hap_pair_setup_ctx_clean(ps_ctx);
if (ps_ctx->process == PROCESS_PAIR_VERIFY) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Clearing Pair Verify Context");
hap_pair_verify_context_deinit(ps_ctx);
} else if (ps_ctx->process == PROCESS_PAIR_SETUP) {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "Restarting Pair Setup");
hap_pair_setup_ctx_clean(ps_ctx);
} else {
ESP_MFI_DEBUG(ESP_MFI_DEBUG_ERR, "Invalid Pair Setup Context");
hap_prepare_error_tlv(recv_state + 1, kTLVError_Unknown, buf, bufsize, outlen);
return HAP_FAIL;
}
int ret = hap_pair_setup_context_init(sock_fd, ctx, buf, bufsize, outlen);
if (ret != HAP_SUCCESS)
return ret;
Expand Down
12 changes: 12 additions & 0 deletions components/homekit/esp_hap_core/src/esp_hap_pair_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ typedef struct {
* of whether the context points to pair_verify_ctx_t or hap_secure_session_t.
*/
uint8_t state;
/* This is to identify pair setup vs pair verify, since both of these operation
* can happen on the same socket and can have similar states
*/
uint8_t process;
uint8_t ctrl_curve_pk[CURVE_KEY_LEN];
uint8_t acc_curve_pk[CURVE_KEY_LEN];
uint8_t hkdf_key[ENCRYPT_KEY_LEN];
Expand Down Expand Up @@ -459,11 +463,19 @@ int hap_pair_verify_context_init(void **ctx, uint8_t *buf, int bufsize, int *out
hap_prepare_error_tlv(STATE_M2, kTLVError_Unknown, buf, bufsize, outlen);
return HAP_FAIL;
}
pv_ctx->process = PROCESS_PAIR_VERIFY;
*ctx = pv_ctx;
ESP_MFI_DEBUG(ESP_MFI_DEBUG_INFO, "######## Starting Pair Verify ########");
return HAP_SUCCESS;
}

void hap_pair_verify_context_deinit(void *pv_ctx)
{
if (pv_ctx) {
hap_platform_memory_free(pv_ctx);
}
}

uint8_t hap_pair_verify_get_state(void *ctx)
{
pair_verify_ctx_t *pv_ctx = (pair_verify_ctx_t *)ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#define STATE_VERIFIED 0x55
#define STATE_INVALID 0xaa

#define PROCESS_PAIR_SETUP 0x33
#define PROCESS_PAIR_VERIFY 0xcc

typedef enum {
HAP_METHOD_RESERVED = 0,
HAP_METHOD_PAIR_SETUP = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

typedef struct {
uint8_t state;
uint8_t process;
uint8_t method;
uint32_t pairing_flags;
int8_t pairing_flags_len;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <esp_hap_pair_common.h>
#include <esp_hap_controllers.h>
int hap_pair_verify_context_init(void **ctx, uint8_t *buf, int bufsize, int *outlen);
void hap_pair_verify_context_deinit(void *pv_ctx);
int hap_pair_verify_process(void **ctx, uint8_t *buf, int inlen, int bufsize, int *outlen);
uint8_t hap_pair_verify_get_state(void *ctx);
void hap_free_session(void *session);
Expand Down

0 comments on commit 68c049e

Please sign in to comment.