Skip to content

Commit

Permalink
Added BLE filter by name feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fellev committed Nov 23, 2024
1 parent bf872de commit cd4c5fe
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions tasmota/tasmota_xdrv_driver/xdrv_79_esp32_ble.ino
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
BLEEnableUnsaved
*0/1 - if BLE is disabled, this can be used to enable BLE without
it being saved - useful as the last command in autoexec.bat
BLEFilterNames
BLEFilterNames0 - clear filter list
BLEFilterNames1 - <name1>,<name2> - set one or more device names
Other drivers can add callbacks to receive advertisements
Other drivers can add 'operations' to be performed and receive callbacks from the operation's success or failure
Expand All @@ -134,6 +137,7 @@ i.e. the Bluetooth of the ESP can be shared without conflict.
*/

#define BLE_ESP32_ALIASES
#define BLE_ESP32_FILTER_BY_NAME

// uncomment for more diagnostic/information messages - + more flash use.
//#define BLE_ESP32_DEBUG
Expand Down Expand Up @@ -472,6 +476,9 @@ std::deque<BLE_ESP32::SCANCOMPLETE_CALLBACK*> scancompleteCallbacks;
std::deque<BLE_ESP32::ble_alias_t*> aliases;
#endif

#ifdef BLE_ESP32_FILTER_BY_NAME
std::vector<String> bleFilterNames;
#endif

/*********************************************************************************************\
* constants
Expand All @@ -480,7 +487,7 @@ std::deque<BLE_ESP32::ble_alias_t*> aliases;
#define D_CMND_BLE "BLE"

const char kBLE_Commands[] PROGMEM = D_CMND_BLE "|"
"Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter|EnableUnsaved";
"Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter|EnableUnsaved|FilterNames";

static void CmndBLEPeriod(void);
static void CmndBLEAdv(void);
Expand All @@ -495,6 +502,7 @@ static void CmndBLEDevices(void);
static void CmndBLEMaxAge(void);
static void CmndBLEAddrFilter(void);
static void CmndBLEEnableUnsaved(void);
static void CmndBleFilterNames(void);

void (*const BLE_Commands[])(void) PROGMEM = {
&BLE_ESP32::CmndBLEPeriod,
Expand All @@ -509,7 +517,8 @@ void (*const BLE_Commands[])(void) PROGMEM = {
&BLE_ESP32::CmndBLEDevices,
&BLE_ESP32::CmndBLEMaxAge,
&BLE_ESP32::CmndBLEAddrFilter,
&BLE_ESP32::CmndBLEEnableUnsaved
&BLE_ESP32::CmndBLEEnableUnsaved,
&BLE_ESP32::CmndBleFilterNames
};

const char *successStates[] PROGMEM = {
Expand Down Expand Up @@ -1128,7 +1137,24 @@ void ReverseMAC(uint8_t _mac[]){
}



/**
* @brief Search for device name in filer list
*
* @param deviceName device name string
*/
#ifdef BLE_ESP32_FILTER_BY_NAME
bool isDeviceInFilter(const String& deviceName) {
#ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: Device chcked in filter %s"), deviceName);
#endif
for (const auto& filterName : bleFilterNames) {
if (deviceName == filterName) {
return true;
}
}
return false;
}
#endif

/*********************************************************************************************\
* Advertisment details
Expand Down Expand Up @@ -1372,9 +1398,18 @@ class BLEAdvCallbacks: public NimBLEScanCallbacks {
BLEAdvertisment.name[sizeof(BLEAdvertisment.name)-1] = 0;
}

int filter = 0;
#ifdef BLE_ESP32_FILTER_BY_NAME
if (!bleFilterNames.empty()) {
if (!advertisedDevice->haveName() || !isDeviceInFilter(namestr))
{
filter = 1;
}
}
#endif

// log this device safely
if (BLEAdvertisment.addrtype <= BLEAddressFilter){
if ((BLEAdvertisment.addrtype <= BLEAddressFilter) && (0 == filter) ){
addSeenDevice(BLEAdvertisment.addr, BLEAdvertisment.addrtype, BLEAdvertisment.name, BLEAdvertisment.RSSI);
}

Expand Down Expand Up @@ -2833,6 +2868,49 @@ void CmndBLEDetails(void){
}


void CmndBleFilterNames(void) {
#ifdef BLE_ESP32_FILTER_BY_NAME
int op = XdrvMailbox.index;
#ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: Name %d %s"), op, XdrvMailbox.data);
#endif

switch(op){
case 0:{
bleFilterNames.clear();
ResponseCmndDone();
} break;
case 1:{
if (XdrvMailbox.data_len) {
String filters = XdrvMailbox.data;
bleFilterNames.clear();

int start = 0;
int end = filters.indexOf(',');
while (end != -1) {
bleFilterNames.push_back(filters.substring(start, end));
start = end + 1;
end = filters.indexOf(',', start);
}
bleFilterNames.push_back(filters.substring(start));

Response_P(PSTR("{\"BLEFilterNames\":\"%s\"}"), filters.c_str());
} else {
String filterList;
for (const auto& name : bleFilterNames) {
if (!filterList.isEmpty()) {
filterList += ", ";
}
filterList += name;
}

Response_P(PSTR("{\"BLEFilterNames\":\"%s\"}"), filterList.c_str());
}
} break;
}
#endif
}

void CmndBLEAlias(void){
#ifdef BLE_ESP32_ALIASES
int op = XdrvMailbox.index;
Expand Down

0 comments on commit cd4c5fe

Please sign in to comment.