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

📄 pioag_send.c

📁 bluetooth audio gateway
💻 C
字号:
#include "pioag_private.h"
#include "ag.h"
#include "ag_types.h"

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


/*
    pioAgSmAddDevReq

    Register a device with the BlueStack security manager
*/
void pioAgSmAddDevReq(void)
{
    bd_addr_t addr;
    uint8 link_key[AG_SIZE_LINK_KEY];

    /* get the address and link key of the device we're paired with */
    (void) PsRetrieve(PIO_AG_PS_PEER_ADDR, &addr, sizeof(bd_addr_t));
    (void) PsRetrieve(PIO_AG_PS_LINK_KEY, link_key, AG_SIZE_LINK_KEY);

    /* send a register request to the ag */
    agSmAddDeviceReqAction(addr, 1, (uint8 *) &(link_key));
}


/*
    pioAgInquiryReq

    This function starts an inquiry procedure looking for headsets.
*/
void pioAgInquiryReq(void)
{
    inquiryReqAction(1, PIO_AG_INQUIRY_TIMEOUT, PIO_AG_HEADSET_COD, 0);
}


/*
    pioAgPinReplyReq

    Respond to the PIN request by getting the stored PIN, if there isn't one 
    send a length of zero i.e. reject request.
*/
void pioAgPinReplyReq(bd_addr_t addr)
{
    /* Read out of PS store if it is in there */    
    uint8 pin[MAX_PIN_LENGTH];
    uint16 pin_length = 0;
    
    /* send the PIN code stored in the PS key if it exists */
    if(PsRetrieve(PIO_AG_PS_PIN_LENGTH, &pin_length, 1) && 
       PsRetrieve(PIO_AG_PS_PIN_CODE, pin, pin_length) && 
       pin_length)
    {        
        pinResAction(addr, pin_length, pin);  
    }
    else
    {
        pinResAction(addr, 0, 0);  
    }
}


/*
    pioAgConnectReq

    Connect to another device either as master or slave
*/
void pioAgConnectReq(pioag_role_t role, timeout_t timeout_sec)
{
    bd_addr_t addr;    
    pioag_conn_params_t *params;

    if (!PsRetrieve(PIO_AG_PS_PEER_ADDR, &addr, sizeof(bd_addr_t)))
    {
        /* If there is no address stored then don't try to connect */
        return;
    }
    
    /* Allocate a block of memory to hold the paremeters we're passing down */
    params = (pioag_conn_params_t *) PanicNull(malloc(sizeof(pioag_conn_params_t)));    

    /* 
        Set the use of authentication and encryption - for now this is the
        same for master and slave so set it once here
    */        
    params->auth.authentication = PIO_AG_USE_AUTHENTICATION;
    params->auth.encryption = PIO_AG_USE_ENCRYPTION;

    /* Set the park parameters once as well */        
    params->park.max_intval = PIO_AG_MAX_PARK_INTERVAL;
    params->park.min_intval = PIO_AG_MIN_PARK_INTERVAL;

    /* Set the sniff parameters too */                
    params->sniff.max_intval = PIO_AG_MAX_SNIFF_INTERVAL;
    params->sniff.min_intval = PIO_AG_MIN_SNIFF_INTERVAL;
    params->sniff.attempt = PIO_AG_SNIFF_ATTEMPT;
    params->sniff.timeout = PIO_AG_SNIFF_TIMEOUT;

    /* Initiate the connect attempt */
    if (role == PioAgMaster)
    {        
        PioAgState.connectAsSlave = 0;
        agConnectAsMasterReqAction(addr, 
            (ag_auth_config_t *)&(params->auth), 
            (ag_park_config_t *)&(params->park), 
            (ag_sniff_config_t *)&(params->sniff), 
            PIO_AG_CONNECTION_TARGET, 
            timeout_sec);
    }
    else if (role == PioAgSlave)
    {
        PioAgState.connectAsSlave = 1;        
        agConnectAsSlaveReqAction(addr, 
            (ag_auth_config_t *)&(params->auth), 
            (ag_park_config_t *)&(params->park), 
            (ag_sniff_config_t *)&(params->sniff),
            timeout_sec, 
            PIO_AG_PAGE_SCAN_INTERVAL, 
            PIO_AG_PAGE_SCAN_WINDOW);
    }
    
    /* Free the memory we allocated at the top */
    free(params);
}


/*
    pioAgCancelReq

    Send a request to cancel the current state
*/
void pioAgCancelReq(void)
{
    agCancelReqAction();
}


/*
    pioAgVolumeReq

    Send a volme up or down request
*/
void pioAgVolumeReq(int8 increment)
{
    agVolumeChangeReqAction(increment, 0);
}


/*
    pioAgRingReq

    Send a ring command to the headset
*/
void pioAgRingReq(void)
{
    agRingReqAction(PIO_AG_RING_DURATION, PIO_AG_RING_COUNT);
}


/*
    pioAgResetReq

    This function resets the state machine and clears link information.
    It returns a uint16 that indicates if the reset occured (0x01) or not (0x00).
*/
uint16 pioAgResetReq(void)
{
    uint16 *zeroBuf = (uint16*) PanicNull(calloc(32, sizeof(uint16)));

    if (agScoConnectedQuery() || agRfcommConnectedQuery())
    {
        /* We can't reset until the active link has been removed */
        return 0;
    }
    else
    {
        if (!agIdleQuery())
        {
            /* There is no link up so we can reset - cancel the current mode if any */
            pioAgCancelReq();        
        }
        
        /* Reset state variable */
        PioAgState.inquiryComplete = 0;        
        PioAgState.connectAsSlave = 0;

        /* Kill any timers that are running */
        if (PioAgState.timerHandle != NULL_TIMER)
        {
            TimerCancel(PioAgState.timerHandle);
            PioAgState.timerHandle = NULL_TIMER;
        }

        if (PioAgState.connectionTimerHandle != NULL_TIMER)
        {
            TimerCancel(PioAgState.connectionTimerHandle);
            PioAgState.connectionTimerHandle = NULL_TIMER;
        }

        /* Clear PIN code */
        PioAgState.pinLengthBytes = 0;
        PioAgState.pinEntered = 0;
        PioAgState.pinCode = 0;

        /* Clear link PS data */
        (void) PsStore(PIO_AG_PS_PEER_ADDR, zeroBuf, sizeof(bd_addr_t));
        (void) PsStore(PIO_AG_PS_LINK_KEY, zeroBuf, AG_SIZE_LINK_KEY);
        (void) PsStore(PIO_AG_PS_IS_PAIRED, zeroBuf, 1);
        (void) PsStore(PIO_AG_PS_LINK_KEY_VALID, zeroBuf, 1);
        (void) PsStore(PIO_AG_PS_PIN_CODE, zeroBuf, MAX_PIN_LENGTH);
        (void) PsStore(PIO_AG_PS_PIN_LENGTH, zeroBuf, 1);

        return 1;
    }
}

⌨️ 快捷键说明

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