From ebbfe45e785e0c8b57eda46ef5269b908579e74f Mon Sep 17 00:00:00 2001 From: h2zero Date: Mon, 11 Apr 2022 21:34:06 -0600 Subject: [PATCH] WIP - compiles with mynewt --- pkg.yml | 20 ++++++++ src/NimBLEClient.cpp | 74 +++++++++++++---------------- src/NimBLEDevice.cpp | 49 +++++++++++++++---- src/NimBLEDevice.h | 25 ++++++++++ src/NimBLELog.h | 6 +-- src/NimBLERemoteCharacteristic.cpp | 75 ++++++++++++------------------ src/NimBLERemoteDescriptor.cpp | 37 +++++++-------- src/NimBLERemoteService.cpp | 16 +++---- src/NimBLEScan.cpp | 14 ++---- src/NimBLEUtils.h | 2 +- src/nimconfig.h | 13 ++++++ 11 files changed, 194 insertions(+), 137 deletions(-) create mode 100644 pkg.yml diff --git a/pkg.yml b/pkg.yml new file mode 100644 index 0000000..4d21f43 --- /dev/null +++ b/pkg.yml @@ -0,0 +1,20 @@ +pkg.name: esp-nimble-cpp +pkg.type: lib +pkg.description: NimBLE CPP wrapper +pkg.author: "Ryan Powell" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-nimble/nimble" + - "@apache-mynewt-nimble/nimble/host" + - "@apache-mynewt-nimble/nimble/host/services/gap" + - "@apache-mynewt-nimble/nimble/host/services/gatt" + - "@apache-mynewt-nimble/nimble/host/store/config" + - "@apache-mynewt-nimble/nimble/host/util" + +pkg.source_dirs: + - src + +pkg.include_dirs: + - src diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 3619b44..a6ce2db 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -23,7 +23,7 @@ #include #if defined(CONFIG_NIMBLE_CPP_IDF) -#include "nimble/nimble_port.h" +//#include "nimble/nimble_port.h" #else #include "nimble/porting/nimble/include/nimble/nimble_port.h" #endif @@ -81,7 +81,11 @@ NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(pee m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units memset(&m_dcTimer, 0, sizeof(m_dcTimer)); +#ifndef MYNEWT ble_npl_callout_init(&m_dcTimer, nimble_port_get_dflt_eventq(), +#else + ble_npl_callout_init(&m_dcTimer, ble_npl_eventq_dflt_get(), +#endif NimBLEClient::dcTimerCb, this); } // NimBLEClient @@ -99,7 +103,7 @@ NimBLEClient::~NimBLEClient() { delete m_pClientCallbacks; } - ble_npl_callout_deinit(&m_dcTimer); + //ble_npl_callout_deinit(&m_dcTimer); } // ~NimBLEClient @@ -215,8 +219,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { m_peerAddress = address; } - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; m_pTaskData = &taskData; int rc = 0; @@ -282,12 +285,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - // Wait for the connect timeout time +1 second for the connection to complete - if(ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(m_connectTimeout + 1000)) == pdFALSE) { + if (!NimBLEDevice::taskWait(m_pTaskData, m_connectTimeout + 1000)) { m_pTaskData = nullptr; // If a connection was made but no response from MTU exchange; disconnect if(isConnected()) { @@ -303,6 +301,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { return false; } else if(taskData.rc != 0){ + m_pTaskData = nullptr; m_lastErr = taskData.rc; NIMBLE_LOGE(LOG_TAG, "Connection failed; status=%d %s", taskData.rc, @@ -314,6 +313,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { } return false; } else { + m_pTaskData = nullptr; NIMBLE_LOGI(LOG_TAG, "Connection established"); } @@ -336,12 +336,12 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { * @return True on success. */ bool NimBLEClient::secureConnection() { - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; int retryCount = 1; do { + taskData.rc = -1; m_pTaskData = &taskData; int rc = NimBLEDevice::startSecurity(m_conn_id); @@ -350,14 +350,14 @@ bool NimBLEClient::secureConnection() { m_pTaskData = nullptr; return false; } - -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); +\ + if (!NimBLEDevice::taskWait(m_pTaskData, 10 * 1000)) { + break; + } } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--); + m_pTaskData = nullptr; + if(taskData.rc != 0){ m_lastErr = taskData.rc; return false; @@ -734,8 +734,7 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) { } int rc = 0; - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; if(uuid_filter == nullptr) { rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData); @@ -750,13 +749,12 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) { return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Retrieve services timeout"); + m_lastErr = taskData.rc; + return false; + } - // wait until we have all the services - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); m_lastErr = taskData.rc; if(taskData.rc == 0){ @@ -791,26 +789,21 @@ int NimBLEClient::serviceDiscoveredCB( return 0; } - if(error->status == 0) { + int rc = error->status; + + if (rc == 0) { // Found a service - add it to the vector NimBLERemoteService* pRemoteService = new NimBLERemoteService(client, service); client->m_servicesVector.push_back(pRemoteService); - return 0; - } - - if(error->status == BLE_HS_EDONE) { - pTaskData->rc = 0; - } else { + } else if (rc != BLE_HS_EDONE) { NIMBLE_LOGE(LOG_TAG, "serviceDiscoveredCB() rc=%d %s", - error->status, - NimBLEUtils::returnCodeToString(error->status)); - pTaskData->rc = error->status; + rc, NimBLEUtils::returnCodeToString(rc)); } - xTaskNotifyGive(pTaskData->task); + NimBLEDevice::taskComplete(pTaskData, rc == BLE_HS_EDONE ? 0 : rc); NIMBLE_LOGD(LOG_TAG,"<< Service Discovered"); - return error->status; + return rc; } @@ -1183,10 +1176,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) { } // Switch if(client->m_pTaskData != nullptr) { - client->m_pTaskData->rc = rc; - if(client->m_pTaskData->task) { - xTaskNotifyGive(client->m_pTaskData->task); - } + NimBLEDevice::taskComplete(client->m_pTaskData, rc); client->m_pTaskData = nullptr; } diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 1c337b8..fff2445 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -35,7 +35,15 @@ # include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" # endif #else -# include "nimble/nimble/controller/include/controller/ble_phy.h" +# include "controller/ble_phy.h" +# include "host/ble_hs.h" +# include "host/util/util.h" +# include "services/gap/ble_svc_gap.h" +# include "services/gatt/ble_svc_gatt.h" +//#include "porting/nimble/include/nimble/nimble_port.h" +#include "os/os.h" + + #endif #ifndef CONFIG_NIMBLE_CPP_IDF @@ -249,7 +257,7 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { } while(pClient->isConnected()) { - taskYIELD(); + os_time_delay(1); } // Since we set the flag to false the app will not get a callback // in the disconnect event so we call it here for good measure. @@ -261,7 +269,7 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { return false; } while(pClient->m_pTaskData != nullptr) { - taskYIELD(); + os_time_delay(1); } } @@ -424,12 +432,13 @@ int NimBLEDevice::getPower(esp_ble_power_type_t powerType) { #else void NimBLEDevice::setPower(int dbm) { - ble_phy_txpwr_set(dbm); + //ble_phy_txpwr_set(dbm); } int NimBLEDevice::getPower() { - return ble_phy_txpwr_get(); + // return ble_phy_txpwr_get(); + return 0; } #endif @@ -804,7 +813,8 @@ void NimBLEDevice::onSync(void) // Yield for houskeeping before returning to operations. // Occasionally triggers exception without. - taskYIELD(); + //taskYIELD(); + os_time_delay(1); m_synced = true; @@ -823,7 +833,6 @@ void NimBLEDevice::onSync(void) } } // onSync - /** * @brief The main host task. */ @@ -833,12 +842,21 @@ void NimBLEDevice::host_task(void *param) NIMBLE_LOGI(LOG_TAG, "BLE Host Task Started"); /* This function will return only when nimble_port_stop() is executed */ +#ifndef MYNEWT nimble_port_run(); nimble_port_freertos_deinit(); +#else + while (1) { + os_eventq_run(os_eventq_dflt_get()); + } +#endif } // host_task +static struct os_task ble_nimble_task; +static os_stack_t ble_nimble_stack[1024]; + /** * @brief Initialize the %BLE environment. * @param [in] deviceName The device name of the device. @@ -880,7 +898,12 @@ void NimBLEDevice::init(const std::string &deviceName) { ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE)); ESP_ERROR_CHECK(esp_nimble_hci_init()); #endif + +#ifndef MYNEWT nimble_port_init(); +#else + sysinit(); +#endif // Setup callbacks for host events ble_hs_cfg.reset_cb = NimBLEDevice::onReset; @@ -902,12 +925,17 @@ void NimBLEDevice::init(const std::string &deviceName) { ble_store_config_init(); +#ifndef MYNEWT nimble_port_freertos_init(NimBLEDevice::host_task); +#else + os_task_init(&ble_nimble_task, "ble_nimble_task", NimBLEDevice::host_task, NULL, 8, + OS_WAIT_FOREVER, ble_nimble_stack, 1024); +#endif } // Wait for host and controller to sync before returning and accepting new tasks while(!m_synced){ - taskYIELD(); + os_time_delay(1); } initialized = true; // Set the initialization flag to ensure we are only initialized once. @@ -921,9 +949,14 @@ void NimBLEDevice::init(const std::string &deviceName) { */ /* STATIC */ void NimBLEDevice::deinit(bool clearAll) { +#ifndef MYNEWT int ret = nimble_port_stop(); if (ret == 0) { nimble_port_deinit(); +#else + int ret = ble_hs_shutdown(0); + if (ret == 0) { +#endif #ifdef ESP_PLATFORM ret = esp_nimble_hci_and_controller_deinit(); if (ret != ESP_OK) { diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index b7e804b..1236d6a 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -175,6 +175,31 @@ public: static NimBLEAddress getBondedAddress(int index); #endif + static bool taskWait(ble_task_data_t* td, uint32_t waitms ) { +#if 0 //defined INC_FREERTOS_H + td->task = xTaskGetCurrentTaskHandle(); +# ifdef ulTaskNotifyValueClear + // Clear the task notification value to ensure we block + ulTaskNotifyValueClear(td->task, ULONG_MAX); +# endif + // Wait for the connect timeout time +1 second for the connection to complete + return ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(waitms)); +#else + ble_npl_time_t ticks = ble_npl_time_ms_to_ticks32(waitms); + while(td->rc < 0 && --ticks){ + ble_npl_time_delay(1); + } + return (ticks > 0); +#endif + } + + static void taskComplete(ble_task_data_t* td, int rc) { + td->rc = rc; +#if 0 //defined INC_FREERTOS_H + xTaskNotifyGive(td->task); +#endif + } + private: #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) friend class NimBLEClient; diff --git a/src/NimBLELog.h b/src/NimBLELog.h index dda9073..81b973d 100644 --- a/src/NimBLELog.h +++ b/src/NimBLELog.h @@ -12,7 +12,7 @@ #if defined(CONFIG_BT_ENABLED) -#if defined(CONFIG_NIMBLE_CPP_IDF) // using esp-idf +#if 0 // using esp-idf # include "esp_log.h" # ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL # define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 @@ -39,8 +39,8 @@ NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__) #else // using Arduino -# include "nimble/porting/nimble/include/syscfg/syscfg.h" -# include "nimble/console/console.h" +# include "syscfg/syscfg.h" +# include "console/console.h" # ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL # if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL) # define CONFIG_NIMBLE_CPP_LOG_LEVEL CORE_DEBUG_LEVEL diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index 6cca615..cf4e78b 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -16,6 +16,7 @@ #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #include "NimBLERemoteCharacteristic.h" +#include "NimBLEDevice.h" #include "NimBLEUtils.h" #include "NimBLELog.h" @@ -179,14 +180,7 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t conn_handle, * Else if rc == 0, just return 0 to continue the discovery until we get BLE_HS_EDONE. * If we get any other error code tell the application to abort by returning non-zero in the rc. */ - if (rc == BLE_HS_EDONE) { - pTaskData->rc = 0; - xTaskNotifyGive(pTaskData->task); - } else if(rc != 0) { - // Error; abort discovery. - pTaskData->rc = rc; - xTaskNotifyGive(pTaskData->task); - } + NimBLEDevice::taskComplete(pTaskData, rc == BLE_HS_EDONE ? 0 : rc); NIMBLE_LOGD(LOG_TAG,"<< Descriptor Discovered. status: %d", pTaskData->rc); return rc; @@ -216,11 +210,9 @@ int NimBLERemoteCharacteristic::nextCharCB(uint16_t conn_handle, rc = BLE_HS_EDONE; } else if (rc == BLE_HS_EDONE) { pChar->m_endHandle = pChar->getRemoteService()->getEndHandle(); - } else { - pTaskData->rc = rc; } - xTaskNotifyGive(pTaskData->task); + NimBLEDevice::taskComplete(pTaskData, rc == BLE_HS_EDONE ? 0 : rc); return rc; } @@ -238,8 +230,7 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt } int rc = 0; - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; // If we don't know the end handle of this characteristic retrieve the next one in the service // The end handle is the next characteristic definition handle -1. @@ -254,11 +245,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Find end handle timeout"); + return false; + } if (taskData.rc != 0) { NIMBLE_LOGE(LOG_TAG, "Could not retrieve end handle rc=%d", taskData.rc); @@ -270,6 +260,7 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt return true; } + taskData.rc = -1; desc_filter_t filter = {uuid_filter, &taskData}; rc = ble_gattc_disc_all_dscs(getRemoteService()->getClient()->getConnId(), @@ -283,11 +274,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Discover Descriptors timeout"); + return false; + } if (taskData.rc != 0) { NIMBLE_LOGE(LOG_TAG, "Failed to retrieve descriptors; startHandle:%d endHandle:%d taskData.rc=%d", @@ -501,10 +491,8 @@ NimBLEAttValue NimBLERemoteCharacteristic::readValue(time_t *timestamp) { int rc = 0; int retryCount = 1; - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, &value}; - do { + ble_task_data_t taskData = {this, nullptr, -1, &value}; rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0, NimBLERemoteCharacteristic::onReadCB, &taskData); @@ -514,11 +502,10 @@ NimBLEAttValue NimBLERemoteCharacteristic::readValue(time_t *timestamp) { return value; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "readValue timeout"); + return value; + } rc = taskData.rc; switch(rc){ @@ -588,9 +575,7 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, } } - pTaskData->rc = rc; - xTaskNotifyGive(pTaskData->task); - + NimBLEDevice::taskComplete(pTaskData, rc); return rc; } @@ -783,10 +768,8 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length, return (rc==0); } - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; - do { + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; if(length > mtu) { NIMBLE_LOGI(LOG_TAG,"long write %d bytes", length); os_mbuf *om = ble_hs_mbuf_from_flat(data, length); @@ -799,16 +782,17 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length, NimBLERemoteCharacteristic::onWriteCB, &taskData); } + if (rc != 0) { NIMBLE_LOGE(LOG_TAG, "Error: Failed to write characteristic; rc=%d", rc); return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Write value timeout"); + return false; + } + rc = taskData.rc; switch(rc){ @@ -854,11 +838,10 @@ int NimBLERemoteCharacteristic::onWriteCB(uint16_t conn_handle, return 0; } - NIMBLE_LOGI(LOG_TAG, "Write complete; status=%d conn_handle=%d", error->status, conn_handle); - - pTaskData->rc = error->status; - xTaskNotifyGive(pTaskData->task); + int rc = error->status; + NIMBLE_LOGI(LOG_TAG, "Write complete; status=%d conn_handle=%d", rc, conn_handle); + NimBLEDevice::taskComplete(pTaskData, rc == BLE_HS_EDONE ? 0 : rc); return 0; } diff --git a/src/NimBLERemoteDescriptor.cpp b/src/NimBLERemoteDescriptor.cpp index cae9103..78eadc8 100644 --- a/src/NimBLERemoteDescriptor.cpp +++ b/src/NimBLERemoteDescriptor.cpp @@ -16,6 +16,7 @@ #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #include "NimBLERemoteDescriptor.h" +#include "NimBLEDevice.h" #include "NimBLEUtils.h" #include "NimBLELog.h" @@ -127,10 +128,10 @@ NimBLEAttValue NimBLERemoteDescriptor::readValue() { int rc = 0; int retryCount = 1; - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, &value}; do { + ble_task_data_t taskData = {this, nullptr, -1, &value}; + rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0, NimBLERemoteDescriptor::onReadCB, &taskData); @@ -140,11 +141,11 @@ NimBLEAttValue NimBLERemoteDescriptor::readValue() { return value; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Read desc value timeout"); + return false; + } + rc = taskData.rc; switch(rc){ @@ -208,9 +209,7 @@ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle, } } - pTaskData->rc = rc; - xTaskNotifyGive(pTaskData->task); - + NimBLEDevice::taskComplete(pTaskData, rc); return rc; } @@ -247,8 +246,7 @@ int NimBLERemoteDescriptor::onWriteCB(uint16_t conn_handle, NIMBLE_LOGI(LOG_TAG, "Write complete; status=%d conn_handle=%d", error->status, conn_handle); - pTaskData->rc = error->status; - xTaskNotifyGive(pTaskData->task); + NimBLEDevice::taskComplete(pTaskData, error->status); return 0; } @@ -306,10 +304,9 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool return (rc == 0); } - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; - do { + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; + if(length > mtu) { NIMBLE_LOGI(LOG_TAG,"long write %d bytes", length); os_mbuf *om = ble_hs_mbuf_from_flat(data, length); @@ -328,11 +325,11 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "Desc write value timeout"); + return false; + } + rc = taskData.rc; switch(rc) { diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index f0cbed1..289ef39 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -199,7 +199,8 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle, pTaskData->rc = error->status; } - xTaskNotifyGive(pTaskData->task); + //xTaskNotifyGive(pTaskData->task); + NimBLEDevice::taskComplete(pTaskData, pTaskData->rc); NIMBLE_LOGD(LOG_TAG,"<< Characteristic Discovered"); return error->status; @@ -215,8 +216,8 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter) NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str()); int rc = 0; - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {this, cur_task, 0, nullptr}; + //TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); + ble_task_data_t taskData = {this, nullptr, -1, nullptr}; if(uuid_filter == nullptr) { rc = ble_gattc_disc_all_chrs(m_pClient->getConnId(), @@ -238,11 +239,10 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter) return false; } -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (!NimBLEDevice::taskWait(&taskData, 10 * 1000)) { + NIMBLE_LOGE(LOG_TAG, "disc chars timeout"); + return false; + } if(taskData.rc == 0){ if (uuid_filter == nullptr) { diff --git a/src/NimBLEScan.cpp b/src/NimBLEScan.cpp index eb87df8..fe11ec9 100644 --- a/src/NimBLEScan.cpp +++ b/src/NimBLEScan.cpp @@ -180,7 +180,7 @@ NimBLEScan::~NimBLEScan() { if(pScan->m_pTaskData != nullptr) { pScan->m_pTaskData->rc = event->disc_complete.reason; - xTaskNotifyGive(pScan->m_pTaskData->task); + NimBLEDevice::taskComplete(pScan->m_pTaskData , 0); } return 0; @@ -397,16 +397,12 @@ NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) { NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever"); } - TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); - ble_task_data_t taskData = {nullptr, cur_task, 0, nullptr}; + //TaskHandle_t cur_task = xTaskGetCurrentTaskHandle(); + ble_task_data_t taskData = {nullptr, nullptr, -1, nullptr}; m_pTaskData = &taskData; if(start(duration, nullptr, is_continue)) { -#ifdef ulTaskNotifyValueClear - // Clear the task notification value to ensure we block - ulTaskNotifyValueClear(cur_task, ULONG_MAX); -#endif - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + NimBLEDevice::taskWait(&taskData, 10 * 1000); } m_pTaskData = nullptr; @@ -436,7 +432,7 @@ bool NimBLEScan::stop() { } if(m_pTaskData != nullptr) { - xTaskNotifyGive(m_pTaskData->task); + NimBLEDevice::taskComplete(m_pTaskData, 0); } NIMBLE_LOGD(LOG_TAG, "<< stop()"); diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index 006d935..39f0a11 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -27,7 +27,7 @@ typedef struct { void *pATT; - TaskHandle_t task; + void * task; int rc; void *buf; } ble_task_data_t; diff --git a/src/nimconfig.h b/src/nimconfig.h index 9c19031..33b50da 100644 --- a/src/nimconfig.h +++ b/src/nimconfig.h @@ -6,6 +6,7 @@ */ #pragma once +#ifdef ESP_PLATFORM #include "sdkconfig.h" #include "nimconfig_rename.h" @@ -138,3 +139,15 @@ #define CONFIG_BT_NIMBLE_TASK_STACK_SIZE 4096 #endif // _DOXYGEN_ + +#else +#include "syscfg/syscfg.h" +#define CONFIG_BT_ENABLED +#define CONFIG_BT_NIMBLE_ROLE_OBSERVER +#define CONFIG_BT_NIMBLE_ROLE_BROADCASTER +#define CONFIG_BT_NIMBLE_ROLE_CENTRAL +#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL +#define CONFIG_NIMBLE_CPP_IDF +#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 3 +#define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 +#endif \ No newline at end of file