diff options
author | Sven Gothel <[email protected]> | 2021-08-04 06:01:22 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-08-04 06:01:22 +0200 |
commit | 75df7a20a2c84de38a049974165434a85420b7ba (patch) | |
tree | a0a41e9a598d8fa95f696306a422f027fd2b46a7 | |
parent | e4d18c7c17c1e6356747e5ad17c85ed9a47cc2da (diff) |
BTDeviceRegistry: Replace print*() to get*String() and clarify method names (C++ and Java)
-rw-r--r-- | api/direct_bt/BTDeviceRegistry.hpp | 17 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 33 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 33 | ||||
-rw-r--r-- | java/org/direct_bt/BTDeviceRegistry.java | 21 | ||||
-rw-r--r-- | src/direct_bt/BTDeviceRegistry.cpp | 47 |
5 files changed, 76 insertions, 75 deletions
diff --git a/api/direct_bt/BTDeviceRegistry.hpp b/api/direct_bt/BTDeviceRegistry.hpp index 3879eb2c..58f975a7 100644 --- a/api/direct_bt/BTDeviceRegistry.hpp +++ b/api/direct_bt/BTDeviceRegistry.hpp @@ -59,7 +59,7 @@ namespace direct_bt { bool isWaitingForDevice(const BDAddressAndType &mac, const std::string &name); bool isWaitingForAnyDevice(); size_t getWaitForDevicesCount(); - void printWaitForDevices(FILE *out, const std::string &msg); + std::string getWaitForDevicesString(); /** * Returns the reference of the current list of DeviceQuery, not a copy. */ @@ -111,11 +111,12 @@ namespace direct_bt { inline bool operator!=(const DeviceID& lhs, const DeviceID& rhs) noexcept { return !(lhs == rhs); } - void addToDevicesProcessed(const BDAddressAndType &a, const std::string& n); + void addToProcessedDevices(const BDAddressAndType &a, const std::string& n); bool isDeviceProcessed(const BDAddressAndType & a); - size_t getDeviceProcessedCount(); - bool allDevicesProcessed(); - void printDevicesProcessed(FILE *out, const std::string &msg); + size_t getProcessedDeviceCount(); + bool areAllDevicesProcessed(); + std::string getProcessedDevicesString(); + /** * Returns a copy of the current collection of processed DeviceID. */ @@ -125,10 +126,10 @@ namespace direct_bt { */ void clearProcessedDevices(); - void addToDevicesProcessing(const BDAddressAndType &a, const std::string& n); - bool removeFromDevicesProcessing(const BDAddressAndType &a); + void addToProcessingDevices(const BDAddressAndType &a, const std::string& n); + bool removeFromProcessingDevices(const BDAddressAndType &a); bool isDeviceProcessing(const BDAddressAndType & a); - size_t getDeviceProcessingCount(); + size_t getProcessingDeviceCount(); /** * Returns a copy of the current collection of processing DeviceID. */ diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index 52418825..09a31d57 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -281,7 +281,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { { deviceReadyCount++; fprintf_td(stderr, "****** READY-0: Processing[%d] %s\n", deviceReadyCount.load(), device->toString(true).c_str()); - BTDeviceRegistry::addToDevicesProcessing(device->getAddressAndType(), device->getName()); + BTDeviceRegistry::addToProcessingDevices(device->getAddressAndType(), device->getName()); processReadyDevice(device); // AdapterStatusListener::deviceReady() explicitly allows prolonged and complex code execution! } else { fprintf_td(stderr, "****** READY-1: NOP %s\n", device->toString(true).c_str()); @@ -298,7 +298,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { std::thread dc(::removeDevice, device); // @suppress("Invalid arguments") dc.detach(); } else { - BTDeviceRegistry::removeFromDevicesProcessing(device->getAddressAndType()); + BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType()); } if( 0 < RESET_ADAPTER_EACH_CONN && 0 == deviceReadyCount % RESET_ADAPTER_EACH_CONN ) { std::thread dc(::resetAdapter, &device->getAdapter(), 1); // @suppress("Invalid arguments") @@ -414,7 +414,7 @@ static void connectDiscoveredDevice(std::shared_ptr<BTDevice> device) { } fprintf_td(stderr, "****** Connecting Device: End result %s of %s\n", to_string(res).c_str(), device->toString().c_str()); - if( !USE_WHITELIST && 0 == BTDeviceRegistry::getDeviceProcessingCount() && HCIStatusCode::SUCCESS != res ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry::getProcessingDeviceCount() && HCIStatusCode::SUCCESS != res ) { startDiscovery(&device->getAdapter(), "post-connect"); } } @@ -546,11 +546,11 @@ static void processReadyDevice(std::shared_ptr<BTDevice> device) { exit: fprintf_td(stderr, "****** Processing Ready Device: End-1: Success %d on %s; devInProc %zu\n", - success, device->toString().c_str(), BTDeviceRegistry::getDeviceProcessingCount()); + success, device->toString().c_str(), BTDeviceRegistry::getProcessingDeviceCount()); - BTDeviceRegistry::removeFromDevicesProcessing(device->getAddressAndType()); + BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType()); - if( !USE_WHITELIST && 0 == BTDeviceRegistry::getDeviceProcessingCount() ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry::getProcessingDeviceCount() ) { startDiscovery(&device->getAdapter(), "post-processing-1"); } @@ -568,10 +568,10 @@ exit: } fprintf_td(stderr, "****** Processing Ready Device: End-2: Success %d on %s; devInProc %zu\n", - success, device->toString().c_str(), BTDeviceRegistry::getDeviceProcessingCount()); + success, device->toString().c_str(), BTDeviceRegistry::getProcessingDeviceCount()); if( success ) { - BTDeviceRegistry::addToDevicesProcessed(device->getAddressAndType(), device->getName()); + BTDeviceRegistry::addToProcessedDevices(device->getAddressAndType(), device->getName()); } device->removeAllCharListener(); @@ -585,7 +585,7 @@ exit: if( 0 < RESET_ADAPTER_EACH_CONN && 0 == deviceReadyCount % RESET_ADAPTER_EACH_CONN ) { resetAdapter(&device->getAdapter(), 2); - } else if( !USE_WHITELIST && 0 == BTDeviceRegistry::getDeviceProcessingCount() ) { + } else if( !USE_WHITELIST && 0 == BTDeviceRegistry::getProcessingDeviceCount() ) { startDiscovery(&device->getAdapter(), "post-processing-2"); } } @@ -600,11 +600,11 @@ static void removeDevice(std::shared_ptr<BTDevice> device) { fprintf_td(stderr, "****** Remove Device: removing: %s\n", device->getAddressAndType().toString().c_str()); device->getAdapter().stopDiscovery(); - BTDeviceRegistry::removeFromDevicesProcessing(device->getAddressAndType()); + BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType()); device->remove(); - if( !USE_WHITELIST && 0 == BTDeviceRegistry::getDeviceProcessingCount() ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry::getProcessingDeviceCount() ) { startDiscovery(&device->getAdapter(), "post-remove-device"); } } @@ -673,13 +673,13 @@ void test() { while( !done ) { if( 0 == MULTI_MEASUREMENTS || - ( -1 == MULTI_MEASUREMENTS && BTDeviceRegistry::isWaitingForAnyDevice() && BTDeviceRegistry::allDevicesProcessed() ) + ( -1 == MULTI_MEASUREMENTS && BTDeviceRegistry::isWaitingForAnyDevice() && BTDeviceRegistry::areAllDevicesProcessed() ) ) { fprintf_td(stderr, "****** EOL Test MULTI_MEASUREMENTS left %d, processed %zu/%zu\n", - MULTI_MEASUREMENTS.load(), BTDeviceRegistry::getDeviceProcessedCount(), BTDeviceRegistry::getWaitForDevicesCount()); - BTDeviceRegistry::printWaitForDevices(stderr, "****** WaitForDevice "); - BTDeviceRegistry::printDevicesProcessed(stderr, "****** DevicesProcessed "); + MULTI_MEASUREMENTS.load(), BTDeviceRegistry::getProcessedDeviceCount(), BTDeviceRegistry::getWaitForDevicesCount()); + fprintf_td(stderr, "****** WaitForDevice %s\n", BTDeviceRegistry::getWaitForDevicesString().c_str()); + fprintf_td(stderr, "****** DevicesProcessed %s\n", BTDeviceRegistry::getProcessedDevicesString().c_str()); done = true; } else { std::this_thread::sleep_for(std::chrono::milliseconds(2000)); @@ -822,8 +822,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "characteristic-value: %d\n", charValue); fprintf(stderr, "security-details: %s\n", BTSecurityRegistry::allToString().c_str()); - BTDeviceRegistry::printWaitForDevices(stderr, "waitForDevice: "); - + fprintf(stderr, "waitForDevice: %s\n", BTDeviceRegistry::getWaitForDevicesString().c_str()); if( waitForEnter ) { fprintf(stderr, "Press ENTER to continue\n"); diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index 90a6da26..048fdde3 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -275,7 +275,7 @@ public class DBTScanner10 { final long td = BTUtils.currentTimeMillis() - timestamp_t0; // adapter-init -> now BTUtils.println(System.err, "PERF: adapter-init -> READY-0 " + td + " ms"); } - BTDeviceRegistry.addToDevicesProcessing(device.getAddressAndType(), device.getName()); + BTDeviceRegistry.addToProcessingDevices(device.getAddressAndType(), device.getName()); processReadyDevice(device); // AdapterStatusListener::deviceReady() explicitly allows prolonged and complex code execution! } else { BTUtils.println(System.err, "****** READY-1: NOP " + device.toString()); @@ -289,7 +289,7 @@ public class DBTScanner10 { if( REMOVE_DEVICE ) { executeOffThread( () -> { removeDevice(device); }, "DBT-Remove-"+device.getAddressAndType(), true /* detach */); } else { - BTDeviceRegistry.removeFromDevicesProcessing(device.getAddressAndType()); + BTDeviceRegistry.removeFromProcessingDevices(device.getAddressAndType()); } if( 0 < RESET_ADAPTER_EACH_CONN && 0 == deviceReadyCount.get() % RESET_ADAPTER_EACH_CONN ) { executeOffThread( () -> { resetAdapter(device.getAdapter(), 1); }, @@ -364,7 +364,7 @@ public class DBTScanner10 { } BTUtils.println(System.err, "****** Connecting Device Command, res "+res+": End result "+res+" of " + device.toString()); - if( !USE_WHITELIST && 0 == BTDeviceRegistry.getDeviceProcessingCount() && HCIStatusCode.SUCCESS != res ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry.getProcessingDeviceCount() && HCIStatusCode.SUCCESS != res ) { startDiscovery(device.getAdapter(), "post-connect"); } } @@ -546,11 +546,11 @@ public class DBTScanner10 { } BTUtils.println(System.err, "****** Processing Ready Device: End-1: Success " + success + - " on " + device.toString() + "; devInProc "+BTDeviceRegistry.getDeviceProcessingCount()); + " on " + device.toString() + "; devInProc "+BTDeviceRegistry.getProcessingDeviceCount()); - BTDeviceRegistry.removeFromDevicesProcessing( device.getAddressAndType() ); + BTDeviceRegistry.removeFromProcessingDevices( device.getAddressAndType() ); - if( !USE_WHITELIST && 0 == BTDeviceRegistry.getDeviceProcessingCount() ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry.getProcessingDeviceCount() ) { startDiscovery(device.getAdapter(), "post-processing-1"); } @@ -568,9 +568,9 @@ public class DBTScanner10 { } BTUtils.println(System.err, "****** Processing Ready Device: End-2: Success " + success + - " on " + device.toString() + "; devInProc "+BTDeviceRegistry.getDeviceProcessingCount()); + " on " + device.toString() + "; devInProc "+BTDeviceRegistry.getProcessingDeviceCount()); if( success ) { - BTDeviceRegistry.addToDevicesProcessed(device.getAddressAndType(), device.getName()); + BTDeviceRegistry.addToProcessedDevices(device.getAddressAndType(), device.getName()); } device.removeAllCharListener(); @@ -585,7 +585,7 @@ public class DBTScanner10 { if( 0 < RESET_ADAPTER_EACH_CONN && 0 == deviceReadyCount.get() % RESET_ADAPTER_EACH_CONN ) { resetAdapter(device.getAdapter(), 2); - } else if( !USE_WHITELIST && 0 == BTDeviceRegistry.getDeviceProcessingCount() ) { + } else if( !USE_WHITELIST && 0 == BTDeviceRegistry.getProcessingDeviceCount() ) { startDiscovery(device.getAdapter(), "post-processing-2"); } } @@ -600,11 +600,11 @@ public class DBTScanner10 { BTUtils.println(System.err, "****** Remove Device: removing: "+device.getAddressAndType()); device.getAdapter().stopDiscovery(); - BTDeviceRegistry.removeFromDevicesProcessing(device.getAddressAndType()); + BTDeviceRegistry.removeFromProcessingDevices(device.getAddressAndType()); device.remove(); - if( !USE_WHITELIST && 0 == BTDeviceRegistry.getDeviceProcessingCount() ) { + if( !USE_WHITELIST && 0 == BTDeviceRegistry.getProcessingDeviceCount() ) { startDiscovery(device.getAdapter(), "post-remove-device"); } } @@ -685,13 +685,13 @@ public class DBTScanner10 { while( !done ) { if( 0 == MULTI_MEASUREMENTS.get() || - ( -1 == MULTI_MEASUREMENTS.get() && BTDeviceRegistry.isWaitingForAnyDevice() && BTDeviceRegistry.allDevicesProcessed() ) + ( -1 == MULTI_MEASUREMENTS.get() && BTDeviceRegistry.isWaitingForAnyDevice() && BTDeviceRegistry.areAllDevicesProcessed() ) ) { BTUtils.println(System.err, "****** EOL Test MULTI_MEASUREMENTS left "+MULTI_MEASUREMENTS.get()+ - ", processed "+BTDeviceRegistry.getDeviceProcessedCount()+"/"+BTDeviceRegistry.getWaitForDevicesCount()); - BTDeviceRegistry.printWaitForDevices(System.err, "****** WaitForDevices "); - BTDeviceRegistry.printDevicesProcessed(System.err, "****** DevicesProcessed "); + ", processed "+BTDeviceRegistry.getProcessedDeviceCount()+"/"+BTDeviceRegistry.getWaitForDevicesCount()); + BTUtils.println(System.err, "****** WaitForDevices "+BTDeviceRegistry.getWaitForDevicesString()); + BTUtils.println(System.err, "****** DevicesProcessed "+BTDeviceRegistry.getProcessedDevicesString()); done = true; } else { try { @@ -863,8 +863,7 @@ public class DBTScanner10 { BTUtils.println(System.err, "characteristic-value: "+test.charValue); BTUtils.println(System.err, "security-details: "+BTSecurityRegistry.allToString() ); - - BTDeviceRegistry.printWaitForDevices(System.err, "waitForDevices: "); + BTUtils.println(System.err, "waitForDevices: "+BTDeviceRegistry.getWaitForDevicesString()); if( waitForEnter ) { BTUtils.println(System.err, "Press ENTER to continue\n"); diff --git a/java/org/direct_bt/BTDeviceRegistry.java b/java/org/direct_bt/BTDeviceRegistry.java index 15ba0b76..fc309b3a 100644 --- a/java/org/direct_bt/BTDeviceRegistry.java +++ b/java/org/direct_bt/BTDeviceRegistry.java @@ -136,8 +136,8 @@ public class BTDeviceRegistry { public static int getWaitForDevicesCount() { return waitForDevices.size(); } - public static void printWaitForDevices(final PrintStream out, final String msg) { - BTUtils.println(out, msg+" "+Arrays.toString(waitForDevices.toArray())); + public static String getWaitForDevicesString() { + return Arrays.toString(waitForDevices.toArray()); } /** * Returns the reference of the current list of {@link DeviceQuery}, not a copy. @@ -152,16 +152,16 @@ public class BTDeviceRegistry { waitForDevices.clear(); } - public static void addToDevicesProcessed(final BDAddressAndType a, final String n) { + public static void addToProcessedDevices(final BDAddressAndType a, final String n) { devicesProcessed.add( new DeviceID(a, n) ); } public static boolean isDeviceProcessed(final BDAddressAndType a) { return devicesProcessed.contains( new DeviceID(a, null) ); } - public static int getDeviceProcessedCount() { + public static int getProcessedDeviceCount() { return devicesProcessed.size(); } - public static boolean allDevicesProcessed() { + public static boolean areAllDevicesProcessed() { for(final Iterator<DeviceQuery> it1=waitForDevices.iterator(); it1.hasNext(); ) { final DeviceQuery q = it1.next(); final Iterator<DeviceID> it2=devicesProcessed.iterator(); @@ -178,9 +178,10 @@ public class BTDeviceRegistry { } return true; } - public static void printDevicesProcessed(final PrintStream out, final String msg) { - BTUtils.println(out, msg+" "+Arrays.toString(devicesProcessed.toArray())); + public static String getProcessedDevicesString() { + return Arrays.toString(devicesProcessed.toArray()); } + /** * Returns a copy of the current collection of processed {@link DeviceID}. */ @@ -194,16 +195,16 @@ public class BTDeviceRegistry { devicesProcessed.clear(); } - public static void addToDevicesProcessing(final BDAddressAndType a, final String n) { + public static void addToProcessingDevices(final BDAddressAndType a, final String n) { devicesInProcessing.add( new DeviceID(a, n) ); } - public static boolean removeFromDevicesProcessing(final BDAddressAndType a) { + public static boolean removeFromProcessingDevices(final BDAddressAndType a) { return devicesInProcessing.remove( new DeviceID(a, null) ); } public static boolean isDeviceProcessing(final BDAddressAndType a) { return devicesInProcessing.contains( new DeviceID(a, null) ); } - public static int getDeviceProcessingCount() { + public static int getProcessingDeviceCount() { return devicesInProcessing.size(); } /** diff --git a/src/direct_bt/BTDeviceRegistry.cpp b/src/direct_bt/BTDeviceRegistry.cpp index 6a820c54..1f98d90c 100644 --- a/src/direct_bt/BTDeviceRegistry.cpp +++ b/src/direct_bt/BTDeviceRegistry.cpp @@ -65,16 +65,17 @@ namespace direct_bt::BTDeviceRegistry { size_t getWaitForDevicesCount() { return waitForDevices.size(); } - static void printList(FILE *out, const std::string &msg, jau::darray<DeviceQuery> &cont) { - jau::fprintf_td(out, "%s ", msg.c_str()); - jau::for_each(cont.cbegin(), cont.cend(), [out](const DeviceQuery& q) { - fprintf(out, "%s, ", q.toString().c_str()); + std::string getWaitForDevicesString() { + std::string res; + jau::for_each(waitForDevices.cbegin(), waitForDevices.cend(), [&res](const DeviceQuery &q) { + if( res.length() > 0 ) { + res.append( ", " ); + } + res.append( q.toString() ); }); - fprintf(out, "\n"); - } - void printWaitForDevices(FILE *out, const std::string &msg) { - printList(out, msg, waitForDevices); + return res; } + jau::darray<DeviceQuery>& getWaitForDevices() { return waitForDevices; } @@ -82,7 +83,7 @@ namespace direct_bt::BTDeviceRegistry { waitForDevices.clear(); } - void addToDevicesProcessed(const BDAddressAndType &a, const std::string& n) { + void addToProcessedDevices(const BDAddressAndType &a, const std::string& n) { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor devicesProcessed.emplace_hint(devicesProcessed.end(), a, n); } @@ -90,11 +91,11 @@ namespace direct_bt::BTDeviceRegistry { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor return devicesProcessed.end() != devicesProcessed.find( DeviceID(a, "") ); } - size_t getDeviceProcessedCount() { + size_t getProcessedDeviceCount() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor return devicesProcessed.size(); } - bool allDevicesProcessed() { + bool areAllDevicesProcessed() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor for (auto it1 = waitForDevices.cbegin(); it1 != waitForDevices.cend(); ++it1) { const DeviceQuery& q = *it1; @@ -113,16 +114,16 @@ namespace direct_bt::BTDeviceRegistry { } return true; } - static void printList(FILE *out, const std::string &msg, std::unordered_set<DeviceID> &cont) { - jau::fprintf_td(out, "%s ", msg.c_str()); - jau::for_each(cont.cbegin(), cont.cend(), [out](const DeviceID &id) { - fprintf(out, "%s, ", id.toString().c_str()); - }); - fprintf(out, "\n"); - } - void printDevicesProcessed(FILE *out, const std::string &msg) { + std::string getProcessedDevicesString() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor - printList(out, msg, devicesProcessed); + std::string res; + jau::for_each(devicesProcessed.cbegin(), devicesProcessed.cend(), [&res](const DeviceID &id) { + if( res.length() > 0 ) { + res.append( ", " ); + } + res.append( id.toString() ); + }); + return res; } jau::darray<DeviceID> getProcessedDevices() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor @@ -141,11 +142,11 @@ namespace direct_bt::BTDeviceRegistry { devicesProcessed.clear(); } - void addToDevicesProcessing(const BDAddressAndType &a, const std::string& n) { + void addToProcessingDevices(const BDAddressAndType &a, const std::string& n) { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessing); // RAII-style acquire and relinquish via destructor devicesInProcessing.emplace_hint(devicesInProcessing.end(), a, n); } - bool removeFromDevicesProcessing(const BDAddressAndType &a) { + bool removeFromProcessingDevices(const BDAddressAndType &a) { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessing); // RAII-style acquire and relinquish via destructor auto it = devicesInProcessing.find( DeviceID(a, "") ); if( devicesInProcessing.end() != it ) { @@ -158,7 +159,7 @@ namespace direct_bt::BTDeviceRegistry { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessing); // RAII-style acquire and relinquish via destructor return devicesInProcessing.end() != devicesInProcessing.find( DeviceID(a, "") ); } - size_t getDeviceProcessingCount() { + size_t getProcessingDeviceCount() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessing); // RAII-style acquire and relinquish via destructor return devicesInProcessing.size(); } |