Conditionally compile code for specific roles.

This allows NimBLE options in menuconfig to reduce code size based on
the roles selected (scan/advertising/central/peripheral).

Significant code space can be saved by removing unnecessary roles for the application.
This commit is contained in:
h2zero 2020-05-13 22:03:56 -06:00
parent 3d6f8b691e
commit 03cb7b21d9
53 changed files with 2335 additions and 2012 deletions

27
Kconfig Normal file
View file

@ -0,0 +1,27 @@
menu "ESP-NimBLE-CPP configuration"
config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT
bool "Show NimBLE return codes as text in debug log."
default "n"
help
Enabling this option will display return code values as text
messages in the debug log. This will use approximately 8kB
of flash memory.
config NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT
bool "Show NimBLE gap events as text in debug log."
default "n"
help
Enabling this option will display gap event codes as text
messages in the debug log. This will use approximately 1kB
of flash memory.
config NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT
bool "Show advertisment types as text in debug log."
default "n"
help
Enabling this option will display advertisment types recieved
while scanning as text messages in the debug log.
This will use approximately 250 bytes of flash memory.
endmenu

View file

@ -20,6 +20,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLE2902.h" #include "NimBLE2902.h"
NimBLE2902::NimBLE2902(NimBLECharacteristic* pCharacterisitic) NimBLE2902::NimBLE2902(NimBLECharacteristic* pCharacterisitic)
@ -72,4 +75,5 @@ void NimBLE2902::setNotifications(bool flag) {
else pValue[0] &= ~(1 << 0); else pValue[0] &= ~(1 << 0);
} // setNotifications } // setNotifications
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif #endif

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
#include <map> #include <map>
@ -46,5 +49,6 @@ private:
}; // NimBLE2902 }; // NimBLE2902
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLE2902_H_ */ #endif /* MAIN_NIMBLE2902_H_ */

View file

@ -19,6 +19,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#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 // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif #endif

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
struct BLE2904_Data { struct BLE2904_Data {
@ -78,5 +81,6 @@ private:
BLE2904_Data m_data; BLE2904_Data m_data;
}; // BLE2904 }; // BLE2904
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLE2904_H_ */ #endif /* MAIN_NIMBLE2904_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -548,5 +551,6 @@ void NimBLEAdvertisedDevice::setAdvertisementResult(uint8_t* payload, uint8_t le
m_payloadLength = length; m_payloadLength = length;
} }
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#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"
@ -129,5 +132,6 @@ public:
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0; virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
}; };
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */ #endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */

View file

@ -15,6 +15,10 @@
*/ */
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#include "services/gap/ble_svc_gap.h" #include "services/gap/ble_svc_gap.h"
#include "NimBLEAdvertising.h" #include "NimBLEAdvertising.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -188,10 +192,15 @@ void NimBLEAdvertising::start() {
return; return;
} }
if(NimBLEDevice::createServer()->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) { #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
NimBLEServer* pServer = NimBLEDevice::createServer();
if(!pServer->m_gattsStarted){
pServer->start();
} else if(pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW(LOG_TAG, "Max connections reached - not advertising"); NIMBLE_LOGW(LOG_TAG, "Max connections reached - not advertising");
return; return;
} }
#endif
int numServices = m_serviceUUIDs.size(); int numServices = m_serviceUUIDs.size();
int rc = 0; int rc = 0;
@ -203,11 +212,6 @@ void NimBLEAdvertising::start() {
return; return;
} }
NimBLEServer* pServer = NimBLEDevice::createServer();
if(!pServer->m_gattsStarted){
pServer->start();
}
if (!m_customAdvData && !m_advSvcsSet && numServices > 0) { if (!m_customAdvData && !m_advSvcsSet && numServices > 0) {
for (int i = 0; i < numServices; i++) { for (int i = 0; i < numServices; i++) {
if(m_serviceUUIDs[i].getNative()->u.type == BLE_UUID_TYPE_16) { if(m_serviceUUIDs[i].getNative()->u.type == BLE_UUID_TYPE_16) {
@ -352,8 +356,13 @@ void NimBLEAdvertising::start() {
abort(); abort();
} }
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
rc = ble_gap_adv_start(addressType, NULL, BLE_HS_FOREVER, rc = ble_gap_adv_start(addressType, NULL, BLE_HS_FOREVER,
&m_advParams, NimBLEServer::handleGapEvent, NimBLEDevice::createServer()); //get a reference to the server (does not create a new one) &m_advParams, NimBLEServer::handleGapEvent, NimBLEDevice::createServer()); //get a reference to the server (does not create a new one)
#else
rc = ble_gap_adv_start(addressType, NULL, BLE_HS_FOREVER,
&m_advParams, NULL,NULL);
#endif
if (rc != 0) { if (rc != 0) {
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc)); NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
abort(); abort();
@ -603,4 +612,5 @@ std::string NimBLEAdvertisementData::getPayload() {
return m_payload; return m_payload;
} // getPayload } // getPayload
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#include "host/ble_gap.h" #include "host/ble_gap.h"
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
@ -90,7 +93,9 @@ public:
private: private:
friend class NimBLEDevice; friend class NimBLEDevice;
void onHostReset(); void onHostReset();
ble_hs_adv_fields m_advData; ble_hs_adv_fields m_advData;
ble_hs_adv_fields m_scanData; ble_hs_adv_fields m_scanData;
ble_gap_adv_params m_advParams; ble_gap_adv_params m_advParams;
@ -101,5 +106,7 @@ private:
bool m_advSvcsSet = false; bool m_advSvcsSet = false;
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_BLEADVERTISING_H_ */ #endif /* MAIN_BLEADVERTISING_H_ */

View file

@ -13,6 +13,7 @@
*/ */
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <string.h> #include <string.h>
#include "NimBLEBeacon.h" #include "NimBLEBeacon.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -89,5 +90,4 @@ void NimBLEBeacon::setSignalPower(int8_t signalPower) {
m_beaconData.signalPower = signalPower; m_beaconData.signalPower = signalPower;
} // setSignalPower } // setSignalPower
#endif #endif

View file

@ -14,6 +14,7 @@
#ifndef MAIN_NIMBLEBEACON_H_ #ifndef MAIN_NIMBLEBEACON_H_
#define MAIN_NIMBLEBEACON_H_ #define MAIN_NIMBLEBEACON_H_
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
/** /**
* @brief Representation of a beacon. * @brief Representation of a beacon.

View file

@ -12,6 +12,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLE2902.h" #include "NimBLE2902.h"
#include "NimBLE2904.h" #include "NimBLE2904.h"
@ -646,4 +649,5 @@ void NimBLECharacteristicCallbacks::onStatus(NimBLECharacteristic* pCharacterist
NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onStatus: default"); NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onStatus: default");
} // onStatus } // onStatus
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -16,6 +16,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "host/ble_hs.h" #include "host/ble_hs.h"
/**** FIX COMPILATION ****/ /**** FIX COMPILATION ****/
#undef min #undef min
@ -191,5 +194,7 @@ public:
virtual void onNotify(NimBLECharacteristic* pCharacteristic); virtual void onNotify(NimBLECharacteristic* pCharacteristic);
virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code); virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /*MAIN_NIMBLECHARACTERISTIC_H_*/ #endif /*MAIN_NIMBLECHARACTERISTIC_H_*/

View file

@ -12,6 +12,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEService.h" #include "NimBLEService.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -125,4 +128,5 @@ std::string NimBLECharacteristicMap::toString() {
} // toString } // toString
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEClient.h" #include "NimBLEClient.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -890,4 +893,5 @@ bool NimBLEClientCallbacks::onConfirmPIN(uint32_t pin){
return true; return true;
} }
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // CONFIG_BT_ENABLED #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_
#if defined(CONFIG_BT_ENABLED)
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
@ -109,5 +112,6 @@ public:
virtual bool onConfirmPIN(uint32_t pin); virtual bool onConfirmPIN(uint32_t pin);
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED
#endif /* MAIN_NIMBLECLIENT_H_ */ #endif /* MAIN_NIMBLECLIENT_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEService.h" #include "NimBLEService.h"
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -244,5 +247,5 @@ void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor) {
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default"); NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default");
} // onWrite } // onWrite
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
@ -97,5 +100,7 @@ public:
virtual void onRead(NimBLEDescriptor* pDescriptor); virtual void onRead(NimBLEDescriptor* pDescriptor);
virtual void onWrite(NimBLEDescriptor* pDescriptor); virtual void onWrite(NimBLEDescriptor* pDescriptor);
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLEDESCRIPTOR_H_ */ #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */

View file

@ -13,6 +13,10 @@
*/ */
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLEDescriptor.h" #include "NimBLEDescriptor.h"
@ -140,4 +144,6 @@ NimBLEDescriptor* NimBLEDescriptorMap::getNext() {
m_iterator++; m_iterator++;
return pRet; return pRet;
} // getNext } // getNext
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -14,6 +14,7 @@
#include "sdkconfig.h" #include "sdkconfig.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"
@ -40,34 +41,33 @@ static const char* LOG_TAG = "NimBLEDevice";
* Singletons for the NimBLEDevice. * Singletons for the NimBLEDevice.
*/ */
bool initialized = false; bool initialized = false;
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
NimBLEScan* NimBLEDevice::m_pScan = nullptr; NimBLEScan* NimBLEDevice::m_pScan = nullptr;
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
NimBLEServer* NimBLEDevice::m_pServer = nullptr; NimBLEServer* NimBLEDevice::m_pServer = nullptr;
#endif
uint32_t NimBLEDevice::m_passkey = 123456; uint32_t NimBLEDevice::m_passkey = 123456;
bool NimBLEDevice::m_synced = false; bool NimBLEDevice::m_synced = false;
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
NimBLEAdvertising* NimBLEDevice::m_bleAdvertising = nullptr; NimBLEAdvertising* NimBLEDevice::m_bleAdvertising = nullptr;
#endif
gap_event_handler NimBLEDevice::m_customGapHandler = nullptr; gap_event_handler NimBLEDevice::m_customGapHandler = nullptr;
ble_gap_event_listener NimBLEDevice::m_listener; ble_gap_event_listener NimBLEDevice::m_listener;
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
std::list <NimBLEClient*> NimBLEDevice::m_cList; std::list <NimBLEClient*> NimBLEDevice::m_cList;
#endif
std::list <NimBLEAddress> NimBLEDevice::m_ignoreList; std::list <NimBLEAddress> NimBLEDevice::m_ignoreList;
NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr; NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr;
//std::map<uint16_t, conn_status_t> BLEDevice::m_connectedClientsMap;
//gattc_event_handler BLEDevice::m_customGattcHandler = nullptr;
//gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
/** /**
* @brief Create a new instance of a server. * @brief Create a new instance of a server.
* @return A new instance of the server. * @return A new instance of the server.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
/* STATIC */ NimBLEServer* NimBLEDevice::createServer() { /* STATIC */ NimBLEServer* NimBLEDevice::createServer() {
/*#ifndef CONFIG_GATTS_ENABLE // Check that BLE GATTS is enabled in make menuconfig
NIMBLE_LOGE(LOG_TAG, "BLE GATTS is not enabled - CONFIG_GATTS_ENABLE not defined");
abort();
#endif // CONFIG_GATTS_ENABLE
*/
if(NimBLEDevice::m_pServer == nullptr) { if(NimBLEDevice::m_pServer == nullptr) {
NimBLEDevice::m_pServer = new NimBLEServer(); NimBLEDevice::m_pServer = new NimBLEServer();
ble_gatts_reset(); ble_gatts_reset();
@ -77,8 +77,10 @@ NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr;
return m_pServer; return m_pServer;
} // createServer } // createServer
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
NimBLEAdvertising* NimBLEDevice::getAdvertising() { NimBLEAdvertising* NimBLEDevice::getAdvertising() {
if(m_bleAdvertising == nullptr) { if(m_bleAdvertising == nullptr) {
m_bleAdvertising = new NimBLEAdvertising(); m_bleAdvertising = new NimBLEAdvertising();
@ -95,6 +97,7 @@ void NimBLEDevice::startAdvertising() {
void NimBLEDevice::stopAdvertising() { void NimBLEDevice::stopAdvertising() {
getAdvertising()->stop(); getAdvertising()->stop();
} // stopAdvertising } // stopAdvertising
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
/** /**
@ -102,19 +105,21 @@ void NimBLEDevice::stopAdvertising() {
* @return The scanning object reference. This is a singleton object. The caller should not * @return The scanning object reference. This is a singleton object. The caller should not
* try and release/delete it. * try and release/delete it.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
/* STATIC */ NimBLEScan* NimBLEDevice::getScan() { /* STATIC */ NimBLEScan* NimBLEDevice::getScan() {
if (m_pScan == nullptr) { if (m_pScan == nullptr) {
m_pScan = new NimBLEScan(); m_pScan = new NimBLEScan();
} }
return m_pScan; return m_pScan;
} // getScan } // getScan
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
/** /**
* @brief Creates a new client object and maintains a list of all client objects * @brief Creates a new client object and maintains a list of all client objects
* each client can connect to 1 peripheral device. * each client can connect to 1 peripheral device.
* @return A reference to the new client object. * @return A reference to the new client object.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
/* STATIC */ NimBLEClient* NimBLEDevice::createClient() { /* STATIC */ NimBLEClient* NimBLEDevice::createClient() {
if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)", NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)",
@ -225,6 +230,8 @@ void NimBLEDevice::stopAdvertising() {
return nullptr; return nullptr;
} // getDisconnectedClient } // getDisconnectedClient
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
/** /**
* @brief Set the transmission power. * @brief Set the transmission power.
@ -340,9 +347,12 @@ void NimBLEDevice::stopAdvertising() {
m_synced = false; m_synced = false;
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if(m_pScan != nullptr) { if(m_pScan != nullptr) {
m_pScan->onHostReset(); m_pScan->onHostReset();
} }
#endif
/* Not needed /* Not needed
if(m_pServer != nullptr) { if(m_pServer != nullptr) {
m_pServer->onHostReset(); m_pServer->onHostReset();
@ -352,9 +362,12 @@ void NimBLEDevice::stopAdvertising() {
(*it)->onHostReset(); (*it)->onHostReset();
} }
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
if(m_bleAdvertising != nullptr) { if(m_bleAdvertising != nullptr) {
m_bleAdvertising->onHostReset(); m_bleAdvertising->onHostReset();
} }
#endif
NIMBLE_LOGC(LOG_TAG, "Resetting state; reason=%d, %s", reason, NIMBLE_LOGC(LOG_TAG, "Resetting state; reason=%d, %s", reason,
NimBLEUtils::returnCodeToString(reason)); NimBLEUtils::returnCodeToString(reason));
@ -380,15 +393,19 @@ void NimBLEDevice::stopAdvertising() {
m_synced = true; m_synced = true;
if(initialized) { if(initialized) {
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if(m_pScan != nullptr) { if(m_pScan != nullptr) {
// Restart scanning with the last values sent, allow to clear results. // Restart scanning with the last values sent, allow to clear results.
m_pScan->start(m_pScan->m_duration, m_pScan->m_scanCompleteCB); m_pScan->start(m_pScan->m_duration, m_pScan->m_scanCompleteCB);
} }
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
if(m_bleAdvertising != nullptr) { if(m_bleAdvertising != nullptr) {
// Restart advertisng, parameters should already be set. // Restart advertisng, parameters should already be set.
m_bleAdvertising->start(); m_bleAdvertising->start();
} }
#endif
} }
} // onSync } // onSync
@ -661,20 +678,4 @@ void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) {
} // setCustomGapHandler } // setCustomGapHandler
/**
* @brief Backward compatibility for bluedroid gatt events.
* NimBLe does not send GATT events
*/
/*
void BLEDevice::setCustomGattcHandler(gattc_event_handler handler) {
setCustomGapHandler(handler);
}
void BLEDevice::setCustomGattsHandler(gatts_event_handler handler) {
setCustomGapHandler(handler);
}
*/
/**********************************************************/
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View file

@ -17,11 +17,27 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEScan.h" #include "NimBLEScan.h"
#include "NimBLEUtils.h" #endif
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#include "NimBLEAdvertising.h"
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEClient.h" #include "NimBLEClient.h"
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEServer.h" #include "NimBLEServer.h"
#endif
#include "NimBLEUtils.h"
#include "NimBLESecurity.h" #include "NimBLESecurity.h"
#include "NimBLEAddress.h"
#include "esp_bt.h" #include "esp_bt.h"
@ -69,22 +85,25 @@
* @brief BLE functions. * @brief BLE functions.
*/ */
typedef int (*gap_event_handler)(ble_gap_event *event, void *arg); typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
//typedef void (*gattc_event_handler)(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param);
//typedef void (*gatts_event_handler)(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param);
extern "C" void ble_store_config_init(void); extern "C" void ble_store_config_init(void);
class NimBLEDevice { class NimBLEDevice {
public: public:
static void init(const std::string &deviceName); // Initialize the local BLE environment. static void init(const std::string &deviceName);
static void deinit(); static void deinit();
static bool getInitialized(); static bool getInitialized();
static NimBLEAddress getAddress(); static NimBLEAddress getAddress();
static std::string toString(); static std::string toString();
static NimBLEScan* getScan(); // Get the scan object
static NimBLEClient* createClient(); #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
static NimBLEScan* getScan();
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
static NimBLEServer* createServer(); static NimBLEServer* createServer();
static bool deleteClient(NimBLEClient* pClient); #endif
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 setCustomGapHandler(gap_event_handler handler); static void setCustomGapHandler(gap_event_handler handler);
@ -101,36 +120,66 @@ public:
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);
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
static NimBLEAdvertising* getAdvertising(); static NimBLEAdvertising* getAdvertising();
static void startAdvertising(); static void startAdvertising();
static void stopAdvertising(); static void stopAdvertising();
#endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
static NimBLEClient* createClient();
static bool deleteClient(NimBLEClient* pClient);
static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByID(uint16_t conn_id);
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
static NimBLEClient* getDisconnectedClient(); static NimBLEClient* getDisconnectedClient();
static size_t getClientListSize(); static size_t getClientListSize();
static std::list<NimBLEClient*>* getClientList(); static std::list<NimBLEClient*>* getClientList();
#endif
private: private:
friend class NimBLEServer; #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
friend class NimBLEClient; friend class NimBLEClient;
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
friend class NimBLEScan; friend class NimBLEScan;
friend class NimBLEAdvertising; #endif
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
friend class NimBLEServer;
friend class NimBLECharacteristic; friend class NimBLECharacteristic;
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
friend class NimBLEAdvertising;
#endif
static void onReset(int reason); static void onReset(int reason);
static void onSync(void); static void onSync(void);
static void host_task(void *param); static void host_task(void *param);
static int startSecurity( uint16_t conn_id); static int startSecurity(uint16_t conn_id);
static bool m_synced; static bool m_synced;
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
static NimBLEScan* m_pScan; static NimBLEScan* m_pScan;
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
static NimBLEServer* m_pServer; static NimBLEServer* m_pServer;
#endif
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
static NimBLEAdvertising* m_bleAdvertising; static NimBLEAdvertising* m_bleAdvertising;
static ble_gap_event_listener m_listener; #endif
static uint32_t m_passkey;
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
static std::list <NimBLEClient*> m_cList; static std::list <NimBLEClient*> m_cList;
#endif
static std::list <NimBLEAddress> m_ignoreList; static std::list <NimBLEAddress> m_ignoreList;
static NimBLESecurityCallbacks* m_securityCallbacks; static NimBLESecurityCallbacks* m_securityCallbacks;
static uint32_t m_passkey;
static ble_gap_event_listener m_listener;
public: public:
static gap_event_handler m_customGapHandler; static gap_event_handler m_customGapHandler;

View file

@ -15,6 +15,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteCharacteristic.h" #include "NimBLERemoteCharacteristic.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -649,4 +652,6 @@ void NimBLERemoteCharacteristic::releaseSemaphores() {
m_semaphoreGetDescEvt.give(1); m_semaphoreGetDescEvt.give(1);
m_semaphoreReadCharEvt.give(1); m_semaphoreReadCharEvt.give(1);
} }
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
//#include "NimBLEUUID.h" //#include "NimBLEUUID.h"
//#include "FreeRTOS.h" //#include "FreeRTOS.h"
#include "NimBLERemoteService.h" #include "NimBLERemoteService.h"
@ -98,5 +101,7 @@ private:
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID. // We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
std::map<std::string, NimBLERemoteDescriptor*> m_descriptorMap; std::map<std::string, NimBLERemoteDescriptor*> m_descriptorMap;
}; // BLERemoteCharacteristic }; // BLERemoteCharacteristic
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */ #endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteDescriptor.h" #include "NimBLERemoteDescriptor.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -339,4 +342,6 @@ void NimBLERemoteDescriptor::releaseSemaphores() {
m_semaphoreDescWrite.give(1); m_semaphoreDescWrite.give(1);
m_semaphoreReadDescrEvt.give(1); m_semaphoreReadDescrEvt.give(1);
} }
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteCharacteristic.h" #include "NimBLERemoteCharacteristic.h"
class NimBLERemoteCharacteristic; class NimBLERemoteCharacteristic;
@ -54,5 +57,7 @@ private:
}; };
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */ #endif /* COMPONENTS_NIMBLEREMOTEDESCRIPTOR_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLERemoteService.h" #include "NimBLERemoteService.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -354,4 +357,5 @@ void NimBLERemoteService::releaseSemaphores() {
m_semaphoreGetCharEvt.give(1); m_semaphoreGetCharEvt.give(1);
} }
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEClient.h" #include "NimBLEClient.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
@ -85,5 +88,6 @@ private:
uint16_t m_endHandle; // The ending handle of this service. uint16_t m_endHandle; // The ending handle of this service.
}; // BLERemoteService }; // BLERemoteService
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEREMOTESERVICE_H_ */ #endif /* COMPONENTS_NIMBLEREMOTESERVICE_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEScan.h" #include "NimBLEScan.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
@ -400,4 +403,5 @@ NimBLEAdvertisedDevice NimBLEScanResults::getDevice(uint32_t i) {
return dev; return dev;
} }
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -16,6 +16,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
@ -82,6 +85,6 @@ private:
uint32_t m_duration; uint32_t m_duration;
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLE_SCAN_H_ */ #endif /* COMPONENTS_NIMBLE_SCAN_H_ */

View file

@ -15,6 +15,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEServer.h" #include "NimBLEServer.h"
#include "NimBLE2902.h" #include "NimBLE2902.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
@ -605,4 +608,6 @@ void NimBLEServer::onHostReset() {
} }
*/ */
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
#include "NimBLEAdvertising.h" #include "NimBLEAdvertising.h"
@ -24,7 +27,6 @@
#include "NimBLESecurity.h" #include "NimBLESecurity.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include <map> #include <map>
class NimBLEService; class NimBLEService;
@ -150,5 +152,6 @@ public:
}; // BLEServerCallbacks }; // BLEServerCallbacks
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* MAIN_NIMBLESERVER_H_ */ #endif /* MAIN_NIMBLESERVER_H_ */

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEService.h" #include "NimBLEService.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -291,4 +294,5 @@ NimBLEServer* NimBLEService::getServer() {
return m_pServer; return m_pServer;
} // getServer } // getServer
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View file

@ -17,6 +17,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLECharacteristic.h" #include "NimBLECharacteristic.h"
#include "NimBLEServer.h" #include "NimBLEServer.h"
#include "NimBLEUUID.h" #include "NimBLEUUID.h"
@ -92,5 +95,6 @@ private:
}; // BLEService }; // BLEService
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED
#endif /* MAIN_NIMBLESERVICE_H_ */ #endif /* MAIN_NIMBLESERVICE_H_ */

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEService.h" #include "NimBLEService.h"
@ -105,6 +108,7 @@ NimBLEService* NimBLEServiceMap::getFirst() {
return pRet; return pRet;
} // getFirst } // getFirst
/** /**
* @brief Get the next service in the map. * @brief Get the next service in the map.
* @return The next service in the map. * @return The next service in the map.
@ -116,6 +120,7 @@ NimBLEService* NimBLEServiceMap::getNext() {
return pRet; return pRet;
} // getNext } // getNext
/** /**
* @brief Removes service from maps. * @brief Removes service from maps.
* @return N/A. * @return N/A.
@ -125,6 +130,7 @@ void NimBLEServiceMap::removeService(NimBLEService* service) {
m_uuidMap.erase(service); m_uuidMap.erase(service);
} // removeService } // removeService
/** /**
* @brief Returns the amount of registered services * @brief Returns the amount of registered services
* @return amount of registered services * @return amount of registered services
@ -134,4 +140,6 @@ int NimBLEServiceMap::getRegisteredServiceCount(){
return m_uuidMap.size(); return m_uuidMap.size();
} }
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -11,42 +11,10 @@
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h" #include "NimBLELog.h"
#if defined(ARDUINO_ARCH_ESP32) && !defined(CONFIG_NIMBLE_ENABLED)
#include "nimconfig.h" #include "nimconfig.h"
#endif
static const char* LOG_TAG = "NimBLEUtils"; static const char* LOG_TAG = "NimBLEUtils";
/**
* @brief Copy memory from source to target but in reverse order.
*
* When we move memory from one location it is normally:
*
* ```
* [0][1][2]...[n] -> [0][1][2]...[n]
* ```
*
* with this function, it is:
*
* ```
* [0][1][2]...[n] -> [n][n-1][n-2]...[0]
* ```
*
* @param [in] target The target of the copy
* @param [in] source The source of the copy
* @param [in] size The number of bytes to copy
*/
void NimBLEUtils::memrcpy(uint8_t* target, const uint8_t* source, uint32_t size) {
assert(size > 0);
target += (size - 1); // Point target to the last byte of the target data
while (size > 0) {
*target = *source;
target--;
source++;
size--;
}
} // memrcpy
int NimBLEUtils::checkConnParams(ble_gap_conn_params* params) { int NimBLEUtils::checkConnParams(ble_gap_conn_params* params) {
/* Check connection interval min */ /* Check connection interval min */
@ -82,7 +50,7 @@ int NimBLEUtils::checkConnParams(ble_gap_conn_params* params) {
const char* NimBLEUtils::returnCodeToString(int rc) { const char* NimBLEUtils::returnCodeToString(int rc) {
#if CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT > 0 #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT)
switch(rc) { switch(rc) {
case 0: case 0:
return "SUCCESS"; return "SUCCESS";
@ -363,19 +331,19 @@ const char* NimBLEUtils::returnCodeToString(int rc) {
default: default:
return "Unknown"; return "Unknown";
} }
#else // #if CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT)
return ""; return "";
#endif // #if CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT)
} }
/** /**
* @brief Convert the BLE Advertising Data flags to a string. * @brief Convert the BLE Advertising Data flags to a string.
* @param adFlags The flags to convert * @param adFlags The flags to convert
* @return std::string A string representation of the advertising flags. * @return std::string A string representation of the advertising flags.
*/ */
const char* NimBLEUtils::advTypeToString(uint8_t advType) { const char* NimBLEUtils::advTypeToString(uint8_t advType) {
#if CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT > 0 #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT)
switch(advType) { switch(advType) {
case BLE_HCI_ADV_TYPE_ADV_IND : //0 case BLE_HCI_ADV_TYPE_ADV_IND : //0
return "Undirected - Connectable / Scannable"; return "Undirected - Connectable / Scannable";
@ -390,9 +358,9 @@ const char* NimBLEUtils::advTypeToString(uint8_t advType) {
default: default:
return "Unknown flag"; return "Unknown flag";
} }
#else // #if CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT)
return ""; return "";
#endif // #if CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT)
} // adFlagsToString } // adFlagsToString
@ -432,18 +400,20 @@ char* NimBLEUtils::buildHexData(uint8_t* target, const uint8_t* source, uint8_t
} // buildHexData } // buildHexData
void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){ void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){
#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));
#endif
} }
/** /**
* @brief Convert a BT GAP event type to a string representation. * @brief Convert a BT GAP event type to a string representation.
* @param [in] eventType The type of event. * @param [in] eventType The type of event.
* @return A string representation of the event type. * @return A string representation of the event type.
*/ */
const char* NimBLEUtils::gapEventToString(uint8_t eventType) { const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
#if CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT > 0 #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
switch (eventType) { switch (eventType) {
case BLE_GAP_EVENT_CONNECT : //0 case BLE_GAP_EVENT_CONNECT : //0
return "BLE_GAP_EVENT_CONNECT "; return "BLE_GAP_EVENT_CONNECT ";
@ -518,9 +488,9 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) {
NIMBLE_LOGD(LOG_TAG, "gapEventToString: Unknown event type %d 0x%.2x", eventType, eventType); NIMBLE_LOGD(LOG_TAG, "gapEventToString: Unknown event type %d 0x%.2x", eventType, eventType);
return "Unknown event type"; return "Unknown event type";
} }
#else // #if CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT #else // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
return ""; return "";
#endif // #if CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT)
} // gapEventToString } // gapEventToString

View file

@ -28,7 +28,6 @@ public:
static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length); static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length);
static const char* advTypeToString(uint8_t advType); static const char* advTypeToString(uint8_t advType);
static const char* returnCodeToString(int rc); static const char* returnCodeToString(int rc);
static void memrcpy(uint8_t* target, const uint8_t* source, uint32_t size);
static int checkConnParams(ble_gap_conn_params* params); static int checkConnParams(ble_gap_conn_params* params);
}; };

View file

@ -14,6 +14,9 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include "NimBLEValue.h" #include "NimBLEValue.h"
#include "NimBLELog.h" #include "NimBLELog.h"
@ -136,5 +139,5 @@ void NimBLEValue::setValue(const uint8_t* pData, size_t length) {
m_value = std::string((char*) pData, length); m_value = std::string((char*) pData, length);
} // setValue } // setValue
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View file

@ -16,6 +16,10 @@
#define MAIN_BLEVALUE_H_ #define MAIN_BLEVALUE_H_
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include "nimconfig.h"
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#include <string> #include <string>
/** /**
@ -42,5 +46,7 @@ private:
std::string m_value; std::string m_value;
}; };
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED
#endif /* MAIN_BLEVALUE_H_ */ #endif /* MAIN_BLEVALUE_H_ */

132
src/nimconfig.h Normal file
View file

@ -0,0 +1,132 @@
#pragma once
#include "sdkconfig.h"
/** For ESP-IDF compatibility
*
* Some versions of ESP-IDF used the config name format "CONFIG_NIMBLE_".
* This converts them to "CONFIG_BT_NIMBLE_" format used in the latest IDF.
*/
/* Detect if using ESP-IDF or Arduino (Arduino won't have these defines in sdkconfig)*/
#if defined(CONFIG_BT_NIMBLE_TASK_STACK_SIZE) || defined(CONFIG_NIMBLE_TASK_STACK_SIZE)
#if defined(CONFIG_NIMBLE_ENABLED)
#define CONFIG_BT_NIMBLE_ENABLED
#endif
#if defined(CONFIG_NIMBLE_ROLE_OBSERVER)
#define CONFIG_BT_NIMBLE_ROLE_OBSERVER
#endif
#if defined(CONFIG_NIMBLE_ROLE_BROADCASTER)
#define CONFIG_BT_NIMBLE_ROLE_BROADCASTER
#endif
#if defined(CONFIG_NIMBLE_ROLE_CENTRAL)
#define CONFIG_BT_NIMBLE_ROLE_CENTRAL
#endif
#if defined(CONFIG_NIMBLE_ROLE_PERIPHERAL)
#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
#endif
#if defined(CONFIG_NIMBLE_DEBUG)
#define CONFIG_BT_NIMBLE_DEBUG
#endif
#else // Using Arduino
/***********************************************
* Arduino config options
**********************************************/
/** Comment out if not using NimBLE Client functions
* Reduces flash size by approx. 7kB.
*/
#define CONFIG_BT_NIMBLE_ROLE_CENTRAL
/** Comment out if not using NimBLE Scan functions
* Reduces flash size by approx. 26kB.
*/
#define CONFIG_BT_NIMBLE_ROLE_OBSERVER
/** Comment out if not using NimBLE Server functions
* Reduces flash size by approx. 16kB.
*/
#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
/** Comment out if not using NimBLE Advertising functions
* Reduces flash size by approx. 5kB.
*/
#define CONFIG_BT_NIMBLE_ROLE_BROADCASTER
/** Uncomment to see debug log messages from the NimBLE host
* Uses approx. 32kB of flash memory.
*/
// #define CONFIG_BT_NIMBLE_DEBUG
/** Uncomment to see NimBLE host return codes as text debug log messages.
* Uses approx. 7kB of flash memory.
*/
// #define CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT
/** Uncomment to see GAP event codes as text in debug log messages.
* Uses approx. 1kB of flash memory.
*/
// #define CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT
/** Uncomment to see advertisment types as text while scanning in debug log messages.
* Uses approx. 250 bytes of flash memory.
*/
// #define CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT
/** Sets the core NimBLE host runs on */
#define CONFIG_BT_NIMBLE_PINNED_TO_CORE 0
/** Sets the stack size for the NimBLE host task */
#define CONFIG_BT_NIMBLE_TASK_STACK_SIZE 4096
/** Sets the number of simultaneous connections (esp controller max is 9) */
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 3
/** Sets the number of devices allowed to store/bond with */
#define CONFIG_BT_NIMBLE_MAX_BONDS 3
/** Sets the number of CCCD's to store per bonded device */
#define CONFIG_BT_NIMBLE_MAX_CCCDS 8
#define CONFIG_BT_NIMBLE_NVS_PERSIST 1
#define CONFIG_BT_NIMBLE_SM_LEGACY 1
#define CONFIG_BT_NIMBLE_SM_SC 1
#define CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME "nimble"
#define CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN 31
#define CONFIG_BT_NIMBLE_ATT_PREFERRED_MTU 256
#define CONFIG_BT_NIMBLE_SVC_GAP_APPEARANCE 0x0
#define CONFIG_BT_NIMBLE_ACL_BUF_COUNT 12
#define CONFIG_BT_NIMBLE_ACL_BUF_SIZE 255
#define CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE 70
#define CONFIG_BT_NIMBLE_HCI_EVT_HI_BUF_COUNT 30
#define CONFIG_BT_NIMBLE_HCI_EVT_LO_BUF_COUNT 8
#define CONFIG_BT_NIMBLE_MSYS1_BLOCK_COUNT 12
#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL 1
#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_ITVL 1000
#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_THRESH 2
#define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT 1
#define CONFIG_BT_NIMBLE_RPA_TIMEOUT 900
#define CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM 0
/** Do not comment out */
#define CONFIG_BT_ENABLED
#define CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY
#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL
#endif // #if defined(CONFIG_BT_NIMBLE_TASK_STACK_SIZE) || defined(CONFIG_NIMBLE_TASK_STACK_SIZE)
/** Cannot use client without scan */
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) && !defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
#define CONFIG_BT_NIMBLE_ROLE_OBSERVER
#endif
/** Cannot use server without advertise */
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) && !defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
#define CONFIG_BT_NIMBLE_ROLE_BROADCASTER
#endif