Skip to content

Commit 96c7914

Browse files
RobertoHERobertoHE
authored andcommitted
Add Settings to ESP32_Nimble
1 parent 58f0ede commit 96c7914

File tree

2 files changed

+81
-38
lines changed

2 files changed

+81
-38
lines changed

src/hardware/BLEMIDI_Client_ESP32.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct BLEDefaultSettings : public CommonBLEDefaultSettings
5151
/**
5252
* Set power transmision
5353
*/
54-
static const uint8_t clientTXPwr = 9; //in dBm
54+
static const uint8_t BLETXPwr = 9; //in dBm
5555

5656
/*
5757
###### SECURITY #####
@@ -64,7 +64,7 @@ struct BLEDefaultSettings : public CommonBLEDefaultSettings
6464
* * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability
6565
* * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability
6666
*/
67-
static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
67+
static const uint8_t BLESecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
6868

6969
/** Set the security method.
7070
* bonding
@@ -73,9 +73,9 @@ struct BLEDefaultSettings : public CommonBLEDefaultSettings
7373
*
7474
* More info in nimBLE lib
7575
*/
76-
static const bool clientBond = true;
77-
static const bool clientMITM = false;
78-
static const bool clientPair = true;
76+
static const bool BLEBond = true;
77+
static const bool BLEMITM = false;
78+
static const bool BLEPair = true;
7979

8080
/**
8181
* This callback function defines what will be done when server requieres PassKey.
@@ -146,7 +146,7 @@ class AdvertisedDeviceCallbacks : public NimBLEScanCallbacks
146146
std::string nameTarget;
147147

148148
protected:
149-
void onResult(NimBLEAdvertisedDevice *advertisedDevice)
149+
void onResult(const NimBLEAdvertisedDevice *advertisedDevice)
150150
{
151151
if (!enableConnection) // not begin() or end()
152152
{
@@ -299,7 +299,7 @@ class MyClientCallbacks : public BLEClientCallbacks
299299
void onConnect(BLEClient *pClient)
300300
{
301301
DEBUGCLIENT("##Connected##");
302-
// pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
302+
pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
303303
vTaskDelay(1);
304304
if (_bluetoothEsp32)
305305
_bluetoothEsp32->connected();
@@ -389,11 +389,11 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
389389
// Core_0 runs here, core_1 runs the BLE stack
390390
mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t));
391391

392-
NimBLEDevice::setSecurityIOCap(_Settings::clientSecurityCapabilities); // Attention, it may need a passkey
393-
NimBLEDevice::setSecurityAuth(_Settings::clientBond, _Settings::clientMITM, _Settings::clientPair);
392+
NimBLEDevice::setSecurityIOCap(_Settings::BLESecurityCapabilities); // Attention, it may need a passkey
393+
NimBLEDevice::setSecurityAuth(_Settings::BLEBond, _Settings::BLEMITM, _Settings::BLEPair);
394394

395395
/** Optional: set the transmit power, default is 3db */
396-
NimBLEDevice::setPower(_Settings::clientTXPwr); /** +9db */
396+
NimBLEDevice::setPower(_Settings::BLETXPwr); /** +9db */
397397

398398
myAdvCB.enableConnection = true;
399399
scan();

src/hardware/BLEMIDI_ESP32_NimBLE.h

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,62 @@ BEGIN_BLEMIDI_NAMESPACE
1010
// Dependanced class settings
1111
struct BLEDefaultSettings : public CommonBLEDefaultSettings
1212
{
13-
//TODO Create parametric configurations
13+
14+
/*
15+
###### TX POWER #####
16+
*/
17+
18+
/**
19+
* Set power transmision
20+
*/
21+
static const uint8_t BLETXPwr = 9; //in dBm
22+
23+
/*
24+
###### SECURITY #####
25+
*/
26+
27+
/** Set the IO capabilities of the device, each option will trigger a different pairing method.
28+
* * 0x00 BLE_HS_IO_DISPLAY_ONLY DisplayOnly IO capability
29+
* * 0x01 BLE_HS_IO_DISPLAY_YESNO DisplayYesNo IO capability
30+
* * 0x02 BLE_HS_IO_KEYBOARD_ONLY KeyboardOnly IO capability
31+
* * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability
32+
* * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability
33+
*/
34+
static const uint8_t BLESecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
35+
36+
/** Set the security method.
37+
* bonding
38+
* man in the middle protection
39+
* pair. secure connections
40+
*
41+
* More info in nimBLE lib
42+
*/
43+
static const bool BLEBond = true;
44+
static const bool BLEMITM = false;
45+
static const bool BLEPair = true;
46+
47+
/*
48+
###### BLE COMMUNICATION PARAMS ######
49+
*/
50+
51+
/** Set connection parameters:
52+
* If you only use one connection, put recomended BLE server param communication
53+
* (you may scan it ussing "nRF Connect" app or other similar apps).
54+
*
55+
* If you use more than one connection adjust, for example, settings like 15ms interval, 0 latency, 120ms timout.
56+
* These settings may be safe for 3 clients to connect reliably, set faster values if you have less
57+
* connections.
58+
*
59+
* Min interval (unit: 1.25ms): 12 * 1.25ms = 15 ms,
60+
* Max interval (unit: 1.25ms): 12 * 1.25ms = 15,
61+
* 0 latency (Number of intervals allowed to skip),
62+
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
63+
*/
64+
static const uint16_t commMinInterval = 6; // 7.5ms
65+
static const uint16_t commMaxInterval = 35; // 40ms
66+
static const uint16_t commLatency = 0; //
67+
static const uint16_t commTimeOut = 200; // 2000ms
68+
1469
};
1570

1671
template <class _Settings>
@@ -90,13 +145,13 @@ class MyServerCallbacks : public BLEServerCallbacks
90145
protected:
91146
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr;
92147

93-
void onConnect(BLEServer *)
148+
void onConnect(BLEServer *, NimBLEConnInfo &)
94149
{
95150
if (_bluetoothEsp32)
96151
_bluetoothEsp32->connected();
97152
};
98153

99-
void onDisconnect(BLEServer *)
154+
void onDisconnect(BLEServer *, NimBLEConnInfo &)
100155
{
101156
if (_bluetoothEsp32)
102157
_bluetoothEsp32->disconnected();
@@ -115,7 +170,7 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
115170
protected:
116171
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr;
117172

118-
void onWrite(BLECharacteristic *characteristic)
173+
void onWrite(BLECharacteristic *characteristic, NimBLEConnInfo &)
119174
{
120175
std::string rxValue = characteristic->getValue();
121176
if (rxValue.length() > 0)
@@ -131,27 +186,11 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
131186
_bleMidiTransport = bleMidiTransport;
132187

133188
BLEDevice::init(deviceName);
189+
190+
NimBLEDevice::setSecurityIOCap(_Settings::BLESecurityCapabilities); // Attention, it may need a passkey
191+
NimBLEDevice::setSecurityAuth(_Settings::BLEBond, _Settings::BLEMITM, _Settings::BLEPair);
134192

135-
/**
136-
* Set the IO capabilities of the device, each option will trigger a different pairing method.
137-
* BLE_HS_IO_DISPLAY_ONLY - Passkey pairing
138-
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing
139-
* BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing
140-
*/
141-
// NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); // use passkey
142-
// NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO); //use numeric comparison
143-
144-
/**
145-
* 2 different ways to set security - both calls achieve the same result.
146-
* no bonding, no man in the middle protection, BLE secure connections.
147-
*
148-
* These are the default values, only shown here for demonstration.
149-
*/
150-
// NimBLEDevice::setSecurityAuth(false, false, true);
151-
152-
// NimBLEDevice::setSecurityAuth(/*BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC);
153-
154-
NimBLEDevice::setSecurityAuth(true, false, false);
193+
/** Optional: set the transmit power, default is 3db */ NimBLEDevice::setPower(_Settings::BLETXPwr); /** +9db */
155194

156195
// To communicate between the 2 cores.
157196
// Core_0 runs here, core_1 runs the BLE stack
@@ -166,17 +205,21 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
166205

167206
// Create a BLE Characteristic
168207
_characteristic = service->createCharacteristic(
169-
BLEUUID(CHARACTERISTIC_UUID),
170-
NIMBLE_PROPERTY::READ |
208+
BLEUUID(CHARACTERISTIC_UUID),
209+
NIMBLE_PROPERTY::READ |
171210
NIMBLE_PROPERTY::WRITE |
172211
NIMBLE_PROPERTY::NOTIFY |
173212
NIMBLE_PROPERTY::WRITE_NR);
174213

175214
_characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
176215

177216

178-
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT); // Attention
179-
217+
NimBLEDevice::setSecurityIOCap(_Settings::BLESecurityCapabilities); // Attention, it may need a passkey
218+
NimBLEDevice::setSecurityAuth(_Settings::BLEBond, _Settings::BLEMITM, _Settings::BLEPair);
219+
220+
/** Optional: set the transmit power, default is 3db */
221+
NimBLEDevice::setPower(_Settings::BLETXPwr); /** +9db */
222+
180223

181224
// Start the service
182225
service->start();

0 commit comments

Comments
 (0)