From d19b56e8becdd5a313b0afa92062f69957ef1e3b Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Fri, 27 Feb 2015 14:17:45 +0000 Subject: [PATCH] use single timer for SM, stop timer on sm_done_for_handle --- ble/sm.c | 56 +++++++++++++++++++++++++++---------------------------- src/hci.h | 1 - 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/ble/sm.c b/ble/sm.c index ceb874863..71bd9d406 100644 --- a/ble/sm.c +++ b/ble/sm.c @@ -166,6 +166,8 @@ static sm_connection_t * sm_csrk_connection_source; // data needed for security setup typedef struct sm_setup_context { + timer_source_t sm_timeout; + // used in all phases uint8_t sm_pairing_failed_reason; @@ -290,28 +292,28 @@ static void sm_truncate_key(sm_key_t key, int max_encryption_size){ // Security Manager Channel. A new SM procedure shall only be performed when a new physical link has been // established. -static void sm_2timeout_handler(timer_source_t * timer){ +static void sm_timeout_handler(timer_source_t * timer){ log_info("SM timeout"); sm_connection_t * sm_conn = (sm_connection_t *) linked_item_get_user((linked_item_t*) timer); - sm_done_for_handle(sm_conn->sm_handle); sm_conn->sm_engine_state = SM_GENERAL_TIMEOUT; + sm_done_for_handle(sm_conn->sm_handle); // trigger handling of next ready connection sm_run(); } -static void sm_2timeout_start(sm_connection_t * sm_conn){ - run_loop_remove_timer(&sm_conn->sm_timeout); - run_loop_set_timer_handler(&sm_conn->sm_timeout, sm_2timeout_handler); - run_loop_set_timer(&sm_conn->sm_timeout, 30000); // 30 seconds sm timeout - linked_item_set_user((linked_item_t*) &sm_conn->sm_timeout, sm_conn); - run_loop_add_timer(&sm_conn->sm_timeout); +static void sm_timeout_start(sm_connection_t * sm_conn){ + run_loop_remove_timer(&setup->sm_timeout); + run_loop_set_timer_handler(&setup->sm_timeout, sm_timeout_handler); + run_loop_set_timer(&setup->sm_timeout, 30000); // 30 seconds sm timeout + linked_item_set_user((linked_item_t*) &setup->sm_timeout, sm_conn); + run_loop_add_timer(&setup->sm_timeout); } -static void sm_2timeout_stop(sm_connection_t * sm_conn){ - run_loop_remove_timer(&sm_conn->sm_timeout); +static void sm_timeout_stop(){ + run_loop_remove_timer(&setup->sm_timeout); } -static void sm_2timeout_reset(sm_connection_t * sm_conn){ - sm_2timeout_stop(sm_conn); - sm_2timeout_start(sm_conn); +static void sm_timeout_reset(sm_connection_t * sm_conn){ + sm_timeout_stop(); + sm_timeout_start(sm_conn); } // end of sm timeout @@ -754,8 +756,9 @@ static int sm_key_distribution_all_received(){ static void sm_done_for_handle(uint16_t handle){ if (sm_active_connection == handle){ - log_info("sm: connection 0x%x relesed setup context", sm_active_connection); + sm_timeout_stop(); sm_active_connection = 0; + log_info("sm: connection 0x%x released setup context", handle); } } @@ -821,7 +824,7 @@ static void sm_responder_setup(sm_connection_t * sm_conn){ sm_setup_key_distribution(setup->sm_m_preq.responder_key_distribution); // start SM timeout - sm_2timeout_start(sm_conn); + sm_timeout_start(sm_conn); // decide on STK generation method sm_setup_tk(); @@ -1043,7 +1046,7 @@ static void sm_run(void){ setup->sm_m_preq.code = SM_CODE_PAIRING_REQUEST; connection->sm_engine_state = SM_INITIATOR_PH1_W4_PAIRING_RESPONSE; l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_m_preq, sizeof(sm_pairing_packet_t)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); break; // responder side @@ -1065,7 +1068,7 @@ static void sm_run(void){ connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM; l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); sm_trigger_user_response(connection); @@ -1083,7 +1086,6 @@ static void sm_run(void){ buffer[1] = setup->sm_pairing_failed_reason; connection->sm_engine_state = SM_GENERAL_IDLE; l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_stop(connection); sm_done_for_handle(connection->sm_handle); break; } @@ -1098,7 +1100,7 @@ static void sm_run(void){ connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_RANDOM; } l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); break; } @@ -1189,7 +1191,7 @@ static void sm_run(void){ connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_CONFIRM; } l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } case SM_RESPONDER_PH2_SEND_LTK_REPLY: { @@ -1230,7 +1232,7 @@ static void sm_run(void){ buffer[0] = SM_CODE_ENCRYPTION_INFORMATION; swap128(setup->sm_ltk, &buffer[1]); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION){ @@ -1240,7 +1242,7 @@ static void sm_run(void){ bt_store_16(buffer, 1, setup->sm_local_ediv); swap64(setup->sm_local_rand, &buffer[3]); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ @@ -1249,7 +1251,7 @@ static void sm_run(void){ buffer[0] = SM_CODE_IDENTITY_INFORMATION; swap128(sm_persistent_irk, &buffer[1]); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION){ @@ -1260,7 +1262,7 @@ static void sm_run(void){ hci_le_advertisement_address(&buffer[1], &local_address); bt_flip_addr(&buffer[2], local_address); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ @@ -1269,7 +1271,7 @@ static void sm_run(void){ buffer[0] = SM_CODE_SIGNING_INFORMATION; swap128(setup->sm_local_csrk, &buffer[1]); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); - sm_2timeout_reset(connection); + sm_timeout_reset(connection); return; } @@ -1279,7 +1281,6 @@ static void sm_run(void){ connection->sm_engine_state = SM_PH3_RECEIVE_KEYS; } else { // master -> all done - sm_2timeout_stop(connection); connection->sm_engine_state = SM_GENERAL_IDLE; sm_done_for_handle(connection->sm_handle); } @@ -1773,7 +1774,7 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac // identical to responder // start SM timeout - sm_2timeout_start(sm_conn); + sm_timeout_start(sm_conn); // decide on STK generation method sm_setup_tk(); @@ -1920,7 +1921,6 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac // done with key distribution? if (sm_key_distribution_all_received()){ if (sm_conn->sm_role){ - sm_2timeout_stop(sm_conn); sm_conn->sm_engine_state = SM_GENERAL_IDLE; sm_done_for_handle(sm_conn->sm_handle); } else { diff --git a/src/hci.h b/src/hci.h index aba616f6c..3be18ee5f 100644 --- a/src/hci.h +++ b/src/hci.h @@ -453,7 +453,6 @@ typedef struct sm_connection { uint8_t sm_actual_encryption_key_size; sm_pairing_packet_t sm_m_preq; // only used during c1 authorization_state_t sm_connection_authorization_state; - timer_source_t sm_timeout; } sm_connection_t; typedef struct {