/*
	Start WBS negotiation if appropriate.
*/
void AghfpStartWbsCodecNegotiation(AGHFP *aghfp)
{
	if(aghfp->state >= aghfp_slc_connected)
	{
		aghfpWbsStartCodecNegotiation(aghfp, aghfp_negotiate_no_audio);
	}
}
void aghfpHandleCodecNegotiationReq(AGHFP *aghfp, AGHFP_INTERNAL_CODEC_NEGOTIATION_REQ_T *codecs)
{
	uint16 counter_1;

	aghfp->hf_codecs = 0;

	/* Convert from a list of UUID16s to a codec mask. */
	for(counter_1 = 0; counter_1 < codecs->num_codecs; counter_1++)
	{
		aghfp->hf_codecs |= WbsUuid16ToCodec((codecs_info*)(&aghfp->codecs_info), codecs->codec_uuids[counter_1]);
	}

	/* Check audio (and AGHFP state?)
	   Restart codec negotiation if in the middle of WBS connect. */
	switch(aghfp->audio_connection_state)
	{
		case(aghfp_audio_disconnected):
			aghfpSendOk(aghfp);
			break;
		case(aghfp_audio_codec_connect):
			/* Start codec negotiation with HF. */
			if (aghfpWbsStartCodecNegotiation(aghfp, aghfp_negotiate_audio_at_ag))
			{
				aghfp->audio_connection_state = aghfp_audio_codec_connect;
			} /* Ignore error case for now. */
			break;
		case(aghfp_audio_connecting_esco):
		case(aghfp_audio_connecting_sco):
		case(aghfp_audio_accepting):
		case(aghfp_audio_disconnecting):
		case(aghfp_audio_connected):
			break;
	}
}
/*
	Handle Codec Connection request from the HF (AT+BCC).
	This function performs the the actual actions of handling the AT+BCC (once the audio params have been
	obtained from the app, i.e. after aghfpHandleWbsCodecConReq() and ).
*/
static void aghfpHandleWbsCodecConReqProcessing(AGHFP *aghfp)
{
	/* Are we supporting WBS */
	if (aghfp->use_wbs)
	{
		/* OK to initiate WBS Codec Negotiation. */
	
		/* Send OK */
		aghfpSendOk(aghfp);
	
		/* If we haven't negotiatied a codec we need to do so.
		   If we have, we're gonna go ahead and use it here. */
		if (aghfp->use_codec == 0)
		{
			/* Start codec negotiation with HF. */
			if (aghfpWbsStartCodecNegotiation(aghfp, aghfp_negotiate_audio_at_hf))
			{
				aghfp->audio_connection_state = aghfp_audio_codec_connect;
			}
		}
		else
		{
			if (!aghfpCallManagerActiveNotComplete(aghfp))
			{
				MAKE_AGHFP_MESSAGE(AGHFP_INTERNAL_AUDIO_CONNECT_REQ);
				AGHFP_DEBUG(("	  Normal Audio Connection\n"));
				
				message->audio_params = aghfp->audio_params;
				message->packet_type = aghfp->audio_packet_type;
				MessageSend(&aghfp->task, AGHFP_INTERNAL_AUDIO_CONNECT_REQ, message);
			}
			else
			{
				/* Save the audio packet type for Call Manager */
				aghfpStoreAudioParams(aghfp, aghfp->audio_packet_type, &aghfp->audio_params);
				
				/* Answer the call again now that the WBS negotiation is complete. */
				aghfpManageCall(aghfp, CallEventAnswer, CallFlagOpenAudio);
			}
		}
	}
	else
	{
		/* WBS not supported, send ERROR. */
		aghfpSendError(aghfp);
	}
}
/* Attempt to create an audio (Synchronous) connection.  Due to firmware operation, eSCO and SCO must be requested
   separately. */
static void startAudioConnectRequest(AGHFP *aghfp)
{
    sync_config_params config_params;
    
    AGHFP_DEBUG(("startAudioConnectRequest\n"));
    AGHFP_DEBUG(("aghfp->hf_supported_features %d\n",aghfp->hf_supported_features));    
    AGHFP_DEBUG(("aghfp_hf_codec_negotiation %d\n",aghfp_hf_codec_negotiation));    

	/* Start Codec Negotiaion if:
			Audio id disconnected AND
			WBS is not being overridden AND
			We wish to use WBS AND
			The AG indicates that it supports Codec Negotiation AND
			No codec has already previously been successfully negotiated
	*/
    if ((aghfp->audio_connection_state == aghfp_audio_disconnected) && (!(aghfp->audio_params.override_wbs)) && 
    							(aghfp->use_wbs) && (aghfp->hf_supported_features & aghfp_hf_codec_negotiation) &&
    							(aghfp->use_codec == 0) && (aghfp->negotiation_type == aghfp_codec_negotiation_type_wbs))
    {
        AGHFP_DEBUG(("aghfpWbsStartCodecNegotiation\n"));
        if (aghfpWbsStartCodecNegotiation(aghfp, aghfp_negotiate_audio_at_ag))
	    {
		    aghfp->audio_connection_state = aghfp_audio_codec_connect;
	    	return;
    	}
    }
    
    /* added to meet wbs test spec 
       if we currently use wbs but the HF does not support it then change the audio
       params so that a CVSD SCO can be negotiatiated */
    if((aghfp->use_wbs) && !(aghfp->hf_supported_features & aghfp_hf_codec_negotiation))
    {
        AGHFP_DEBUG(("using default CVSD Sco parameters \n"));
        aghfp->audio_packet_type = 0x2BF;
        aghfp->audio_params.bandwidth = 8000;
        aghfp->audio_params.max_latency = 16;
        aghfp->audio_params.voice_settings = 0;
    }
    
    
	if((aghfp->negotiation_type == aghfp_codec_negotiation_type_csr) && (aghfp->app_pending_codec_negotiation))
	{
		AGHFP_DEBUG(("aghfpSendCsrFeaturesNegotiationReqInd\n"));
        /* Prompt the app to negotiate the codec */
		aghfpSendCsrFeatureNegotiationReqInd(aghfp);
		aghfp->audio_connection_state = aghfp_audio_codec_connect;
		return;
	}

    AGHFP_DEBUG(("aghfp->audio_packet_type %d\n", aghfp->audio_packet_type));
    /* Save the packet type for use later if this connection fails. */
    aghfp->audio_packet_type_to_try = aghfp->audio_packet_type;

    if ( aghfp->audio_packet_type & sync_all_esco )
    {   /* Attempt to open an eSCO connection */
        AGHFP_DEBUG(("attempt to open an eSCO connection\n"));
        aghfp->audio_connection_state = aghfp_audio_connecting_esco;
        config_params.retx_effort = aghfp->audio_params.retx_effort;
    }
    else
    {   /* Attempt to open a SCO connection */
        AGHFP_DEBUG(("attempt to open a SCO connection\n"));
        aghfp->audio_connection_state = aghfp_audio_connecting_sco;
            /* No re-transmissions for SCO */
        config_params.retx_effort = sync_retx_disabled;
    }
            /* set packet type - pass through without checking anything*/
    config_params.packet_type = aghfp->audio_packet_type ;
    

    config_params.tx_bandwidth = aghfp->audio_params.bandwidth;
    config_params.rx_bandwidth = aghfp->audio_params.bandwidth;
    config_params.max_latency = aghfp->audio_params.max_latency;
    config_params.voice_settings = aghfp->audio_params.voice_settings;
    
    AGHFP_DEBUG(("config_params.packet_type %d\n", config_params.packet_type));
    AGHFP_DEBUG(("config_params.tx_bandwidth %d\n", (int)config_params.tx_bandwidth));
    AGHFP_DEBUG(("config_params.rx_bandwidth %d\n", (int)config_params.rx_bandwidth));
    AGHFP_DEBUG(("config_params.max_latency %d\n", config_params.max_latency));
    AGHFP_DEBUG(("config_params.voice_settings %d\n", config_params.voice_settings));

    
    /* Issue a Synchronous connect request to the connection lib */
    ConnectionSyncConnectRequest(&aghfp->task, aghfp->rfcomm_sink, &config_params);
}