Early whitelisting API prototype implementation

This commit is contained in:
Andres Amaya Garcia 2015-12-31 13:51:52 +00:00
parent 31c4a7c475
commit fdead9ec36
2 changed files with 117 additions and 7 deletions

View file

@ -173,8 +173,8 @@ ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
adv_para.type = params.getAdvertisingType();
adv_para.p_peer_addr = NULL; // Undirected advertisement
adv_para.fp = BLE_GAP_ADV_FP_ANY;
adv_para.p_whitelist = NULL;
adv_para.fp = advertisingPolicyMode;
adv_para.p_whitelist = &whitelist;
adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
adv_para.timeout = params.getTimeout();
@ -234,8 +234,8 @@ ble_error_t nRF5xGap::connect(const Address_t peerAddr,
}
ble_gap_scan_params_t scanParams;
scanParams.selective = 0; /**< If 1, ignore unknown devices (non whitelisted). */
scanParams.p_whitelist = NULL; /**< Pointer to whitelist, NULL if none is given. */
scanParams.selective = scanningPolicyMode; /**< If 1, ignore unknown devices (non whitelisted). */
scanParams.p_whitelist = &whitelist; /**< Pointer to whitelist, NULL if none is given. */
if (scanParamsIn != NULL) {
scanParams.active = scanParamsIn->getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
@ -507,3 +507,65 @@ void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *co
*valueArrayPP = permittedTxValues;
*countP = sizeof(permittedTxValues) / sizeof(int8_t);
}
/////////////////////////WHITELISTING
int8_t nRF5xGap::getMaxWhitelistSize(void) const
{
return YOTTA_CFG_WHITELIST_MAX_SIZE;
}
ble_error_t nRF5xGap::getWhitelist(std::set<BLEProtocol::Address_t> &whitelist) const
{
for (uint8_t i = 0; i < whitelistAddressesSize; i++) {
BLEProtocol::Address_t addr;//((BLEProtocol::AddressType_t)whitelistAddrs[i].addr_type, (BLEProtocol::AddressBytes_t) whitelistAddrs[i].addr);
addr.type = (BLEProtocol::AddressType_t) whitelistAddrs[i].addr_type;
memcpy(addr.address, whitelistAddrs[i].addr, sizeof(BLEProtocol::AddressBytes_t));
whitelist.insert(addr);
}
return BLE_ERROR_NONE;
}
ble_error_t nRF5xGap::setWhitelist(std::set<BLEProtocol::Address_t> whitelistIn)
{
whitelistAddressesSize = 0;
for (std::set<BLEProtocol::Address_t>::iterator it = whitelistIn.begin(); it != whitelistIn.end(); it++) {
whitelistAddrs[whitelistAddressesSize].addr_type = it->type;
memcpy(whitelistAddrs[whitelistAddressesSize].addr, it->address, sizeof(BLEProtocol::AddressBytes_t));
whitelistAddressesSize++;
}
whitelist.addr_count = whitelistAddressesSize;
whitelist.irk_count = whitelistIrksSize;
return BLE_ERROR_NONE;
}
// Accessors
void nRF5xGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode)
{
advertisingPolicyMode = mode;
}
void nRF5xGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
{
scanningPolicyMode = mode;
}
void nRF5xGap::setInitiatorPolicyMode(Gap::InitiatorPolicyMode_t mode)
{
initiatorPolicyMode = mode;
}
Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode(void) const
{
return advertisingPolicyMode;
}
Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode(void) const
{
return scanningPolicyMode;
}
Gap::InitiatorPolicyMode_t nRF5xGap::getInitiatorPolicyMode(void) const
{
return initiatorPolicyMode;
}
/////////////////////////

View file

@ -22,6 +22,12 @@
#else
#include "mbed.h"
#endif
#ifndef YOTTA_CFG_WHITELIST_MAX_SIZE
#define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
#elif YOTTA_CFG_WHITELIST_MAX_SIZE > BLE_GAP_WHITELIST_ADDR_MAX_COUNT
#undef YOTTA_CFG_WHITELIST_MAX_SIZE
#define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
#endif
#include "ble/blecommon.h"
#include "ble.h"
#include "ble/GapAdvertisingParams.h"
@ -37,6 +43,8 @@ extern "C" {
#include "btle_security.h"
#include <set>
void radioNotificationStaticCallback(bool param);
/**************************************************************************/
@ -80,6 +88,20 @@ public:
virtual ble_error_t reset(void);
/////////////////// WHITELISTING
virtual int8_t getMaxWhitelistSize(void) const;
virtual ble_error_t getWhitelist(std::set<BLEProtocol::Address_t> &whitelist) const;
virtual ble_error_t setWhitelist(std::set<BLEProtocol::Address_t> whitelist);
// Accessors
virtual void setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
virtual void setScanningPolicyMode(ScanningPolicyMode_t mode);
virtual void setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
virtual Gap::AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
virtual Gap::ScanningPolicyMode_t getScanningPolicyMode(void) const;
virtual Gap::InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
///////////////////
virtual ble_error_t initRadioNotification(void) {
if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
return BLE_ERROR_NONE;
@ -93,8 +115,8 @@ public:
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
ble_gap_scan_params_t scanParams = {
.active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
.selective = 0, /**< If 1, ignore unknown devices (non whitelisted). */
.p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */
.selective = scanningPolicyMode, /**< If 1, ignore unknown devices (non whitelisted). */
.p_whitelist = &whitelist, /**< Pointer to whitelist, NULL if none is given. */
.interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
@ -117,6 +139,21 @@ public:
#endif
private:
/////////////////WHITELISTING
Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
Gap::ScanningPolicyMode_t scanningPolicyMode;
Gap::InitiatorPolicyMode_t initiatorPolicyMode; /* Unused */
ble_gap_addr_t whitelistAddrs[YOTTA_CFG_WHITELIST_MAX_SIZE];
ble_gap_addr_t *whitelistAddresses[YOTTA_CFG_WHITELIST_MAX_SIZE];
uint8_t whitelistAddressesSize;
ble_gap_irk_t *whitelistIrks[YOTTA_CFG_WHITELIST_MAX_SIZE];
uint8_t whitelistIrksSize;
ble_gap_whitelist_t whitelist;
/////////////////
bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */
Timeout radioNotificationTimeout;
@ -206,8 +243,19 @@ private:
*/
friend class nRF5xn;
nRF5xGap() {
nRF5xGap() :
advertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST),
scanningPolicyMode(Gap::SCAN_POLICY_IGNORE_WHITELIST),
initiatorPolicyMode(Gap::INIT_POLICY_IGNORE_WHITELIST),
whitelistAddressesSize(0),
whitelistIrksSize(0) {
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
whitelist.pp_addrs = whitelistAddresses;
for (int i = 0; i < YOTTA_CFG_WHITELIST_MAX_SIZE; i++) {
whitelistAddresses[i] = &(whitelistAddrs[i]);
}
whitelist.pp_irks = whitelistIrks;
}
nRF5xGap(nRF5xGap const &);