Merge pull request #14439 from Jason2866/updNimBLE

NimBLE update to v1.3.5
This commit is contained in:
Jason2866 2022-01-14 19:47:33 +01:00 committed by GitHub
commit 9c057bda17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
427 changed files with 6914 additions and 46658 deletions

View File

@ -1,6 +1,20 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [1.3.5] - 2022-01-14
### Added
- CONFIG_NIMBLE_CPP_DEBUG_LEVEL macro in nimconfig.h to allow setting the log level separately from the Arduino core log level.
### Fixed
- Memory leak when initializing/deinitializing the BLE stack caused by new FreeRTOS timers be created on each initialization.
## [1.3.4] - 2022-01-09
### Fixed
- Workaround for latest Arduino-esp32 core that causes tasks not to block when required, which caused functions to return prematurely resulting in exceptions/crashing.
- The wrong length value was being used to set the values read from peer attributes. This has been corrected to use the proper value size.
## [1.3.3] - 2021-11-24 ## [1.3.3] - 2021-11-24

View File

@ -2,7 +2,7 @@
"name": "NimBLE-Arduino", "name": "NimBLE-Arduino",
"keywords": "esp32, bluetooth", "keywords": "esp32, bluetooth",
"description": "Bluetooth low energy (BLE) library for arduino-esp32 based on NimBLE", "description": "Bluetooth low energy (BLE) library for arduino-esp32 based on NimBLE",
"version": "1.3.3", "version": "1.3.5",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "espressif32" "platforms": "espressif32"
} }

View File

@ -1,5 +1,5 @@
name=NimBLE-Arduino name=NimBLE-Arduino
version=1.3.3 version=1.3.5
author=h2zero author=h2zero
maintainer=h2zero <powellperalta@gmail.com> maintainer=h2zero <powellperalta@gmail.com>
sentence=Bluetooth low energy (BLE) library for arduino-esp32 based on NimBLE. sentence=Bluetooth low energy (BLE) library for arduino-esp32 based on NimBLE.

View File

@ -1,6 +1,5 @@
Apache Mynewt NimBLE Apache Mynewt NimBLE
Copyright 2015-2020 The Apache Software Foundation Copyright 2015-2018 The Apache Software Foundation
Modifications Copyright 2017-2020 Espressif Systems (Shanghai) CO., LTD.
This product includes software developed at This product includes software developed at
The Apache Software Foundation (http://www.apache.org/). The Apache Software Foundation (http://www.apache.org/).

View File

@ -16,8 +16,11 @@
* See also: * See also:
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLE2904.h" #include "NimBLE2904.h"
@ -83,4 +86,5 @@ void NimBLE2904::setUnit(uint16_t unit) {
setValue((uint8_t*) &m_data, sizeof(m_data)); setValue((uint8_t*) &m_data, sizeof(m_data));
} // setUnit } // setUnit
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif

View File

@ -14,8 +14,11 @@
#ifndef MAIN_NIMBLE2904_H_ #ifndef MAIN_NIMBLE2904_H_
#define MAIN_NIMBLE2904_H_ #define MAIN_NIMBLE2904_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
@ -79,5 +82,6 @@ private:
BLE2904_Data m_data; BLE2904_Data m_data;
}; // BLE2904 }; // BLE2904
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLE2904_H_ */ #endif /* MAIN_NIMBLE2904_H_ */

View File

@ -11,7 +11,7 @@
* Created on: Jul 2, 2017 * Created on: Jul 2, 2017
* Author: kolban * Author: kolban
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <algorithm> #include <algorithm>

View File

@ -14,15 +14,10 @@
#ifndef COMPONENTS_NIMBLEADDRESS_H_ #ifndef COMPONENTS_NIMBLEADDRESS_H_
#define COMPONENTS_NIMBLEADDRESS_H_ #define COMPONENTS_NIMBLEADDRESS_H_
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "nimble/ble.h" #include "nimble/ble.h"
#else
#include "nimble/nimble/include/nimble/ble.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
#undef max #undef max

View File

@ -11,9 +11,11 @@
* Created on: Jul 3, 2017 * Created on: Jul 3, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
@ -781,5 +783,7 @@ size_t NimBLEAdvertisedDevice::getPayloadLength() {
return m_payload.size(); return m_payload.size();
} // getPayloadLength } // getPayloadLength
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,22 +14,20 @@
#ifndef COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ #ifndef COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
#define COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ #define COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEScan.h" #include "NimBLEScan.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_hs_adv.h" #include "host/ble_hs_adv.h"
#else
#include "nimble/nimble/host/include/host/ble_hs_adv.h"
#endif
#include <map> #include <map>
#include <vector> #include <vector>
#include <time.h>
class NimBLEScan; class NimBLEScan;
@ -173,5 +171,6 @@ public:
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0; virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */ #endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */

View File

@ -13,14 +13,13 @@
* Author: kolban * Author: kolban
* *
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "services/gap/ble_svc_gap.h" #include "services/gap/ble_svc_gap.h"
#else
#include "nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h"
#endif
#include "NimBLEAdvertising.h" #include "NimBLEAdvertising.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
#include "NimBLEServer.h" #include "NimBLEServer.h"
@ -69,7 +68,6 @@ void NimBLEAdvertising::reset() {
m_advDataSet = false; m_advDataSet = false;
// Set this to non-zero to prevent auto start if host reset before started by app. // Set this to non-zero to prevent auto start if host reset before started by app.
m_duration = BLE_HS_FOREVER; m_duration = BLE_HS_FOREVER;
m_advCompCB = nullptr;
} // reset } // reset
@ -655,8 +653,12 @@ bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv
break; break;
} }
if(rc != 0) {
return false;
}
NIMBLE_LOGD(LOG_TAG, "<< Advertising start"); NIMBLE_LOGD(LOG_TAG, "<< Advertising start");
return (rc == 0); return true;
} // start } // start
@ -1026,4 +1028,5 @@ std::string NimBLEAdvertisementData::getPayload() {
return m_payload; return m_payload;
} // getPayload } // getPayload
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,15 +14,13 @@
#ifndef MAIN_BLEADVERTISING_H_ #ifndef MAIN_BLEADVERTISING_H_
#define MAIN_BLEADVERTISING_H_ #define MAIN_BLEADVERTISING_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_gap.h" #include "host/ble_gap.h"
#else
#include "nimble/nimble/host/include/host/ble_gap.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
#undef max #undef max
@ -134,5 +132,6 @@ private:
std::vector<uint8_t> m_uri; std::vector<uint8_t> m_uri;
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_BLEADVERTISING_H_ */ #endif /* MAIN_BLEADVERTISING_H_ */

View File

@ -11,7 +11,7 @@
* Created on: Jan 4, 2018 * Created on: Jan 4, 2018
* Author: kolban * Author: kolban
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <string.h> #include <string.h>

View File

@ -9,9 +9,11 @@
* Created on: Jun 22, 2017 * Created on: Jun 22, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLE2904.h" #include "NimBLE2904.h"
@ -49,6 +51,7 @@ NimBLECharacteristic::NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t prop
m_pCallbacks = &defaultCallback; m_pCallbacks = &defaultCallback;
m_pService = pService; m_pService = pService;
m_value = ""; m_value = "";
m_valMux = portMUX_INITIALIZER_UNLOCKED;
m_timestamp = 0; m_timestamp = 0;
m_removed = 0; m_removed = 0;
} // NimBLECharacteristic } // NimBLECharacteristic
@ -234,12 +237,12 @@ NimBLEUUID NimBLECharacteristic::getUUID() {
* @return A std::string containing the current characteristic value. * @return A std::string containing the current characteristic value.
*/ */
std::string NimBLECharacteristic::getValue(time_t *timestamp) { std::string NimBLECharacteristic::getValue(time_t *timestamp) {
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&m_valMux);
std::string retVal = m_value; std::string retVal = m_value;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
return retVal; return retVal;
} // getValue } // getValue
@ -250,9 +253,10 @@ std::string NimBLECharacteristic::getValue(time_t *timestamp) {
* @return The length of the current characteristic data. * @return The length of the current characteristic data.
*/ */
size_t NimBLECharacteristic::getDataLength() { size_t NimBLECharacteristic::getDataLength() {
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&m_valMux);
size_t len = m_value.length(); size_t len = m_value.length();
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
return len; return len;
} }
@ -285,10 +289,11 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han
pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc); pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc);
} }
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&pCharacteristic->m_valMux);
rc = os_mbuf_append(ctxt->om, (uint8_t*)pCharacteristic->m_value.data(), rc = os_mbuf_append(ctxt->om, (uint8_t*)pCharacteristic->m_value.data(),
pCharacteristic->m_value.length()); pCharacteristic->m_value.length());
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&pCharacteristic->m_valMux);
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
@ -427,7 +432,7 @@ void NimBLECharacteristic::notify(bool is_notification) {
int rc = 0; int rc = 0;
for (auto &it : m_subscribedVec) { for (auto &it : m_subscribedVec) {
uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first) - 3; uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first);
// check if connected and subscribed // check if connected and subscribed
if(_mtu == 0 || it.second == 0) { if(_mtu == 0 || it.second == 0) {
@ -443,8 +448,8 @@ void NimBLECharacteristic::notify(bool is_notification) {
} }
} }
if (length > _mtu) { if (length > _mtu - 3) {
NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu); NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu - 3);
} }
if(is_notification && (!(it.second & NIMBLE_SUB_NOTIFY))) { if(is_notification && (!(it.second & NIMBLE_SUB_NOTIFY))) {
@ -511,7 +516,7 @@ NimBLECharacteristicCallbacks* NimBLECharacteristic::getCallbacks() {
* @param [in] length The length of the data in bytes. * @param [in] length The length of the data in bytes.
*/ */
void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) { void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
#if CONFIG_LOG_DEFAULT_LEVEL > 3 || (ARDUINO_ARCH_ESP32 && CORE_DEBUG_LEVEL >= 4) #if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
char* pHex = NimBLEUtils::buildHexData(nullptr, data, length); char* pHex = NimBLEUtils::buildHexData(nullptr, data, length);
NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str());
free(pHex); free(pHex);
@ -523,10 +528,10 @@ void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
} }
time_t t = time(nullptr); time_t t = time(nullptr);
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&m_valMux);
m_value = std::string((char*)data, length); m_value = std::string((char*)data, length);
m_timestamp = t; m_timestamp = t;
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
NIMBLE_LOGD(LOG_TAG, "<< setValue"); NIMBLE_LOGD(LOG_TAG, "<< setValue");
} // setValue } // setValue
@ -636,4 +641,6 @@ void NimBLECharacteristicCallbacks::onSubscribe(NimBLECharacteristic* pCharacter
NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onSubscribe: default"); NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onSubscribe: default");
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -13,15 +13,13 @@
#ifndef MAIN_NIMBLECHARACTERISTIC_H_ #ifndef MAIN_NIMBLECHARACTERISTIC_H_
#define MAIN_NIMBLECHARACTERISTIC_H_ #define MAIN_NIMBLECHARACTERISTIC_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_hs.h" #include "host/ble_hs.h"
#else
#include "nimble/nimble/host/include/host/ble_hs.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
#undef max #undef max
@ -153,6 +151,7 @@ private:
NimBLEService* m_pService; NimBLEService* m_pService;
std::string m_value; std::string m_value;
std::vector<NimBLEDescriptor*> m_dscVec; std::vector<NimBLEDescriptor*> m_dscVec;
portMUX_TYPE m_valMux;
time_t m_timestamp; time_t m_timestamp;
uint8_t m_removed; uint8_t m_removed;
@ -196,5 +195,6 @@ public:
virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue); virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */
#endif /*MAIN_NIMBLECHARACTERISTIC_H_*/ #endif /*MAIN_NIMBLECHARACTERISTIC_H_*/

View File

@ -11,8 +11,11 @@
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEClient.h" #include "NimBLEClient.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -21,11 +24,8 @@
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#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
static const char* LOG_TAG = "NimBLEClient"; static const char* LOG_TAG = "NimBLEClient";
static NimBLEClientCallbacks defaultCallbacks; static NimBLEClientCallbacks defaultCallbacks;
@ -74,7 +74,6 @@ NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(pee
m_pConnParams.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; // Minimum length of connection event in 0.625ms units m_pConnParams.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; // Minimum length of connection event in 0.625ms units
m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units 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));
ble_npl_callout_init(&m_dcTimer, nimble_port_get_dflt_eventq(), ble_npl_callout_init(&m_dcTimer, nimble_port_get_dflt_eventq(),
NimBLEClient::dcTimerCb, this); NimBLEClient::dcTimerCb, this);
} // NimBLEClient } // NimBLEClient
@ -207,7 +206,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
m_peerAddress = address; m_peerAddress = address;
} }
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
m_pTaskData = &taskData; m_pTaskData = &taskData;
int rc = 0; int rc = 0;
@ -260,6 +260,10 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
return false; 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 // Wait for the connect timeout time +1 second for the connection to complete
if(ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(m_connectTimeout + 1000)) == pdFALSE) { if(ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(m_connectTimeout + 1000)) == pdFALSE) {
m_pTaskData = nullptr; m_pTaskData = nullptr;
@ -310,7 +314,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
* @return True on success. * @return True on success.
*/ */
bool NimBLEClient::secureConnection() { bool NimBLEClient::secureConnection() {
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
int retryCount = 1; int retryCount = 1;
@ -324,6 +329,10 @@ bool NimBLEClient::secureConnection() {
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--); } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
@ -444,28 +453,6 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
} // updateConnParams } // updateConnParams
/**
* @brief Request an update of the data packet length.
* * Can only be used after a connection has been established.
* @details Sends a data length update request to the server the client is connected to.
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
* The server needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
*/
void NimBLEClient::setDataLen(uint16_t tx_octets) {
#ifdef CONFIG_NIMBLE_CPP_IDF // not yet available in IDF, Sept 9 2021
return;
#else
uint16_t tx_time = (tx_octets + 14) * 8;
int rc = ble_gap_set_data_len(m_conn_id, tx_octets, tx_time);
if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
}
#endif
} // setDataLen
/** /**
* @brief Get detailed information about the current peer connection. * @brief Get detailed information about the current peer connection.
*/ */
@ -670,7 +657,8 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
} }
int rc = 0; int rc = 0;
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
if(uuid_filter == nullptr) { if(uuid_filter == nullptr) {
rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData); rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData);
@ -685,6 +673,11 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
// wait until we have all the services // wait until we have all the services
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
m_lastErr = taskData.rc; m_lastErr = taskData.rc;
@ -958,11 +951,11 @@ uint16_t NimBLEClient::getMTU() {
(*characteristic)->toString().c_str()); (*characteristic)->toString().c_str());
time_t t = time(nullptr); time_t t = time(nullptr);
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&(*characteristic)->m_valMux);
(*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data, (*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data,
event->notify_rx.om->om_len); event->notify_rx.om->om_len);
(*characteristic)->m_timestamp = t; (*characteristic)->m_timestamp = t;
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&(*characteristic)->m_valMux);
if ((*characteristic)->m_notifyCallback != nullptr) { if ((*characteristic)->m_notifyCallback != nullptr) {
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
@ -1212,4 +1205,5 @@ bool NimBLEClientCallbacks::onConfirmPIN(uint32_t pin){
return true; return true;
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // CONFIG_BT_ENABLED

View File

@ -14,8 +14,11 @@
#ifndef MAIN_NIMBLECLIENT_H_ #ifndef MAIN_NIMBLECLIENT_H_
#define MAIN_NIMBLECLIENT_H_ #define MAIN_NIMBLECLIENT_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
@ -68,7 +71,6 @@ public:
uint16_t scanInterval=16, uint16_t scanWindow=16); uint16_t scanInterval=16, uint16_t scanWindow=16);
void updateConnParams(uint16_t minInterval, uint16_t maxInterval, void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout); uint16_t latency, uint16_t timeout);
void setDataLen(uint16_t tx_octets);
void discoverAttributes(); void discoverAttributes();
NimBLEConnInfo getConnInfo(); NimBLEConnInfo getConnInfo();
int getLastError(); int getLastError();
@ -158,5 +160,6 @@ public:
virtual bool onConfirmPIN(uint32_t pin); virtual bool onConfirmPIN(uint32_t pin);
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // CONFIG_BT_ENABLED
#endif /* MAIN_NIMBLECLIENT_H_ */ #endif /* MAIN_NIMBLECLIENT_H_ */

View File

@ -11,9 +11,11 @@
* Created on: Jun 22, 2017 * Created on: Jun 22, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEService.h" #include "NimBLEService.h"
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
@ -49,6 +51,7 @@ NimBLEDescriptor::NimBLEDescriptor(NimBLEUUID uuid, uint16_t properties, uint16_
m_pCharacteristic = pCharacteristic; m_pCharacteristic = pCharacteristic;
m_pCallbacks = &defaultCallbacks; // No initial callback. m_pCallbacks = &defaultCallbacks; // No initial callback.
m_value.attr_value = (uint8_t*) calloc(max_len,1); // Allocate storage for the value. m_value.attr_value = (uint8_t*) calloc(max_len,1); // Allocate storage for the value.
m_valMux = portMUX_INITIALIZER_UNLOCKED;
m_properties = 0; m_properties = 0;
m_removed = 0; m_removed = 0;
@ -142,9 +145,6 @@ NimBLECharacteristic* NimBLEDescriptor::getCharacteristic() {
int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle, int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) { struct ble_gatt_access_ctxt *ctxt, void *arg) {
(void)conn_handle;
(void)attr_handle;
const ble_uuid_t *uuid; const ble_uuid_t *uuid;
int rc; int rc;
NimBLEDescriptor* pDescriptor = (NimBLEDescriptor*)arg; NimBLEDescriptor* pDescriptor = (NimBLEDescriptor*)arg;
@ -161,10 +161,9 @@ int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
if(ctxt->om->om_pkthdr_len > 8) { if(ctxt->om->om_pkthdr_len > 8) {
pDescriptor->m_pCallbacks->onRead(pDescriptor); pDescriptor->m_pCallbacks->onRead(pDescriptor);
} }
portENTER_CRITICAL(&pDescriptor->m_valMux);
ble_npl_hw_enter_critical();
rc = os_mbuf_append(ctxt->om, pDescriptor->getValue(), pDescriptor->getLength()); rc = os_mbuf_append(ctxt->om, pDescriptor->getValue(), pDescriptor->getLength());
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&pDescriptor->m_valMux);
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
@ -235,12 +234,10 @@ void NimBLEDescriptor::setValue(const uint8_t* data, size_t length) {
NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, m_value.attr_max_len); NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, m_value.attr_max_len);
return; return;
} }
portENTER_CRITICAL(&m_valMux);
ble_npl_hw_enter_critical();
m_value.attr_len = length; m_value.attr_len = length;
memcpy(m_value.attr_value, data, length); memcpy(m_value.attr_value, data, length);
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
} // setValue } // setValue
@ -280,7 +277,6 @@ NimBLEDescriptorCallbacks::~NimBLEDescriptorCallbacks() {}
* @param [in] pDescriptor The descriptor that is the source of the event. * @param [in] pDescriptor The descriptor that is the source of the event.
*/ */
void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor) { void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor) {
(void)pDescriptor;
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onRead: default"); NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onRead: default");
} // onRead } // onRead
@ -290,8 +286,8 @@ void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor) {
* @param [in] pDescriptor The descriptor that is the source of the event. * @param [in] pDescriptor The descriptor that is the source of the event.
*/ */
void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor) { void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor) {
(void)pDescriptor;
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default"); NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default");
} // onWrite } // onWrite
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,9 +14,11 @@
#ifndef MAIN_NIMBLEDESCRIPTOR_H_ #ifndef MAIN_NIMBLEDESCRIPTOR_H_
#define MAIN_NIMBLEDESCRIPTOR_H_ #define MAIN_NIMBLEDESCRIPTOR_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
@ -90,6 +92,7 @@ private:
NimBLECharacteristic* m_pCharacteristic; NimBLECharacteristic* m_pCharacteristic;
uint8_t m_properties; uint8_t m_properties;
attr_value_t m_value; attr_value_t m_value;
portMUX_TYPE m_valMux;
uint8_t m_removed; uint8_t m_removed;
}; // NimBLEDescriptor }; // NimBLEDescriptor
@ -110,5 +113,6 @@ public:
#include "NimBLE2904.h" #include "NimBLE2904.h"
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLEDESCRIPTOR_H_ */ #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */

View File

@ -11,45 +11,27 @@
* Created on: Mar 16, 2017 * Created on: Mar 16, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#ifdef ESP_PLATFORM #include "esp_err.h"
# include "esp_err.h" #include "esp_bt.h"
# include "esp_bt.h" #include "nvs_flash.h"
# include "nvs_flash.h" #include "esp_nimble_hci.h"
# if defined(CONFIG_NIMBLE_CPP_IDF) #include "nimble/nimble_port.h"
# include "esp_nimble_hci.h" #include "nimble/nimble_port_freertos.h"
# include "nimble/nimble_port.h" #include "host/ble_hs.h"
# include "nimble/nimble_port_freertos.h" #include "host/ble_hs_pvcy.h"
# include "host/ble_hs.h" #include "host/util/util.h"
# include "host/ble_hs_pvcy.h" #include "services/gap/ble_svc_gap.h"
# include "host/util/util.h" #include "services/gatt/ble_svc_gatt.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/nimble/controller/include/controller/ble_phy.h"
#endif
#ifndef CONFIG_NIMBLE_CPP_IDF #ifdef CONFIG_ENABLE_ARDUINO_DEPENDS
# include "nimble/porting/nimble/include/nimble/nimble_port.h" #include "esp32-hal-bt.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 #endif
#include "NimBLELog.h" #include "NimBLELog.h"
@ -81,10 +63,9 @@ std::list <NimBLEAddress> NimBLEDevice::m_ignoreList;
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList; std::vector<NimBLEAddress> NimBLEDevice::m_whiteList;
NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr; NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr;
uint8_t NimBLEDevice::m_own_addr_type = BLE_OWN_ADDR_PUBLIC; 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; uint16_t NimBLEDevice::m_scanDuplicateSize = CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE;
uint8_t NimBLEDevice::m_scanFilterMode = CONFIG_BTDM_SCAN_DUPL_TYPE; uint8_t NimBLEDevice::m_scanFilterMode = CONFIG_BTDM_SCAN_DUPL_TYPE;
#endif
/** /**
* @brief Create a new instance of a server. * @brief Create a new instance of a server.
@ -149,8 +130,7 @@ void NimBLEDevice::stopAdvertising() {
* try and release/delete it. * try and release/delete it.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
/* STATIC */ /* STATIC */ NimBLEScan* NimBLEDevice::getScan() {
NimBLEScan* NimBLEDevice::getScan() {
if (m_pScan == nullptr) { if (m_pScan == nullptr) {
m_pScan = new NimBLEScan(); m_pScan = new NimBLEScan();
} }
@ -167,8 +147,7 @@ NimBLEScan* NimBLEDevice::getScan() {
* @return A reference to the new client object. * @return A reference to the new client object.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
/* STATIC */ /* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW(LOG_TAG,"Number of clients exceeds Max connections. Cur=%d Max=%d", NIMBLE_LOGW(LOG_TAG,"Number of clients exceeds Max connections. Cur=%d Max=%d",
m_cList.size(), NIMBLE_MAX_CONNECTIONS); m_cList.size(), NIMBLE_MAX_CONNECTIONS);
@ -186,8 +165,7 @@ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
* Checks if it is connected or trying to connect and disconnects/stops it first. * Checks if it is connected or trying to connect and disconnects/stops it first.
* @param [in] pClient A pointer to the client object. * @param [in] pClient A pointer to the client object.
*/ */
/* STATIC */ /* STATIC */ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) {
bool NimBLEDevice::deleteClient(NimBLEClient* pClient) {
if(pClient == nullptr) { if(pClient == nullptr) {
return false; return false;
} }
@ -231,8 +209,7 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) {
* @brief Get the list of created client objects. * @brief Get the list of created client objects.
* @return A pointer to the list of clients. * @return A pointer to the list of clients.
*/ */
/* STATIC */ /* STATIC */std::list<NimBLEClient*>* NimBLEDevice::getClientList() {
std::list<NimBLEClient*>* NimBLEDevice::getClientList() {
return &m_cList; return &m_cList;
} // getClientList } // getClientList
@ -241,8 +218,7 @@ std::list<NimBLEClient*>* NimBLEDevice::getClientList() {
* @brief Get the number of created client objects. * @brief Get the number of created client objects.
* @return Number of client objects created. * @return Number of client objects created.
*/ */
/* STATIC */ /* STATIC */size_t NimBLEDevice::getClientListSize() {
size_t NimBLEDevice::getClientListSize() {
return m_cList.size(); return m_cList.size();
} // getClientList } // getClientList
@ -252,8 +228,7 @@ size_t NimBLEDevice::getClientListSize() {
* @param [in] conn_id The client connection ID to search for. * @param [in] conn_id The client connection ID to search for.
* @return A pointer to the client object with the spcified connection ID. * @return A pointer to the client object with the spcified connection ID.
*/ */
/* STATIC */ /* STATIC */NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) {
NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
if((*it)->getConnId() == conn_id) { if((*it)->getConnId() == conn_id) {
return (*it); return (*it);
@ -269,8 +244,7 @@ NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) {
* @param [in] peer_addr The address of the peer to search for. * @param [in] peer_addr The address of the peer to search for.
* @return A pointer to the client object with the peer address. * @return A pointer to the client object with the peer address.
*/ */
/* STATIC */ /* STATIC */NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) {
NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
if((*it)->getPeerAddress().equals(peer_addr)) { if((*it)->getPeerAddress().equals(peer_addr)) {
return (*it); return (*it);
@ -284,8 +258,7 @@ NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_add
* @brief Finds the first disconnected client in the list. * @brief Finds the first disconnected client in the list.
* @return A pointer to the first client object that is not connected to a peer. * @return A pointer to the first client object that is not connected to a peer.
*/ */
/* STATIC */ /* STATIC */NimBLEClient* NimBLEDevice::getDisconnectedClient() {
NimBLEClient* NimBLEDevice::getDisconnectedClient() {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
if(!(*it)->isConnected()) { if(!(*it)->isConnected()) {
return (*it); return (*it);
@ -296,7 +269,7 @@ NimBLEClient* NimBLEDevice::getDisconnectedClient() {
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#ifdef ESP_PLATFORM
/** /**
* @brief Set the transmission power. * @brief Set the transmission power.
* @param [in] powerLevel The power level to set, can be one of: * @param [in] powerLevel The power level to set, can be one of:
@ -322,15 +295,12 @@ NimBLEClient* NimBLEDevice::getDisconnectedClient() {
* * ESP_BLE_PWR_TYPE_SCAN = 10, For scan * * 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 * * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value
*/ */
/* STATIC */ /* STATIC */ void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType) {
void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType) {
NIMBLE_LOGD(LOG_TAG, ">> setPower: %d (type: %d)", powerLevel, powerType); NIMBLE_LOGD(LOG_TAG, ">> setPower: %d (type: %d)", powerLevel, powerType);
esp_err_t errRc = esp_ble_tx_power_set(powerType, powerLevel); esp_err_t errRc = esp_ble_tx_power_set(powerType, powerLevel);
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_set: rc=%d", errRc); NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_set: rc=%d", errRc);
} }
NIMBLE_LOGD(LOG_TAG, "<< setPower"); NIMBLE_LOGD(LOG_TAG, "<< setPower");
} // setPower } // setPower
@ -352,8 +322,9 @@ void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t p
* * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value * * 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. * @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)) { switch(esp_ble_tx_power_get(powerType)) {
case ESP_PWR_LVL_N12: case ESP_PWR_LVL_N12:
return -12; return -12;
@ -376,25 +347,13 @@ int NimBLEDevice::getPower(esp_ble_power_type_t powerType) {
} }
} // getPower } // 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. * @brief Get our device address.
* @return A NimBLEAddress object of our public address if we have one, * @return A NimBLEAddress object of our public address if we have one,
* if not then our current random address. * if not then our current random address.
*/ */
/* STATIC*/ /* STATIC*/ NimBLEAddress NimBLEDevice::getAddress() {
NimBLEAddress NimBLEDevice::getAddress() {
ble_addr_t addr = {BLE_ADDR_PUBLIC, 0}; ble_addr_t addr = {BLE_ADDR_PUBLIC, 0};
if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) { if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) {
@ -411,8 +370,7 @@ NimBLEAddress NimBLEDevice::getAddress() {
* @brief Return a string representation of the address of this device. * @brief Return a string representation of the address of this device.
* @return A string representation of this device address. * @return A string representation of this device address.
*/ */
/* STATIC */ /* STATIC */ std::string NimBLEDevice::toString() {
std::string NimBLEDevice::toString() {
return getAddress().toString(); return getAddress().toString();
} // toString } // toString
@ -422,8 +380,7 @@ std::string NimBLEDevice::toString() {
* @param [in] mtu Value to set local mtu: * @param [in] mtu Value to set local mtu:
* * This should be larger than 23 and lower or equal to BLE_ATT_MTU_MAX = 527. * * This should be larger than 23 and lower or equal to BLE_ATT_MTU_MAX = 527.
*/ */
/* STATIC */ /* STATIC */int NimBLEDevice::setMTU(uint16_t mtu) {
int NimBLEDevice::setMTU(uint16_t mtu) {
NIMBLE_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu); NIMBLE_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu);
int rc = ble_att_set_preferred_mtu(mtu); int rc = ble_att_set_preferred_mtu(mtu);
@ -441,13 +398,11 @@ int NimBLEDevice::setMTU(uint16_t mtu) {
* @brief Get local MTU value set. * @brief Get local MTU value set.
* @return The current preferred MTU setting. * @return The current preferred MTU setting.
*/ */
/* STATIC */ /* STATIC */uint16_t NimBLEDevice::getMTU() {
uint16_t NimBLEDevice::getMTU() {
return ble_att_preferred_mtu(); return ble_att_preferred_mtu();
} }
#ifdef ESP_PLATFORM
/** /**
* @brief Set the duplicate filter cache size for filtering scanned devices. * @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 * @param [in] cacheSize The number of advertisements filtered before the cache is reset.\n
@ -493,7 +448,6 @@ void NimBLEDevice::setScanFilterMode(uint8_t mode) {
m_scanFilterMode = mode; m_scanFilterMode = mode;
} }
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
/** /**
@ -516,7 +470,7 @@ int NimBLEDevice::getNumBonds() {
/** /**
* @brief Deletes all bonding information. * @brief Deletes all bonding information.
*/ */
/*STATIC*/ /*STATIC*/
void NimBLEDevice::deleteAllBonds() { void NimBLEDevice::deleteAllBonds() {
ble_store_clear(); ble_store_clear();
} }
@ -596,7 +550,6 @@ NimBLEAddress NimBLEDevice::getBondedAddress(int index) {
* @param [in] address The address to check for in the whitelist. * @param [in] address The address to check for in the whitelist.
* @returns true if the address is in the whitelist. * @returns true if the address is in the whitelist.
*/ */
/*STATIC*/
bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) { bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) {
for (auto &it : m_whiteList) { for (auto &it : m_whiteList) {
if (it == address) { if (it == address) {
@ -613,7 +566,6 @@ bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) {
* @param [in] address The address to add to the whitelist. * @param [in] address The address to add to the whitelist.
* @returns true if successful. * @returns true if successful.
*/ */
/*STATIC*/
bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) { bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) {
if (NimBLEDevice::onWhiteList(address)) { if (NimBLEDevice::onWhiteList(address)) {
return true; return true;
@ -645,7 +597,6 @@ bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) {
* @param [in] address The address to remove from the whitelist. * @param [in] address The address to remove from the whitelist.
* @returns true if successful. * @returns true if successful.
*/ */
/*STATIC*/
bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) { bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) {
if (!NimBLEDevice::onWhiteList(address)) { if (!NimBLEDevice::onWhiteList(address)) {
return true; return true;
@ -685,7 +636,6 @@ bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) {
* @brief Gets the count of addresses in the whitelist. * @brief Gets the count of addresses in the whitelist.
* @returns The number of addresses in the whitelist. * @returns The number of addresses in the whitelist.
*/ */
/*STATIC*/
size_t NimBLEDevice::getWhiteListCount() { size_t NimBLEDevice::getWhiteListCount() {
return m_whiteList.size(); return m_whiteList.size();
} }
@ -696,7 +646,6 @@ size_t NimBLEDevice::getWhiteListCount() {
* @param [in] index The vector index to retrieve the address from. * @param [in] index The vector index to retrieve the address from.
* @returns the NimBLEAddress at the whitelist index or nullptr if not found. * @returns the NimBLEAddress at the whitelist index or nullptr if not found.
*/ */
/*STATIC*/
NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) {
if (index > m_whiteList.size()) { if (index > m_whiteList.size()) {
NIMBLE_LOGE(LOG_TAG, "Invalid index; %u", index); NIMBLE_LOGE(LOG_TAG, "Invalid index; %u", index);
@ -710,8 +659,7 @@ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) {
* @brief Host reset, we pass the message so we don't make calls until resynced. * @brief Host reset, we pass the message so we don't make calls until resynced.
* @param [in] reason The reason code for the reset. * @param [in] reason The reason code for the reset.
*/ */
/* STATIC */ /* STATIC */ void NimBLEDevice::onReset(int reason)
void NimBLEDevice::onReset(int reason)
{ {
if(!m_synced) { if(!m_synced) {
return; return;
@ -735,8 +683,7 @@ void NimBLEDevice::onReset(int reason)
/** /**
* @brief Host resynced with controller, all clear to make calls to the stack. * @brief Host resynced with controller, all clear to make calls to the stack.
*/ */
/* STATIC */ /* STATIC */ void NimBLEDevice::onSync(void)
void NimBLEDevice::onSync(void)
{ {
NIMBLE_LOGI(LOG_TAG, "NimBle host synced."); NIMBLE_LOGI(LOG_TAG, "NimBle host synced.");
// This check is needed due to potentially being called multiple times in succession // This check is needed due to potentially being called multiple times in succession
@ -749,14 +696,6 @@ void NimBLEDevice::onSync(void)
int rc = ble_hs_util_ensure_addr(0); int rc = ble_hs_util_ensure_addr(0);
assert(rc == 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. // Yield for houskeeping before returning to operations.
// Occasionally triggers exception without. // Occasionally triggers exception without.
taskYIELD(); taskYIELD();
@ -782,11 +721,9 @@ void NimBLEDevice::onSync(void)
/** /**
* @brief The main host task. * @brief The main host task.
*/ */
/* STATIC */ /* STATIC */ 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");
/* This function will return only when nimble_port_stop() is executed */ /* This function will return only when nimble_port_stop() is executed */
nimble_port_run(); nimble_port_run();
@ -798,11 +735,9 @@ void NimBLEDevice::host_task(void *param)
* @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.
*/ */
/* STATIC */ /* STATIC */ void NimBLEDevice::init(const std::string &deviceName) {
void NimBLEDevice::init(const std::string &deviceName) {
if(!initialized){ if(!initialized){
int rc=0; int rc=0;
#ifdef ESP_PLATFORM
esp_err_t errRc = ESP_OK; esp_err_t errRc = ESP_OK;
#ifdef CONFIG_ENABLE_ARDUINO_DEPENDS #ifdef CONFIG_ENABLE_ARDUINO_DEPENDS
@ -834,7 +769,6 @@ void NimBLEDevice::init(const std::string &deviceName) {
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg)); ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE)); ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
ESP_ERROR_CHECK(esp_nimble_hci_init()); ESP_ERROR_CHECK(esp_nimble_hci_init());
#endif
nimble_port_init(); nimble_port_init();
// Setup callbacks for host events // Setup callbacks for host events
@ -859,10 +793,9 @@ void NimBLEDevice::init(const std::string &deviceName) {
nimble_port_freertos_init(NimBLEDevice::host_task); nimble_port_freertos_init(NimBLEDevice::host_task);
} }
// 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
while(!m_synced){ while(!m_synced){
taskYIELD(); vTaskDelay(1 / portTICK_PERIOD_MS);
} }
initialized = true; // Set the initialization flag to ensure we are only initialized once. initialized = true; // Set the initialization flag to ensure we are only initialized once.
@ -874,17 +807,16 @@ void NimBLEDevice::init(const std::string &deviceName) {
* @param [in] clearAll If true, deletes all server/advertising/scan/client objects after deinitializing. * @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. * @note If clearAll is true when called, any references to the created objects become invalid.
*/ */
/* STATIC */ /* STATIC */ void NimBLEDevice::deinit(bool clearAll) {
void NimBLEDevice::deinit(bool clearAll) {
int ret = nimble_port_stop(); int ret = nimble_port_stop();
if (ret == 0) { if (ret == 0) {
nimble_port_deinit(); nimble_port_deinit();
#ifdef ESP_PLATFORM
ret = esp_nimble_hci_and_controller_deinit(); ret = esp_nimble_hci_and_controller_deinit();
if (ret != ESP_OK) { if (ret != ESP_OK) {
NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", ret); NIMBLE_LOGE(LOG_TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", ret);
} }
#endif
initialized = false; initialized = false;
m_synced = false; m_synced = false;
@ -931,7 +863,6 @@ void NimBLEDevice::deinit(bool clearAll) {
* @brief Check if the initialization is complete. * @brief Check if the initialization is complete.
* @return true if initialized. * @return true if initialized.
*/ */
/*STATIC*/
bool NimBLEDevice::getInitialized() { bool NimBLEDevice::getInitialized() {
return initialized; return initialized;
} // getInitialized } // getInitialized
@ -943,8 +874,7 @@ bool NimBLEDevice::getInitialized() {
* @param mitm If true we are capable of man in the middle protection, false if not. * @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. * @param sc If true we will perform secure connection pairing, false we will use legacy pairing.
*/ */
/*STATIC*/ /*STATIC*/ void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) {
void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) {
NIMBLE_LOGD(LOG_TAG, "Setting bonding: %d, mitm: %d, sc: %d",bonding,mitm,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_bonding = bonding;
ble_hs_cfg.sm_mitm = mitm; ble_hs_cfg.sm_mitm = mitm;
@ -961,8 +891,7 @@ void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) {
* * 0x08 BLE_SM_PAIR_AUTHREQ_SC * * 0x08 BLE_SM_PAIR_AUTHREQ_SC
* * 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported. * * 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported.
*/ */
/*STATIC*/ /*STATIC*/void NimBLEDevice::setSecurityAuth(uint8_t auth_req) {
void NimBLEDevice::setSecurityAuth(uint8_t auth_req) {
NimBLEDevice::setSecurityAuth((auth_req & BLE_SM_PAIR_AUTHREQ_BOND)>0, NimBLEDevice::setSecurityAuth((auth_req & BLE_SM_PAIR_AUTHREQ_BOND)>0,
(auth_req & BLE_SM_PAIR_AUTHREQ_MITM)>0, (auth_req & BLE_SM_PAIR_AUTHREQ_MITM)>0,
(auth_req & BLE_SM_PAIR_AUTHREQ_SC)>0); (auth_req & BLE_SM_PAIR_AUTHREQ_SC)>0);
@ -978,8 +907,7 @@ void NimBLEDevice::setSecurityAuth(uint8_t auth_req) {
* * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability * * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability
* * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability * * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability
*/ */
/*STATIC*/ /*STATIC*/ void NimBLEDevice::setSecurityIOCap(uint8_t iocap) {
void NimBLEDevice::setSecurityIOCap(uint8_t iocap) {
ble_hs_cfg.sm_io_cap = iocap; ble_hs_cfg.sm_io_cap = iocap;
} // setSecurityIOCap } // setSecurityIOCap
@ -993,8 +921,7 @@ void NimBLEDevice::setSecurityIOCap(uint8_t iocap) {
* * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN
* * 0x08: BLE_SM_PAIR_KEY_DIST_LINK * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK
*/ */
/*STATIC*/ /*STATIC*/void NimBLEDevice::setSecurityInitKey(uint8_t init_key) {
void NimBLEDevice::setSecurityInitKey(uint8_t init_key) {
ble_hs_cfg.sm_our_key_dist = init_key; ble_hs_cfg.sm_our_key_dist = init_key;
} // setsSecurityInitKey } // setsSecurityInitKey
@ -1008,8 +935,7 @@ void NimBLEDevice::setSecurityInitKey(uint8_t init_key) {
* * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN
* * 0x08: BLE_SM_PAIR_KEY_DIST_LINK * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK
*/ */
/*STATIC*/ /*STATIC*/void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) {
void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) {
ble_hs_cfg.sm_their_key_dist = resp_key; ble_hs_cfg.sm_their_key_dist = resp_key;
} // setsSecurityRespKey } // setsSecurityRespKey
@ -1018,8 +944,7 @@ void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) {
* @brief Set the passkey the server will ask for when pairing. * @brief Set the passkey the server will ask for when pairing.
* @param [in] pin The passkey to use. * @param [in] pin The passkey to use.
*/ */
/*STATIC*/ /*STATIC*/void NimBLEDevice::setSecurityPasskey(uint32_t pin) {
void NimBLEDevice::setSecurityPasskey(uint32_t pin) {
m_passkey = pin; m_passkey = pin;
} // setSecurityPasskey } // setSecurityPasskey
@ -1028,8 +953,7 @@ void NimBLEDevice::setSecurityPasskey(uint32_t pin) {
* @brief Get the current passkey used for pairing. * @brief Get the current passkey used for pairing.
* @return The current passkey. * @return The current passkey.
*/ */
/*STATIC*/ /*STATIC*/uint32_t NimBLEDevice::getSecurityPasskey() {
uint32_t NimBLEDevice::getSecurityPasskey() {
return m_passkey; return m_passkey;
} // getSecurityPasskey } // getSecurityPasskey
@ -1039,13 +963,11 @@ uint32_t NimBLEDevice::getSecurityPasskey() {
* @param [in] callbacks Pointer to NimBLESecurityCallbacks class * @param [in] callbacks Pointer to NimBLESecurityCallbacks class
* @deprecated For backward compatibility, New code should use client/server callback methods. * @deprecated For backward compatibility, New code should use client/server callback methods.
*/ */
/*STATIC*/
void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
NimBLEDevice::m_securityCallbacks = callbacks; NimBLEDevice::m_securityCallbacks = callbacks;
} // setSecurityCallbacks } // setSecurityCallbacks
#ifdef ESP_PLATFORM
/** /**
* @brief Set the own address type. * @brief Set the own address type.
* @param [in] own_addr_type Own Bluetooth Device address type.\n * @param [in] own_addr_type Own Bluetooth Device address type.\n
@ -1056,7 +978,6 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
* * 0x03: BLE_OWN_ADDR_RPA_RANDOM_DEFAULT * * 0x03: BLE_OWN_ADDR_RPA_RANDOM_DEFAULT
* @param [in] useNRPA If true, and address type is random, uses a non-resolvable random address. * @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) { void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) {
m_own_addr_type = own_addr_type; m_own_addr_type = own_addr_type;
switch (own_addr_type) { switch (own_addr_type) {
@ -1080,15 +1001,18 @@ void NimBLEDevice::setOwnAddrType(uint8_t own_addr_type, bool useNRPA) {
break; break;
} }
} // setOwnAddrType } // setOwnAddrType
#endif
/** /**
* @brief Start the connection securing and authorization for this connection. * @brief Start the connection securing and authorization for this connection.
* @param conn_id The connection id of the peer device. * @param conn_id The connection id of the peer device.
* @returns NimBLE stack return code, 0 = success. * @returns NimBLE stack return code, 0 = success.
*/ */
/* STATIC */ /* STATIC */int NimBLEDevice::startSecurity(uint16_t conn_id) {
int NimBLEDevice::startSecurity(uint16_t conn_id) { /* if(m_securityCallbacks != nullptr) {
m_securityCallbacks->onSecurityRequest();
}
*/
int rc = ble_gap_security_initiate(conn_id); int rc = ble_gap_security_initiate(conn_id);
if(rc != 0){ if(rc != 0){
NIMBLE_LOGE(LOG_TAG, "ble_gap_security_initiate: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); NIMBLE_LOGE(LOG_TAG, "ble_gap_security_initiate: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
@ -1103,8 +1027,7 @@ int NimBLEDevice::startSecurity(uint16_t conn_id) {
* @param [in] address The address to look for. * @param [in] address The address to look for.
* @return True if ignoring. * @return True if ignoring.
*/ */
/*STATIC*/ /*STATIC*/ bool NimBLEDevice::isIgnored(const NimBLEAddress &address) {
bool NimBLEDevice::isIgnored(const NimBLEAddress &address) {
for(auto &it : m_ignoreList) { for(auto &it : m_ignoreList) {
if(it.equals(address)){ if(it.equals(address)){
return true; return true;
@ -1119,8 +1042,7 @@ bool NimBLEDevice::isIgnored(const NimBLEAddress &address) {
* @brief Add a device to the ignore list. * @brief Add a device to the ignore list.
* @param [in] address The address of the device we want to ignore. * @param [in] address The address of the device we want to ignore.
*/ */
/*STATIC*/ /*STATIC*/ void NimBLEDevice::addIgnored(const NimBLEAddress &address) {
void NimBLEDevice::addIgnored(const NimBLEAddress &address) {
m_ignoreList.push_back(address); m_ignoreList.push_back(address);
} }
@ -1129,8 +1051,7 @@ void NimBLEDevice::addIgnored(const NimBLEAddress &address) {
* @brief Remove a device from the ignore list. * @brief Remove a device from the ignore list.
* @param [in] address The address of the device we want to remove from the list. * @param [in] address The address of the device we want to remove from the list.
*/ */
/*STATIC*/ /*STATIC*/void NimBLEDevice::removeIgnored(const NimBLEAddress &address) {
void NimBLEDevice::removeIgnored(const NimBLEAddress &address) {
for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) { for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) {
if((*it).equals(address)){ if((*it).equals(address)){
m_ignoreList.erase(it); m_ignoreList.erase(it);
@ -1144,7 +1065,6 @@ void NimBLEDevice::removeIgnored(const NimBLEAddress &address) {
* @brief Set a custom callback for gap events. * @brief Set a custom callback for gap events.
* @param [in] handler The function to call when gap events occur. * @param [in] handler The function to call when gap events occur.
*/ */
/*STATIC*/
void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) { void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) {
m_customGapHandler = handler; m_customGapHandler = handler;
int rc = ble_gap_event_listener_register(&m_listener, m_customGapHandler, NULL); int rc = ble_gap_event_listener_register(&m_listener, m_customGapHandler, NULL);
@ -1156,4 +1076,5 @@ void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) {
} }
} // setCustomGapHandler } // setCustomGapHandler
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View File

@ -14,9 +14,10 @@
#ifndef MAIN_NIMBLEDEVICE_H_ #ifndef MAIN_NIMBLEDEVICE_H_
#define MAIN_NIMBLEDEVICE_H_ #define MAIN_NIMBLEDEVICE_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEScan.h" #include "NimBLEScan.h"
@ -38,9 +39,7 @@
#include "NimBLESecurity.h" #include "NimBLESecurity.h"
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#ifdef ESP_PLATFORM #include "esp_bt.h"
# include "esp_bt.h"
#endif
#include <map> #include <map>
#include <string> #include <string>
@ -111,17 +110,8 @@ public:
static NimBLEServer* getServer(); static NimBLEServer* getServer();
#endif #endif
#ifdef ESP_PLATFORM
static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT); 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 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 setCustomGapHandler(gap_event_handler handler);
static void setSecurityAuth(bool bonding, bool mitm, bool sc); static void setSecurityAuth(bool bonding, bool mitm, bool sc);
static void setSecurityAuth(uint8_t auth_req); static void setSecurityAuth(uint8_t auth_req);
@ -131,12 +121,15 @@ public:
static void setSecurityPasskey(uint32_t pin); static void setSecurityPasskey(uint32_t pin);
static uint32_t getSecurityPasskey(); static uint32_t getSecurityPasskey();
static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks); 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 startSecurity(uint16_t conn_id);
static int setMTU(uint16_t mtu); static int setMTU(uint16_t mtu);
static uint16_t getMTU(); static uint16_t getMTU();
static bool isIgnored(const NimBLEAddress &address); static bool isIgnored(const NimBLEAddress &address);
static void addIgnored(const NimBLEAddress &address); static void addIgnored(const NimBLEAddress &address);
static void removeIgnored(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) #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
static NimBLEAdvertising* getAdvertising(); static NimBLEAdvertising* getAdvertising();
@ -206,10 +199,8 @@ private:
static ble_gap_event_listener m_listener; static ble_gap_event_listener m_listener;
static gap_event_handler m_customGapHandler; static gap_event_handler m_customGapHandler;
static uint8_t m_own_addr_type; static uint8_t m_own_addr_type;
#ifdef ESP_PLATFORM
static uint16_t m_scanDuplicateSize; static uint16_t m_scanDuplicateSize;
static uint8_t m_scanFilterMode; static uint8_t m_scanFilterMode;
#endif
static std::vector<NimBLEAddress> m_whiteList; static std::vector<NimBLEAddress> m_whiteList;
}; };

View File

@ -11,14 +11,12 @@
* Created on: Mar 12, 2018 * Created on: Mar 12, 2018
* Author: pcbreflux * Author: pcbreflux
*/ */
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLEEddystoneTLM.h" #include "NimBLEEddystoneTLM.h"
#include "NimBLELog.h" #include "NimBLELog.h"
#include <stdio.h>
#include <cstring> #include <cstring>
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) #define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
@ -126,30 +124,30 @@ std::string NimBLEEddystoneTLM::toString() {
out += " C\n"; out += " C\n";
out += "Adv. Count "; out += "Adv. Count ";
snprintf(val, sizeof(val), "%" PRIu32, ENDIAN_CHANGE_U32(m_eddystoneData.advCount)); snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
out += val; out += val;
out += "\n"; out += "\n";
out += "Time in seconds "; out += "Time in seconds ";
snprintf(val, sizeof(val), "%" PRIu32, rawsec/10); snprintf(val, sizeof(val), "%d", rawsec/10);
out += val; out += val;
out += "\n"; out += "\n";
out += "Time "; out += "Time ";
snprintf(val, sizeof(val), "%04" PRIu32, rawsec / 864000); snprintf(val, sizeof(val), "%04d", rawsec / 864000);
out += val; out += val;
out += "."; out += ".";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 36000) % 24); snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
out += val; out += val;
out += ":"; out += ":";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 600) % 60); snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
out += val; out += val;
out += ":"; out += ":";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 10) % 60); snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
out += val; out += val;
out += "\n"; out += "\n";

View File

@ -14,7 +14,6 @@
#ifndef _NimBLEEddystoneTLM_H_ #ifndef _NimBLEEddystoneTLM_H_
#define _NimBLEEddystoneTLM_H_ #define _NimBLEEddystoneTLM_H_
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
#include <string> #include <string>

View File

@ -11,7 +11,7 @@
* Created on: Mar 12, 2018 * Created on: Mar 12, 2018
* Author: pcbreflux * Author: pcbreflux
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLEEddystoneURL.h" #include "NimBLEEddystoneURL.h"

View File

@ -11,9 +11,11 @@
* Created on: Jan 03, 2018 * Created on: Jan 03, 2018
* Author: chegewara * Author: chegewara
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEHIDDevice.h" #include "NimBLEHIDDevice.h"
#include "NimBLE2904.h" #include "NimBLE2904.h"
@ -27,7 +29,7 @@ NimBLEHIDDevice::NimBLEHIDDevice(NimBLEServer* server) {
* Here we create mandatory services described in bluetooth specification * Here we create mandatory services described in bluetooth specification
*/ */
m_deviceInfoService = server->createService(NimBLEUUID((uint16_t) 0x180a)); m_deviceInfoService = server->createService(NimBLEUUID((uint16_t) 0x180a));
m_hidService = server->createService(NimBLEUUID((uint16_t) 0x1812)); m_hidService = server->createService(NimBLEUUID((uint16_t) 0x1812), 40);
m_batteryService = server->createService(NimBLEUUID((uint16_t) 0x180f)); m_batteryService = server->createService(NimBLEUUID((uint16_t) 0x180f));
/* /*
@ -245,4 +247,5 @@ NimBLEService* NimBLEHIDDevice::batteryService() {
return m_batteryService; return m_batteryService;
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // #if defined(CONFIG_BT_ENABLED)

View File

@ -15,8 +15,11 @@
#ifndef _BLEHIDDEVICE_H_ #ifndef _BLEHIDDEVICE_H_
#define _BLEHIDDEVICE_H_ #define _BLEHIDDEVICE_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLEService.h" #include "NimBLEService.h"
@ -81,6 +84,6 @@ private:
NimBLECharacteristic* m_protocolModeCharacteristic; //0x2a4e NimBLECharacteristic* m_protocolModeCharacteristic; //0x2a4e
NimBLECharacteristic* m_batteryLevelCharacteristic; //0x2a19 NimBLECharacteristic* m_batteryLevelCharacteristic; //0x2a19
}; };
#endif // CONFIG_BT_NIMBLE_ROLE_BROADCASTER
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER */ #endif // CONFIG_BT_ENABLED
#endif /* _BLEHIDDEVICE_H_ */ #endif /* _BLEHIDDEVICE_H_ */

View File

@ -12,7 +12,49 @@
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF) // using esp-idf #ifdef ARDUINO_ARCH_ESP32
#include "syscfg/syscfg.h"
#include "modlog/modlog.h"
// If Arduino is being used, strip out the colors and ignore log printing below ui setting.
// Note: because CONFIG_LOG_DEFAULT_LEVEL is set at ERROR in Arduino we must use MODLOG_DFLT(ERROR
// otherwise no messages will be printed above that level.
#ifndef CONFIG_NIMBLE_CPP_DEBUG_LEVEL
#ifdef CORE_DEBUG_LEVEL
#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL CORE_DEBUG_LEVEL
#else
#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL 0
#endif
#endif
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
#define NIMBLE_LOGD( tag, format, ... ) MODLOG_DFLT(ERROR, "D %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGD( tag, format, ... ) (void)tag
#endif
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 3
#define NIMBLE_LOGI( tag, format, ... ) MODLOG_DFLT(ERROR, "I %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGI( tag, format, ... ) (void)tag
#endif
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 2
#define NIMBLE_LOGW( tag, format, ... ) MODLOG_DFLT(ERROR, "W %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGW( tag, format, ... ) (void)tag
#endif
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 1
#define NIMBLE_LOGE( tag, format, ... ) MODLOG_DFLT(ERROR, "E %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGE( tag, format, ... ) (void)tag
#endif
#define NIMBLE_LOGC( tag, format, ... ) MODLOG_DFLT(CRITICAL, "CRIT %s: "#format"\n",tag,##__VA_ARGS__)
#else
#include "esp_log.h" #include "esp_log.h"
@ -22,51 +64,7 @@
#define NIMBLE_LOGD(tag, format, ...) ESP_LOGD(tag, format, ##__VA_ARGS__) #define NIMBLE_LOGD(tag, format, ...) ESP_LOGD(tag, format, ##__VA_ARGS__)
#define NIMBLE_LOGC(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__) #define NIMBLE_LOGC(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__)
#else // using Arduino #endif /*ARDUINO_ARCH_ESP32*/
#include "nimble/porting/nimble/include/syscfg/syscfg.h" #endif /*CONFIG_BT_ENABLED*/
#include "nimble/console/console.h" #endif /*MAIN_NIMBLELOG_H_*/
// If Arduino is being used, strip out the colors and ignore log printing below ui setting.
// Note: because CONFIG_LOG_DEFAULT_LEVEL is set at ERROR in Arduino we must use MODLOG_DFLT(ERROR
// otherwise no messages will be printed above that level.
#ifndef NIMBLE_CPP_DEBUG_LEVEL
#if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL)
#define NIMBLE_CPP_DEBUG_LEVEL CORE_DEBUG_LEVEL
#else
#define NIMBLE_CPP_DEBUG_LEVEL 0
#endif
#endif
#if NIMBLE_CPP_DEBUG_LEVEL >= 4
#define NIMBLE_LOGD( tag, format, ... ) console_printf("D %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGD( tag, format, ... ) (void)tag
#endif
#if NIMBLE_CPP_DEBUG_LEVEL >= 3
#define NIMBLE_LOGI( tag, format, ... ) console_printf("I %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGI( tag, format, ... ) (void)tag
#endif
#if NIMBLE_CPP_DEBUG_LEVEL >= 2
#define NIMBLE_LOGW( tag, format, ... ) console_printf("W %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGW( tag, format, ... ) (void)tag
#endif
#if NIMBLE_CPP_DEBUG_LEVEL >= 1
#define NIMBLE_LOGE( tag, format, ... ) console_printf("E %s: "#format"\n",tag,##__VA_ARGS__)
#define NIMBLE_LOGC( tag, format, ... ) console_printf("CRIT %s: "#format"\n",tag,##__VA_ARGS__)
#else
#define NIMBLE_LOGE( tag, format, ... ) (void)tag
#define NIMBLE_LOGC( tag, format, ... ) (void)tag
#endif
#endif /* CONFIG_NIMBLE_CPP_IDF */
#endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLELOG_H_ */

View File

@ -12,8 +12,11 @@
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteCharacteristic.h" #include "NimBLERemoteCharacteristic.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
@ -57,6 +60,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
m_pRemoteService = pRemoteService; m_pRemoteService = pRemoteService;
m_notifyCallback = nullptr; m_notifyCallback = nullptr;
m_timestamp = 0; m_timestamp = 0;
m_valMux = portMUX_INITIALIZER_UNLOCKED;
NIMBLE_LOGD(LOG_TAG, "<< NimBLERemoteCharacteristic(): %s", m_uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, "<< NimBLERemoteCharacteristic(): %s", m_uuid.toString().c_str());
} // NimBLERemoteCharacteristic } // NimBLERemoteCharacteristic
@ -237,7 +241,8 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
} }
int rc = 0; int rc = 0;
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
// If we don't know the end handle of this characteristic retrieve the next one in the service // 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. // The end handle is the next characteristic definition handle -1.
@ -252,6 +257,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (taskData.rc != 0) { if (taskData.rc != 0) {
@ -273,6 +282,10 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (taskData.rc != 0) { if (taskData.rc != 0) {
@ -404,12 +417,12 @@ NimBLEUUID NimBLERemoteCharacteristic::getUUID() {
* @return The value of the remote characteristic. * @return The value of the remote characteristic.
*/ */
std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) { std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&m_valMux);
std::string value = m_value; std::string value = m_value;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
return value; return value;
} }
@ -473,7 +486,8 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
int rc = 0; int rc = 0;
int retryCount = 1; int retryCount = 1;
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(),0, &value}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, &value};
do { do {
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0, rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
@ -485,6 +499,10 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
return value; return value;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
rc = taskData.rc; rc = taskData.rc;
@ -511,13 +529,13 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
} while(rc != 0 && retryCount--); } while(rc != 0 && retryCount--);
time_t t = time(nullptr); time_t t = time(nullptr);
ble_npl_hw_enter_critical(); portENTER_CRITICAL(&m_valMux);
m_value = value; m_value = value;
m_timestamp = t; m_timestamp = t;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
ble_npl_hw_exit_critical(0); portEXIT_CRITICAL(&m_valMux);
NIMBLE_LOGD(LOG_TAG, "<< readValue length: %d rc=%d", value.length(), rc); NIMBLE_LOGD(LOG_TAG, "<< readValue length: %d rc=%d", value.length(), rc);
return value; return value;
@ -547,11 +565,12 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
if(rc == 0) { if(rc == 0) {
if(attr) { if(attr) {
if(((*strBuf).length() + attr->om->om_len) > BLE_ATT_ATTR_MAX_LEN) { uint32_t data_len = OS_MBUF_PKTLEN(attr->om);
if(((*strBuf).length() + data_len) > BLE_ATT_ATTR_MAX_LEN) {
rc = BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; rc = BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
} else { } else {
NIMBLE_LOGD(LOG_TAG, "Got %d bytes", attr->om->om_len); NIMBLE_LOGD(LOG_TAG, "Got %d bytes", data_len);
(*strBuf) += std::string((char*) attr->om->om_data, attr->om->om_len); (*strBuf) += std::string((char*) attr->om->om_data, data_len);
return 0; return 0;
} }
} }
@ -741,7 +760,8 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length,
return (rc==0); return (rc==0);
} }
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
do { do {
if(length > mtu) { if(length > mtu) {
@ -761,6 +781,10 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length,
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
rc = taskData.rc; rc = taskData.rc;
@ -815,4 +839,6 @@ int NimBLERemoteCharacteristic::onWriteCB(uint16_t conn_handle,
return 0; return 0;
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,9 +14,11 @@
#ifndef COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ #ifndef COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
#define COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ #define COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteService.h" #include "NimBLERemoteService.h"
#include "NimBLERemoteDescriptor.h" #include "NimBLERemoteDescriptor.h"
@ -159,10 +161,12 @@ private:
std::string m_value; std::string m_value;
notify_callback m_notifyCallback; notify_callback m_notifyCallback;
time_t m_timestamp; time_t m_timestamp;
portMUX_TYPE m_valMux;
// We maintain a vector of descriptors owned by this characteristic. // We maintain a vector of descriptors owned by this characteristic.
std::vector<NimBLERemoteDescriptor*> m_descriptorVector; std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
}; // NimBLERemoteCharacteristic }; // NimBLERemoteCharacteristic
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */ #endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */

View File

@ -11,9 +11,11 @@
* Created on: Jul 8, 2017 * Created on: Jul 8, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteDescriptor.h" #include "NimBLERemoteDescriptor.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
@ -137,7 +139,8 @@ std::string NimBLERemoteDescriptor::readValue() {
int rc = 0; int rc = 0;
int retryCount = 1; int retryCount = 1;
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(),0, &value}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, &value};
do { do {
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0, rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
@ -149,6 +152,10 @@ std::string NimBLERemoteDescriptor::readValue() {
return value; return value;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
rc = taskData.rc; rc = taskData.rc;
@ -186,7 +193,6 @@ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle,
const struct ble_gatt_error *error, const struct ble_gatt_error *error,
struct ble_gatt_attr *attr, void *arg) struct ble_gatt_attr *attr, void *arg)
{ {
(void)attr;
ble_task_data_t *pTaskData = (ble_task_data_t*)arg; ble_task_data_t *pTaskData = (ble_task_data_t*)arg;
NimBLERemoteDescriptor* desc = (NimBLERemoteDescriptor*)pTaskData->pATT; NimBLERemoteDescriptor* desc = (NimBLERemoteDescriptor*)pTaskData->pATT;
uint16_t conn_id = desc->getRemoteCharacteristic()->getRemoteService()->getClient()->getConnId(); uint16_t conn_id = desc->getRemoteCharacteristic()->getRemoteService()->getClient()->getConnId();
@ -202,11 +208,12 @@ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle,
if(rc == 0) { if(rc == 0) {
if(attr) { if(attr) {
if(((*strBuf).length() + attr->om->om_len) > BLE_ATT_ATTR_MAX_LEN) { uint32_t data_len = OS_MBUF_PKTLEN(attr->om);
if(((*strBuf).length() + data_len) > BLE_ATT_ATTR_MAX_LEN) {
rc = BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; rc = BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
} else { } else {
NIMBLE_LOGD(LOG_TAG, "Got %d bytes", attr->om->om_len); NIMBLE_LOGD(LOG_TAG, "Got %d bytes", data_len);
(*strBuf) += std::string((char*) attr->om->om_data, attr->om->om_len); (*strBuf) += std::string((char*) attr->om->om_data, data_len);
return 0; return 0;
} }
} }
@ -288,7 +295,8 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool
return (rc == 0); return (rc == 0);
} }
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
do { do {
if(length > mtu) { if(length > mtu) {
@ -309,6 +317,10 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
rc = taskData.rc; rc = taskData.rc;
@ -349,4 +361,5 @@ bool NimBLERemoteDescriptor::writeValue(const std::string &newValue, bool respon
return writeValue((uint8_t*) newValue.data(), newValue.length(), response); return writeValue((uint8_t*) newValue.data(), newValue.length(), response);
} // writeValue } // writeValue
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,9 +14,11 @@
#ifndef COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ #ifndef COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
#define COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ #define COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteCharacteristic.h" #include "NimBLERemoteCharacteristic.h"
@ -79,5 +81,6 @@ private:
NimBLERemoteCharacteristic* m_pRemoteCharacteristic; NimBLERemoteCharacteristic* m_pRemoteCharacteristic;
}; };
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */ #endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */

View File

@ -11,9 +11,11 @@
* Created on: Jul 8, 2017 * Created on: Jul 8, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteService.h" #include "NimBLERemoteService.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
@ -196,7 +198,8 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter)
NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());
int rc = 0; int rc = 0;
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
if(uuid_filter == nullptr) { if(uuid_filter == nullptr) {
rc = ble_gattc_disc_all_chrs(m_pClient->getConnId(), rc = ble_gattc_disc_all_chrs(m_pClient->getConnId(),
@ -218,6 +221,10 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter)
return false; return false;
} }
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if(taskData.rc == 0){ if(taskData.rc == 0){
@ -384,4 +391,6 @@ std::string NimBLERemoteService::toString() {
return res; return res;
} // toString } // toString
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */

View File

@ -14,9 +14,11 @@
#ifndef COMPONENTS_NIMBLEREMOTESERVICE_H_ #ifndef COMPONENTS_NIMBLEREMOTESERVICE_H_
#define COMPONENTS_NIMBLEREMOTESERVICE_H_ #define COMPONENTS_NIMBLEREMOTESERVICE_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEClient.h" #include "NimBLEClient.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
@ -81,5 +83,6 @@ private:
uint16_t m_endHandle; uint16_t m_endHandle;
}; // NimBLERemoteService }; // NimBLERemoteService
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */ #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTESERVICE_H_ */ #endif /* COMPONENTS_NIMBLEREMOTESERVICE_H_ */

View File

@ -11,9 +11,11 @@
* Created on: Jul 1, 2017 * Created on: Jul 1, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEScan.h" #include "NimBLEScan.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -356,10 +358,15 @@ NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) {
NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever"); NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever");
} }
ble_task_data_t taskData = {nullptr, xTaskGetCurrentTaskHandle(),0, nullptr}; TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {nullptr, cur_task, 0, nullptr};
m_pTaskData = &taskData; m_pTaskData = &taskData;
if(start(duration, nullptr, is_continue)) { 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); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
@ -532,4 +539,5 @@ NimBLEAdvertisedDevice *NimBLEScanResults::getDevice(const NimBLEAddress &addres
return nullptr; return nullptr;
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#endif /* CONFIG_BT_ENABLED */

View File

@ -13,18 +13,16 @@
*/ */
#ifndef COMPONENTS_NIMBLE_SCAN_H_ #ifndef COMPONENTS_NIMBLE_SCAN_H_
#define COMPONENTS_NIMBLE_SCAN_H_ #define COMPONENTS_NIMBLE_SCAN_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_gap.h" #include "host/ble_gap.h"
#else
#include "nimble/nimble/host/include/host/ble_gap.h"
#endif
#include <vector> #include <vector>
@ -99,5 +97,6 @@ private:
uint8_t m_maxResults; uint8_t m_maxResults;
}; };
#endif /* CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLE_SCAN_H_ */ #endif /* COMPONENTS_NIMBLE_SCAN_H_ */

View File

@ -12,7 +12,7 @@
* Author: chegewara * Author: chegewara
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLESecurity.h" #include "NimBLESecurity.h"

View File

@ -14,16 +14,10 @@
#ifndef COMPONENTS_NIMBLESECURITY_H_ #ifndef COMPONENTS_NIMBLESECURITY_H_
#define COMPONENTS_NIMBLESECURITY_H_ #define COMPONENTS_NIMBLESECURITY_H_
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_gap.h" #include "host/ble_gap.h"
#else
#include "nimble/nimble/host/include/host/ble_gap.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
#undef max #undef max

View File

@ -12,20 +12,19 @@
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEServer.h" #include "NimBLEServer.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
#include "NimBLELog.h" #include "NimBLELog.h"
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "services/gap/ble_svc_gap.h" #include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h" #include "services/gatt/ble_svc_gatt.h"
#else
#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
static const char* LOG_TAG = "NimBLEServer"; static const char* LOG_TAG = "NimBLEServer";
static NimBLEServerCallbacks defaultCallbacks; static NimBLEServerCallbacks defaultCallbacks;
@ -80,17 +79,18 @@ NimBLEService* NimBLEServer::createService(const char* uuid) {
* to provide inst_id value different for each service. * to provide inst_id value different for each service.
* @return A reference to the new service object. * @return A reference to the new service object.
*/ */
NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid) { NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid, uint32_t numHandles, uint8_t inst_id) {
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
// TODO: add functionality to use inst_id for multiple services with same uuid
(void)inst_id;
// Check that a service with the supplied UUID does not already exist. // Check that a service with the supplied UUID does not already exist.
if(getServiceByUUID(uuid) != nullptr) { if(getServiceByUUID(uuid) != nullptr) {
NIMBLE_LOGW(LOG_TAG, "Warning creating a duplicate service UUID: %s", NIMBLE_LOGW(LOG_TAG, "Warning creating a duplicate service UUID: %s",
std::string(uuid).c_str()); std::string(uuid).c_str());
} }
NimBLEService* pService = new NimBLEService(uuid); NimBLEService* pService = new NimBLEService(uuid, numHandles, this);
m_svcVec.push_back(pService); m_svcVec.push_back(pService); // Save a reference to this service being on this server.
serviceChanged(); serviceChanged();
NIMBLE_LOGD(LOG_TAG, "<< createService"); NIMBLE_LOGD(LOG_TAG, "<< createService");
@ -181,7 +181,7 @@ void NimBLEServer::start() {
abort(); abort();
} }
#if CONFIG_LOG_DEFAULT_LEVEL > 3 || (ARDUINO_ARCH_ESP32 && CORE_DEBUG_LEVEL >= 4) #if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
ble_gatts_show_local(); ble_gatts_show_local();
#endif #endif
/*** Future use *** /*** Future use ***
@ -734,7 +734,7 @@ void NimBLEServer::startAdvertising() {
*/ */
void NimBLEServer::stopAdvertising() { void NimBLEServer::stopAdvertising() {
NimBLEDevice::stopAdvertising(); NimBLEDevice::stopAdvertising();
} // stopAdvertising } // startAdvertising
/** /**
@ -772,30 +772,7 @@ void NimBLEServer::updateConnParams(uint16_t conn_handle,
if(rc != 0) { if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
} }
} // updateConnParams }// updateConnParams
/**
* @brief Request an update of the data packet length.
* * Can only be used after a connection has been established.
* @details Sends a data length update request to the peer.
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
* The peer needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
* @param [in] conn_handle The connection handle of the peer to send the request to.
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
*/
void NimBLEServer::setDataLen(uint16_t conn_handle, uint16_t tx_octets) {
#ifdef CONFIG_NIMBLE_CPP_IDF // not yet available in IDF, Sept 9 2021
return;
#else
uint16_t tx_time = (tx_octets + 14) * 8;
int rc = ble_gap_set_data_len(conn_handle, tx_octets, tx_time);
if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
}
#endif
} // setDataLen
bool NimBLEServer::setIndicateWait(uint16_t conn_handle) { bool NimBLEServer::setIndicateWait(uint16_t conn_handle) {
@ -865,4 +842,6 @@ bool NimBLEServerCallbacks::onConfirmPIN(uint32_t pin){
return true; return true;
} }
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED

View File

@ -14,9 +14,11 @@
#ifndef MAIN_NIMBLESERVER_H_ #ifndef MAIN_NIMBLESERVER_H_
#define MAIN_NIMBLESERVER_H_ #define MAIN_NIMBLESERVER_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#define NIMBLE_ATT_REMOVE_HIDE 1 #define NIMBLE_ATT_REMOVE_HIDE 1
#define NIMBLE_ATT_REMOVE_DELETE 2 #define NIMBLE_ATT_REMOVE_DELETE 2
@ -41,7 +43,8 @@ class NimBLEServer {
public: public:
size_t getConnectedCount(); size_t getConnectedCount();
NimBLEService* createService(const char* uuid); NimBLEService* createService(const char* uuid);
NimBLEService* createService(const NimBLEUUID &uuid); NimBLEService* createService(const NimBLEUUID &uuid, uint32_t numHandles=15,
uint8_t inst_id=0);
void removeService(NimBLEService* service, bool deleteSvc = false); void removeService(NimBLEService* service, bool deleteSvc = false);
void addService(NimBLEService* service); void addService(NimBLEService* service);
NimBLEAdvertising* getAdvertising(); NimBLEAdvertising* getAdvertising();
@ -58,7 +61,6 @@ public:
void updateConnParams(uint16_t conn_handle, void updateConnParams(uint16_t conn_handle,
uint16_t minInterval, uint16_t maxInterval, uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout); uint16_t latency, uint16_t timeout);
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
uint16_t getPeerMTU(uint16_t conn_id); uint16_t getPeerMTU(uint16_t conn_id);
std::vector<uint16_t> getPeerDevices(); std::vector<uint16_t> getPeerDevices();
NimBLEConnInfo getPeerInfo(size_t index); NimBLEConnInfo getPeerInfo(size_t index);
@ -166,5 +168,7 @@ public:
virtual bool onConfirmPIN(uint32_t pin); virtual bool onConfirmPIN(uint32_t pin);
}; // NimBLEServerCallbacks }; // NimBLEServerCallbacks
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLESERVER_H_ */ #endif /* MAIN_NIMBLESERVER_H_ */

View File

@ -14,10 +14,12 @@
// A service is identified by a UUID. A service is also the container for one or more characteristics. // A service is identified by a UUID. A service is also the container for one or more characteristics.
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEDevice.h"
#include "NimBLEService.h" #include "NimBLEService.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -32,19 +34,25 @@ static const char* LOG_TAG = "NimBLEService"; // Tag for logging.
/** /**
* @brief Construct an instance of the NimBLEService * @brief Construct an instance of the NimBLEService
* @param [in] uuid The UUID of the service. * @param [in] uuid The UUID of the service.
* @param [in] numHandles The maximum number of handles associated with the service.
* @param [in] pServer A pointer to the server instance that this service belongs to.
*/ */
NimBLEService::NimBLEService(const char* uuid) NimBLEService::NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer* pServer)
: NimBLEService(NimBLEUUID(uuid)) { : NimBLEService(NimBLEUUID(uuid), numHandles, pServer) {
} }
/** /**
* @brief Construct an instance of the BLEService * @brief Construct an instance of the BLEService
* @param [in] uuid The UUID of the service. * @param [in] uuid The UUID of the service.
* @param [in] numHandles The maximum number of handles associated with the service.
* @param [in] pServer A pointer to the server instance that this service belongs to.
*/ */
NimBLEService::NimBLEService(const NimBLEUUID &uuid) { NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer) {
m_uuid = uuid; m_uuid = uuid;
m_handle = NULL_HANDLE; m_handle = NULL_HANDLE;
m_pServer = pServer;
m_numHandles = numHandles;
m_pSvcDef = nullptr; m_pSvcDef = nullptr;
m_removed = 0; m_removed = 0;
@ -421,7 +429,8 @@ std::string NimBLEService::toString() {
* @return The BLEServer associated with this service. * @return The BLEServer associated with this service.
*/ */
NimBLEServer* NimBLEService::getServer() { NimBLEServer* NimBLEService::getServer() {
return NimBLEDevice::getServer(); return m_pServer;
}// getServer }// getServer
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED

View File

@ -14,9 +14,11 @@
#ifndef MAIN_NIMBLESERVICE_H_ #ifndef MAIN_NIMBLESERVICE_H_
#define MAIN_NIMBLESERVICE_H_ #define MAIN_NIMBLESERVICE_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEServer.h" #include "NimBLEServer.h"
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
@ -34,8 +36,8 @@ class NimBLECharacteristic;
class NimBLEService { class NimBLEService {
public: public:
NimBLEService(const char* uuid); NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer* pServer);
NimBLEService(const NimBLEUUID &uuid); NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer);
~NimBLEService(); ~NimBLEService();
NimBLEServer* getServer(); NimBLEServer* getServer();
@ -74,12 +76,16 @@ private:
friend class NimBLEDevice; friend class NimBLEDevice;
uint16_t m_handle; uint16_t m_handle;
NimBLEServer* m_pServer;
NimBLEUUID m_uuid; NimBLEUUID m_uuid;
uint16_t m_numHandles;
ble_gatt_svc_def* m_pSvcDef; ble_gatt_svc_def* m_pSvcDef;
uint8_t m_removed; uint8_t m_removed;
std::vector<NimBLECharacteristic*> m_chrVec; std::vector<NimBLECharacteristic*> m_chrVec;
}; // NimBLEService }; // NimBLEService
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED
#endif /* MAIN_NIMBLESERVICE_H_ */ #endif /* MAIN_NIMBLESERVICE_H_ */

View File

@ -11,8 +11,7 @@
* Created on: Jun 21, 2017 * Created on: Jun 21, 2017
* Author: kolban * Author: kolban
*/ */
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLEUtils.h" #include "NimBLEUtils.h"

View File

@ -14,16 +14,10 @@
#ifndef COMPONENTS_NIMBLEUUID_H_ #ifndef COMPONENTS_NIMBLEUUID_H_
#define COMPONENTS_NIMBLEUUID_H_ #define COMPONENTS_NIMBLEUUID_H_
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_uuid.h" #include "host/ble_uuid.h"
#else
#include "nimble/nimble/host/include/host/ble_uuid.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
#undef max #undef max

View File

@ -6,13 +6,12 @@
* *
*/ */
#include "nimconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
#include "nimconfig.h"
#include <stdlib.h>
static const char* LOG_TAG = "NimBLEUtils"; static const char* LOG_TAG = "NimBLEUtils";
@ -343,7 +342,6 @@ const char* NimBLEUtils::returnCodeToString(int rc) {
return "Unknown"; return "Unknown";
} }
#else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT) #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT)
(void)rc;
return ""; return "";
#endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT) #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT)
} }
@ -371,7 +369,6 @@ const char* NimBLEUtils::advTypeToString(uint8_t advType) {
return "Unknown flag"; return "Unknown flag";
} }
#else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT) #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT)
(void)advType;
return ""; return "";
#endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT) #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT)
} // adFlagsToString } // adFlagsToString
@ -419,11 +416,8 @@ char* NimBLEUtils::buildHexData(uint8_t* target, const uint8_t* source, uint8_t
* @param [in] arg Unused. * @param [in] arg Unused.
*/ */
void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){ void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){
(void)arg;
#if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
NIMBLE_LOGD(LOG_TAG, "Received a GAP event: %s", gapEventToString(event->type)); NIMBLE_LOGD(LOG_TAG, "Received a GAP event: %s", gapEventToString(event->type));
#else
(void)event;
#endif #endif
} }
@ -510,7 +504,6 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
return "Unknown event type"; return "Unknown event type";
} }
#else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
(void)eventType;
return ""; return "";
#endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
} // gapEventToString } // gapEventToString

View File

@ -8,15 +8,10 @@
#ifndef COMPONENTS_NIMBLEUTILS_H_ #ifndef COMPONENTS_NIMBLEUTILS_H_
#define COMPONENTS_NIMBLEUTILS_H_ #define COMPONENTS_NIMBLEUTILS_H_
#include "sdkconfig.h"
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_NIMBLE_CPP_IDF)
#include "host/ble_gap.h" #include "host/ble_gap.h"
#else
#include "nimble/nimble/host/include/host/ble_gap.h"
#endif
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min

View File

@ -23,7 +23,7 @@
## Overview ## Overview
Apache NimBLE is an open-source Bluetooth 5.1 stack (both Host & Controller) Apache NimBLE is an open-source Bluetooth 5.0 stack (both Host & Controller)
that completely replaces the proprietary SoftDevice on Nordic chipsets. It is that completely replaces the proprietary SoftDevice on Nordic chipsets. It is
part of [Apache Mynewt project](https://github.com/apache/mynewt-core). part of [Apache Mynewt project](https://github.com/apache/mynewt-core).
@ -33,6 +33,7 @@ Features highlight:
- Support for up to 32 simultaneous connections. - Support for up to 32 simultaneous connections.
- Legacy and SC (secure connections) SMP support (pairing and bonding). - Legacy and SC (secure connections) SMP support (pairing and bonding).
- Advertising Extensions. - Advertising Extensions.
- Periodic Advertising.
- Coded (aka Long Range) and 2M PHYs. - Coded (aka Long Range) and 2M PHYs.
- Bluetooth Mesh. - Bluetooth Mesh.
@ -83,24 +84,27 @@ There are also some sample applications that show how to Apache Mynewt NimBLE
stack. These sample applications are located in the `apps/` directory of stack. These sample applications are located in the `apps/` directory of
Apache Mynewt [repo](https://github.com/apache/mynewt-core). Some examples: Apache Mynewt [repo](https://github.com/apache/mynewt-core). Some examples:
* [blecent](https://github.com/apache/mynewt-nimble/tree/master/apps/blecent): * [blecent](https://github.com/apache/mynewt-core/tree/master/apps/blecent):
A basic central device with no user interface. This application scans for A basic central device with no user interface. This application scans for
a peripheral that supports the alert notification service (ANS). Upon a peripheral that supports the alert notification service (ANS). Upon
discovering such a peripheral, blecent connects and performs a characteristic discovering such a peripheral, blecent connects and performs a characteristic
read, characteristic write, and notification subscription. read, characteristic write, and notification subscription.
* [blehci](https://github.com/apache/mynewt-nimble/tree/master/apps/blehci): * [blehci](https://github.com/apache/mynewt-core/tree/master/apps/blehci):
Implements a BLE controller-only application. A separate host-only Implements a BLE controller-only application. A separate host-only
implementation, such as Linux's BlueZ, can interface with this application via implementation, such as Linux's BlueZ, can interface with this application via
HCI over UART. HCI over UART.
* [bleprph](https://github.com/apache/mynewt-nimble/tree/master/apps/bleprph): An * [bleprph](https://github.com/apache/mynewt-core/tree/master/apps/bleprph): An
implementation of a minimal BLE peripheral. implementation of a minimal BLE peripheral.
* [btshell](https://github.com/apache/mynewt-nimble/tree/master/apps/btshell): A * [btshell](https://github.com/apache/mynewt-core/tree/master/apps/btshell): A
shell-like application allowing to configure and use most of NimBLE shell-like application allowing to configure and use most of NimBLE
functionality from command line. functionality from command line.
* [bleuart](https://github.com/apache/mynewt-core/tree/master/apps/bleuart): * [bleuart](https://github.com/apache/mynewt-core/tree/master/apps/bleuart):
Implements a simple BLE peripheral that supports the Nordic Implements a simple BLE peripheral that supports the Nordic
UART / Serial Port Emulation service UART / Serial Port Emulation service
(https://developer.nordicsemi.com/nRF5_SDK/nRF51_SDK_v8.x.x/doc/8.0.0/s110/html/a00072.html). (https://developer.nordicsemi.com/nRF5_SDK/nRF51_SDK_v8.x.x/doc/8.0.0/s110/html/a00072.html).
* [test](https://github.com/apache/mynewt-core/tree/master/apps/test): Test
project which can be compiled either with the simulator, or on a per-architecture basis.
Test will run all the package's unit tests.
# Getting Help # Getting Help
@ -109,7 +113,7 @@ want to talk to a human about what you're working on, you can contact us via the
[developers mailing list](mailto:dev@mynewt.apache.org). [developers mailing list](mailto:dev@mynewt.apache.org).
Although not a formal channel, you can also find a number of core developers Although not a formal channel, you can also find a number of core developers
on the #mynewt channel on Freenode IRC or #general channel on [Mynewt Slack](https://mynewt.slack.com/join/shared_invite/enQtNjA1MTg0NzgyNzg3LTcyMmZiOGQzOGMxM2U4ODFmMTIwNjNmYTE5Y2UwYjQwZWIxNTE0MTUzY2JmMTEzOWFjYWZkNGM0YmM4MzAxNWQ) on the #mynewt channel on Freenode IRC or #general channel on [Mynewt Slack](https://join.slack.com/mynewt/shared_invite/MTkwMTg1ODM1NTg5LTE0OTYxNzQ4NzQtZTU1YmNhYjhkMg)
Also, be sure to checkout the [Frequently Asked Questions](https://mynewt.apache.org/faq/answers) Also, be sure to checkout the [Frequently Asked Questions](https://mynewt.apache.org/faq/answers)
for some help troubleshooting first. for some help troubleshooting first.

View File

@ -1,32 +1,26 @@
# RELEASE NOTES # RELEASE NOTES
18 March 2020 - Apache NimBLE v1.3.0 16 July 2019 - Apache NimBLE v1.2.0
For full release notes, please visit the For full release notes, please visit the
[Apache Mynewt Wiki](https://cwiki.apache.org/confluence/display/MYNEWT/Release+Notes). [Apache Mynewt Wiki](https://cwiki.apache.org/confluence/display/MYNEWT/Release+Notes).
Apache NimBLE is an open-source Bluetooth 5.1 stack (both Host & Controller) that completely Apache NimBLE is an open-source Bluetooth 5.0 stack (both Host & Controller) that completely
replaces the proprietary SoftDevice on Nordic chipsets. replaces the proprietary SoftDevice on Nordic chipsets.
New features in this version of NimBLE include: New features in this version of NimBLE include:
* Support for Bluetooth Core Specification 5.1 * Perdiodic Advertising support with up to 1650 bytes of data (scanner and advertiser)
* New blestress test application * Support for scan request notification in GAP API
* Dialog DA1469x CMAC driver * Updated host qualification ID
* Support for LE Secure Connections out-of-band (OOB) association model
* Support for automated generation of syscfg for ports
* Qualification related bugfixes * Qualification related bugfixes
* GAP API doxygen documentation update
* BLE Mesh improvements - fixes and resync with latest Zephyr code * BLE Mesh improvements - fixes and resync with latest Zephyr code
* RIOT OS port fixes and improvements * RIOT OS port fixes and improvements
* btshell sample application improvements * btshell sample application improvements
* improvements for bttester application * improvements for bttester application
* Controller duplicates filtering improvements * Controller duplicates filtering improvements
* Multi PHY support improvements * Memory and CPU usage optimizations in controller
* Memory and CPU usage optimizations
* Use of packed structs for HCI (code size reduction)
* Linux sample improvements
* PTS test instructions updates
* Clock managements improvements in controller
If working on next-generation RTOS and Bluetooth protocol stack If working on next-generation RTOS and Bluetooth protocol stack
sounds exciting to you, get in touch, by sending a mail to the Apache Mynewt sounds exciting to you, get in touch, by sending a mail to the Apache Mynewt

View File

@ -0,0 +1,21 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
#ifndef _CONSOLE_H
#define _CONSOLE_H
#include <stdio.h>
#define console_printf printf
#endif

View File

@ -19,19 +19,17 @@
* under the License. * under the License.
*/ */
#ifdef ESP_PLATFORM
#include <assert.h> #include <assert.h>
#include "nimble/porting/nimble/include/sysinit/sysinit.h" #include "sysinit/sysinit.h"
#include "nimble/nimble/include/nimble/hci_common.h" #include "nimble/hci_common.h"
#include "nimble/nimble/host/include/host/ble_hs.h" #include "host/ble_hs.h"
#include "nimble/porting/nimble/include/nimble/nimble_port.h" #include "nimble/nimble_port.h"
#include "nimble/porting/npl/freertos/include/nimble/nimble_port_freertos.h" #include "nimble/nimble_port_freertos.h"
#include "../include/esp_nimble_hci.h" #include "esp_nimble_hci.h"
#include "../../port/include/esp_nimble_mem.h" #include "esp_nimble_mem.h"
#include <esp_bt.h> #include "esp_bt.h"
#include <freertos/semphr.h> #include "freertos/semphr.h"
#include "../include/esp_compiler.h" #include "esp_compiler.h"
/* IPC is used to improve performance when calls come from a processor not running the NimBLE stack */ /* IPC is used to improve performance when calls come from a processor not running the NimBLE stack */
/* but does not exist for solo */ /* but does not exist for solo */
#ifndef CONFIG_FREERTOS_UNICORE #ifndef CONFIG_FREERTOS_UNICORE
@ -312,6 +310,7 @@ static struct os_mbuf *ble_hci_trans_acl_buf_alloc(void)
static void ble_hci_rx_acl(uint8_t *data, uint16_t len) static void ble_hci_rx_acl(uint8_t *data, uint16_t len)
{ {
struct os_mbuf *m; struct os_mbuf *m;
int rc;
int sr; int sr;
if (len < BLE_HCI_DATA_HDR_SZ || len > MYNEWT_VAL(BLE_ACL_BUF_SIZE)) { if (len < BLE_HCI_DATA_HDR_SZ || len > MYNEWT_VAL(BLE_ACL_BUF_SIZE)) {
return; return;
@ -320,9 +319,11 @@ static void ble_hci_rx_acl(uint8_t *data, uint16_t len)
m = ble_hci_trans_acl_buf_alloc(); m = ble_hci_trans_acl_buf_alloc();
if (!m) { if (!m) {
ESP_LOGE(TAG, "%s failed to allocate ACL buffers; increase ACL_BUF_COUNT", __func__);
return; return;
} }
if (os_mbuf_append(m, data, len)) { if ((rc = os_mbuf_append(m, data, len)) != 0) {
ESP_LOGE(TAG, "%s failed to os_mbuf_append; rc = %d", __func__, rc);
os_mbuf_free_chain(m); os_mbuf_free_chain(m);
return; return;
} }
@ -530,6 +531,7 @@ esp_err_t esp_nimble_hci_and_controller_init(void)
if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) { if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) {
return ret; return ret;
} }
return esp_nimble_hci_init(); return esp_nimble_hci_init();
} }
@ -591,5 +593,3 @@ esp_err_t esp_nimble_hci_and_controller_deinit(void)
return ESP_OK; return ESP_OK;
} }
#endif

View File

@ -0,0 +1,33 @@
// Copyright 2016-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ESP_COMPILER_H
#define __ESP_COMPILER_H
/*
* The likely and unlikely macro pairs:
* These macros are useful to place when application
* knows the majority ocurrence of a decision paths,
* placing one of these macros can hint the compiler
* to reorder instructions producing more optimized
* code.
*/
#if (CONFIG_COMPILER_OPTIMIZATION_PERF)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#endif

View File

@ -19,11 +19,10 @@
* under the License. * under the License.
*/ */
#ifdef ESP_PLATFORM
#ifndef __ESP_NIMBLE_HCI_H__ #ifndef __ESP_NIMBLE_HCI_H__
#define __ESP_NIMBLE_HCI_H__ #define __ESP_NIMBLE_HCI_H__
#include "nimble/nimble/include/nimble/ble_hci_trans.h" #include "nimble/ble_hci_trans.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -137,4 +136,3 @@ esp_err_t esp_nimble_hci_and_controller_deinit(void);
#endif #endif
#endif /* __ESP_NIMBLE_HCI_H__ */ #endif /* __ESP_NIMBLE_HCI_H__ */
#endif

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/aes.h" #include <tinycrypt/aes.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
static const uint8_t inv_sbox[256] = { static const uint8_t inv_sbox[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/aes.h" #include <tinycrypt/aes.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
static const uint8_t sbox[256] = { static const uint8_t sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/cbc_mode.h" #include <tinycrypt/cbc_mode.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in, int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
unsigned int inlen, const uint8_t *iv, unsigned int inlen, const uint8_t *iv,

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/ccm_mode.h" #include <tinycrypt/ccm_mode.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
#include <stdio.h> #include <stdio.h>

View File

@ -30,10 +30,10 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/aes.h" #include <tinycrypt/aes.h>
#include "../include/tinycrypt/cmac_mode.h" #include <tinycrypt/cmac_mode.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
/* max number of calls until change the key (2^48).*/ /* max number of calls until change the key (2^48).*/
const static uint64_t MAX_CALLS = ((uint64_t)1 << 48); const static uint64_t MAX_CALLS = ((uint64_t)1 << 48);

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/ctr_mode.h" #include <tinycrypt/ctr_mode.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in, int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched) unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched)

View File

@ -27,9 +27,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/ctr_prng.h" #include <tinycrypt/ctr_prng.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include <string.h> #include <string.h>
/* /*

View File

@ -52,8 +52,8 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/ecc.h" #include <tinycrypt/ecc.h>
#include "../include/tinycrypt/ecc_platform_specific.h" #include <tinycrypt/ecc_platform_specific.h>
#include <string.h> #include <string.h>
/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform /* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform

View File

@ -54,9 +54,9 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/ecc.h" #include <tinycrypt/ecc.h>
#include "../include/tinycrypt/ecc_dh.h" #include <tinycrypt/ecc_dh.h>
#include <string.h> #include <string.h>
#if default_RNG_defined #if default_RNG_defined

View File

@ -53,9 +53,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/ecc.h" #include <tinycrypt/ecc.h>
#include "../include/tinycrypt/ecc_dsa.h" #include <tinycrypt/ecc_dsa.h>
#if default_RNG_defined #if default_RNG_defined
static uECC_RNG_Function g_rng_function = &default_CSPRNG; static uECC_RNG_Function g_rng_function = &default_CSPRNG;

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/hmac.h" #include <tinycrypt/hmac.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
static void rekey(uint8_t *key, const uint8_t *new_key, unsigned int key_size) static void rekey(uint8_t *key, const uint8_t *new_key, unsigned int key_size)
{ {

View File

@ -30,10 +30,10 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/hmac_prng.h" #include <tinycrypt/hmac_prng.h>
#include "../include/tinycrypt/hmac.h" #include <tinycrypt/hmac.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
/* /*
* min bytes in the seed string. * min bytes in the seed string.

View File

@ -30,9 +30,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/sha256.h" #include <tinycrypt/sha256.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
static void compress(unsigned int *iv, const uint8_t *data); static void compress(unsigned int *iv, const uint8_t *data);

View File

@ -30,8 +30,8 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "../include/tinycrypt/utils.h" #include <tinycrypt/utils.h>
#include "../include/tinycrypt/constants.h" #include <tinycrypt/constants.h>
#include <string.h> #include <string.h>

View File

@ -29,7 +29,7 @@
#define H_HAL_TIMER_ #define H_HAL_TIMER_
#include <inttypes.h> #include <inttypes.h>
#include "../os/queue.h" #include "os/queue.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -27,7 +27,7 @@
* @{ * @{
*/ */
#include "nimble/porting/nimble/include/os/queue.h" #include "os/queue.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -28,9 +28,9 @@
*/ */
#include <inttypes.h> #include <inttypes.h>
#include "ble_hs.h" #include "host/ble_hs.h"
#include "ble_hs_adv.h" #include "host/ble_hs_adv.h"
#include "nimble/porting/nimble/include/syscfg/syscfg.h" #include "syscfg/syscfg.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -1821,22 +1821,6 @@ int ble_gap_wl_tx_rmv(const ble_addr_t *addrs);
int ble_gap_update_params(uint16_t conn_handle, int ble_gap_update_params(uint16_t conn_handle,
const struct ble_gap_upd_params *params); const struct ble_gap_upd_params *params);
/**
* Configure LE Data Length in controller (OGF = 0x08, OCF = 0x0022).
*
* @param conn_handle Connection handle.
* @param tx_octets The preferred value of payload octets that the Controller
* should use for a new connection (Range
* 0x001B-0x00FB).
* @param tx_time The preferred maximum number of microseconds that the local Controller
* should use to transmit a single link layer packet
* (Range 0x0148-0x4290).
*
* @return 0 on success,
* other error code on failure.
*/
int ble_gap_set_data_len(uint16_t conn_handle, uint16_t tx_octets, uint16_t tx_time);
/** /**
* Initiates the GAP security procedure. * Initiates the GAP security procedure.
* *

View File

@ -28,8 +28,8 @@
*/ */
#include <inttypes.h> #include <inttypes.h>
#include "ble_att.h" #include "host/ble_att.h"
#include "ble_uuid.h" #include "host/ble_uuid.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -27,23 +27,23 @@
*/ */
#include <inttypes.h> #include <inttypes.h>
#include "nimble/nimble/include/nimble/hci_common.h" #include "nimble/hci_common.h"
#include "ble_att.h" #include "host/ble_att.h"
#include "ble_eddystone.h" #include "host/ble_eddystone.h"
#include "ble_gap.h" #include "host/ble_gap.h"
#include "ble_gatt.h" #include "host/ble_gatt.h"
#include "ble_hs_adv.h" #include "host/ble_hs_adv.h"
#include "ble_hs_id.h" #include "host/ble_hs_id.h"
#include "ble_hs_hci.h" #include "host/ble_hs_hci.h"
#include "ble_hs_log.h" #include "host/ble_hs_log.h"
#include "ble_hs_mbuf.h" #include "host/ble_hs_mbuf.h"
#include "ble_hs_stop.h" #include "host/ble_hs_stop.h"
#include "ble_ibeacon.h" #include "host/ble_ibeacon.h"
#include "ble_l2cap.h" #include "host/ble_l2cap.h"
#include "ble_sm.h" #include "host/ble_sm.h"
#include "ble_store.h" #include "host/ble_store.h"
#include "ble_uuid.h" #include "host/ble_uuid.h"
#include "nimble/nimble/include/nimble/nimble_npl.h" #include "nimble/nimble_npl.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -21,7 +21,7 @@
#define H_BLE_HS_ADV_ #define H_BLE_HS_ADV_
#include <inttypes.h> #include <inttypes.h>
#include "ble_uuid.h" #include "host/ble_uuid.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -28,7 +28,7 @@
*/ */
#include <inttypes.h> #include <inttypes.h>
#include "nimble/nimble/include/nimble/ble.h" #include "nimble/ble.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -20,11 +20,11 @@
#ifndef H_BLE_HS_LOG_ #ifndef H_BLE_HS_LOG_
#define H_BLE_HS_LOG_ #define H_BLE_HS_LOG_
#include "nimble/porting/nimble/include/modlog/modlog.h" #include "modlog/modlog.h"
/* Only include the logcfg header if this version of newt can generate it. */ /* Only include the logcfg header if this version of newt can generate it. */
#if MYNEWT_VAL(NEWT_FEATURE_LOGCFG) #if MYNEWT_VAL(NEWT_FEATURE_LOGCFG)
#include "nimble/porting/nimble/include/logcfg/logcfg.h" #include "logcfg/logcfg.h"
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -22,7 +22,7 @@
#ifndef H_BLE_HS_PVCY_ #ifndef H_BLE_HS_PVCY_
#define H_BLE_HS_PVCY_ #define H_BLE_HS_PVCY_
#include "ble_hs.h" #include "host/ble_hs.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -20,7 +20,7 @@
#ifndef H_BLE_L2CAP_ #ifndef H_BLE_L2CAP_
#define H_BLE_L2CAP_ #define H_BLE_L2CAP_
#include "nimble/nimble/include/nimble/nimble_opt.h" #include "nimble/nimble_opt.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -20,7 +20,7 @@
#ifndef H_BLE_MONITOR_ #ifndef H_BLE_MONITOR_
#define H_BLE_MONITOR_ #define H_BLE_MONITOR_
#include "nimble/porting/nimble/include/syscfg/syscfg.h" #include <syscfg/syscfg.h>
#undef BLE_MONITOR #undef BLE_MONITOR
#define BLE_MONITOR (MYNEWT_VAL(BLE_MONITOR_UART) || MYNEWT_VAL(BLE_MONITOR_RTT)) #define BLE_MONITOR (MYNEWT_VAL(BLE_MONITOR_UART) || MYNEWT_VAL(BLE_MONITOR_RTT))

View File

@ -21,7 +21,7 @@
#define H_BLE_SM_ #define H_BLE_SM_
#include <inttypes.h> #include <inttypes.h>
#include "nimble/porting/nimble/include/syscfg/syscfg.h" #include "syscfg/syscfg.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -21,7 +21,7 @@
#define H_BLE_STORE_ #define H_BLE_STORE_
#include <inttypes.h> #include <inttypes.h>
#include "nimble/nimble/include/nimble/ble.h" #include "nimble/ble.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -20,7 +20,7 @@
#ifndef H_LOG_COMMON_ #ifndef H_LOG_COMMON_
#define H_LOG_COMMON_ #define H_LOG_COMMON_
#include "ignore.h" #include "log_common/ignore.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -5,8 +5,8 @@
#ifndef H_MYNEWT_LOGCFG_ #ifndef H_MYNEWT_LOGCFG_
#define H_MYNEWT_LOGCFG_ #define H_MYNEWT_LOGCFG_
#include "../modlog/modlog.h" #include "modlog/modlog.h"
#include "../log_common/log_common.h" #include "log_common/log_common.h"
#if (MYNEWT_VAL(BLE_HS_LOG_LVL) == LOG_LEVEL_DEBUG) #if (MYNEWT_VAL(BLE_HS_LOG_LVL) == LOG_LEVEL_DEBUG)
#define BLE_HS_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__) #define BLE_HS_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__)
@ -130,7 +130,6 @@
#define BLE_MESH_TRANS_LOG_ERROR(...) MODLOG_ERROR(21, __VA_ARGS__) #define BLE_MESH_TRANS_LOG_ERROR(...) MODLOG_ERROR(21, __VA_ARGS__)
#define BLE_MESH_TRANS_LOG_CRITICAL(...) MODLOG_CRITICAL(21, __VA_ARGS__) #define BLE_MESH_TRANS_LOG_CRITICAL(...) MODLOG_CRITICAL(21, __VA_ARGS__)
#define BLE_MESH_TRANS_LOG_DISABLED(...) MODLOG_DISABLED(21, __VA_ARGS__) #define BLE_MESH_TRANS_LOG_DISABLED(...) MODLOG_DISABLED(21, __VA_ARGS__)
#define DFLT_LOG_DEBUG(...) IGNORE(__VA_ARGS__) #define DFLT_LOG_DEBUG(...) IGNORE(__VA_ARGS__)
#define DFLT_LOG_INFO(...) MODLOG_INFO(0, __VA_ARGS__) #define DFLT_LOG_INFO(...) MODLOG_INFO(0, __VA_ARGS__)
#define DFLT_LOG_WARN(...) MODLOG_WARN(0, __VA_ARGS__) #define DFLT_LOG_WARN(...) MODLOG_WARN(0, __VA_ARGS__)

Some files were not shown because too many files have changed in this diff Show More