From 8620092c907c08d3137fa6309b9b09f95740c0d8 Mon Sep 17 00:00:00 2001 From: h2zero Date: Wed, 29 Dec 2021 08:08:25 -0700 Subject: [PATCH] Cleanup and add support for non-esp devices. --- src/NimBLEAdvertisedDevice.cpp | 8 +- src/NimBLEAdvertisedDevice.h | 1 + src/NimBLECharacteristic.cpp | 6 +- src/NimBLEDescriptor.cpp | 6 + src/NimBLEDevice.cpp | 201 ++++++++++++++++++++++----------- src/NimBLEDevice.h | 18 ++- src/NimBLEEddystoneTLM.cpp | 12 +- src/NimBLERemoteDescriptor.cpp | 1 + src/NimBLEUtils.cpp | 9 +- 9 files changed, 175 insertions(+), 87 deletions(-) diff --git a/src/NimBLEAdvertisedDevice.cpp b/src/NimBLEAdvertisedDevice.cpp index ecfd498..01dd75d 100644 --- a/src/NimBLEAdvertisedDevice.cpp +++ b/src/NimBLEAdvertisedDevice.cpp @@ -11,11 +11,9 @@ * Created on: Jul 3, 2017 * Author: kolban */ -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) #include "nimconfig.h" -#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) +#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #include "NimBLEDevice.h" #include "NimBLEAdvertisedDevice.h" @@ -783,7 +781,5 @@ size_t NimBLEAdvertisedDevice::getPayloadLength() { return m_payload.size(); } // getPayloadLength - -#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) -#endif /* CONFIG_BT_ENABLED */ +#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ diff --git a/src/NimBLEAdvertisedDevice.h b/src/NimBLEAdvertisedDevice.h index b00aae9..39410e6 100644 --- a/src/NimBLEAdvertisedDevice.h +++ b/src/NimBLEAdvertisedDevice.h @@ -29,6 +29,7 @@ #include #include +#include class NimBLEScan; diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 9a27301..c5e1ba9 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -439,7 +439,7 @@ void NimBLECharacteristic::notify(std::string value, bool is_notification) { int rc = 0; for (auto &it : m_subscribedVec) { - uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first); + uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first) - 3; // check if connected and subscribed if(_mtu == 0 || it.second == 0) { @@ -455,8 +455,8 @@ void NimBLECharacteristic::notify(std::string value, bool is_notification) { } } - if (length > _mtu - 3) { - NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu - 3); + if (length > _mtu) { + NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu); } if(is_notification && (!(it.second & NIMBLE_SUB_NOTIFY))) { diff --git a/src/NimBLEDescriptor.cpp b/src/NimBLEDescriptor.cpp index c57c80d..14efbab 100644 --- a/src/NimBLEDescriptor.cpp +++ b/src/NimBLEDescriptor.cpp @@ -143,6 +143,9 @@ NimBLECharacteristic* NimBLEDescriptor::getCharacteristic() { int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + (void)conn_handle; + (void)attr_handle; + const ble_uuid_t *uuid; int rc; NimBLEDescriptor* pDescriptor = (NimBLEDescriptor*)arg; @@ -236,6 +239,7 @@ void NimBLEDescriptor::setValue(const uint8_t* data, size_t length) { m_value.attr_len = length; memcpy(m_value.attr_value, data, length); portEXIT_CRITICAL(&m_valMux); + } // setValue @@ -275,6 +279,7 @@ NimBLEDescriptorCallbacks::~NimBLEDescriptorCallbacks() {} * @param [in] pDescriptor The descriptor that is the source of the event. */ void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor) { + (void)pDescriptor; NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onRead: default"); } // onRead @@ -284,6 +289,7 @@ void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor) { * @param [in] pDescriptor The descriptor that is the source of the event. */ void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor) { + (void)pDescriptor; NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default"); } // onWrite diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 3e3378d..0302cb8 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -18,32 +18,38 @@ #include "NimBLEDevice.h" #include "NimBLEUtils.h" -#include "esp_err.h" -#include "esp_bt.h" -#include "nvs_flash.h" - -#if defined(CONFIG_NIMBLE_CPP_IDF) -#include "esp_nimble_hci.h" -#include "nimble/nimble_port.h" -#include "nimble/nimble_port_freertos.h" -#include "host/ble_hs.h" -#include "host/ble_hs_pvcy.h" -#include "host/util/util.h" -#include "services/gap/ble_svc_gap.h" -#include "services/gatt/ble_svc_gatt.h" +#ifdef ESP_PLATFORM +# include "esp_err.h" +# include "esp_bt.h" +# include "nvs_flash.h" +# if defined(CONFIG_NIMBLE_CPP_IDF) +# include "esp_nimble_hci.h" +# include "nimble/nimble_port.h" +# include "nimble/nimble_port_freertos.h" +# include "host/ble_hs.h" +# include "host/ble_hs_pvcy.h" +# include "host/util/util.h" +# include "services/gap/ble_svc_gap.h" +# include "services/gatt/ble_svc_gatt.h" +# else +# include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" +# endif #else -#include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" -#include "nimble/porting/nimble/include/nimble/nimble_port.h" -#include "nimble/porting/npl/freertos/include/nimble/nimble_port_freertos.h" -#include "nimble/nimble/host/include/host/ble_hs.h" -#include "nimble/nimble/host/include/host/ble_hs_pvcy.h" -#include "nimble/nimble/host/util/include/host/util/util.h" -#include "nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h" -#include "nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h" +# include "nimble/nimble/controller/include/controller/ble_phy.h" #endif -#ifdef CONFIG_ENABLE_ARDUINO_DEPENDS -#include "esp32-hal-bt.h" +#ifndef CONFIG_NIMBLE_CPP_IDF +# include "nimble/porting/nimble/include/nimble/nimble_port.h" +# include "nimble/porting/npl/freertos/include/nimble/nimble_port_freertos.h" +# include "nimble/nimble/host/include/host/ble_hs.h" +# include "nimble/nimble/host/include/host/ble_hs_pvcy.h" +# include "nimble/nimble/host/util/include/host/util/util.h" +# include "nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h" +# include "nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h" +#endif + +#if defined(ESP_PLATFORM) && defined(CONFIG_ENABLE_ARDUINO_DEPENDS) +# include "esp32-hal-bt.h" #endif #include "NimBLELog.h" @@ -75,9 +81,10 @@ std::list NimBLEDevice::m_ignoreList; std::vector NimBLEDevice::m_whiteList; NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr; uint8_t NimBLEDevice::m_own_addr_type = BLE_OWN_ADDR_PUBLIC; +#ifdef ESP_PLATFORM uint16_t NimBLEDevice::m_scanDuplicateSize = CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE; uint8_t NimBLEDevice::m_scanFilterMode = CONFIG_BTDM_SCAN_DUPL_TYPE; - +#endif /** * @brief Create a new instance of a server. @@ -142,7 +149,8 @@ void NimBLEDevice::stopAdvertising() { * try and release/delete it. */ #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) -/* STATIC */ NimBLEScan* NimBLEDevice::getScan() { +/* STATIC */ +NimBLEScan* NimBLEDevice::getScan() { if (m_pScan == nullptr) { m_pScan = new NimBLEScan(); } @@ -159,7 +167,8 @@ void NimBLEDevice::stopAdvertising() { * @return A reference to the new client object. */ #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) -/* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) { +/* STATIC */ +NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) { if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { NIMBLE_LOGW(LOG_TAG,"Number of clients exceeds Max connections. Cur=%d Max=%d", m_cList.size(), NIMBLE_MAX_CONNECTIONS); @@ -177,7 +186,8 @@ void NimBLEDevice::stopAdvertising() { * Checks if it is connected or trying to connect and disconnects/stops it first. * @param [in] pClient A pointer to the client object. */ -/* STATIC */ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { +/* STATIC */ +bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { if(pClient == nullptr) { return false; } @@ -221,7 +231,8 @@ void NimBLEDevice::stopAdvertising() { * @brief Get the list of created client objects. * @return A pointer to the list of clients. */ -/* STATIC */std::list* NimBLEDevice::getClientList() { +/* STATIC */ +std::list* NimBLEDevice::getClientList() { return &m_cList; } // getClientList @@ -230,7 +241,8 @@ void NimBLEDevice::stopAdvertising() { * @brief Get the number of created client objects. * @return Number of client objects created. */ -/* STATIC */size_t NimBLEDevice::getClientListSize() { +/* STATIC */ +size_t NimBLEDevice::getClientListSize() { return m_cList.size(); } // getClientList @@ -240,7 +252,8 @@ void NimBLEDevice::stopAdvertising() { * @param [in] conn_id The client connection ID to search for. * @return A pointer to the client object with the spcified connection ID. */ -/* STATIC */NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) { +/* STATIC */ +NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { if((*it)->getConnId() == conn_id) { return (*it); @@ -256,7 +269,8 @@ void NimBLEDevice::stopAdvertising() { * @param [in] peer_addr The address of the peer to search for. * @return A pointer to the client object with the peer address. */ -/* STATIC */NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) { +/* STATIC */ +NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { if((*it)->getPeerAddress().equals(peer_addr)) { return (*it); @@ -270,7 +284,8 @@ void NimBLEDevice::stopAdvertising() { * @brief Finds the first disconnected client in the list. * @return A pointer to the first client object that is not connected to a peer. */ -/* STATIC */NimBLEClient* NimBLEDevice::getDisconnectedClient() { +/* STATIC */ +NimBLEClient* NimBLEDevice::getDisconnectedClient() { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { if(!(*it)->isConnected()) { return (*it); @@ -281,7 +296,7 @@ void NimBLEDevice::stopAdvertising() { #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) - +#ifdef ESP_PLATFORM /** * @brief Set the transmission power. * @param [in] powerLevel The power level to set, can be one of: @@ -307,12 +322,15 @@ void NimBLEDevice::stopAdvertising() { * * ESP_BLE_PWR_TYPE_SCAN = 10, For scan * * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value */ -/* STATIC */ void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType) { +/* STATIC */ +void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType) { NIMBLE_LOGD(LOG_TAG, ">> setPower: %d (type: %d)", powerLevel, powerType); + esp_err_t errRc = esp_ble_tx_power_set(powerType, powerLevel); if (errRc != ESP_OK) { NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_set: rc=%d", errRc); } + NIMBLE_LOGD(LOG_TAG, "<< setPower"); } // setPower @@ -334,9 +352,8 @@ void NimBLEDevice::stopAdvertising() { * * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value * @return the power level currently used by the type specified. */ - -/* STATIC */ int NimBLEDevice::getPower(esp_ble_power_type_t powerType) { - +/* STATIC */ +int NimBLEDevice::getPower(esp_ble_power_type_t powerType) { switch(esp_ble_tx_power_get(powerType)) { case ESP_PWR_LVL_N12: return -12; @@ -359,13 +376,25 @@ void NimBLEDevice::stopAdvertising() { } } // getPower +#else + +void NimBLEDevice::setPower(int dbm) { + ble_phy_txpwr_set(dbm); +} + + +int NimBLEDevice::getPower() { + return ble_phy_txpwr_get(); +} +#endif /** * @brief Get our device address. * @return A NimBLEAddress object of our public address if we have one, * if not then our current random address. */ -/* STATIC*/ NimBLEAddress NimBLEDevice::getAddress() { +/* STATIC*/ +NimBLEAddress NimBLEDevice::getAddress() { ble_addr_t addr = {BLE_ADDR_PUBLIC, 0}; if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) { @@ -382,7 +411,8 @@ void NimBLEDevice::stopAdvertising() { * @brief Return a string representation of the address of this device. * @return A string representation of this device address. */ -/* STATIC */ std::string NimBLEDevice::toString() { +/* STATIC */ +std::string NimBLEDevice::toString() { return getAddress().toString(); } // toString @@ -392,7 +422,8 @@ void NimBLEDevice::stopAdvertising() { * @param [in] mtu Value to set local mtu: * * This should be larger than 23 and lower or equal to BLE_ATT_MTU_MAX = 527. */ -/* STATIC */int NimBLEDevice::setMTU(uint16_t mtu) { +/* STATIC */ +int NimBLEDevice::setMTU(uint16_t mtu) { NIMBLE_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu); int rc = ble_att_set_preferred_mtu(mtu); @@ -410,11 +441,13 @@ void NimBLEDevice::stopAdvertising() { * @brief Get local MTU value set. * @return The current preferred MTU setting. */ -/* STATIC */uint16_t NimBLEDevice::getMTU() { +/* STATIC */ +uint16_t NimBLEDevice::getMTU() { return ble_att_preferred_mtu(); } +#ifdef ESP_PLATFORM /** * @brief Set the duplicate filter cache size for filtering scanned devices. * @param [in] cacheSize The number of advertisements filtered before the cache is reset.\n @@ -460,6 +493,7 @@ void NimBLEDevice::setScanFilterMode(uint8_t mode) { m_scanFilterMode = mode; } +#endif #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) /** @@ -482,7 +516,7 @@ int NimBLEDevice::getNumBonds() { /** * @brief Deletes all bonding information. */ - /*STATIC*/ +/*STATIC*/ void NimBLEDevice::deleteAllBonds() { ble_store_clear(); } @@ -562,6 +596,7 @@ NimBLEAddress NimBLEDevice::getBondedAddress(int index) { * @param [in] address The address to check for in the whitelist. * @returns true if the address is in the whitelist. */ +/*STATIC*/ bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) { for (auto &it : m_whiteList) { if (it == address) { @@ -578,6 +613,7 @@ bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) { * @param [in] address The address to add to the whitelist. * @returns true if successful. */ +/*STATIC*/ bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) { if (NimBLEDevice::onWhiteList(address)) { return true; @@ -609,6 +645,7 @@ bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) { * @param [in] address The address to remove from the whitelist. * @returns true if successful. */ +/*STATIC*/ bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) { if (!NimBLEDevice::onWhiteList(address)) { return true; @@ -648,6 +685,7 @@ bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) { * @brief Gets the count of addresses in the whitelist. * @returns The number of addresses in the whitelist. */ +/*STATIC*/ size_t NimBLEDevice::getWhiteListCount() { return m_whiteList.size(); } @@ -658,6 +696,7 @@ size_t NimBLEDevice::getWhiteListCount() { * @param [in] index The vector index to retrieve the address from. * @returns the NimBLEAddress at the whitelist index or nullptr if not found. */ +/*STATIC*/ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { if (index > m_whiteList.size()) { NIMBLE_LOGE(LOG_TAG, "Invalid index; %u", index); @@ -671,7 +710,8 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { * @brief Host reset, we pass the message so we don't make calls until resynced. * @param [in] reason The reason code for the reset. */ -/* STATIC */ void NimBLEDevice::onReset(int reason) +/* STATIC */ +void NimBLEDevice::onReset(int reason) { if(!m_synced) { return; @@ -695,7 +735,8 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { /** * @brief Host resynced with controller, all clear to make calls to the stack. */ -/* STATIC */ void NimBLEDevice::onSync(void) +/* STATIC */ +void NimBLEDevice::onSync(void) { NIMBLE_LOGI(LOG_TAG, "NimBle host synced."); // This check is needed due to potentially being called multiple times in succession @@ -708,6 +749,14 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { int rc = ble_hs_util_ensure_addr(0); assert(rc == 0); +#ifndef ESP_PLATFORM + rc = ble_hs_id_infer_auto(m_own_addr_type, &m_own_addr_type); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "error determining address type; rc=%d", rc); + return; + } +#endif + // Yield for houskeeping before returning to operations. // Occasionally triggers exception without. taskYIELD(); @@ -733,9 +782,11 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { /** * @brief The main host task. */ -/* STATIC */ void NimBLEDevice::host_task(void *param) +/* STATIC */ +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 */ nimble_port_run(); @@ -747,9 +798,11 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { * @brief Initialize the %BLE environment. * @param [in] deviceName The device name of the device. */ -/* STATIC */ void NimBLEDevice::init(const std::string &deviceName) { +/* STATIC */ +void NimBLEDevice::init(const std::string &deviceName) { if(!initialized){ int rc=0; +#ifdef ESP_PLATFORM esp_err_t errRc = ESP_OK; #ifdef CONFIG_ENABLE_ARDUINO_DEPENDS @@ -781,6 +834,7 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg)); ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE)); ESP_ERROR_CHECK(esp_nimble_hci_init()); +#endif nimble_port_init(); // Setup callbacks for host events @@ -805,9 +859,10 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { 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); + taskYIELD(); } initialized = true; // Set the initialization flag to ensure we are only initialized once. @@ -819,16 +874,17 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { * @param [in] clearAll If true, deletes all server/advertising/scan/client objects after deinitializing. * @note If clearAll is true when called, any references to the created objects become invalid. */ -/* STATIC */ void NimBLEDevice::deinit(bool clearAll) { +/* STATIC */ +void NimBLEDevice::deinit(bool clearAll) { int ret = nimble_port_stop(); if (ret == 0) { nimble_port_deinit(); - +#ifdef ESP_PLATFORM ret = esp_nimble_hci_and_controller_deinit(); if (ret != ESP_OK) { NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", ret); } - +#endif initialized = false; m_synced = false; @@ -875,6 +931,7 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { * @brief Check if the initialization is complete. * @return true if initialized. */ +/*STATIC*/ bool NimBLEDevice::getInitialized() { return initialized; } // getInitialized @@ -886,7 +943,8 @@ bool NimBLEDevice::getInitialized() { * @param mitm If true we are capable of man in the middle protection, false if not. * @param sc If true we will perform secure connection pairing, false we will use legacy pairing. */ -/*STATIC*/ void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) { +/*STATIC*/ +void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) { NIMBLE_LOGD(LOG_TAG, "Setting bonding: %d, mitm: %d, sc: %d",bonding,mitm,sc); ble_hs_cfg.sm_bonding = bonding; ble_hs_cfg.sm_mitm = mitm; @@ -903,7 +961,8 @@ bool NimBLEDevice::getInitialized() { * * 0x08 BLE_SM_PAIR_AUTHREQ_SC * * 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported. */ -/*STATIC*/void NimBLEDevice::setSecurityAuth(uint8_t auth_req) { +/*STATIC*/ +void NimBLEDevice::setSecurityAuth(uint8_t auth_req) { NimBLEDevice::setSecurityAuth((auth_req & BLE_SM_PAIR_AUTHREQ_BOND)>0, (auth_req & BLE_SM_PAIR_AUTHREQ_MITM)>0, (auth_req & BLE_SM_PAIR_AUTHREQ_SC)>0); @@ -919,7 +978,8 @@ bool NimBLEDevice::getInitialized() { * * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability * * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability */ -/*STATIC*/ void NimBLEDevice::setSecurityIOCap(uint8_t iocap) { +/*STATIC*/ +void NimBLEDevice::setSecurityIOCap(uint8_t iocap) { ble_hs_cfg.sm_io_cap = iocap; } // setSecurityIOCap @@ -933,7 +993,8 @@ bool NimBLEDevice::getInitialized() { * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK */ -/*STATIC*/void NimBLEDevice::setSecurityInitKey(uint8_t init_key) { +/*STATIC*/ +void NimBLEDevice::setSecurityInitKey(uint8_t init_key) { ble_hs_cfg.sm_our_key_dist = init_key; } // setsSecurityInitKey @@ -947,7 +1008,8 @@ bool NimBLEDevice::getInitialized() { * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK */ -/*STATIC*/void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) { +/*STATIC*/ +void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) { ble_hs_cfg.sm_their_key_dist = resp_key; } // setsSecurityRespKey @@ -956,7 +1018,8 @@ bool NimBLEDevice::getInitialized() { * @brief Set the passkey the server will ask for when pairing. * @param [in] pin The passkey to use. */ -/*STATIC*/void NimBLEDevice::setSecurityPasskey(uint32_t pin) { +/*STATIC*/ +void NimBLEDevice::setSecurityPasskey(uint32_t pin) { m_passkey = pin; } // setSecurityPasskey @@ -965,7 +1028,8 @@ bool NimBLEDevice::getInitialized() { * @brief Get the current passkey used for pairing. * @return The current passkey. */ -/*STATIC*/uint32_t NimBLEDevice::getSecurityPasskey() { +/*STATIC*/ +uint32_t NimBLEDevice::getSecurityPasskey() { return m_passkey; } // getSecurityPasskey @@ -975,11 +1039,13 @@ bool NimBLEDevice::getInitialized() { * @param [in] callbacks Pointer to NimBLESecurityCallbacks class * @deprecated For backward compatibility, New code should use client/server callback methods. */ +/*STATIC*/ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { NimBLEDevice::m_securityCallbacks = callbacks; } // setSecurityCallbacks +#ifdef ESP_PLATFORM /** * @brief Set the own address type. * @param [in] own_addr_type Own Bluetooth Device address type.\n @@ -990,6 +1056,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { * * 0x03: BLE_OWN_ADDR_RPA_RANDOM_DEFAULT * @param [in] useNRPA If true, and address type is random, uses a non-resolvable random address. */ +/*STATIC*/ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { m_own_addr_type = own_addr_type; switch (own_addr_type) { @@ -1013,18 +1080,15 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { break; } } // setOwnAddrType - +#endif /** * @brief Start the connection securing and authorization for this connection. * @param conn_id The connection id of the peer device. * @returns NimBLE stack return code, 0 = success. */ -/* STATIC */int NimBLEDevice::startSecurity(uint16_t conn_id) { - /* if(m_securityCallbacks != nullptr) { - m_securityCallbacks->onSecurityRequest(); - } - */ +/* STATIC */ +int NimBLEDevice::startSecurity(uint16_t conn_id) { int rc = ble_gap_security_initiate(conn_id); if(rc != 0){ NIMBLE_LOGE(LOG_TAG, "ble_gap_security_initiate: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); @@ -1039,7 +1103,8 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { * @param [in] address The address to look for. * @return True if ignoring. */ -/*STATIC*/ bool NimBLEDevice::isIgnored(const NimBLEAddress &address) { +/*STATIC*/ +bool NimBLEDevice::isIgnored(const NimBLEAddress &address) { for(auto &it : m_ignoreList) { if(it.equals(address)){ return true; @@ -1054,7 +1119,8 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { * @brief Add a device to the ignore list. * @param [in] address The address of the device we want to ignore. */ -/*STATIC*/ void NimBLEDevice::addIgnored(const NimBLEAddress &address) { +/*STATIC*/ +void NimBLEDevice::addIgnored(const NimBLEAddress &address) { m_ignoreList.push_back(address); } @@ -1063,7 +1129,8 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { * @brief Remove a device from the ignore list. * @param [in] address The address of the device we want to remove from the list. */ -/*STATIC*/void NimBLEDevice::removeIgnored(const NimBLEAddress &address) { +/*STATIC*/ +void NimBLEDevice::removeIgnored(const NimBLEAddress &address) { for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) { if((*it).equals(address)){ m_ignoreList.erase(it); @@ -1077,6 +1144,7 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) { * @brief Set a custom callback for gap events. * @param [in] handler The function to call when gap events occur. */ +/*STATIC*/ void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) { m_customGapHandler = handler; int rc = ble_gap_event_listener_register(&m_listener, m_customGapHandler, NULL); @@ -1088,5 +1156,4 @@ void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) { } } // setCustomGapHandler - #endif // CONFIG_BT_ENABLED diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index cc3db9a..94ad294 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -38,7 +38,9 @@ #include "NimBLESecurity.h" #include "NimBLEAddress.h" -#include "esp_bt.h" +#ifdef ESP_PLATFORM +# include "esp_bt.h" +#endif #include #include @@ -109,8 +111,17 @@ public: static NimBLEServer* getServer(); #endif +#ifdef ESP_PLATFORM static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT); static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT); + static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false); + static void setScanDuplicateCacheSize(uint16_t cacheSize); + static void setScanFilterMode(uint8_t type); +#else + static void setPower(int dbm); + static int getPower(); +#endif + static void setCustomGapHandler(gap_event_handler handler); static void setSecurityAuth(bool bonding, bool mitm, bool sc); static void setSecurityAuth(uint8_t auth_req); @@ -120,15 +131,12 @@ public: static void setSecurityPasskey(uint32_t pin); static uint32_t getSecurityPasskey(); static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks); - static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false); static int startSecurity(uint16_t conn_id); static int setMTU(uint16_t mtu); static uint16_t getMTU(); static bool isIgnored(const NimBLEAddress &address); static void addIgnored(const NimBLEAddress &address); static void removeIgnored(const NimBLEAddress &address); - static void setScanDuplicateCacheSize(uint16_t cacheSize); - static void setScanFilterMode(uint8_t type); #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) static NimBLEAdvertising* getAdvertising(); @@ -198,8 +206,10 @@ private: static ble_gap_event_listener m_listener; static gap_event_handler m_customGapHandler; static uint8_t m_own_addr_type; +#ifdef ESP_PLATFORM static uint16_t m_scanDuplicateSize; static uint8_t m_scanFilterMode; +#endif static std::vector m_whiteList; }; diff --git a/src/NimBLEEddystoneTLM.cpp b/src/NimBLEEddystoneTLM.cpp index 0c89930..255131c 100644 --- a/src/NimBLEEddystoneTLM.cpp +++ b/src/NimBLEEddystoneTLM.cpp @@ -126,30 +126,30 @@ std::string NimBLEEddystoneTLM::toString() { out += " C\n"; out += "Adv. Count "; - snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount)); + snprintf(val, sizeof(val), "%" PRIu32, ENDIAN_CHANGE_U32(m_eddystoneData.advCount)); out += val; out += "\n"; out += "Time in seconds "; - snprintf(val, sizeof(val), "%d", rawsec/10); + snprintf(val, sizeof(val), "%" PRIu32, rawsec/10); out += val; out += "\n"; out += "Time "; - snprintf(val, sizeof(val), "%04d", rawsec / 864000); + snprintf(val, sizeof(val), "%04" PRIu32, rawsec / 864000); out += val; out += "."; - snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24); + snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 36000) % 24); out += val; out += ":"; - snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60); + snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 600) % 60); out += val; out += ":"; - snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60); + snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 10) % 60); out += val; out += "\n"; diff --git a/src/NimBLERemoteDescriptor.cpp b/src/NimBLERemoteDescriptor.cpp index 02fbdc7..44b5590 100644 --- a/src/NimBLERemoteDescriptor.cpp +++ b/src/NimBLERemoteDescriptor.cpp @@ -186,6 +186,7 @@ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { + (void)attr; ble_task_data_t *pTaskData = (ble_task_data_t*)arg; NimBLERemoteDescriptor* desc = (NimBLERemoteDescriptor*)pTaskData->pATT; uint16_t conn_id = desc->getRemoteCharacteristic()->getRemoteService()->getClient()->getConnId(); diff --git a/src/NimBLEUtils.cpp b/src/NimBLEUtils.cpp index 5e7f79f..c6bd823 100644 --- a/src/NimBLEUtils.cpp +++ b/src/NimBLEUtils.cpp @@ -11,7 +11,8 @@ #include "NimBLEUtils.h" #include "NimBLELog.h" -#include "nimconfig.h" + +#include static const char* LOG_TAG = "NimBLEUtils"; @@ -342,6 +343,7 @@ const char* NimBLEUtils::returnCodeToString(int rc) { return "Unknown"; } #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT) + (void)rc; return ""; #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT) } @@ -369,6 +371,7 @@ const char* NimBLEUtils::advTypeToString(uint8_t advType) { return "Unknown flag"; } #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT) + (void)advType; return ""; #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT) } // adFlagsToString @@ -416,8 +419,11 @@ char* NimBLEUtils::buildHexData(uint8_t* target, const uint8_t* source, uint8_t * @param [in] arg Unused. */ void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){ + (void)arg; #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) NIMBLE_LOGD(LOG_TAG, "Received a GAP event: %s", gapEventToString(event->type)); +#else + (void)event; #endif } @@ -504,6 +510,7 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) { return "Unknown event type"; } #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) + (void)eventType; return ""; #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) } // gapEventToString