Skip to content

Commit ab3cabf

Browse files
committed
Change client setConnectionParams().
setConnectionParams() no longer takes min/max connection event time as arguments as they are not used by NimBLE. Added setting of scan interval and window to connection parameters to allow custom scan settings when finding the connection target. Added define for IDF v3.3
1 parent 4933931 commit ab3cabf

File tree

3 files changed

+51
-79
lines changed

3 files changed

+51
-79
lines changed

src/NimBLEClient.cpp

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ NimBLEClient::NimBLEClient()
5353
m_haveServices = false;
5454
m_isConnected = false;
5555
m_connectTimeout = 30000;
56-
m_pConnParams = nullptr;
56+
57+
m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
58+
m_pConnParams.scan_window = 16; // Scan window in 0.625ms units (NimBLE Default)
59+
m_pConnParams.itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN; // min_int = 0x10*1.25ms = 20ms
60+
m_pConnParams.itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX; // max_int = 0x20*1.25ms = 40ms
61+
m_pConnParams.latency = BLE_GAP_INITIAL_CONN_LATENCY; // number of packets allowed to skip (extends max interval)
62+
m_pConnParams.supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT; // timeout = 400*10ms = 4000ms
63+
m_pConnParams.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; // Minimum length of connection event in 0.625ms units
64+
m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units
5765
} // NimBLEClient
5866

5967

@@ -69,10 +77,7 @@ NimBLEClient::~NimBLEClient() {
6977
if(m_deleteCallbacks) {
7078
delete m_pClientCallbacks;
7179
}
72-
73-
if(m_pConnParams != nullptr) {
74-
free(m_pConnParams);
75-
}
80+
7681
} // ~NimBLEClient
7782

7883

@@ -142,7 +147,7 @@ bool NimBLEClient::connect(NimBLEAddress address, uint8_t type, bool refreshServ
142147
* Loop on BLE_HS_EBUSY if the scan hasn't stopped yet.
143148
*/
144149
do{
145-
rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peerAddrt, m_connectTimeout, m_pConnParams,
150+
rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peerAddrt, m_connectTimeout, &m_pConnParams,
146151
NimBLEClient::handleGapEvent, this);
147152
if(rc == BLE_HS_EBUSY) {
148153
vTaskDelay(1);
@@ -168,19 +173,6 @@ bool NimBLEClient::connect(NimBLEAddress address, uint8_t type, bool refreshServ
168173
if(rc != 0){
169174
return false;
170175
}
171-
172-
m_semaphoreOpenEvt.take("exg-mtu");
173-
174-
rc = ble_gattc_exchange_mtu(m_conn_id, NULL,NULL);
175-
if(rc != 0) {
176-
NIMBLE_LOGE(LOG_TAG, "ble_gattc_exchange_mtu: rc=%d %s",rc,
177-
NimBLEUtils::returnCodeToString(rc));
178-
disconnect();
179-
m_semaphoreOpenEvt.give();
180-
return false;
181-
}
182-
183-
m_semaphoreOpenEvt.wait("exg-mtu");
184176

185177
if(refreshServices) {
186178
NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str());
@@ -241,8 +233,12 @@ int NimBLEClient::disconnect(uint8_t reason) {
241233
m_isConnected = false; // flag the disconnect now so no calls are performed after
242234
rc = ble_gap_terminate(m_conn_id, reason);
243235
if(rc != 0){
244-
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
236+
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc,
237+
NimBLEUtils::returnCodeToString(rc));
245238
}
239+
// Sometimes a disconnect event is not sent so we need to make sure
240+
// the device can be found again.
241+
NimBLEDevice::removeIgnored(m_peerAddress);
246242
}
247243

248244
NIMBLE_LOGD(LOG_TAG, "<< disconnect()");
@@ -255,56 +251,41 @@ int NimBLEClient::disconnect(uint8_t reason) {
255251
*/
256252
void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
257253
uint16_t latency, uint16_t timeout,
258-
uint16_t minConnTime, uint16_t maxConnTime)
254+
uint16_t scanInterval, uint16_t scanWindow)/*,
255+
uint16_t minConnTime, uint16_t maxConnTime)*/
259256
{
260-
if(m_pConnParams == nullptr) {
261-
m_pConnParams = (ble_gap_conn_params*)calloc(1, sizeof(ble_gap_conn_params));
262-
if(m_pConnParams == nullptr) {
263-
NIMBLE_LOGE(LOG_TAG, "setConnectionParams: Error No Mem");
264-
return;
265-
}
266-
}else if(0 == (minInterval | maxInterval | latency | timeout)) {
267-
free(m_pConnParams);
268-
m_pConnParams = nullptr;
269-
return;
270-
}
271-
m_pConnParams->scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
272-
m_pConnParams->scan_window = 16; // Scan window in 0.625ms units (NimBLE Default)
273-
m_pConnParams->itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
274-
m_pConnParams->itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
275-
m_pConnParams->latency = latency; // number of packets allowed to skip (extends max interval)
276-
m_pConnParams->supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
277-
m_pConnParams->min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units
278-
m_pConnParams->max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
257+
258+
m_pConnParams.scan_itvl = scanInterval; // Scan interval in 0.625ms units
259+
m_pConnParams.scan_window = scanWindow; // Scan window in 0.625ms units
260+
m_pConnParams.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
261+
m_pConnParams.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
262+
m_pConnParams.latency = latency; // number of packets allowed to skip (extends max interval)
263+
m_pConnParams.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
279264

280-
int rc = NimBLEUtils::checkConnParams(m_pConnParams);
281-
if(rc != 0) {
282-
NIMBLE_LOGE(LOG_TAG,"setConnectionParams : %s", NimBLEUtils::returnCodeToString(rc));
283-
free(m_pConnParams);
284-
m_pConnParams = nullptr;
285-
}
265+
// These are not used by NimBLE at this time - Must leave at defaults
266+
//m_pConnParams->min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units
267+
//m_pConnParams->max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
268+
269+
int rc = NimBLEUtils::checkConnParams(&m_pConnParams);
270+
assert(rc == 0 && "Invalid Connection parameters");
286271
}
287272

288273

289274
/**
290275
* Update connection parameters can be called only after connection has been established
291276
*/
292277
void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
293-
uint16_t latency, uint16_t timeout,
294-
uint16_t minConnTime, uint16_t maxConnTime)
295-
{
296-
if(m_pConnParams == nullptr) {
297-
setConnectionParams(minInterval, maxInterval, latency, timeout, minConnTime, maxConnTime);
298-
}
299-
278+
uint16_t latency, uint16_t timeout)
279+
{
300280
ble_gap_upd_params params;
301281

302282
params.latency = latency;
303283
params.itvl_max = maxInterval;
304284
params.itvl_min = minInterval;
305-
params.supervision_timeout = timeout;
306-
params.min_ce_len = minConnTime;
307-
params.max_ce_len = maxConnTime;
285+
params.supervision_timeout = timeout;
286+
// These are not used by NimBLE at this time - Must leave at defaults
287+
params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
288+
params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
308289

309290
int rc = ble_gap_update_params(m_conn_id, &params);
310291
if(rc != 0) {
@@ -648,28 +629,23 @@ uint16_t NimBLEClient::getMTU() {
648629
// print_conn_desc(&desc);
649630
// MODLOG_DFLT(INFO, "\n");
650631

651-
//client->m_pClientCallbacks->onConnect(client);
652632

653633
// In the case of a multiconnecting device we ignore this device when
654634
// scanning since we are already connected to it
655635
NimBLEDevice::addIgnored(client->m_peerAddress);
656636

657-
/* rc = ble_gattc_exchange_mtu(client->m_conn_id, NULL,NULL);
637+
rc = ble_gattc_exchange_mtu(client->m_conn_id, NULL,NULL);
658638
if(rc != 0) {
659639
NIMBLE_LOGE(LOG_TAG, "ble_gattc_exchange_mtu: rc=%d %s",rc,
660640
NimBLEUtils::returnCodeToString(rc));
661641
// if error getting mtu indicate a connection error.
662642
client->m_semaphoreOpenEvt.give(rc);
663-
} else {
664-
client->m_semaphoreOpenEvt.give(0);
665-
}
666-
*/
643+
}
667644
} else {
668645
// Connection attempt failed
669646
NIMBLE_LOGE(LOG_TAG, "Error: Connection failed; status=%d %s",
670647
event->connect.status,
671648
NimBLEUtils::returnCodeToString(event->connect.status));
672-
// client->m_semaphoreOpenEvt.give(event->connect.status);
673649
}
674650
client->m_semaphoreOpenEvt.give(event->connect.status);
675651
return 0;
@@ -723,17 +699,10 @@ uint16_t NimBLEClient::getMTU() {
723699

724700

725701
if(!rc && event->type == BLE_GAP_EVENT_CONN_UPDATE_REQ ) {
726-
if(client->m_pConnParams != nullptr) {
727-
event->conn_update_req.self_params->itvl_min = client->m_pConnParams->itvl_min;
728-
event->conn_update_req.self_params->itvl_max = client->m_pConnParams->itvl_max;
729-
event->conn_update_req.self_params->latency = client->m_pConnParams->latency;
730-
event->conn_update_req.self_params->supervision_timeout = client->m_pConnParams->supervision_timeout;
731-
} else {
732-
event->conn_update_req.self_params->itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
733-
event->conn_update_req.self_params->itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
734-
event->conn_update_req.self_params->latency = BLE_GAP_INITIAL_CONN_LATENCY;
735-
event->conn_update_req.self_params->supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
736-
}
702+
event->conn_update_req.self_params->itvl_min = client->m_pConnParams.itvl_min;
703+
event->conn_update_req.self_params->itvl_max = client->m_pConnParams.itvl_max;
704+
event->conn_update_req.self_params->latency = client->m_pConnParams.latency;
705+
event->conn_update_req.self_params->supervision_timeout = client->m_pConnParams.supervision_timeout;
737706
}
738707

739708
NIMBLE_LOGD(LOG_TAG, "%s peer params", (rc == 0) ? "Accepted" : "Rejected");
@@ -780,7 +749,7 @@ uint16_t NimBLEClient::getMTU() {
780749
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
781750
event->mtu.conn_handle,
782751
event->mtu.value);
783-
client->m_semaphoreOpenEvt.give(0);
752+
client->m_semaphoreOpenEvt.give(0);
784753
//client->m_mtu = event->mtu.value;
785754
return 0;
786755
} // BLE_GAP_EVENT_MTU

src/NimBLEClient.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ class NimBLEClient {
5252
void setConnectTimeout(uint8_t timeout);
5353
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
5454
uint16_t latency, uint16_t timeout,
55-
uint16_t minConnTime=16, uint16_t maxConnTime=768);
55+
uint16_t scanInterval=16, uint16_t scanWindow=16); // NimBLE default scan settings
5656
void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
57-
uint16_t latency, uint16_t timeout,
58-
uint16_t minConnTime=16, uint16_t maxConnTime=768);
57+
uint16_t latency, uint16_t timeout);
5958

6059

6160
private:
@@ -89,7 +88,7 @@ class NimBLEClient {
8988

9089
private:
9190
friend class NimBLEClientCallbacks;
92-
ble_gap_conn_params* m_pConnParams;
91+
ble_gap_conn_params m_pConnParams;
9392

9493
}; // class NimBLEClient
9594

src/NimBLEDevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959
#define BLEEddystoneTLM NimBLEEddystoneTLM
6060
#define BLEEddystoneURL NimBLEEddystoneURL
6161

62+
#ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
6263
#define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
64+
#else
65+
#define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
66+
#endif
6367

6468
/**
6569
* @brief BLE functions.

0 commit comments

Comments
 (0)