hfp: hf rewriten codecs connection sm

This commit is contained in:
Milanka Ringwald 2015-11-26 11:29:51 +01:00
parent 42dbbb66b4
commit 63ffda6041
3 changed files with 187 additions and 146 deletions

View File

@ -330,6 +330,7 @@ typedef enum {
HFP_CODECS_W4_AG_COMMON_CODEC,
HFP_CODECS_AG_SENT_COMMON_CODEC,
HFP_CODECS_AG_RESEND_COMMON_CODEC,
HFP_CODECS_HF_CONFIRMED_CODEC,
HFP_CODECS_EXCHANGED,
HFP_CODECS_ERROR
} hfp_codecs_state_t;

View File

@ -300,8 +300,132 @@ static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * co
return done;
}
static void hfp_hf_handle_ok_service_level_connection_establishment(hfp_connection_t *context){
if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return;
static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * context){
if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
if (context->ok_pending) return 0;
int done = 0;
if (context->enable_status_update_for_ag_indicators != 0xFF){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators);
return done;
};
if (context->change_status_update_for_individual_ag_indicators){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_activate_status_update_for_ag_indicator(context->rfcomm_cid,
context->ag_indicators_status_update_bitmap,
context->ag_indicators_nr);
return done;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid);
return done;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_query_operator_name(context->rfcomm_cid);
return done;
}
if (context->enable_extended_audio_gateway_error_report){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, context->enable_extended_audio_gateway_error_report);
return done;
}
return done;
}
static int codecs_exchange_state_machine(hfp_connection_t * context){
/* events ( == commands):
HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
hf_trigger_codec_connection_setup == received BCC
ag_trigger_codec_connection_setup == received from AG to send BCS
HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
*/
if (context->ok_pending) return 0;
int done = 1;
printf(" -> State machine: CC\n");
switch (context->command){
case HFP_CMD_AVAILABLE_CODECS:
if (context->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
context->ok_pending = 1;
hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
return 1;
case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
context->codec_confirmed = 0;
context->suggested_codec = 0;
context->negotiated_codec = 0;
context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
context->ok_pending = 1;
hfp_hf_cmd_trigger_codec_connection_setup(context->rfcomm_cid);
break;
case HFP_CMD_AG_SUGGESTED_CODEC:
if (hfp_hf_supports_codec(context->suggested_codec)){
context->codec_confirmed = context->suggested_codec;
context->ok_pending = 1;
context->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
hfp_hf_cmd_confirm_codec(context->rfcomm_cid, context->suggested_codec);
} else {
context->codec_confirmed = 0;
context->suggested_codec = 0;
context->negotiated_codec = 0;
context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
context->ok_pending = 1;
hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
}
break;
default:
break;
}
return 0;
}
static void hfp_run_for_context(hfp_connection_t * context){
if (!context) return;
if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return;
int done = hfp_hf_run_for_context_service_level_connection(context);
if (!done){
done = hfp_hf_run_for_context_service_level_connection_queries(context);
}
if (!done){
done = codecs_exchange_state_machine(context);
}
if (done) return;
// deal with disconnect
switch (context->state){
case HFP_W2_DISCONNECT_RFCOMM:
context->state = HFP_W4_RFCOMM_DISCONNECTED;
rfcomm_disconnect_internal(context->rfcomm_cid);
break;
default:
break;
}
}
static void hfp_hf_switch_on_ok(hfp_connection_t *context){
context->ok_pending = 0;
int done = 1;
switch (context->state){
case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
if (has_codec_negotiation_feature(context)){
@ -357,174 +481,52 @@ static void hfp_hf_handle_ok_service_level_connection_establishment(hfp_connecti
context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
break;
default:
break;
}
}
static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * context){
if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
if (context->ok_pending) return 0;
int done = 0;
if (context->enable_status_update_for_ag_indicators != 0xFF){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators);
return done;
};
if (context->change_status_update_for_individual_ag_indicators){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_activate_status_update_for_ag_indicator(context->rfcomm_cid,
context->ag_indicators_status_update_bitmap,
context->ag_indicators_nr);
return done;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid);
return done;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_query_operator_name(context->rfcomm_cid);
return done;
}
if (context->enable_extended_audio_gateway_error_report){
context->ok_pending = 1;
done = 1;
hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, context->enable_extended_audio_gateway_error_report);
return done;
}
return done;
}
static void hfp_hf_handle_ok_service_level_connection_queries(hfp_connection_t * context){
if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return;
case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
if (context->enable_status_update_for_ag_indicators != 0xFF){
context->enable_status_update_for_ag_indicators = 0xFF;
hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
return;
};
break;
}
if (context->change_status_update_for_individual_ag_indicators == 1){
context->change_status_update_for_individual_ag_indicators = 0;
hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
return;
break;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT){
context->command = HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
return;
break;
}
if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME){
hfp_emit_network_operator_event(hfp_callback, 0, context->network_operator);
return;
break;
}
if (context->enable_extended_audio_gateway_error_report){
context->enable_extended_audio_gateway_error_report = 0;
return;
}
break;
}
printf(" CC received ok\n");
static int codecs_exchange_state_machine(hfp_connection_t * context){
if (context->ok_pending) return 0;
int done = 1;
switch(context->command){
case HFP_CMD_AVAILABLE_CODECS:
switch (context->codecs_state){
case HFP_CODECS_W4_AG_COMMON_CODEC:
context->codec_confirmed = 0;
context->suggested_codec = 0;
context->negotiated_codec = 0;
break;
case HFP_CODECS_EXCHANGED:
context->negotiated_codec = 0;
context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
break;
default:
break;
}
hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
break;
case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
hfp_hf_cmd_trigger_codec_connection_setup(context->rfcomm_cid);
break;
case HFP_CMD_AG_SUGGESTED_CODEC:
if (hfp_hf_supports_codec(context->suggested_codec)){
context->codec_confirmed = context->suggested_codec;
hfp_hf_cmd_confirm_codec(context->rfcomm_cid, context->suggested_codec);
} else {
context->codec_confirmed = 0;
context->suggested_codec = 0;
context->negotiated_codec = 0;
hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
}
break;
default:
return 0;
}
if (done){
context->ok_pending = 1;
}
return done;
}
static void hfp_hf_handle_ok_codecs_connection(hfp_connection_t * context){
// handle audio connection setup
switch (context->codecs_state){
case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
break;
case HFP_CODECS_HF_CONFIRMED_CODEC:
context->codecs_state = HFP_CODECS_EXCHANGED;
hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
break;
default:
done = 0;
break;
}
}
static void hfp_run_for_context(hfp_connection_t * context){
if (!context) return;
if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return;
int done = hfp_hf_run_for_context_service_level_connection(context);
if (!done){
done = hfp_hf_run_for_context_service_level_connection_queries(context);
}
if (!done){
done = codecs_exchange_state_machine(context);
}
if (done) return;
// deal with disconnect
switch (context->state){
case HFP_W2_DISCONNECT_RFCOMM:
context->state = HFP_W4_RFCOMM_DISCONNECTED;
rfcomm_disconnect_internal(context->rfcomm_cid);
break;
default:
done = 0;
break;
}
}
static void hfp_hf_switch_on_ok(hfp_connection_t *context){
// printf("switch on ok\n");
context->ok_pending = 0;
hfp_hf_handle_ok_service_level_connection_establishment(context);
hfp_hf_handle_ok_service_level_connection_queries(context);
hfp_hf_handle_ok_codecs_connection(context);
// done
context->command = HFP_CMD_NONE;
}

View File

@ -71,10 +71,18 @@ const uint8_t rfcomm_channel_nr = 1;
static bd_addr_t device_addr = {0xD8,0xBb,0x2C,0xDf,0xF1,0x08};
static uint8_t codecs[2] = {1,2};
static uint8_t default_codecs[2] = {1};
static uint16_t indicators[1] = {0x01};
static uint8_t service_level_connection_established = 0;
static uint8_t codecs_connection_established = 0;
static uint8_t audio_connection_established = 0;
static uint8_t start_ringing = 0;
static uint8_t stop_ringing = 0;
static uint8_t call_termiated = 0;
static int supported_features_with_codec_negotiation = 438; // 0001 1011 0110
static int supported_features_without_codec_negotiation = 310; // 0001 0011 0110
int expected_rfcomm_command(const char * cmd){
char * ag_cmd = (char *)get_rfcomm_payload();
@ -143,28 +151,37 @@ void packet_handler(uint8_t * event, uint16_t event_size){
case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
printf("\n** SLC established **\n\n");
service_level_connection_established = 1;
codecs_connection_established = 0;
audio_connection_established = 0;
break;
case HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE:
printf("\n** CC established **\n\n");
codecs_connection_established = 1;
audio_connection_established = 0;
break;
case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
printf("\n** SLC released **\n\n");
service_level_connection_established = 0;
break;
case HFP_SUBEVENT_COMPLETE:
printf("HFP_SUBEVENT_COMPLETE.\n\n");
case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
printf("\n** AC established **\n\n");
audio_connection_established = 1;
break;
case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
printf("AG_INDICATOR_STATUS_CHANGED, AG indicator index: %d, status: %d\n", event[4], event[5]);
case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
printf("\n** AC released **\n\n");
audio_connection_established = 0;
break;
case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", event[4], event[5], (char *) &event[6]);
case HFP_SUBEVENT_START_RINGINIG:
printf("\n** Start ringing **\n\n");
start_ringing = 1;
break;
case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
if (event[4])
printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n", event[3]);
case HFP_SUBEVENT_STOP_RINGINIG:
printf("\n** Stop ringing **\n\n");
stop_ringing = 1;
start_ringing = 0;
break;
case HFP_SUBEVENT_CALL_TERMINATED:
call_termiated = 1;
break;
default:
printf("event not handled %u\n", event[2]);
@ -179,14 +196,22 @@ TEST_GROUP(HFPClient){
void setup(void){
service_level_connection_established = 0;
codecs_connection_established = 0;
audio_connection_established = 0;
start_ringing = 0;
stop_ringing = 0;
call_termiated = 0;
hfp_hf_init(rfcomm_channel_nr, supported_features_with_codec_negotiation, indicators, sizeof(indicators)/sizeof(uint16_t), 1);
hfp_hf_set_codecs(codecs, sizeof(codecs));
}
void teardown(void){
if (service_level_connection_established){
hfp_hf_release_audio_connection(device_addr);
hfp_hf_release_service_level_connection(device_addr);
CHECK_EQUAL(service_level_connection_established, 0);
}
service_level_connection_established = 0;
codecs_connection_established = 0;
audio_connection_established = 0;
}
void setup_hfp_service_level_connection(char ** test_steps, int nr_test_steps){
@ -202,6 +227,22 @@ TEST_GROUP(HFPClient){
};
TEST(HFPClient, HFAudioConnectionEstablishedWithoutCodecNegotiation){
hfp_hf_init(rfcomm_channel_nr, supported_features_without_codec_negotiation, indicators, sizeof(indicators)/sizeof(uint16_t), 1);
hfp_hf_set_codecs(default_codecs, 1);
setup_hfp_service_level_connection(hfp_slc_tests()[1].test, hfp_slc_tests()[1].len);
CHECK_EQUAL(service_level_connection_established, 1);
setup_hfp_codecs_connection(default_cc_setup(), default_cc_setup_size());
CHECK_EQUAL(codecs_connection_established, 1);
// hfp_hf_establish_audio_connection(device_addr);
// CHECK_EQUAL(audio_connection_established, 1);
// hfp_hf_release_audio_connection(device_addr);
// CHECK_EQUAL(audio_connection_established, 0);
}
TEST(HFPClient, HFCodecsConnectionEstablished){
for (int i = 0; i < cc_tests_size(); i++){
@ -231,9 +272,6 @@ TEST(HFPClient, HFServiceLevelConnectionEstablished){
int main (int argc, const char * argv[]){
hfp_hf_init(rfcomm_channel_nr, 438, indicators, sizeof(indicators)/sizeof(uint16_t), 1);
hfp_hf_set_codecs(codecs, sizeof(codecs));
hfp_hf_register_packet_handler(packet_handler);
return CommandLineTestRunner::RunAllTests(argc, argv);
}