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

📄 main.c

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

FILE NAME
    main.c        

DESCRIPTION
    This file contains the main stereo_headset application.

NOTES

***** Project property options *****

Selected board (Development board 1307/1442/1508/1645) : Decide if the application is
to run on a CSR 1307, 1442, 1508 or 1645 development board (default - Development board 1645).

Include CVC (Yes/No) : Determines if the application is to use CVC processing for noice  
reduction and echo cancellation on the SCO audio (default - Yes).

Enable debug (Yes/No) : Determines if the application should be run in debug mode which 
displays debug output (default - No). Note: the debug for individual files can be enabled 
and disabled by modifying the defines in headset_debug.h.

***** User information *****

When the application starts up, the headset is powered off. The headset must be  
powered on by a long press (> 2s) of# the MFB button (labelled F1 on the 1307 boards).  
If the MFB is continued to be held down for 5s, the headset will enter pairing mode  
for 1 minute. With the headset on, a long press of the MFB button will power the  
headset off. Please refer to the user interface guide for more details of how the  
headset operates.

This headset takes readings of the battery and charger. Once the battery has reached a  
low level the headset will power off. The battery is read through AIO0 and the charger  
is read through AIO1 and AIO0 which are multiplexed with PIO13 and PIO15.

***** PS Keys *****

The headset is configurable using the following PS Keys:

USR3 - Auto reconnect (0/1) : Set to 1 for the headset to reconnect back to the last  
used AG and last used AV source on power on.

USR4 - Headset security (0/1) : Set to 1 to only allow pairing when the headset is in  
pairing mode, or if the headset is initiating a connection.

USR5 - Ringtone select (0/1) : Chooses one of two different headset ringtones.

USR6 - Auto switch off enabled (0/1) : Set to 1 to automatically switch off the  
headset if it has been idle for for a time of 'auto switch off timeout' (see USR7).

USR7 - If USR6 is set to 1 then this is the time that the headset is active before  
turning off.

USR9 - MP3 Enabled (0/1) - Set to 1 to enable MP3 support. Note: make sure the mp3  
component of Bluelab is installed otherwise the application will fail on startup.

Note: The pin for this application is stored in the PS Key FIXED_PIN.
      

****************************************************************************
    Header files
*/

#include "headset_private.h"
#include "a2dp_handler.h"
#include "av_stream_control.h"
#include "avrcp_handler.h"
#include "headset_battery.h"
#include "headset_common.h"
#include "headset_init.h"
#include "leds.h"
#include "headset_power.h"
#include "headset_security.h"
#include "headset_tones.h"
#include "headset_volume.h"
#include "hfp_audio.h"
#include "hfp_handler.h"
#include "hfp_ring.h"
#include "hfp_slc.h"
#include "stereo_headset_buttons.h"
#include "headset_button_handler.h"


#ifdef TEST_HARNESS
#include "test_stereo.h"
#endif

#include <a2dp.h>
#include <audio.h>
#include <avrcp.h>
#include <bdaddr.h>
#include <codec.h>
#include <connection.h>
#include <hfp.h>
#include <panic.h>
#include <pio.h>
#include <ps.h>
#include <stdlib.h>
#include <stream.h>
#include <charger.h>

#ifdef DEBUG_MAIN
#define MAIN_DEBUG(x) DEBUG(x)
#else
#define MAIN_DEBUG(x) 
#endif

#ifdef DEBUG_BUTTONS
#define BUTTON_DEBUG(x) DEBUG(x)
#else
#define BUTTON_DEBUG(x) 
#endif

#ifdef TEST_HARNESS
#include "test_stereo.h"
#endif

/* Single instance of AV Headset state */
static headsetTaskData app;

/*************************************************************************
NAME    
    getAppTask
    
DESCRIPTION
    Returns the AV application main task.

RETURNS
    Task
*/
Task getAppTask(void)
{
    return &app.task;
}

/*************************************************************************
NAME    
    getApp
    
DESCRIPTION
    Returns the AV application main task.

RETURNS
    Task
*/
headsetTaskData *getApp(void)
{
    return &app;
}

        
/**************************************************************************/
static void unhandledA2dpState(headsetA2dpState state, MessageId id)
{
	state = state;
	id = id;

    MAIN_DEBUG(("A2DP current state %d message id 0x%x\n", state, id));  
}

static void unhandledAvrcpState(headsetAvrcpState state, MessageId id)
{
	state = state;
	id = id;

    MAIN_DEBUG(("AVRCP current state %d message id 0x%x\n", state, id));
}

static void unhandledHfpState(headsetHfpState state, MessageId id)
{
	state = state;
	id = id;

    MAIN_DEBUG(("HFP current state %d message id 0x%x\n", state, id));
}


/**************************************************************************/
        
/**************************************************************************/
static void avrcp_msg_handler(MessageId id, Message message)
{
    headsetAvrcpState current_avrcp_state = app.avrcp_state;
	
    switch(id)
    {
    case AVRCP_INIT_CFM:
        MAIN_DEBUG(("AVRCP_INIT_CFM\n"));

        switch(current_avrcp_state)
        {
        case avHeadsetAvrcpInitialising:
            avHeadsetHandleAvrcpInitCfm(&app, (AVRCP_INIT_CFM_T *) message);
            break;
            
        case avHeadsetAvrcpReady:
        case avHeadsetAvrcpConnecting:
        case avHeadsetAvrcpConnected:
        default:
            unhandledAvrcpState(current_avrcp_state, id);
            break;      
        }
        break;
        
        case AVRCP_CONNECT_CFM:
            MAIN_DEBUG(("AVRCP_CONNECT_CFM status = %d\n",((AVRCP_CONNECT_CFM_T*)message)->status));

            switch(current_avrcp_state)
            {
            case avHeadsetAvrcpReady:
            case avHeadsetAvrcpConnecting:
                avHeadsetHandleAvrcpConnectCfm(&app, (AVRCP_CONNECT_CFM_T*) message);
                break;

            case avHeadsetAvrcpInitialising:
            case avHeadsetAvrcpConnected:
            default:
                unhandledAvrcpState(current_avrcp_state, id);
                break;      
            }
            break;
            
        case AVRCP_CONNECT_IND:
            MAIN_DEBUG(("AVRCP_CONNECT_IND\n"));
            switch(current_avrcp_state)
            {
            case avHeadsetAvrcpReady:
                /* Only accept the RCP connection if we are in the ready state */
                avHeadsetHandleAvrcpConnectInd(&app, (AVRCP_CONNECT_IND_T *) message);
                break;

            case avHeadsetAvrcpConnecting:
            case avHeadsetAvrcpConnected:
                avHeadsetHandleAvrcpConnectIndReject(app.avrcp, (AVRCP_CONNECT_IND_T *) message);
                break;
                
            case avHeadsetAvrcpInitialising:
            default:
                unhandledAvrcpState(current_avrcp_state, id);
                break;      
            }
            break;
            
            
        case AVRCP_PASSTHROUGH_CFM:
            MAIN_DEBUG(("AVRCP_PASSTHROUGH_CFM status = %d\n",((AVRCP_PASSTHROUGH_CFM_T*)message)->status));

            /* 
                Clearing the pending flag should allow another
                pending event to be delivered to controls_handler 
            */
            app.avrcp_pending = FALSE;
            break;
            
        case AVRCP_PASSTHROUGH_IND:
            MAIN_DEBUG(("AVRCP_PASSTHROUGH_IND\n"));

			/* Always respond whatever the state */
            avHeadsetHandleAvrcpPassthroughInd((AVRCP_PASSTHROUGH_IND_T *) message);
            break;
            
        case AVRCP_UNITINFO_IND:
            MAIN_DEBUG(("AVRCP_UNITINFO_IND\n"));

			/* Always respond whatever the state */
            avHeadetHandleAvrcpUnitInfo((AVRCP_UNITINFO_IND_T*) message);
            break;

        case AVRCP_SUBUNITINFO_IND:
            MAIN_DEBUG(("AVRCP_SUBUNITINFO_IND\n"));
			
            /* Always respond whatever the state */
            avHeadetHandleAvrcpSubUnitInfo((AVRCP_SUBUNITINFO_IND_T*) message);
            break;

        case AVRCP_VENDORDEPENDENT_IND:
            MAIN_DEBUG(("AVRCP_VENDORDEPENDENT_IND\n"));
			
            /* Always respond whatever the state */
			avHeadetHandleAvrcpVendorDependent((AVRCP_VENDORDEPENDENT_IND_T*) message);
            break;

        case AVRCP_DISCONNECT_IND:
            MAIN_DEBUG(("AVRCP_DISCONNECT_IND\n"));
            
            switch(current_avrcp_state)
            {
                case avHeadsetAvrcpConnected:
                    avHeadsetHandleAvrcpDisconnectInd(&app);
                    break;

                case avHeadsetAvrcpConnecting:
                case avHeadsetAvrcpInitialising:
                case avHeadsetAvrcpReady:
                default:
                    unhandledAvrcpState(current_avrcp_state, id);
                    break;      
            }   
            break;

        default:
            MAIN_DEBUG(("AV Headset - Unhandled avrcp msg 0x%x\n",id));
            break;
    }
}



/**************************************************************************/
static void a2dp_msg_handler(MessageId id, Message message)
{
    headsetA2dpState current_a2dp_state = app.a2dp_state;

    switch (id)
    {
        case A2DP_INIT_CFM:
            MAIN_DEBUG(("A2DP_INIT_CFM\n"));
                    
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpInitialising:
                    avHeadsetHandleA2dpInitCfm(&app, (A2DP_INIT_CFM_T*) message);
                    break;
                    
                case avHeadsetA2dpReady:
                case avHeadsetA2dpConnected:
                case avHeadsetA2dpStreaming:
                case avHeadsetA2dpInitiating:
                case avHeadsetA2dpSignallingActive:
                default:
                    unhandledA2dpState(current_a2dp_state, id);
                    break;      
            }
            break;
            
        case A2DP_ADD_SEP_CFM:
            MAIN_DEBUG(("A2DP_ADD_SEP_CFM\n"));
                    
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpInitialising:
                    avHeadsetHandleA2dpAddSepCfm(&app, (A2DP_ADD_SEP_CFM_T*) message);
                    break;
                    
                case avHeadsetA2dpReady:
                case avHeadsetA2dpConnected:
                case avHeadsetA2dpStreaming:
                case avHeadsetA2dpInitiating:
                case avHeadsetA2dpSignallingActive:
                default:
                    unhandledA2dpState(current_a2dp_state, id);
                    break;      
            }
            break;
            
        case A2DP_OPEN_IND:
            MAIN_DEBUG(("A2DP_OPEN_IND\n"));
                    
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpReady:
                case avHeadsetA2dpSignallingActive:
                    avHeadsetHandleA2dpOpenInd(&app, (A2DP_OPEN_IND_T*) message);
                    break;
                    
                case avHeadsetA2dpInitialising:
                case avHeadsetA2dpConnected:
                case avHeadsetA2dpStreaming:
                case avHeadsetA2dpInitiating:
                default:
                    unhandledA2dpState(current_a2dp_state, id);
                    break;      
            }
            break;
            
        case A2DP_OPEN_CFM:
            MAIN_DEBUG(("A2DP_OPEN_CFM %d\n",((A2DP_OPEN_CFM_T*) message)->result));
            
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpReady:
                case avHeadsetA2dpInitiating:
                case avHeadsetA2dpSignallingActive:
                    avHeadsetHandleA2dpOpenCfm(&app, (A2DP_OPEN_CFM_T*) message);
                    break;
                    
                case avHeadsetA2dpStreaming:
                case avHeadsetA2dpConnected:
                case avHeadsetA2dpInitialising:
                default:
                    unhandledA2dpState(current_a2dp_state, id);
                    break;      
            }
            break;
                        
        case A2DP_START_IND:
            MAIN_DEBUG(("A2DP_START_IND\n"));
                    
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpConnected:
                    avHeadsetHandleA2dpStartInd(&app, (A2DP_START_IND_T*) message);
                    break;
                    
                case avHeadsetA2dpReady:
                case avHeadsetA2dpInitialising:
                case avHeadsetA2dpStreaming:
                case avHeadsetA2dpInitiating:
                case avHeadsetA2dpSignallingActive:
                default:
                    unhandledA2dpState(current_a2dp_state, id);
                    break;      
            }
            break;
            
        case A2DP_START_CFM:
            MAIN_DEBUG(("A2DP_START_CFM\n"));
                    
            switch(current_a2dp_state)
            {
                case avHeadsetA2dpInitiating:
                case avHeadsetA2dpConnected:
                    avHeadsetHandleA2dpStartCfm(&app, (A2DP_START_CFM_T*) message);
                    break;
                    
                case avHeadsetA2dpReady:
                case avHeadsetA2dpStreaming:  
                case avHeadsetA2dpInitialising:

⌨️ 快捷键说明

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