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

📄 pair.c

📁 蓝牙HANDFREE软件源代码
💻 C
字号:
#include "handsfree_private.h"
#include "handsfree.h"

#include <cm_rfcomm.h>
#include <message.h>
#include <print.h>
#include <stdlib.h>


/* Store params needed when initiating pairing */
static void storePairParams(timeout_t timeout, framework_role_t pair_role)
{
    /* Create a struct and copy the parameters */
    HFstate.pair_data = (hf_pair_data_t *)hfAlloc(sizeof(hf_pair_data_t));
    HFstate.pair_data->pairing_timeout = D_SEC(timeout);
    HFstate.pair_data->pairing_role = pair_role;
}


/*
    pairReqAction

    Send a request to the headset framework to put the device into
    pairing mode.
*/
void pairReqAction(timeout_t timeout, framework_role_t pair_role)
{
    /* Perform a reset in case the default pIN was not set */
    resetReqAction();
    
    /* Take pairing action based on the current state of the handsfree */
    switch (HFstate.connectState)
    {
        /* If idle, initiate pairing */
        case idle:
        {            
            MAKE_MSG(HS_PAIR_REQ);
            msg->timeout = D_SEC(timeout);
            msg->pair_profile = pair_role;
            putMsg(msg);            

            /* Set the pairing role */
            HFstate.applicationRole = pair_role;

            /* Set the local state to pairing */
            setLocalState(pairing);
        }
        break;

        /* 
            If the handsfree is already pairing, cancel the request
            because we are already pairing.
        */
        case pairing:
        {
            /* 
                If timeout set to zero store pair params so we can 
                enter new pairing mode. Useful when switching between 
                pairing as a hs, hf or both. 
            */
            if (timeout != 0)
                storePairParams(timeout, pair_role);            
            
            /* Cancel current pairing mode */
            cancelCurrentStateAction();
        }
        break ;

        /* 
            If already paired this state is enterd by default, but we
            must allow some method to initiate pairing again so need
            to exit this mode and enter pairing mode.
        */
        case connectingAsMaster:
        case connectingAsSlave:
            /* Store the pairing data while its pending */
            storePairParams(timeout, pair_role);

            /* Cancel the current state so we can go into pairing mode */
            cancelCurrentStateAction();
            break;

        /* For all other states, pairing is not possible */
        default:
            PRINT(("pair.c - unhandled state 0x%2x\n", HFstate.connectState));
            handleErrorInd(HfErrorUnknownOrUnhandledState);
            break;
    }
}


/*
    hfPairCfm

    Called by the headset framework to indicate a change of pair
    status.
*/

void hfPairCfm(const HS_PAIR_CFM_T *cfm)
{
    pairing_status_t pair_status = HfPairingError;

    /* Role used in pairing to select mode so reset it here */
    HFstate.applicationRole = frameworkRoleUnset;

     /* No matter the outcome of the pairing procedure, go to idle state. */
    setLocalState(idle);
    
    /* Check the result of the pairing attempt */
    switch (cfm->status)
    {
        /* Pairing succeeded */
    case CmPairingComplete:
        /* On successful pairing go into connectable mode */
        connectReqAction();

        pair_status = HfPairingComplete;
        break;
        
        /* Pairing attempt timed out */
    case CmPairingTimeout:        
        pair_status = HfPairingTimedOut;               
        break;
        
        /* Pairing cancelled by user */
    case CmPairingCancelled:
        pair_status = HfPairingCancelled;

        if (HFstate.pair_data)
        {
            /* Request to enter pairing mode if we have valid pairing params */
            pairReqAction(HFstate.pair_data->pairing_timeout, HFstate.pair_data->pairing_role);
            
            /* If we had stored pairing data free it as its no longer needed */
            free(HFstate.pair_data);
            HFstate.pair_data = 0;
        }
        break;
        
        /* Pairing failed */
    case CmPairingFail:
        pair_status = HfPairingFailed;                
        break;
        
        /* Message internal to the framework lib should never see here */
    case CmPairingNotFinished:
    default:
        pair_status = HfPairingError;

        PRINT(("pair.c - unknown pairing result 0x%x\n", cfm->status));
        break;
    }

    /* Need to let the interface know the outcome of pairing */
    handlePairCfm(pair_status);
}

⌨️ 快捷键说明

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