diff options
author | Sven Gothel <[email protected]> | 2020-02-15 11:19:27 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-02-15 11:19:27 +0100 |
commit | cdaf51206a95494d8de43fd5461313efff6badd7 (patch) | |
tree | e9996bd1de60a844cf800fb15e7c82728acf1386 /java/tinyb/hci | |
parent | 3fed423e2c81232bef6a3916b924996df1ff046e (diff) |
C++ tinyb_hci: Working HCIScanner showing AD packets, demonstrating new HCI classes
See scripts/build-x86_64.sh and README.md for build instructions.
See scripts/run-hci_scanner.sh to start new HCI scanner (C++).
New HCI C++ implementation redised in libtinyb_hci.so w/o GLIB/DBus dependencies,
but 'libbluetooth.so' dependency.
Following Class datastructures are complete:
- HCIUtil: Exception types, uint128_t and endian conversions
- General UUID interface and its efficient UUID16, UUID32 + UUID128 implementation.
Conversion 'toString', respecting endianess,
and UUID* -> UUID128 conversion.
Requird member comparison operations due to using interface
- HCIAdapter with its opened HCISession,
as well as its discovered HCIDevices
- HCIAdapter discover includes multiple advertising AD records,
up to 25 according to the spec.
- HCIAdapter parses full AD segment. TODO: Handle few more AD types
API/Impl Details:
- Datastructures utilize 'vector<shares_ptr<T>>' collections.
- Most attributes are specified 'const' -> immutable for efficancy
- Convenient collection access member operations
- etc ..
RESULTS:
- Fast AD scanning of multiple devices w/ UUID and RSSI
- Proper integration into tinyb project
TODO:
- Handle few more AD types
- Test multiple parallel HCISession from HCIAdapter
- HCIAdapter.connect()
- Represent GATT Service, Characteristics and Description
- Catch up with the Java binding step by step
Diffstat (limited to 'java/tinyb/hci')
-rw-r--r-- | java/tinyb/hci/HCIAdapter.java | 199 | ||||
-rw-r--r-- | java/tinyb/hci/HCIDevice.java | 231 | ||||
-rw-r--r-- | java/tinyb/hci/HCIEvent.java | 62 | ||||
-rw-r--r-- | java/tinyb/hci/HCIGattCharacteristic.java | 115 | ||||
-rw-r--r-- | java/tinyb/hci/HCIGattDescriptor.java | 86 | ||||
-rw-r--r-- | java/tinyb/hci/HCIGattService.java | 90 | ||||
-rw-r--r-- | java/tinyb/hci/HCIManager.java | 137 | ||||
-rw-r--r-- | java/tinyb/hci/HCIObject.java | 91 |
8 files changed, 1011 insertions, 0 deletions
diff --git a/java/tinyb/hci/HCIAdapter.java b/java/tinyb/hci/HCIAdapter.java new file mode 100644 index 00000000..71c9d818 --- /dev/null +++ b/java/tinyb/hci/HCIAdapter.java @@ -0,0 +1,199 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import org.tinyb.BluetoothAdapter; +import org.tinyb.BluetoothDevice; +import org.tinyb.BluetoothException; +import org.tinyb.BluetoothManager; +import org.tinyb.BluetoothNotification; +import org.tinyb.BluetoothType; +import org.tinyb.TransportType; + +public class HCIAdapter extends HCIObject implements BluetoothAdapter +{ + private final String address; + private final String name; + + /* pp */ HCIAdapter(final String address, final String name) + { + super(compHash(address, name)); + this.address = address; + this.name = name; + } + + @Override + public boolean equals(final Object obj) + { + if (obj == null || !(obj instanceof HCIDevice)) { + return false; + } + final HCIAdapter other = (HCIAdapter)obj; + return address.equals(other.address) && name.equals(other.name); + } + + @Override + public String getAddress() { return address; } + + @Override + public String getName() { return name; } + + public String getInterfaceName() { + throw new UnsupportedOperationException(); // FIXME + } + + @Override + public native BluetoothType getBluetoothType(); + + @Override + public native BluetoothAdapter clone(); + + static BluetoothType class_type() { return BluetoothType.ADAPTER; } + + @Override + public BluetoothDevice find(final String name, final String address, final long timeoutMS) { + final BluetoothManager manager = HCIManager.getBluetoothManager(); + return (BluetoothDevice) manager.find(BluetoothType.DEVICE, name, address, this, timeoutMS); + } + + @Override + public BluetoothDevice find(final String name, final String address) { + return find(name, address, 0); + } + + /* D-Bus method calls: */ + + @Override + public native boolean startDiscovery() throws BluetoothException; + + @Override + public native boolean stopDiscovery() throws BluetoothException; + + @Override + public native List<BluetoothDevice> getDevices(); + + @Override + public native int removeDevices() throws BluetoothException; + + /* D-Bus property accessors: */ + + @Override + public native String getAlias(); + + @Override + public native void setAlias(String value); + + @Override + public native long getBluetoothClass(); + + @Override + public native boolean getPowered(); + + @Override + public native void enablePoweredNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disablePoweredNotifications(); + + @Override + public native void setPowered(boolean value); + + @Override + public native boolean getDiscoverable(); + + @Override + public native void enableDiscoverableNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableDiscoverableNotifications(); + + @Override + public native void setDiscoverable(boolean value); + + @Override + public native long getDiscoverableTimeout(); + + @Override + public native void setDiscoverableTimout(long value); + + @Override + public native BluetoothDevice connectDevice(String address, String addressType); + + @Override + public native boolean getPairable(); + + @Override + public native void enablePairableNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disablePairableNotifications(); + + @Override + public native void setPairable(boolean value); + + @Override + public native long getPairableTimeout(); + + @Override + public native void setPairableTimeout(long value); + + @Override + public native boolean getDiscovering(); + + @Override + public native void enableDiscoveringNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableDiscoveringNotifications(); + + @Override + public native String[] getUUIDs(); + + @Override + public native String getModalias(); + + @Override + public void setDiscoveryFilter(final List<UUID> uuids, final int rssi, final int pathloss, final TransportType transportType) { + final List<String> uuidsFmt = new ArrayList<>(uuids.size()); + for (final UUID uuid : uuids) { + uuidsFmt.add(uuid.toString()); + } + setDiscoveryFilter(uuidsFmt, rssi, pathloss, transportType.ordinal()); + } + + public void setRssiDiscoveryFilter(final int rssi) { + setDiscoveryFilter(Collections.EMPTY_LIST, rssi, 0, TransportType.AUTO); + } + + private native void delete(); + + private native void setDiscoveryFilter(List<String> uuids, int rssi, int pathloss, int transportType); +} diff --git a/java/tinyb/hci/HCIDevice.java b/java/tinyb/hci/HCIDevice.java new file mode 100644 index 00000000..b9a41a53 --- /dev/null +++ b/java/tinyb/hci/HCIDevice.java @@ -0,0 +1,231 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import java.util.List; +import java.util.Map; + +import org.tinyb.BluetoothDevice; +import org.tinyb.BluetoothException; +import org.tinyb.BluetoothGattService; +import org.tinyb.BluetoothManager; +import org.tinyb.BluetoothNotification; +import org.tinyb.BluetoothType; + +import tinyb.dbus.DBusObject; + +public class HCIDevice extends HCIObject implements BluetoothDevice +{ + private final HCIAdapter adapter; + private final String address; + private final String name; + + /* pp */ HCIDevice(final HCIAdapter adptr, final String address, final String name) + { + super(compHash(address, name)); + this.adapter = adptr; + this.address = address; + this.name = name; + } + + @Override + public boolean equals(final Object obj) + { + if (obj == null || !(obj instanceof HCIDevice)) { + return false; + } + final HCIDevice other = (HCIDevice)obj; + return address.equals(other.address) && name.equals(other.name); + } + + @Override + public String getAddress() { return address; } + + @Override + public String getName() { return name; } + + @Override + public BluetoothType getBluetoothType() { return class_type(); } + + @Override + public native HCIDevice clone(); + + static BluetoothType class_type() { return BluetoothType.DEVICE; } + + @Override + public BluetoothGattService find(final String UUID, final long timeoutMS) { + final BluetoothManager manager = HCIManager.getBluetoothManager(); + return (BluetoothGattService) manager.find(BluetoothType.GATT_SERVICE, + null, UUID, this, timeoutMS); + } + + @Override + public BluetoothGattService find(final String UUID) { + return find(UUID, 0); + } + + /* D-Bus method calls: */ + + @Override + public native boolean disconnect() throws BluetoothException; + + @Override + public native boolean connect() throws BluetoothException; + + @Override + public native boolean connectProfile(String arg_UUID) throws BluetoothException; + + @Override + public native boolean disconnectProfile(String arg_UUID) throws BluetoothException; + + @Override + public boolean pair() throws BluetoothException + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public native boolean remove() throws BluetoothException; + + @Override + public boolean cancelPairing() throws BluetoothException + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public native List<BluetoothGattService> getServices(); + + /* D-Bus property accessors: */ + + @Override + public String getAlias() { return null; } // FIXME + + @Override + public void setAlias(final String value) + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public native int getBluetoothClass(); + + @Override + public native short getAppearance(); + + @Override + public native String getIcon(); + + @Override + public native boolean getPaired(); + + @Override + public native void enablePairedNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disablePairedNotifications(); + + @Override + public native boolean getTrusted(); + + @Override + public native void enableTrustedNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableTrustedNotifications(); + + @Override + public native void setTrusted(boolean value); + + @Override + public native boolean getBlocked(); + + @Override + public native void enableBlockedNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableBlockedNotifications(); + + @Override + public native void setBlocked(boolean value); + + @Override + public native boolean getLegacyPairing(); + + @Override + public native short getRSSI(); + + @Override + public native void enableRSSINotifications(BluetoothNotification<Short> callback); + + @Override + public native void disableRSSINotifications(); + + @Override + public native boolean getConnected(); + + @Override + public native void enableConnectedNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableConnectedNotifications(); + + @Override + public native String[] getUUIDs(); + + @Override + public native String getModalias(); + + @Override + public native HCIAdapter getAdapter(); + + @Override + public native Map<Short, byte[]> getManufacturerData(); + + @Override + public native void enableManufacturerDataNotifications(BluetoothNotification<Map<Short, byte[]> > callback); + + @Override + public native void disableManufacturerDataNotifications(); + + + @Override + public native Map<String, byte[]> getServiceData(); + + @Override + public native void enableServiceDataNotifications(BluetoothNotification<Map<String, byte[]> > callback); + + @Override + public native void disableServiceDataNotifications(); + + @Override + public native short getTxPower (); + + @Override + public native boolean getServicesResolved (); + + @Override + public native void enableServicesResolvedNotifications(BluetoothNotification<Boolean> callback); + + @Override + public native void disableServicesResolvedNotifications(); + + private native void delete(); +} diff --git a/java/tinyb/hci/HCIEvent.java b/java/tinyb/hci/HCIEvent.java new file mode 100644 index 00000000..4dc30ec3 --- /dev/null +++ b/java/tinyb/hci/HCIEvent.java @@ -0,0 +1,62 @@ +/* + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import org.tinyb.BluetoothCallback; +import org.tinyb.BluetoothEvent; +import org.tinyb.BluetoothType; + +public class HCIEvent implements BluetoothEvent +{ + private long nativeInstance; + + @Override + public native BluetoothType getType(); + @Override + public native String getName(); + @Override + public native String getIdentifier(); + @Override + public native boolean executeCallback(); + @Override + public native boolean hasCallback(); + + private native void init(BluetoothType type, String name, String identifier, + HCIObject parent, BluetoothCallback cb, Object data); + private native void delete(); + + public HCIEvent(final BluetoothType type, final String name, final String identifier, + final HCIObject parent, final BluetoothCallback cb, final Object data) + { + init(type, name, identifier, parent, cb, data); + } + + @Override + protected void finalize() + { + delete(); + } +} diff --git a/java/tinyb/hci/HCIGattCharacteristic.java b/java/tinyb/hci/HCIGattCharacteristic.java new file mode 100644 index 00000000..a2047e1e --- /dev/null +++ b/java/tinyb/hci/HCIGattCharacteristic.java @@ -0,0 +1,115 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import java.util.List; + +import org.tinyb.BluetoothException; +import org.tinyb.BluetoothGattCharacteristic; +import org.tinyb.BluetoothGattDescriptor; +import org.tinyb.BluetoothGattService; +import org.tinyb.BluetoothManager; +import org.tinyb.BluetoothNotification; +import org.tinyb.BluetoothType; + +public class HCIGattCharacteristic extends HCIObject implements BluetoothGattCharacteristic +{ + private final String uuid; + + /* pp */ HCIGattCharacteristic(final String uuid) + { + super(uuid.hashCode()); + this.uuid = uuid; + } + + @Override + public boolean equals(final Object obj) + { + if (obj == null || !(obj instanceof HCIGattCharacteristic)) { + return false; + } + final HCIGattCharacteristic other = (HCIGattCharacteristic)obj; + return uuid.equals(other.uuid); + } + + @Override + public String getUUID() { return uuid; } + + @Override + public native BluetoothType getBluetoothType(); + @Override + public native HCIGattCharacteristic clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_CHARACTERISTIC; } + + @Override + public BluetoothGattDescriptor find(final String UUID, final long timeoutMS) { + final BluetoothManager manager = HCIManager.getBluetoothManager(); + return (BluetoothGattDescriptor) manager.find(BluetoothType.GATT_DESCRIPTOR, + null, UUID, this, timeoutMS); + } + + @Override + public BluetoothGattDescriptor find(final String UUID) { + return find(UUID, 0); + } + + /* D-Bus method calls: */ + + @Override + public native byte[] readValue() throws BluetoothException; + + @Override + public native void enableValueNotifications(BluetoothNotification<byte[]> callback); + + @Override + public native void disableValueNotifications(); + + @Override + public native boolean writeValue(byte[] argValue) throws BluetoothException; + + /* D-Bus property accessors: */ + + @Override + public native BluetoothGattService getService(); + + @Override + public native byte[] getValue(); + + @Override + public native boolean getNotifying(); + + @Override + public native String[] getFlags(); + + @Override + public native List<BluetoothGattDescriptor> getDescriptors(); + + private native void init(HCIGattCharacteristic obj); + + private native void delete(); + +} diff --git a/java/tinyb/hci/HCIGattDescriptor.java b/java/tinyb/hci/HCIGattDescriptor.java new file mode 100644 index 00000000..b04816a4 --- /dev/null +++ b/java/tinyb/hci/HCIGattDescriptor.java @@ -0,0 +1,86 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import org.tinyb.BluetoothException; +import org.tinyb.BluetoothGattDescriptor; +import org.tinyb.BluetoothNotification; +import org.tinyb.BluetoothType; + +public class HCIGattDescriptor extends HCIObject implements BluetoothGattDescriptor +{ + private final String uuid; + + /* pp */ HCIGattDescriptor(final String uuid) + { + super(uuid.hashCode()); + this.uuid = uuid; + } + + @Override + public boolean equals(final Object obj) + { + if (obj == null || !(obj instanceof HCIGattDescriptor)) { + return false; + } + final HCIGattDescriptor other = (HCIGattDescriptor)obj; + return uuid.equals(other.uuid); + } + + @Override + public String getUUID() { return uuid; } + + @Override + public native BluetoothType getBluetoothType(); + @Override + public native BluetoothGattDescriptor clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_DESCRIPTOR; } + + /* D-Bus method calls: */ + + @Override + public native byte[] readValue(); + + @Override + public native boolean writeValue(byte[] argValue) throws BluetoothException; + + @Override + public native void enableValueNotifications(BluetoothNotification<byte[]> callback); + + @Override + public native void disableValueNotifications(); + + /* D-Bus property accessors: */ + + @Override + public native HCIGattCharacteristic getCharacteristic(); + + @Override + public native byte[] getValue(); + + private native void delete(); +} diff --git a/java/tinyb/hci/HCIGattService.java b/java/tinyb/hci/HCIGattService.java new file mode 100644 index 00000000..584f68b3 --- /dev/null +++ b/java/tinyb/hci/HCIGattService.java @@ -0,0 +1,90 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import java.util.List; + +import org.tinyb.BluetoothGattCharacteristic; +import org.tinyb.BluetoothGattService; +import org.tinyb.BluetoothManager; +import org.tinyb.BluetoothType; + +public class HCIGattService extends HCIObject implements BluetoothGattService +{ + private final String uuid; + + /* pp */ HCIGattService(final String uuid) + { + super(uuid.hashCode()); + this.uuid = uuid; + } + + @Override + public boolean equals(final Object obj) + { + if (obj == null || !(obj instanceof HCIGattService)) { + return false; + } + final HCIGattService other = (HCIGattService)obj; + return uuid.equals(other.uuid); + } + + @Override + public String getUUID() { return uuid; } + + @Override + public native BluetoothType getBluetoothType(); + + @Override + public native BluetoothGattService clone(); + + static BluetoothType class_type() { return BluetoothType.GATT_SERVICE; } + + @Override + public BluetoothGattCharacteristic find(final String UUID, final long timeoutMS) { + final BluetoothManager manager = HCIManager.getBluetoothManager(); + return (HCIGattCharacteristic) manager.find(BluetoothType.GATT_CHARACTERISTIC, + null, UUID, this, timeoutMS); + } + + @Override + public BluetoothGattCharacteristic find(final String UUID) { + return find(UUID, 0); + } + + /* D-Bus property accessors: */ + + @Override + public native HCIDevice getDevice(); + + @Override + public native boolean getPrimary(); + + @Override + public native List<BluetoothGattCharacteristic> getCharacteristics(); + + private native void delete(); +} diff --git a/java/tinyb/hci/HCIManager.java b/java/tinyb/hci/HCIManager.java new file mode 100644 index 00000000..7afba39c --- /dev/null +++ b/java/tinyb/hci/HCIManager.java @@ -0,0 +1,137 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import java.util.ArrayList; +import java.util.List; + +import org.tinyb.BluetoothAdapter; +import org.tinyb.BluetoothDevice; +import org.tinyb.BluetoothException; +import org.tinyb.BluetoothGattService; +import org.tinyb.BluetoothObject; +import org.tinyb.BluetoothManager; +import org.tinyb.BluetoothType; + +public class HCIManager implements BluetoothManager +{ + private static HCIManager inst; + private final List<BluetoothAdapter> adapters = new ArrayList<BluetoothAdapter>(); + + public native BluetoothType getBluetoothType(); + + private HCIObject find(final int type, final String name, final String identifier, final BluetoothObject parent, final long milliseconds) + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public HCIObject find(final BluetoothType type, final String name, final String identifier, final BluetoothObject parent, final long timeoutMS) { + return find(type.ordinal(), name, identifier, parent, timeoutMS); + } + + @Override + public HCIObject find(final BluetoothType type, final String name, final String identifier, final BluetoothObject parent) { + return find(type, name, identifier, parent, 0); + } + + @SuppressWarnings("unchecked") + @Override + public <T extends BluetoothObject> T find(final String name, final String identifier, final BluetoothObject parent, final long timeoutMS) { + return (T) find(HCIObject.class_type().ordinal(), name, identifier, parent, timeoutMS); + } + + @SuppressWarnings("unchecked") + @Override + public <T extends BluetoothObject> T find(final String name, final String identifier, final BluetoothObject parent) { + return (T) find(name, identifier, parent, 0); + } + + @Override + public BluetoothObject getObject(final BluetoothType type, final String name, + final String identifier, final BluetoothObject parent) { + return getObject(type.ordinal(), name, identifier, parent); + } + private BluetoothObject getObject(final int type, final String name, final String identifier, final BluetoothObject parent) + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public List<BluetoothObject> getObjects(final BluetoothType type, final String name, + final String identifier, final BluetoothObject parent) { + return getObjects(type.ordinal(), name, identifier, parent); + } + private List<BluetoothObject> getObjects(final int type, final String name, final String identifier, final BluetoothObject parent) + { throw new UnsupportedOperationException(); } // FIXME + + @Override + public List<BluetoothAdapter> getAdapters() { return adapters; } + + @Override + public List<BluetoothDevice> getDevices() { return getDefaultAdapter().getDevices(); } + + @Override + public List<BluetoothGattService> getServices() { throw new UnsupportedOperationException(); } // FIXME + + @Override + public boolean setDefaultAdapter(final BluetoothAdapter adapter) { throw new UnsupportedOperationException(); } // FIXME + + @Override + public BluetoothAdapter getDefaultAdapter() { return adapters.get(0); } + + @Override + public boolean startDiscovery() throws BluetoothException { return getDefaultAdapter().startDiscovery(); } + + @Override + public boolean stopDiscovery() throws BluetoothException { return getDefaultAdapter().stopDiscovery(); } + + @Override + public boolean getDiscovering() throws BluetoothException { return getDefaultAdapter().getDiscovering(); } + + private native HCIAdapter init() throws BluetoothException; + private native void delete(); + + private HCIManager() + { + adapters.add(init()); + } + + /** Returns an instance of BluetoothManager, to be used instead of constructor. + * @return An initialized BluetoothManager instance. + */ + public static synchronized BluetoothManager getBluetoothManager() throws RuntimeException, BluetoothException + { + if (inst == null) + { + inst = new HCIManager(); + } + return inst; + } + + @Override + protected void finalize() + { + adapters.clear(); + delete(); + } +} diff --git a/java/tinyb/hci/HCIObject.java b/java/tinyb/hci/HCIObject.java new file mode 100644 index 00000000..85a903e5 --- /dev/null +++ b/java/tinyb/hci/HCIObject.java @@ -0,0 +1,91 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tinyb.hci; + +import org.tinyb.BluetoothFactory; +import org.tinyb.BluetoothObject; +import org.tinyb.BluetoothType; + +public abstract class HCIObject implements BluetoothObject +{ + private final int hashValue; + private boolean isValid; + + static { + try { + System.loadLibrary(BluetoothFactory.JavaNativeLibBasename); + } catch (final Throwable e) { + System.err.println("Failed to load native library "+BluetoothFactory.JavaNativeLibBasename); + e.printStackTrace(); + throw e; // fwd exception - end here + } + } + + static BluetoothType class_type() { return BluetoothType.NONE; } + + /* pp */ static int compHash(final String a, final String b) { + // 31 * x == (x << 5) - x + final int hash = 31 + a.hashCode(); + return ((hash << 5) - hash) + b.hashCode(); + } + + protected HCIObject(final int hashValue) + { + this.hashValue = hashValue; + isValid = true; + } + + @Override + public abstract boolean equals(final Object obj); + + @Override + public final int hashCode() { + return hashValue; + } + + @Override + protected void finalize() + { + close(); + } + + @Override + public synchronized void close() { + if (!isValid) { + return; + } + isValid = false; + delete(); + } + @Override + public native BluetoothType getBluetoothType(); + + @Override + public BluetoothObject clone() + { throw new UnsupportedOperationException(); } // FIXME + + private native void delete(); +} |