Compare commits

...

5 Commits

Author SHA1 Message Date
h2zero efd709083d
Merge 4c8a13afe4 into bf4b5d4ffa 2024-03-03 00:03:56 +10:30
kiss81 bf4b5d4ffa
ESP IDF 5.2 include fix (#144) 2024-03-01 12:29:18 -07:00
psitto 58f86cb7bd
Fix inconsistent use of time units (#143)
Seconds to milliseconds
2024-02-29 17:34:53 -07:00
h2zero 4c8a13afe4 Add secondary services as an include on the primary. 2021-09-13 08:39:59 -06:00
h2zero 79f442d24b Add secondary service capability. 2021-09-13 08:38:27 -06:00
8 changed files with 75 additions and 9 deletions

View File

@ -66,7 +66,7 @@ If false the service is only removed from visibility by clients. The pointers to
# Advertising
`NimBLEAdvertising::start`
Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback that is invoked when advertising ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
Now takes 2 optional parameters, the first is the duration to advertise for (in milliseconds), the second is a callback that is invoked when advertising ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
This provides an opportunity to update the advertisement data if desired.

View File

@ -255,7 +255,7 @@ Calling `NimBLEAdvertising::setAdvertisementData` will entirely replace any data
> BLEAdvertising::start (NimBLEAdvertising::start)
Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback that is invoked when advertising ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
Now takes 2 optional parameters, the first is the duration to advertise for (in milliseconds), the second is a callback that is invoked when advertising ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
This provides an opportunity to update the advertisement data if desired.
<br/>

View File

@ -146,8 +146,8 @@ bool connectToServer() {
* Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 12 * 10ms = 120ms timeout
*/
pClient->setConnectionParams(6,6,0,15);
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
pClient->setConnectTimeout(5);
/** Set how long we are willing to wait for the connection to complete (milliseconds), default is 30000. */
pClient->setConnectTimeout(5 * 1000);
if (!pClient->connect(advDevice)) {
@ -358,7 +358,7 @@ void app_main (void){
* but will use more energy from both devices
*/
pScan->setActiveScan(true);
/** Start scanning for advertisers for the scan time specified (in seconds) 0 = forever
/** Start scanning for advertisers for the scan time specified (in milliseconds) 0 = forever
* Optional callback for when scanning stops.
*/
pScan->start(scanTime);

View File

@ -154,7 +154,7 @@ void app_main (void) {
*/
pScan->setActiveScan(true);
/* Start scanning for advertisers for the scan time specified (in seconds) 0 = forever
/* Start scanning for advertisers for the scan time specified (in milliseconds) 0 = forever
* Optional callback for when scanning stops.
*/
pScan->start(scanTime);

View File

@ -24,6 +24,7 @@
#include <string>
#include <vector>
#include <ctime>
#ifndef CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
# define CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED 0

View File

@ -459,7 +459,7 @@ void NimBLEScan::onHostSync() {
/**
* @brief Start scanning and block until scanning has been completed.
* @param [in] duration The duration in seconds for which to scan.
* @param [in] duration The duration in milliseconds for which to scan.
* @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @return The scan results.
*/

View File

@ -47,6 +47,8 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid) {
m_handle = NULL_HANDLE;
m_pSvcDef = nullptr;
m_removed = 0;
m_secondary = false;
m_pSecSvcDef = nullptr;
} // NimBLEService
@ -65,6 +67,13 @@ NimBLEService::~NimBLEService() {
delete(m_pSvcDef);
}
if(m_pSecSvcDef != nullptr) {
for(auto &it : m_secSvcVec) {
delete it;
}
delete m_pSecSvcDef;
}
for(auto &it : m_chrVec) {
delete it;
}
@ -130,7 +139,7 @@ bool NimBLEService::start() {
ble_gatt_chr_def* pChr_a = nullptr;
ble_gatt_dsc_def* pDsc_a = nullptr;
svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
svc[0].type = m_secondary ? BLE_GATT_SVC_TYPE_SECONDARY : BLE_GATT_SVC_TYPE_PRIMARY;
svc[0].uuid = &m_uuid.getNative()->u;
svc[0].includes = NULL;
@ -222,6 +231,19 @@ bool NimBLEService::start() {
// end of services must indicate to api with type = 0
svc[1].type = 0;
m_pSvcDef = svc;
if(m_secSvcVec.size() > 0){
size_t numSecSvcs = m_secSvcVec.size();
ble_gatt_svc_def** m_pSecSvcDef = new ble_gatt_svc_def*[numSecSvcs + 1];
int i = 0;
for(auto& it : m_secSvcVec) {
it->start();
m_pSecSvcDef[i] = it->m_pSvcDef;
++i;
}
m_pSecSvcDef[numSecSvcs] = nullptr;
m_pSvcDef->includes = (const ble_gatt_svc_def**)m_pSecSvcDef;
}
}
int rc = ble_gatts_count_cfg((const ble_gatt_svc_def*)m_pSvcDef);
@ -234,7 +256,6 @@ bool NimBLEService::start() {
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "ble_gatts_add_svcs, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
return false;
}
NIMBLE_LOGD(LOG_TAG, "<< start()");
@ -254,6 +275,44 @@ uint16_t NimBLEService::getHandle() {
} // getHandle
/**
* @brief Creates a BLE service as a secondary service to the service this was called from.
* @param [in] uuid The UUID of the secondary service.
* @return A reference to the new secondary service object.
*/
NimBLEService* NimBLEService::createService(const NimBLEUUID &uuid) {
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
NimBLEServer* pServer = getServer();
NimBLEService* pService = new NimBLEService(uuid, 0, pServer);
m_secSvcVec.push_back(pService);
pService->m_secondary = true;
pServer->serviceChanged();
NIMBLE_LOGD(LOG_TAG, "<< createService");
return pService;
}
/**
* @brief Adds a secondary service to this service which was either already created but removed from availability,\n
* or created and later added.
* @param [in] service The secondary service object to add.
*/
void NimBLEService::addService(NimBLEService* service) {
// If adding a service that was not removed add it and return.
// Else reset GATT and send service changed notification.
if(service->m_removed == 0) {
m_secSvcVec.push_back(service);
return;
}
service->m_secondary = true;
service->m_removed = 0;
getServer()->serviceChanged();
}
/**
* @brief Create a new BLE Characteristic associated with this service.
* @param [in] uuid - The UUID of the characteristic.

View File

@ -69,6 +69,9 @@ public:
std::vector<NimBLECharacteristic*> getCharacteristics(const char* uuid);
std::vector<NimBLECharacteristic*> getCharacteristics(const NimBLEUUID &uuid);
void addService(NimBLEService* service);
NimBLEService* createService(const NimBLEUUID &uuid);
private:
@ -79,7 +82,10 @@ private:
NimBLEUUID m_uuid;
ble_gatt_svc_def* m_pSvcDef;
uint8_t m_removed;
bool m_secondary;
ble_gatt_svc_def** m_pSecSvcDef;
std::vector<NimBLECharacteristic*> m_chrVec;
std::vector<NimBLEService*> m_secSvcVec;
}; // NimBLEService