/****************************************************************************
NAME	
	avrcpProfileHandler

DESCRIPTION
	All messages for the profile lib instance are handled by this function.
*/
void avrcpProfileHandler(Task task, MessageId id, Message message)
{
	AVRCP *avrcp = (AVRCP *) task;
	avrcpState profileState = avrcp->state;

	/* Check the message id */
	switch (id)
	{
	case CL_L2CAP_REGISTER_CFM:

		switch(profileState)
		{
		case avrcpInitialising:
			avrcpHandleL2capRegisterCfm(avrcp, (CL_L2CAP_REGISTER_CFM_T *) message);
			break;

		case avrcpReady:
		case avrcpConnecting:
		case avrcpConnected:
		default:
			/* Shouldn't get this message in any other state */
			handleUnexpected(avrcpUnexpectedClPrim, profileState, id);
			break;
		}
		break;

	case CL_L2CAP_CONNECT_CFM:
	
		switch(profileState)
		{
		case avrcpConnecting:
			avrcpHandleL2capConnectCfm(avrcp, (CL_L2CAP_CONNECT_CFM_T *) message);
			break;

		case avrcpInitialising:
		case avrcpReady:
		case avrcpConnected:
		default:
			/* Shouldn't get this message in any other state */
			handleUnexpected(avrcpUnexpectedClPrim, profileState, id);
			break;
		}
		break;	

	case CL_L2CAP_CONNECT_IND:
		
		switch(profileState)
		{
		case avrcpReady:
			avrcpHandleL2capConnectInd(avrcp, (CL_L2CAP_CONNECT_IND_T *) message);
			break;
		
		case avrcpConnecting:
		case avrcpConnected:
			avrcpHandleL2capConnectIndReject(avrcp, (CL_L2CAP_CONNECT_IND_T *) message);
			break;

		case avrcpInitialising:
		default:
			/* Shouldn't get this message in any other state */
			handleUnexpected(avrcpUnexpectedClPrim, profileState, id);
			break;
		}
		break;

	case CL_L2CAP_DISCONNECT_IND:
		
		switch(profileState)
		{
		case avrcpConnected:
			avrcpHandleL2capDisconnectInd(avrcp, (CL_L2CAP_DISCONNECT_IND_T *) message);
			break;
		
		case avrcpInitialising:
		case avrcpReady:
		case avrcpConnecting:
		default:
			/* Shouldn't get this message in any other state */
			handleUnexpected(avrcpUnexpectedClPrim, profileState, id);
			break;
		}
		break;

	case CL_SDP_REGISTER_CFM:
		
		switch(profileState)
		{
		case avrcpInitialising:
			avrcpHandleSdpRegisterCfm(avrcp, (CL_SDP_REGISTER_CFM_T *) message);
			break;

        case avrcpReady:
		case avrcpConnecting:
		case avrcpConnected:
		default:
			handleUnexpected(avrcpUnexpectedClPrim, profileState, id);
			break;
		}
		break;

	default:
		switch (id)
		{
		case AVRCP_INTERNAL_INIT_REQ:
			
			switch(profileState)
			{
			case avrcpInitialising:
				avrcpHandleInternalInitReq(avrcp);
				break;

			case avrcpReady:
			case avrcpConnecting:
			case avrcpConnected:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_INIT_CFM:
			
			switch(profileState)
			{		
			case avrcpInitialising:
				avrcpHandleInternalInitCfm(avrcp, (AVRCP_INTERNAL_INIT_CFM_T *) message);
				break;
			
			case avrcpReady:
			case avrcpConnecting:
			case avrcpConnected:
			default:
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_CONNECT_REQ:
			
			switch(profileState)
			{		
			case avrcpReady:
				avrcpHandleInternalConnectReq(avrcp, (AVRCP_INTERNAL_CONNECT_REQ_T *) message);
				break;
			case avrcpConnecting:
			case avrcpConnected:
				avrcpSendCommonCfmMessageToApp(AVRCP_CONNECT_CFM, avrcp_bad_state, 0, avrcp);
				break;
			case avrcpInitialising:
			default:
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_DISCONNECT_REQ:
			
			switch(profileState)
			{		
			case avrcpConnected:
				avrcpHandleInternalDisconnectReq(avrcp);
				break;
			case avrcpReady:
			case avrcpConnecting:
				avrcpSendCommonCfmMessageToApp(AVRCP_DISCONNECT_IND, avrcp_device_not_connected, 0, avrcp);
				break;
			case avrcpInitialising:
			default:
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_CONNECT_RES:
			
			switch(profileState)
			{
			case avrcpConnecting:
				avrcpHandleInternalL2capConnectRes(avrcp, (AVRCP_INTERNAL_CONNECT_RES_T *) message);
				break;
			case avrcpReady:
				break;
			case avrcpInitialising:
			case avrcpConnected:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_PASSTHROUGH_REQ:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalPassThroughReq(avrcp, (AVRCP_INTERNAL_PASSTHROUGH_REQ_T *) message);
				break;
			case avrcpReady:
			case avrcpConnecting:
				avrcpSendCommonCfmMessageToApp(AVRCP_PASSTHROUGH_CFM, avrcp_device_not_connected, 0, avrcp);
				break;
			case avrcpInitialising:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_PASSTHROUGH_RES:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalPassThroughRes(avrcp, (AVRCP_INTERNAL_PASSTHROUGH_RES_T *) message);
				break;
			case avrcpReady:
				break;
			case avrcpInitialising:
			case avrcpConnecting:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_UNITINFO_REQ:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalUnitInfoReq(avrcp);
				break;
			case avrcpReady:
			case avrcpConnecting:
				avrcpSendUnitInfoCfmToClient(avrcp, avrcp_device_not_connected, 0, 0, (uint32) 0);
				break;
			case avrcpInitialising:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_UNITINFO_RES:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalUnitInfoRes(avrcp, (AVRCP_INTERNAL_UNITINFO_RES_T *) message);
				break;
			case avrcpReady:
				break;
			case avrcpInitialising:
			case avrcpConnecting:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_SUBUNITINFO_REQ:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalSubUnitInfoReq(avrcp, (AVRCP_INTERNAL_SUBUNITINFO_REQ_T *) message);
				break;
			case avrcpReady:
			case avrcpConnecting:
				avrcpSendSubunitInfoCfmToClient(avrcp, avrcp_device_not_connected, 0, 0);
				break;
			case avrcpInitialising:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_SUBUNITINFO_RES:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalSubUnitInfoRes(avrcp, (AVRCP_INTERNAL_SUBUNITINFO_RES_T *) message);
				break;
			case avrcpReady:
				break;
			case avrcpInitialising:
			case avrcpConnecting:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_VENDORDEPENDENT_REQ:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalVendorDependentReq(avrcp, (AVRCP_INTERNAL_VENDORDEPENDENT_REQ_T *) message);
				break;
			case avrcpReady:	
			case avrcpConnecting:
				avrcpSendCommonCfmMessageToApp(AVRCP_VENDORDEPENDENT_CFM, avrcp_device_not_connected, 0, avrcp);
				break;
			case avrcpInitialising:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_VENDORDEPENDENT_RES:
			
			switch(profileState)
			{
			case avrcpConnected:
				avrcpHandleInternalVendorDependentRes(avrcp, (AVRCP_INTERNAL_VENDORDEPENDENT_RES_T *) message);
				break;
			case avrcpReady:
				break;
			case avrcpInitialising:
			case avrcpConnecting:
			default:
				/* Shouldn't get this message in any other state */
				handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
				break;
			}
			break;

		case AVRCP_INTERNAL_WATCHDOG_TIMEOUT:
			
			avrcpHandleInternalWatchdogTimeout(avrcp);
			break;

		default:
			switch (id)
			{	
			case MESSAGE_MORE_DATA:
				
				switch(profileState)
				{
				case avrcpConnected:
					avrcpHandleReceivedData(avrcp);
					break;
				
				case avrcpInitialising:
				case avrcpReady:
				case avrcpConnecting:
				default:
					/* Shouldn't get this message in any other state */
					handleUnexpected(avrcpUnexpectedAvrcpPrim, profileState, id);
					break;
				}
				break;

			/* Ignored primitives */
			case MESSAGE_MORE_SPACE:
			case MESSAGE_SOURCE_EMPTY:
				break;
				
			default:		
				/* Received an unknown message */
				handleUnexpected(avrcpUnexpectedMessage, profileState, id);
				break;
			}
		}
	}
}
/****************************************************************************
NAME    
    hfpProfileHandler

DESCRIPTION
    All messages for this profile lib are handled by this function

RETURNS
    void
*/
void hfpProfileHandler(Task task, MessageId id, Message message)
{
    if(!theHfp->initialised)
    {
        /* Initialisation messages */
        if(id >= HFP_MSG_BASE && id < HFP_INTERNAL_TOP)
        {
            switch(id)
            {
                case HFP_INTERNAL_SDP_REGISTER_REQ:
                    PRINT(("HFP_INTERNAL_SDP_REGISTER_REQ\n"));
                    hfpRegisterServiceRecord( ((HFP_INTERNAL_SDP_REGISTER_REQ_T *)message)->service );
                break;
                
                default:
                     /* Panic in debug and ignore in release lib variants */  
                    goto hfp_prim_error;
            }
        }
        else if(id >= CL_MESSAGE_BASE && id < CL_MESSAGE_TOP)
        {
            switch (id)
            {
                case CL_DM_SYNC_REGISTER_CFM:
                    PRINT(("CL_DM_SYNC_REGISTER_CFM\n"));
                    /* Ignore this silently */
                break;
                
                case CL_RFCOMM_REGISTER_CFM:
                    PRINT(("CL_RFCOMM_REGISTER_CFM\n"));
                    hfpInitRfcommRegisterCfm((CL_RFCOMM_REGISTER_CFM_T *) message);
                break;
                
                case CL_SDP_REGISTER_CFM:
                    PRINT(("CL_SDP_REGISTER_CFM\n"));
                    hfpInitSdpRegisterCfm((CL_SDP_REGISTER_CFM_T *) message);
                break;
                
                default:
                     /* Panic in debug and ignore in release lib variants */  
                    goto cl_prim_error;
                break;
            }
        }
        else
        {
            /* Panic in debug and ignore in release lib variants */  
            goto sys_prim_error;
        }
    }
    else
    {
        /* Run time messages */
        if(id == HFP_INTERNAL_SDP_REGISTER_REQ)
        {
            /* 
               This message does not conform to the common structure,
               also its sent from inside the lib only so we know service
               is valid. 
            */
            PRINT(("HFP_INTERNAL_SDP_REGISTER_REQ\n"));
            hfpRegisterServiceRecord( ((HFP_INTERNAL_SDP_REGISTER_REQ_T *)message)->service );
        }
        else if(id >= HFP_MSG_BASE && id < HFP_INTERNAL_TOP)
        {
            hfp_link_data* link = NULL;
            bool success = FALSE;
            
            /* Try and obtain the link for this message */
            if(message)
                link = ((HFP_COMMON_INTERNAL_MESSAGE_T*)message)->link;
            else
                link = hfpGetLinkFromTimeoutMessage(id);
            
            if(link)
            {
                switch(link->ag_slc_state)
                {
                    /* 
                       In theory audio connect should only happen once SLC is complete, 
                       however in practice some AG's will open SCO before completing the
                       SLC so we should be able to handle it. 
                    */
                    case hfp_slc_searching:
                    case hfp_slc_incoming:
                    case hfp_slc_outgoing:
                    case hfp_slc_connected:
                        switch(id)
                        {
                            case HFP_INTERNAL_AUDIO_CONNECT_RES:
                                PRINT(("HFP_INTERNAL_AUDIO_CONNECT_RES\n"));
                                hfpHandleAudioConnectRes((HFP_INTERNAL_AUDIO_CONNECT_RES_T*)message);
                                success = TRUE;
                            break;
                            
                            case HFP_RFCOMM_LINK_LOSS_RECONNECT_LINK_0_IND:
                            case HFP_RFCOMM_LINK_LOSS_RECONNECT_LINK_1_IND:
                                PRINT(("HFP_RFCOMM_LINK_LOSS_RECONNECT\n"));
                                hfpHandleRfcommLinkLossReconnect(link);
                                success = TRUE;
                            break;
                            
                            case HFP_RFCOMM_LINK_LOSS_TIMEOUT_LINK_0_IND:
                            case HFP_RFCOMM_LINK_LOSS_TIMEOUT_LINK_1_IND:
                                PRINT(("HFP_RFCOMM_LINK_LOSS_TIMEOUT\n"));
                                hfpHandleRfcommLinkLossTimeout(link);
                                success = TRUE;
                            break;
                                
                            case HFP_INTERNAL_SLC_DISCONNECT_REQ:
                                PRINT(("HFP_INTERNAL_SLC_DISCONNECT_REQ\n"));
                                hfpHandleDisconnectRequest(link);
                                success = TRUE;
                            break;
                            
                            default:
                            break;
                        }
                    break;
                    
                    /*
                       AT commands/requests that are valid once SLC is completed
                    */
                    case hfp_slc_complete:
                        if(hfpLinkIsHfp(link))
                        {
                            /* Messages valid for HFP in this state */
                            switch(id)
                            {
                                case HFP_INTERNAL_AT_BTRH_STATUS_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BTRH_STATUS_REQ\n"));
                                    hfpHandleBtrhStatusReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_BAC_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BAC_REQ\n"));
                                    /* Only allowed if we are an HFP device */
                                    hfpHandleBacRequest(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_CLIP_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CLIP_REQ\n"));
                                    success = hfpHandleCallerIdEnableReq((HFP_INTERNAL_AT_CLIP_REQ_T*)message);
                                break;
                        
                                case HFP_INTERNAL_AT_ANSWER_REQ:
                                    PRINT(("HFP_INTERNAL_AT_ANSWER_REQ\n"));
                                    success = hfpHandleAnswerCall((HFP_INTERNAL_AT_ANSWER_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_AT_TERMINATE_REQ:
                                    PRINT(("HFP_INTERNAL_AT_TERMINATE_REQ\n"));
                                    success = hfpHandleTerminateCall(link, hfpChupCmdPending);
                                break;
                                
                                case HFP_INTERNAL_AT_CLCC_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CLCC_REQ\n"));
                                    success = hfpHandleCurrentCallsGetReq(link);
                                break;
                                
                                case HFP_INTERNAL_AT_CHLD_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CHLD_REQ\n"));
                                    success = hfpSendChldCmd((HFP_INTERNAL_AT_CHLD_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_AT_BTRH_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BTRH_REQ\n"));
                                    success = hfpHandleBtrhReq((HFP_INTERNAL_AT_BTRH_REQ_T*)message);
                                break;
                        
                                case HFP_INTERNAL_AT_BLDN_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BLDN_REQ\n"));
                                    hfpHandleLastNumberRedial(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_ATD_NUMBER_REQ:
                                    PRINT(("HFP_INTERNAL_AT_ATD_NUMBER_REQ\n"));
                                    hfpHandleDialNumberRequest((HFP_INTERNAL_AT_ATD_NUMBER_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_ATD_MEMORY_REQ:
                                    PRINT(("HFP_INTERNAL_AT_ATD_MEMORY_REQ\n"));
                                    hfpHandleDialMemoryRequest((HFP_INTERNAL_AT_ATD_MEMORY_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_BVRA_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BVRA_REQ\n"));
                                    success = hfpHandleVoiceRecognitionEnable((HFP_INTERNAL_AT_BVRA_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_AT_NREC_REQ:
                                    PRINT(("HFP_INTERNAL_AT_NREC_REQ\n"));
                                    hfpHandleNrEcDisable(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_BIA_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BIA_REQ\n"));
                                    hfpHandleSetActiveIndicatorsReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_CMEE_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CMEE_REQ\n"));
                                    hfpHandleExtendedErrorReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_CNUM_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CNUM_REQ\n"));
                                    hfpHandleSubscriberNumberGetReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_BINP_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BINP_REQ\n"));
                                    success = hfpHandleGetVoiceTagReq(link);
                                break;
                                
                                case HFP_INTERNAL_AT_VTS_REQ:
                                    PRINT(("HFP_INTERNAL_AT_VTS_REQ\n"));
                                    hfpHandleDtmfRequest((HFP_INTERNAL_AT_VTS_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_COPS_REQ:
                                    PRINT(("HFP_INTERNAL_AT_COPS_REQ\n"));
                                    hfpHandleNetworkOperatorReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_CCWA_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CCWA_REQ\n"));
                                    success = hfpHandleCallWaitingNotificationEnable((HFP_INTERNAL_AT_CCWA_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_AT_CMD_REQ:
                                {
                                    HFP_INTERNAL_AT_CMD_REQ_T* req = (HFP_INTERNAL_AT_CMD_REQ_T*)message;
                                    PRINT(("HFP_INTERNAL_AT_CMD_REQ\n"));
                                    if(!req->size_cmd) break;
                                    hfpSendAtCmd(req->link, req->size_cmd, req->cmd, hfpAtCmdPending);
                                    success = TRUE;
                                }
                                break;
                                
                                case HFP_INTERNAL_CSR_SUPPORTED_FEATURES_REQ:
                                    PRINT(("HFP_INTERNAL_CSR_SUPPORTED_FEATURES_REQ\n"));
                                    hfpCsrFeaturesReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_CSR_BATTERY_LEVEL_REQ:
                                    PRINT(("HFP_INTERNAL_CSR_BATTERY_LEVEL_REQ\n"));
                                    hfpCsrFeaturesHandleBatteryLevelReq((HFP_INTERNAL_CSR_BATTERY_LEVEL_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_CSR_POWER_SOURCE_REQ:
                                    PRINT(("HFP_INTERNAL_CSR_POWER_SOURCE_REQ\n"));
                                    hfpCsrFeaturesHandlePowerSourceReq((HFP_INTERNAL_CSR_POWER_SOURCE_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_CSR_GET_SMS_REQ:
                                    PRINT(("HFP_INTERNAL_CSR_GET_SMS_REQ\n"));
                                    success = hfpHandleCsrGetSmsReq((HFP_INTERNAL_CSR_GET_SMS_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_0_IND:
                                case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_1_IND:
                                    PRINT(("HFP_INTERNAL_WAIT_AT_TIMEOUT_IND\n"));
                                    hfpHandleWaitAtTimeout(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_SLC_DISCONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_SLC_DISCONNECT_REQ\n"));
                                    hfpHandleDisconnectRequest(link);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_CONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_CONNECT_REQ\n"));
                                    hfpHandleAudioConnectReq((HFP_INTERNAL_AUDIO_CONNECT_REQ_T*)message);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_CONNECT_RES:
                                    PRINT(("HFP_INTERNAL_AUDIO_CONNECT_RES\n"));
                                    hfpHandleAudioConnectRes((HFP_INTERNAL_AUDIO_CONNECT_RES_T*)message);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_TRANSFER_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_TRANSFER_REQ\n"));
                                    hfpHandleAudioTransferReq((HFP_INTERNAL_AUDIO_TRANSFER_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AUDIO_DISCONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_DISCONNECT_REQ\n"));
                                    hfpHandleAudioDisconnectReq(link);
                                    success = TRUE;
                                break;
                                
                                default:
                                break;
                            }
                        }
                        else if(hfpLinkIsHsp(link))
                        {
                            /* Messages valid for HSP in this state */
                            switch(id)
                            {
                                case HFP_INTERNAL_AT_ANSWER_REQ:
                                    PRINT(("HFP_INTERNAL_AT_ANSWER_REQ\n"));
                                    success = hfpHandleAnswerHspCall((HFP_INTERNAL_AT_ANSWER_REQ_T*)message);
                                break;
                                
                                case HFP_INTERNAL_AT_TERMINATE_REQ:
                                    PRINT(("HFP_INTERNAL_AT_TERMINATE_REQ\n"));
                                    success = hfpHandleTerminateHspCall(link);
                                break;
                                
                                case HFP_INTERNAL_AT_BVRA_REQ:
                                    PRINT(("HFP_INTERNAL_AT_BVRA_REQ\n"));
                                    success = hfpHandleHspVoiceRecognitionEnable((HFP_INTERNAL_AT_BVRA_REQ_T*) message);
                                break;
                                
                                case HFP_INTERNAL_AT_CMD_REQ:
                                {
                                    HFP_INTERNAL_AT_CMD_REQ_T* req = (HFP_INTERNAL_AT_CMD_REQ_T*)message;
                                    PRINT(("HFP_INTERNAL_AT_CMD_REQ\n"));
                                    hfpSendAtCmd(req->link, req->size_cmd, req->cmd, hfpAtCmdPending);
                                }
                                break;
                                
                                case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_0_IND:
                                case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_1_IND:
                                    PRINT(("HFP_INTERNAL_WAIT_AT_TIMEOUT_IND\n"));
                                    hfpHandleWaitAtTimeout(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_SLC_DISCONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_SLC_DISCONNECT_REQ\n"));
                                    hfpHandleDisconnectRequest(link);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_CONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_CONNECT_REQ\n"));
                                    hfpSendHsButtonPress(link, hfpCmdPending);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_CONNECT_RES:
                                    PRINT(("HFP_INTERNAL_AUDIO_CONNECT_RES\n"));
                                    hfpHandleAudioConnectRes((HFP_INTERNAL_AUDIO_CONNECT_RES_T*)message);
                                    success = TRUE;
                                break;
                        
                                case HFP_INTERNAL_AUDIO_TRANSFER_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_TRANSFER_REQ\n"));
                                    hfpHandleAudioTransferReq((HFP_INTERNAL_AUDIO_TRANSFER_REQ_T*)message);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AUDIO_DISCONNECT_REQ:
                                    PRINT(("HFP_INTERNAL_AUDIO_DISCONNECT_REQ\n"));
                                    hfpHandleAudioDisconnectReq(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_HS_INCOMING_TIMEOUT_LINK_0_IND:
                                case HFP_INTERNAL_HS_INCOMING_TIMEOUT_LINK_1_IND:
                                    PRINT(("HFP_INTERNAL_HS_INCOMING_TIMEOUT_IND\n"));
                                    hfpHandleCallIncomingTimeout(link);
                                    success = TRUE;
                                break;
                                
                                case HFP_INTERNAL_AT_CKPD_REQ:
                                    PRINT(("HFP_INTERNAL_AT_CKPD_REQ\n"));
                                    hfpSendHsButtonPress(link, hfpCkpdCmdPending);
                                    success = TRUE;
                                break;
                                
                                default:
                                break;
                            }
                        }
                    break;
                    
                    /* 
                       No internal messages valid in these states 
                    */
                    case hfp_slc_idle:
                    case hfp_slc_disabled:
                    default:
                    break;
                }
            }
            
            if(!success)
            {
                MessageId cfm_id = HFP_NO_CFM;
                
                PRINT(("Link %X, SLC state %X\n", (uint16)link, link ? link->ag_slc_state : 0));
                
                /* Either the link is NULL or link state was invalid. */
                switch (id)
                {
                    case HFP_RFCOMM_LINK_LOSS_RECONNECT_LINK_0_IND:
                    case HFP_RFCOMM_LINK_LOSS_RECONNECT_LINK_1_IND:
                        PRINT(("HFP_RFCOMM_LINK_LOSS_RECONNECT\n"));
                        goto hfp_prim_error;
                    break;
                    
                    case HFP_RFCOMM_LINK_LOSS_TIMEOUT_LINK_0_IND:
                    case HFP_RFCOMM_LINK_LOSS_TIMEOUT_LINK_1_IND:
                        PRINT(("HFP_RFCOMM_LINK_LOSS_TIMEOUT\n"));
                        goto hfp_prim_error;
                    break;

                    case HFP_INTERNAL_AT_BAC_REQ:
                        PRINT(("HFP_INTERNAL_AT_BAC_REQ\n"));
                        /* Ignore this silently */
                    break;
                    
                    case HFP_INTERNAL_AT_BTRH_STATUS_REQ:
                        PRINT(("HFP_INTERNAL_AT_BTRH_STATUS_REQ\n"));
                        /* Internal request failed, ignore */
                    break;
                    
                    case HFP_INTERNAL_AT_CLIP_REQ:
                        PRINT(("HFP_INTERNAL_AT_CLIP_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_CALLER_ID_ENABLE_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_ANSWER_REQ:
                        PRINT(("HFP_INTERNAL_AT_ANSWER_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_CALL_ANSWER_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_TERMINATE_REQ:
                        PRINT(("HFP_INTERNAL_AT_TERMINATE_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = hfpHandleChupAtAck(link);
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_BLDN_REQ:
                        PRINT(("HFP_INTERNAL_AT_BLDN_REQ"));
                        cfm_id = HFP_DIAL_LAST_NUMBER_CFM;
                    break;
                    
                    case HFP_INTERNAL_AT_ATD_NUMBER_REQ:
                        PRINT(("HFP_INTERNAL_AT_ATD_NUMBER_REQ"));
                        cfm_id = HFP_DIAL_NUMBER_CFM;
                    break;
                    
                    case HFP_INTERNAL_AT_ATD_MEMORY_REQ:
                        PRINT(("HFP_INTERNAL_AT_ATD_MEMORY_REQ"));
                        cfm_id = HFP_DIAL_MEMORY_CFM;
                    break;
                    
                    case HFP_INTERNAL_AT_BVRA_REQ:
                        PRINT(("HFP_INTERNAL_AT_BVRA_REQ\n"));
                        cfm_id = HFP_VOICE_RECOGNITION_ENABLE_CFM;
                    break;
                    
                    case HFP_INTERNAL_AT_NREC_REQ:
                        PRINT(("HFP_INTERNAL_AT_NREC_REQ\n"));
                        /* Internal request failed, ignore */
                    break;
                    
                    case HFP_INTERNAL_AT_CLCC_REQ:
                        PRINT(("HFP_INTERNAL_AT_CLCC_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_CURRENT_CALLS_CFM;
#endif
                    break;
                            
                    case HFP_INTERNAL_AT_CHLD_REQ:
                        PRINT(("HFP_INTERNAL_AT_CHLD_REQ\n"));
                        hfpHandleChldAtAck(link, ((HFP_INTERNAL_AT_CHLD_REQ_T*)message)->action, hfp_fail);
                    break;
                    
                    case HFP_INTERNAL_AT_BTRH_REQ:
                        PRINT(("HFP_INTERNAL_AT_BTRH_REQ\n"));
#ifndef HFP_MIN_CFM
                        hfpHandleBtrhAtAck(link, ((HFP_INTERNAL_AT_BTRH_REQ_T*)message)->action, hfp_fail);
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_BIA_REQ:
                        PRINT(("HFP_INTERNAL_AT_BIA_REQ\n"));
                        /* Internal request failed, ignore */
                    break;
                    
                    case HFP_INTERNAL_AT_CMEE_REQ:
                        PRINT(("HFP_INTERNAL_AT_CMEE_REQ\n"));
                        /* Internal request failed, ignore */
                    break;
                    
                    case HFP_INTERNAL_AT_CNUM_REQ:
                        PRINT(("HFP_INTERNAL_AT_CNUM_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_SUBSCRIBER_NUMBERS_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_BINP_REQ:
                        PRINT(("HFP_INTERNAL_AT_BINP_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_VOICE_TAG_NUMBER_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_VTS_REQ:
                        PRINT(("HFP_INTERNAL_AT_VTS_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_DTMF_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_COPS_REQ:
                        PRINT(("HFP_INTERNAL_AT_COPS_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_NETWORK_OPERATOR_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_CCWA_REQ:
                        PRINT(("HFP_INTERNAL_AT_CCWA_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_CALL_WAITING_ENABLE_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_AT_CMD_REQ:
                        PRINT(("HFP_INTERNAL_AT_CMD_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_AT_CMD_CFM;
#endif
                    break;
                    
                    case HFP_INTERNAL_CSR_SUPPORTED_FEATURES_REQ:
                        PRINT(("HFP_INTERNAL_CSR_SUPPORTED_FEATURES_REQ\n"));
                        hfpCsrFeaturesHandleCsrSfAtAck(link, hfp_fail);
                    break;
                    
                    case HFP_INTERNAL_CSR_BATTERY_LEVEL_REQ:
                        PRINT(("HFP_INTERNAL_CSR_BATTERY_LEVEL_REQ\n"));
                        /* No notification for app so ignore this */
                    break;
                    
                    case HFP_INTERNAL_CSR_POWER_SOURCE_REQ:
                        PRINT(("HFP_INTERNAL_CSR_POWER_SOURCE_REQ\n"));
                        /* No notification for app so ignore this */
                    break;
                    
                    case HFP_INTERNAL_CSR_GET_SMS_REQ:
                        PRINT(("HFP_INTERNAL_CSR_GET_SMS_REQ\n"));
                        hfpCsrFeaturesHandleGetSmsAtAck(link, hfp_fail);
                    break;
                    
                    case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_0_IND:
                    case HFP_INTERNAL_WAIT_AT_TIMEOUT_LINK_1_IND:
                        PRINT(("HFP_INTERNAL_WAIT_AT_TIMEOUT_IND\n"));
                        /* Timeout on a link we don't recognise */
                        goto hfp_prim_error;
                    break;
                    
                    case HFP_INTERNAL_SLC_DISCONNECT_REQ:
                        PRINT(("HFP_INTERNAL_SLC_DISCONNECT_REQ\n"));
                        /* Disconnect request on invalid link/link with no SLC */
                        hfpSendSlcDisconnectIndToApp(link, hfp_disconnect_no_slc);
                    break;
                    
                    case HFP_INTERNAL_AUDIO_CONNECT_REQ:
                        PRINT(("HFP_INTERNAL_AUDIO_CONNECT_REQ\n"));
                        sendAudioConnectCfmFailToApp(link, hfp_audio_connect_error);
                    break;
                    
                    case HFP_INTERNAL_AUDIO_CONNECT_RES:
                        PRINT(("HFP_INTERNAL_AUDIO_CONNECT_RES\n"));
                        sendAudioConnectCfmFailToApp(link, hfp_audio_connect_error);
                    break;
                    
                    case HFP_INTERNAL_AUDIO_DISCONNECT_REQ:
                        PRINT(("HFP_INTERNAL_AUDIO_DISCONNECT_REQ\n"));
                        sendAudioDisconnectIndToApp(link, hfp_audio_disconnect_error);
                    break;
                    
                    case HFP_INTERNAL_AUDIO_TRANSFER_REQ:
                        PRINT(("HFP_INTERNAL_AUDIO_TRANSFER_REQ\n"));
                        /* What fail message we send depends on the direction requested */
                        switch(((HFP_INTERNAL_AUDIO_TRANSFER_REQ_T*)message)->direction)
                        {
                            case hfp_audio_to_hfp:
                                sendAudioConnectCfmFailToApp(link, hfp_audio_connect_error);
                                break;

                            case hfp_audio_to_ag:
                                sendAudioDisconnectIndToApp(link, hfp_audio_disconnect_error);
                            break;

                            case hfp_audio_transfer:
                                if (link && link->audio_sink)
                                    sendAudioDisconnectIndToApp(link, hfp_audio_disconnect_error);
                                else
                                    sendAudioConnectCfmFailToApp(link, hfp_audio_connect_error);
                            break;
                        }
                    break;
                    
                    case HFP_INTERNAL_HS_INCOMING_TIMEOUT_LINK_0_IND:
                    case HFP_INTERNAL_HS_INCOMING_TIMEOUT_LINK_1_IND:
                        PRINT(("HFP_INTERNAL_HS_INCOMING_TIMEOUT_IND\n"));
                        /* SLC has closed before the incoming timeout fired */
                        goto hfp_prim_error;
                    break;
                    
                    case HFP_INTERNAL_AT_CKPD_REQ:
                        PRINT(("HFP_INTERNAL_AT_CKPD_REQ\n"));
#ifndef HFP_MIN_CFM
                        cfm_id = HFP_HS_BUTTON_PRESS_CFM;
#endif
                    break;
                    
                    default:
                        /* Panic in debug and ignore in release lib variants */  
                        goto hfp_prim_error;
                    break;
                }
                
                /* Send _CFM message to app if necessary */
                if(cfm_id) hfpSendCommonCfmMessageToApp(cfm_id, link, hfp_fail);
            }
        }
        else if(id >= CL_MESSAGE_BASE && id < CL_MESSAGE_TOP)
        {
            switch(id)
            {
                case CL_RFCOMM_REGISTER_CFM:
                    PRINT(("CL_RFCOMM_REGISTER_CFM\n"));
                    goto cl_prim_error;
                break;

                case CL_RFCOMM_CONNECT_IND:
                    PRINT(("CL_RFCOMM_CONNECT_IND\n"));
                    /* Handle the connect cfm */
                    hfpHandleRfcommConnectInd((CL_RFCOMM_CONNECT_IND_T *) message);
                break;
                
                case CL_RFCOMM_CLIENT_CONNECT_CFM:
                    PRINT(("CL_RFCOMM_CLIENT_CONNECT_CFM\n"));
                    hfpHandleRfcommClientConnectCfm((CL_RFCOMM_CLIENT_CONNECT_CFM_T *) message);
                break;
                
                case CL_RFCOMM_SERVER_CONNECT_CFM:
                    PRINT(("CL_RFCOMM_SERVER_CONNECT_CFM\n"));
                    hfpHandleRfcommServerConnectCfm((CL_RFCOMM_SERVER_CONNECT_CFM_T *) message);
                break;
                
                case CL_RFCOMM_DISCONNECT_IND:
                    PRINT(("CL_RFCOMM_DISCONNECT_IND\n"));
                    hfpHandleRfcommDisconnectInd((CL_RFCOMM_DISCONNECT_IND_T *) message);
                break;
                
                case CL_RFCOMM_DISCONNECT_CFM:
                    PRINT(("CL_RFCOMM_DISCONNECT_CFM\n"));
                    hfpHandleRfcommDisconnectCfm((CL_RFCOMM_DISCONNECT_CFM_T *) message);
                break;

                case CL_SDP_REGISTER_CFM:
                    PRINT(("CL_SDP_REGISTER_CFM\n"));
                    hfpHandleSdpRegisterCfm((CL_SDP_REGISTER_CFM_T *) message);
                break;

                case CL_SDP_UNREGISTER_CFM:
                    PRINT(("CL_SDP_UNREGISTER_CFM\n"));
                    handleSdpUnregisterCfm((CL_SDP_UNREGISTER_CFM_T *) message);
                break;

                case CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM:
                    PRINT(("CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM\n"));
                    /* Currently we only look for attributes during SLC establishment and connection */
                    hfpHandleServiceSearchAttributeCfm((CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T *) message);
                break;

                case CL_DM_SYNC_CONNECT_IND:
                    PRINT(("CL_DM_SYNC_CONNECT_IND\n"));
                    hfpHandleSyncConnectInd((CL_DM_SYNC_CONNECT_IND_T*)message);
                break;

                case CL_DM_SYNC_CONNECT_CFM:
                    PRINT(("CL_DM_SYNC_CONNECT_CFM\n"));
                    hfpHandleSyncConnectCfm((CL_DM_SYNC_CONNECT_CFM_T*)message);
                break;
                
                case CL_DM_SYNC_DISCONNECT_IND:
                    PRINT(("CL_DM_SYNC_DISCONNECT_IND\n"));
                    hfpHandleSyncDisconnectInd((CL_DM_SYNC_DISCONNECT_IND_T*)message);
                break;

                case CL_RFCOMM_PORTNEG_IND:
                {
                    PRINT(("CL_RFCOMM_PORTNEG_IND\n"));
                    hfpHandleRfcommPortnegInd((CL_RFCOMM_PORTNEG_IND_T*)message);
                }
                break;
                
                case CL_RFCOMM_PORTNEG_CFM:
                case CL_RFCOMM_CONTROL_IND:
                case CL_RFCOMM_CONTROL_CFM:
                case CL_RFCOMM_LINE_STATUS_IND:
                case CL_DM_ROLE_CFM:
                case CL_DM_SYNC_REGISTER_CFM:
                case CL_SM_ENCRYPTION_CHANGE_IND:
                case CL_SM_ENCRYPTION_KEY_REFRESH_IND:
                    /* Silently ignore these */
                break;
                
                default:
                    /* Panic in debug and ignore in release lib variants */  
                    goto cl_prim_error;
                break;
            }
        }
        else
        {
            switch(id)
            {
                case MESSAGE_MORE_DATA:
                {
                    PRINT(("MESSAGE_MORE_DATA"));
                    /* We have received more data into the RFCOMM buffer */
                    hfpHandleReceivedData(((MessageMoreData *) message)->source);
                    PRINT(("\n"));
                }
                break;
                
                case MESSAGE_MORE_SPACE:
                case MESSAGE_SOURCE_EMPTY:
                case MESSAGE_STREAM_DISCONNECT:
                    /* Silently ignore these */
                    break;
                
                default:
                    /* Panic in debug and ignore in release lib variants */    
                    goto sys_prim_error;
                break;
            }
        }
    }
    return;
    
    sys_prim_error:
    handleUnexpected(hfpUnexpectedMessage, id);
    return;
    
    cl_prim_error:
    handleUnexpected(hfpUnexpectedClPrim, id);
    return;

    hfp_prim_error:
    handleUnexpected(hfpUnexpectedHfpPrim, id);
    return;

}
static void handleAghfpMessages(Task task, MessageId id, Message message)
{
	AGHFP *aghfp = (AGHFP *) task;
	aghfp_state profileState = aghfp->state;

	switch(id)
	{
		/*************************************************************************/
		/* Init messages                                                         */
		/*************************************************************************/



		case AGHFP_INTERNAL_INIT_REQ:
			PRINT(("AGHFP_INTERNAL_INIT_REQ\n"));

			switch(profileState)
			{
				case aghfp_initialising:
					aghfpHandleInternalInitReq(aghfp);
					break;
				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_INIT_CFM:
			PRINT(("AGHFP_INTERNAL_INIT_CFM\n"));

			switch(profileState)
			{
				case aghfp_initialising:
					aghfpHandleInternalInitCfm(aghfp, (AGHFP_INTERNAL_INIT_CFM_T *) message);
					break;
				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		/*************************************************************************/
		/* SDP Messages                                                          */
		/*************************************************************************/

		case AGHFP_INTERNAL_SDP_REGISTER_CFM:
			PRINT(("AGHFP_INTERNAL_SDP_REGISTER_CFM\n"));

			switch(profileState)
			{
				case aghfp_initialising:
					aghfpHandleSdpInternalRegisterInit(aghfp, (AGHFP_INTERNAL_SDP_REGISTER_CFM_T *) message);
					break;
				case aghfp_ready:
					aghfpHandleSdpInternalRegisterCfm((AGHFP_INTERNAL_SDP_REGISTER_CFM_T *) message);
					break;
				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		/*************************************************************************/
		/* SLC Messages                                                          */
		/*************************************************************************/

		case AGHFP_INTERNAL_SLC_CONNECT_REQ:
			PRINT(("AGHFP_INTERNAL_SLC_CONNECT_REQ\n"));

			switch(profileState)
			{
				case aghfp_ready:
					aghfpHandleSlcConnectRequest(aghfp, (AGHFP_INTERNAL_SLC_CONNECT_REQ_T *) message);
					break;

				case aghfp_slc_connecting:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
					/*  If this profile instance is already connecting/connected then
					reject the SLC connect request and inform the app. */
					aghfpSendSlcConnectCfmToApp(aghfp_connect_failed_busy, aghfp);
					break;

				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_SLC_CONNECT_RES:
			PRINT(("AGHFP_INTERNAL_SLC_CONNECT_RES\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
					aghfpHandleSlcConnectResponse(aghfp, (AGHFP_INTERNAL_SLC_CONNECT_RES_T *) message);
					break;

				case aghfp_ready:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
					/*  If the app is sending us an SLC connect response when we're not
					connecting then send it an error message since we currently don't
					have an SLC being established. */
					aghfpSendSlcConnectCfmToApp(aghfp_connect_failed_busy, aghfp);
					break;

				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_RFCOMM_CONNECT_REQ:
			PRINT(("AGHFP_INTERNAL_RFCOMM_CONNECT_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
					aghfpHandleRfcommConnectRequest(aghfp, (AGHFP_INTERNAL_RFCOMM_CONNECT_REQ_T *) message);
					break;
				default:
					/* Something wrong, this message is sent internally from the AGHFP */
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_SLC_DISCONNECT_REQ:
			PRINT(("AGHFP_INTERNAL_SLC_DISCONNECT_REQ"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
                case aghfp_active_call:				
					aghfpHandleSlcDisconnect(aghfp);
					break;
				case aghfp_ready:
					/* Send disconnect message with error - nothing to disconnect */
					aghfpHandleDisconnectRequestFail(aghfp);
					break;
                case aghfp_initialising:
				default:
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;


		/*************************************************************************/
		/* AT Messages                                                           */
		/*************************************************************************/

		case AGHFP_INTERNAL_AT_CKPD_CMD:
			PRINT(("AGHFP_INTERNAL_AT_CKPD_CMD\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleKeypadCommand(aghfp, ((AGHFP_INTERNAL_AT_CKPD_CMD_T*)message)->keycode);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;
			
		case AGHFP_INTERNAL_AT_BRSF_REQ:
			PRINT(("AGHFP_INTERNAL_AT_BRSF_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
				{
					uint16 hf_supported_features = ((AGHFP_INTERNAL_AT_BRSF_REQ_T *)message)->hf_supported_features;

					/* Only allowed if we are an HFP device */
					if (!supportedProfileIsHfp(aghfp->supported_profile)) Panic();

					aghfpHandleBrsfRequest(aghfp, hf_supported_features);
					break;
				}
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_AT_CIND_SUPPORTED_REQ:
			PRINT(("AGHFP_INTERNAL_AT_CIND_SUPPORTED_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					/* Only allowed if we are an HFP device */
					if (!supportedProfileIsHfp(aghfp->supported_profile)) Panic();
					aghfpHandleCindSupportedRequest(aghfp);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_AT_CIND_STATUS_REQ:
			PRINT(("AGHFP_INTERNAL_AT_CIND_STATUS_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					/* Only allowed if we are an HFP device */
					if (!supportedProfileIsHfp(aghfp->supported_profile)) Panic();
					aghfpHandleCindStatusRequest(aghfp);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_AT_CMER_REQ:
			PRINT(("AGHFP_INTERNAL_AT_CMER_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
				{
					AGHFP_INTERNAL_AT_CMER_REQ_T *data = (AGHFP_INTERNAL_AT_CMER_REQ_T *)message;

					/* Only allowed if we are an HFP device */
					if (!supportedProfileIsHfp(aghfp->supported_profile)) Panic();

					aghfpHandleCmerRequest(aghfp, data->mode, data->ind);
					break;
				}
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_CALL_HOLD_SUPPORT_REQ: /* HF sent AT+CHLD=? query */
			PRINT(("AGHFP_INTERNAL_CALL_HOLD_SUPPORT_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connecting:
					/* Only allowed if we are an HFP device */
					if (!supportedProfileIsHfp(aghfp->supported_profile)) Panic();
					aghfpHandleCallHoldSupportRequest(aghfp);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_CALL_HOLD_REQ: /* HF sent AT+CHLD=<action>[<idx>] instruction */
		{
    		AGHFP_INTERNAL_CALL_HOLD_REQ_T *msg = (AGHFP_INTERNAL_CALL_HOLD_REQ_T *)message;
			PRINT(("AGHFP_INTERNAL_CALL_HOLD_REQ\n"));

			switch(profileState)
			{
				case aghfp_active_call:
					aghfpHandleCallHoldReq(aghfp, msg->action, msg->index);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}

			break;
		}

		case AGHFP_INTERNAL_ANSWER_REQ: /* HF wants to answer the call */
			PRINT(("AGHFP_INTERNAL_ANSWER_REQ\n"));

			switch(profileState)
			{
				case aghfp_incoming_call_establish:
#if 0	/* Always pass notification to app and let it call AghfpCallAnswer() */						
					if ( aghfpCallManagerActive(aghfp) )
					{
						aghfpSendOk(aghfp);
						aghfpManageCall(aghfp, CallEventAnswer, CallFlagOpenAudio);
					}
					else
#endif
					{
						aghfpHandleAnswer(aghfp);
					}
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_CALLER_ID_SETUP_REQ: /* HF wants to enable/disable caller ID notification */
			PRINT(("AGHFP_INTERNAL_CALLER_ID_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleCallerIdSetupReq(aghfp, ((AGHFP_INTERNAL_CALLER_ID_SETUP_REQ_T*)message)->state);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_CALL_WAITING_SETUP_REQ: /* HF wants to enable/disable call waiting notification */
			PRINT(("AGHFP_INTERNAL_CALL_WAITING_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleCallWaitingSetupReq(aghfp, ((AGHFP_INTERNAL_CALL_WAITING_SETUP_REQ_T*)message)->state);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_CALL_HANG_UP_REQ: /* HF wants to reject incoming call */
			PRINT(("AGHFP_INTERNAL_CALL_HANG_UP_REQ\n"));

			switch(profileState)
			{
				case aghfp_incoming_call_establish:
                case aghfp_outgoing_call_establish:
				case aghfp_active_call:
#if 0	/* Always pass notification up to app and let it call AghfpCallTerminate() */
					if ( aghfpCallManagerActive(aghfp) )
					{
						aghfpSendOk(aghfp);
						aghfpManageCall(aghfp, CallEventTerminate, CallFlagCloseAudio);
					}
					else
#endif
					{
						aghfpHandleCallHangUpReq(aghfp);
					}
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_DIAL_REQ: /* HF wants to dial a number */
			PRINT (("AGHFP_INTERNAL_DIAL_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_active_call:
				{
					AGHFP_INTERNAL_DIAL_REQ_T *data = (AGHFP_INTERNAL_DIAL_REQ_T*)message;
					aghfpHandleDialReq(aghfp, data->number, data->size_number);
					break;
				}
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_MEMORY_DIAL_REQ: /* HF wants to dial a number from the AG's phonebook */
			PRINT (("AGHFP_INTERNAL_MEMORY_DIAL_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				{
					AGHFP_INTERNAL_MEMORY_DIAL_REQ_T *data = (AGHFP_INTERNAL_MEMORY_DIAL_REQ_T*)message;
					aghfpHandleMemoryDialReq(aghfp, data->number, data->size_number);
					break;
				}
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_LAST_NUMBER_REDIAL_REQ: /* HF wants to redial last number dialed by AG */
			PRINT (("AGHFP_INTERNAL_LAST_NUMBER_REDIAL_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
					aghfpHandleLastNumberRedialReq(aghfp);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_NREC_SETUP_REQ:
			PRINT (("AGHFP_INTERNAL_NREC_SETUP_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleNrecSetupReq(aghfp, ((AGHFP_INTERNAL_NREC_SETUP_REQ_T*)message)->state);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;

		case AGHFP_INTERNAL_VOICE_RECOGNITION_SETUP_REQ:
			PRINT (("AGHFP_INTERNAL_VOICE_RECOGNITION_SETUP_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleVoiceRecognitionSetupReq(aghfp, ((AGHFP_INTERNAL_VOICE_RECOGNITION_SETUP_REQ_T*)message)->state);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;


		case AGHFP_INTERNAL_PHONE_NUMBER_REQ:
			PRINT (("AGHFP_INTERNAL_PHONE_NUMBER_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandlePhoneNumberForVoiceTagReq(aghfp);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;


		case AGHFP_INTERNAL_TRANSMIT_DTMF_CODE:
		{
			AGHFP_INTERNAL_TRANSMIT_DTMF_CODE_T *data = (AGHFP_INTERNAL_TRANSMIT_DTMF_CODE_T*)message;
			PRINT (("AGHFP_INTERNAL_TRANSMIT_DTMF_CODE\n"));

			switch(profileState)
			{
				case aghfp_active_call:
					aghfpHandleTransmitDtmfCode(aghfp, data->code);
					break;
				default:
					aghfpSendError(aghfp);
					break;
			}
			break;
		}

        case AGHFP_INTERNAL_RESPONSE_HOLD_STATUS_REQUEST_REQ:    /* AT+BTRH?         */
            PRINT (("AGHFP_INTERNAL_RESPONSE_HOLD_STATUS_REQUEST_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleResponseHoldStatusRequest(aghfp);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;

        case AGHFP_INTERNAL_SET_RESPONSE_HOLD_STATUS_REQ:           /* AT+BTRH=c        */
        {
            AGHFP_INTERNAL_SET_RESPONSE_HOLD_STATUS_REQ_T *msg = (AGHFP_INTERNAL_SET_RESPONSE_HOLD_STATUS_REQ_T *)message;
            PRINT (("AGHFP_INTERNAL_SET_RESPONSE_HOLD_STATUS_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleSetResponseHoldStatus(aghfp,msg->cmd);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;
        }

        case AGHFP_INTERNAL_SUBSCRIBER_NUMBER_REQ:       /* AT+CNUM          */
            PRINT (("AGHFP_INTERNAL_SUBSCRIBER_NUMBER_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleSubscriberNumbersRequest(aghfp);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;

        case AGHFP_INTERNAL_CURRENT_CALLS_REQ:           /* AT+CLCC          */
            PRINT (("AGHFP_INTERNAL_CURRENT_CALLS_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleCurrentCallsRequest(aghfp);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;

        case AGHFP_INTERNAL_NETWORK_OPERATOR_FORMAT_REQ: /* AT+COPS=m,f      */
        {
            AGHFP_INTERNAL_NETWORK_OPERATOR_FORMAT_REQ_T *msg = (AGHFP_INTERNAL_NETWORK_OPERATOR_FORMAT_REQ_T *)message;
            PRINT (("AGHFP_INTERNAL_NETWORK_OPERATOR_FORMAT_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleNetworkOperatorFormatRequest(aghfp, msg->mode, msg->format);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;
        }

        case AGHFP_INTERNAL_NETWORK_OPERATOR_REQ:        /* AT+COPS?         */
            PRINT (("AGHFP_INTERNAL_NETWORK_OPERATOR_REQ\n"));
        	switch(profileState)
        	{
            	case aghfp_slc_connected:
            	case aghfp_incoming_call_establish:
            	case aghfp_outgoing_call_establish:
            	case aghfp_active_call:
            	    aghfpHandleNetworkOperatorRequest(aghfp);
            	    break;
        		default:
        			aghfpSendError(aghfp);
        			break;
        	}
            break;


		/*************************************************************************/
		/* Audio Connection Messages                                             */
		/*************************************************************************/

		case AGHFP_INTERNAL_AUDIO_TRANSFER_REQ:
			/* TODO - really need to handle this message! */
			break;

		case AGHFP_INTERNAL_AUDIO_CONNECT_REQ:
			PRINT(("AGHFP_INTERNAL_AUDIO_CONNECTION_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleAudioConnectReq(aghfp, (AGHFP_INTERNAL_AUDIO_CONNECT_REQ_T*)message);
					break;
				default:
					/* Panic in debug and ignore in release lib variants */
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_AUDIO_CONNECT_RES:
			PRINT(("AGHFP_INTERNAL_AUDIO_CONNECT_RES\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
				{
					aghfpHandleAudioConnectRes(aghfp, (AGHFP_INTERNAL_AUDIO_CONNECT_RES_T*)message);
					break;
				}
				default:
					/* Panic in debug and ignore in release lib variants */
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_AUDIO_DISCONNECT_REQ:
			PRINT(("AGHFP_INTERNAL_AUDIO_DISCONNECT_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleAudioDisconnectReq(aghfp);
					break;
				default:
					/* Panic in debug and ignore in release lib variants */
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;

		case AGHFP_INTERNAL_SET_AUDIO_PARAMS_REQ:
			PRINT(("AGHFP_INTERNAL_SET_AUDIO_PARAMS_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSetAudioParamsReq(aghfp, (AGHFP_INTERNAL_SET_AUDIO_PARAMS_REQ_T*)message);
					break;
				default:
					/* Panic in debug and ignore in release lib variants */
					handleUnexpected(aghfpUnexpectedAghfpPrim, profileState, id);
					break;
			}
			break;
			

		/*************************************************************************/
		/* Indicator Messages                                                    */
		/*************************************************************************/

		case AGHFP_INTERNAL_SEND_SERVICE_INDICATOR:
			PRINT(("AGHFP_INTERNAL_SEND_SERVICE_INDICATOR\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
					aghfpHandleSendServiceIndicator(aghfp,
					((AGHFP_INTERNAL_SEND_SERVICE_INDICATOR_T*)message)->availability);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SEND_SERVICE_INDICATOR_CFM, aghfp, aghfp_fail);
					break;
			}
			break;

		case AGHFP_INTERNAL_SEND_CALL_INDICATOR:
		{
			aghfp_call_status status = ((AGHFP_INTERNAL_SEND_CALL_INDICATOR_T*)message)->status;

			PRINT(("AGHFP_INTERNAL_SEND_CALL_INDICATOR\n"));

			if ((profileState == aghfp_incoming_call_establish && status == aghfp_call_active) ||
				(profileState == aghfp_outgoing_call_establish && status == aghfp_call_active) ||
				(profileState == aghfp_active_call             && status == aghfp_call_none))
			{
				aghfpHandleSendCallIndicator(aghfp, status);
			}
			else
			{
				aghfpSendCommonCfmMessageToApp(AGHFP_SEND_CALL_INDICATOR_CFM, aghfp, aghfp_fail);
			}

			break;
		}

		case AGHFP_INTERNAL_SEND_CALL_SETUP_INDICATOR:
		{
			aghfp_call_setup_status status;
			status = ((AGHFP_INTERNAL_SEND_CALL_SETUP_INDICATOR_T*)message)->type;

			PRINT(("AGHFP_INTERNAL_SEND_CALL_SETUP_INDICATOR\n"));

			if ((profileState == aghfp_slc_connected    		&& status == aghfp_call_setup_incoming) ||
				(profileState == aghfp_slc_connected    		&& status == aghfp_call_setup_outgoing) ||
				(profileState == aghfp_incoming_call_establish 	&& status == aghfp_call_setup_none) ||
                (profileState == aghfp_outgoing_call_establish 	&& status == aghfp_call_setup_none) ||
				(profileState == aghfp_outgoing_call_establish  && status == aghfp_call_setup_remote_alert) ||
				(profileState == aghfp_active_call) )
			{
				aghfpHandleSendCallSetupIndicator(aghfp, status);
			}
			else
			{
				aghfpSendCommonCfmMessageToApp(AGHFP_SEND_CALL_SETUP_INDICATOR_CFM, aghfp, aghfp_fail);
			}

			break;
		}

		case AGHFP_INTERNAL_SEND_CALL_HELD_INDICATOR:
			/* TODO - really need to handle this message */
			break;
			
		case AGHFP_INTERNAL_SEND_SIGNAL_INDICATOR:
			/* TODO - really need to handle this message */
			break;
		
		case AGHFP_INTERNAL_SEND_ROAM_INDICATOR:
			/* TODO - really need to handle this message */
			break;
			
		case AGHFP_INTERNAL_SEND_BATT_CHG_INDICATOR:
			/* TODO - really need to handle this message */
			break;

		case AGHFP_INTERNAL_SEND_CALL_WAITING_NOTIFICATION:
		{
			AGHFP_INTERNAL_SEND_CALL_WAITING_NOTIFICATION_T *scwn =	(AGHFP_INTERNAL_SEND_CALL_WAITING_NOTIFICATION_T *)message;
			PRINT(("AGHFP_INTERNAL_SEND_CALL_WAITING_NOTIFICATION\n"));

			switch(profileState)
			{
				case aghfp_active_call:
					aghfpHandleSendCallWaitingNotification(aghfp, scwn->type, scwn->size_number, scwn->number, scwn->size_string, scwn->string);
					break;
				default:
					free(scwn->string);
					aghfpSendCommonCfmMessageToApp(AGHFP_SEND_CALL_WAITING_NOTIFICATION_CFM, aghfp, aghfp_fail);
					break;
			}

			break;
		}

		case AGHFP_INTERNAL_SET_SERVICE_STATE:
		{
			PRINT(("AGHFP_INTERNAL_SET_SERVICE_STATE\n"));

			switch (profileState)
			{
				case aghfp_initialising:
					break;	/* Silently ignore */
				case aghfp_ready:
				case aghfp_slc_connecting:
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSetServiceState(aghfp, ((AGHFP_INTERNAL_SET_SERVICE_STATE_T*)message)->service_state);
					break;
			}

			break;
		}


		/*************************************************************************/
		/* Call Related Messages                                                 */
		/*************************************************************************/

		case AGHFP_INTERNAL_SEND_RING_ALERT:
			PRINT(("AGHFP_INTERNAL_SEND_RING_ALERT\n"));

			switch(profileState)
			{
				case aghfp_incoming_call_establish:
					aghfpHandleSendRingAlert(aghfp);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SEND_RING_ALERT_CFM, aghfp, aghfp_fail);
					break;
			}
			break;

		case AGHFP_INTERNAL_SEND_CALLER_ID:
		{
			AGHFP_INTERNAL_SEND_CALLER_ID_T *msg = (AGHFP_INTERNAL_SEND_CALLER_ID_T*)message;
			switch(profileState)
			{
				case aghfp_incoming_call_establish:
					aghfpHandleSendCallerId(aghfp, msg->type, msg->size_number, msg->size_string, msg->data);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SEND_CALLER_ID_CFM, aghfp, aghfp_fail);
					break;
			}
			break;
		}
		case AGHFP_INTERNAL_INBAND_RING_TONE_ENABLE:
			PRINT(("AGHFP_INTERNAL_INBAND_RING_TONE_ENABLE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
					aghfpHandleInBandRingToneEnable(aghfp, ((AGHFP_INTERNAL_INBAND_RING_TONE_ENABLE_T*)message)->enable);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_INBAND_RING_ENABLE_CFM, aghfp, aghfp_fail);
					break;
			}
			break;


		/*************************************************************************/
		/* Call Management Messages                                              */
		/*************************************************************************/

		case AGHFP_INTERNAL_CALL_MGR_CREATE_REQ:
			PRINT(("AGHFP_INTERNAL_CALL_MGR_CREATE_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_slc_connected:
					aghfpHandleCallCreate(aghfp, (AGHFP_INTERNAL_CALL_MGR_CREATE_REQ_T *)message);
					break;
				default:
					aghfpSendCallCreateCfmToApp(aghfp, aghfp_call_create_error);
					break;
			}
			break;
			
			
		case AGHFP_INTERNAL_CALL_MGR_CREATE_WITH_AUDIO_REQ:
			PRINT(("AGHFP_INTERNAL_CALL_MGR_CREATE_WITH_AUDIO_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_slc_connected:
					aghfpHandleCallCreateAudio(aghfp, (AGHFP_INTERNAL_CALL_MGR_CREATE_WITH_AUDIO_REQ_T *)message);
					break;
				default:
					aghfpSendCallCreateCfmToApp(aghfp, aghfp_call_create_error);
					break;
			}
			break;
			
			
		case AGHFP_INTERNAL_CALL_MGR_ANSWER_REQ:
			PRINT(("AGHFP_INTERNAL_CALL_MGR_ANSWER_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_incoming_call_establish:
					aghfpHandleCallAnswer(aghfp);
					break;
				default:
					/* Silently ignore message */
					break;
			}
			break;
			
			
		case AGHFP_INTERNAL_CALL_MGR_REMOTE_ANSWERED_REQ:
			PRINT(("AGHFP_INTERNAL_CALL_MGR_REMOTE_ANSWERED_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_outgoing_call_establish:
					aghfpHandleCallRemoteAnswered(aghfp);
					break;
				default:
					/* Silently ignore message */
					break;
			}
			break;
			
			
		case AGHFP_INTERNAL_CALL_MGR_TERMINATE_REQ:
			PRINT(("AGHFP_INTERNAL_CALL_MGR_TERMINATE_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleCallTerminate(aghfp, (AGHFP_INTERNAL_CALL_MGR_TERMINATE_REQ_T *)message);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_CALL_MGR_TERMINATE_IND, aghfp, aghfp_fail);
					break;
			}
			break;
			
			
		case AGHFP_INTERNAL_RING_REPEAT_REQ:
			PRINT(("AGHFP_INTERNAL_RING_REPEAT_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
					aghfpManageCall(aghfp, CallEventRing, CallFlagsNone);
					break;
				default:
					/* Silently ignore message */
					break;
			}
			break;
			
		
			
		/*************************************************************************/
		/* Misc Other Messages                                                   */
		/*************************************************************************/

		case AGHFP_INTERNAL_SET_CALLER_ID_DETAILS_REQ:
			PRINT(("AGHFP_INTERNAL_SET_CALLER_ID_DETAILS_REQ\n"));
			
			switch(profileState)
			{
				case aghfp_initialising:
				case aghfp_ready:
				case aghfp_slc_connecting:
				case aghfp_slc_connected:
					aghfpHandleSetCallerIdDetails(aghfp, (AGHFP_INTERNAL_SET_CALLER_ID_DETAILS_REQ_T *)message);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SET_CALLER_ID_DETAILS_CFM, aghfp, aghfp_fail);
					break;
			}
			break;
			
		case AGHFP_INTERNAL_VOICE_RECOGNITION_ENABLE:
			PRINT(("AGHFP_INTERNAL_VOICE_RECOGNITION_ENABLE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleVoiceRecognitionEnable(aghfp, ((AGHFP_INTERNAL_VOICE_RECOGNITION_ENABLE_T*)message)->enable);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_VOICE_RECOGNITION_ENABLE_CFM, aghfp, aghfp_fail);
			}
			break;

		case AGHFP_INTERNAL_SEND_PHONE_NUMBER_FOR_VOICE_TAG:
		{
			AGHFP_INTERNAL_SEND_PHONE_NUMBER_FOR_VOICE_TAG_T *data;
			data = (AGHFP_INTERNAL_SEND_PHONE_NUMBER_FOR_VOICE_TAG_T *)message;
			PRINT(("AGHFP_INTERNAL_SEND_PHONE_NUMBER_FOR_VOICE_TAG\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendPhoneNumberForVoiceTag(aghfp, data->size_number, data->number);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SEND_PHONE_NUMBER_FOR_VOICE_TAG_CFM, aghfp, aghfp_fail);
			}
			break;
		}

		case AGHFP_INTERNAL_SET_REMOTE_MICROPHONE_GAIN:
			PRINT(("AGHFP_INTERNAL_SET_REMOTE_MICROPHONE_GAIN\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSetRemoteMicrophoneGain(aghfp, ((AGHFP_INTERNAL_SET_REMOTE_MICROPHONE_GAIN_T*)message)->gain);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SET_REMOTE_MICROPHONE_GAIN_CFM, aghfp, aghfp_fail);
			}
			break;

		case AGHFP_INTERNAL_SET_REMOTE_SPEAKER_VOLUME:
			PRINT(("AGHFP_INTERNAL_SET_REMOTE_SPEAKER_VOLUME\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSetRemoteSpeakerVolume(aghfp, ((AGHFP_INTERNAL_SET_REMOTE_SPEAKER_VOLUME_T*)message)->volume);
					break;
				default:
					aghfpSendCommonCfmMessageToApp(AGHFP_SET_REMOTE_SPEAKER_VOLUME_CFM, aghfp, aghfp_fail);
			}
			break;

		case AGHFP_INTERNAL_SYNC_MIC_GAIN:
			PRINT(("AGHFP_INTERNAL_SYNC_MIC_GAIN\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSyncMicrophoneGain(aghfp, ((AGHFP_INTERNAL_SYNC_MIC_GAIN_T*)message)->gain);
					break;
				default:
					/* We've received a mic gain command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

		case AGHFP_INTERNAL_SYNC_SPEAKER_VOLUME:
			PRINT(("AGHFP_INTERNAL_SYNC_SPEAKER_VOLUME\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSyncSpeakerVolume(aghfp, ((AGHFP_INTERNAL_SYNC_SPEAKER_VOLUME_T*)message)->volume);
					break;
				default:
					/* We've received a speaker gain command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_RESPONSE_HOLD_STATE:
			PRINT(("AGHFP_INTERNAL_SEND_RESPONSE_HOLD_STATE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendResponseHoldState(aghfp, ((AGHFP_INTERNAL_SEND_RESPONSE_HOLD_STATE_T*)message)->state);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_CONFIRM_RESPONSE_HOLD_STATE:
			PRINT(("AGHFP_INTERNAL_CONFIRM_RESPONSE_HOLD_STATE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleConfirmResponseHoldState(aghfp, ((AGHFP_INTERNAL_CONFIRM_RESPONSE_HOLD_STATE_T*)message)->state);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_SUBSCRIBER_NUMBER:
			PRINT(("AGHFP_INTERNAL_SEND_SUBSCRIBER_NUMBER\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendSubscriberNumber(aghfp, (AGHFP_INTERNAL_SEND_SUBSCRIBER_NUMBER_T*)message);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_SUBSCRIBER_NUMBERS_COMPLETE:
			PRINT(("AGHFP_INTERNAL_SEND_SUBSCRIBER_NUMBERS_COMPLETE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendSubscriberNumbersComplete(aghfp);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_CURRENT_CALL:
			PRINT(("AGHFP_INTERNAL_SEND_CURRENT_CALL\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendCurrentCall(aghfp, (AGHFP_INTERNAL_SEND_CURRENT_CALL_T*)message);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_CURRENT_CALLS_COMPLETE:
			PRINT(("AGHFP_INTERNAL_SEND_CURRENT_CALLS_COMPLETE\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendCurrentCallsComplete(aghfp);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;

        case AGHFP_INTERNAL_SEND_NETWORK_OPERATOR:
        {
			AGHFP_INTERNAL_SEND_NETWORK_OPERATOR_T *msg = (AGHFP_INTERNAL_SEND_NETWORK_OPERATOR_T*)message;
			PRINT(("AGHFP_INTERNAL_SEND_NETWORK_OPERATOR\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleSendNetworkOperator(aghfp, msg->mode, msg->size_operator, msg->operator);
					break;
				default:
					/* We've received a  command from the HF, but we haven't currently
					   got an SLC. That can't really happen. If it does, then just silently ignore. */
					break;
			}
			break;
		}

        case AGHFP_INTERNAL_USER_DATA_REQ:
            PRINT(("AGHFP_INTERNAL_USER_DATA_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
					aghfpHandleUserDataReq(aghfp, (AGHFP_INTERNAL_USER_DATA_REQ_T *) message);
					break;

                case aghfp_ready:
                case aghfp_slc_connecting:
                    /* Cannot send the data, haven't got a valid SLC established */
                    aghfpHandleUserDataReqFail(aghfp, (AGHFP_INTERNAL_USER_DATA_REQ_T *) message);
                    break;

                case aghfp_initialising:
				default:
					/* We have received a request to send user data and we're in the wrong state */
					break;
			}
            break;

        case AGHFP_INTERNAL_SEND_ERROR_REQ:
            PRINT(("AGHFP_INTERNAL_SEND_ERROR_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
                case aghfp_slc_connecting:
					aghfpSendError(aghfp);
					break;

                case aghfp_ready:
                    /* Cannot send error, haven't got a valid RFCOMM established */
                    aghfpSendCommonCfmMessageToApp(AGHFP_SEND_ERROR_CFM, aghfp, aghfp_fail);
                    break;

                case aghfp_initialising:
				default:
					/* We have received this request in the wrong state */
					break;
			}
            break;

        case AGHFP_INTERNAL_SEND_OK_REQ:
            PRINT(("AGHFP_INTERNAL_SEND_OK_REQ\n"));

			switch(profileState)
			{
				case aghfp_slc_connected:
				case aghfp_incoming_call_establish:
				case aghfp_outgoing_call_establish:
				case aghfp_active_call:
                case aghfp_slc_connecting:
					aghfpSendOk(aghfp);
					break;

                case aghfp_ready:
                    /* Cannot send error, haven't got a valid RFCOMM established */
                    aghfpSendCommonCfmMessageToApp(AGHFP_SEND_OK_CFM, aghfp, aghfp_fail);
                    break;

                case aghfp_initialising:
				default:
					/* We have received this request in the wrong state */
					break;
			}
            break;

		/* WB-Speech Messages */
		case AGHFP_INTERNAL_WBS_CODEC_NEGOTIATE_REQ:
			PRINT(("AGHFP_INTERNAL_WBS_CODEC_NEGOTIATE_REQ\n"));
			aghfpHandleWbsCodecNegReq(aghfp, ((AGHFP_INTERNAL_WBS_CODEC_NEGOTIATE_REQ_T*)message)->codec);
			break;
			
		case AGHFP_INTERNAL_WBS_CODEC_CONNECTION_REQ:
			PRINT(("AGHFP_INTERNAL_WBS_CODEC_CONNECTION_REQ\n"));
			aghfpHandleWbsCodecConReq(aghfp);
			break;
				
		case AGHFP_INTERNAL_CODEC_NEGOTIATION_REQ:
			PRINT(("AGHFP_INTERNAL_CODEC_NEGOTIATION_REQ\n"));
			aghfpHandleCodecNegotiationReq(aghfp, (AGHFP_INTERNAL_CODEC_NEGOTIATION_REQ_T*)message);
			break;

		default:
			PRINT(("Unhandled message\n"));
			/* Received an unhandled message */
			handleUnexpected(aghfpUnexpectedMessage, profileState, id);
			break;
    }
}
/****************************************************************************
 All messages for this profile lib are handled by this function
*/
void aghfpProfileHandler(Task task, MessageId id, Message message)
{
	AGHFP *aghfp = (AGHFP*)task;
	aghfp_state profileState = aghfp->state;

    /*printf("%s - ", stateNames[profileState]);*/
	if (id >= AGHFP_INTERNAL_MSG_BASE && id < AGHFP_INTERNAL_MSG_TOP)
	{
		handleAghfpMessages(task, id, message);
	}
	else
	{
		/* Check the message id */
		switch (id)
		{
	       case CL_DM_SYNC_RENEGOTIATE_IND:
				PRINT(("CL_DM_SYNC_RENEGOTIATE_IND \n" ));
	       break ;
    
			/*************************************************************************/
			/* SDP Messages                                                          */
			/*************************************************************************/
	
			case CL_SDP_REGISTER_CFM:
				PRINT(("CL_SDP_REGISTER_CFM\n"));
	
				switch(profileState)
				{
					case aghfp_initialising:
					case aghfp_ready:
					case aghfp_slc_connecting:
						/* Handle the register cfm */
						aghfpHandleSdpRegisterCfm(aghfp, (CL_SDP_REGISTER_CFM_T *) message);
						break;
	
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
	        case CL_SDP_UNREGISTER_CFM:
	            switch(profileState)
				{
					case aghfp_initialising:
					case aghfp_ready:
					case aghfp_slc_connecting:
	                case aghfp_slc_connected:
	                case aghfp_incoming_call_establish:
	                case aghfp_outgoing_call_establish:
	                case aghfp_active_call:
						/* Handle the register cfm */
						aghfpHandleSdpUnregisterCfm(aghfp, (CL_SDP_UNREGISTER_CFM_T *) message);
						break;
	
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			case CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM:
				PRINT(("CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connecting:
						/* Currently we only look for attributes during SLC establishment */
						aghfpHandleServiceSearchAttributeCfm(aghfp, (CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T *) message);
						break;
	                case aghfp_slc_connected:
	                case aghfp_incoming_call_establish:
	                case aghfp_outgoing_call_establish:
	                case aghfp_active_call:
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
	
			/*************************************************************************/
			/* RFCOMM Messages                                                       */
			/*************************************************************************/
	
			case CL_RFCOMM_REGISTER_CFM:
				PRINT(("CL_RFCOMM_REGISTER_CFM\n"));
	
				switch(profileState)
				{
					case aghfp_initialising:
						aghfpHandleRfcommRegisterCfm(aghfp, (CL_RFCOMM_REGISTER_CFM_T *) message);
						break;
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			case CL_RFCOMM_CONNECT_IND:
				PRINT(("CL_RFCOMM_CONNECT_IND\n"));
	
				switch(profileState)
				{
					case aghfp_ready:
						/* Handle the connect indication */
						aghfpHandleRfcommConnectInd(aghfp, (CL_RFCOMM_CONNECT_IND_T *) message);
						break;
	
					case aghfp_slc_connecting:
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
						/*  Reject the connect request - this shouldn't happen too often as we
						unregister our service record after we establish an SLC */
						aghfpHandleSlcConnectIndReject(aghfp, (CL_RFCOMM_CONNECT_IND_T *) message);
						break;
	
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			case CL_RFCOMM_CONNECT_CFM:
				PRINT(("CL_RFCOMM_CONNECT_CFM\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connecting:
						/* Handle the connect cfm */
						aghfpHandleRfcommConnectCfm(aghfp, (CL_RFCOMM_CONNECT_CFM_T *) message);
						break;
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			case CL_RFCOMM_DISCONNECT_IND:
				PRINT(("CL_RFCOMM_DISCONNECT_IND\n"));
	
				switch(profileState)
				{
					case aghfp_ready:
					case aghfp_slc_connecting:
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
	                case aghfp_active_call:
						aghfpHandleRfcommDisconnectInd(aghfp, (CL_RFCOMM_DISCONNECT_IND_T*) message);
						break;
	
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			/*************************************************************************/
			/* Audio Connection Messages                                             */
			/*************************************************************************/
	
			case CL_DM_SYNC_CONNECT_IND:
				PRINT(("CL_DM_SYNC_CONNECT_IND\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
					case aghfp_active_call:
						aghfpHandleSyncConnectInd(aghfp, (CL_DM_SYNC_CONNECT_IND_T *)message);
						break;
					default:
						/* Reject request outright because we are in the wrong state */
						aghfpHandleSyncConnectIndReject(aghfp, (CL_DM_SYNC_CONNECT_IND_T *)message);
						break;
				}
				break;
	
			case CL_DM_SYNC_CONNECT_CFM:
				PRINT(("CL_DM_SYNC_CONNECT_CFM\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
					case aghfp_active_call:
						aghfpHandleSyncConnectCfm(aghfp, (CL_DM_SYNC_CONNECT_CFM_T *)message);
						break;
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
			case CL_DM_SYNC_DISCONNECT_IND:
				PRINT(("CL_DM_SYNC_DISCONNECT_IND\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
					case aghfp_active_call:
						aghfpHandleSyncDisconnectInd(aghfp, (CL_DM_SYNC_DISCONNECT_IND_T *)message);
						break;
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
				
				
			/*************************************************************************/
			/* Misc Other Messages                                                   */
			/*************************************************************************/
	
			case MESSAGE_MORE_DATA:
				PRINT(("MESSAGE_MORE_DATA\n"));
	
				switch(profileState)
				{
					case aghfp_slc_connecting:
					case aghfp_slc_connected:
					case aghfp_incoming_call_establish:
					case aghfp_outgoing_call_establish:
					case aghfp_active_call:
						/* We have received more data into the RFCOMM buffer */
						aghfpHandleReceivedData(aghfp, ((MessageMoreData *) message)->source);
						break;
	
					default:
						/* Panic in debug and ignore in release lib variants */
						handleUnexpected(aghfpUnexpectedClPrim, profileState, id);
						break;
				}
				break;
	
	        case CL_DM_SYNC_REGISTER_CFM:
	            PRINT(("CL_DM_SYNC_REGISTER_CFM\n"));
	
				switch(profileState)
				{
	            case aghfp_initialising:
	            case aghfp_ready:
	            case aghfp_slc_connecting:
	            case aghfp_slc_connected:
	            case aghfp_incoming_call_establish:
	            case aghfp_outgoing_call_establish:
	            case aghfp_active_call:
	                /* Registered for Synchronous connection indications */
	    			aghfpHandleSyncRegisterCfm(aghfp);
	                break;
	
	            default:
	                break;
	            }
	            break;
	
	
	        /* Unhandled messages */
            case CL_SM_ENCRYPTION_CHANGE_IND:
			case CL_SM_ENCRYPTION_KEY_REFRESH_IND:
	        case MESSAGE_MORE_SPACE:
	        case MESSAGE_SOURCE_EMPTY:
	            break;
	
	
			default:
				PRINT(("Unhandled message\n"));
				/* Received an unhandled message */
				handleUnexpected(aghfpUnexpectedMessage, profileState, id);
				break;
		}
	}
}
void a2dpProfileHandler(Task task, MessageId id, Message message)
{
    switch (id)
    {
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 0:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 1:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 2:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 3:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 4:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 5:
    case A2DP_INTERNAL_L2CAP_CONNECT_REQ + 6:
        PRINT(("A2DP_INTERNAL_L2CAP_CONNECT_REQ\n"));
        a2dpHandleL2capConnectReq((A2DP_INTERNAL_L2CAP_CONNECT_REQ_T *) message);
        break;
    
    case A2DP_INTERNAL_SIGNALLING_CONNECT_REQ:
        PRINT(("A2DP_INTERNAL_SIGNALLING_CONNECT_REQ\n"));
        a2dpHandleSignallingConnectReq((A2DP_INTERNAL_SIGNALLING_CONNECT_REQ_T *) message);
        break;

    case A2DP_INTERNAL_SIGNALLING_CONNECT_RES:
        PRINT(("A2DP_INTERNAL_SIGNALLING_CONNECT_RES\n"));
        a2dpHandleSignallingConnectRes((A2DP_INTERNAL_SIGNALLING_CONNECT_RES_T *) message);
        break;

    case A2DP_INTERNAL_SIGNALLING_DISCONNECT_REQ:
        PRINT(("A2DP_INTERNAL_SIGNALLING_DISCONNECT_REQ\n"));
        a2dpHandleSignallingDisconnectReq((A2DP_INTERNAL_SIGNALLING_DISCONNECT_REQ_T *) message);
        break;

    case A2DP_INTERNAL_CODEC_CONFIGURE_RSP:
        PRINT(("A2DP_INTERNAL_CODEC_CONFIGURE_RSP\n"));
        a2dpHandleCodecConfigureResponse((A2DP_INTERNAL_CODEC_CONFIGURE_RSP_T *) message);
        break;

    case A2DP_INTERNAL_MEDIA_OPEN_REQ:
        PRINT(("A2DP_INTERNAL_OPEN_REQ\n"));
        a2dpStreamEstablish((A2DP_INTERNAL_MEDIA_OPEN_REQ_T *) message);
        break;

    case A2DP_INTERNAL_MEDIA_OPEN_RES:
        PRINT(("A2DP_INTERNAL_OPEN_RES\n"));
        a2dpStreamOpenResponse((A2DP_INTERNAL_MEDIA_OPEN_RES_T *) message);
        break;

    case A2DP_INTERNAL_MEDIA_START_REQ:
        PRINT(("A2DP_INTERNAL_START_REQ\n"));
        a2dpStreamStart((A2DP_INTERNAL_MEDIA_START_REQ_T *) message);
        break;
        
    case A2DP_INTERNAL_MEDIA_START_RES:
        PRINT(("A2DP_INTERNAL_START_RES\n"));
        a2dpStreamStartResponse((A2DP_INTERNAL_MEDIA_START_RES_T *) message);
        break;
        
    case A2DP_INTERNAL_MEDIA_SUSPEND_REQ:
        PRINT(("A2DP_INTERNAL_MEDIA_SUSPEND_REQ\n"));
        a2dpStreamSuspend((A2DP_INTERNAL_MEDIA_SUSPEND_REQ_T *) message);
        break;

    case A2DP_INTERNAL_MEDIA_CLOSE_REQ:
        PRINT(("A2DP_INTERNAL_MEDIA_CLOSE_REQ\n"));
        a2dpStreamRelease((A2DP_INTERNAL_MEDIA_CLOSE_REQ_T *) message);
        break;
        
    case A2DP_INTERNAL_MEDIA_RECONFIGURE_REQ:
        PRINT(("A2DP_INTERNAL_MEDIA_RECONFIGURE_REQ\n"));
        a2dpStreamReconfigure((A2DP_INTERNAL_MEDIA_RECONFIGURE_REQ_T *) message);
        break;
        
    case A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_REQ:
        PRINT(("A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_REQ\n"));
        a2dpStreamDelayReport(((A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_REQ_T *)message)->device, ((A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_REQ_T *)message)->delay);
        break;
        
    case A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_RES:
        PRINT(("A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_RES\n"));
        a2dpStreamDelayReport(((A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_RES_T *)message)->device, ((A2DP_INTERNAL_MEDIA_AV_SYNC_DELAY_RES_T *)message)->delay);
        break;
        
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 0:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 1:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 2:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 3:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 4:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 5:
    case A2DP_INTERNAL_LINKLOSS_TIMEOUT_BASE + 6:
        PRINT(("A2DP_INTERNAL_LINKLOSS_TIMEOUT\n"));
        a2dpHandleL2capLinklossTimeout(id);
        break;
    
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 0:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 1:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 2:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 3:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 4:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 5:
    case A2DP_INTERNAL_CLIENT_RSP_TIMEOUT_BASE + 6:
        PRINT(("A2DP_INTERNAL_CLIENT_RSP_TIMEOUT\n"));
        a2dpHandleInternalClientRspTimeout(id);
        break;
    
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 0:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 1:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 2:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 3:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 4:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 5:
    case A2DP_INTERNAL_REMOTE_CMD_TIMEOUT_BASE + 6:
        PRINT(("A2DP_INTERNAL_REMOTE_CMD_TIMEOUT\n"));
        a2dpHandleInternalRemoteCmdTimeout(id);
        break;
    
    case A2DP_INTERNAL_WATCHDOG_BASE + 0:
    case A2DP_INTERNAL_WATCHDOG_BASE + 1:
    case A2DP_INTERNAL_WATCHDOG_BASE + 2:
    case A2DP_INTERNAL_WATCHDOG_BASE + 3:
    case A2DP_INTERNAL_WATCHDOG_BASE + 4:
    case A2DP_INTERNAL_WATCHDOG_BASE + 5:
    case A2DP_INTERNAL_WATCHDOG_BASE + 6:
        PRINT(("A2DP_INTERNAL_WATCHDOG_IND\n"));
        a2dpHandleInternalWatchdogTimeout(id);
        break;

#if 0
    case A2DP_INTERNAL_RECONFIGURE_REQ:
        PRINT(("A2DP_INTERNAL_RECONFIGURE_REQ\n"));
        a2dpHandleReconfigureReq((A2DP_INTERNAL_RECONFIGURE_REQ_T *) message);
        break;
#endif

#if 0
    case A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ:
        PRINT(("A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ\n"));
        a2dpSendCodecAudioParams();
        if (((A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ_T *) message)->send_reconfigure_message)
            a2dpSendReconfigureCfm(a2dp_success);
        break;
#endif

#if 0
    case A2DP_INTERNAL_GET_CAPS_TIMEOUT_IND:
        PRINT(("A2DP_INTERNAL_GET_CAPS_TIMEOUT_IND\n"));
        a2dpGetCapsTimeout();
        break;
#endif

    default:
        switch(id)
        {
        case CL_SDP_REGISTER_CFM:
            PRINT(("CL_SDP_REGISTER_CFM\n"));
            a2dpHandleSdpRegisterCfm((CL_SDP_REGISTER_CFM_T *) message);
            break;

        case CL_L2CAP_REGISTER_CFM:
            PRINT(("CL_L2CAP_REGISTER_CFM\n"));
            a2dpHandleL2capRegisterCfm((CL_L2CAP_REGISTER_CFM_T *) message);
            break;

        case CL_L2CAP_CONNECT_IND:
            PRINT(("CL_L2CAP_CONNECT_IND\n"));
            a2dpHandleL2capConnectInd((CL_L2CAP_CONNECT_IND_T *) message);
            break;

        case CL_L2CAP_CONNECT_CFM:
            PRINT(("CL_L2CAP_CONNECT_CFM\n"));
            a2dpHandleL2capConnectCfm((CL_L2CAP_CONNECT_CFM_T *) message);
            break;

        case CL_L2CAP_DISCONNECT_IND:
            PRINT(("CL_L2CAP_DISCONNECT_IND\n"));
            a2dpHandleL2capDisconnect(((CL_L2CAP_DISCONNECT_IND_T *)message)->identifier, ((CL_L2CAP_DISCONNECT_IND_T *)message)->sink, ((CL_L2CAP_DISCONNECT_IND_T *)message)->status);
            ConnectionL2capDisconnectResponse(((CL_L2CAP_DISCONNECT_IND_T *)message)->identifier, ((CL_L2CAP_DISCONNECT_IND_T *)message)->sink);
            break;

        case CL_L2CAP_DISCONNECT_CFM:
            PRINT(("CL_L2CAP_DISCONNECT_CFM\n"));
            a2dpHandleL2capDisconnect(0, ((CL_L2CAP_DISCONNECT_CFM_T *)message)->sink, ((CL_L2CAP_DISCONNECT_CFM_T *)message)->status);
            break;

        case CL_SM_ENCRYPTION_CHANGE_IND:
            PRINT(("CL_SM_ENCRYPTION_CHANGE_IND\n"));
            /* We have received an indication that the encryption status of the sink has changed */
            sendEncryptionChangeInd((CL_SM_ENCRYPTION_CHANGE_IND_T *) message);
            break;

        case CL_DM_ROLE_CFM:
        case CL_SM_ENCRYPTION_KEY_REFRESH_IND:
        case CL_L2CAP_TIMEOUT_IND:
            break;

        case CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM:
            a2dpHandleSdpServiceSearchAttributeCfm ((CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T *)message);
            break;

        default:
            switch(id)
            {
            case MESSAGE_MORE_DATA:
                PRINT(("MESSAGE_MORE_DATA\n"));
                /* Data has arrived on the signalling channel */
                a2dpHandleSignalPacket( a2dpFindDeviceFromSink( StreamSinkFromSource(((MessageMoreData *)message)->source) ));
                break;

            case MESSAGE_MORE_SPACE:
            case MESSAGE_STREAM_DISCONNECT:
            case MESSAGE_SOURCE_EMPTY:
                break;

            default:
                handleUnexpected(id);
                break;
            }
        }
    }
}
void sppProfileHandler(Task task, MessageId id, Message message)
{
	SPP* spp = (SPP*) task;
	sppState profileState = spp->state;

	/* Check the message id */
    switch (id)
    {
    case SPP_INTERNAL_TASK_INIT_REQ:
        {
            uint16 app = (*((uint16 *)message));
            sppInitTaskData(spp, 0, 0, (Task) app, sppReady, 0, 0, 0, 0, 1);
        }
        break;

    case SPP_INTERNAL_TASK_DELETE_REQ:
        sppHandleFreeSppTask(spp);
        break;

    case SPP_INTERNAL_INIT_REQ:
        switch(profileState)
        {
        case sppInitialising:
            sppHandleInternalInitReq(spp, (SPP_INTERNAL_INIT_REQ_T *) message);
            break;

        case sppReady:
        case sppSearching:
        case sppConnecting:
        case sppConnected:
        default:
            handleUnexpected(sppUnexpectedSppPrim, profileState, id);
        }
        break;

    case CL_RFCOMM_REGISTER_CFM:
        switch(profileState)
        {
        case sppInitialising:
            sppHandleRfcommRegisterCfm(spp, (CL_RFCOMM_REGISTER_CFM_T *) message);
            break;
            
        case sppReady:
        case sppSearching:
        case sppConnecting:
        case sppConnected:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
        break;

    case CL_SDP_REGISTER_CFM:
        switch(profileState)
        {
        case sppInitialising:
            sppHandleSdpRegisterCfm(spp, (CL_SDP_REGISTER_CFM_T *) message);
            break;
            
        case sppReady:
        case sppSearching:
        case sppConnecting:
            sppHandleSdpRegisterCfmReady(spp, (CL_SDP_REGISTER_CFM_T *) message);
            break;
            
        case sppConnected:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

	case CL_SDP_UNREGISTER_CFM:
        switch(profileState)
        {
        case sppReady:
        case sppConnected:
            sppHandleSdpUnregisterCfm(spp, (CL_SDP_UNREGISTER_CFM_T *) message);
            break;
            
        case sppInitialising:		
        case sppSearching:
        case sppConnecting:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

    case SPP_INTERNAL_CONNECT_REQ:
        switch(profileState)
        {
        case sppReady:
            sppHandleConnectRequest(spp, (SPP_INTERNAL_CONNECT_REQ_T *) message);
            break;
            
        case sppSearching:
        case sppConnecting:
        case sppConnected:	
            sppSendConnectCfmToApp(spp_connect_failed_busy, spp);
            break;
            
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedSppPrim, profileState, id);
            break;
        }
		break;

	case SPP_INTERNAL_CONNECT_RES:
        switch(profileState)
        {
        case sppConnecting:
            sppHandleConnectResponse(spp, (SPP_INTERNAL_CONNECT_RES_T *) message);
            break;
            
        case sppReady:
        case sppSearching:
        case sppConnected:	
            sppSendConnectCfmToApp(spp_connect_failed_busy, spp);
            break;
            
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedSppPrim, profileState, id);
            break;
        }
		break;

	case CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM:
        switch(profileState)
        {
        case sppSearching:
            sppHandleSdpServiceSearchAttributeCfm(spp, (CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T *) message);
            break;
            
        case sppReady:
        case sppConnecting:
        case sppConnected:
            break;
            
        case sppInitialising:		
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

	case SPP_INTERNAL_RFCOMM_CONNECT_REQ:
        switch(profileState)
        {
        case sppConnecting:
            sppHandleInternalRfcommConnectRequest(spp, (SPP_INTERNAL_RFCOMM_CONNECT_REQ_T *) message);
            break;
            
        case sppReady:
        case sppSearching:
        case sppInitialising:
        case sppConnected:	
        default:
            handleUnexpected(sppUnexpectedSppPrim, profileState, id);
            break;
        }
		break;

	case CL_RFCOMM_CONNECT_CFM:
        switch(profileState)
        {
        case sppConnecting: 
            sppHandleRfcommConnectCfm(spp, (CL_RFCOMM_CONNECT_CFM_T *) message);
            break;
            
        case sppConnected:	
        case sppReady:
        case sppSearching:
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

	case CL_RFCOMM_CONNECT_IND:
        switch(profileState)
        {
        case sppReady:
            sppHandleRfcommConnectInd(spp, (CL_RFCOMM_CONNECT_IND_T *) message);
            break;
            
        case sppSearching:
        case sppConnecting:
        case sppConnected:	
            sppHandleConnectIndReject(spp, (CL_RFCOMM_CONNECT_IND_T *) message);
            break;
            
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

	case CL_RFCOMM_DISCONNECT_IND:
        switch(profileState)
        {
        case sppReady:
        case sppSearching:
        case sppConnecting:
        case sppConnected:
            sppHandleRfcommDisconnectInd(spp, (CL_RFCOMM_DISCONNECT_IND_T *) message);
            break;
            
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedClPrim, profileState, id);
            break;
        }
		break;

	case SPP_INTERNAL_DISCONNECT_REQ:
        switch(profileState)
        {
        case sppConnecting:
        case sppConnected:
            sppHandleInternalDisconnectReq(spp);
            break;
            
        case sppReady:
        case sppSearching:
            sppSendDisconnectIndToApp(spp, spp_disconnect_no_slc);
            break;
            
        case sppInitialising:
        default:
            handleUnexpected(sppUnexpectedSppPrim, profileState, id);
            break;
        }
		break;

	case CL_RFCOMM_CONTROL_IND:
        { 
            /* Forward the Modem Control Indicators */
            CL_RFCOMM_CONTROL_IND_T *src_msg;
            CL_RFCOMM_CONTROL_IND_T *ind = PanicUnlessNew(CL_RFCOMM_CONTROL_IND_T);
            src_msg = (CL_RFCOMM_CONTROL_IND_T *)message;
            *ind = *src_msg;    	    
            MessageSend(spp->clientTask, CL_RFCOMM_CONTROL_IND, ind);
        }
		break;
        
    case MESSAGE_MORE_DATA:
        {
            SPP_MESSAGE_MORE_DATA_T* msg = PanicUnlessNew(SPP_MESSAGE_MORE_DATA_T);
            msg->source = ((MessageMoreData*)message)->source;
            msg->spp = spp;
            MessageSend(spp->clientTask, SPP_MESSAGE_MORE_DATA, msg);
        }
        break;
        
    case MESSAGE_MORE_SPACE:
        {
            SPP_MESSAGE_MORE_SPACE_T* msg = PanicUnlessNew(SPP_MESSAGE_MORE_SPACE_T);
            msg->sink = ((MessageMoreSpace*)message)->sink;
            msg->spp = spp;
            MessageSend(spp->clientTask, SPP_MESSAGE_MORE_SPACE, msg);
        }
		break;
		
	case SPP_INTERNAL_SEND_CFM_TO_APP:
		{
			SPP_INTERNAL_SEND_CFM_TO_APP_T* msg = (SPP_INTERNAL_SEND_CFM_TO_APP_T*)message;
			sppSendConnectCfmToApp(msg->status, msg->spp);
		}
		break;
		
		/* Ignored messages */
	case MESSAGE_STREAM_DISCONNECT:
	case MESSAGE_SOURCE_EMPTY:
		break;
		
	default:
		/* Received an unknown message */
		SPP_DEBUG(("spp profile handler - msg type  not yet handled 0x%x\n", id));
		break;
	}
}
void a2dpProfileHandler(Task task, MessageId id, Message message)
{
    A2DP *a2dp = (A2DP *) task;
	uint16 app;

    switch (id)
    {
	case A2DP_INTERNAL_TASK_INIT_REQ:
		PRINT(("A2DP_INTERNAL_TASK_INIT_REQ\n"));
		app = (*((uint16 *)message));
		a2dpInitTask(a2dp, (Task) app);
		break;

	case A2DP_INTERNAL_CONNECT_SIGNALLING_REQ:
		PRINT(("A2DP_INTERNAL_CONNECT_SIGNALLING_REQ\n"));
		a2dpHandleConnectSignallingReq(a2dp, (A2DP_INTERNAL_CONNECT_SIGNALLING_REQ_T *) message);
		break;

	case A2DP_INTERNAL_CONNECT_SIGNALLING_RES:
		PRINT(("A2DP_INTERNAL_CONNECT_SIGNALLING_RES\n"));
		a2dpHandleConnectSignallingRes(a2dp, (A2DP_INTERNAL_CONNECT_SIGNALLING_RES_T *) message);
		break;

	case A2DP_INTERNAL_CONFIGURE_CODEC_RSP:
		PRINT(("A2DP_INTERNAL_CONFIGURE_CODEC_RSP\n"));
		a2dpHandleConfigureCodecRes(a2dp, (A2DP_INTERNAL_CONFIGURE_CODEC_RSP_T *) message);
		break;

    case A2DP_INTERNAL_OPEN_REQ:
        PRINT(("A2DP_INTERNAL_OPEN_REQ\n"));
        a2dpHandleOpenReq(a2dp, (A2DP_INTERNAL_OPEN_REQ_T *) message);
        break;

	case A2DP_INTERNAL_TASK_DELETE_REQ:
		PRINT(("A2DP_INTERNAL_TASK_DELETE_REQ\n"));
        a2dpHandleDeleteTask(a2dp);
        break;

	case A2DP_INTERNAL_WATCHDOG_IND:
		PRINT(("A2DP_INTERNAL_WATCHDOG_IND\n"));
		a2dpHandleWatchdogInd(a2dp);
		break;

	case A2DP_INTERNAL_SIGNAL_PACKET_IND:
		PRINT(("A2DP_INTERNAL_SIGNAL_PACKET_IND\n"));
		a2dpHandleNewSignalPacket(a2dp);
		break;

	case A2DP_INTERNAL_SIGNAL_CONNECTION_TIMEOUT_IND:
		PRINT(("A2DP_INTERNAL_SIGNAL_CONNECTION_TIMEOUT_IND\n"));
		a2dpHandleSignalConnectionTimeoutInd(a2dp);
		break;

	case A2DP_INTERNAL_RECONFIGURE_REQ:
		PRINT(("A2DP_INTERNAL_RECONFIGURE_REQ\n"));
		a2dpHandleReconfigureReq(a2dp, (A2DP_INTERNAL_RECONFIGURE_REQ_T *) message);
		break;

	case A2DP_INTERNAL_START_REQ:
		PRINT(("A2DP_INTERNAL_START_REQ\n"));
		a2dpHandleStartReq(a2dp);
		break;

	case A2DP_INTERNAL_SUSPEND_REQ:
		PRINT(("A2DP_INTERNAL_SUSPEND_REQ\n"));
		a2dpHandleSuspendReq(a2dp);
		break;

	case A2DP_INTERNAL_CLOSE_REQ:
		PRINT(("A2DP_INTERNAL_CLOSE_REQ\n"));
		a2dpHandleCloseReq(a2dp);
		break;

	case A2DP_INTERNAL_DISCONNECT_ALL_REQ:
		PRINT(("A2DP_INTERNAL_DISCONNECT_ALL_REQ\n"));
		a2dpHandleDisconnectAllReq(a2dp);
		break;

	case A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ:
		PRINT(("A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ\n"));
		a2dpSendCodecAudioParams(a2dp);
		if (((A2DP_INTERNAL_SEND_CODEC_PARAMS_REQ_T *) message)->send_reconfigure_message)
			a2dpSendReconfigureCfm(a2dp, a2dp_success);
		break;

	case A2DP_INTERNAL_GET_CAPS_TIMEOUT_IND:
		PRINT(("A2DP_INTERNAL_GET_CAPS_TIMEOUT_IND\n"));
		a2dpGetCapsTimeout(a2dp);
		break;


	default:
		switch(id)
		{
		case CL_SDP_REGISTER_CFM:
			PRINT(("CL_SDP_REGISTER_CFM\n"));				
			a2dpHandleSdpRegisterCfm(a2dp, (CL_SDP_REGISTER_CFM_T *) message);		
			break;

		case CL_L2CAP_REGISTER_CFM:
			PRINT(("CL_L2CAP_REGISTER_CFM\n"));
			a2dpHandleL2capRegisterCfm(a2dp, (CL_L2CAP_REGISTER_CFM_T *) message);
			break;

		case CL_L2CAP_CONNECT_IND:
			PRINT(("CL_L2CAP_CONNECT_IND\n"));
			a2dpHandleL2capConnectInd(a2dp, (CL_L2CAP_CONNECT_IND_T *) message);
			break;

		case CL_L2CAP_CONNECT_CFM:
			PRINT(("CL_L2CAP_CONNECT_CFM\n"));
			a2dpHandleL2capConnectCfm(a2dp, (CL_L2CAP_CONNECT_CFM_T *) message);
			break;

		case CL_L2CAP_DISCONNECT_IND:
			PRINT(("CL_L2CAP_DISCONNECT_IND\n"));
			a2dpHandleL2capDisconnectInd(a2dp, (CL_L2CAP_DISCONNECT_IND_T *) message);
			break;

		case CL_SM_ENCRYPTION_CHANGE_IND:
			PRINT(("CL_SM_ENCRYPTION_CHANGE_IND\n"));
			/* We have received an indication that the encryption status of the sink has changed */
			sendEncryptionChangeInd(a2dp, (CL_SM_ENCRYPTION_CHANGE_IND_T *) message);
			break;

		case CL_DM_ROLE_CFM:
			break;

		default:
			switch(id)
			{
			case MESSAGE_MORE_DATA:
				PRINT(("MESSAGE_MORE_DATA\n"));
				/* Data has arrived on the signalling channel */
				a2dpHandleSignalPacket(a2dp, ((MessageMoreData *) message)->source);
				break;

			case MESSAGE_MORE_SPACE:
			case MESSAGE_STREAM_DISCONNECT:
			case MESSAGE_SOURCE_EMPTY:
				break;

			default:
				handleUnexpected(id);
				break;
			}
		}
    }
}