CH582 CH592 CH573 CH579 Observer增加白名单
白名单机制
白名单(white list)是BLE协议中最简单、直白的一种安全机制。其原理很简单,总结如下(前面的分析文章中都有介绍):
所谓的白名单,就是一组蓝牙地址;
通过白名单,可以只允许特定的蓝牙设备(白名单中列出的)扫描(Scan)、连接(connect)我们,也可以只扫描、连接特定的蓝牙设备(白名单中列出的)。
协议栈关于白名单的描述
4.3.1 White List The set of devices that the Link Layer uses for device filtering is called the White List. A White List contains a set of White List
Records used for Link Layer Device Filtering. A White List Record contains both the device address and the device address type
(public or random). All Link Layers supporting Link Layer DeviceFiltering shall support a White List capable of storing at least
one White List Record. On reset, the White List shall be empty. The White List is configured by the Host and is used by the Link Layer to filter advertisers,
scanners or initiators. This allows the Host to configure the Link Layer to act on a request without awakening the Host. All the device
filter policies shall use the same White List
Observer例子增加白名单(以CH582为例子),(白名单设置2个addr 01 5a 44 46 57 44 这个地址是20ms一次 00 5a 44 46 57 44 这个地址是100ms一包)
CH58X_BLEInit();初始化函数,因为设置2个addr,所以SNVNum设置为2,默认是1(否则 LL_AddWhiteListDevice 设置第二个地址的时候返回出错)
以下三个参数对应修改,第一个是扫描个数,第二个是扫描持续时间,第三个开启白名单(例子默认关闭)
// Maximum number of scan responses #define DEFAULT_MAX_SCAN_RES 1 // Scan duration in (625us) #define DEFAULT_SCAN_DURATION 10 // TRUE to use white list during discovery #define DEFAULT_DISCOVERY_WHITE_LIST TRUE
初始化增加广播包重复过滤(TRUE/FALSE)
GAP_SetParamValue(TGAP_FILTER_ADV_REPORTS,FALSE); #define TGAP_FILTER_ADV_REPORTS 21 //!< Filter duplicate advertising reports. Default TRUE. 过滤重复广播包,默认TRUE
初始化增加白名单设置,如下,addr如果不知道顺序就正反都试一下(实际是低字节在前,高字节在后)
void Observer_Init() { ObserverTaskId = TMOS_ProcessEventRegister(Observer_ProcessEvent); // Setup Observer Profile { uint8_t scanRes = DEFAULT_MAX_SCAN_RES; GAPRole_SetParameter(GAPROLE_MAX_SCAN_RES, sizeof(uint8_t), &scanRes); } // White List u8 WhiteListDevAddr[6]; WhiteListDevAddr[5]=0x44;WhiteListDevAddr[4]=0x57; WhiteListDevAddr[3]=0x46;WhiteListDevAddr[2]=0x44; WhiteListDevAddr[1]=0x5A;WhiteListDevAddr[0]=0x01; LL_AddWhiteListDevice(ADDRTYPE_PUBLIC,WhiteListDevAddr); //White List u8 WhiteListDevAddr1[6]; WhiteListDevAddr1[5]=0x44;WhiteListDevAddr1[4]=0x57; WhiteListDevAddr1[3]=0x46;WhiteListDevAddr1[2]=0x44; WhiteListDevAddr1[1]=0x5A;WhiteListDevAddr1[0]=0x00; PRINT("AddWhiteListRet= %x\n",LL_AddWhiteListDevice(ADDRTYPE_PUBLIC,WhiteListDevAddr1)); // Setup GAP GAP_SetParamValue(TGAP_DISC_SCAN, DEFAULT_SCAN_DURATION); // Setup a delayed profile startup tmos_set_event(ObserverTaskId, START_DEVICE_EVT); }
获取扫描数据,找到函数 static void ObserverEventCB(gapRoleEvent_t *pEvent) ,如下一个case,可以打印信息
case GAP_DEVICE_INFO_EVENT: { int i; ObserverAddDeviceInfo(pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType); PRINT("Addr: "); for(i = 0; i < 6; i++) { PRINT("%x ", pEvent->deviceInfo.addr[i]); } PRINT("\n"); for(i = 0; i < pEvent->deviceInfo.dataLen; i++) { PRINT("%x ", pEvent->deviceInfo.pEvtData[i]); } PRINT("\n"); } break;
运行结果如下(我用了921600的波特率 PA9 是TXD):