Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bt discovery fix (#880) #2

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct decompose {
bool reverse;
};

vector<BLEdevice> devices;
vector<BLEdevice*> devices;
int newDevices = 0;

static BLEdevice NO_DEVICE_FOUND = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false, false, false, UNKNOWN_MODEL};
static bool oneWhite = false;
Expand Down Expand Up @@ -194,9 +195,9 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
BLEdevice* getDeviceByMac(const char* mac) {
Log.trace(F("getDeviceByMac %s" CR), mac);

for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
if ((strcmp(p->macAdr, mac) == 0)) {
return &(*p);
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
if ((strcmp((*it)->macAdr, mac) == 0)) {
return *it;
}
}
return &NO_DEVICE_FOUND;
Expand Down Expand Up @@ -234,7 +235,8 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
device->isWhtL = flags & device_flags_isWhiteL;
device->isBlkL = flags & device_flags_isBlackL;
if (model != UNKNOWN_MODEL) device->sensorModel = model;
devices.push_back(*device);
devices.push_back(device);
newDevices++;
} else {
Log.trace(F("update %s" CR), mac);

Expand Down Expand Up @@ -263,7 +265,8 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
# define isDiscovered(device) device->isDisc

void dumpDevices() {
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
Log.trace(F("macAdr %s" CR), p->macAdr);
Log.trace(F("isDisc %d" CR), p->isDisc);
Log.trace(F("isWhtL %d" CR), p->isWhtL);
Expand Down Expand Up @@ -625,7 +628,8 @@ void notifyCB(
JsonObject& BLEdata = getBTJsonObject();
String mac_adress = pBLERemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString().c_str();
mac_adress.toUpperCase();
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
if (p->sensorModel == LYWSD03MMC)
BLEdata.set("model", "LYWSD03MMC");
Expand Down Expand Up @@ -657,7 +661,8 @@ void notifyCB(
void BLEconnect() {
Log.notice(F("BLE Connect begin" CR));
BLEDevice::init("");
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) {
Log.trace(F("Model to connect found" CR));
NimBLEClient* pClient;
Expand Down Expand Up @@ -730,10 +735,6 @@ void coreTask(void* pvParameters) {
// Launching a connect every BLEscanBeforeConnect
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
BLEconnect();
# ifdef ZmqttDiscovery
if (disc)
launchDiscovery();
# endif
dumpDevices();
}
if (lowpowermode) {
Expand Down Expand Up @@ -961,8 +962,22 @@ boolean valid_service_data(const char* data) {
return false;
}

void launchDiscovery() {
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
// This function always should be called from the main core as it generates direct mqtt messages
void launchBTDiscovery() {
if (newDevices == 0)
return;
# ifdef ESP32
if (!semaphoreCreateOrUpdateDevice.take(1000, "launchBTDiscovery"))
return;
newDevices = 0;
vector<BLEdevice*> localDevices = devices;
semaphoreCreateOrUpdateDevice.give();
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
# else
newDevices = 0;
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
# endif
BLEdevice* p = *it;
if (p->sensorModel != UNKNOWN_MODEL && !isDiscovered(p)) {
String macWOdots = String(p->macAdr);
macWOdots.replace(":", "");
Expand All @@ -983,9 +998,14 @@ void launchDiscovery() {
if (p->sensorModel == LYWSD03MMC || p->sensorModel == LYWSD03MMC_ATC) LYWSD03MMCDiscovery((char*)macWOdots.c_str(), "LYWSD03MMC");
if (p->sensorModel == MHO_C401) MHO_C401Discovery((char*)macWOdots.c_str(), "MHO_C401");
if (p->sensorModel == INODE_EM) INodeEMDiscovery((char*)macWOdots.c_str(), "INODE_EM");
createOrUpdateDevice(p->macAdr, device_flags_isDisc, p->sensorModel);
p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice
} else {
Log.trace(F("Device already discovered or UNKNOWN_MODEL" CR));
if (!isDiscovered(p)) {
Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr);
p->isDisc = true;
} else {
Log.trace(F("Device already discovered %s" CR), p->macAdr);
}
}
}
}
Expand Down Expand Up @@ -1416,8 +1436,16 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
WorBupdated |= updateWorB(BTdata, true);
WorBupdated |= updateWorB(BTdata, false);

if (WorBupdated)
if (WorBupdated) {
# ifdef ESP32
if (!semaphoreCreateOrUpdateDevice.take(1000, "dumpDevices")) {
dumpDevices();
semaphoreCreateOrUpdateDevice.give();
}
# else
dumpDevices();
# endif
}

// Scan interval set
if (BTdata.containsKey("interval")) {
Expand Down
2 changes: 1 addition & 1 deletion main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void createDiscovery(char* sensor_type,
deviceid[12] = '\0';
StaticJsonBuffer<JSON_MSG_BUFFER> jsonDeviceBuffer;
JsonObject& device = jsonDeviceBuffer.createObject();
if (device_mac != "") {
if (device_mac[0] != 0) {
JsonArray& connections = device.createNestedArray("connections");
JsonArray& connection_mac = connections.createNestedArray();
connection_mac.add("mac");
Expand Down
2 changes: 2 additions & 0 deletions main/config_BT.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
extern void setupBT();
extern bool BTtoMQTT();
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
extern void emptyBTQueue();
extern void launchBTDiscovery();

#ifdef ESP32
extern int btQueueBlocked;
Expand Down
4 changes: 4 additions & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,10 @@ void loop() {
PilighttoMQTT();
#endif
#ifdef ZgatewayBT
# ifdef ZmqttDiscovery
if (disc)
launchBTDiscovery();
# endif
# ifndef ESP32
if (BTtoMQTT())
Log.trace(F("BTtoMQTT OK" CR));
Expand Down