CC2640R2F学习笔记(11)——GAP从机端广播

74 阅读3分钟

一、背景

链路层(LL)控制设备的射频状态,有五个设备状态:待机、广播、扫描、初始化和连接。

广播 为广播数据包,而 扫描 则是监听广播。

GAP通信中角色,中心设备(Central - 主机)用来扫描和连接 外围设备(Peripheral - 从机)。

大部分情况下外围设备通过广播自己来让中心设备发现自己,并建立 GATT 连接,从而进行更多的数据交换。

也有些情况是不需要连接的,只要外设广播自己的数据即可,用这种方式主要目的是让外围设备,把自己的信息发送给多个中心设备。

本篇是关于配置以及启动或关闭广播的流程,广播自定义数据包查看CC2640R2F学习笔记(19)——GAP从机端广播自定义数据

二、配置广播参数

2.1 广播参数相关宏

// Advertising interval when device is discoverable (units of 625us, 160=100ms)
#define DEFAULT_ADVERTISING_INTERVAL          160   

// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL

2.2 广播相关变量

// 扫描响应包
static uint8_t scanRspData[] =
{
  // complete name
  0x05,                             // length of this data
  GAP_ADTYPE_LOCAL_NAME_COMPLETE,
  'B', 'A', 'N', 'D',

  // Tx power level
  0x02,                             // length of this data
  GAP_ADTYPE_POWER_LEVEL,
  0                                 // 0dBm
};

// 广播数据包
// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertisting)
static uint8_t advertData[] =
{
  // Flags; this sets the device to use limited discoverable
  // mode (advertises for 30 seconds at a time) instead of general
  // discoverable mode (advertises indefinitely)
  0x02,                             // length of this data
  GAP_ADTYPE_FLAGS,
  DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

  // service UUID, to notify central devices what services are included
  // in this peripheral
  0x03,                             // length of this data
  GAP_ADTYPE_16BIT_MORE,            // some of the UUID's, but not all
  LO_UINT16(SIMPLEPROFILE_SERV_UUID),
  HI_UINT16(SIMPLEPROFILE_SERV_UUID)
};

2.3 配置GAP参数值

以SDK2.4 multi_role工程为例,在 multi_role_init() 初始化多角色应用程序函数中,

/*===================================== 从机 =====================================*/
/*------------------- 广播参数 -------------------*/
uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;             // 广播间隔,间隔越大功耗越低
GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt);
GAP_SetParamValue(TGAP_CONN_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_CONN_ADV_INT_MAX, advInt);

2.4 配置GAP角色规范(Role Profile)

以SDK2.4 multi_role工程为例,在 multi_role_init() 初始化多角色应用程序函数中,

/*===================================== 从机 =====================================*/
/*------------------- 广播参数 -------------------*/
uint8_t initialAdvertEnable = TRUE;                         // 是否开机广播
uint16_t advertOffTime = 0;

// 设置开机广播
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initialAdvertEnable, NULL);
// By setting this to zero, the device will go into the waiting state after
// being discoverable for 30.72 second, and will not being advertising again
// until the enabler is set back to TRUE
GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t), &advertOffTime, NULL);

// 设置扫描响应包内容
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData, NULL);

// 设置广播包内容
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData, NULL);

三、执行广播

3.1 执行广播函数

原mr_doAdvertise函数修改后

/**
 @brief 执行广播函数
 @param index 1 - 开启广播;0 - 关闭广播
 @return TRUE - 成功;FALSE - 失败
*/
bool mr_doAdvertise(uint8_t index)
{
    uint8_t adv;

    if(!index)                                                          // 关闭广播
    {
        adv = FALSE;
        GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &adv, NULL);
    }
    else                                                                // 开启广播
    {
        adv = TRUE;
        GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &adv, NULL);
    }

    return TRUE;
}

四、广播相关事件

在执行完开启广播函数或者停止广播函数后,会产生相应的事件。 以SDK2.4 multi_role工程为例,在 multi_role_processRoleEvent() 处理多角色事件函数中,

4.1 开启广播事件

switch(pEvent->gap.opcode)
{
    /*===================================== 开启广播事件 =====================================*/
    case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
    {
        /* Display_print0(dispHandle, MR_ROW_ADV, 0, "Advertising"); */
    }
    break;
}

4.2 关闭广播事件

switch(pEvent->gap.opcode)
{
    /*===================================== 关闭广播事件 =====================================*/
    case GAP_END_DISCOVERABLE_DONE_EVENT:
    {
        if(linkDB_NumActive() < maxNumBleConns)
        {
            /* Display_print0(dispHandle, MR_ROW_ADV, 0, "Ready to Advertise"); */
        }
        else
        {
            /* Display_print0(dispHandle, MR_ROW_ADV, 0, "Can't Adv : Max conns reached"); */
        }
    }
    break;
}

• 由 Leung 写于 2019 年 3 月 28 日

• 参考:simplelink_cc2640r2_sdk_2_40_00_32 [提取码:3pg6]