Skip to content

Commit

Permalink
Code changes for #291
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolban committed Dec 18, 2017
1 parent e2257d9 commit 3d39b22
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cpp_utils/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
} // setAppearance


/**
* @brief Set the filtering for the scan filter.
* @param [in] scanRequestWhitelistOnly If true, only allow scan requests from those on the white list.
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
*/
void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
ESP_LOGD(LOG_TAG, ">> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d", scanRequestWhitelistOnly, connectWhitelistOnly);
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
return;
}
if (scanRequestWhitelistOnly && !connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
return;
}
if (!scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
return;
}
if (scanRequestWhitelistOnly && connectWhitelistOnly) {
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST;
ESP_LOGD(LOG_TAG, "<< setScanFilter");
return;
}
} // setScanFilter


/**
* @brief Set the advertisement data that is to be published in a regular advertisement.
* @param [in] advertisementData The data to be advertised.
Expand Down Expand Up @@ -417,4 +447,5 @@ void BLEBeacon::setSignalPower(uint16_t signalPower) {
} // setSignalPower



#endif /* CONFIG_BT_ENABLED */
1 change: 1 addition & 0 deletions cpp_utils/BLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class BLEAdvertising {
void stop();
void setAppearance(uint16_t appearance);
void setAdvertisementData(BLEAdvertisementData& advertisementData);
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
void setScanResponseData(BLEAdvertisementData& advertisementData);

private:
Expand Down
28 changes: 28 additions & 0 deletions cpp_utils/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,32 @@ bool initialized = false; // Have we been initialized?
return oss.str();
} // toString


/**
* @brief Add an entry to the BLE white list.
* @param [in] address The address to add to the white list.
*/
void BLEDevice::whiteListAdd(BLEAddress address) {
ESP_LOGD(LOG_TAG, ">> whiteListAdd: %s", address.toString().c_str());
esp_err_t errRc = esp_ble_gap_update_whitelist(true, *address.getNative()); // True to add an entry.
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_update_whitelist: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
ESP_LOGD(LOG_TAG, "<< whiteListAdd");
} // whiteListAdd


/**
* @brief Remove an entry from the BLE white list.
* @param [in] address The address to remove from the white list.
*/
void BLEDevice::whiteListRemove(BLEAddress address) {
ESP_LOGD(LOG_TAG, ">> whiteListRemove: %s", address.toString().c_str());
esp_err_t errRc = esp_ble_gap_update_whitelist(false, *address.getNative()); // False to remove an entry.
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_update_whitelist: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
ESP_LOGD(LOG_TAG, "<< whiteListRemove");
} // whiteListRemove

#endif // CONFIG_BT_ENABLED
2 changes: 2 additions & 0 deletions cpp_utils/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class BLEDevice {
static void setPower(esp_power_level_t powerLevel); // Set our power level.
static void setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a characteristic on a service on a server.
static std::string toString(); // Return a string representation of our device.
static void whiteListAdd(BLEAddress address); // Add an entry to the BLE white list.
static void whiteListRemove(BLEAddress address); // Remove an entry from the BLE white list.

private:
static BLEServer *m_pServer;
Expand Down

0 comments on commit 3d39b22

Please sign in to comment.