From 1779a3f72328e57f5ee5f033a31abf4f555643a9 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 3 May 2020 13:50:49 -0600 Subject: [PATCH] Bugfixes + add new NimBLEUUID constructor. --- src/NimBLEAddress.cpp | 4 ++-- src/NimBLEAddress.h | 18 +----------------- src/NimBLEClient.cpp | 2 +- src/NimBLEClient.h | 2 +- src/NimBLEDevice.cpp | 28 +++++++++++++++------------- src/NimBLEScan.cpp | 2 +- src/NimBLEUUID.cpp | 20 ++++++++++++++++++++ src/NimBLEUUID.h | 1 + 8 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/NimBLEAddress.cpp b/src/NimBLEAddress.cpp index 074ce7e..4a8f52d 100644 --- a/src/NimBLEAddress.cpp +++ b/src/NimBLEAddress.cpp @@ -59,9 +59,9 @@ NimBLEAddress::NimBLEAddress(std::string stringAddress) { /** * @brief Constructor for compatibility with bluedrioid esp library. - * @param [in] esp_bd_addr_t struct containing the address. + * @param [in] uint8_t[6] or esp_bd_addr_t struct containing the address. */ -NimBLEAddress::NimBLEAddress(esp_bd_addr_t address) { +NimBLEAddress::NimBLEAddress(uint8_t address[6]) { NimBLEUtils::memrcpy(m_address, address, 6); } // NimBLEAddress diff --git a/src/NimBLEAddress.h b/src/NimBLEAddress.h index ac1f26c..883608a 100644 --- a/src/NimBLEAddress.h +++ b/src/NimBLEAddress.h @@ -25,22 +25,6 @@ #include -typedef enum { - BLE_ADDR_TYPE_PUBLIC = 0x00, - BLE_ADDR_TYPE_RANDOM = 0x01, - BLE_ADDR_TYPE_RPA_PUBLIC = 0x02, - BLE_ADDR_TYPE_RPA_RANDOM = 0x03, -} esp_nimble_addr_type_t; - -typedef uint8_t esp_ble_addr_type_t ; - -/// Bluetooth address length -#define ESP_BD_ADDR_LEN 6 - -/// Bluetooth device address -typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]; - - /** * @brief A %BLE device address. * @@ -49,7 +33,7 @@ typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]; class NimBLEAddress { public: NimBLEAddress(ble_addr_t address); - NimBLEAddress(esp_bd_addr_t address); + NimBLEAddress(uint8_t address[6]); NimBLEAddress(std::string stringAddress); bool equals(NimBLEAddress otherAddress); uint8_t* getNative(); diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 3930acf..022fecb 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -74,7 +74,7 @@ NimBLEClient::~NimBLEClient() { // Before we are finished with the client, we must release resources. clearServices(); - if(m_deleteCallbacks) { + if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) { delete m_pClientCallbacks; } diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 3d3613c..14d14bf 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -34,7 +34,7 @@ class NimBLEAdvertisedDevice; class NimBLEClient { public: bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); - bool connect(NimBLEAddress address, uint8_t type = BLE_ADDR_TYPE_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server + bool connect(NimBLEAddress address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server int getRssi(); // Get the RSSI of the remote BLE Server diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 37c3690..d85eae1 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -379,14 +379,16 @@ void NimBLEDevice::stopAdvertising() { m_synced = true; - if(m_pScan != nullptr) { - // Restart scanning with the last values sent, allow to clear results. - m_pScan->start(m_pScan->m_duration, m_pScan->m_scanCompleteCB); - } - - if(m_bleAdvertising != nullptr) { - // Restart advertisng, parameters should already be set. - m_bleAdvertising->start(); + if(initialized) { + if(m_pScan != nullptr) { + // Restart scanning with the last values sent, allow to clear results. + m_pScan->start(m_pScan->m_duration, m_pScan->m_scanCompleteCB); + } + + if(m_bleAdvertising != nullptr) { + // Restart advertisng, parameters should already be set. + m_bleAdvertising->start(); + } } } // onSync @@ -410,8 +412,6 @@ void NimBLEDevice::stopAdvertising() { */ /* STATIC */ void NimBLEDevice::init(std::string deviceName) { if(!initialized){ - initialized = true; // Set the initialization flag to ensure we are only initialized once. - int rc=0; esp_err_t errRc = ESP_OK; @@ -421,7 +421,7 @@ void NimBLEDevice::stopAdvertising() { #endif errRc = nvs_flash_init(); - + if (errRc == ESP_ERR_NVS_NO_FREE_PAGES || errRc == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); errRc = nvs_flash_init(); @@ -452,14 +452,15 @@ void NimBLEDevice::stopAdvertising() { assert(rc == 0); ble_store_config_init(); - + nimble_port_freertos_init(NimBLEDevice::host_task); } // Wait for host and controller to sync before returning and accepting new tasks while(!m_synced){ vTaskDelay(1 / portTICK_PERIOD_MS); } - //vTaskDelay(200 / portTICK_PERIOD_MS); // Delay for 200 msecs as a workaround to an apparent Arduino environment issue. + + initialized = true; // Set the initialization flag to ensure we are only initialized once. } // init @@ -477,6 +478,7 @@ void NimBLEDevice::stopAdvertising() { } initialized = false; + m_synced = false; } } // deinit diff --git a/src/NimBLEScan.cpp b/src/NimBLEScan.cpp index e5cbed1..65c8e5b 100644 --- a/src/NimBLEScan.cpp +++ b/src/NimBLEScan.cpp @@ -141,7 +141,7 @@ NimBLEScan::NimBLEScan() { if (pScan->m_pAdvertisedDeviceCallbacks) { // If not active scanning report the result to the listener. - if(pScan->m_scan_params.passive) { + if(pScan->m_scan_params.passive || event->disc.event_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND) { pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); // Otherwise wait for the scan response so we can report all of the data at once. } else if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) { diff --git a/src/NimBLEUUID.cpp b/src/NimBLEUUID.cpp index 0002d19..c718cd6 100644 --- a/src/NimBLEUUID.cpp +++ b/src/NimBLEUUID.cpp @@ -156,6 +156,26 @@ NimBLEUUID::NimBLEUUID(ble_uuid128_t* uuid) { } // NimBLEUUID +/** + * @brief Create a UUID from the 128bit value using hex parts instead of string, + * instead of BLEUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"), it becomes + * BLEUUID(0xebe0ccb0, 0x7a0a, 0x4b0c, 0x8a1a6ff2997da3a6) + * + * @param [in] first The first 32bit of the UUID. + * @param [in] second The next 16bit of the UUID. + * @param [in] third The next 16bit of the UUID. + * @param [in] fourth The last 64bit of the UUID, combining the last 2 parts of the string equivalent + */ +NimBLEUUID::NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t fourth) { + m_uuid.u.type = BLE_UUID_TYPE_128; + memcpy(m_uuid.u128.value + 12, &first, 4); + memcpy(m_uuid.u128.value + 10, &second, 2); + memcpy(m_uuid.u128.value + 8, &third, 2); + memcpy(m_uuid.u128.value, &fourth, 8); + m_valueSet = true; +} + + NimBLEUUID::NimBLEUUID() { m_valueSet = false; } // NimBLEUUID diff --git a/src/NimBLEUUID.h b/src/NimBLEUUID.h index 63230fb..ed08a14 100644 --- a/src/NimBLEUUID.h +++ b/src/NimBLEUUID.h @@ -35,6 +35,7 @@ public: NimBLEUUID(uint32_t uuid); NimBLEUUID(ble_uuid128_t* uuid); NimBLEUUID(uint8_t* pData, size_t size, bool msbFirst); + NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t fourth); NimBLEUUID(); uint8_t bitSize(); // Get the number of bits in this uuid. bool equals(NimBLEUUID uuid);