introduce State_t within NordicServiceDiscovery

This commit is contained in:
Rohit Grover 2015-05-26 08:43:08 +01:00
parent ede98b007e
commit 549b246f6a
2 changed files with 17 additions and 17 deletions

View file

@ -151,7 +151,7 @@ void
NordicServiceDiscovery::progressCharacteristicDiscovery(void)
{
/* Iterate through the previously discovered characteristics cached in characteristics[]. */
while (cDiscoveryActive && (characteristicIndex < numCharacteristics)) {
while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
(matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID())) {
if (characteristicCallback) {
@ -163,7 +163,7 @@ NordicServiceDiscovery::progressCharacteristicDiscovery(void)
}
/* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
if (cDiscoveryActive) {
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
/* Determine the ending handle of the last cached characteristic. */
Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
Gap::Handle_t endHandle = services[serviceIndex].getEndHandle();
@ -187,14 +187,14 @@ void
NordicServiceDiscovery::progressServiceDiscovery(void)
{
/* Iterate through the previously discovered services cached in services[]. */
while (sDiscoveryActive && (serviceIndex < numServices)) {
while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) {
if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
(matchingServiceUUID == services[serviceIndex].getShortUUID())) {
if (serviceCallback) {
serviceCallback(services[serviceIndex]);
}
if (sDiscoveryActive && characteristicCallback) {
if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) {
launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle());
} else {
serviceIndex++;
@ -205,7 +205,7 @@ NordicServiceDiscovery::progressServiceDiscovery(void)
}
/* Relaunch discovery of new services beyond the last entry cached in services[]. */
if (sDiscoveryActive && (numServices > 0) && (serviceIndex > 0)) {
if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) {
/* Determine the ending handle of the last cached service. */
Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle();
resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */

View file

@ -41,9 +41,7 @@ public:
void terminateServiceDiscovery(void) {
bool wasActive = isActive();
sDiscoveryActive = false;
cDiscoveryActive = false;
state = INACTIVE;
if (wasActive && onTerminationCallback) {
onTerminationCallback(connHandle);
@ -51,13 +49,14 @@ public:
}
void terminateCharacteristicDiscovery(void) {
cDiscoveryActive = false;
sDiscoveryActive = true;
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
state = SERVICE_DISCOVERY_ACTIVE;
}
serviceIndex++; /* Progress service index to keep discovery alive. */
}
bool isActive(void) const {
return (sDiscoveryActive || cDiscoveryActive);
return state != INACTIVE;
}
void setOnTermination(TerminationCallback_t callback) {
@ -81,16 +80,14 @@ public:
void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
connHandle = connectionHandle;
resetDiscoveredServices();
sDiscoveryActive = true;
cDiscoveryActive = false;
state = SERVICE_DISCOVERY_ACTIVE;
}
private:
void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
connHandle = connectionHandle;
resetDiscoveredCharacteristics();
cDiscoveryActive = true;
sDiscoveryActive = false;
state = CHARACTERISTIC_DISCOVERY_ACTIVE;
}
private:
@ -104,8 +101,11 @@ private:
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.*/
bool sDiscoveryActive;
bool cDiscoveryActive;
enum State_t {
INACTIVE,
SERVICE_DISCOVERY_ACTIVE,
CHARACTERISTIC_DISCOVERY_ACTIVE,
} state;
DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
* This is intended for internal use during service discovery. */