From 78404023e9d71a36d0625e1ed57e505b97ef6bea Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 29 Jun 2022 12:02:54 +0200 Subject: [PATCH] example: use sm pairing events instead of hci encryption change (v2) --- example/hog_boot_host_demo.c | 20 +++++--------------- example/hog_host_demo.c | 26 ++++++-------------------- example/sm_pairing_central.c | 5 ----- 3 files changed, 11 insertions(+), 40 deletions(-) diff --git a/example/hog_boot_host_demo.c b/example/hog_boot_host_demo.c index 40d509918..c981a19d5 100644 --- a/example/hog_boot_host_demo.c +++ b/example/hog_boot_host_demo.c @@ -513,20 +513,6 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack app_state = W4_ENCRYPTED; sm_request_pairing(connection_handle); break; - case HCI_EVENT_ENCRYPTION_CHANGE: - case HCI_EVENT_ENCRYPTION_CHANGE_V2: - if (connection_handle != hci_event_encryption_change_get_connection_handle(packet)) break; - printf("Connection encrypted: %u\n", hci_event_encryption_change_get_encryption_enabled(packet)); - if (hci_event_encryption_change_get_encryption_enabled(packet) == 0){ - printf("Encryption failed -> abort\n"); - handle_outgoing_connection_error(); - break; - } - // continue - query primary services - printf("Search for HID service.\n"); - app_state = W4_HID_SERVICE_FOUND; - gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE); - break; default: break; } @@ -568,12 +554,16 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa switch (sm_event_pairing_complete_get_status(packet)){ case ERROR_CODE_SUCCESS: printf("Pairing complete, success\n"); + // continue - query primary services + printf("Search for HID service.\n"); + app_state = W4_HID_SERVICE_FOUND; + gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE); break; case ERROR_CODE_CONNECTION_TIMEOUT: printf("Pairing failed, timeout\n"); break; case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: - printf("Pairing faileed, disconnected\n"); + printf("Pairing failed, disconnected\n"); break; case ERROR_CODE_AUTHENTICATION_FAILURE: printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); diff --git a/example/hog_host_demo.c b/example/hog_host_demo.c index 45eb49f11..736bd0dcb 100644 --- a/example/hog_host_demo.c +++ b/example/hog_host_demo.c @@ -374,7 +374,6 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack UNUSED(channel); UNUSED(size); uint8_t event; - uint8_t status; /* LISTING_RESUME */ switch (packet_type) { case HCI_EVENT_PACKET: @@ -426,24 +425,6 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack app_state = W4_ENCRYPTED; sm_request_pairing(connection_handle); break; - case HCI_EVENT_ENCRYPTION_CHANGE: - case HCI_EVENT_ENCRYPTION_CHANGE_V2: - if (connection_handle != hci_event_encryption_change_get_connection_handle(packet)) break; - printf("Connection encrypted: %u\n", hci_event_encryption_change_get_encryption_enabled(packet)); - if (hci_event_encryption_change_get_encryption_enabled(packet) == 0){ - printf("Encryption failed -> abort\n"); - handle_outgoing_connection_error(); - break; - } - // continue - query primary services - printf("Search for HID service.\n"); - app_state = W4_HID_CLIENT_CONNECTED; - - status = hids_client_connect(connection_handle, handle_gatt_client_event, protocol_mode, &hids_cid); - if (status != ERROR_CODE_SUCCESS){ - printf("HID client connection failed, status 0x%02x\n", status); - } - break; default: break; } @@ -485,12 +466,17 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa switch (sm_event_pairing_complete_get_status(packet)){ case ERROR_CODE_SUCCESS: printf("Pairing complete, success\n"); + + // continue - query primary services + printf("Search for HID service.\n"); + app_state = W4_HID_CLIENT_CONNECTED; + hids_client_connect(connection_handle, handle_gatt_client_event, protocol_mode, &hids_cid); break; case ERROR_CODE_CONNECTION_TIMEOUT: printf("Pairing failed, timeout\n"); break; case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: - printf("Pairing faileed, disconnected\n"); + printf("Pairing failed, disconnected\n"); break; case ERROR_CODE_AUTHENTICATION_FAILURE: printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); diff --git a/example/sm_pairing_central.c b/example/sm_pairing_central.c index 3234f3110..89e6ea58b 100644 --- a/example/sm_pairing_central.c +++ b/example/sm_pairing_central.c @@ -213,11 +213,6 @@ static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p // general gatt client request to trigger mandatory authentication // gatt_client_discover_primary_services(&hci_packet_handler, con_handle); break; - case HCI_EVENT_ENCRYPTION_CHANGE: - case HCI_EVENT_ENCRYPTION_CHANGE_V2: - con_handle = hci_event_encryption_change_get_connection_handle(packet); - printf("Connection encrypted: %u\n", hci_event_encryption_change_get_encryption_enabled(packet)); - break; case GATT_EVENT_QUERY_COMPLETE: status = gatt_event_query_complete_get_att_status(packet); switch (status){