Previously we used the service end handle or, if available, the handle of the next characteristic in the service
as the end handle when retrieving the descriptors of a characteristic. This process would fail when connecting to some
devices if the characteristics were retrieved individually instead of all together. The cause of failure was requesting
a handle range that is out of characteristic scope which is also out of line with BLE
specifications.
The fix included in this is to set the end handles of the characteristics either after retrieving all the characteristics
in a service by using the next charactertistic definition handle or, if the characteristic was retrieved separately,
we retrieve the next characteristic just before retrieving the descriptors.
This allows for clearing the duplicate filter cache when required and will also clear it
automatically when starting a scan.
This is useful when unexpectedly disconnected from a device and attempting to reconnect.
Previously the scan filter would not report advertisements from a device if it was multi-connectable
and advertising while connected. The result was long delays until the device would be scanned again.
This resolves it by clearing the filter cache on each scan start/clearResults call
and adding a method to NimBLEScan for manually clearing the cache on demand.
* Add method to get all characteristics of a service
* Added method on NimBLEDescriptor to get the value out as a std::string
* Added methods to get things by handle where UUID could be used already.
* Added helper methods for getting a list of characteristics with a given UUID and service
* Demote the warning log for adding duplicate characteristics to a debug level log.
* Add methods to get services and characteristics using UUID + index
This prevents an exception when initializing a NimBLEAdvertising instance before calling NimBLEDevice::init().
The constructor calls reset() which calls stop(), if the stack was not yet initialized it will cause an exception.
Previous to this NimBLEAdvertising did not have some functionality that was in NimBLEAdvertisementData.
Also NimBLEAdvertisementData was missing functionality that exists in NimBLEAdvertising.
This resolves that by adding the missing functions to both classes.
Changed:
NimBLEAdvertising: Transmission power is no longer advertised by default and can be added to the advertisement by calling ::addTxPower()
Added:
- NimBLEAdvertising::setName
- NimBLEAdvertising::setManufacturerData
- NimBLEAdvertising::setURI
- NimBLEAdvertising::setServiceData
- NimBLEAdvertising::addTxPower
- NimBLEAdvertising::reset
- NimBLEAdvertisementData::addTxPower
- NimBLEAdvertisementData::setPreferredParams
- NimBLEAdvertisementData::setURI
Adds:
- setPartialServices16
- setPartialServices32
- setCompleteServices16
- setCompleteServices32
Each takes an input parameter of std::vector<NimBLEUUID> to allow for advertising multiple services with a simple interface.
* Add the option to change the scan filter mode through NimBLEDevice::setScanFilterMode
* Add the option to change the scan filter cache size through NimBLEDevice::setScanDuplicateCacheSize
* Refactor nimconfig to make it for doxygen use only.
* Adds setMaxResults to NimBLEScan to limit the number of advertised devices stored.
If set to 0 no devices will be stored in memory and only the callback will be invoked.
If set to 0xFF (default) stored devices will be unlimited. Any other value will be the limit.
* Uses the controller to filter duplicate devices from the scan results when wantDuplicates == false.
* Stores complete advertisement payload and only performs parsing on demand. The advertised data is no longer parsed automatically as it is discovered, instead it will be parsed to find only the data requested by the user application when it makes a call to do so. This saves processing time and approximately 2.4k in flash size, with the new methods listed below included.
Add more advertised device field methods:
* Adds haveAdvInterval/getAdvInterval - checks if the interval is advertised / gets the advertisement interval value.
* Adds haveConnParams/getMinInterval/getMaxInterval - checks if the parameters are advertised / get min value / get max value.
* Adds haveURI/getURI - checks if a URI is advertised / gets the URI data.
* Adds haveTargetAddress/getTargetAddressCount/getTargetAddress(index) - checks if a target address is present / gets a count of the addresses targeted / gets the address of the target at index.
- Added `READ_ENC` to the attribute of characteristics in 1812/2a4d
- Added `READ_ENC` and `WRITE_ENC` to the default values of properties of `createDescriptor()`
Connect should set m_pTaskData before calling ble_gap_connect in case of an early event.
Disconnect should check if the timer has already started before starting the timer so it does not reset it.
The timer should also be started before calling ble_gap_terminate in case of an early event that would cause the timer to start
after disconnection, resetting the host unnecessarily.
There is a chance to become disconnected before returning from the onConnect callback so the connection
status should be checked when returning from client connect.
In the case the controller become unresponsive, or does not comply with the timeout period,
this will ensure the application continues instead of potentially blocking forever.
Advertising start did not return a value to indicate success/failure,
this patch adds that functionality. In addition, more verbose logging of errors from
this function are provided with the removal of the related asserts.
* Minor code cleanup
m_duration member variable was not set previously which could trigger advertising to start if the
host was reset prior to the application calling start.
On some devices, searching for descriptors using the service end handle would return an invalid error.
This patch instead uses the handle of the next characteristic as the end handle (if available).
Previously when the host reset while scanning (if active prior) it would be restarted automatically.
This caused errors for some applications and has been removed since the event invokes the scan
ended callback for the app to take action. Instead scanning will now only be restarted if the duration
was indefinite and a callback was set for the advertisment events, this use case is less likely to have
a scan ended callback.
Advertising (if active prior) would be started without regard to it's previous state.
This has been corrected to only start advertising again if it was advertising for an idefinite time.
While deleting the client attribute database, if a notification occurs there is a possibility of
concurrency causing an exception. This fixes that by setting a flag before calling disconnect in
the deleteClient function to prevent processing further notifications.
This change is needed to allow any tasks that were stopped during a host reset to finish
before resuming from the re-sync. This would occasionally result in a LoadStoreError exception
if not done.
If we client just connected and a notification comes before deleting services it could cause an exception
when accessing a vector that is being deleted. This will check the connection established flag before
processing of notifications.
If a connection event was sent but failed to establish then the disconnect
callback would be triggered when the application was not yet informed of the connection.
* Cleanup logs and add comments.
* Handle the return codes from ble_gap_connect to take proper actions for different codes.
* Improve client event handling to accomodate delayed PDU responses.
* Use connection ID as a replacement for the isConnected flag. Also check if a task is waiting for
results instead of the waitingToConnect flag.
* Adds a disconnect timer so that calling disconnect will start a timer for the connection
supervision timeout time + a small delay. When this expires it will reset the host if a disconnect event
does not occur in this time. This is added as a workaround for the occasional situation
when the controller does not send an event after the disconnect command.
The BLE_HS_EBUSY return code was causing the application to hang when starting scan as
occasionally the code would not change, resulting in an infinite loop.
This patch handles the return code more appopriately and removes the loop.
Additionally a race condition would sometimes allow the code to execute past the conditional checks
and clear the advertised device vector while a scan was active and writing to it causing an exception.
This has been hopefully corrected by only clearing the vector if the return code from starting the scan is 0.
* Some peripherals will advertise 16/32bit UUIDs but when queried for their handles
they do not convert the UUID to 128 bit locally and will return attribute not found.
This patch will query the peripheral a second time with a 128bit converted UUID.
- This changes NimBLERemoteCharacteristic::subscribe and NimBLERemoteCharacteristic::registerForNotify functionality
such that the notification callback will always be set.
Additionally these methods will always return true unless the descriptor write fails.
This allows for notifications to trigger the callback when a peer device sends them and does not have a CCCD.
Also it should not be flagged as a failure if the CCCD does not exist.
It should only be flagged when an acutal write operation fails.
* Fix for random notify crash in ISR context
Tested for stability over 30 minutes with a daisy chain of 3 esp32
Co-authored-by: h2zero <powellperalta@gmail.com>
* Adds functionality to advertise for a set duration, similar to NimBLEScan::start.
The first parameter being the duration (in seconds).
The second parameter is a pointer to a callback function that is invoked when advertising stops.
* NimBLEAdvertising::isAdvertising method added, returns true if advertising is currently active.
* These methods were noop's and are now removed due to lack of usefulness and advertisment space consumed if used.
Applications can still manually set these values if desired using NimBLEAdvertisementData::addData.
* The type is available in the NimBLEAddress parameter, no longer needed separately.
Co-authored-by: wakwak-koba <wakwak-koba@users.noreply.github.com>
* Add clearAll parameter to deinit()
Adds the ability to clear all resources consumed during BLE operation when deinitializing and shutting down BLE.
Useful when BLE is used intermittently or in a task that initializes, performs operations then deinitializes.
By setting the clearAll parameter to true all created BLE objects will be deleted, freeing the memory for other tasks.
Warning: This will invalidate any pointers that may be referencing the deleted objects.
* Add bool deleteCallbacks parameter to NimBLEServer::setCallbacks, if true (default) will delete the callback class when server is destructed.
* Delete scan results when scan is destructed.
* Add support for getting multiple services data from advertisments.
* Adds new methods for getting advertised service data and UUIDS.
- getServiceData(index), gets the service data by index value.
- getServiceData(NimBLEUUID), gets the service data by UUID.
- getServiceDataCount(), gets the number of services with data advertised.
* Templates added for getServiceData(index) and getServiceData(NimBLEUUID)
to be able to specify the data type returned by these methods
Example:
getServiceData<uint32_t>(NimBLEUUID("ABCD");
* Also added:
- getServiceUUID(index), gets the advertised service UUID by index value.
- getServiceUUIDCount(), gets the number of advertised services.
Server: Add onSubscribe() callback for characteristics.
Adds a new method to NimBLECharacteristicCallbacks that gets called when a client
changes it's subscription status.
* Remove NimBLE2902 class.
As the NimBLE2902 class usefulness was only related to callback functions that were replaced
by the NimBLECharacteristicCallbacks:onSubscribe() method this removes the NimBLE2902 class and
moves all subscription handling to NimBLECharacteristic.
* Update documents and examples to reflect this change.
* Add getSubscribedCount() to get the number of subscribed clients.
Added alternative characteristic read/write callbacks that carry the connection description information.
Fixesh2zero/NimBLE-Arduino#83
Author: lknop <lknop@users.noreply.github.com>
* Get service handles on server start
* remove service + indicate service changed
* Reset gatt services when no connections active and services changed.
* NimBLEServer::createService can now be used any time and will send service changed
indication if server was already active.
* Add ability to remove advertised serviceUUIDS
* Adds addService() method to server to be allow user to re-add a service previously removed
* Add destructior to NimBLEServer, NimBLEService and NimBLECharacteristic to release allocated resources.
Also adds:
* NimBLEScan::setDuplicateFilter() to tell the controller to filter duplicates
before sending the result to the host.
* NimBLEScan::setLimitedOnly() to tell the controller only report scan results
from devices advertising in limited discovery mode, i.e. directed advertising.
* NimBLEScan::setFilterPolicy() to set the filter policy i.e whitelist only devices.