I'm trying to run HFP_ag_Demo using an STM32F091RC and a CC2564. I managed to do the first changes to make the code compile on Eclipse but I still don't know how to personalize the Demo to connect my CC2564 to a bluetooth earpiece. I admit that i'm a bit lost:
- Where in the code i can enter the address of the device i want to be connected to ?
- I don't see in the code where a quiry is done to look for the available devices and try to connect to one of them.
- Should I change something in the code of "HFP_ag_Demo" or just run it as it is.
- I triyed to do a quiry just after the event "BTSTACK_EVENT_STATE" and connect to a specific device (my earpiece) after the inquiry result , this is how my code looks like (I put in blue color what i added to the standard code):
void port_main(void){
// init memory pools
btstack_memory_init();
// default run loop for embedded systems - classic while loop
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
// enable packet logging, at least while porting
hci_dump_open( NULL, HCI_DUMP_STDOUT );
// init HCI
hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
hci_set_chipset(btstack_chipset_cc256x_instance());
// inform about BTstack state
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
// hand over to BTstack example code
btstack_main(0, NULL);
// go
btstack_run_loop_execute();
}
* In HFP_ag_Demo :
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
UNUSED(channel);
bd_addr_t addr;
uint8_t status;
switch (packet_type){
case HCI_EVENT_PACKET:
switch(hci_event_packet_get_type(event)){
#ifndef HAVE_BTSTACK_STDIN
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
hfp_ag_establish_service_level_connection(device_addr);
break;
#endif
case GAP_EVENT_INQUIRY_RESULT:
gap_event_inquiry_result_get_bd_addr(event, addr);
// print info
printf("Device found: %s ", bd_addr_to_str(addr));
printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
if (gap_event_inquiry_result_get_rssi_available(event)){
printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
}
if (gap_event_inquiry_result_get_name_available(event)){
char name_buffer[240];
int name_len = gap_event_inquiry_result_get_name_len(event);
memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
name_buffer[name_len] = 0;
printf(", name '%s'", name_buffer);
}
printf("\n");
if (strcmp(bd_addr_to_str(addr),"00:22:A6:33:AE:D3") == 0)
{
printf("\r\nDevice Achref Found\r\n");
for (int i = 0; i< BD_ADDR_LEN; i++)
{
address_Ach[i] = addr[i];
}
}
break;
case GAP_EVENT_INQUIRY_COMPLETE:
printf("Inquiry scan complete.\n");
if (address_Ach[1] == 34)
{
gap_connectable_control(1);
hsp_ag_connect(address_Ach);
}
break;
case HCI_EVENT_SCO_CAN_SEND_NOW:
sco_demo_send(sco_handle);
break;
case HCI_EVENT_COMMAND_COMPLETE:
if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
dump_supported_codecs();
}
break;
default:
break;
}
if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
if (event[3]
&& hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER
&& hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG
&& hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_TRANSMIT_DTMF_CODES){
printf("ERROR, status: %u\n", event[3]);
return;
}
switch (hci_event_hfp_meta_get_subevent_code(event)) {
case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
status = hfp_subevent_service_level_connection_established_get_status(event);
if (status){
printf("Connection failed, staus 0x%02x\n", status);
break;
}
acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
dump_supported_codecs();
#ifndef HAVE_BTSTACK_STDIN
log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
hfp_ag_establish_audio_connection(acl_handle);
#endif
break;
case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
printf("Service level connection released.\n");
sco_handle = 0;
break;
case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
if (hfp_subevent_audio_connection_established_get_status(event)){
printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
sco_handle = 0;
} else {
sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
switch (negotiated_codec){
case 0x01:
printf("Using CVSD codec.\n");
break;
case 0x02:
printf("Using mSBC codec.\n");
break;
default:
printf("Using unknown codec 0x%02x.\n", negotiated_codec);
break;
}
sco_demo_set_codec(negotiated_codec);
hci_request_sco_can_send_now_event();
}
break;
case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
printf("Audio connection released\n");
sco_handle = 0;
sco_demo_close();
break;
case HFP_SUBEVENT_START_RINGINIG:
printf("Start Ringing\n");
break;
case HFP_SUBEVENT_STOP_RINGINIG:
printf("Stop Ringing\n");
break;
case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
// validate number
if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
|| strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
|| (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
printf("Dialstring valid: accept call\n");
hfp_ag_outgoing_call_accepted();
} else {
printf("Dialstring invalid: reject call\n");
hfp_ag_outgoing_call_rejected();
}
break;
case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
printf("Attach number to voice tag. Sending '1234567\n");
hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
break;
case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
hfp_ag_send_dtmf_code_done(acl_handle);
break;
case HFP_SUBEVENT_CALL_ANSWERED:
printf("Call answered by HF\n");
break;
default:
printf("Event not handled %u\n", hci_event_hfp_meta_get_subevent_code(event));
break;
}
break;
case HCI_SCO_DATA_PACKET:
sco_demo_receive(event, event_size);
break;
default:
break;
}
}
int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
(void)argc;
(void)argv;
sco_demo_init();
// register for HCI events
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
hci_register_sco_packet_handler(&packet_handler);
gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
gap_discoverable_control(1);
// L2CAP
l2cap_init();
uint16_t supported_features =
(1<<HFP_AGSF_ESCO_S4) |
(1<<HFP_AGSF_HF_INDICATORS) |
(1<<HFP_AGSF_CODEC_NEGOTIATION) |
(1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
(1<<HFP_AGSF_ENHANCED_CALL_CONTROL) |
(1<<HFP_AGSF_ENHANCED_CALL_STATUS) |
(1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) |
(1<<HFP_AGSF_IN_BAND_RING_TONE) |
(1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) |
(1<<HFP_AGSF_THREE_WAY_CALLING);
int wide_band_speech = 1;
// HFP
rfcomm_init();
hfp_ag_init(rfcomm_channel_nr);
hfp_ag_init_supported_features(supported_features);
hfp_ag_init_codecs(sizeof(codecs), codecs);
hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
hfp_ag_register_packet_handler(&packet_handler);
hci_register_sco_packet_handler(&packet_handler);
// SDP Server
sdp_init();
memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
sdp_register_service(hfp_service_buffer);
// parse humand readable Bluetooth address
sscanf_bd_addr(device_addr_string, device_addr);
#ifdef HAVE_BTSTACK_STDIN
btstack_stdin_setup(stdin_process);
#endif
// turn on!
hci_power_control(HCI_POWER_ON);
return 0;
}
This is what i get when i run it (messages after i found the bluetooth earpiece and try to connect to it)
sp_ag.c.470: Start SDP query 00:22:A6:33:AE:D3, 0x1108
l2cap.c.1821: L2CAP_CREATE_CHANNEL addr 00:22:A6:33:AE:D3 psm 0x1 mtu 1017 -> local mtu 1017
hci.c.3563: Create_connection to 00:22:A6:33:AE:D3
hci.c.185: create_connection_for_addr 00:22:A6:33:AE:D3, type ff
hci.c.3575: conn state 0
Connection_complete (status=0) 00:22:A6:33:AE:D3
hci.c.2019: New connection: handle 1, 00:22:A6:33:AE:D3
hci.c.3922: BTSTACK_EVENT_NR_CONNECTIONS_CHANGED 1
CI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags 2, eSCO 1
l2cap.c.1757: l2cap received remote supported features, sec_level_0_allowed for psm 1 = 1
hci_send_acl_packet_fragments entered
hci.c.627: hci_send_acl_packet_fragments loop entered
hci.c.652: hci_send_acl_packet_fragments loop before send (more fragments 0)
.
.
.
ACL classic buffers: 1 used of 4
.
.
.
LOG -- l2cap.c.2463: L2CAP signaling handler code 5, state 11
LOG -- l2cap.c.1053: l2cap_stop_rtx for local cid 0x40
LOG -- l2cap.c.2395: l2cap_signaling_handle_configure_response
LOG -- hci.c.448: ACL classic buffers: 0 used of 4
LOG -- l2cap.c.2463: L2CAP signaling handler code 4, state 11
LOG -- l2cap.c.2315: Remote MTU 48
.
.
.
HCI_EVENT_LINK_KEY_REQUEST
.
.
.
2CAP_EVENT_CHANNEL_OPENED status 0x0 addr 00:22:A6:33:AE:D3 handle 0x1 psm 0x1 local_cid 0x40 remote_cid 0x40 local_mtu 1017, remote_mtu 48, flush_timeout 0
.
.
.
(Messages indicating that FRCOMM and L2CAP channels are closed and DSP ...
Can you please tell me the steps to make this demo run correctly and how to connect it to a specific bluetooth earpiece. Thank you in advance.
Best regards,
Achref,