This commit is contained in:
Ryan Powell 2024-12-11 11:10:11 -07:00
parent 152cc9d1b9
commit a198498483
8 changed files with 81 additions and 11 deletions

20
pkg.yml Normal file
View file

@ -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

View file

@ -214,7 +214,7 @@ bool NimBLEAdvertisementData::removeServiceUUID(const NimBLEUUID& serviceUUID) {
} }
int uuidLoc = -1; 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) { if (memcmp(&m_payload[i], serviceUUID.getValue(), bytes) == 0) {
uuidLoc = i; uuidLoc = i;
break; 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. * @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 NimBLEAdvertisementData::getDataLocation(uint8_t type) const {
int index = 0; size_t index = 0;
while (index < m_payload.size()) { while (index < m_payload.size()) {
if (m_payload[index + 1] == type) { if (m_payload[index + 1] == type) {
return index; return index;

View file

@ -21,7 +21,7 @@
# include "NimBLELog.h" # include "NimBLELog.h"
# if defined(CONFIG_NIMBLE_CPP_IDF) # if defined(CONFIG_NIMBLE_CPP_IDF)
# include "nimble/nimble_port.h" //# include "nimble/nimble_port.h"
# else # else
# include "nimble/porting/nimble/include/nimble/nimble_port.h" # include "nimble/porting/nimble/include/nimble/nimble_port.h"
# endif # endif

View file

@ -39,7 +39,12 @@
# include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" # include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h"
# endif # endif
# else # 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 # endif
# ifndef CONFIG_NIMBLE_CPP_IDF # ifndef CONFIG_NIMBLE_CPP_IDF
@ -512,7 +517,7 @@ int NimBLEDevice::getPower() {
return 0; return 0;
# endif # endif
# else # else
return ble_phy_txpwr_get(); return ble_phy_tx_power_get(); //ble_phy_txpwr_get();
# endif # endif
} // getPower } // getPower
@ -809,10 +814,19 @@ void NimBLEDevice::onSync(void) {
*/ */
void NimBLEDevice::host_task(void* param) { void NimBLEDevice::host_task(void* param) {
NIMBLE_LOGI(LOG_TAG, "BLE Host Task Started"); 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_run(); // This function will return only when nimble_port_stop() is executed
nimble_port_freertos_deinit(); nimble_port_freertos_deinit();
#else
while (1) {
os_eventq_run(os_eventq_dflt_get());
}
#endif
} // host_task } // host_task
static struct os_task ble_nimble_task;
static os_stack_t ble_nimble_stack[1024];
/** /**
* @brief Initialize the BLE environment. * @brief Initialize the BLE environment.
* @param [in] deviceName The device name of the device. * @param [in] deviceName The device name of the device.
@ -875,8 +889,10 @@ bool NimBLEDevice::init(const std::string& deviceName) {
} }
# endif # endif
# endif # endif
nimble_port_init();
#ifndef MYNEWT
nimble_port_init();
#endif
// Setup callbacks for host events // Setup callbacks for host events
ble_hs_cfg.reset_cb = NimBLEDevice::onReset; ble_hs_cfg.reset_cb = NimBLEDevice::onReset;
ble_hs_cfg.sync_cb = NimBLEDevice::onSync; 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*/ ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/
setDeviceName(deviceName); setDeviceName(deviceName);
#ifndef MYNEWT
ble_store_config_init(); ble_store_config_init();
nimble_port_freertos_init(NimBLEDevice::host_task); 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 // 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) { bool NimBLEDevice::deinit(bool clearAll) {
int rc = 0; int rc = 0;
if (m_initialized) { if (m_initialized) {
#ifndef MYNEWT
rc = nimble_port_stop(); rc = nimble_port_stop();
if (rc == 0) { if (rc == 0) {
nimble_port_deinit(); nimble_port_deinit();
@ -924,6 +946,10 @@ bool NimBLEDevice::deinit(bool clearAll) {
NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", rc); NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", rc);
} }
# endif # endif
# endif
#else
rc = ble_hs_shutdown(0);
if (rc == 0) {
#endif #endif
m_initialized = false; m_initialized = false;
m_synced = false; m_synced = false;

View file

@ -12,7 +12,7 @@
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF) // using esp-idf #if (0) // using esp-idf
# include "esp_log.h" # include "esp_log.h"
# include "console/console.h" # include "console/console.h"
# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL # ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL
@ -37,8 +37,10 @@
NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__) NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__)
#else // using Arduino #else // using Arduino
# include "nimble/porting/nimble/include/syscfg/syscfg.h" //# include "nimble/porting/nimble/include/syscfg/syscfg.h"
# include "nimble/console/console.h" //# include "nimble/console/console.h"
# include "syscfg/syscfg.h"
# include "console/console.h"
# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL # ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL
# if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL) # if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL)
# define CONFIG_NIMBLE_CPP_LOG_LEVEL CORE_DEBUG_LEVEL # define CONFIG_NIMBLE_CPP_LOG_LEVEL CORE_DEBUG_LEVEL

View file

@ -476,6 +476,7 @@ void NimBLEScan::clearResults() {
*/ */
void NimBLEScanResults::dump() const { void NimBLEScanResults::dump() const {
for (const auto& dev : m_deviceVec) { 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()); NIMBLE_LOGI(LOG_TAG, "- %s", dev->toString().c_str());
} }
} // dump } // dump

View file

@ -66,7 +66,6 @@ NimBLETaskData::NimBLETaskData(void* pInstance, int flags, void* buf)
NimBLETaskData::~NimBLETaskData() { NimBLETaskData::~NimBLETaskData() {
# if !defined INC_FREERTOS_H # if !defined INC_FREERTOS_H
if (m_pHandle != nullptr) { if (m_pHandle != nullptr) {
ble_npl_sem_deinit(static_cast<ble_npl_sem*>(m_pHandle));
delete static_cast<ble_npl_sem*>(m_pHandle); delete static_cast<ble_npl_sem*>(m_pHandle);
} }
# endif # endif

View file

@ -6,7 +6,7 @@
*/ */
#pragma once #pragma once
#ifdef ESP_PLATFORM
#include "sdkconfig.h" #include "sdkconfig.h"
#include "nimconfig_rename.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 #define CONFIG_BT_NIMBLE_TASK_STACK_SIZE 4096
#endif // _DOXYGEN_ #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