编程参考 - 蓝牙地址转换函数


#include <stdint.h>

   /* Definitions for compilers that required structure to be explicitly*/
   /* declared as packed.                                               */
#define __PACKED_STRUCT_BEGIN__


#define __PACKED_STRUCT_END__                __attribute__((packed))


   /* Miscellaneous defined type declarations.                          */


   /* Simply BOOLEAN type.                                              */
typedef char Boolean_t;


   /* Miscellaneous Type definitions that should already be defined,    */
   /* but are necessary.                                                */
#ifndef NULL
   #define NULL ((void *)0)
#endif


#ifndef TRUE
   #define TRUE (1 == 1)
#endif


#ifndef FALSE
   #define FALSE (0 == 1)
#endif


   /* Unsigned basic types.                                             */
typedef uint8_t Byte_t;                         /* Generic 8 bit Container.   */


#define BYTE_SIZE                               (sizeof(Byte_t))


typedef uint16_t Word_t;                        /* Generic 16 bit Container.  */


#define WORD_SIZE                               (sizeof(Word_t))


typedef uint32_t DWord_t;                       /* Generic 32 bit Container.  */


#define DWORD_SIZE                              (sizeof(DWord_t))


typedef uint64_t QWord_t;                       /* Generic 64 bit Container.  */


#define QWORD_SIZE                              (sizeof(QWord_t))


   /* Signed basic types.                                               */
typedef int8_t SByte_t;                         /* Signed 8 bit Container.    */


#define SBYTE_SIZE                              (sizeof(SByte_t))


typedef int16_t SWord_t;                        /* Signed 16 bit Container.   */


#define SWORD_SIZE                              (sizeof(SWord_t))


typedef int32_t SDWord_t;                       /* Signed 32 bit Container.   */


#define SDWORD_SIZE                             (sizeof(SDWord_t))


typedef int64_t SQWord_t;                       /* Signed 64 bit Container.   */


#define SQWORD_SIZE                             (sizeof(SQWord_t))


   /* Unaligned Unsigned basic types.                                   */
typedef unsigned char NonAlignedByte_t;         /* Unaligned Generic 8 Bit    */
                                                /* Container.                 */


#define NON_ALIGNED_BYTE_SIZE                   (sizeof(NonAlignedByte_t))


   /* The following type declaration represents the structure of a      */
   /* single Bluetooth Board Address.                                   */
typedef __PACKED_STRUCT_BEGIN__ struct _tagBD_ADDR_t
{
   Byte_t BD_ADDR0;
   Byte_t BD_ADDR1;
   Byte_t BD_ADDR2;
   Byte_t BD_ADDR3;
   Byte_t BD_ADDR4;
   Byte_t BD_ADDR5;
} __PACKED_STRUCT_END__ BD_ADDR_t;


#define BD_ADDR_SIZE                            (sizeof(BD_ADDR_t))


   /* The following MACRO is a utility MACRO that exists to assign      */
   /* the individual Byte values into the specified BD_ADDR variable.   */
   /* The Bytes are NOT in Little Endian Format, however, they are      */
   /* assigned to the BD_ADDR variable in Little Endian Format.  The    */
   /* first parameter is the BD_ADDR variable (of type BD_ADDR_t) to    */
   /* assign, and the next six parameters are the Individual BD_ADDR    */
   /* Byte values to assign to the variable.                            */
#define ASSIGN_BD_ADDR(_dest, _a, _b, _c, _d, _e, _f) \
{                                                     \
   (_dest).BD_ADDR0 = (_f);                           \
   (_dest).BD_ADDR1 = (_e);                           \
   (_dest).BD_ADDR2 = (_d);                           \
   (_dest).BD_ADDR3 = (_c);                           \
   (_dest).BD_ADDR4 = (_b);                           \
   (_dest).BD_ADDR5 = (_a);                           \
}


/* The following MACRO is a utility MACRO that exists to copy       */
/* BD_ADDR variable into the another specified BD_ADDR variable.    */
/* The Bytes are NOT in Little Endian Format, however, they are     */
/* assigned to the BD_ADDR variable in Little Endian Format.  The   */
/* first parameter is the BD_ADDR variable (of type BD_ADDR_t) to   */
/* assign, and the next parameter is the source BD_ADDR variable.   */
#define COPY_BD_ADDR(_dest, _src) \
{                                                     \
(_dest).BD_ADDR0 = (_src).BD_ADDR0;                \
(_dest).BD_ADDR1 = (_src).BD_ADDR1;                \
(_dest).BD_ADDR2 = (_src).BD_ADDR2;                \
(_dest).BD_ADDR3 = (_src).BD_ADDR3;                \
(_dest).BD_ADDR4 = (_src).BD_ADDR4;                \
(_dest).BD_ADDR5 = (_src).BD_ADDR5;                \
}


   /* The following MACRO is a utility MACRO that exists to aid in the  */
   /* Comparison of two BD_ADDR_t variables.  This MACRO only returns   */
   /* whether the two BD_ADDR_t variables are equal (MACRO returns      */
   /* boolean result) NOT less than/greater than.  The two parameters to*/
   /* this MACRO are both of type BD_ADDR_t and represent the BD_ADDR_t */
   /* variables to compare.                                             */
#define COMPARE_BD_ADDR(_x, _y) (((_x).BD_ADDR0 == (_y).BD_ADDR0) && ((_x).BD_ADDR1 == (_y).BD_ADDR1) && ((_x).BD_ADDR2 == (_y).BD_ADDR2) && ((_x).BD_ADDR3 == (_y).BD_ADDR3) && ((_x).BD_ADDR4 == (_y).BD_ADDR4) && ((_x).BD_ADDR5 == (_y).BD_ADDR5))


   /* The following MACRO is a utility MACRO that exists to aid in the  */
   /* Comparison of a BD_ADDR_t variables to the NULL BD_ADDR. This     */
   /* MACRO only returns whether the the BD_ADDR_t variable is equal to */
   /* the NULL BD_ADDR (MACRO returns boolean result) NOT less          */
   /* than/greater than.  The parameter to this MACRO is the BD_ADDR_t  */
   /* structure to compare to the NULL BD_ADDR.                         */
#define COMPARE_NULL_BD_ADDR(_x) (((_x).BD_ADDR0 == 0x00) && ((_x).BD_ADDR1 == 0x00) && ((_x).BD_ADDR2 == 0x00) && ((_x).BD_ADDR3 == 0x00) && ((_x).BD_ADDR4 == 0x00) && ((_x).BD_ADDR5 == 0x00))
#include <stdio.h>
#include <stdint.h>
#include <string.h>

   /* The following function is responsible for converting data of type */
   /* BD_ADDR to a string.  The first parameter of this function is the */
   /* BD_ADDR to be converted to a string.  The second parameter of this*/
   /* function is a pointer to the string in which the converted BD_ADDR*/
   /* is to be stored.                                                  */
void BD_ADDRToStr(BD_ADDR_t Board_Address, char *BoardStr)
{
   sprintf(BoardStr, "%02X%02X%02X%02X%02X%02X", Board_Address.BD_ADDR5,
                                                 Board_Address.BD_ADDR4,
                                                 Board_Address.BD_ADDR3,
                                                 Board_Address.BD_ADDR2,
                                                 Board_Address.BD_ADDR1,
                                                 Board_Address.BD_ADDR0);
}


   /* The following function is responsible for the specified string    */
   /* into data of type BD_ADDR.  The first parameter of this function  */
   /* is the BD_ADDR string to be converted to a BD_ADDR.  The second   */
   /* parameter of this function is a pointer to the BD_ADDR in which   */
   /* the converted BD_ADDR String is to be stored.                     */
void StrToBD_ADDR(char *BoardStr, BD_ADDR_t *Board_Address)
{
   unsigned int Address[sizeof(BD_ADDR_t)];


   if((BoardStr) && (strlen(BoardStr) == sizeof(BD_ADDR_t)*2) && (Board_Address))
   {
      sscanf(BoardStr, "%02X%02X%02X%02X%02X%02X", &(Address[5]), &(Address[4]), &(Address[3]), &(Address[2]), &(Address[1]), &(Address[0]));


      Board_Address->BD_ADDR5 = (Byte_t)Address[5];
      Board_Address->BD_ADDR4 = (Byte_t)Address[4];
      Board_Address->BD_ADDR3 = (Byte_t)Address[3];
      Board_Address->BD_ADDR2 = (Byte_t)Address[2];
      Board_Address->BD_ADDR1 = (Byte_t)Address[1];
      Board_Address->BD_ADDR0 = (Byte_t)Address[0];
   }
   else
   {
      if(Board_Address)
         memset(Board_Address, 0, sizeof(BD_ADDR_t));
   }
}

void BufferToBD_ADDR(uint8_t *addr, BD_ADDR_t *Board_Address){
    Board_Address->BD_ADDR5=addr[0];
    Board_Address->BD_ADDR4=addr[1];
    Board_Address->BD_ADDR3=addr[2];
    Board_Address->BD_ADDR2=addr[3];
    Board_Address->BD_ADDR1=addr[4];
    Board_Address->BD_ADDR0=addr[5];
}


void BD_ADDRToBuffer(BD_ADDR_t Board_Address, uint8_t *addr){
    addr[0]=Board_Address.BD_ADDR5;
    addr[1]=Board_Address.BD_ADDR4;
    addr[2]=Board_Address.BD_ADDR3;
    addr[3]=Board_Address.BD_ADDR2;
    addr[4]=Board_Address.BD_ADDR1;
    addr[5]=Board_Address.BD_ADDR0;
}

void BD_ADDRReverse(BD_ADDR_t *Board_Address)
{
    uint8_t tmp;

    tmp = Board_Address->BD_ADDR5;
    Board_Address->BD_ADDR5=Board_Address->BD_ADDR0;
    Board_Address->BD_ADDR0=tmp;

    tmp = Board_Address->BD_ADDR4;
    Board_Address->BD_ADDR4=Board_Address->BD_ADDR1;
    Board_Address->BD_ADDR1=tmp;

    tmp = Board_Address->BD_ADDR3;
    Board_Address->BD_ADDR3=Board_Address->BD_ADDR2;
    Board_Address->BD_ADDR2=tmp;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值