From a198498483d1d0b2d80d552d317ef5bb7e9a0260 Mon Sep 17 00:00:00 2001 From: Ryan Powell Date: Wed, 11 Dec 2024 11:10:11 -0700 Subject: [PATCH] wip --- pkg.yml | 20 ++++++++++++++++++++ src/NimBLEAdvertisementData.cpp | 4 ++-- src/NimBLEClient.cpp | 2 +- src/NimBLEDevice.cpp | 32 +++++++++++++++++++++++++++++--- src/NimBLELog.h | 8 +++++--- src/NimBLEScan.cpp | 1 + src/NimBLEUtils.cpp | 1 - src/nimconfig.h | 24 +++++++++++++++++++++++- 8 files changed, 81 insertions(+), 11 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/NimBLEAdvertisementData.cpp b/src/NimBLEAdvertisementData.cpp index 0cfb077..3f79c64 100644 --- a/src/NimBLEAdvertisementData.cpp +++ b/src/NimBLEAdvertisementData.cpp @@ -214,7 +214,7 @@ bool NimBLEAdvertisementData::removeServiceUUID(const NimBLEUUID& serviceUUID) { } int uuidLoc = -1; - for (int i = dataLoc + 2; i < m_payload.size(); i += bytes) { + for (size_t i = dataLoc + 2; i < m_payload.size(); i += bytes) { if (memcmp(&m_payload[i], serviceUUID.getValue(), bytes) == 0) { uuidLoc = i; break; @@ -510,7 +510,7 @@ bool NimBLEAdvertisementData::setServiceData(const NimBLEUUID& uuid, const std:: * @return -1 if the data is not found, otherwise the index of the data in the payload. */ int NimBLEAdvertisementData::getDataLocation(uint8_t type) const { - int index = 0; + size_t index = 0; while (index < m_payload.size()) { if (m_payload[index + 1] == type) { return index; diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index f97bd8d..19186b0 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -21,7 +21,7 @@ # include "NimBLELog.h" # 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 diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 6f99ec3..0669dac 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -39,7 +39,12 @@ # include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" # endif # else -# include "nimble/nimble/controller/include/controller/ble_phy.h" +//# 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" # endif # ifndef CONFIG_NIMBLE_CPP_IDF @@ -512,7 +517,7 @@ int NimBLEDevice::getPower() { return 0; # endif # else - return ble_phy_txpwr_get(); + return ble_phy_tx_power_get(); //ble_phy_txpwr_get(); # endif } // getPower @@ -809,10 +814,19 @@ void NimBLEDevice::onSync(void) { */ void NimBLEDevice::host_task(void* param) { NIMBLE_LOGI(LOG_TAG, "BLE Host Task Started"); +#ifndef MYNEWT nimble_port_run(); // This function will return only when nimble_port_stop() is executed 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. @@ -875,8 +889,10 @@ bool NimBLEDevice::init(const std::string& deviceName) { } # endif # endif - nimble_port_init(); +#ifndef MYNEWT + nimble_port_init(); +#endif // Setup callbacks for host events ble_hs_cfg.reset_cb = NimBLEDevice::onReset; ble_hs_cfg.sync_cb = NimBLEDevice::onSync; @@ -891,8 +907,13 @@ bool NimBLEDevice::init(const std::string& deviceName) { ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/ setDeviceName(deviceName); +#ifndef MYNEWT ble_store_config_init(); 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 @@ -914,6 +935,7 @@ bool NimBLEDevice::init(const std::string& deviceName) { bool NimBLEDevice::deinit(bool clearAll) { int rc = 0; if (m_initialized) { +#ifndef MYNEWT rc = nimble_port_stop(); if (rc == 0) { nimble_port_deinit(); @@ -925,6 +947,10 @@ bool NimBLEDevice::deinit(bool clearAll) { } # endif # endif +#else + rc = ble_hs_shutdown(0); + if (rc == 0) { +#endif m_initialized = false; m_synced = false; } diff --git a/src/NimBLELog.h b/src/NimBLELog.h index 0ccdf41..38d44f2 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" # include "console/console.h" # ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL @@ -37,8 +37,10 @@ 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 "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/NimBLEScan.cpp b/src/NimBLEScan.cpp index 31ea9c7..75c678c 100644 --- a/src/NimBLEScan.cpp +++ b/src/NimBLEScan.cpp @@ -476,6 +476,7 @@ void NimBLEScan::clearResults() { */ void NimBLEScanResults::dump() const { for (const auto& dev : m_deviceVec) { + (void)dev; // suppress unused variable warning when log level is less than info NIMBLE_LOGI(LOG_TAG, "- %s", dev->toString().c_str()); } } // dump diff --git a/src/NimBLEUtils.cpp b/src/NimBLEUtils.cpp index b8e09c2..f83d75d 100644 --- a/src/NimBLEUtils.cpp +++ b/src/NimBLEUtils.cpp @@ -66,7 +66,6 @@ NimBLETaskData::NimBLETaskData(void* pInstance, int flags, void* buf) NimBLETaskData::~NimBLETaskData() { # if !defined INC_FREERTOS_H if (m_pHandle != nullptr) { - ble_npl_sem_deinit(static_cast(m_pHandle)); delete static_cast(m_pHandle); } # endif diff --git a/src/nimconfig.h b/src/nimconfig.h index d96e37c..04f2ace 100644 --- a/src/nimconfig.h +++ b/src/nimconfig.h @@ -6,7 +6,7 @@ */ #pragma once - +#ifdef ESP_PLATFORM #include "sdkconfig.h" #include "nimconfig_rename.h" @@ -150,3 +150,25 @@ void nimble_cpp_assert(const char *file, unsigned line) __attribute((weak, noret #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 +#if CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED && !defined NDEBUG +void nimble_cpp_assert(const char *file, unsigned line) __attribute((weak, noreturn)); +# define NIMBLE_ATT_VAL_FILE (__builtin_strrchr(__FILE__, '/') ? \ + __builtin_strrchr (__FILE__, '/') + 1 : __FILE__) +# define NIMBLE_CPP_DEBUG_ASSERT(cond) \ + if (!(cond)) { \ + nimble_cpp_assert(NIMBLE_ATT_VAL_FILE, __LINE__); \ + } +#else +# define NIMBLE_CPP_DEBUG_ASSERT(cond) (void(0)) +#endif +#endif // ESP_PLATFORM \ No newline at end of file