Changed #define for Struct in Config
Mayor changes in all configuration setup. Create a custom Config Struct for the client, it heritages from regular repo Struct and adds specific settings of the class. All configurations, including onPassRequest function, may be configured in the upper layers of the code, like in main.c
This commit is contained in:
parent
39c16474b3
commit
24dce9ac6a
|
|
@ -8,48 +8,41 @@
|
||||||
#define DEBUGCLIENT(_text_) ;
|
#define DEBUGCLIENT(_text_) ;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Headers for ESP32 nimBLE
|
||||||
|
#include <NimBLEDevice.h>
|
||||||
|
|
||||||
/*
|
BEGIN_BLEMIDI_NAMESPACE
|
||||||
#############################################
|
|
||||||
########## USER DEFINES BEGINNING ###########
|
|
||||||
####### Modify only these parameters ########
|
|
||||||
#############################################
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
using PasskeyRequestCallback = uint32_t (*)(void);
|
||||||
##### BLE DEVICE NAME #####
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
static uint32_t defautlPasskeyRequest()
|
||||||
* Set always the same name independently of name server
|
{
|
||||||
|
// FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
|
||||||
|
// FOR EXAMPLE:
|
||||||
|
uint32_t passkey = 123456;
|
||||||
|
|
||||||
|
// Serial.println("Client Passkey Request");
|
||||||
|
|
||||||
|
/** return the passkey to send to the server */
|
||||||
|
return passkey;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
|
||||||
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
##### BLE DEVICE NAME #####
|
||||||
*/
|
*/
|
||||||
//#define BLEMIDI_CLIENT_FIXED_NAME "BleMidiClient"
|
/**
|
||||||
|
* Set name of ble device (not affect to connection with server)
|
||||||
#ifndef BLEMIDI_CLIENT_FIXED_NAME //Not modify
|
* max 16 characters
|
||||||
/**
|
|
||||||
* When client tries to connect to specific server, BLE name is composed as follows:
|
|
||||||
* BLEMIDI_CLIENT_NAME_PREFIX + <NameServer/addrServer> + BLEMIDI_CLIENT_NAME_SUBFIX
|
|
||||||
*
|
|
||||||
* example:
|
|
||||||
* BLEMIDI_CLIENT_NAME_PREFIX "Client-"
|
|
||||||
* <NameServer/addrServer> "AX-Edge"
|
|
||||||
* BLEMIDI_CLIENT_NAME_SUBFIX "-Midi1"
|
|
||||||
*
|
|
||||||
* Result: "Client-AX-Edge-Midi1"
|
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CLIENT_NAME_PREFIX "C-"
|
static constexpr char *name = "BleMidiClient";
|
||||||
#define BLEMIDI_CLIENT_NAME_SUBFIX ""
|
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* When client tries to connect to the first midi server found:
|
###### TX POWER #####
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT"
|
/**
|
||||||
#endif //Not modify
|
|
||||||
|
|
||||||
/*
|
|
||||||
###### TX POWER #####
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Set power transmision
|
* Set power transmision
|
||||||
*
|
*
|
||||||
* ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum
|
* ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum
|
||||||
|
|
@ -60,55 +53,39 @@
|
||||||
* ESP_PWR_LVL_P3 // Corresponding to +3dbm
|
* ESP_PWR_LVL_P3 // Corresponding to +3dbm
|
||||||
* ESP_PWR_LVL_P6 // Corresponding to +6dbm
|
* ESP_PWR_LVL_P6 // Corresponding to +6dbm
|
||||||
* ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum
|
* ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum
|
||||||
*/
|
*/
|
||||||
|
static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9;
|
||||||
|
|
||||||
#define BLEMIDI_TX_PWR ESP_PWR_LVL_P9
|
/*
|
||||||
|
###### SECURITY #####
|
||||||
/*
|
*/
|
||||||
###### SECURITY #####
|
/** Set the IO capabilities of the device, each option will trigger a different pairing method.
|
||||||
*/
|
|
||||||
|
|
||||||
/** Set the IO capabilities of the device, each option will trigger a different pairing method.
|
|
||||||
* BLE_HS_IO_KEYBOARD_ONLY - Passkey pairing
|
* BLE_HS_IO_KEYBOARD_ONLY - Passkey pairing
|
||||||
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing
|
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing
|
||||||
* BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing
|
* BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CLIENT_SECURITY_CAP BLE_HS_IO_NO_INPUT_OUTPUT
|
static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
|
||||||
|
|
||||||
/** Set the security method.
|
/** Set the security method.
|
||||||
* bonding
|
* bonding
|
||||||
* man in the middle protection
|
* man in the middle protection
|
||||||
* pair. secure connections
|
* pair. secure connections
|
||||||
*
|
*
|
||||||
* More info in nimBLE lib
|
* More info in nimBLE lib
|
||||||
*
|
|
||||||
* Uncomment what you need
|
|
||||||
* These are the default values.
|
|
||||||
* You can select some simultaneously.
|
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CLIENT_BOND
|
static const bool clientBond = true;
|
||||||
//#define BLEMIDI_CLIENT_MITM
|
static const bool clientMITM = false;
|
||||||
#define BLEMIDI_CLIENT_PAIR
|
static const bool clientPair = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This callback function defines what will be done when server requieres PassKey.
|
* This callback function defines what will be done when server requieres PassKey.
|
||||||
* Add your custom code here.
|
* Add your custom code here.
|
||||||
*/
|
*/
|
||||||
static uint32_t userOnPassKeyRequest()
|
static constexpr PasskeyRequestCallback userOnPassKeyRequest = defautlPasskeyRequest;
|
||||||
{
|
|
||||||
//FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
|
|
||||||
//FOR EXAMPLE:
|
|
||||||
uint32_t passkey = 123456;
|
|
||||||
|
|
||||||
//Serial.println("Client Passkey Request");
|
|
||||||
|
|
||||||
/** return the passkey to send to the server */
|
|
||||||
return passkey;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
###### BLE COMMUNICATION PARAMS ######
|
###### BLE COMMUNICATION PARAMS ######
|
||||||
*/
|
*/
|
||||||
/** Set connection parameters:
|
/** Set connection parameters:
|
||||||
* If you only use one connection, put recomended BLE server param communication
|
* If you only use one connection, put recomended BLE server param communication
|
||||||
* (you may scan it ussing "nRF Connect" app or other similar apps).
|
* (you may scan it ussing "nRF Connect" app or other similar apps).
|
||||||
|
|
@ -122,65 +99,25 @@ 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
|
static const uint16_t commMinInterval = 6; // 7.5ms
|
||||||
#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
|
static const uint16_t commMaxInterval = 35; // 40ms
|
||||||
#define BLEMIDI_CLIENT_COMM_LATENCY 0 //
|
static const uint16_t commLatency = 0; //
|
||||||
#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms
|
static const uint16_t commTimeOut = 200; // 2000ms
|
||||||
|
|
||||||
/*
|
/*
|
||||||
###### BLE FORCE NEW CONNECTION ######
|
###### BLE FORCE NEW CONNECTION ######
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This parameter force to skip the "soft-reconnection" and force to create a new connection after a disconnect event.
|
* This parameter force to skip the "soft-reconnection" and force to create a new connection after a disconnect event.
|
||||||
* "Soft-reconnection" save some time and energy in comparation with performming a new connection, but some BLE devices
|
* "Soft-reconnection" save some time and energy in comparation with performming a new connection, but some BLE devices
|
||||||
* don't support or don't perform correctly a "soft-reconnection" after a disconnection event.
|
* don't support or don't perform correctly a "soft-reconnection" after a disconnection event.
|
||||||
* Uncomment this define if your device doesn't work propertily after a reconnection.
|
* Set to "true" if your device doesn't work propertily after a reconnection.
|
||||||
*
|
*
|
||||||
*/
|
|
||||||
//#define BLEMIDI_FORCE_NEW_CONNECTION
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
#############################################
|
|
||||||
############ USER DEFINES END ###############
|
|
||||||
#############################################
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Headers for ESP32 nimBLE
|
|
||||||
#include <NimBLEDevice.h>
|
|
||||||
|
|
||||||
BEGIN_BLEMIDI_NAMESPACE
|
|
||||||
|
|
||||||
#ifdef BLEMIDI_CLIENT_BOND
|
|
||||||
#define BLEMIDI_CLIENT_BOND_DUMMY BLE_SM_PAIR_AUTHREQ_BOND
|
|
||||||
#else
|
|
||||||
#define BLEMIDI_CLIENT_BOND_DUMMY 0x00
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef BLEMIDI_CLIENT_MITM
|
|
||||||
#define BLEMIDI_CLIENT_MITM_DUMMY BLE_SM_PAIR_AUTHREQ_MITM
|
|
||||||
#else
|
|
||||||
#define BLEMIDI_CLIENT_MITM_DUMMY 0x00
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef BLEMIDI_CLIENT_PAIR
|
|
||||||
#define BLEMIDI_CLIENT_PAIR_DUMMY BLE_SM_PAIR_AUTHREQ_SC
|
|
||||||
#else
|
|
||||||
#define BLEMIDI_CLIENT_PAIR_DUMMY 0x00
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Set the security method.
|
|
||||||
* bonding
|
|
||||||
* man in the middle protection
|
|
||||||
* pair. secure connections
|
|
||||||
*
|
|
||||||
* More info in nimBLE lib
|
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CLIENT_SECURITY_AUTH (BLEMIDI_CLIENT_BOND_DUMMY | BLEMIDI_CLIENT_MITM_DUMMY | BLEMIDI_CLIENT_PAIR_DUMMY)
|
static const bool forceNewConnection = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/** Define a class to handle the callbacks when advertisments are received */
|
/** Define a class to handle the callbacks when advertisments are received */
|
||||||
class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
|
class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
|
||||||
|
|
@ -239,7 +176,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.
|
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<_Settings>, _Settings> *_bleMidiTransport = nullptr;
|
BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *_bleMidiTransport = nullptr;
|
||||||
|
|
||||||
|
|
@ -338,19 +275,21 @@ protected:
|
||||||
|
|
||||||
uint32_t onPassKeyRequest()
|
uint32_t onPassKeyRequest()
|
||||||
{
|
{
|
||||||
return userOnPassKeyRequest();
|
//if (nullptr != _Settings::userOnPassKeyRequest)
|
||||||
|
return _Settings::userOnPassKeyRequest();
|
||||||
|
//return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
void onConnect(BLEClient*)
|
void onConnect(BLEClient *pClient)
|
||||||
{
|
{
|
||||||
DEBUGCLIENT("##Connected##");
|
DEBUGCLIENT("##Connected##");
|
||||||
//pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
|
//pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
|
||||||
vTaskDelay(1);
|
vTaskDelay(1);
|
||||||
if (_bluetoothEsp32)
|
if (_bluetoothEsp32)
|
||||||
_bluetoothEsp32->connected();
|
_bluetoothEsp32->connected();
|
||||||
};
|
};
|
||||||
|
|
||||||
void onDisconnect(BLEClient*)
|
void onDisconnect(BLEClient *pClient)
|
||||||
{
|
{
|
||||||
DEBUGCLIENT(pClient->getPeerAddress().toString().c_str());
|
DEBUGCLIENT(pClient->getPeerAddress().toString().c_str());
|
||||||
DEBUGCLIENT(" Disconnected - Starting scan");
|
DEBUGCLIENT(" Disconnected - Starting scan");
|
||||||
|
|
@ -358,38 +297,34 @@ protected:
|
||||||
if (_bluetoothEsp32)
|
if (_bluetoothEsp32)
|
||||||
{
|
{
|
||||||
_bluetoothEsp32->disconnected();
|
_bluetoothEsp32->disconnected();
|
||||||
#ifdef BLEMIDI_FORCE_NEW_CONNECTION
|
|
||||||
// Try reconnection or search a new one
|
|
||||||
_bluetoothEsp32->scan();
|
|
||||||
#endif // BLEMIDI_FORCE_NEW_CONNECTION
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BLEMIDI_FORCE_NEW_CONNECTION
|
if (_Settings::forceNewConnection)
|
||||||
|
{
|
||||||
// Renew Client
|
// Renew Client
|
||||||
NimBLEDevice::deleteClient(pClient);
|
NimBLEDevice::deleteClient(pClient);
|
||||||
NimBLEDevice::createClient();
|
|
||||||
pClient = nullptr;
|
pClient = nullptr;
|
||||||
#endif // BLEMIDI_FORCE_NEW_CONNECTION
|
}
|
||||||
|
|
||||||
//Try reconnection or search a new one
|
// Try reconnection or search a new one
|
||||||
NimBLEDevice::getScan()->start(1, 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)
|
||||||
{
|
{
|
||||||
if (params->itvl_min < BLEMIDI_CLIENT_COMM_MIN_INTERVAL)
|
if (params->itvl_min < _Settings::commMinInterval)
|
||||||
{ /** 1.25ms units */
|
{ /** 1.25ms units */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (params->itvl_max > BLEMIDI_CLIENT_COMM_MAX_INTERVAL)
|
else if (params->itvl_max > _Settings::commMaxInterval)
|
||||||
{ /** 1.25ms units */
|
{ /** 1.25ms units */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (params->latency > BLEMIDI_CLIENT_COMM_LATENCY)
|
else if (params->latency > _Settings::commLatency)
|
||||||
{ /** 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)
|
else if (params->supervision_timeout > _Settings::commMinInterval)
|
||||||
{ /** 10ms units */
|
{ /** 10ms units */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -415,24 +350,18 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
|
||||||
{
|
{
|
||||||
myAdvCB.specificTarget = false;
|
myAdvCB.specificTarget = false;
|
||||||
myAdvCB.nameTarget = "";
|
myAdvCB.nameTarget = "";
|
||||||
|
|
||||||
#ifdef BLEMIDI_CLIENT_FIXED_NAME
|
|
||||||
strDeviceName = BLEMIDI_CLIENT_FIXED_NAME;
|
|
||||||
#else
|
|
||||||
strDeviceName = BLEMIDI_CLIENT_DEFAULT_NAME;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else // Connect to a specific name or address
|
else // Connect to a specific name or address
|
||||||
{
|
{
|
||||||
myAdvCB.specificTarget = true;
|
myAdvCB.specificTarget = true;
|
||||||
myAdvCB.nameTarget = strDeviceName;
|
myAdvCB.nameTarget = strDeviceName;
|
||||||
|
|
||||||
#ifdef BLEMIDI_CLIENT_FIXED_NAME
|
|
||||||
strDeviceName = BLEMIDI_CLIENT_FIXED_NAME;
|
|
||||||
#else
|
|
||||||
strDeviceName = BLEMIDI_CLIENT_NAME_PREFIX + strDeviceName + BLEMIDI_CLIENT_NAME_SUBFIX;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char array[16] = "patata";
|
||||||
|
|
||||||
|
memcpy(array, _Settings::name, 16);
|
||||||
|
strDeviceName = array;
|
||||||
|
|
||||||
DEBUGCLIENT(strDeviceName.c_str());
|
DEBUGCLIENT(strDeviceName.c_str());
|
||||||
|
|
||||||
NimBLEDevice::init(strDeviceName);
|
NimBLEDevice::init(strDeviceName);
|
||||||
|
|
@ -441,11 +370,11 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
|
||||||
// Core_0 runs here, core_1 runs the BLE stack
|
// Core_0 runs here, core_1 runs the BLE stack
|
||||||
mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t));
|
mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t));
|
||||||
|
|
||||||
NimBLEDevice::setSecurityIOCap(BLEMIDI_CLIENT_SECURITY_CAP); // Attention, it may need a passkey
|
NimBLEDevice::setSecurityIOCap(_Settings::clientSecurityCapabilities); // Attention, it may need a passkey
|
||||||
NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH);
|
NimBLEDevice::setSecurityAuth(_Settings::clientBond, _Settings::clientMITM, _Settings::clientPair);
|
||||||
|
|
||||||
/** Optional: set the transmit power, default is 3db */
|
/** Optional: set the transmit power, default is 3db */
|
||||||
NimBLEDevice::setPower(BLEMIDI_TX_PWR); /** +9db */
|
NimBLEDevice::setPower(_Settings::clientTXPwr); /** +9db */
|
||||||
|
|
||||||
myAdvCB.enableConnection = true;
|
myAdvCB.enableConnection = true;
|
||||||
scan();
|
scan();
|
||||||
|
|
@ -456,9 +385,12 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
|
||||||
template <class _Settings>
|
template <class _Settings>
|
||||||
bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
|
bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
|
||||||
{
|
{
|
||||||
if (myAdvCB.enableConnection)
|
if (!myAdvCB.enableConnection)
|
||||||
{
|
{
|
||||||
if (_client == nullptr || !_client->isConnected()) //Try to connect/reconnect
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_client == nullptr || !_client->isConnected()) // Try to connect/reconnect
|
||||||
{
|
{
|
||||||
if (myAdvCB.doConnect)
|
if (myAdvCB.doConnect)
|
||||||
{
|
{
|
||||||
|
|
@ -473,20 +405,16 @@ bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
|
||||||
scan();
|
scan();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return 1 byte from the Queue
|
// return 1 byte from the Queue
|
||||||
return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty
|
return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Notification receiving handler callback */
|
/** Notification receiving handler callback */
|
||||||
template <class _Settings>
|
template <class _Settings>
|
||||||
void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
|
void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
|
||||||
{
|
{
|
||||||
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
|
if (this->_characteristic == pRemoteCharacteristic) // Redundant protection
|
||||||
{
|
{
|
||||||
receive(pData, length);
|
receive(pData, length);
|
||||||
}
|
}
|
||||||
|
|
@ -517,7 +445,9 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
{
|
{
|
||||||
using namespace std::placeholders; //<- for bind funtion in callback notification
|
using namespace std::placeholders; //<- for bind funtion in callback notification
|
||||||
|
|
||||||
#ifndef BLEMIDI_FORCE_NEW_CONNECTION
|
//Retry to connecto to last one
|
||||||
|
if (!_Settings::forceNewConnection)
|
||||||
|
{
|
||||||
/** Check if we have a client we should reuse first
|
/** Check if we have a client we should reuse first
|
||||||
* Special case when we already know this device
|
* Special case when we already know this device
|
||||||
* This saves considerable time and power.
|
* This saves considerable time and power.
|
||||||
|
|
@ -533,7 +463,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
{
|
{
|
||||||
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)))
|
||||||
{
|
{
|
||||||
//Re-connection SUCCESS
|
// Re-connection SUCCESS
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -549,7 +479,8 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
NimBLEDevice::deleteClient(_client);
|
NimBLEDevice::deleteClient(_client);
|
||||||
_client = nullptr;
|
_client = nullptr;
|
||||||
}
|
}
|
||||||
#endif //BLEMIDI_FORCE_NEW_CONNECTION
|
}
|
||||||
|
|
||||||
|
|
||||||
if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS)
|
if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS)
|
||||||
{
|
{
|
||||||
|
|
@ -562,7 +493,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
|
|
||||||
_client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false);
|
_client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false);
|
||||||
|
|
||||||
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
|
_client->setConnectionParams(_Settings::commMinInterval,_Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
|
||||||
|
|
||||||
/** 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);
|
||||||
|
|
@ -602,14 +533,14 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
{
|
{
|
||||||
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)))
|
||||||
{
|
{
|
||||||
//Connection SUCCESS
|
// Connection SUCCESS
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//If anything fails, disconnect and delete client
|
// If anything fails, disconnect and delete client
|
||||||
_client->disconnect();
|
_client->disconnect();
|
||||||
NimBLEDevice::deleteClient(_client);
|
NimBLEDevice::deleteClient(_client);
|
||||||
_client = nullptr;
|
_client = nullptr;
|
||||||
|
|
@ -619,7 +550,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
|
||||||
/** Callback to process the results of the last scan or restart it */
|
/** Callback to process the results of the last scan or restart it */
|
||||||
void scanEndedCB(NimBLEScanResults results)
|
void scanEndedCB(NimBLEScanResults results)
|
||||||
{
|
{
|
||||||
DEBUGCLIENT("Scan Ended");
|
// DEBUGCLIENT("Scan Ended");
|
||||||
}
|
}
|
||||||
|
|
||||||
END_BLEMIDI_NAMESPACE
|
END_BLEMIDI_NAMESPACE
|
||||||
|
|
@ -635,7 +566,7 @@ END_BLEMIDI_NAMESPACE
|
||||||
It will try to connect to a specific server with equal name or addr than <DeviceName>. If <DeviceName> is "", it will connect to first midi server
|
It will try to connect to a specific server with equal name or addr than <DeviceName>. If <DeviceName> is "", it will connect to first midi server
|
||||||
*/
|
*/
|
||||||
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
|
||||||
BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
|
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettingsClient)
|
||||||
|
|
||||||
/*! \brief Create a default instance for ESP32 named BLEMIDI-CLIENT.
|
/*! \brief Create a default instance for ESP32 named BLEMIDI-CLIENT.
|
||||||
It will try to connect to first midi ble server found.
|
It will try to connect to first midi ble server found.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue