aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-05-14 04:45:20 +0200
committerSven Gothel <[email protected]>2022-05-14 04:45:20 +0200
commit313b27cfcf5aa6ea6c16eaec0e6a8452e998d481 (patch)
treee7f57f495f384f8a55a81ae19437a84a24d70088 /examples
parent044c55d46de095721104ed8d511f70c47ee3d008 (diff)
C++/JNI Lifecycle Fix: BTManager must be passed as shared_ptr<> and hold as such in BTAdapter, ... (API CHANGE)
BTAdapter removes itself from BTManager, hence needs to ensure BTManager is not yet destructed. This goes well along with our new JNI mapping, holding the shared_ptr reference in nativeInstance.
Diffstat (limited to 'examples')
-rw-r--r--examples/dbt_peripheral00.cpp8
-rw-r--r--examples/dbt_repeater00.cpp18
-rw-r--r--examples/dbt_scanner10.cpp20
3 files changed, 23 insertions, 23 deletions
diff --git a/examples/dbt_peripheral00.cpp b/examples/dbt_peripheral00.cpp
index 18aeccba..4fb75315 100644
--- a/examples/dbt_peripheral00.cpp
+++ b/examples/dbt_peripheral00.cpp
@@ -599,7 +599,7 @@ static void processDisconnectedDevice(BTDeviceRef device) {
device->remove();
BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType());
- std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait a little (FIXME: Fast restart of advertising error)
+ jau::sleep_for( 100_ms ); // wait a little (FIXME: Fast restart of advertising error)
if( !RUN_ONLY_ONCE ) {
startAdvertising(&device->getAdapter(), "device-disconnected");
@@ -723,11 +723,11 @@ void test() {
std::shared_ptr<MyGATTServerListener> listener = std::make_shared<MyGATTServerListener>();
dbGattServer->addListener( listener );
- BTManager & mngr = BTManager::get();
- mngr.addChangedAdapterSetCallback(myChangedAdapterSetFunc);
+ std::shared_ptr<BTManager> mngr = BTManager::get();
+ mngr->addChangedAdapterSetCallback(myChangedAdapterSetFunc);
while( !RUN_ONLY_ONCE || 0 == servedConnections ) {
- std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+ jau::sleep_for( 2_s );
}
fprintf_td(stderr, "****** Test Shutdown.01 (DBGattServer.remove-listener)\n");
diff --git a/examples/dbt_repeater00.cpp b/examples/dbt_repeater00.cpp
index a84a353e..0f74863a 100644
--- a/examples/dbt_repeater00.cpp
+++ b/examples/dbt_repeater00.cpp
@@ -713,7 +713,7 @@ static void processDisconnectedDeviceToClient(BTDeviceRef device) {
// already unpaired
stopAdvertisingToClient(adapterToClient, "processDisconnectedDeviceToClient");
BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType());
- std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait a little (FIXME: Fast restart of advertising error)
+ jau::sleep_for( 100_ms ); // wait a little (FIXME: Fast restart of advertising error)
BTDeviceRef devToServer;
{
@@ -891,11 +891,11 @@ static bool myChangedAdapterSetFunc(const bool added, std::shared_ptr<BTAdapter>
void test() {
timestamp_t0 = getCurrentMilliseconds();
- BTManager & mngr = BTManager::get();
- mngr.addChangedAdapterSetCallback(myChangedAdapterSetFunc);
+ std::shared_ptr<BTManager> mngr = BTManager::get();
+ mngr->addChangedAdapterSetCallback(myChangedAdapterSetFunc);
while( 0 == MAX_SERVED_CONNECTIONS || MAX_SERVED_CONNECTIONS > servedClientConnections ) {
- std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+ jau::sleep_for( 2_s );
}
adapterToServer = nullptr;
adapterToClient = nullptr;
@@ -903,17 +903,17 @@ void test() {
//
// just a manually controlled pull down to show status, not required
//
- jau::darray<std::shared_ptr<BTAdapter>> adapterList = mngr.getAdapters();
+ jau::darray<std::shared_ptr<BTAdapter>> adapterList = mngr->getAdapters();
jau::for_each_const(adapterList, [](const std::shared_ptr<BTAdapter>& adapter) {
fprintf_td(stderr, "****** EOL Adapter's Devices - pre close: %s\n", adapter->toString().c_str());
adapter->printDeviceLists();
});
{
- int count = mngr.removeChangedAdapterSetCallback(myChangedAdapterSetFunc);
+ int count = mngr->removeChangedAdapterSetCallback(myChangedAdapterSetFunc);
fprintf_td(stderr, "****** EOL Removed ChangedAdapterSetCallback %d\n", count);
- mngr.close();
+ mngr->close();
}
jau::for_each_const(adapterList, [](const std::shared_ptr<BTAdapter>& adapter) {
fprintf_td(stderr, "****** EOL Adapter's Devices - post close: %s\n", adapter->toString().c_str());
@@ -1040,8 +1040,8 @@ int main(int argc, char *argv[])
// Just for testing purpose, i.e. triggering BTManager::close() within the test controlled app,
// instead of program shutdown.
fprintf_td(stderr, "****** Manager close start\n");
- BTManager & mngr = BTManager::get(); // already existing
- mngr.close();
+ std::shared_ptr<BTManager> mngr = BTManager::get(); // already existing
+ mngr->close();
fprintf_td(stderr, "****** Manager close end\n");
}
}
diff --git a/examples/dbt_scanner10.cpp b/examples/dbt_scanner10.cpp
index c3e28dad..b2bb29e8 100644
--- a/examples/dbt_scanner10.cpp
+++ b/examples/dbt_scanner10.cpp
@@ -570,7 +570,7 @@ static void processReadyDevice(BTDeviceRef device) {
fprintf_td(stderr, "\n");
}
// FIXME sleep 1s for potential callbacks ..
- std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+ jau::sleep_for( 1_s );
success = true;
} catch ( std::exception & e ) {
fprintf_td(stderr, "****** Processing Ready Device: Exception caught for %s: %s\n", device->toString().c_str(), e.what());
@@ -589,7 +589,7 @@ exit:
if( KEEP_CONNECTED && GATT_PING_ENABLED && success ) {
while( device->pingGATT() ) {
fprintf_td(stderr, "****** Processing Ready Device: pingGATT OK: %s\n", device->getAddressAndType().toString().c_str());
- std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+ jau::sleep_for( 1_s );
}
fprintf_td(stderr, "****** Processing Ready Device: pingGATT failed, waiting for disconnect: %s\n", device->getAddressAndType().toString().c_str());
// Even w/ GATT_PING_ENABLED, we utilize disconnect event to clean up -> remove
@@ -713,8 +713,8 @@ void test() {
timestamp_t0 = jau::getCurrentMilliseconds();
- BTManager & mngr = BTManager::get();
- mngr.addChangedAdapterSetCallback(myChangedAdapterSetFunc);
+ std::shared_ptr<BTManager> mngr = BTManager::get();
+ mngr->addChangedAdapterSetCallback(myChangedAdapterSetFunc);
while( !done ) {
if( 0 == MULTI_MEASUREMENTS ||
@@ -727,7 +727,7 @@ void test() {
fprintf_td(stderr, "****** DevicesProcessed %s\n", BTDeviceRegistry::getProcessedDevicesString().c_str());
done = true;
} else {
- std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+ jau::sleep_for( 2_s );
}
}
chosenAdapter = nullptr;
@@ -735,17 +735,17 @@ void test() {
//
// just a manually controlled pull down to show status, not required
//
- jau::darray<std::shared_ptr<BTAdapter>> adapterList = mngr.getAdapters();
+ jau::darray<std::shared_ptr<BTAdapter>> adapterList = mngr->getAdapters();
jau::for_each_const(adapterList, [](const std::shared_ptr<BTAdapter>& adapter) {
fprintf_td(stderr, "****** EOL Adapter's Devices - pre close: %s\n", adapter->toString().c_str());
adapter->printDeviceLists();
});
{
- int count = mngr.removeChangedAdapterSetCallback(myChangedAdapterSetFunc);
+ int count = mngr->removeChangedAdapterSetCallback(myChangedAdapterSetFunc);
fprintf_td(stderr, "****** EOL Removed ChangedAdapterSetCallback %d\n", count);
- mngr.close();
+ mngr->close();
}
jau::for_each_const(adapterList, [](const std::shared_ptr<BTAdapter>& adapter) {
fprintf_td(stderr, "****** EOL Adapter's Devices - post close: %s\n", adapter->toString().c_str());
@@ -883,8 +883,8 @@ int main(int argc, char *argv[])
// Just for testing purpose, i.e. triggering BTManager::close() within the test controlled app,
// instead of program shutdown.
fprintf_td(stderr, "****** Manager close start\n");
- BTManager & mngr = BTManager::get(); // already existing
- mngr.close();
+ std::shared_ptr<BTManager> mngr = BTManager::get(); // already existing
+ mngr->close();
fprintf_td(stderr, "****** Manager close end\n");
}
}