summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-03 15:26:37 +0200
committerSven Gothel <[email protected]>2020-07-03 15:26:37 +0200
commit58722cabae622579e17c860618e7f34576d2d521 (patch)
tree5edb272c2085ef41ab0fd03e3d964eb7e16eba40
parent5f43b2454eac3ed762f2576ad8e549c26200661c (diff)
EUI48::getBLERandomAddressType(): Pass BDAddressType, to only return a valid BLERandomAddressType related to the BDAddressType.
We validated the BLERandomAddressType in DBTDevice.java, but created an invalid value in DBTDevice.cpp regardless of BDAddressType. Resolved! We also perform the validate in DBTDevice.cpp now.
-rw-r--r--api/direct_bt/BTAddress.hpp6
-rw-r--r--java/direct_bt/tinyb/DBTDevice.java12
-rw-r--r--java/org/tinyb/BLERandomAddressType.java7
-rw-r--r--src/direct_bt/BTTypes.cpp9
-rw-r--r--src/direct_bt/DBTDevice.cpp25
5 files changed, 39 insertions, 20 deletions
diff --git a/api/direct_bt/BTAddress.hpp b/api/direct_bt/BTAddress.hpp
index 80d13408..bac15bf1 100644
--- a/api/direct_bt/BTAddress.hpp
+++ b/api/direct_bt/BTAddress.hpp
@@ -85,7 +85,7 @@ namespace direct_bt {
RESERVED = 0x02,
/** Static public 'random' device address 0b11 */
STATIC_PUBLIC = 0x03,
- /** Undefined */
+ /** Undefined, e.g. address not of type {@link BDAddressType::BDADDR_LE_RANDOM} */
UNDEFINED = 0xff
};
std::string getBLERandomAddressTypeString(const BLERandomAddressType type);
@@ -155,7 +155,7 @@ namespace direct_bt {
EUI48& operator=(const EUI48 &o) noexcept = default;
EUI48& operator=(EUI48 &&o) noexcept = default;
- BLERandomAddressType getBLERandomAddressType() const;
+ BLERandomAddressType getBLERandomAddressType(const BDAddressType addressType) const;
std::string toString() const;
};
@@ -168,8 +168,6 @@ namespace direct_bt {
inline bool operator!=(const EUI48& lhs, const EUI48& rhs)
{ return !(lhs == rhs); }
- std::string getBLERandomAddressTypeString(const EUI48 &a);
-
/** EUI48 MAC address matching any device, i.e. '0:0:0:0:0:0'. */
extern const EUI48 EUI48_ANY_DEVICE;
/** EUI48 MAC address matching all device, i.e. 'ff:ff:ff:ff:ff:ff'. */
diff --git a/java/direct_bt/tinyb/DBTDevice.java b/java/direct_bt/tinyb/DBTDevice.java
index 66c8c48c..c021ade5 100644
--- a/java/direct_bt/tinyb/DBTDevice.java
+++ b/java/direct_bt/tinyb/DBTDevice.java
@@ -207,11 +207,11 @@ public class DBTDevice extends DBTObject implements BluetoothDevice
this.leRandomAddressType = BLERandomAddressType.get(intBLERandomAddressType);
if( BluetoothAddressType.BDADDR_LE_RANDOM == addressType ) {
if( BLERandomAddressType.UNDEFINED == leRandomAddressType ) {
- throw new IllegalArgumentException("BDADDR_LE_RANDOM: Invalid given native BLERandomAddressType "+intBLERandomAddressType);
+ throw new IllegalArgumentException("BDADDR_LE_RANDOM: Invalid given native BLERandomAddressType "+intBLERandomAddressType+": "+toString());
}
} else {
if( BLERandomAddressType.UNDEFINED != leRandomAddressType ) {
- throw new IllegalArgumentException("Not BDADDR_LE_RANDOM: Invalid given native BLERandomAddressType "+leRandomAddressType);
+ throw new IllegalArgumentException("Not BDADDR_LE_RANDOM: Invalid given native BLERandomAddressType "+leRandomAddressType+": "+toString());
}
}
this.ts_creation = ts_creation;
@@ -514,7 +514,13 @@ public class DBTDevice extends DBTObject implements BluetoothDevice
public final String toString() {
if( !isValid() ) {
// UTF-8 271D = Cross
- return "Device" + "\u271D" + "["+address+", '"+name+"']";
+ final String leRandomStr;
+ if( BLERandomAddressType.UNDEFINED != this.leRandomAddressType ) {
+ leRandomStr = ", random "+leRandomAddressType.toString();
+ } else {
+ leRandomStr = "";
+ }
+ return "Device" + "\u271D" + "[address["+address+", "+addressType.toString()+leRandomStr+"], '"+name+"']";
}
return toStringImpl();
}
diff --git a/java/org/tinyb/BLERandomAddressType.java b/java/org/tinyb/BLERandomAddressType.java
index e31c7148..dbecb497 100644
--- a/java/org/tinyb/BLERandomAddressType.java
+++ b/java/org/tinyb/BLERandomAddressType.java
@@ -56,7 +56,7 @@ public enum BLERandomAddressType {
RESERVED (0x02),
/** Static public 'random' device address 0b11 */
STATIC_PUBLIC (0x03),
- /** Undefined */
+ /** Undefined, e.g. address not of type {@link BluetoothAddressType#BDADDR_LE_RANDOM} */
UNDEFINED (0xff);
public final int value;
@@ -76,7 +76,10 @@ public enum BLERandomAddressType {
}
}
- public static BLERandomAddressType getLERandomAddressType(final String address) {
+ public static BLERandomAddressType getLERandomAddressType(final String address, final BluetoothAddressType addressType) {
+ if( BluetoothAddressType.BDADDR_LE_RANDOM != addressType ) {
+ return BLERandomAddressType.UNDEFINED;
+ }
final String highByteS = address.substring(0, 3);
int highByte = 0;
try {
diff --git a/src/direct_bt/BTTypes.cpp b/src/direct_bt/BTTypes.cpp
index 64b321ce..40a994bc 100644
--- a/src/direct_bt/BTTypes.cpp
+++ b/src/direct_bt/BTTypes.cpp
@@ -130,11 +130,10 @@ std::string direct_bt::getBLERandomAddressTypeString(const BLERandomAddressType
return "Unknown BLERandomAddressType";
}
-std::string direct_bt::getBLERandomAddressTypeString(const EUI48 &a) {
- return getBLERandomAddressTypeString(a.getBLERandomAddressType());
-}
-
-BLERandomAddressType EUI48::getBLERandomAddressType() const {
+BLERandomAddressType EUI48::getBLERandomAddressType(const BDAddressType addressType) const {
+ if( BDAddressType::BDADDR_LE_RANDOM != addressType ) {
+ return BLERandomAddressType::UNDEFINED;
+ }
const uint8_t high2 = ( b[5] >> 6 ) & 0x03;
switch( high2 ) {
case 0x00: return BLERandomAddressType::UNRESOLVABLE_PRIVAT;
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index 7e5f723d..30d8abe5 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -43,20 +43,33 @@
using namespace direct_bt;
DBTDevice::DBTDevice(DBTAdapter & a, EInfoReport const & r)
-: adapter(a), ts_creation(r.getTimestamp()), address(r.getAddress()),
- addressType(r.getAddressType()), leRandomAddressType(address.getBLERandomAddressType())
+: adapter(a), ts_creation(r.getTimestamp()),
+ address(r.getAddress()), addressType(r.getAddressType()),
+ leRandomAddressType(address.getBLERandomAddressType(addressType))
{
ts_last_discovery = ts_creation;
hciConnHandle = 0;
isConnected = false;
isConnectIssued = false;
if( !r.isSet(EIRDataType::BDADDR) ) {
- throw IllegalArgumentException("DBTDevice ctor: Address not set: "+r.toString(), E_FILE_LINE);
+ throw IllegalArgumentException("Address not set: "+r.toString(), E_FILE_LINE);
}
if( !r.isSet(EIRDataType::BDADDR_TYPE) ) {
- throw IllegalArgumentException("DBTDevice ctor: AddressType not set: "+r.toString(), E_FILE_LINE);
+ throw IllegalArgumentException("AddressType not set: "+r.toString(), E_FILE_LINE);
}
update(r);
+
+ if( BDAddressType::BDADDR_LE_RANDOM == addressType ) {
+ if( BLERandomAddressType::UNDEFINED == leRandomAddressType ) {
+ throw IllegalArgumentException("BDADDR_LE_RANDOM: Invalid BLERandomAddressType "+
+ getBLERandomAddressTypeString(leRandomAddressType)+": "+toString(), E_FILE_LINE);
+ }
+ } else {
+ if( BLERandomAddressType::UNDEFINED != leRandomAddressType ) {
+ throw new IllegalArgumentException("Not BDADDR_LE_RANDOM: Invalid given native BLERandomAddressType "+
+ getBLERandomAddressTypeString(leRandomAddressType)+": "+toString(), E_FILE_LINE);
+ }
+ }
}
DBTDevice::~DBTDevice() {
@@ -123,8 +136,8 @@ std::vector<std::shared_ptr<uuid_t>> DBTDevice::getServices() const {
std::string DBTDevice::toString(bool includeDiscoveredServices) const {
const std::lock_guard<std::recursive_mutex> lock(const_cast<DBTDevice*>(this)->mtx_data); // RAII-style acquire and relinquish via destructor
const uint64_t t0 = getCurrentMilliseconds();
- std::string leaddrtype = "";
- if( BDAddressType::BDADDR_LE_RANDOM == addressType ) {
+ std::string leaddrtype;
+ if( BLERandomAddressType::UNDEFINED != leRandomAddressType ) {
leaddrtype = ", random "+getBLERandomAddressTypeString(leRandomAddressType);
}
std::string msdstr = nullptr != msd ? msd->toString() : "MSD[null]";