From f9ad1e5b83234f8eb4ee0e6f8ccd852fb8be137e Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 21 Sep 2021 17:39:06 +0200 Subject: [PATCH 1/3] Transmition speed Improved WriteNoResponse Now it sends data to server in Write Without Response mode. This increase the transmission speed and it get adapted to MIDI over BLE protocol. Some devices needs a first message with confirmation for clean security flags. After a connection or after an error, the first message is sended like Write (with response). --- src/hardware/BLEMIDI_Client_ESP32.h | 50 ++++++++++++++++++----------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 6b066e3..e6bb66b 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -58,7 +58,7 @@ * Uncomment what you need * These are the default values. */ -//#define BLEMIDI_CLIENT_BOND +#define BLEMIDI_CLIENT_BOND //#define BLEMIDI_CLIENT_MITM #define BLEMIDI_CLIENT_PAIR @@ -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_LATENCY 0 -#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms +#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 200 //2000ms /* ############################################# @@ -191,6 +191,7 @@ private: BLEAdvertising *_advertising = nullptr; BLERemoteCharacteristic *_characteristic = 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 *_bleMidiTransport = nullptr; @@ -228,7 +229,18 @@ public: return; if (_characteristic == NULL) 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); @@ -236,7 +248,7 @@ public: void add(byte value) { // called from BLE-MIDI, to add it to a buffer here - xQueueSend(mRxQueue, &value, portMAX_DELAY/2); + xQueueSend(mRxQueue, &value, portMAX_DELAY / 2); } protected: @@ -254,6 +266,7 @@ protected: { _bleMidiTransport->_connectedCallback(); } + firstTimeSend = true; } void disconnected() @@ -262,6 +275,7 @@ protected: { _bleMidiTransport->_disconnectedCallback(); } + firstTimeSend = true; } void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify); @@ -421,7 +435,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter { if (this->_characteristic == pRemoteCharacteristic) //Redundant protection { - receive(pData, length); + receive(pData, length); } } @@ -466,14 +480,14 @@ bool BLEMIDI_Client_ESP32::connect() //Re-connection SUCCESS return true; } - } + } /** 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; + return false; } /*If client does not match, delete previous client and create a new one*/ NimBLEDevice::deleteClient(_client); @@ -485,14 +499,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, 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); @@ -523,15 +537,15 @@ bool BLEMIDI_Client_ESP32::connect() 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 */ - { + { _characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID); - + 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))) @@ -542,7 +556,7 @@ bool BLEMIDI_Client_ESP32::connect() } } } - + //If anything fails, disconnect and delete client _client->disconnect(); NimBLEDevice::deleteClient(_client); From 6ea32b340fa9fd14e9439d981a59c0f0a1123ce5 Mon Sep 17 00:00:00 2001 From: Roberto Date: Tue, 21 Sep 2021 18:16:13 +0200 Subject: [PATCH 2/3] Transmision power --- src/hardware/BLEMIDI_Client_ESP32.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index e6bb66b..8679a9a 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -37,6 +37,24 @@ #define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT" #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 ##### */ @@ -394,7 +412,7 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport Date: Tue, 21 Sep 2021 18:22:25 +0200 Subject: [PATCH 3/3] Update Communication Parameters --- src/hardware/BLEMIDI_Client_ESP32.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 8679a9a..dd81b58 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -112,10 +112,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_LATENCY 0 // -#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms +#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 200 //2000ms /* ############################################# @@ -322,7 +322,7 @@ protected: void onConnect(BLEClient *pClient) { //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); if (_bluetoothEsp32) { @@ -358,10 +358,11 @@ protected: { /** Number of intervals allowed to skip */ return false; } - else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT + 10) + else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT) { /** 10ms units */ return false; } + pClient->updateConnParams(params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout); return true; };