Merge pull request #1 from RobertoHE/Client_Upgrade

Update and simplify connect()
This commit is contained in:
RobertoHE 2021-08-05 17:41:40 +02:00 committed by GitHub
commit 6d00baaf53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 54 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:
@ -97,7 +97,7 @@ static uint32_t userOnPassKeyRequest()
#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms #define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms #define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
#define BLEMIDI_CLIENT_COMM_LATENCY 0 #define BLEMIDI_CLIENT_COMM_LATENCY 0
#define BLEMIDI_CLIENT_COMM_TIMEOUT 400 //4000ms #define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms
/* /*
############################################# #############################################
@ -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);
@ -307,7 +309,7 @@ protected:
} }
//Try reconnection or search a new one //Try reconnection or search a new one
NimBLEDevice::getScan()->start(3, scanEndedCB); NimBLEDevice::getScan()->start(1, scanEndedCB);
} }
bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params) bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
@ -419,7 +421,6 @@ 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
receive(pData, length); receive(pData, length);
} }
} }
@ -439,13 +440,12 @@ void BLEMIDI_Client_ESP32::scan()
pBLEScan->setActiveScan(true); pBLEScan->setActiveScan(true);
Serial.println("Scanning..."); Serial.println("Scanning...");
pBLEScan->start(3, scanEndedCB); pBLEScan->start(1, scanEndedCB);
} }
}; };
bool BLEMIDI_Client_ESP32::connect() bool BLEMIDI_Client_ESP32::connect()
{ {
Serial.println("Try Connection...");
using namespace std::placeholders; //<- for bind funtion in callback notification using namespace std::placeholders; //<- for bind funtion in callback notification
/** Check if we have a client we should reuse first /** Check if we have a client we should reuse first
@ -461,26 +461,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 +486,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 +501,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 +519,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 */