From a87db99d2bfc0ede64aa6edb223f8f1838037416 Mon Sep 17 00:00:00 2001 From: Milanka Ringwald Date: Wed, 18 Nov 2015 19:39:36 +0100 Subject: [PATCH] hfp hf cleanup --- src/hfp.c | 155 ++++++++++++++++++++++++++------------------------- src/hfp_hf.c | 58 ++++++++----------- 2 files changed, 100 insertions(+), 113 deletions(-) diff --git a/src/hfp.c b/src/hfp.c index 7ce62fa25..ffe3a450b 100644 --- a/src/hfp.c +++ b/src/hfp.c @@ -606,138 +606,139 @@ void hfp_handle_hci_event(hfp_callback_t callback, uint8_t packet_type, uint8_t } // translates command string into hfp_command_t CMD and flags to distinguish between CMD=, CMD?, CMD=? -static void process_command(hfp_connection_t * context, int isHandsFree){ - if (context->line_size < 2) return; - +static hfp_command_t process_command(hfp_connection_t * context, int isHandsFree){ int offset = isHandsFree ? 0 : 2; + hfp_command_t command = context->command; + + char * line_buffer = (char *)context->line_buffer; - if (strncmp((char *)context->line_buffer+offset, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){ - context->command = HFP_CMD_CALL_ANSWERED; - return; + if (strncmp(line_buffer+offset, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){ + command = HFP_CMD_CALL_ANSWERED; + return command; } - if (strncmp((char *)context->line_buffer, "AT", 2) == 0){ + if (strncmp(line_buffer, "AT", 2) == 0){ offset = 2; isHandsFree = 0; } - if (strncmp((char *)context->line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ - context->command = HFP_CMD_ERROR; - return; + if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ + command = HFP_CMD_ERROR; + return command; } - if (isHandsFree && strncmp((char *)context->line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ + if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ //printf("parsed HFP_CMD_OK \n"); - context->command = HFP_CMD_OK; - return; + command = HFP_CMD_OK; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ - context->command = HFP_CMD_SUPPORTED_FEATURES; - return; + if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ + command = HFP_CMD_SUPPORTED_FEATURES; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ - if (strncmp((char *)context->line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ - context->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; + if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ + if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ + command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; } - if (strncmp((char *)context->line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ - context->command = HFP_CMD_RETRIEVE_AG_INDICATORS; + if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ + command = HFP_CMD_RETRIEVE_AG_INDICATORS; } - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ - context->command = HFP_CMD_AVAILABLE_CODECS; - return; + if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ + command = HFP_CMD_AVAILABLE_CODECS; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ - context->command = HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; - return; + if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ + command = HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ - context->command = HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; - return; + if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ + command = HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ - if (isHandsFree) return; + if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ + if (isHandsFree) return command; - if (strncmp((char *)context->line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ - context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; - } else if (strncmp((char *)context->line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ - context->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; + if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ + command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; + } else if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ + command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; } else { - context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; + command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; } - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ - context->command = HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; - return; + if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ + command = HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ - context->command = HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; - - if (isHandsFree) return; + if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ + command = HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; + if (isHandsFree) return command; - if (strncmp((char *)context->line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ - context->command = HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; + if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ + command = HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; } - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ - context->command = HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; - return; + if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ + command = HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; + return command; } - if (isHandsFree && strncmp((char *)context->line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ - context->command = HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; - return; + if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ + command = HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; + return command; } - if (!isHandsFree && strncmp((char *)context->line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ - context->command = HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; - return; + if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ + command = HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ - context->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; - return; + if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ + command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; + return command; } - if (strncmp((char *)context->line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ - if (!isHandsFree){ - context->command = HFP_CMD_HF_CONFIRMED_CODEC; + if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ + if (isHandsFree){ + command = HFP_CMD_AG_SUGGESTED_CODEC; } else { - context->command = HFP_CMD_AG_SUGGESTED_CODEC; + command = HFP_CMD_HF_CONFIRMED_CODEC; } - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, "AT+", 3) == 0){ - context->command = HFP_CMD_UNKNOWN; + if (strncmp(line_buffer+offset, "AT+", 3) == 0){ + command = HFP_CMD_UNKNOWN; printf(" process unknown HF command %s \n", context->line_buffer); - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, "+", 1) == 0){ - context->command = HFP_CMD_UNKNOWN; + if (strncmp(line_buffer+offset, "+", 1) == 0){ + command = HFP_CMD_UNKNOWN; printf(" process unknown AG command %s \n", context->line_buffer); - return; + return command; } - if (strncmp((char *)context->line_buffer+offset, "NOP", 3) == 0){ - context->command = HFP_CMD_NONE; - return; + if (strncmp(line_buffer+offset, "NOP", 3) == 0){ + command = HFP_CMD_NONE; + return command; } - context->command = HFP_CMD_NONE; + command = HFP_CMD_NONE; + return command; } #if 0 @@ -851,7 +852,7 @@ void hfp_parse(hfp_connection_t * context, uint8_t byte, int isHandsFree){ // printf(" parse header 2 %s, keep separator $ %d\n", context->line_buffer, context->keep_separator); if (hfp_parser_is_end_of_header(byte) || context->keep_separator == 1){ // printf(" parse header 3 %s, keep separator $ %d\n", context->line_buffer, context->keep_separator); - process_command(context, isHandsFree); + context->command = process_command(context, isHandsFree); } break; diff --git a/src/hfp_hf.c b/src/hfp_hf.c index 01ebaefb9..6879f0994 100644 --- a/src/hfp_hf.c +++ b/src/hfp_hf.c @@ -253,19 +253,17 @@ static void hfp_emit_network_operator_event(hfp_callback_t callback, int status, static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * context){ if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; - int done = 0; - if (context->ok_pending) return done; - + if (context->ok_pending) return 0; + int done = 1; + switch (context->state){ case HFP_EXCHANGE_SUPPORTED_FEATURES: - hfp_hf_cmd_exchange_supported_features(context->rfcomm_cid); - done = 1; context->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; + hfp_hf_cmd_exchange_supported_features(context->rfcomm_cid); break; case HFP_NOTIFY_ON_CODECS: - hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); - done = 1; context->state = HFP_W4_NOTIFY_ON_CODECS; + hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); break; case HFP_RETRIEVE_INDICATORS: hfp_hf_cmd_retrieve_indicators(context->rfcomm_cid); @@ -274,40 +272,35 @@ static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * co context->command = HFP_CMD_RETRIEVE_AG_INDICATORS; break; case HFP_RETRIEVE_INDICATORS_STATUS: - hfp_hf_cmd_retrieve_indicators_status(context->rfcomm_cid); - done = 1; context->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; context->command = HFP_CMD_RETRIEVE_AG_INDICATORS; + hfp_hf_cmd_retrieve_indicators_status(context->rfcomm_cid); break; case HFP_ENABLE_INDICATORS_STATUS_UPDATE: - hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, 1); - done = 1; context->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; + hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, 1); break; case HFP_RETRIEVE_CAN_HOLD_CALL: - hfp_hf_cmd_retrieve_can_hold_call(context->rfcomm_cid); - done = 1; context->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; + hfp_hf_cmd_retrieve_can_hold_call(context->rfcomm_cid); break; case HFP_LIST_GENERIC_STATUS_INDICATORS: - hfp_hf_cmd_list_supported_generic_status_indicators(context->rfcomm_cid); - done = 1; context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; context->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; + hfp_hf_cmd_list_supported_generic_status_indicators(context->rfcomm_cid); break; case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: - hfp_hf_cmd_retrieve_supported_generic_status_indicators(context->rfcomm_cid); - done = 1; context->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; + hfp_hf_cmd_retrieve_supported_generic_status_indicators(context->rfcomm_cid); break; case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: - hfp_hf_cmd_list_initital_supported_generic_status_indicators(context->rfcomm_cid); - done = 1; context->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; + hfp_hf_cmd_list_initital_supported_generic_status_indicators(context->rfcomm_cid); break; default: + done = 0; break; } return done; @@ -381,37 +374,37 @@ static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connectio int done = 0; if (context->enable_status_update_for_ag_indicators != 0xFF){ - hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators); 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); - context->ok_pending = 1; - done = 1; return done; } if (context->command == HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT){ - hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid); 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){ - hfp_hf_cmd_query_operator_name(context->rfcomm_cid); 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){ - hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, 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; } @@ -450,7 +443,7 @@ static void hfp_hf_handle_ok_service_level_connection_queries(hfp_connection_t * static int codecs_exchange_state_machine(hfp_connection_t * context){ if (context->ok_pending) return 0; - int done = 0; + int done = 1; switch(context->command){ case HFP_CMD_AVAILABLE_CODECS: @@ -467,33 +460,26 @@ static int codecs_exchange_state_machine(hfp_connection_t * context){ default: break; } - hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); - done = 1; - 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); - done = 1; 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); - done = 1; } else { context->codec_confirmed = 0; context->suggested_codec = 0; context->negotiated_codec = 0; hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); - done = 1; } break; - default: - break; + return 0; } if (done){ @@ -563,8 +549,8 @@ static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8 // emit indicators status changed for (i = 0; i < context->ag_indicators_nr; i++){ if (context->ag_indicators[i].status_changed) { - hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]); context->ag_indicators[i].status_changed = 0; + hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]); break; } } @@ -577,8 +563,8 @@ static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8 } if (context->command == HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR){ context->ok_pending = 0; - hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error); context->extended_audio_gateway_error = 0; + hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error); return; }