Add status parameter in terminateCharacteristicDiscovery function.

Fix terminate discovery (the replacement of the discovery was done after
the call to terminate).
When searching for a running discovery, dismiss results where the
characteristic is equal to the default characteristic value
Add Discovery::operator!=
Add support of DiscoveredCharacteristic last handle in the characteristic
discovery process
This commit is contained in:
Vincent Coubard 2015-11-17 14:20:17 +00:00
parent cdd5b92193
commit dc2dfb0a95
5 changed files with 86 additions and 30 deletions

View file

@ -47,7 +47,7 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
default:
sdSingleton.terminateCharacteristicDiscovery();
sdSingleton.terminateCharacteristicDiscovery(BLE_ERROR_NONE);
break;
}
break;

View file

@ -3,8 +3,6 @@
#include "mbed-drivers/mbed_error.h"
#include "ble/DiscoveredCharacteristicDescriptor.h"
namespace {
void emptyDiscoveryCallback(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t*) { }
void emptyTerminationCallback(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t*) { }
@ -111,8 +109,10 @@ void nRF5xCharacteristicDescriptorDiscoverer::terminate(uint16_t handle, ble_err
if(!discovery) {
error("logic error in nRF5xCharacteristicDescriptorDiscoverer::process !!!");
}
discovery->terminate(err);
removeDiscovery(discovery);
Discovery tmp = *discovery;
*discovery = Discovery();
tmp.terminate(err);
}
nRF5xCharacteristicDescriptorDiscoverer::Discovery*
@ -137,8 +137,9 @@ nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCh
nRF5xCharacteristicDescriptorDiscoverer::Discovery*
nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(uint16_t handle) {
for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
if(discoveryRunning[i].characteristic.getConnectionHandle() == handle) {
for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
if(discoveryRunning[i].characteristic.getConnectionHandle() == handle &&
discoveryRunning[i] != Discovery()) {
return &discoveryRunning[i];
}
}

View file

@ -116,7 +116,11 @@ private:
friend bool operator==(const Discovery& lhs, const Discovery& rhs) {
return lhs.characteristic == rhs.characteristic &&
lhs.onDiscovery == rhs.onDiscovery &&
lhs.onTerminate == lhs.onTerminate;
lhs.onTerminate == rhs.onTerminate;
}
friend bool operator!=(const Discovery& lhs, const Discovery& rhs) {
return !(lhs == rhs);
}
};

View file

@ -27,22 +27,32 @@ nRF5xServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHan
.start_handle = startHandle,
.end_handle = endHandle
};
uint32_t rc;
if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
terminateCharacteristicDiscovery();
switch (rc) {
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_ADDR:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
default:
case NRF_ERROR_INVALID_STATE:
return BLE_ERROR_INVALID_STATE;
}
uint32_t rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange);
ble_error_t err = BLE_ERROR_NONE;
switch (rc) {
case NRF_SUCCESS:
err = BLE_ERROR_NONE;
break;
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_ADDR:
err = BLE_ERROR_INVALID_PARAM;
break;
case NRF_ERROR_BUSY:
err = BLE_STACK_BUSY;
break;
case NRF_ERROR_INVALID_STATE:
err = BLE_ERROR_INVALID_STATE;
break;
default:
err = BLE_ERROR_UNSPECIFIED;
break;
}
return BLE_ERROR_NONE;
if(err) {
terminateCharacteristicDiscovery(err);
}
return err;
}
void
@ -113,13 +123,36 @@ nRF5xServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_d
void
nRF5xServiceDiscovery::progressCharacteristicDiscovery(void)
{
if(state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
return;
}
if(remainingCharacteristic != nRF5xDiscoveredCharacteristic() && numCharacteristics > 0) {
remainingCharacteristic.setLastHandle(characteristics[0].getDeclHandle() - 1);
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == remainingCharacteristic.getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&remainingCharacteristic);
}
}
}
for(uint8_t i = 0; i < numCharacteristics; ++i) {
if(state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
return;
}
if(i == numCharacteristics - 1) {
remainingCharacteristic = characteristics[i];
break;
} else {
characteristics[i].setLastHandle(characteristics[i + 1].getDeclHandle() - 1);
}
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == characteristics[characteristicIndex].getUUID()) &&
((matchingCharacteristicUUID == characteristics[i].getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&characteristics[i]);
@ -131,7 +164,8 @@ nRF5xServiceDiscovery::progressCharacteristicDiscovery(void)
return;
}
Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
Gap::Handle_t startHandle = (numCharacteristics > 0) ? characteristics[numCharacteristics - 1].getValueHandle() + 1 : SRV_DISC_END_HANDLE;
Gap::Handle_t endHandle = services[serviceIndex].getEndHandle();
resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
@ -141,10 +175,10 @@ nRF5xServiceDiscovery::progressCharacteristicDiscovery(void)
.end_handle = endHandle
};
if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
terminateCharacteristicDiscovery();
terminateCharacteristicDiscovery(BLE_ERROR_UNSPECIFIED);
}
} else {
terminateCharacteristicDiscovery();
terminateCharacteristicDiscovery(BLE_ERROR_NONE);
}
}

View file

@ -41,7 +41,6 @@ public:
gattc(gattcIn),
serviceIndex(0),
numServices(0),
characteristicIndex(0),
numCharacteristics(0),
state(INACTIVE),
services(),
@ -111,6 +110,8 @@ private:
void removeFirstServiceNeedingUUIDDiscovery(void);
void terminateServiceDiscovery(void) {
remainingCharacteristic = nRF5xDiscoveredCharacteristic();
bool wasActive = isActive();
state = INACTIVE;
@ -119,8 +120,24 @@ private:
}
}
void terminateCharacteristicDiscovery(void) {
void terminateCharacteristicDiscovery(ble_error_t err) {
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
if(remainingCharacteristic != nRF5xDiscoveredCharacteristic()) {
if(err == BLE_ERROR_NONE) {
// fullfill the last characteristic
remainingCharacteristic.setLastHandle(services[serviceIndex].getEndHandle());
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == remainingCharacteristic.getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&remainingCharacteristic);
}
}
}
remainingCharacteristic = nRF5xDiscoveredCharacteristic();
}
state = SERVICE_DISCOVERY_ACTIVE;
}
serviceIndex++; /* Progress service index to keep discovery alive. */
@ -134,7 +151,6 @@ private:
void resetDiscoveredCharacteristics(void) {
numCharacteristics = 0;
characteristicIndex = 0;
}
private:
@ -281,7 +297,6 @@ private:
private:
uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
uint8_t numServices; /**< Number of services at the peers GATT database.*/
uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
enum State_t {
@ -300,6 +315,8 @@ private:
CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
TerminationCallback_t onTerminationCallback;
nRF5xDiscoveredCharacteristic remainingCharacteristic;
};
#endif /*__NRF_SERVICE_DISCOVERY_H__*/