⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 av_headset_sep.c

📁 csr 蓝牙芯片 无线蓝牙耳机的嵌入式程序
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2004

FILE NAME
    av_headset_sep.c        

DESCRIPTION
    
NOTES

*/


/****************************************************************************
    Header files
*/
#include "av_headset_private.h"
#include "av_headset_sep.h"
#include "av_headset_sep.h"
#include "av_headset_kalimba.h"
#include "av_headset_service_record.h"
#include "av_headset_scan.h"

#include <bdaddr.h>
#include <stdlib.h>
#include <ps.h>
#include <panic.h>

/*************************************************************************
NAME    
     avHeadsetRegisterSep
    
DESCRIPTION
     This function is called to register the required Stream Endpoints
     into the GAVDP SEP database

RETURNS
     
*/
void avHeadsetRegisterSep(const avTaskData *theAvApp, a2dp_sep_type sep)
{
	a2dp_sep_config config;    
	config.sep_type = sep;
	config.params = 0;
    	
    A2dpAddSep(theAvApp->a2dp, &config);
}


/*************************************************************************
NAME    
     avHeadsetHandleSdpRegisterCfm
    
DESCRIPTION
     This function is called to once a SDP service has been registered

RETURNS
     
*/
void avHeadsetHandleSdpRegisterCfm(avTaskData *theAvApp, const CL_SDP_REGISTER_CFM_T *cfm)
{
    if(cfm->status == success)
    {
        avrcp_init_params config;
        config.device_type = avrcp_controller;
        config.priority = 50;

        /* Go ahead and Initialise the AVRCP library */
        AvrcpInit(&theAvApp->task, &config);
        
        /* Set COD */
        ConnectionWriteClassOfDevice(AV_MAJOR_DEVICE_CLASS | AV_MINOR_HEADPHONES | AV_COD_RENDER);
            
        /* Permit browsing of SDP service records without pairing */
        ConnectionSmSetSdpSecurityIn(TRUE);
            
        /* Change to Ready state */
        avHeadsetSetA2dpState(avHeadsetA2dpReady);
    }
    else
    {
        DEBUG(("Failed to register service record\n"));
    }
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpAddSepCfm
    
DESCRIPTION
     This function is called to when confirmation that the stream endpoints have been 
     registered

RETURNS
     
*/
void avHeadsetHandleA2dpAddSepCfm(avTaskData *theAvApp, const A2DP_ADD_SEP_CFM_T *cfm)
{
    if(cfm->status == a2dp_success)
    {
		/* Wait for both SEPs to be registered before proceeding */
		if (cfm->sep_type == a2dp_sbc)
		{
			/* Register service record */
			ConnectionRegisterServiceRecord(&theAvApp->task, sizeof(av_headset_service_record), av_headset_service_record);
        
			/* Make connectable */
			headsetEnableConnectable(theAvApp);
		}
    }
    else
    {
        DEBUG(("Unable to register SEPs\n"));
	Panic();
    }
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpOpenInd
    
DESCRIPTION
     This function is called on receipt of an A2DP_OPEN_IND message indicating
     that the SEP has opened

RETURNS
     
*/
void avHeadsetHandleA2dpOpenInd(avTaskData *theAvApp, const A2DP_OPEN_IND_T *ind)
{   
    bdaddr bdaddr_ind;
    
    (void) SinkGetBdAddr(ind->media_sink, &bdaddr_ind);
    DEBUG(("Bdaddr from Sink: 0x%lx / 0x%x / 0x%x\n",bdaddr_ind.lap,bdaddr_ind.uap,bdaddr_ind.nap));

    /* We are now connected */
    avHeadsetSetA2dpState(avHeadsetA2dpConnected);
    headsetTestForConnectablilty(theAvApp);
    
    if (BdaddrIsZero(&theAvApp->remote_bdaddr))
    {
        theAvApp->remote_bdaddr = bdaddr_ind;
        (void) PsStore(0, &theAvApp->remote_bdaddr, sizeof(bdaddr));
    }
    else
    {
        /* if this is not the source we expect, disconnect */
        if (!BdaddrIsSame(&theAvApp->remote_bdaddr, &bdaddr_ind))
        {
            A2dpClose(theAvApp->a2dp, ind->media_sink);
         
			if (theAvApp->avrcp_state == avHeadsetAvrcpConnected)
                AvrcpDisconnect(theAvApp->avrcp);
        }
    }
    
    /* Store the media sink and source id*/
    theAvApp->media_sink = ind->media_sink;
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpOpenCfm
    
DESCRIPTION
     This function is called on receipt of an A2DP_OPEN_CFM message indicating
     that the SEP has opened

RETURNS
     
*/
void avHeadsetHandleA2dpOpenCfm(avTaskData *theAvApp, const A2DP_OPEN_CFM_T *cfm)
{
    if (cfm->result == a2dp_success)
    {
        A2dpStart(theAvApp->a2dp, cfm->media_sink);    
     
		/* Update the local state */
		avHeadsetSetA2dpState(avHeadsetA2dpConnected);

		/* Store the media sink and source id */
        theAvApp->media_sink = cfm->media_sink;

        headsetTestForConnectablilty(theAvApp);
    }
    else
    {
        avHeadsetSetA2dpState(avHeadsetA2dpReady);  
    }
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpCodecSettingsInd
    
DESCRIPTION
     Handle the receipt of new codec settings.

RETURNS
     
*/
void avHeadsetHandleA2dpCodecSettingsInd(avTaskData *theAvApp, const A2DP_CODEC_SETTINGS_IND_T *ind)
{
    /* Store the codec config settings */
	theAvApp->channel_mode = ind->channel_mode;
	theAvApp->rate = ind->rate;
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpStartInd
    
DESCRIPTION
     This function is called on receipt of an A2DP_START_IND message 
     indicating that we are about to start streaming

RETURNS
     
*/
void avHeadsetHandleA2dpStartInd(avTaskData *theAvApp)
{
    avHeadsetStartKalimba(theAvApp);
    
    /* Change to streaming state */
    avHeadsetSetA2dpState(avHeadsetA2dpStreaming);
            
    /* Cancel pairing mode */
    (void) MessageCancelAll(getAppTask(), PAIR_MODE_END);
    MessageSend(getAppTask(), PAIR_MODE_END, 0);
    
	/* Establish AVRCP connection not active */
	if (theAvApp->avrcp_state == avHeadsetAvrcpReady)
	{
	    /* Change to connecting state for AVRCP */
	    avHeadsetSetAvrcpState(avHeadsetAvrcpConnecting);

		AvrcpConnect(theAvApp->avrcp, &theAvApp->remote_bdaddr);
	}
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpStartCfm
    
DESCRIPTION
     This function is called on receipt of an A2DP_START_CFM message indicating
     the result of our request to start streaming

RETURNS
     
*/
void avHeadsetHandleA2dpStartCfm(avTaskData *theAvApp, const A2DP_START_CFM_T *cfm)
{
    if (cfm->result == a2dp_success)
    {
        /* we are now connected */
        avHeadsetSetA2dpState(avHeadsetA2dpStreaming);

        /* start Kalimba decoding */
        avHeadsetStartKalimba(theAvApp);
    }
    else
    {
        /* Failed to start, close the connection */
        A2dpClose(theAvApp->a2dp, cfm->media_sink);

		/* If we have an AVRCP connection, disconnect it */
        if (theAvApp->avrcp_state == avHeadsetAvrcpConnected)
            AvrcpDisconnect(theAvApp->avrcp);
    }
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpSuspendInd
    
DESCRIPTION
     This function is called on receipt of an A2DP_SUSPEND_IND message 

RETURNS
     
*/
void avHeadsetHandleA2dpSuspendInd(void)
{
    /* Change to connected state */
    avHeadsetSetA2dpState(avHeadsetA2dpConnected);
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpCloseInd
    
DESCRIPTION
     This function is called on receipt of an A2DP_CLOSE_IND message 

RETURNS
     
*/
void avHeadsetHandleA2dpCloseInd(avTaskData* theAvApp)
{
    /* Stop the media stream */
    avHeadsetStopKalimba(theAvApp);

    /* Change to ready state */
    avHeadsetSetA2dpState(avHeadsetA2dpReady);
    headsetTestForConnectablilty(theAvApp);
    
    if (theAvApp->avrcp_state == avHeadsetAvrcpConnected)
    {
        /* Change AVRCP to disconnecting state */
        avHeadsetSetAvrcpState(avHeadsetAvrcpDisconnecting);
	    /* Disconnect the AVRCP connection */
        AvrcpDisconnect(theAvApp->avrcp);
    }
}


/*************************************************************************
NAME    
     avHeadsetHandleA2dpCloseCfm
    
DESCRIPTION
     This function is called on receipt of an A2DP_CLOSE_CFM message 

RETURNS
     
*/
void avHeadsetHandleA2dpCloseCfm(avTaskData *theAvApp)
{
    /* Stop the media stream */
    avHeadsetStopKalimba(theAvApp);

    /* Change to ready state */
    avHeadsetSetA2dpState(avHeadsetA2dpReady);

	/* Check if we need to become connectable */
    headsetTestForConnectablilty(theAvApp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -