Update and simplify connect()

(UNTESTED)
Connect() method is simplified. Only one client connection by object is allowed now. If any connection step fails during connection setup, this client is deleted and reset. This deleted client doesn't ocupe for NIMBLE_MAX_CONNECTIONS count now.
Some Serial.print() traces was commented.
Now NotifyCB() doesn't check space available in rx buffer.
This commit is contained in:
RobertoHE 2021-08-05 10:58:02 +02:00 committed by GitHub
parent 4e52f70062
commit 895089174b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 48 deletions

View File

@ -78,7 +78,7 @@ static uint32_t userOnPassKeyRequest()
return passkey; return passkey;
}; };
/* /*
###### BLE COMMUNICATION PARAMS ###### ###### BLE COMMUNICATION PARAMS ######
*/ */
/** Set connection parameters: /** Set connection parameters:
@ -162,7 +162,7 @@ protected:
{ {
/** Ready to connect now */ /** Ready to connect now */
doConnect = true; doConnect = true;
/** Save the device reference in a public variable the client can use*/ /** Save the device reference in a public variable that the client can use*/
advDevice = *advertisedDevice; advDevice = *advertisedDevice;
/** stop scan before connecting */ /** stop scan before connecting */
NimBLEDevice::getScan()->stop(); NimBLEDevice::getScan()->stop();
@ -259,8 +259,10 @@ protected:
void disconnected() void disconnected()
{ {
if (_bleMidiTransport->_disconnectedCallback) if (_bleMidiTransport->_disconnectedCallback)
{
_bleMidiTransport->_disconnectedCallback(); _bleMidiTransport->_disconnectedCallback();
} }
}
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify); void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@ -419,7 +421,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
{ {
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
{ {
if (uxQueueSpacesAvailable(mRxQueue) >= length) //Don't overflow the queue. If the queue is overflowed, comunication breaks out //if (uxQueueSpacesAvailable(mRxQueue) >= length) //Don't overflow the queue.
receive(pData, length); receive(pData, length);
} }
} }
@ -461,26 +463,23 @@ bool BLEMIDI_Client_ESP32::connect()
{ {
if (_characteristic->canNotify()) if (_characteristic->canNotify())
{ {
if (!_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4)))
{
Serial.println("Error - Not subscribe");
/** Disconnect if subscribe failed */
_client->disconnect();
return false;
}
else
{ {
//Re-connection SUCCESS
return true; return true;
} }
} }
/** Disconnect if subscribe failed */
_client->disconnect();
} }
else /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
{ NimBLEDevice::deleteClient(_client);
Serial.println("Error. Reconnect failed");
_client = nullptr; _client = nullptr;
return false; return false;
} }
} /*If client does not match, delete previous client and create a new one*/
NimBLEDevice::deleteClient(_client);
_client = nullptr;
} }
if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS)
@ -489,11 +488,13 @@ bool BLEMIDI_Client_ESP32::connect()
return false; return false;
} }
// Create and setup a new client
_client = BLEDevice::createClient(); _client = BLEDevice::createClient();
_client->setClientCallbacks(new MyClientCallbacks(this), false); _client->setClientCallbacks(new MyClientCallbacks(this), false);
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL + 10, BLEMIDI_CLIENT_COMM_LATENCY + 1, BLEMIDI_CLIENT_COMM_TIMEOUT + 10); _client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */ /** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
_client->setConnectTimeout(15); _client->setConnectTimeout(15);
@ -502,14 +503,15 @@ bool BLEMIDI_Client_ESP32::connect()
/** Created a client but failed to connect, don't need to keep it as it has no data */ /** Created a client but failed to connect, don't need to keep it as it has no data */
NimBLEDevice::deleteClient(_client); NimBLEDevice::deleteClient(_client);
_client = nullptr; _client = nullptr;
Serial.println("Failed to connect, deleted client"); //Serial.println("Failed to connect, deleted client");
return false; return false;
} }
if (!_client->isConnected()) if (!_client->isConnected())
{ {
Serial.println("Failed to connect"); //Serial.println("Failed to connect");
_client->disconnect(); _client->disconnect();
NimBLEDevice::deleteClient(_client);
_client = nullptr; _client = nullptr;
return false; return false;
} }
@ -519,44 +521,35 @@ bool BLEMIDI_Client_ESP32::connect()
Serial.print(" / "); Serial.print(" / ");
Serial.println(_client->getPeerAddress().toString().c_str()); Serial.println(_client->getPeerAddress().toString().c_str());
/*
Serial.print("RSSI: "); Serial.print("RSSI: ");
Serial.println(_client->getRssi()); Serial.println(_client->getRssi());
*/
/** Now we can read/write/subscribe the charateristics of the services we are interested in */ /** Now we can read/write/subscribe the charateristics of the services we are interested in */
pSvc = _client->getService(SERVICE_UUID); pSvc = _client->getService(SERVICE_UUID);
if (pSvc) if (pSvc) /** make sure it's not null */
{ /** make sure it's not null */ {
_characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID); _characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
if (_characteristic) if (_characteristic) /** make sure it's not null */
{ /** make sure it's not null */ {
if (_characteristic->canNotify()) if (_characteristic->canNotify())
{ {
if (!_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4)))
{ {
Serial.println("Error - Subcribe error"); //Connection SUCCESS
/** Disconnect if subscribe failed */ return true;
_client->disconnect();
return false;
} }
} }
else
{
return false;
} }
} }
else
{
return false;
}
}
else
{
Serial.println("Error. MIDI service not found.");
return false;
}
return true; //If anything fails, disconnect and delete client
_client->disconnect();
NimBLEDevice::deleteClient(_client);
_client = nullptr;
return false;
}; };
/** Callback to process the results of the last scan or restart it */ /** Callback to process the results of the last scan or restart it */