MicroBit as BLE gamepad
Fork of nRF51822 by
Diff: source/nRF5xServiceDiscovery.h
- Revision:
- 616:a8f9b022d8fd
- Parent:
- 615:65ea2acfc6a2
diff -r 65ea2acfc6a2 -r a8f9b022d8fd source/nRF5xServiceDiscovery.h --- a/source/nRF5xServiceDiscovery.h Wed Apr 06 22:38:43 2016 +0100 +++ b/source/nRF5xServiceDiscovery.h Wed Apr 06 22:39:17 2016 +0100 @@ -41,7 +41,6 @@ gattc(gattcIn), serviceIndex(0), numServices(0), - characteristicIndex(0), numCharacteristics(0), state(INACTIVE), services(), @@ -95,10 +94,43 @@ terminateServiceDiscovery(); } + void terminate(Gap::Handle_t connectionHandle) { + if(connHandle == connectionHandle) { + terminate(); + } + } + virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) { onTerminationCallback = callback; } + /** + * @brief Clear nRF5xServiceDiscovery's state. + * + * @return + * BLE_ERROR_NONE if successful. + */ + virtual ble_error_t reset(void) { + /* Clear all state that is from the parent, including private members */ + if (ServiceDiscovery::reset() != BLE_ERROR_NONE) { + return BLE_ERROR_INVALID_STATE; + } + + /* Clear derived class members */ + serviceIndex = 0; + numServices = 0; + numCharacteristics = 0; + + state = INACTIVE; + + serviceUUIDDiscoveryQueue.reset(); + charUUIDDiscoveryQueue.reset(); + + onTerminationCallback = NULL; + + return BLE_ERROR_NONE; + } + private: ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle); @@ -111,6 +143,8 @@ void removeFirstServiceNeedingUUIDDiscovery(void); void terminateServiceDiscovery(void) { + discoveredCharacteristic = nRF5xDiscoveredCharacteristic(); + bool wasActive = isActive(); state = INACTIVE; @@ -119,8 +153,24 @@ } } - void terminateCharacteristicDiscovery(void) { + void terminateCharacteristicDiscovery(ble_error_t err) { if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) { + if(discoveredCharacteristic != nRF5xDiscoveredCharacteristic()) { + if(err == BLE_ERROR_NONE) { + // fullfill the last characteristic + discoveredCharacteristic.setLastHandle(services[serviceIndex].getEndHandle()); + + if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || + ((matchingCharacteristicUUID == discoveredCharacteristic.getUUID()) && + (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) { + if (characteristicCallback) { + characteristicCallback(&discoveredCharacteristic); + } + } + } + discoveredCharacteristic = nRF5xDiscoveredCharacteristic(); + } + state = SERVICE_DISCOVERY_ACTIVE; } serviceIndex++; /* Progress service index to keep discovery alive. */ @@ -134,7 +184,6 @@ void resetDiscoveredCharacteristics(void) { numCharacteristics = 0; - characteristicIndex = 0; } private: @@ -281,7 +330,6 @@ 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 +348,19 @@ CharUUIDDiscoveryQueue charUUIDDiscoveryQueue; TerminationCallback_t onTerminationCallback; + + /* + * The currently discovered characteristic. Discovery of a characteristic + * is a two phase process. + * First, declaration handle is fetched, it provide the UUID, the value handle and + * the properties of a characteristic. + * Second, the next declaration handle is fetched, with its declaration handle, it is + * possible to compute the last handle of the discovered characteristic and fill the + * missing part of the object. + * If there is no remaining characteristic to discover, the last handle of the + * discovered characteristic will be set to the last handle of its enclosing service. + */ + nRF5xDiscoveredCharacteristic discoveredCharacteristic; }; #endif /*__NRF_SERVICE_DISCOVERY_H__*/ \ No newline at end of file