[BLE] Simplify the publication of advertisement data (#1408)

With only one control that enables the publication of all the advertisement data, default: false
Note that if the sensor is not recognized, we will no longer publish its advertisement data unless the control is true. This is for the sake of traffic reduction.
This commit is contained in:
Florian authored and GitHub committed 2023-01-20 14:30:11 -06:00
1 parent 27b568e4f9
commit a3603c6a78
5 files changed
+41 -85

No files matched your search

+13 -34
View File
@@ -92,11 +92,7 @@ void BTConfig_init() {
BTConfig.extDecoderEnable = UseExtDecoder;
BTConfig.extDecoderTopic = MQTTDecodeTopic;
BTConfig.filterConnectable = BLE_FILTER_CONNECTABLE;
BTConfig.pubKnownServiceData = pubKnownBLEServiceData;
BTConfig.pubUnknownServiceData = pubUnknownBLEServiceData;
BTConfig.pubKnownManufData = pubBLEManufacturerData;
BTConfig.pubUnknownManufData = pubUnknownBLEManufacturerData;
BTConfig.pubServiceDataUUID = pubBLEServiceUUID;
BTConfig.pubAdvData = pubBLEAdvData;
BTConfig.pubBeaconUuidForTopic = useBeaconUuidForTopic;
BTConfig.ignoreWBlist = false;
}
@@ -141,16 +137,8 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
BTConfig_update(BTdata, "extDecoderTopic", BTConfig.extDecoderTopic);
// Sets whether to filter publishing
BTConfig_update(BTdata, "filterConnectable", BTConfig.filterConnectable);
// Publish service data belonging to recognised sensors
BTConfig_update(BTdata, "pubKnownServiceData", BTConfig.pubKnownServiceData);
// Publish service data belonging to unrecognised sensors
BTConfig_update(BTdata, "pubUnknownServiceData", BTConfig.pubUnknownServiceData);
// Publish known manufacturer's data
BTConfig_update(BTdata, "pubKnownManufData", BTConfig.pubKnownManufData);
// Publish unknown manufacturer's data
BTConfig_update(BTdata, "pubUnknownManufData", BTConfig.pubUnknownManufData);
// Publish the service UUID data
BTConfig_update(BTdata, "pubServiceDataUUID", BTConfig.pubServiceDataUUID);
// Publish advertisment data
BTConfig_update(BTdata, "pubadvdata", BTConfig.pubAdvData);
// Use iBeacon UUID as topic, instead of sender (random) MAC address
BTConfig_update(BTdata, "pubBeaconUuidForTopic", BTConfig.pubBeaconUuidForTopic);
// Disable Whitelist & Blacklist
@@ -170,11 +158,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
jo["extDecoderEnable"] = BTConfig.extDecoderEnable;
jo["extDecoderTopic"] = BTConfig.extDecoderTopic;
jo["filterConnectable"] = BTConfig.filterConnectable;
jo["pubKnownServiceData"] = BTConfig.pubKnownServiceData;
jo["pubUnknownServiceData"] = BTConfig.pubUnknownServiceData;
jo["pubKnownManufData"] = BTConfig.pubKnownManufData;
jo["pubUnknownManufData"] = BTConfig.pubUnknownManufData;
jo["pubServiceDataUUID"] = BTConfig.pubServiceDataUUID;
jo["pubadvdata"] = BTConfig.pubAdvData;
jo["pubBeaconUuidForTopic"] = BTConfig.pubBeaconUuidForTopic;
jo["ignoreWBlist"] = BTConfig.ignoreWBlist;
@@ -544,6 +528,7 @@ void procBLETask(void* pvParameters) {
mac_adress.toUpperCase();
BLEdata["id"] = (char*)mac_adress.c_str();
BLEdata["mac_type"] = advertisedDevice->getAddress().getType();
BLEdata["adv_type"] = advertisedDevice->getAdvType();
Log.notice(F("Device detected: %s" CR), (char*)mac_adress.c_str());
BLEdevice* device = getDeviceByMac(BLEdata["id"].as<const char*>());
@@ -827,13 +812,6 @@ bool BTtoMQTT() { // for on demand BLE scans
return true;
}
void RemoveJsonPropertyIf(JsonObject& obj, const char* key, bool condition) {
if (condition) {
Log.trace(F("Removing %s" CR), key);
obj.remove(key);
}
}
boolean valid_service_data(const char* data, int size) {
for (int i = 0; i < size; ++i) {
if (data[i] != 48) // 48 correspond to 0 in ASCII table
@@ -946,14 +924,15 @@ void launchBTDiscovery(bool overrideDiscovery) {
void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) {
if (abs((int)BLEdata["rssi"] | 0) < abs(BTConfig.minRssi)) { // process only the devices close enough
if (processBLEData) process_bledata(BLEdata);
if (!BTConfig.pubOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) {
RemoveJsonPropertyIf(BLEdata, "servicedatauuid", !BTConfig.pubServiceDataUUID && BLEdata.containsKey("model"));
RemoveJsonPropertyIf(BLEdata, "servicedata", !BTConfig.pubKnownServiceData && BLEdata.containsKey("model"));
RemoveJsonPropertyIf(BLEdata, "manufacturerdata", !BTConfig.pubKnownManufData && BLEdata.containsKey("model"));
if (!BTConfig.pubAdvData) {
BLEdata.remove("servicedatauuid");
BLEdata.remove("servicedata");
BLEdata.remove("manufacturerdata");
BLEdata.remove("mac_type");
BLEdata.remove("adv_type");
}
if (!BTConfig.pubOnlySensors || BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { // Identified device
pubBT(BLEdata);
} else {
RemoveJsonPropertyIf(BLEdata, "servicedata", !BTConfig.pubUnknownServiceData);
RemoveJsonPropertyIf(BLEdata, "manufacturerdata", !BTConfig.pubUnknownManufData && BLEdata.containsKey("model"));
}
} else if (BLEdata.containsKey("distance")) {
pubBT(BLEdata);
+10
View File
@@ -997,6 +997,16 @@ void pubMqttDiscovery() {
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
createDiscovery("switch", //set Type
subjectBTtoMQTT, "BT: Publish Advertisement data", (char*)getUniqueId("pubadvdata", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.pubadvdata }}", //set availability_topic,device_class,value_template,
"{\"pubadvdata\":true,\"save\":true}", "{\"pubadvdata\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
createDiscovery("switch", //set Type
subjectBTtoMQTT, "BT: Connect to devices", (char*)getUniqueId("bleconnect", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.bleconnect }}", //set availability_topic,device_class,value_template,
+3 -23
View File
@@ -112,24 +112,8 @@ extern int btQueueLengthCount;
unsigned long scanCount = 0;
#ifndef pubKnownBLEServiceData
# define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors
#endif
#ifndef pubUnknownBLEServiceData
# define pubUnknownBLEServiceData true // define false if you don't want to publish service data to unrecognised sensors (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707
#endif
#ifndef pubBLEManufacturerData
# define pubBLEManufacturerData false // define true if you want to publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client)
#endif
#ifndef pubUnknownBLEManufacturerData
# define pubUnknownBLEManufacturerData true // define true if you want to publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client)
#endif
#ifndef pubBLEServiceUUID
# define pubBLEServiceUUID false // define true if you want to publish the service UUID data
#ifndef pubBLEAdvData
# define pubBLEAdvData false // define true if you want to publish all advertisement data
#endif
#ifndef useBeaconUuidForTopic
@@ -157,11 +141,7 @@ struct BTConfig_s {
bool extDecoderEnable; // Send undecoded device data to another gateway device for decoding
String extDecoderTopic; // Topic to send undecoded device data on
bool filterConnectable; // Sets whether to filter publishing of scanned devices that require a connection.
bool pubKnownServiceData; // Publish service data belonging to recognised sensors
bool pubUnknownServiceData; // Publish service data belonging to unrecognised sensors (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707
bool pubKnownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client)
bool pubUnknownManufData; // Publish the manufacturer's data (sometimes contains characters that aren't valid with receiving client)
bool pubServiceDataUUID; // Publish the service UUID data
bool pubAdvData; // Publish advertisement data
bool pubBeaconUuidForTopic; // Use iBeacon UUID as topic, instead of sender (random) MAC address
bool ignoreWBlist; // Disable Whitelist & Blacklist
};