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.bluez] update to dbus version 0.3.0 #18124

Merged
merged 1 commit into from
Jan 18, 2025
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
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.bluetooth.bluez/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>com.github.hypfvieh</groupId>
<artifactId>bluez-dbus-osgi</artifactId>
<version>0.1.4</version>
<version>0.3.0</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<feature name="openhab-binding-bluetooth-bluez" description="Bluetooth Binding Bluez" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0</bundle>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.bluez/${project.version}</bundle>
</feature>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ public synchronized void updateBlueZDevice(@Nullable BluetoothDevice blueZDevice
this.name = blueZDevice.getName();
Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData();
if (manData != null) {
manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst()
.ifPresent((UInt16 manufacturerId) ->
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
manData.keySet().stream().filter(Objects::nonNull).findFirst().ifPresent((UInt16 manufacturerId) ->
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
}

if (Boolean.TRUE.equals(blueZDevice.isConnected())) {
Expand Down Expand Up @@ -208,7 +207,7 @@ public boolean disconnect() {

private void ensureConnected() {
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
throw new IllegalStateException("DBusBlueZ device is not set or not connected");
}
}
Expand Down Expand Up @@ -265,7 +264,7 @@ private void ensureConnected() {
@Override
public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
}
Expand Down Expand Up @@ -301,7 +300,7 @@ private void ensureConnected() {
logger.debug("writeCharacteristic()");

BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
}
Expand Down Expand Up @@ -346,21 +345,21 @@ public void onNameUpdate(NameEvent event) {

@Override
public void onManufacturerDataUpdate(ManufacturerDataEvent event) {
for (Map.Entry<Short, byte[]> entry : event.getData().entrySet()) {
event.getData().forEach((key, value) -> {
BluetoothScanNotification notification = new BluetoothScanNotification();
byte[] data = new byte[entry.getValue().length + 2];
data[0] = (byte) (entry.getKey() & 0xFF);
data[1] = (byte) (entry.getKey() >>> 8);
byte[] data = new byte[value.length + 2];
data[0] = (byte) (key & 0xFF);
data[1] = (byte) (key >>> 8);

System.arraycopy(entry.getValue(), 0, data, 2, entry.getValue().length);
System.arraycopy(value, 0, data, 2, value.length);

if (logger.isDebugEnabled()) {
logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " "));
}

notification.setManufacturerData(data);
notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
}
});
}

@Override
Expand Down Expand Up @@ -513,7 +512,7 @@ public boolean isNotifying(BluetoothCharacteristic characteristic) {
@Override
public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bluez.exceptions.BluezNotSupportedException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.types.Variant;
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
import org.openhab.binding.bluetooth.BluetoothAddress;
Expand Down Expand Up @@ -149,10 +150,10 @@ public void dispose() {
Map<String, Variant<?>> filter = new HashMap<>();
filter.put("DuplicateData", new Variant<>(true));
try {
adapter.setDiscoveryFilter(filter);
localAdapter.setDiscoveryFilter(filter);
} catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException
| BluezNotReadyException e) {
throw new RuntimeException(e);
throw new DBusExecutionException("failed to set the discovery filter", e);
}

// now lets make sure that discovery is turned on
Expand All @@ -175,14 +176,14 @@ private void initializeAndRefreshDevices() {
return;
}

BluetoothAdapter adapter = prepareAdapter(deviceManager);
if (adapter == null) {
BluetoothAdapter localAdapter = prepareAdapter(deviceManager);
if (localAdapter == null) {
// adapter isn't prepared yet
return;
}

// now lets refresh devices
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(adapter);
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(localAdapter);
logger.debug("Found {} Bluetooth devices.", bluezDevices.size());
for (BluetoothDevice bluezDevice : bluezDevices) {
if (bluezDevice.getAddress() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.DBusMap;
import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler;
import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged;
import org.freedesktop.dbus.types.UInt16;
Expand Down Expand Up @@ -138,102 +137,84 @@ public void handle(@Nullable PropertiesChanged properties) {
}

private void onDiscoveringUpdate(String dbusPath, Variant<?> variant) {
Object discovered = variant.getValue();
if (discovered instanceof Boolean) {
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, (boolean) discovered));
if (variant.getValue() instanceof Boolean discovered) {
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, discovered));
}
}

private void onPoweredUpdate(String dbusPath, Variant<?> variant) {
Object powered = variant.getValue();
if (powered instanceof Boolean) {
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, (boolean) powered));
if (variant.getValue() instanceof Boolean powered) {
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, powered));
}
}

private void onServicesResolved(String dbusPath, Variant<?> variant) {
Object resolved = variant.getValue();
if (resolved instanceof Boolean) {
notifyListeners(new ServicesResolvedEvent(dbusPath, (boolean) resolved));
if (variant.getValue() instanceof Boolean resolved) {
notifyListeners(new ServicesResolvedEvent(dbusPath, resolved));
}
}

private void onNameUpdate(String dbusPath, Variant<?> variant) {
Object name = variant.getValue();
if (name instanceof String) {
notifyListeners(new NameEvent(dbusPath, (String) name));
if (variant.getValue() instanceof String name) {
notifyListeners(new NameEvent(dbusPath, name));
}
}

private void onTXPowerUpdate(String dbusPath, Variant<?> variant) {
Object txPower = variant.getValue();
if (txPower instanceof Short) {
notifyListeners(new TXPowerEvent(dbusPath, (short) txPower));
if (variant.getValue() instanceof Short txPower) {
notifyListeners(new TXPowerEvent(dbusPath, txPower));
}
}

private void onConnectedUpdate(String dbusPath, Variant<?> variant) {
Object connected = variant.getValue();
if (connected instanceof Boolean) {
notifyListeners(new ConnectedEvent(dbusPath, (boolean) connected));
if (variant.getValue() instanceof Boolean connected) {
notifyListeners(new ConnectedEvent(dbusPath, connected));
}
}

private void onManufacturerDataUpdate(String dbusPath, Variant<?> variant) {
Map<Short, byte[]> eventData = new HashMap<>();

Object map = variant.getValue();
if (map instanceof DBusMap) {
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key instanceof UInt16 && value instanceof Variant<?>) {
value = ((Variant<?>) value).getValue();
if (value instanceof byte[]) {
eventData.put(((UInt16) key).shortValue(), ((byte[]) value));
}
if (variant.getValue() instanceof Map<?, ?> map) {
Map<Short, byte[]> eventData = new HashMap<>();

map.forEach((key, value) -> {
if (key instanceof UInt16 iKey && value instanceof Variant<?> vValue
&& vValue.getValue() instanceof byte[] bValue) {
eventData.put(iKey.shortValue(), bValue);
}
});

if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
}
}
if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
}
}

private void onServiceDataUpdate(String dbusPath, Variant<?> variant) {
Map<String, byte[]> serviceData = new HashMap<>();

Object map = variant.getValue();
if (map instanceof DBusMap) {
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key instanceof String && value instanceof Variant<?>) {
value = ((Variant<?>) value).getValue();
if (value instanceof byte[]) {
serviceData.put(((String) key), ((byte[]) value));
}
if (variant.getValue() instanceof Map<?, ?> map) {
Map<String, byte[]> serviceData = new HashMap<>();

map.forEach((key, value) -> {
if (key instanceof String sKey && value instanceof Variant<?> vValue
&& vValue.getValue() instanceof byte[] bValue) {
serviceData.put(sKey, bValue);
}
});

if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
}
}
if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
}
}

private void onValueUpdate(String dbusPath, Variant<?> variant) {
Object value = variant.getValue();
if (value instanceof byte[]) {
notifyListeners(new CharacteristicUpdateEvent(dbusPath, (byte[]) value));
if (variant.getValue() instanceof byte[] bytes) {
notifyListeners(new CharacteristicUpdateEvent(dbusPath, bytes));
}
}

private void onRSSIUpdate(String dbusPath, Variant<?> variant) {
Object rssi = variant.getValue();
if (rssi instanceof Short) {
notifyListeners(new RssiEvent(dbusPath, (short) rssi));
if (variant.getValue() instanceof Short rssi) {
notifyListeners(new RssiEvent(dbusPath, rssi));
}
}
}
2 changes: 1 addition & 1 deletion features/openhab-addons/src/main/resources/footer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature name="openhab-binding-bluetooth" description="Bluetooth Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<feature>openhab-transport-serial</feature>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0</bundle>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.airthings/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}</bundle>
Expand Down