Skip to content

Commit 8687fc4

Browse files
committed
Few bugixes, compatibility changes and requested functionalities
1 parent 011ea72 commit 8687fc4

10 files changed

+84
-54
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
233233
uint8_t ad_type;
234234
uint8_t sizeConsumed = 0;
235235
bool finished = false;
236-
setPayload(payload);
236+
m_payload = payload;
237+
m_payloadLength = total_len;
237238

238239
while(!finished) {
239240
length = *payload; // Retrieve the length of the record.
@@ -351,9 +352,9 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
351352
} // Length <> 0
352353

353354

354-
if (sizeConsumed >= total_len || length == 0) {
355+
if (sizeConsumed >= total_len)
355356
finished = true;
356-
}
357+
357358
} // !finished
358359
} // parseAdvertisement
359360

@@ -510,10 +511,6 @@ uint8_t* BLEAdvertisedDevice::getPayload() {
510511
return m_payload;
511512
}
512513

513-
void BLEAdvertisedDevice::setPayload(uint8_t* payload) {
514-
m_payload = payload;
515-
}
516-
517514
esp_ble_addr_type_t BLEAdvertisedDevice::getAddressType() {
518515
return m_addressType;
519516
}
@@ -522,6 +519,9 @@ void BLEAdvertisedDevice::setAddressType(esp_ble_addr_type_t type) {
522519
m_addressType = type;
523520
}
524521

522+
size_t BLEAdvertisedDevice::getPayloadLength() {
523+
return m_payloadLength;
524+
}
525525

526526
#endif /* CONFIG_BT_ENABLED */
527527

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BLEAdvertisedDevice {
4040
BLEUUID getServiceUUID();
4141
int8_t getTXPower();
4242
uint8_t* getPayload();
43+
size_t getPayloadLength();
4344
esp_ble_addr_type_t getAddressType();
4445
void setAddressType(esp_ble_addr_type_t type);
4546

@@ -72,7 +73,6 @@ class BLEAdvertisedDevice {
7273
void setServiceUUID(const char* serviceUUID);
7374
void setServiceUUID(BLEUUID serviceUUID);
7475
void setTXPower(int8_t txPower);
75-
void setPayload(uint8_t* payload);
7676

7777
bool m_haveAppearance;
7878
bool m_haveManufacturerData;
@@ -96,6 +96,7 @@ class BLEAdvertisedDevice {
9696
std::string m_serviceData;
9797
BLEUUID m_serviceDataUUID;
9898
uint8_t* m_payload;
99+
size_t m_payloadLength = 0;
99100
esp_ble_addr_type_t m_addressType;
100101
};
101102

cpp_utils/BLECharacteristic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ void BLECharacteristic::handleGATTServerEvent(
430430
// - uint16_t conn_id – The connection used.
431431
//
432432
case ESP_GATTS_CONF_EVT: {
433-
ESP_LOGD(LOG_TAG, "m_handle = %d, conf->handle = %d", m_handle, param->conf.handle);
433+
// ESP_LOGD(LOG_TAG, "m_handle = %d, conf->handle = %d", m_handle, param->conf.handle);
434434
if(param->conf.conn_id == getService()->getServer()->getConnId()) // && param->conf.handle == m_handle) // bug in esp-idf and not implemented in arduino yet
435435
m_semaphoreConfEvt.give(param->conf.status);
436436
break;

cpp_utils/BLEClient.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
102102
// We need the connection handle that we get from registering the application. We register the app
103103
// and then block on its completion. When the event has arrived, we will have the handle.
104104
m_appId = BLEDevice::m_appId++;
105-
BLEDevice::addPeerDevice(this, true, ESP_GATT_IF_NONE);
105+
BLEDevice::addPeerDevice(this, true, m_appId);
106106
m_semaphoreRegEvt.take("connect");
107107

108108
// clearServices(); // we dont need to delete services since every client is unique?
@@ -119,10 +119,10 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
119119
// Perform the open connection request against the target BLE Server.
120120
m_semaphoreOpenEvt.take("connect");
121121
errRc = ::esp_ble_gattc_open(
122-
getGattcIf(),
122+
m_gattc_if,
123123
*getPeerAddress().getNative(), // address
124124
type, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature.
125-
1 // direct connection
125+
1 // direct connection <-- maybe needs to be changed in case of direct indirect connection???
126126
);
127127
if (errRc != ESP_OK) {
128128
ESP_LOGE(LOG_TAG, "esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -184,10 +184,12 @@ void BLEClient::gattClientEventHandler(
184184
case ESP_GATTC_DISCONNECT_EVT: {
185185
// If we receive a disconnect event, set the class flag that indicates that we are
186186
// no longer connected.
187+
m_isConnected = false;
187188
if (m_pClientCallbacks != nullptr) {
188189
m_pClientCallbacks->onDisconnect(this);
189190
}
190-
m_isConnected = false;
191+
BLEDevice::removePeerDevice(m_appId, true);
192+
esp_ble_gattc_app_unregister(m_gattc_if);
191193
m_semaphoreRssiCmplEvt.give();
192194
m_semaphoreSearchCmplEvt.give(1);
193195
break;
@@ -261,14 +263,15 @@ void BLEClient::gattClientEventHandler(
261263
ESP_LOGE(LOG_TAG, "search service failed, error status = %x", p_data->search_cmpl.status);
262264
break;
263265
}
264-
#ifndef ARDUINO_ARCH_ESP32
265-
if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) {
266-
ESP_LOGI(LOG_TAG, "Get service information from remote device");
267-
} else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) {
268-
ESP_LOGI(LOG_TAG, "Get service information from flash");
269-
} else {
270-
ESP_LOGI(LOG_TAG, "unknown service source");
271-
}
266+
#ifndef ARDUINO_ARCH_ESP32
267+
// commented out just for now to keep backward compatibility
268+
// if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) {
269+
// ESP_LOGI(LOG_TAG, "Get service information from remote device");
270+
// } else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) {
271+
// ESP_LOGI(LOG_TAG, "Get service information from flash");
272+
// } else {
273+
// ESP_LOGI(LOG_TAG, "unknown service source");
274+
// }
272275
#endif
273276
m_semaphoreSearchCmplEvt.give(0);
274277
break;

cpp_utils/BLEDevice.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ void BLEDevice::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
603603

604604
void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
605605
ESP_LOGI(LOG_TAG, "remove: %d, GATT role %s", conn_id, _client?"client":"server");
606-
m_connectedClientsMap.erase(conn_id);
606+
if(m_connectedClientsMap.find(conn_id) != m_connectedClientsMap.end())
607+
m_connectedClientsMap.erase(conn_id);
607608
}
608609

609610
/* multi connect support */
@@ -621,7 +622,7 @@ void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
621622
esp_bt_controller_deinit();
622623
#ifndef ARDUINO_ARCH_ESP32
623624
if (release_memory) {
624-
esp_bt_mem_release(ESP_BT_MODE_BTDM); // <-- require tests because we released classic BT memory and this can cause crash (most likely not, esp-idf takes care of it)
625+
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // <-- require tests because we released classic BT memory and this can cause crash (most likely not, esp-idf takes care of it)
625626
} else {
626627
initialized = false;
627628
}

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
473473
}
474474

475475
uint8_t val[] = {0x00, 0x00};
476-
BLERemoteDescriptor* desc = getDescriptor(BLEUUID("0x2902"));
476+
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
477477
desc->writeValue(val, 2);
478478
} // End Unregister
479479

@@ -520,7 +520,32 @@ std::string BLERemoteCharacteristic::toString() {
520520
* @return N/A.
521521
*/
522522
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
523-
ESP_LOGD(LOG_TAG, ">> writeValue(), length: %d", newValue.length());
523+
writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response);
524+
} // writeValue
525+
526+
527+
/**
528+
* @brief Write the new value for the characteristic.
529+
*
530+
* This is a convenience function. Many BLE characteristics are a single byte of data.
531+
* @param [in] newValue The new byte value to write.
532+
* @param [in] response Whether we require a response from the write.
533+
* @return N/A.
534+
*/
535+
void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
536+
writeValue(&newValue, 1, response);
537+
} // writeValue
538+
539+
540+
/**
541+
* @brief Write the new value for the characteristic from a data buffer.
542+
* @param [in] data A pointer to a data buffer.
543+
* @param [in] length The length of the data in the data buffer.
544+
* @param [in] response Whether we require a response from the write.
545+
*/
546+
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) {
547+
// writeValue(std::string((char*)data, length), response);
548+
ESP_LOGD(LOG_TAG, ">> writeValue(), length: %d", length);
524549

525550
// Check to see that we are connected.
526551
if (!getRemoteService()->getClient()->isConnected()) {
@@ -534,8 +559,8 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
534559
m_pRemoteService->getClient()->getGattcIf(),
535560
m_pRemoteService->getClient()->getConnId(),
536561
getHandle(),
537-
newValue.length(),
538-
(uint8_t*)newValue.data(),
562+
length,
563+
data,
539564
response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP,
540565
ESP_GATT_AUTH_REQ_NONE
541566
);
@@ -550,30 +575,6 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
550575
ESP_LOGD(LOG_TAG, "<< writeValue");
551576
} // writeValue
552577

553-
554-
/**
555-
* @brief Write the new value for the characteristic.
556-
*
557-
* This is a convenience function. Many BLE characteristics are a single byte of data.
558-
* @param [in] newValue The new byte value to write.
559-
* @param [in] response Whether we require a response from the write.
560-
* @return N/A.
561-
*/
562-
void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
563-
writeValue(std::string(reinterpret_cast<char*>(&newValue), 1), response);
564-
} // writeValue
565-
566-
567-
/**
568-
* @brief Write the new value for the characteristic from a data buffer.
569-
* @param [in] data A pointer to a data buffer.
570-
* @param [in] length The length of the data in the data buffer.
571-
* @param [in] response Whether we require a response from the write.
572-
*/
573-
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) {
574-
writeValue(std::string((char*)data, length), response);
575-
} // writeValue
576-
577578
/**
578579
* @brief Read raw data from remote characteristic as hex bytes
579580
* @return return pointer data read

cpp_utils/BLEScan.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void BLEScan::handleGAPEvent(
101101

102102
if (found && !m_wantDuplicates) { // If we found a previous entry AND we don't want duplicates, then we are done.
103103
ESP_LOGD(LOG_TAG, "Ignoring %s, already seen it.", advertisedAddress.toString().c_str());
104-
vTaskDelay(1); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we will blocked here
104+
vTaskDelay(1); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
105105
break;
106106
}
107107

@@ -314,5 +314,15 @@ BLEAdvertisedDevice BLEScanResults::getDevice(uint32_t i) {
314314
return dev;
315315
}
316316

317+
BLEScanResults BLEScan::getResults() {
318+
return m_scanResults;
319+
}
320+
321+
void BLEScan::clearResults() {
322+
for(auto _dev : m_scanResults.m_vectorAdvertisedDevices){
323+
delete _dev.second;
324+
}
325+
m_scanResults.m_vectorAdvertisedDevices.clear();
326+
}
317327

318328
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEServer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,17 @@ void BLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
405405
m_connectedServersMap.erase(conn_id);
406406
}
407407
/* multi connect support */
408+
409+
/**
410+
* Update connection parameters can be called only after connection has been established
411+
*/
412+
void BLEServer::updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) {
413+
esp_ble_conn_update_params_t conn_params;
414+
memcpy(conn_params.bda, remote_bda, sizeof(esp_bd_addr_t));
415+
conn_params.latency = latency;
416+
conn_params.max_int = maxInterval; // max_int = 0x20*1.25ms = 40ms
417+
conn_params.min_int = minInterval; // min_int = 0x10*1.25ms = 20ms
418+
conn_params.timeout = timeout; // timeout = 400*10ms = 4000ms
419+
esp_ble_gap_update_conn_params(&conn_params);
420+
}
408421
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class BLEServer {
7373
BLEService* getServiceByUUID(BLEUUID uuid);
7474
bool connect(BLEAddress address);
7575
uint16_t m_appId;
76+
void updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
7677

7778
/* multi connection support */
7879
std::map<uint16_t, conn_status_t> getPeerDevices(bool client);
@@ -81,6 +82,7 @@ class BLEServer {
8182
BLEServer* getServerByConnId(uint16_t conn_id);
8283
void updatePeerMTU(uint16_t connId, uint16_t mtu);
8384
uint16_t getPeerMTU(uint16_t conn_id);
85+
uint16_t getConnId();
8486

8587

8688
private:
@@ -102,7 +104,6 @@ class BLEServer {
102104
BLEServerCallbacks* m_pServerCallbacks = nullptr;
103105

104106
void createApp(uint16_t appId);
105-
uint16_t getConnId();
106107
uint16_t getGattsIf();
107108
void handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
108109
void registerApp(uint16_t);

cpp_utils/BLEUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
947947
return "ESP_GATTC_WRITE_DESCR_EVT";
948948
#endif
949949
default:
950-
ESP_LOGW(LOG_TAG, "Unknown GATT Client event type: %d", eventType);
950+
ESP_LOGV(LOG_TAG, "Unknown GATT Client event type: %d", eventType);
951951
return "Unknown";
952952
}
953953
} // gattClientEventTypeToString

0 commit comments

Comments
 (0)