diff --git a/ble/sm_minimal.c b/ble/sm_minimal.c index 3c79d1803..499d8ee12 100644 --- a/ble/sm_minimal.c +++ b/ble/sm_minimal.c @@ -122,40 +122,8 @@ static void sm_pdu_received_in_wrong_state(void){ sm_state_responding = SM_GENERAL_SEND_PAIRING_FAILED; } -static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ - if (packet_type != SM_DATA_PACKET) return; - - if (handle != sm_response_handle){ - log_info("sm_packet_handler: packet from handle %u, but expecting from %u", handle, sm_response_handle); - return; - } - - if (packet[0] == SM_CODE_PAIRING_FAILED){ - sm_state_responding = SM_GENERAL_SEND_PAIRING_FAILED; - return; - } - - switch (sm_state_responding){ - - case SM_GENERAL_IDLE: { - if (packet[0] != SM_CODE_PAIRING_REQUEST){ - sm_pdu_received_in_wrong_state(); - break;; - } - sm_state_responding = SM_GENERAL_SEND_PAIRING_FAILED; - sm_pairing_failed_reason = SM_REASON_PAIRING_NOT_SUPPORTED; - break; - } - default: - break; - } - - // try to send preparared packet - sm_run(); -} - -static void sm_event_packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ +static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ switch (packet_type) { @@ -199,6 +167,44 @@ static void sm_event_packet_handler (void * connection, uint8_t packet_type, uin sm_run(); } +static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ + + if (packet_type == HCI_EVENT_PACKET) { + sm_event_packet_handler(packet_type, handle, packet, size); + return; + } + + if (packet_type != SM_DATA_PACKET) return; + + if (handle != sm_response_handle){ + log_info("sm_packet_handler: packet from handle %u, but expecting from %u", handle, sm_response_handle); + return; + } + + if (packet[0] == SM_CODE_PAIRING_FAILED){ + sm_state_responding = SM_GENERAL_SEND_PAIRING_FAILED; + return; + } + + switch (sm_state_responding){ + + case SM_GENERAL_IDLE: { + if (packet[0] != SM_CODE_PAIRING_REQUEST){ + sm_pdu_received_in_wrong_state(); + break;; + } + sm_state_responding = SM_GENERAL_SEND_PAIRING_FAILED; + sm_pairing_failed_reason = SM_REASON_PAIRING_NOT_SUPPORTED; + break; + } + default: + break; + } + + // try to send preparared packet + sm_run(); +} + static void sm_run(void){ // assert that we can send either one @@ -225,7 +231,6 @@ static void sm_run(void){ void sm_init(void){ // attach to lower layers l2cap_register_fixed_channel(sm_packet_handler, L2CAP_CID_SECURITY_MANAGER_PROTOCOL); - l2cap_register_packet_handler(sm_event_packet_handler); } // GAP LE diff --git a/src/hci.c b/src/hci.c index 5d4241047..181b01109 100644 --- a/src/hci.c +++ b/src/hci.c @@ -1039,17 +1039,19 @@ static void hci_initializing_run(void){ hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); break; #endif - // DONE - case HCI_INIT_DONE: - // done. - hci_stack->state = HCI_STATE_WORKING; - hci_emit_state(); - return; default: return; } } +static void hci_init_done(void){ + // done. tell the app + log_info("hci_init_done -> HCI_STATE_WORKING"); + hci_stack->state = HCI_STATE_WORKING; + hci_emit_state(); + hci_run(); +} + static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ uint8_t command_completed = 0; @@ -1197,7 +1199,7 @@ static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ return; } else { log_error("Neither BR/EDR nor LE supported"); - hci_stack->substate = HCI_INIT_DONE; // skip all + hci_init_done(); return; } } @@ -1221,7 +1223,7 @@ static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: if (!hci_le_supported()){ // SKIP LE init for Classic only configuration - hci_stack->substate = HCI_INIT_DONE; + hci_init_done(); return; } break; @@ -1229,11 +1231,16 @@ static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ case HCI_INIT_W4_WRITE_SCAN_ENABLE: if (!hci_le_supported()){ // SKIP LE init for Classic only configuration - hci_stack->substate = HCI_INIT_DONE; + hci_init_done(); return; } #endif break; + // Response to command before init done state -> init done + case (HCI_INIT_DONE-1): + hci_init_done(); + return; + default: break; }