Beispiel #1
0
void triggerToggledWrite(const GattReadCallbackParams *response) {
    if (response->handle == ledCharacteristic.getValueHandle()) {
        printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
        for (unsigned index = 0; index < response->len; index++) {
            printf("%c[%02x]", response->data[index], response->data[index]);
        }
        printf("\r\n");

        uint8_t toggledValue = response->data[0] ^ 0x1;
        ledCharacteristic.write(1, &toggledValue);
    }
}
Beispiel #2
0
    /**
     * Initate the read of the characteristic in input.
     *
     * The completion of the operation will happens in when_characteristic_read()
     */
    void read_characteristic(const DiscoveredCharacteristic &characteristic)
    {
        printf("Initiating read at %u.\r\n", characteristic.getValueHandle());
        ble_error_t error = characteristic.read(
            0, as_cb(&Self::when_characteristic_read)
        );

        if (error) {
            printf(
                "Error: cannot initiate read at %u due to %u\r\n",
                characteristic.getValueHandle(), error
            );
            stop();
        }
    }
Beispiel #3
0
    /**
     * Initiate the discovery of the descriptors of the characteristic in input.
     *
     * When a descriptor is discovered, the function when_descriptor_discovered
     * is invoked.
     */
    void discover_descriptors(const DiscoveredCharacteristic &characteristic)
    {
        printf("Initiating descriptor discovery of %u.\r\n", characteristic.getValueHandle());

        _descriptor_handle = 0;
        ble_error_t error = characteristic.discoverDescriptors(
            as_cb(&Self::when_descriptor_discovered),
            as_cb(&Self::when_descriptor_discovery_ends)
        );

        if (error) {
            printf(
                "Error: cannot initiate discovery of %04X due to %u.\r\n",
                characteristic.getValueHandle(), error
            );
            stop();
        }
    }
ble_error_t nRF5xCharacteristicDescriptorDiscoverer::launch(
    const DiscoveredCharacteristic& characteristic,
    const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
    const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
) {
    Gap::Handle_t connHandle = characteristic.getConnectionHandle();
    // it is ok to deduce that the start handle for descriptors is after
    // the characteristic declaration and the characteristic value declaration
    // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] (3.3)
    Gap::Handle_t descriptorStartHandle = characteristic.getDeclHandle() + 2;
    Gap::Handle_t descriptorEndHandle = characteristic.getLastHandle();

    // check if there is any descriptor to discover
    if (descriptorEndHandle < descriptorStartHandle) {
        CharacteristicDescriptorDiscovery::TerminationCallbackParams_t termParams = {
            characteristic,
            BLE_ERROR_NONE
        };
        terminationCallback.call(&termParams);
        return BLE_ERROR_NONE;
    }

    // check if we can run this discovery
    if (isConnectionInUse(connHandle)) {
        return BLE_STACK_BUSY;
    }

    // get a new discovery slot, if none are available, just return
    Discovery* discovery = getAvailableDiscoverySlot();
    if(discovery == NULL) {
        return BLE_STACK_BUSY;
    }

    // try to launch the discovery
    ble_error_t err = gattc_descriptors_discover(connHandle, descriptorStartHandle, descriptorEndHandle);
    if(!err) {
        // commit the new discovery to its slot
        *discovery = Discovery(characteristic, discoveryCallback, terminationCallback);
    }

    return err;
}
Beispiel #5
0
void updateLedCharacteristic(void) {
    if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) {
        ledCharacteristic.read();
    }
}
Beispiel #6
0
void triggerRead(const GattWriteCallbackParams *response) {
    if (response->handle == ledCharacteristic.getValueHandle()) {
        ledCharacteristic.read();
    }
}