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

📄 sb_custom.c

📁 UART sample code for AVR
💻 C
📖 第 1 页 / 共 3 页
字号:
    Function: SbReleaseLink

	Description
	-----------
    This function initates release of SPP link associated with the port number specified
    on the local device.
    
    This function waits on Mbox events untill simply blue CFM message and 
    link_released messages are processed.

	Arguments
	---------
    LocalPortNo  : port number of the local device

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbReleaseLink(uint8 LocalPortNo)  
{
    uint16 payloadlen;
    uint8  SbCommand[8];
    uint8  err;
    void   *msg;

    payloadlen = 0x0001;

    SbCommand[2] = SPP_RELEASE_LINK; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    SbCommand[6] = LocalPortNo; 
	
    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        //wait till link is released
    	msg = OSMboxPend(SbDevInfo.SbCmdMbox, BTCORE_CALLBACK_TIMEOUT, &err);
        if ( err == OS_NO_ERR && ((uint32)(msg) == SBSTATUS_OK)) {
            return SBSTATUS_OK;
        }
    }

    return SBSTATUS_ERROR;
}


/****************************************************************************
    Function: SbEnterTransparentMode

	Description
	-----------
    This function switches an active link to transparent mode.
    This function sends the "send transparent mode" command to the module

	Arguments
	---------
    LocalPortNo  : port number of the local device 

    Returns
	-------
    A SBStatus_T value indicating result of the operation.

****************************************************************************/
SBStatus_T SbEnterTransparentMode(uint8 LocalPortNo)
{

    int16 payloadlen;
    uint8 SbCommand[8];
    uint8 err;
    void  *msg;

    payloadlen = 0x0001;

    SbCommand[2] = SPP_TRANSPARENT_MODE; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    SbCommand[6] = LocalPortNo; 
	
    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        Transparent_flag = TRUE;          // transparent mode is now active     
        return SBSTATUS_OK;
    }

    return SBSTATUS_ERROR;
}

/****************************************************************************
    Function: SbSendUartBreak

	Description
	-----------
    This function switches the module to command mode.
    This function sends a UART Break to leave transparent mode.

	Arguments
	---------
    None.

    Returns
	-------
    None. 

****************************************************************************/
void SbSendUartBreak(void)
{
    uint32 ticks,new_tick=0;
    usart_t* usart = usart_tab[SB_UART_PORT];

    OS_ENTER_CRITICAL();

    UMDSL1 |= UBRK;                  // asserts TX line to 0
    ticks = OSTimeGet();
    while(OSTimeGet() < (ticks+1));  // wait for 10 ms (1 tick)
	UMDSL1 &= (~UBRK);           // asserts TX line to 1

    Transparent_flag = FALSE;        // command mode active

    OS_EXIT_CRITICAL();
}

/******************************************************************************
    Function: SbSdapStartSession

	Description
	-----------
    This function start an SDP session with a specified Bluetooth device and
	reports the result.

	Arguments
	---------
	BdAddr:
	The BD address of the device with which SDP session is to be started.

	Returns
	-------
	A boolean flag indicating whether the SDP session has been successfully
	established.
*******************************************************************************/
boolean SbSdapStartSession( BdAddrType bdAddr )
{
    uint16 payloadlen;
    uint8  SbCommand[13];
    int    i;

    payloadlen = 0x0006;

    SbCommand[2] = SDAP_CONNECT; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    for (i=0;i<6;i++)
        SbCommand[6 + i] = bdAddr[i];

    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        return TRUE;
    }

    return FALSE;
}

/******************************************************************************
    Function: SbSdapStopSession

	Description
	-----------
    This function stops the current SDP session and reports the result.

	Arguments
	---------
	None.

	Returns
	-------
	A boolean flag indicating whether the SDP session has been successfully
	terminated.
*******************************************************************************/
boolean SbSdapStopSession(void)
{
    uint16 payloadlen;
    uint8  SbCommand[7];

    payloadlen = 0x0;

    SbCommand[2] = SDAP_DISCONNECT; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion
	
    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        return TRUE;
    }
    return FALSE;
}

// service discovery(sdap) api
//
/******************************************************************************  
    Function: SbDeleteSdpRecords

	Description
	-----------
    This function deletes all the SDP records on the local device.

	Arguments
	---------
    None.

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbDeleteSdpRecords(void)
{
    uint16 payloadlen;
    uint8  SbCommand[7];

    payloadlen = 0x0000;

    SbCommand[2] = DELETE_SDP_RECORDS; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbStoreSdpRecord

	Description
	-----------
    This function stores a SDP record on the local device.

	Arguments
	---------
    entry  : sdp record bytes
    length : length of the sdp record

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbStoreSdpRecord(uint8* entry, uint16 length)
{
    uint16 i;
    uint16 payloadlen;
    uint8  SbCommand[340];

    payloadlen = length;

    SbCommand[2] = STORE_SDP_RECORD; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    for (i=0;i<length;i++)
        SbCommand[6 + i] = entry[i];	

    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbSetOperationMode

	Description
	-----------
    This function sets simply blue device operation mode.Please check simply blue
    software documentation for the details.

	Arguments
	---------
    mode  : simply blue operation mode that follows SBOperationMode_T enum.

    Returns
	-------
    A SBStatus_T value indicating result of the operation.
	
*******************************************************************************/
SBStatus_T SbSetOperationMode(SBOperationMode_T mode)
{
    uint16 payloadlen;
    uint8  SbCommand[8];

    payloadlen = 0x0001;

    SbCommand[2] = WRITE_OPERATION_MODE; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion
    SbCommand[6] = mode;

    return SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbNvsReadBdAddr

	Description
	-----------
    This function gets the local device's Bluetooth Device Address

	Arguments
	---------
    BdAddr : BD address of the local device will be fetched into this parameter. 
             
	
    Returns
	-------
	None.
	
*******************************************************************************/
void SbNvsReadBdAddr(BdAddrType BdAddr)
{
    uint16 payloadlen;
    uint8  SbCommand[7];

    payloadlen = 0x0000;

    SbCommand[2] = GAP_READ_LOCAL_BDA; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion

    if (SbSendCommand(SbCommand, 7 + payloadlen) == SBSTATUS_OK) {
        int i;
	    
        for (i=1;i<7;i++) {
	        BdAddr[i-1]=SbEvent.pPayload[i];
        }
    }
}

/******************************************************************************  
    Function: SbNvsWriteBdAddr

	Description
	-----------
    This function writes the local device's Bluetooth Device Address

	Arguments
	---------
    BdAddr : BD address of the local device will be fetched into this parameter. 
             
	
    Returns
	-------
	None.
	
*******************************************************************************/
void SbNvsWriteBdAddr(BdAddrType BdAddr)
{
    int i;
    uint16 payloadlen;
    uint8  SbCommand[13];

    payloadlen = 0x0006;

    SbCommand[2] = CHANGE_LOCAL_BDADDRESS; 
    SbCommand[3] = (payloadlen & 0x00FF); // payload size is stored 
    SbCommand[4] = (payloadlen >> 8);     // in little endian fashion
    for(i=0;i<6;i++)
    {
        SbCommand[6+i] = BdAddr[5-i];
    }

    SbSendCommand(SbCommand, 7 + payloadlen);
}

/******************************************************************************  
    Function: SbNvsReadDeviceName

	Description
	-----------
    This function gets the local device name

	Arguments
	---------

⌨️ 快捷键说明

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