test/security_manager_sc: split app_packet_handler to avoid multiple connect messages

This commit is contained in:
Matthias Ringwald 2020-09-23 13:52:17 +02:00
parent 8b22887d86
commit 090b2ae21f
2 changed files with 97 additions and 71 deletions

View File

@ -277,74 +277,99 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
fflush(stdout);
}
static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
bd_addr_t local_addr;
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (packet[0]) {
case BTSTACK_EVENT_STATE:
// bt stack activated, get started
if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
gap_local_bd_addr(local_addr);
printf("BD_ADDR: %s\n", bd_addr_to_str(local_addr));
// generate OOB data
sm_generate_sc_oob_data(sc_local_oob_generated_callback);
}
break;
case HCI_EVENT_LE_META:
switch (hci_event_le_meta_get_subevent_code(packet)) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
connection_handle = little_endian_read_16(packet, 4);
printf("CONNECTED: Connection handle 0x%04x\n", connection_handle);
break;
default:
break;
}
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
break;
case SM_EVENT_JUST_WORKS_REQUEST:
printf("JUST_WORKS_REQUEST\n");
break;
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
printf("NUMERIC_COMPARISON_REQUEST\n");
break;
case SM_EVENT_PASSKEY_INPUT_NUMBER:
// display number
printf("PASSKEY_INPUT_NUMBER\n");
ui_passkey = 0;
ui_digits_for_passkey = 6;
sm_keypress_notification(connection_handle, SM_KEYPRESS_PASSKEY_ENTRY_STARTED);
break;
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
// display number
printf("PASSKEY_DISPLAY_NUMBER: %06u\n", little_endian_read_32(packet, 11));
break;
case SM_EVENT_PASSKEY_DISPLAY_CANCEL:
break;
case SM_EVENT_AUTHORIZATION_REQUEST:
break;
case SM_EVENT_PAIRING_COMPLETE:
printf("\nPAIRING_COMPLETE: %u,%u\n", sm_event_pairing_complete_get_status(packet), sm_event_pairing_complete_get_reason(packet));
if (sm_event_pairing_complete_get_status(packet)) break;
if (we_are_central){
printf("Search for LE Counter service.\n");
state = TC_W4_SERVICE_RESULT;
gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_counter_service_uuid);
}
break;
case ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE:
break;
case ATT_EVENT_CAN_SEND_NOW:
att_server_notify(connection_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t *) "Pairing Success!", 16);
break;
default:
break;
}
}
fflush(stdout);
static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
bd_addr_t local_addr;
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (packet[0]) {
case BTSTACK_EVENT_STATE:
// bt stack activated, get started
if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
gap_local_bd_addr(local_addr);
printf("BD_ADDR: %s\n", bd_addr_to_str(local_addr));
// generate OOB data
sm_generate_sc_oob_data(sc_local_oob_generated_callback);
}
break;
case HCI_EVENT_LE_META:
switch (hci_event_le_meta_get_subevent_code(packet)) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
connection_handle = little_endian_read_16(packet, 4);
printf("CONNECTED: Connection handle 0x%04x\n", connection_handle);
break;
default:
break;
}
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
break;
default:
break;
}
}
fflush(stdout);
}
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (packet[0]) {
case SM_EVENT_JUST_WORKS_REQUEST:
printf("JUST_WORKS_REQUEST\n");
break;
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
printf("NUMERIC_COMPARISON_REQUEST\n");
break;
case SM_EVENT_PASSKEY_INPUT_NUMBER:
// display number
printf("PASSKEY_INPUT_NUMBER\n");
ui_passkey = 0;
ui_digits_for_passkey = 6;
sm_keypress_notification(connection_handle, SM_KEYPRESS_PASSKEY_ENTRY_STARTED);
break;
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
// display number
printf("PASSKEY_DISPLAY_NUMBER: %06u\n", little_endian_read_32(packet, 11));
break;
case SM_EVENT_PASSKEY_DISPLAY_CANCEL:
break;
case SM_EVENT_AUTHORIZATION_REQUEST:
break;
case SM_EVENT_PAIRING_COMPLETE:
printf("\nPAIRING_COMPLETE: %u,%u\n", sm_event_pairing_complete_get_status(packet), sm_event_pairing_complete_get_reason(packet));
if (sm_event_pairing_complete_get_status(packet)) break;
if (we_are_central){
printf("Search for LE Counter service.\n");
state = TC_W4_SERVICE_RESULT;
gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_counter_service_uuid);
}
break;
default:
break;
}
}
fflush(stdout);
}
static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (packet[0]) {
case ATT_EVENT_CAN_SEND_NOW:
att_server_notify(connection_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t *) "Pairing Success!", 16);
break;
default:
break;
}
}
fflush(stdout);
}
static void stdin_process(char c){
@ -495,7 +520,7 @@ int btstack_main(int argc, const char * argv[]){
}
// inform about BTstack state
hci_event_callback_registration.callback = &app_packet_handler;
hci_event_callback_registration.callback = &hci_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
// set up l2cap_le
@ -518,12 +543,12 @@ int btstack_main(int argc, const char * argv[]){
sm_test_set_pairing_failure(sm_failure);
}
sm_event_callback_registration.callback = &app_packet_handler;
sm_event_callback_registration.callback = &sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
// setup ATT server
att_server_init(profile_data, att_read_callback, att_write_callback);
att_server_register_packet_handler(app_packet_handler);
att_server_register_packet_handler(&att_packet_handler);
btstack_stdin_setup(stdin_process);

View File

@ -137,6 +137,7 @@ class Node:
self.peer_addr = addr
def write(self, string):
print("CMD -> %s: %s" % (self.name, string))
self.stdin.write(string.encode('utf-8'))
def terminate(self):