diff --git a/src/NimBLEAdvertising.h b/src/NimBLEAdvertising.h index 1d25b81..ad76d7e 100644 --- a/src/NimBLEAdvertising.h +++ b/src/NimBLEAdvertising.h @@ -27,7 +27,6 @@ /**************************/ #include "NimBLEUUID.h" -#include "FreeRTOS.h" #include diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 82c3183..bf93b8e 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -19,7 +19,6 @@ #include "NimBLE2902.h" #include "NimBLE2904.h" #include "NimBLEDevice.h" -#include "NimBLEUtils.h" #include "NimBLELog.h" #define NULL_HANDLE (0xffff) @@ -51,21 +50,13 @@ NimBLECharacteristic::NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t prop m_pService = pService; m_value = ""; m_valMux = portMUX_INITIALIZER_UNLOCKED; - - if(properties & NIMBLE_PROPERTY::INDICATE){ - m_pIndSemaphore = new FreeRTOS::Semaphore("ConfEvt"); - } else { - m_pIndSemaphore = nullptr; - } + m_pTaskData = nullptr; } // NimBLECharacteristic /** * @brief Destructor. */ NimBLECharacteristic::~NimBLECharacteristic() { - if(m_pIndSemaphore != nullptr) { - delete(m_pIndSemaphore); - } } // ~NimBLECharacteristic @@ -271,9 +262,10 @@ void NimBLECharacteristic::setSubscribe(struct ble_gap_event *event) { subVal |= NIMBLE_DESC_FLAG_INDICATE; } - if(m_pIndSemaphore != nullptr) { - m_pIndSemaphore->give((subVal & NIMBLE_DESC_FLAG_INDICATE) ? 0 : - NimBLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED); + if(m_pTaskData != nullptr) { + m_pTaskData->rc = (subVal & NIMBLE_DESC_FLAG_INDICATE) ? 0 : + NimBLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED; + xTaskNotifyGive(m_pTaskData->task); } NIMBLE_LOGI(LOG_TAG, "New subscribe value for conn: %d val: %d", @@ -358,18 +350,13 @@ void NimBLECharacteristic::notify(bool is_notification) { for (auto &it : p2902->m_subscribedVec) { uint16_t _mtu = getService()->getServer()->getPeerMTU(it.conn_id); - os_mbuf *om; - if(_mtu == 0) { + // check if connected and subscribed + if(_mtu == 0 || it.sub_val == 0) { //NIMBLE_LOGD(LOG_TAG, "peer not connected"); continue; } - if(it.sub_val == 0) { - //NIMBLE_LOGD(LOG_TAG, "Skipping unsubscribed client"); - continue; - } - if (length > _mtu - 3) { NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu - 3); } @@ -389,20 +376,25 @@ void NimBLECharacteristic::notify(bool is_notification) { // don't create the m_buf until we are sure to send the data or else // we could be allocating a buffer that doesn't get released. // We also must create it in each loop iteration because it is consumed with each host call. - om = ble_hs_mbuf_from_flat((uint8_t*)value.data(), length); + os_mbuf *om = ble_hs_mbuf_from_flat((uint8_t*)value.data(), length); NimBLECharacteristicCallbacks::Status statusRC; - if(m_pIndSemaphore != nullptr && !is_notification) { - m_pIndSemaphore->take("indicate"); + + if(!is_notification && (m_properties & NIMBLE_PROPERTY::INDICATE)) { + ble_task_data_t taskData = {nullptr, xTaskGetCurrentTaskHandle(),0, nullptr}; + m_pTaskData = &taskData; + rc = ble_gattc_indicate_custom(it.conn_id, m_handle, om); if(rc != 0){ - m_pIndSemaphore->give(); statusRC = NimBLECharacteristicCallbacks::Status::ERROR_GATT; } else { - rc = m_pIndSemaphore->wait(); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + rc = m_pTaskData->rc; } - if(rc == 0 || rc == BLE_HS_EDONE) { + m_pTaskData = nullptr; + + if(rc == BLE_HS_EDONE) { rc = 0; statusRC = NimBLECharacteristicCallbacks::Status::SUCCESS_INDICATE; } else if(rc == BLE_HS_ETIMEOUT) { diff --git a/src/NimBLECharacteristic.h b/src/NimBLECharacteristic.h index 1a15c5c..1624f9a 100644 --- a/src/NimBLECharacteristic.h +++ b/src/NimBLECharacteristic.h @@ -42,7 +42,6 @@ typedef enum { #include "NimBLEService.h" #include "NimBLEDescriptor.h" -#include "FreeRTOS.h" #include #include @@ -120,7 +119,7 @@ private: NimBLEService* m_pService; std::string m_value; std::vector m_dscVec; - FreeRTOS::Semaphore *m_pIndSemaphore; + ble_task_data_t *m_pTaskData; portMUX_TYPE m_valMux; }; // NimBLECharacteristic diff --git a/src/NimBLEDescriptor.h b/src/NimBLEDescriptor.h index 870fc65..34f1a11 100644 --- a/src/NimBLEDescriptor.h +++ b/src/NimBLEDescriptor.h @@ -22,7 +22,6 @@ #include "NimBLECharacteristic.h" #include "NimBLEUUID.h" -#include "FreeRTOS.h" #include diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index f914bc5..d1d1fa9 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -19,7 +19,6 @@ #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #include "NimBLEServer.h" -#include "NimBLEUtils.h" #include "NimBLEDevice.h" #include "NimBLELog.h" @@ -296,8 +295,9 @@ size_t NimBLEServer::getConnectedCount() { if(event->notify_tx.indication && event->notify_tx.status != 0) { for(auto &it : server->m_notifyChrVec) { if(it->getHandle() == event->notify_tx.attr_handle) { - if(it->m_pIndSemaphore != nullptr) { - it->m_pIndSemaphore->give(event->notify_tx.status); + if(it->m_pTaskData != nullptr) { + it->m_pTaskData->rc = event->notify_tx.status; + xTaskNotifyGive(it->m_pTaskData->task); } break; } diff --git a/src/NimBLEServer.h b/src/NimBLEServer.h index 1f95c4a..cfaa8ac 100644 --- a/src/NimBLEServer.h +++ b/src/NimBLEServer.h @@ -20,11 +20,12 @@ #include "nimconfig.h" #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) +#include "NimBLEUtils.h" #include "NimBLEAddress.h" #include "NimBLEAdvertising.h" #include "NimBLEService.h" #include "NimBLESecurity.h" -#include "FreeRTOS.h" + class NimBLEService; class NimBLECharacteristic; diff --git a/src/NimBLEService.h b/src/NimBLEService.h index 1b496f4..4c1fa2a 100644 --- a/src/NimBLEService.h +++ b/src/NimBLEService.h @@ -20,10 +20,9 @@ #include "nimconfig.h" #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) -#include "NimBLECharacteristic.h" #include "NimBLEServer.h" +#include "NimBLECharacteristic.h" #include "NimBLEUUID.h" -#include "FreeRTOS.h" class NimBLEServer; diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index b26d41a..9e537cc 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -13,6 +13,15 @@ #include "host/ble_gap.h" +#include + +typedef struct { + void *pATT; + TaskHandle_t task; + int rc; + std::string *buf; +} ble_task_data_t; + extern "C"{ char *addr_str(const void *addr); void print_conn_desc(const struct ble_gap_conn_desc *desc);