Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bluetooth] Add some utility classes #9064

Merged
merged 10 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public boolean discoverServices() {

for (BluetoothGattDescriptor dBusBlueZDescriptor : dBusBlueZCharacteristic.getGattDescriptors()) {
BluetoothDescriptor descriptor = new BluetoothDescriptor(characteristic,
UUID.fromString(dBusBlueZDescriptor.getUuid()));
UUID.fromString(dBusBlueZDescriptor.getUuid()), 0);
characteristic.addDescriptor(descriptor);
}
service.addCharacteristic(characteristic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
*/
package org.openhab.binding.bluetooth.bluez.internal;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.exceptions.DBusException;
import org.openhab.binding.bluetooth.util.RetryException;
import org.openhab.binding.bluetooth.util.RetryFuture;
import org.openhab.core.common.ThreadPoolManager;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
Expand Down Expand Up @@ -71,7 +71,7 @@ public BlueZPropertiesChangedHandler getPropertiesChangedHandler() {
public void initialize() {
logger.debug("initializing DeviceManagerFactory");

var stage1 = this.deviceManagerFuture = callAsync(() -> {
var stage1 = this.deviceManagerFuture = RetryFuture.callWithRetry(() -> {
try {
// if this is the first call to the library, this call
// should throw an exception (that we are catching)
Expand All @@ -83,12 +83,10 @@ public void initialize() {
}
}, scheduler);

stage1.thenCompose(devManager -> {
this.deviceManagerWrapperFuture = stage1.thenCompose(devManager -> {
// lambdas can't modify outside variables due to scoping, so instead we use an AtomicInteger.
AtomicInteger tryCount = new AtomicInteger();
// We need to set deviceManagerWrapperFuture here since we want to be able to cancel the underlying
// AsyncCompletableFuture instance
return this.deviceManagerWrapperFuture = callAsync(() -> {
return RetryFuture.callWithRetry(() -> {
int count = tryCount.incrementAndGet();
try {
logger.debug("Registering property handler attempt: {}", count);
Expand Down Expand Up @@ -127,60 +125,4 @@ public void dispose() {
}
this.deviceManagerWrapperFuture = null;
}

private static <T> CompletableFuture<T> callAsync(Callable<T> callable, ScheduledExecutorService scheduler) {
return new AsyncCompletableFuture<>(callable, scheduler);
}

// this is a utility class that allows use of Callable with CompletableFutures in a way such that the
// async future is cancellable thru this CompletableFuture instance.
private static class AsyncCompletableFuture<T> extends CompletableFuture<T> implements Runnable {

private final Callable<T> callable;
private final ScheduledExecutorService scheduler;
private final Object futureLock = new Object();
private Future<?> future;

public AsyncCompletableFuture(Callable<T> callable, ScheduledExecutorService scheduler) {
this.callable = callable;
this.scheduler = scheduler;
future = scheduler.submit(this);
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
synchronized (futureLock) {
future.cancel(mayInterruptIfRunning);
}
return super.cancel(mayInterruptIfRunning);
}

@Override
public void run() {
try {
complete(callable.call());
} catch (RetryException e) {
synchronized (futureLock) {
if (!future.isCancelled()) {
future = scheduler.schedule(this, e.delay, e.unit);
}
}
} catch (Exception e) {
completeExceptionally(e);
}
}
}

// this is a special exception to indicate to a AsyncCompletableFuture that the task needs to be retried.
private static class RetryException extends Exception {

private static final long serialVersionUID = 8512275408512109328L;
private long delay;
private TimeUnit unit;

public RetryException(long delay, TimeUnit unit) {
this.delay = delay;
this.unit = unit;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ public class BluetoothBindingConstants {

public static final long BLUETOOTH_BASE_UUID = 0x800000805f9b34fbL;

public static UUID createBluetoothUUID(long uuid16) {
return new UUID((uuid16 << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
}

// Bluetooth profile UUID definitions
public static final UUID PROFILE_GATT = UUID.fromString("00001801-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_A2DP_SOURCE = UUID.fromString("0000110a-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_A2DP_SINK = UUID.fromString("0000110b-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_A2DP = UUID.fromString("0000110d-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_AVRCP_REMOTE = UUID.fromString("0000110c-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_CORDLESS_TELEPHONE = UUID.fromString("00001109-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_DID_PNPINFO = UUID.fromString("00001200-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_HEADSET = UUID.fromString("00001108-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_HFP = UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_HFP_AUDIOGATEWAY = UUID.fromString("0000111f-0000-1000-8000-00805f9b34fb");
public static final UUID PROFILE_GATT = createBluetoothUUID(0x1801);
public static final UUID PROFILE_A2DP_SOURCE = createBluetoothUUID(0x110a);
public static final UUID PROFILE_A2DP_SINK = createBluetoothUUID(0x110b);
public static final UUID PROFILE_A2DP = createBluetoothUUID(0x110d);
public static final UUID PROFILE_AVRCP_REMOTE = createBluetoothUUID(0x110c);
public static final UUID PROFILE_CORDLESS_TELEPHONE = createBluetoothUUID(0x1109);
public static final UUID PROFILE_DID_PNPINFO = createBluetoothUUID(0x1200);
public static final UUID PROFILE_HEADSET = createBluetoothUUID(0x1108);
public static final UUID PROFILE_HFP = createBluetoothUUID(0x111e);
public static final UUID PROFILE_HFP_AUDIOGATEWAY = createBluetoothUUID(0x111f);

public static final UUID ATTR_CHARACTERISTIC_DECLARATION = createBluetoothUUID(0x2803);
}
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public enum GattCharacteristic {
private UUID uuid;

private GattCharacteristic(long key) {
this.uuid = new UUID((key << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
}

private static void initMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class BluetoothDescriptor {

protected final BluetoothCharacteristic characteristic;
protected final UUID uuid;
protected final int handle;
protected byte[] value;

/**
Expand All @@ -38,9 +39,10 @@ public class BluetoothDescriptor {
* @param characteristic the characteristic that this class describes
* @param uuid the uuid of the descriptor
*/
public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid) {
public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid, int handle) {
this.characteristic = characteristic;
this.uuid = uuid;
this.handle = handle;
}

/**
Expand Down Expand Up @@ -70,6 +72,15 @@ public UUID getUuid() {
return uuid;
}

/**
* Returns the handle for this descriptor
*
* @return the handle for the descriptor
*/
public int getHandle() {
return handle;
}

/**
* Returns the stored value for this descriptor. It doesn't read remote data.
*
Expand Down Expand Up @@ -111,7 +122,7 @@ public enum GattDescriptor {
private final UUID uuid;

private GattDescriptor(long key) {
this.uuid = new UUID((key << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
}

private static void initMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public enum GattService {
private UUID uuid;

private GattService(long key) {
this.uuid = new UUID((key << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
}

private static void initMapping() {
Expand Down
Loading