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

📄 avrcp_handler.c

📁 实现蓝牙立体声耳机功能。。可以接收并播放来自有蓝牙功能的手机
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2004-2006
Part of BlueLab 3.6.2-release

FILE NAME
    avrcp_handler.c        

DESCRIPTION
    Deals with AVRCP connection.

NOTES

*/

/****************************************************************************
    Header files
*/
#include "headset_private.h"
#include "avrcp_handler.h"
#include "headset_common.h"
#include "headset_power.h"

#include <stdlib.h>


#ifdef DEBUG_AVRCP
#define AVRCP_DEBUG(x) DEBUG(x)
#else
#define AVRCP_DEBUG(x) 
#endif


enum
{
    PAUSE_PRESS,
    PAUSE_RELEASE,
    PLAY_PRESS,
    PLAY_RELEASE,
    FORWARD_PRESS,
    FORWARD_RELEASE,
    BACKWARD_PRESS,
    BACKWARD_RELEASE,
    STOP_PRESS,
    STOP_RELEASE,
    FF_PRESS,
    FF_RELEASE,
    REW_PRESS,
    REW_RELEASE
};


/* 
    Send remote control command to remote device 
*/
static void sendAVRCP(headsetTaskData* app, avc_operation_id op_id, uint8 state)
{
    app->avrcp_pending = TRUE;
    
    /* Send a key press */
    AvrcpPassthrough(app->avrcp, subunit_panel, 0, state, op_id, 0, 0);   
}

/**************************************************************************/
void controls_handler(Task task, MessageId id, Message message)
{
	task = task;
	message = message;

    switch (id)
    {
        case PAUSE_PRESS:
        AVRCP_DEBUG(("AVRCP: Sending Pause Pressed\n"));
            sendAVRCP(getApp(), opid_pause, 0);
            break;

        case PAUSE_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending Pause Released\n"));
            sendAVRCP(getApp(), opid_pause, 1);
            break;

        case PLAY_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending Play Pressed\n"));
            sendAVRCP(getApp(), opid_play, 0);
            break;

        case PLAY_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending Play Released\n"));
            sendAVRCP(getApp(), opid_play, 1);
            break;

        case FORWARD_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending Forward Pressed\n"));
            sendAVRCP(getApp(), opid_forward, 0);
            break;

        case FORWARD_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending Forward Released\n"));
            sendAVRCP(getApp(), opid_forward, 1);
            break;

        case BACKWARD_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending Backward Pressed\n"));
            sendAVRCP(getApp(), opid_backward, 0);
            break;

        case BACKWARD_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending Backward Released\n"));
            sendAVRCP(getApp(), opid_backward, 1);
            break;

        case STOP_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending Stop Pressed\n"));
            sendAVRCP(getApp(), opid_stop, 0);
            break;

        case STOP_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending Stop Released\n"));
            sendAVRCP(getApp(), opid_stop, 1);
            break;
            
        case FF_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending FF Pressed\n"));
            sendAVRCP(getApp(), opid_fast_forward, 0);
            break;
            
        case FF_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending FF Released\n"));
            sendAVRCP(getApp(), opid_fast_forward, 1);
            break;
            
        case REW_PRESS:
            AVRCP_DEBUG(("AVRCP: Sending REW Pressed\n"));
            sendAVRCP(getApp(), opid_rewind, 0);
            break;
            
        case REW_RELEASE:
            AVRCP_DEBUG(("AVRCP: Sending REW Released\n"));
            sendAVRCP(getApp(), opid_rewind, 1);
            break;

		default:
			break;
    }
}


/* Send response to the AVRCP lib */
static void sendRcpConnectResponse(AVRCP *avrcp, const AVRCP_CONNECT_IND_T *ind, bool accept)
{
    /* Send the connect response */
    AvrcpConnectResponse(avrcp, ind->connection_id, accept);
}


/**************************************************************************/
void avHeadsetHandleAvrcpConnectReq(headsetTaskData *app, bdaddr addr)
{
    if (app->avrcp_state == avHeadsetAvrcpReady)
    {
        MessageCancelAll(getAppTask(), APP_AVRCP_CONNECT_REQ);
        /* Change to connecting state */
	    setAvrcpState(app, avHeadsetAvrcpConnecting);
        /* Establish AVRCP connection */
	    AvrcpConnect(app->avrcp, &addr);
    }
}


/**************************************************************************/
void avHeadsetHandleAvrcpConnectCfm(headsetTaskData *app, const AVRCP_CONNECT_CFM_T *cfm)
{
    if(cfm->status == avrcp_success)
    {
        /* Change to Connected state */
        setAvrcpState(app, avHeadsetAvrcpConnected);
        
        /* If now AVRCP is connected then we now only send AVRCP commands to play/pause music.
           Before we might have been Suspending/Starting audio stream as AVRCP wasn't in use.
           So incase the audio was suspended as a pause, we must resume the audio here. It might
           mean the music suddenly starting but better than being stuck in the Suspended state.
        */
     
		if (app->a2dp_state == avHeadsetA2dpStreaming)
		{
			avHeadsetUpdateAvrcpPlayState(app, 1);       
		}
		else
		{
			avHeadsetUpdateAvrcpPlayState(app, 0);
		}
        if ((app->active_profile == av_active) &&
            (!app->headset_connecting_hfp)
        )
        {
            headsetRestartAV(app);
        }
    }
    else
    {
        /* Change to Ready state */
        setAvrcpState(app, avHeadsetAvrcpReady);
    }
}


/**************************************************************************/
void avHeadsetHandleAvrcpConnectInd(const headsetTaskData *app, const AVRCP_CONNECT_IND_T *ind)
{
    MessageCancelAll(getAppTask(), APP_AVRCP_CONNECT_REQ);
	sendRcpConnectResponse(app->avrcp, ind, 1); 
}


/**************************************************************************/
void avHeadsetHandleAvrcpConnectIndReject(AVRCP *avrcp, const AVRCP_CONNECT_IND_T *ind)
{
    sendRcpConnectResponse(avrcp, ind, 0);
}


/**************************************************************************/
void avHeadsetHandleAvrcpDisconnectInd(headsetTaskData *app)
{
    /* Change to Ready state */
    setAvrcpState(app, avHeadsetAvrcpReady);
    
    /* Reset pending state as we won't get a CFM back from any sent AVRCP commands,
       now that that connection is closed.
    */
    app->avrcp_pending = FALSE;

}


/**************************************************************************/
void avHeadsetHandleAvrcpPassthroughInd(const AVRCP_PASSTHROUGH_IND_T *ind)
{
    /* Acknowledge the request */
    AvrcpPassthroughResponse(ind->avrcp, avctp_response_not_implemented);
}


/**************************************************************************/
void avHeadetHandleAvrcpUnitInfo(const AVRCP_UNITINFO_IND_T *ind)
{
    /*
        We are not a target so reject UnitInfo requests
    */
    AvrcpUnitInfoResponse(ind->avrcp, FALSE, subunit_monitor, 0, (uint32) 0);
}


/**************************************************************************/
void avHeadetHandleAvrcpSubUnitInfo(const AVRCP_SUBUNITINFO_IND_T *ind)
{
    /*
        We are not a target so reject SubUnitInfo requests
    */
    AvrcpSubUnitInfoResponse(ind->avrcp, FALSE, 0);
}


/**************************************************************************/
void avHeadetHandleAvrcpVendorDependent(const AVRCP_VENDORDEPENDENT_IND_T *ind)
{
    /*
        We are not a target so reject vendor requests
    */
	AvrcpVendorDependentResponse(ind->avrcp, avctp_response_not_implemented);
}


/**************************************************************************/
void avHeadsetSendPause(headsetTaskData* app)
{
    if (app->avrcp_state == avHeadsetAvrcpConnected)
    {
        avHeadsetPausePress(app);
        avHeadsetPauseRelease(app);
		avHeadsetUpdateAvrcpPlayState(app, 0);
    }
}


/**************************************************************************/
void avHeadsetPausePress(headsetTaskData* app)
{
    /* see controls_handler description */
    MessageSendConditionally(&app->controls_task, PAUSE_PRESS, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetPauseRelease(headsetTaskData* app)
{
    /* see button_handler message queue description */
    MessageSendConditionally(&app->controls_task, PAUSE_RELEASE, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetSendPlay(headsetTaskData* app)
{
    if (app->avrcp_state == avHeadsetAvrcpConnected)
    {
        avHeadsetPlayPress(app);
        avHeadsetPlayRelease(app);
		avHeadsetUpdateAvrcpPlayState(app, 1);
    }
}


/**************************************************************************/
void avHeadsetPlayPress(headsetTaskData* app)
{
    /* see controls_handler description */
    MessageSendConditionally(&app->controls_task, PLAY_PRESS, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetPlayRelease(headsetTaskData* app)
{
    /* see button_handler message queue description */
    MessageSendConditionally(&app->controls_task, PLAY_RELEASE, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetForwardPress(headsetTaskData* app)
{
    /* see controls_handler description */
    MessageSendConditionally(&app->controls_task, FORWARD_PRESS, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetForwardRelease(headsetTaskData* app)
{
    /* see button_handler message queue description */
    MessageSendConditionally(&app->controls_task, FORWARD_RELEASE, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetBackwardPress(headsetTaskData* app)
{
    /* see controls_handler description */
    MessageSendConditionally(&app->controls_task, BACKWARD_PRESS, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetBackwardRelease(headsetTaskData* app)
{
    /* see button_handler message queue description */
    MessageSendConditionally(&app->controls_task, BACKWARD_RELEASE, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetSendStop(headsetTaskData* app)
{
    if (app->avrcp_state == avHeadsetAvrcpConnected)
    {
        avHeadsetStopPress(app);
        avHeadsetStopRelease(app);
		avHeadsetUpdateAvrcpPlayState(app, 0);
    }
}


/**************************************************************************/
void avHeadsetStopPress(headsetTaskData* app)
{
    /* see controls_handler description */
    MessageSendConditionally(&app->controls_task, STOP_PRESS, NULL, &app->avrcp_pending);
}


/**************************************************************************/
void avHeadsetStopRelease(headsetTaskData* app)
{
    /* see button_handler message queue description */
    MessageSendConditionally(&app->controls_task, STOP_RELEASE, NULL, &app->avrcp_pending);
}


/*************************************************************************/
void avHeadsetFastForwardPress(headsetTaskData* app)
{
    MessageSendConditionally(&app->controls_task, FF_PRESS, NULL, &app->avrcp_pending);
}


/*************************************************************************/
void avHeadsetFastForwardRelease(headsetTaskData* app)
{
    MessageSendConditionally(&app->controls_task, FF_RELEASE, NULL, &app->avrcp_pending);
}


/*************************************************************************/
void avHeadsetFastRewindPress(headsetTaskData* app)
{
    MessageSendConditionally(&app->controls_task, REW_PRESS, NULL, &app->avrcp_pending);
}


/*************************************************************************/
void avHeadsetFastRewindRelease(headsetTaskData* app)
{
    MessageSendConditionally(&app->controls_task, REW_RELEASE, NULL, &app->avrcp_pending);
}

/**************************************************************************/
void avHeadsetUpdateAvrcpPlayState(headsetTaskData *app, const bool state)
{
	AVRCP_DEBUG(("AVRCP_DEBUG: AVRCP play state - C=%d N=%d\n", app->play_state, state));
	app->play_state = state;
}
/**************************************************************************/

⌨️ 快捷键说明

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