hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ hfp_connection_t * context = get_hfp_connection_context_for_bd_addr(bd_addr); if (context) return context; context = create_hfp_connection_context(); memcpy(context->remote_addr, bd_addr, 6); return context; }
void hfp_ag_establish_audio_connection(bd_addr_t bd_addr) { hfp_ag_establish_service_level_connection(bd_addr); hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (!connection) { log_error("HFP AG: connection doesn't exist."); return; } if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; // if (connection->remote_codecs_nr == 0) { // log_error("HFP AG: codecs not exchanged, or no codecs specified in HF."); // return; // } connection->trigger_codec_connection_setup = 1; connection->establish_audio_connection = 1; if (!has_codec_negotiation_feature(connection)) { connection->trigger_codec_connection_setup = 0; connection->establish_audio_connection = 0; return; } if (connection->state == HFP_CODECS_CONNECTION_ESTABLISHED) { connection->trigger_codec_connection_setup = 0; return; } hfp_run_for_context(connection); }
void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){ hfp_ag_establish_service_level_connection(bd_addr); hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); connection->establish_audio_connection = 0; if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; if (connection->state >= HFP_W2_DISCONNECT_SCO) return; connection->establish_audio_connection = 1; if (!has_codec_negotiation_feature(connection)){ log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); connection->codecs_state = HFP_CODECS_EXCHANGED; } switch (connection->codecs_state){ case HFP_CODECS_IDLE: case HFP_CODECS_RECEIVED_LIST: case HFP_CODECS_AG_RESEND_COMMON_CODEC: case HFP_CODECS_ERROR: connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; break; default: break; } hfp_run_for_context(connection); }
static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ hfp_connection_t * context = get_hfp_connection_context_for_bd_addr(bd_addr); if (context) return context; context = create_hfp_connection_context(); printf("created context for address %s\n", bd_addr_to_str(bd_addr)); memcpy(context->remote_addr, bd_addr, 6); return context; }
void hfp_ag_transfer_callsetup_status(bd_addr_t bd_addr, hfp_callsetup_status_t status){ hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (!connection){ log_error("HFP HF: connection doesn't exist."); return; } if (!connection->enable_status_update_for_ag_indicators) return; hfp_ag_update_indicator_status(connection, (char *)"callsetup", status); }
void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){ hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (!connection){ log_error("HFP HF: connection doesn't exist."); return; } connection->extended_audio_gateway_error = 0; if (!connection->enable_extended_audio_gateway_error_report){ return; } connection->extended_audio_gateway_error = error; hfp_run_for_context(connection); }
void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){ hfp_ag_establish_service_level_connection(bd_addr); hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (!has_codec_negotiation_feature(connection)) return; connection->establish_audio_connection = 0; if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; if (connection->state >= HFP_W2_DISCONNECT_SCO) return; connection->establish_audio_connection = 1; if (connection->state < HFP_SLE_W4_EXCHANGE_COMMON_CODEC){ connection->ag_trigger_codec_connection_setup = 1; } hfp_run_for_context(connection); }
void hfp_ag_negotiate_codecs(bd_addr_t bd_addr){ hfp_ag_establish_service_level_connection(bd_addr); hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); if (!has_codec_negotiation_feature(connection)) return; if (connection->remote_codecs_nr == 0) return; if (connection->state >= HFP_W2_DISCONNECT_SCO) return; if (connection->state != HFP_SLE_W2_EXCHANGE_COMMON_CODEC && connection->state != HFP_SLE_W4_EXCHANGE_COMMON_CODEC){ connection->ag_trigger_codec_connection_setup = 1; } hfp_run_for_context(connection); }
void hfp_ag_release_audio_connection(bd_addr_t bd_addr){ hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); hfp_release_audio_connection(connection); hfp_run_for_context(connection); }
void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){ printf(" hfp_ag_release_service_level_connection \n"); hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); hfp_release_service_level_connection(connection); hfp_run_for_context(connection); }
void hfp_handle_hci_event(hfp_callback_t callback, uint8_t packet_type, uint8_t *packet, uint16_t size){ bd_addr_t event_addr; uint16_t rfcomm_cid, handle; hfp_connection_t * context = NULL; switch (packet[0]) { case BTSTACK_EVENT_STATE: // bt stack activated, get started if (packet[2] == HCI_STATE_WORKING){ printf("BTstack activated, get started .\n"); } break; case HCI_EVENT_PIN_CODE_REQUEST: // inform about pin code request printf("Pin code request - using '0000'\n\r"); bt_flip_addr(event_addr, &packet[2]); hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); break; case RFCOMM_EVENT_INCOMING_CONNECTION: // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) bt_flip_addr(event_addr, &packet[2]); context = get_hfp_connection_context_for_bd_addr(event_addr); if (!context || context->state != HFP_IDLE) return; context->rfcomm_cid = READ_BT_16(packet, 9); context->state = HFP_W4_RFCOMM_CONNECTED; printf("RFCOMM channel %u requested for %s\n", context->rfcomm_cid, bd_addr_to_str(context->remote_addr)); rfcomm_accept_connection_internal(context->rfcomm_cid); break; case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); bt_flip_addr(event_addr, &packet[3]); context = get_hfp_connection_context_for_bd_addr(event_addr); if (!context || context->state != HFP_W4_RFCOMM_CONNECTED) return; if (packet[2]) { hfp_emit_event(callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]); remove_hfp_connection_context(context); } else { context->con_handle = READ_BT_16(packet, 9); context->rfcomm_cid = READ_BT_16(packet, 12); uint16_t mtu = READ_BT_16(packet, 14); printf("RFCOMM channel open succeeded. Context %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", context, context->rfcomm_cid, mtu); switch (context->state){ case HFP_W4_RFCOMM_CONNECTED: context->state = HFP_EXCHANGE_SUPPORTED_FEATURES; break; case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: context->state = HFP_W2_DISCONNECT_RFCOMM; printf("Shutting down RFCOMM.\n"); break; default: break; } } break; case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ int index = 2; uint8_t status = packet[index++]; uint16_t sco_handle = READ_BT_16(packet, index); index+=2; bd_addr_t address; memcpy(address, &packet[index], 6); index+=6; uint8_t link_type = packet[index++]; uint8_t transmission_interval = packet[index++]; // measured in slots uint8_t retransmission_interval = packet[index++];// measured in slots uint16_t rx_packet_length = READ_BT_16(packet, index); // measured in bytes index+=2; uint16_t tx_packet_length = READ_BT_16(packet, index); // measured in bytes index+=2; uint8_t air_mode = packet[index]; if (status != 0){ log_error("(e)SCO Connection is not established, status %u", status); break; } switch (link_type){ case 0x00: printf("SCO Connection established. \n"); if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); break; case 0x02: printf("eSCO Connection established. \n"); break; default: log_error("(e)SCO reserved link_type 0x%2x", link_type); break; } log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); context = get_hfp_connection_context_for_bd_addr(address); if (context->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ context->state = HFP_W2_DISCONNECT_SCO; break; } context->sco_handle = sco_handle; context->state = HFP_AUDIO_CONNECTION_ESTABLISHED; hfp_emit_event(callback, HFP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]); break; } case RFCOMM_EVENT_CHANNEL_CLOSED: rfcomm_cid = READ_BT_16(packet,2); context = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); if (!context) break; if (context->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ context->state = HFP_IDLE; hfp_establish_service_level_connection(context->remote_addr, context->service_uuid); break; } remove_hfp_connection_context(context); hfp_emit_event(callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); break; case HCI_EVENT_DISCONNECTION_COMPLETE: handle = READ_BT_16(packet,3); context = get_hfp_connection_context_for_handle(handle); if (!context) break; if (context->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ context->state = HFP_IDLE; hfp_establish_service_level_connection(context->remote_addr, context->service_uuid); break; } hfp_emit_event(callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, packet[2]); remove_hfp_connection_context(context); break; default: break; } }