Merge pull request #36 from RobertoHE/master

Improve transmissions in client mode (thank you @RobertoHE)
This commit is contained in:
lathoub 2021-09-21 22:06:03 +02:00 committed by GitHub
commit 927b3ac8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 21 deletions

View File

@ -37,6 +37,24 @@
#define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT" #define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT"
#endif //Not modify #endif //Not modify
/*
###### TX POWER #####
*/
/**
* Set power transmision
*
* ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum
* ESP_PWR_LVL_N9 // Corresponding to -9dbm
* ESP_PWR_LVL_N6 // Corresponding to -6dbm
* ESP_PWR_LVL_N3 // Corresponding to -3dbm
* ESP_PWR_LVL_N0 // Corresponding to 0dbm
* ESP_PWR_LVL_P3 // Corresponding to +3dbm
* ESP_PWR_LVL_P6 // Corresponding to +6dbm
* ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum
*/
#define BLEMIDI_TX_PWR ESP_PWR_LVL_P9
/* /*
###### SECURITY ##### ###### SECURITY #####
*/ */
@ -58,7 +76,7 @@
* Uncomment what you need * Uncomment what you need
* These are the default values. * These are the default values.
*/ */
//#define BLEMIDI_CLIENT_BOND #define BLEMIDI_CLIENT_BOND
//#define BLEMIDI_CLIENT_MITM //#define BLEMIDI_CLIENT_MITM
#define BLEMIDI_CLIENT_PAIR #define BLEMIDI_CLIENT_PAIR
@ -78,7 +96,7 @@ static uint32_t userOnPassKeyRequest()
return passkey; return passkey;
}; };
/* /*
###### BLE COMMUNICATION PARAMS ###### ###### BLE COMMUNICATION PARAMS ######
*/ */
/** Set connection parameters: /** Set connection parameters:
@ -94,10 +112,10 @@ static uint32_t userOnPassKeyRequest()
* 0 latency (Number of intervals allowed to skip), * 0 latency (Number of intervals allowed to skip),
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms. * TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
*/ */
#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 200 //2000ms #define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms
/* /*
############################################# #############################################
@ -191,6 +209,7 @@ private:
BLEAdvertising *_advertising = nullptr; BLEAdvertising *_advertising = nullptr;
BLERemoteCharacteristic *_characteristic = nullptr; BLERemoteCharacteristic *_characteristic = nullptr;
BLERemoteService *pSvc = nullptr; BLERemoteService *pSvc = nullptr;
bool firstTimeSend = true; //First writeValue get sends like Write with reponse for clean security flags. After first time, all messages are send like WriteNoResponse for increase transmision speed.
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr; BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
@ -228,7 +247,18 @@ public:
return; return;
if (_characteristic == NULL) if (_characteristic == NULL)
return; return;
_characteristic->writeValue(data, length, true);
if (firstTimeSend)
{
_characteristic->writeValue(data, length, true);
firstTimeSend = false;
return;
}
if (!_characteristic->writeValue(data, length, !_characteristic->canWriteNoResponse()))
firstTimeSend = true;
return;
} }
bool available(byte *pvBuffer); bool available(byte *pvBuffer);
@ -236,7 +266,7 @@ public:
void add(byte value) void add(byte value)
{ {
// called from BLE-MIDI, to add it to a buffer here // called from BLE-MIDI, to add it to a buffer here
xQueueSend(mRxQueue, &value, portMAX_DELAY/2); xQueueSend(mRxQueue, &value, portMAX_DELAY / 2);
} }
protected: protected:
@ -254,6 +284,7 @@ protected:
{ {
_bleMidiTransport->_connectedCallback(); _bleMidiTransport->_connectedCallback();
} }
firstTimeSend = true;
} }
void disconnected() void disconnected()
@ -262,6 +293,7 @@ protected:
{ {
_bleMidiTransport->_disconnectedCallback(); _bleMidiTransport->_disconnectedCallback();
} }
firstTimeSend = true;
} }
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify); void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@ -290,7 +322,7 @@ protected:
void onConnect(BLEClient *pClient) void onConnect(BLEClient *pClient)
{ {
//Serial.println("##Connected##"); //Serial.println("##Connected##");
pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); //pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
vTaskDelay(1); vTaskDelay(1);
if (_bluetoothEsp32) if (_bluetoothEsp32)
{ {
@ -326,10 +358,11 @@ protected:
{ /** Number of intervals allowed to skip */ { /** Number of intervals allowed to skip */
return false; return false;
} }
else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT + 10) else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT)
{ /** 10ms units */ { /** 10ms units */
return false; return false;
} }
pClient->updateConnParams(params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout);
return true; return true;
}; };
@ -380,7 +413,7 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class
NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH); NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH);
/** Optional: set the transmit power, default is 3db */ /** Optional: set the transmit power, default is 3db */
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */ NimBLEDevice::setPower(BLEMIDI_TX_PWR); /** +9db */
myAdvCB.enableConnection = true; myAdvCB.enableConnection = true;
scan(); scan();
@ -421,7 +454,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
{ {
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
{ {
receive(pData, length); receive(pData, length);
} }
} }
@ -466,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect()
//Re-connection SUCCESS //Re-connection SUCCESS
return true; return true;
} }
} }
/** Disconnect if subscribe failed */ /** Disconnect if subscribe failed */
_client->disconnect(); _client->disconnect();
} }
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/ /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
NimBLEDevice::deleteClient(_client); NimBLEDevice::deleteClient(_client);
_client = nullptr; _client = nullptr;
return false; return false;
} }
/*If client does not match, delete previous client and create a new one*/ /*If client does not match, delete previous client and create a new one*/
NimBLEDevice::deleteClient(_client); NimBLEDevice::deleteClient(_client);
@ -485,14 +518,14 @@ bool BLEMIDI_Client_ESP32::connect()
Serial.println("Max clients reached - no more connections available"); Serial.println("Max clients reached - no more connections available");
return false; return false;
} }
// Create and setup a new client // 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, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); _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);
@ -523,15 +556,15 @@ bool BLEMIDI_Client_ESP32::connect()
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) /** make sure it's not null */ if (pSvc) /** make sure it's not null */
{ {
_characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID); _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->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)))
@ -542,7 +575,7 @@ bool BLEMIDI_Client_ESP32::connect()
} }
} }
} }
//If anything fails, disconnect and delete client //If anything fails, disconnect and delete client
_client->disconnect(); _client->disconnect();
NimBLEDevice::deleteClient(_client); NimBLEDevice::deleteClient(_client);