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;
};
/*
/*
###### BLE COMMUNICATION PARAMS ######
*/
/** Set connection parameters:
@ -94,10 +94,10 @@ static uint32_t userOnPassKeyRequest()
* 0 latency (Number of intervals allowed to skip),
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
*/
#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
#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 */
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;
/** stop scan before connecting */
NimBLEDevice::getScan()->stop();
@ -259,7 +259,9 @@ protected:
void disconnected()
{
if (_bleMidiTransport->_disconnectedCallback)
{
_bleMidiTransport->_disconnectedCallback();
}
}
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@ -307,7 +309,7 @@ protected:
}
//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)
@ -419,7 +421,6 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
{
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);
}
}
@ -439,13 +440,12 @@ void BLEMIDI_Client_ESP32::scan()
pBLEScan->setActiveScan(true);
Serial.println("Scanning...");
pBLEScan->start(3, scanEndedCB);
pBLEScan->start(1, scanEndedCB);
}
};
bool BLEMIDI_Client_ESP32::connect()
{
Serial.println("Try Connection...");
using namespace std::placeholders; //<- for bind funtion in callback notification
/** Check if we have a client we should reuse first
@ -461,26 +461,23 @@ bool BLEMIDI_Client_ESP32::connect()
{
if (_characteristic->canNotify())
{
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
if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4)))
{
//Re-connection SUCCESS
return true;
}
}
}
else
{
Serial.println("Error. Reconnect failed");
_client = nullptr;
return false;
}
/** Disconnect if subscribe failed */
_client->disconnect();
}
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
NimBLEDevice::deleteClient(_client);
_client = nullptr;
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)
@ -488,12 +485,14 @@ bool BLEMIDI_Client_ESP32::connect()
Serial.println("Max clients reached - no more connections available");
return false;
}
// Create and setup a new client
_client = BLEDevice::createClient();
_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. */
_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 */
NimBLEDevice::deleteClient(_client);
_client = nullptr;
Serial.println("Failed to connect, deleted client");
//Serial.println("Failed to connect, deleted client");
return false;
}
if (!_client->isConnected())
{
Serial.println("Failed to connect");
//Serial.println("Failed to connect");
_client->disconnect();
NimBLEDevice::deleteClient(_client);
_client = nullptr;
return false;
}
@ -519,44 +519,35 @@ bool BLEMIDI_Client_ESP32::connect()
Serial.print(" / ");
Serial.println(_client->getPeerAddress().toString().c_str());
/*
Serial.print("RSSI: ");
Serial.println(_client->getRssi());
*/
/** Now we can read/write/subscribe the charateristics of the services we are interested in */
pSvc = _client->getService(SERVICE_UUID);
if (pSvc)
{ /** make sure it's not null */
if (pSvc) /** make sure it's not null */
{
_characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
if (_characteristic)
{ /** make sure it's not null */
if (_characteristic) /** make sure it's not null */
{
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");
/** Disconnect if subscribe failed */
_client->disconnect();
return false;
//Connection SUCCESS
return true;
}
}
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 */