aboutsummaryrefslogtreecommitdiffstats
path: root/trial
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-04-15 04:36:45 +0200
committerSven Gothel <[email protected]>2022-04-15 04:36:45 +0200
commitb7389ed1207ce47d133add693aebccaa13afd7ce (patch)
treec9abff25891863148ae64ccbaad8bacde30f3a00 /trial
parent540341b759789974e99f8ef10e1773c96c344cf6 (diff)
TestDBTClientServer*: Cleanup stop()/close() of server and ensure server gets close() at test end
Diffstat (limited to 'trial')
-rw-r--r--trial/java/trial/org/direct_bt/DBTClientServer1x.java3
-rw-r--r--trial/java/trial/org/direct_bt/DBTServer00.java123
-rw-r--r--trial/java/trial/org/direct_bt/DBTServerTest.java13
-rw-r--r--trial/java/trial/org/direct_bt/TestDBTClientServer00.java3
4 files changed, 90 insertions, 52 deletions
diff --git a/trial/java/trial/org/direct_bt/DBTClientServer1x.java b/trial/java/trial/org/direct_bt/DBTClientServer1x.java
index 8e7d0460..2c53cfba 100644
--- a/trial/java/trial/org/direct_bt/DBTClientServer1x.java
+++ b/trial/java/trial/org/direct_bt/DBTClientServer1x.java
@@ -151,7 +151,8 @@ public abstract class DBTClientServer1x extends BaseDBTClientServer {
//
// Server stop
//
- DBTServerTest.stopAdvertising(server, false /* current_exp_advertising_state */, "test"+suffix+"_stopAdvertising");
+ DBTServerTest.stop(server, false /* current_exp_advertising_state */, "test"+suffix+"_stopAdvertising");
+ server.close("test"+suffix+"_close");
//
// Validating Security Mode
diff --git a/trial/java/trial/org/direct_bt/DBTServer00.java b/trial/java/trial/org/direct_bt/DBTServer00.java
index 005d248a..3a0916b6 100644
--- a/trial/java/trial/org/direct_bt/DBTServer00.java
+++ b/trial/java/trial/org/direct_bt/DBTServer00.java
@@ -73,7 +73,10 @@ public class DBTServer00 implements DBTServerTest {
BTMode btMode = BTMode.DUAL;
boolean use_SC = true;
BTSecurityLevel adapterSecurityLevel = BTSecurityLevel.UNSET;
+ private final MyGATTServerListener gattServerListener = new MyGATTServerListener();
BTAdapter serverAdapter = null;
+ private volatile boolean sync_data;
+ private volatile BTDevice connectedDevice;
public AtomicInteger servingConnectionsLeft = new AtomicInteger(1);
public AtomicInteger servedConnections = new AtomicInteger(0);
@@ -85,8 +88,7 @@ public class DBTServer00 implements DBTServerTest {
this.use_SC = use_SC;
this.adapterSecurityLevel = adapterSecurityLevel;
- final MyGATTServerListener listener = new MyGATTServerListener();
- dbGattServer.addListener( listener );
+ dbGattServer.addListener( gattServerListener );
}
public DBTServer00(final String adapterName, final EUI48 useAdapter, final BTSecurityLevel adapterSecurityLevel) {
this(adapterName, useAdapter, BTMode.DUAL, true /* SC */, adapterSecurityLevel);
@@ -108,6 +110,24 @@ public class DBTServer00 implements DBTServerTest {
@Override
public BTAdapter getAdapter() { return serverAdapter; }
+ private final void setDevice(final BTDevice cd) {
+ final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
+ connectedDevice = cd;
+ sync_data = local; // SC-DRF release via sc_atomic_bool::store()
+ }
+
+ private final BTDevice getDevice() {
+ final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
+ final BTDevice d = connectedDevice;
+ sync_data = local; // SC-DRF release via sc_atomic_bool::store()
+ return d;
+ }
+
+ private boolean matches(final BTDevice device) {
+ final BTDevice d = getDevice();
+ return null != d ? d.equals(device) : false;
+ }
+
boolean matches(final List<BDAddressAndType> cont, final BDAddressAndType mac) {
for(final Iterator<BDAddressAndType> it = cont.iterator(); it.hasNext(); ) {
if( it.next().matches(mac) ) {
@@ -274,6 +294,10 @@ public class DBTServer00 implements DBTServerTest {
@Override
public void deviceConnected(final BTDevice device, final short handle, final long timestamp) {
BTUtils.println(System.err, "****** Server CONNECTED (served "+servedConnections.get()+", left "+servingConnectionsLeft.get()+"): handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter());
+ final boolean available = null == getDevice();
+ if( available ) {
+ setDevice(device);
+ }
}
@Override
@@ -336,7 +360,10 @@ public class DBTServer00 implements DBTServerTest {
@Override
public void deviceDisconnected(final BTDevice device, final HCIStatusCode reason, final short handle, final long timestamp) {
BTUtils.println(System.err, "****** Server DISCONNECTED (served "+servedConnections.get()+", left "+servingConnectionsLeft.get()+"): Reason "+reason+", old handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter());
-
+ final boolean match = matches(device);
+ if( match ) {
+ setDevice(null);
+ }
executeOffThread( () -> { processDisconnectedDevice(device); },
"DBT-Disconnected-"+device.getAdapter().getAddressAndType(), true /* detach */);
}
@@ -348,7 +375,6 @@ public class DBTServer00 implements DBTServerTest {
};
class MyGATTServerListener extends DBGattServer.Listener implements AutoCloseable {
- private volatile boolean sync_data;
private final Thread pulseSenderThread;
private volatile boolean stopPulseSender = false;
@@ -357,16 +383,8 @@ public class DBTServer00 implements DBTServerTest {
private volatile short handleResponseDataNotify = 0;
private volatile short handleResponseDataIndicate = 0;
- private BTDevice connectedDevice;
private int usedMTU = 23; // BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU);
- private boolean matches(final BTDevice device) {
- final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- final boolean res = null != connectedDevice ? connectedDevice.equals(device) : false;
- sync_data = local; // SC-DRF release via sc_atomic_bool::store()
- return res;
- }
-
private void clear() {
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
@@ -374,7 +392,6 @@ public class DBTServer00 implements DBTServerTest {
handlePulseDataIndicate = 0;
handleResponseDataNotify = 0;
handleResponseDataIndicate = 0;
- connectedDevice = null;
dbGattServer.resetGattClientCharConfig(DBTConstants.DataServiceUUID, DBTConstants.PulseDataUUID);
dbGattServer.resetGattClientCharConfig(DBTConstants.DataServiceUUID, DBTConstants.ResponseUUID);
@@ -386,21 +403,22 @@ public class DBTServer00 implements DBTServerTest {
while( !stopPulseSender ) {
{
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- if( null != connectedDevice && connectedDevice.getConnected() ) {
+ final BTDevice connectedDevice_ = getDevice();
+ if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) {
final String data = String.format("Dynamic Data Example. Elapsed Milliseconds: %,9d", BTUtils.elapsedTimeMillis());
final byte[] v = data.getBytes(StandardCharsets.UTF_8);
if( 0 != handlePulseDataNotify ) {
if( GATT_VERBOSE ) {
- BTUtils.fprintf_td(System.err, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice.toString());
+ BTUtils.fprintf_td(System.err, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice_.toString());
}
- connectedDevice.sendNotification(handlePulseDataNotify, v);
+ connectedDevice_.sendNotification(handlePulseDataNotify, v);
}
if( 0 != handlePulseDataIndicate ) {
if( GATT_VERBOSE ) {
- BTUtils.fprintf_td(System.err, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice.toString());
+ BTUtils.fprintf_td(System.err, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice_.toString());
}
- connectedDevice.sendIndication(handlePulseDataIndicate, v);
+ connectedDevice_.sendIndication(handlePulseDataIndicate, v);
}
}
}
@@ -413,21 +431,22 @@ public class DBTServer00 implements DBTServerTest {
}
void sendResponse(final byte[] data) {
- if( null != connectedDevice && connectedDevice.getConnected() ) {
+ final BTDevice connectedDevice_ = getDevice();
+ if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
if( 0 != handleResponseDataNotify || 0 != handleResponseDataIndicate ) {
if( 0 != handleResponseDataNotify ) {
if( GATT_VERBOSE ) {
BTUtils.fprintf_td(System.err, "****** Server GATT::sendNotification: %s to %s\n",
- BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice.toString());
+ BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
}
- connectedDevice.sendNotification(handleResponseDataNotify, data);
+ connectedDevice_.sendNotification(handleResponseDataNotify, data);
}
if( 0 != handleResponseDataIndicate ) {
if( GATT_VERBOSE ) {
BTUtils.fprintf_td(System.err, "****** Server GATT::sendIndication: %s to %s\n",
- BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice.toString());
+ BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
}
- connectedDevice.sendIndication(handleResponseDataIndicate, data);
+ connectedDevice_.sendIndication(handleResponseDataIndicate, data);
}
}
}
@@ -442,7 +461,6 @@ public class DBTServer00 implements DBTServerTest {
{
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
stopPulseSender = true;
- connectedDevice = null;
sync_data = local; // SC-DRF release via sc_atomic_bool::store()
}
if( !pulseSenderThread.isDaemon() ) {
@@ -456,11 +474,10 @@ public class DBTServer00 implements DBTServerTest {
@Override
public void connected(final BTDevice device, final int initialMTU) {
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- final boolean available = null == connectedDevice;
- BTUtils.fprintf_td(System.err, "****** Server GATT::connected(available %b): initMTU %d, %s\n",
- available, initialMTU, device.toString());
- if( available ) {
- connectedDevice = device;
+ final boolean match = matches(device);
+ BTUtils.fprintf_td(System.err, "****** Server GATT::connected(match %b): initMTU %d, %s\n",
+ match, initialMTU, device.toString());
+ if( match ) {
usedMTU = initialMTU;
}
sync_data = local; // SC-DRF release via sc_atomic_bool::store()
@@ -469,7 +486,7 @@ public class DBTServer00 implements DBTServerTest {
@Override
public void disconnected(final BTDevice device) {
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- final boolean match = null != connectedDevice ? connectedDevice.equals(device) : false;
+ final boolean match = matches(device);
BTUtils.fprintf_td(System.err, "****** Server GATT::disconnected(match %b): %s\n", match, device.toString());
if( match ) {
clear();
@@ -609,20 +626,38 @@ public class DBTServer00 implements DBTServerTest {
static final byte filter_policy=(byte)0x00;
@Override
- public HCIStatusCode stopAdvertising(final BTAdapter adapter, final String msg) {
- if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) {
- BTUtils.fprintf_td(System.err, "****** Server Stop advertising (%s): Adapter not selected: %s\n", msg, adapter.toString());
+ public HCIStatusCode stop(final String msg) {
+ BTUtils.println(System.err, "****** Server Stop: "+msg);
+ final HCIStatusCode res = stopAdvertising(msg);
+ final BTDevice connectedDevice_ = getDevice();
+ if( null != connectedDevice_ ) {
+ connectedDevice_.disconnect();
+ }
+ gattServerListener.clear();
+ return res;
+ }
+
+ @Override
+ public void close(final String msg) {
+ BTUtils.println(System.err, "****** Server Close: "+msg);
+ stop(msg);
+ dbGattServer.close();
+ }
+
+ private HCIStatusCode stopAdvertising(final String msg) {
+ if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(serverAdapter.getAddressAndType().address) ) {
+ BTUtils.fprintf_td(System.err, "****** Server Stop advertising (%s): Adapter not selected: %s\n", msg, serverAdapter.toString());
return HCIStatusCode.FAILED;
}
- final HCIStatusCode status = adapter.stopAdvertising();
- BTUtils.println(System.err, "****** Server Stop advertising ("+msg+") result: "+status+": "+adapter.toString());
+ final HCIStatusCode status = serverAdapter.stopAdvertising();
+ BTUtils.println(System.err, "****** Server Stop advertising ("+msg+") result: "+status+": "+serverAdapter.toString());
return status;
}
@Override
- public HCIStatusCode startAdvertising(final BTAdapter adapter, final String msg) {
- if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) {
- BTUtils.fprintf_td(System.err, "****** Server Start advertising (%s): Adapter not selected: %s\n", msg, adapter.toString());
+ public HCIStatusCode startAdvertising(final String msg) {
+ if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(serverAdapter.getAddressAndType().address) ) {
+ BTUtils.fprintf_td(System.err, "****** Server Start advertising (%s): Adapter not selected: %s\n", msg, serverAdapter.toString());
return HCIStatusCode.FAILED;
}
final EInfoReport eir = new EInfoReport();
@@ -641,22 +676,22 @@ public class DBTServer00 implements DBTServerTest {
eir.addService(DBTConstants.DataServiceUUID);
eir.setServicesComplete(false);
- eir.setName(adapter.getName());
+ eir.setName(serverAdapter.getName());
eir.setConnInterval((short)8, (short)12); // 10ms - 15ms
final DBGattChar gattDevNameChar = dbGattServer.findGattChar(DBGattService.UUID16.GENERIC_ACCESS, DBGattChar.UUID16.DEVICE_NAME);
if( null != gattDevNameChar ) {
- final byte[] aname_bytes = adapter.getName().getBytes(StandardCharsets.UTF_8);
+ final byte[] aname_bytes = serverAdapter.getName().getBytes(StandardCharsets.UTF_8);
gattDevNameChar.setValue(aname_bytes, 0, aname_bytes.length, 0);
}
BTUtils.println(System.err, "****** Server Start advertising ("+msg+"): EIR "+eir.toString());
BTUtils.println(System.err, "****** Server Start advertising ("+msg+"): adv "+adv_mask.toString()+", scanrsp "+scanrsp_mask.toString());
- final HCIStatusCode status = adapter.startAdvertising(dbGattServer, eir, adv_mask, scanrsp_mask,
+ final HCIStatusCode status = serverAdapter.startAdvertising(dbGattServer, eir, adv_mask, scanrsp_mask,
adv_interval_min, adv_interval_max,
adv_type, adv_chan_map, filter_policy);
- BTUtils.println(System.err, "****** Server Start advertising ("+msg+") result: "+status+": "+adapter.toString());
+ BTUtils.println(System.err, "****** Server Start advertising ("+msg+") result: "+status+": "+serverAdapter.toString());
if( GATT_VERBOSE ) {
BTUtils.println(System.err, dbGattServer.toFullString());
}
@@ -667,7 +702,7 @@ public class DBTServer00 implements DBTServerTest {
BTUtils.println(System.err, "****** Server Disconnected Device (served "+servedConnections.get()+", left "+servingConnectionsLeft.get()+"): Start "+device.toString());
// already unpaired
- stopAdvertising(device.getAdapter(), "device-disconnected");
+ stopAdvertising("device-disconnected");
device.remove();
BTDeviceRegistry.removeFromProcessingDevices(device.getAddressAndType());
@@ -676,7 +711,7 @@ public class DBTServer00 implements DBTServerTest {
} catch (final InterruptedException e) { }
if( servingConnectionsLeft.get() > 0 ) {
- startAdvertising(device.getAdapter(), "device-disconnected");
+ startAdvertising("device-disconnected");
}
BTUtils.println(System.err, "****** Server Disonnected Device: End "+device.toString());
diff --git a/trial/java/trial/org/direct_bt/DBTServerTest.java b/trial/java/trial/org/direct_bt/DBTServerTest.java
index 24464b6b..d7047e3f 100644
--- a/trial/java/trial/org/direct_bt/DBTServerTest.java
+++ b/trial/java/trial/org/direct_bt/DBTServerTest.java
@@ -33,16 +33,17 @@ public interface DBTServerTest extends DBTEndpoint {
BTSecurityLevel getSecurityLevel();
- HCIStatusCode stopAdvertising(BTAdapter adapter, String msg);
+ HCIStatusCode stop(final String msg);
+ void close(final String msg);
- HCIStatusCode startAdvertising(BTAdapter adapter, String msg);
+ HCIStatusCode startAdvertising(String msg);
public static void startAdvertising(final DBTServerTest server, final boolean current_exp_advertising_state, final String msg) {
final BTAdapter adapter = server.getAdapter();
Assert.assertEquals(current_exp_advertising_state, adapter.isAdvertising());
Assert.assertFalse(adapter.isDiscovering());
- Assert.assertEquals( HCIStatusCode.SUCCESS, server.startAdvertising(adapter, msg) );
+ Assert.assertEquals( HCIStatusCode.SUCCESS, server.startAdvertising(msg) );
Assert.assertTrue(adapter.isAdvertising());
Assert.assertFalse(adapter.isDiscovering());
Assert.assertEquals( BTRole.Slave, adapter.getRole() );
@@ -50,14 +51,14 @@ public interface DBTServerTest extends DBTEndpoint {
}
- public static void stopAdvertising(final DBTServerTest server, final boolean current_exp_advertising_state, final String msg) {
+ public static void stop(final DBTServerTest server, final boolean current_exp_advertising_state, final String msg) {
final BTAdapter adapter = server.getAdapter();
Assert.assertEquals(current_exp_advertising_state, adapter.isAdvertising());
Assert.assertFalse(adapter.isDiscovering());
Assert.assertEquals( BTRole.Slave, adapter.getRole() ); // kept
- // Stopping advertising even if stopped must be OK!
- Assert.assertEquals( HCIStatusCode.SUCCESS, server.stopAdvertising(adapter, msg) );
+ // Stopping advertising and serving even if stopped must be OK!
+ Assert.assertEquals( HCIStatusCode.SUCCESS, server.stop(msg) );
Assert.assertFalse(adapter.isAdvertising());
Assert.assertFalse(adapter.isDiscovering());
Assert.assertEquals( BTRole.Slave, adapter.getRole() ); // kept
diff --git a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java
index 03c47b9b..9d90b1c3 100644
--- a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java
+++ b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java
@@ -116,7 +116,8 @@ public class TestDBTClientServer00 extends BaseDBTClientServer {
//
// Server stop
//
- DBTServerTest.stopAdvertising(server, true /* current_exp_advertising_state */, "test10_stopAdvertising");
+ DBTServerTest.stop(server, true /* current_exp_advertising_state */, "test10_stopAdvertising");
+ server.close("test10_close");
//
// Now reuse adapter for client mode -> Start discovery + Stop Discovery