diff options
author | Sven Gothel <[email protected]> | 2022-02-07 21:50:31 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-02-07 21:50:31 +0100 |
commit | 3608b45464094ea465a490a0095863eae2aa4328 (patch) | |
tree | 16a806bf5ce42329fe996ffd3b91fa7c80a85347 /trial | |
parent | 72b403ff0f28d099928269120ddcec1835980f11 (diff) |
Refactor TestDBTClientServer* (reuse tasks); Adding server-adapter for client-mode discovery test post client connection
Diffstat (limited to 'trial')
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTClient00.java | 7 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTClientTest.java | 74 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTEndpoint.java | 123 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTServer00.java | 14 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTServerTest.java | 76 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/TestDBTClientServer00.java | 89 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/TestDBTClientServer10.java | 121 |
7 files changed, 343 insertions, 161 deletions
diff --git a/trial/java/trial/org/direct_bt/DBTClient00.java b/trial/java/trial/org/direct_bt/DBTClient00.java index 19721e8b..133d6ae9 100644 --- a/trial/java/trial/org/direct_bt/DBTClient00.java +++ b/trial/java/trial/org/direct_bt/DBTClient00.java @@ -58,7 +58,7 @@ import org.jau.net.EUI48; /** * This Java scanner {@link BTRole::Master} test case, working with {@link DBTServer00}. */ -public class DBTClient00 { +public class DBTClient00 implements DBTClientTest { /** * Disconnect after processing. * @@ -102,9 +102,11 @@ public class DBTClient00 { this.useAdapter = useAdapter; this.btMode = btMode; } + @Override public void setAdapter(final BTAdapter clientAdapter) { this.clientAdapter = clientAdapter; } + @Override public BTAdapter getAdapter() { return clientAdapter; } static void printf(final String format, final Object... args) { @@ -563,6 +565,7 @@ public class DBTClient00 { static final byte filter_policy = (byte)0; // default value static final boolean filter_dup = true; // default value + @Override public HCIStatusCode startDiscovery(final BTAdapter adapter, final String msg) { if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) { BTUtils.fprintf_td(System.err, "****** Start discovery (%s): Adapter not selected: %s\n", msg, adapter.toString()); @@ -576,6 +579,7 @@ public class DBTClient00 { return status; } + @Override public HCIStatusCode stopDiscovery(final BTAdapter adapter, final String msg) { if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) { BTUtils.fprintf_td(System.err, "****** Stop discovery (%s): Adapter not selected: %s\n", msg, adapter.toString()); @@ -587,6 +591,7 @@ public class DBTClient00 { return status; } + @Override public boolean initAdapter(final BTAdapter adapter) { if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) { BTUtils.fprintf_td(System.err, "initClientAdapter: Adapter not selected: %s\n", adapter.toString()); diff --git a/trial/java/trial/org/direct_bt/DBTClientTest.java b/trial/java/trial/org/direct_bt/DBTClientTest.java new file mode 100644 index 00000000..7a91f440 --- /dev/null +++ b/trial/java/trial/org/direct_bt/DBTClientTest.java @@ -0,0 +1,74 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * 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 trial.org.direct_bt; + +import org.direct_bt.BTAdapter; +import org.direct_bt.BTRole; +import org.direct_bt.HCIStatusCode; +import org.junit.Assert; + +public interface DBTClientTest extends DBTEndpoint { + + @Override + boolean initAdapter(BTAdapter adapter); + + @Override + void setAdapter(BTAdapter clientAdapter); + + @Override + BTAdapter getAdapter(); + + HCIStatusCode startDiscovery(BTAdapter adapter, String msg); + + HCIStatusCode stopDiscovery(BTAdapter adapter, String msg); + + public static void startDiscovery(final DBTClientTest client, final boolean current_exp_discovering_state, final String msg) { + final BTAdapter adapter = client.getAdapter(); + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering()); + + Assert.assertEquals( HCIStatusCode.SUCCESS, client.startDiscovery(adapter, msg) ); + while( !adapter.isDiscovering() ) { // pending action + try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } + } + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertTrue(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + } + + public static void stopDiscovery(final DBTClientTest client, final boolean current_exp_discovering_state, final String msg) { + final BTAdapter adapter = client.getAdapter(); + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + + Assert.assertEquals( HCIStatusCode.SUCCESS, client.stopDiscovery(client.getAdapter(), msg) ); + while( adapter.isDiscovering() ) { // pending action + try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } + } + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertFalse(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + } +}
\ No newline at end of file diff --git a/trial/java/trial/org/direct_bt/DBTEndpoint.java b/trial/java/trial/org/direct_bt/DBTEndpoint.java new file mode 100644 index 00000000..71e4f637 --- /dev/null +++ b/trial/java/trial/org/direct_bt/DBTEndpoint.java @@ -0,0 +1,123 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * 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 trial.org.direct_bt; + +import java.util.ArrayList; +import java.util.List; + +import org.direct_bt.BTAdapter; +import org.direct_bt.BTManager; +import org.direct_bt.BTRole; +import org.direct_bt.BTUtils; +import org.direct_bt.HCIStatusCode; +import org.junit.Assert; + +public interface DBTEndpoint { + + void setAdapter(BTAdapter serverAdapter); + + BTAdapter getAdapter(); + + boolean initAdapter(BTAdapter adapter); + + public static void checkInitializedState(final DBTEndpoint endp) { + final BTAdapter adapter = endp.getAdapter(); + Assert.assertTrue( adapter.isInitialized() ); + Assert.assertTrue( adapter.isPowered() ); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + Assert.assertTrue( 4 <= adapter.getBTMajorVersion() ); + } + + public static ChangedAdapterSetListener initChangedAdapterSetListener(final BTManager manager, final List<DBTEndpoint> endpts) { + final ChangedAdapterSetListener casl = new ChangedAdapterSetListener(endpts); + manager.addChangedAdapterSetListener(casl); + for(final DBTEndpoint endpt : endpts ) { + Assert.assertNotNull("No adapter found for "+endpt.getClass().getSimpleName(), endpt.getAdapter()); + } + return casl; + } + public static class ChangedAdapterSetListener implements BTManager.ChangedAdapterSetListener { + List<DBTEndpoint> endpts = new ArrayList<DBTEndpoint>(); + + public ChangedAdapterSetListener() { } + public ChangedAdapterSetListener(final List<DBTEndpoint> el) { + endpts.addAll(el); + } + public boolean add(final DBTEndpoint e) { return endpts.add(e); } + + @Override + public void adapterAdded(final BTAdapter adapter) { + for(final DBTEndpoint endpt : endpts ) { + if( null == endpt.getAdapter() ) { + if( endpt.initAdapter( adapter ) ) { + endpt.setAdapter(adapter); + BTUtils.println(System.err, "****** Adapter-"+endpt.getClass().getSimpleName()+" ADDED__: InitOK: " + adapter); + return; + } + } + } + BTUtils.println(System.err, "****** Adapter ADDED__: Ignored: " + adapter); + } + + @Override + public void adapterRemoved(final BTAdapter adapter) { + for(final DBTEndpoint endpt : endpts ) { + if( null != endpt.getAdapter() && adapter == endpt.getAdapter() ) { + endpt.setAdapter(null); + BTUtils.println(System.err, "****** Adapter-"+endpt.getClass().getSimpleName()+" REMOVED: " + adapter); + return; + } + } + BTUtils.println(System.err, "****** Adapter REMOVED: Ignored " + adapter); + } + }; + + public static void startDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state) { + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering()); + + Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.startDiscovery() ); + while( !adapter.isDiscovering() ) { // pending action + try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } + } + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertTrue(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + } + + public static void stopDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state) { + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertEquals(current_exp_discovering_state, adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + + Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.stopDiscovery() ); // pending action + while( adapter.isDiscovering() ) { // pending action + try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } + } + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertFalse(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Master, adapter.getRole() ); + } + +}
\ No newline at end of file diff --git a/trial/java/trial/org/direct_bt/DBTServer00.java b/trial/java/trial/org/direct_bt/DBTServer00.java index 571387a6..713504c5 100644 --- a/trial/java/trial/org/direct_bt/DBTServer00.java +++ b/trial/java/trial/org/direct_bt/DBTServer00.java @@ -63,7 +63,7 @@ import org.jau.net.EUI48; /** * This Java peripheral {@link BTRole::Slave} test case working with {@link DBTClient00}. */ -public class DBTServer00 { +public class DBTServer00 implements DBTServerTest { final boolean GATT_VERBOSE = false; boolean SHOW_UPDATE_EVENTS = false; @@ -93,9 +93,18 @@ public class DBTServer00 { public DBTServer00(final String adapterName, final BTSecurityLevel adapterSecurityLevel) { this(EUI48.ALL_DEVICE, BTMode.DUAL, true /* SC */, adapterName, adapterSecurityLevel); } + + @Override + public String getName() { return adapterName; } + + @Override + public BTSecurityLevel getSecurityLevel() { return adapterSecurityLevel; } + + @Override public void setAdapter(final BTAdapter serverAdapter) { this.serverAdapter = serverAdapter; } + @Override public BTAdapter getAdapter() { return serverAdapter; } boolean matches(final List<BDAddressAndType> cont, final BDAddressAndType mac) { @@ -591,6 +600,7 @@ public class DBTServer00 { static final byte adv_chan_map=(byte)0x07; 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, "****** Stop advertising (%s): Adapter not selected: %s\n", msg, adapter.toString()); @@ -601,6 +611,7 @@ public class DBTServer00 { 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, "****** Start advertising (%s): Adapter not selected: %s\n", msg, adapter.toString()); @@ -657,6 +668,7 @@ public class DBTServer00 { servedConnections.addAndGet(1); } + @Override public boolean initAdapter(final BTAdapter adapter) { if( !useAdapter.equals(EUI48.ALL_DEVICE) && !useAdapter.equals(adapter.getAddressAndType().address) ) { BTUtils.fprintf_td(System.err, "initServerAdapter: Adapter not selected: %s\n", adapter.toString()); diff --git a/trial/java/trial/org/direct_bt/DBTServerTest.java b/trial/java/trial/org/direct_bt/DBTServerTest.java new file mode 100644 index 00000000..ba542cc6 --- /dev/null +++ b/trial/java/trial/org/direct_bt/DBTServerTest.java @@ -0,0 +1,76 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * 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 trial.org.direct_bt; + +import org.direct_bt.BTAdapter; +import org.direct_bt.BTRole; +import org.direct_bt.BTSecurityLevel; +import org.direct_bt.HCIStatusCode; +import org.junit.Assert; + +public interface DBTServerTest extends DBTEndpoint { + + @Override + boolean initAdapter(BTAdapter adapter); + + @Override + void setAdapter(BTAdapter serverAdapter); + + @Override + BTAdapter getAdapter(); + + String getName(); + + BTSecurityLevel getSecurityLevel(); + + HCIStatusCode stopAdvertising(BTAdapter adapter, String msg); + + HCIStatusCode startAdvertising(BTAdapter adapter, 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.assertTrue(adapter.isAdvertising()); + Assert.assertFalse(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Slave, adapter.getRole() ); + Assert.assertEquals( server.getName(), adapter.getName() ); + + } + + public static void stopAdvertising(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) ); + Assert.assertFalse(adapter.isAdvertising()); + Assert.assertFalse(adapter.isDiscovering()); + Assert.assertEquals( BTRole.Slave, adapter.getRole() ); // kept + } +}
\ No newline at end of file diff --git a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java index 161a65a5..da18ba5e 100644 --- a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java +++ b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java @@ -26,6 +26,7 @@ package trial.org.direct_bt; import java.lang.invoke.MethodHandles; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.List; import org.direct_bt.BTMode; @@ -36,7 +37,6 @@ import org.direct_bt.BTException; import org.direct_bt.BTFactory; import org.direct_bt.BTManager; import org.direct_bt.BTUtils; -import org.direct_bt.HCIStatusCode; import org.jau.net.EUI48; import org.junit.Assert; import org.junit.BeforeClass; @@ -45,7 +45,10 @@ import org.junit.Test; import org.junit.runners.MethodSorters; /** - * Basic client and server Bluetooth tests, requiring one BT adapter. + * Basic client and server Bluetooth tests, requiring one BT adapter: + * - start server advertising + * - server stop advertising + * - reuse server-adapter for client-mode discovery (just toggle on/off) */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestDBTClientServer00 extends BaseDBTClientServer { @@ -92,13 +95,6 @@ public class TestDBTClientServer00 extends BaseDBTClientServer { } } - /** - * Testing start and stop advertising (server mode) using a full DBGattServer, - * having the adapter in BTRole::Slave. - * - * Thereafter start and stop discovery (client mode), - * having the adapter in BTRole::Client. - */ @Test(timeout = 5000) public final void test10_ServerStartStop_and_ToggleDiscovery() { BTManager manager = null; @@ -117,94 +113,33 @@ public class TestDBTClientServer00 extends BaseDBTClientServer { final String serverName = "TestDBTCS00-T10"; final DBTServer00 server = new DBTServer00(EUI48.ALL_DEVICE, BTMode.DUAL, true /* SC */, serverName, BTSecurityLevel.NONE); - final BTManager.ChangedAdapterSetListener myChangedAdapterSetListener = - new BTManager.ChangedAdapterSetListener() { - @Override - public void adapterAdded(final BTAdapter adapter) { - if( null == server.getAdapter() ) { - if( server.initAdapter( adapter ) ) { - server.setAdapter(adapter); - BTUtils.println(System.err, "****** Adapter-Server ADDED__: InitOK: " + adapter); - return; - } - } - BTUtils.println(System.err, "****** Adapter ADDED__: Ignored: " + adapter); - } - - @Override - public void adapterRemoved(final BTAdapter adapter) { - if( null != server.getAdapter() && adapter == server.getAdapter() ) { - server.setAdapter(null); - BTUtils.println(System.err, "****** Adapter-Server REMOVED: " + adapter); - return; - } - BTUtils.println(System.err, "****** Adapter REMOVED: Ignored " + adapter); - } - }; - - manager.addChangedAdapterSetListener(myChangedAdapterSetListener); - Assert.assertNotNull("No server adapter found", server.getAdapter()); + final DBTEndpoint.ChangedAdapterSetListener myChangedAdapterSetListener = + DBTEndpoint.initChangedAdapterSetListener(manager, Arrays.asList(server)); // // Server start // - Assert.assertTrue( server.getAdapter().isInitialized() ); - Assert.assertTrue( server.getAdapter().isPowered() ); - Assert.assertEquals( BTRole.Master, server.getAdapter().getRole() ); - Assert.assertTrue( 4 <= server.getAdapter().getBTMajorVersion() ); - { - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - - Assert.assertEquals( HCIStatusCode.SUCCESS, server.startAdvertising(server.getAdapter(), "test10_startAdvertising") ); - Assert.assertTrue(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Slave, server.getAdapter().getRole() ); - Assert.assertEquals( serverName, server.getAdapter().getName() ); - } + DBTEndpoint.checkInitializedState(server); + DBTServerTest.startAdvertising(server, false /* current_exp_advertising_state */, "test10_startAdvertising"); // // Server stop // - { - Assert.assertTrue(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - - Assert.assertEquals( HCIStatusCode.SUCCESS, server.stopAdvertising(server.getAdapter(), "test10_stopAdvertising") ); - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Slave, server.getAdapter().getRole() ); // keeps role - } + DBTServerTest.stopAdvertising(server, true /* current_exp_advertising_state */, "test10_stopAdvertising"); // // Now reuse adapter for client mode -> Start discovery + Stop Discovery // { - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - final BTAdapter adapter = server.getAdapter(); { final int r = adapter.removeAllStatusListener(); Assert.assertTrue("Not > 0 removed listener, but "+r, 0 < r ); } - Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.startDiscovery() ); // pending action - while( !adapter.isDiscovering() ) { - try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } - } - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertTrue(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Master, server.getAdapter().getRole() ); // changed role - - Assert.assertEquals( HCIStatusCode.SUCCESS, adapter.stopDiscovery() ); // pending action - while( adapter.isDiscovering() ) { - try { Thread.sleep(100); } catch (final InterruptedException e) { e.printStackTrace(); } - } - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Master, server.getAdapter().getRole() ); // keeps role + DBTEndpoint.startDiscovery(adapter, false /* current_exp_discovering_state */); + DBTEndpoint.stopDiscovery(adapter, true /* current_exp_discovering_state */); } final int count = manager.removeChangedAdapterSetListener(myChangedAdapterSetListener); diff --git a/trial/java/trial/org/direct_bt/TestDBTClientServer10.java b/trial/java/trial/org/direct_bt/TestDBTClientServer10.java index cceeed9b..231f1cab 100644 --- a/trial/java/trial/org/direct_bt/TestDBTClientServer10.java +++ b/trial/java/trial/org/direct_bt/TestDBTClientServer10.java @@ -25,10 +25,10 @@ package trial.org.direct_bt; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.List; import org.direct_bt.BTMode; -import org.direct_bt.BTRole; import org.direct_bt.BTSecurityLevel; import org.direct_bt.AdapterStatusListener; import org.direct_bt.BTAdapter; @@ -42,7 +42,6 @@ import org.direct_bt.BTUtils; import org.direct_bt.DiscoveryPolicy; import org.direct_bt.EIRDataTypeSet; import org.direct_bt.EInfoReport; -import org.direct_bt.HCIStatusCode; import org.direct_bt.PairingMode; import org.direct_bt.SMPKeyBin; import org.jau.net.EUI48; @@ -59,6 +58,7 @@ import org.junit.runners.MethodSorters; * - client disconnect * - server stop advertising * - security-level: NONE, ENC_ONLY freshly-paired and ENC_ONLY pre-paired + * - reuse server-adapter for client-mode discovery (just toggle on/off) */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestDBTClientServer10 extends BaseDBTClientServer { @@ -114,52 +114,13 @@ public class TestDBTClientServer10 extends BaseDBTClientServer { final BTSecurityRegistry.Entry sec = BTSecurityRegistry.getOrCreate(serverName); sec.sec_level = secLevelClient; } - client.KEEP_CONNECTED = false; // default - client.REMOVE_DEVICE = false; // default and test side-effects + client.KEEP_CONNECTED = false; // default, auto-disconnect after work is done + client.REMOVE_DEVICE = false; // default, test side-effects client.measurementsLeft.set(1); client.discoveryPolicy = DiscoveryPolicy.PAUSE_CONNECTED_UNTIL_DISCONNECTED; - final BTManager.ChangedAdapterSetListener myChangedAdapterSetListener = - new BTManager.ChangedAdapterSetListener() { - @Override - public void adapterAdded(final BTAdapter adapter) { - if( null == server.getAdapter() ) { - if( server.initAdapter( adapter ) ) { - server.setAdapter(adapter); - BTUtils.println(System.err, "****** Adapter-Server ADDED__: InitOK: " + adapter); - return; - } - } - if( null == client.getAdapter() ) { - if( client.initAdapter( adapter ) ) { - client.setAdapter(adapter); - BTUtils.println(System.err, "****** Adapter-Client ADDED__: InitOK: " + adapter); - return; - } - } - BTUtils.println(System.err, "****** Adapter ADDED__: Ignored: " + adapter); - } - - @Override - public void adapterRemoved(final BTAdapter adapter) { - if( null != server.getAdapter() && adapter == server.getAdapter() ) { - server.setAdapter(null); - BTUtils.println(System.err, "****** Adapter-Server REMOVED: " + adapter); - return; - } - if( null != client.getAdapter() && adapter == client.getAdapter() ) { - client.setAdapter(null); - BTUtils.println(System.err, "****** Adapter-Client REMOVED: " + adapter); - return; - } - BTUtils.println(System.err, "****** Adapter REMOVED: Ignored " + adapter); - } - }; - - manager.addChangedAdapterSetListener(myChangedAdapterSetListener); - Assert.assertNotNull("No server adapter found", server.getAdapter()); - Assert.assertNotNull("No client adapter found", client.getAdapter()); - + final DBTEndpoint.ChangedAdapterSetListener myChangedAdapterSetListener = + DBTEndpoint.initChangedAdapterSetListener(manager, Arrays.asList(server, client)); lastCompletedDevice = null; lastCompletedDevicePairingMode = PairingMode.NONE; lastCompletedDeviceSecurityLevel = BTSecurityLevel.NONE; @@ -179,37 +140,14 @@ public class TestDBTClientServer10 extends BaseDBTClientServer { // // Server start // - Assert.assertTrue( server.getAdapter().isInitialized() ); - Assert.assertTrue( server.getAdapter().isPowered() ); - Assert.assertEquals( BTRole.Master, server.getAdapter().getRole() ); - Assert.assertTrue( 4 <= server.getAdapter().getBTMajorVersion() ); - { - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - - Assert.assertEquals( HCIStatusCode.SUCCESS, server.startAdvertising(server.getAdapter(), "test"+suffix+"_startAdvertising") ); - Assert.assertTrue(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Slave, server.getAdapter().getRole() ); - Assert.assertEquals( serverName, server.getAdapter().getName() ); - } + DBTEndpoint.checkInitializedState(server); + DBTServerTest.startAdvertising(server, false /* current_exp_advertising_state */, "test"+suffix+"_startAdvertising"); // // Client start // - Assert.assertTrue( client.getAdapter().isInitialized() ); - Assert.assertTrue( client.getAdapter().isPowered() ); - Assert.assertEquals( BTRole.Master, client.getAdapter().getRole() ); - Assert.assertTrue( 4 <= client.getAdapter().getBTMajorVersion() ); - { - Assert.assertFalse(client.getAdapter().isAdvertising()); - Assert.assertFalse(client.getAdapter().isDiscovering()); - - Assert.assertEquals( HCIStatusCode.SUCCESS, client.startDiscovery(client.getAdapter(), "test"+suffix+"_startDiscovery") ); - Assert.assertFalse(client.getAdapter().isAdvertising()); - Assert.assertTrue(client.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Master, client.getAdapter().getRole() ); - } + DBTEndpoint.checkInitializedState(client); + DBTClientTest.startDiscovery(client, false /* current_exp_discovering_state */, "test"+suffix+"_startDiscovery"); while( 1 > server.servedConnections.get() || 1 > client.completedMeasurements.get() || @@ -223,19 +161,20 @@ public class TestDBTClientServer10 extends BaseDBTClientServer { Assert.assertNotNull(lastCompletedDevice); Assert.assertNotNull(lastCompletedDeviceEIR); Assert.assertFalse(lastCompletedDevice.getConnected()); - Assert.assertEquals( HCIStatusCode.SUCCESS, client.stopDiscovery(client.getAdapter(), "test"+suffix+"_stopDiscovery") ); - { - Assert.assertFalse(server.getAdapter().isAdvertising()); // stopped by connection - Assert.assertFalse(server.getAdapter().isDiscovering()); + // + // Client stop + // + DBTClientTest.stopDiscovery(client, true /* current_exp_discovering_state */, "test"+suffix+"_stopDiscovery"); - // Stopping advertising wven if stopped must be OK! - Assert.assertEquals( HCIStatusCode.SUCCESS, server.stopAdvertising(server.getAdapter(), "test"+suffix+"_stopAdvertising") ); - Assert.assertFalse(server.getAdapter().isAdvertising()); - Assert.assertFalse(server.getAdapter().isDiscovering()); - Assert.assertEquals( BTRole.Slave, server.getAdapter().getRole() ); // kept - } + // + // Server stop + // + DBTServerTest.stopAdvertising(server, false /* current_exp_advertising_state */, "test"+suffix+"_stopAdvertising"); + // + // Validating Security Mode + // final SMPKeyBin clientKeys = SMPKeyBin.read(DBTConstants.CLIENT_KEY_PATH, lastCompletedDevice, true /* verbose */); Assert.assertTrue(clientKeys.isValid()); final BTSecurityLevel clientKeysSecLevel = clientKeys.getSecLevel(); @@ -257,6 +196,9 @@ public class TestDBTClientServer10 extends BaseDBTClientServer { } } + // + // Validating EIR + // Assert.assertNotEquals(0, lastCompletedDeviceEIR.getEIRDataMask().mask); Assert.assertTrue( lastCompletedDeviceEIR.isSet(EIRDataTypeSet.DataType.FLAGS) ); Assert.assertTrue( lastCompletedDeviceEIR.isSet(EIRDataTypeSet.DataType.SERVICE_UUID) ); @@ -269,6 +211,21 @@ public class TestDBTClientServer10 extends BaseDBTClientServer { Assert.assertEquals(0, lastCompletedDeviceEIR.getName().length()); } + // + // Now reuse adapter for client mode -> Start discovery + Stop Discovery + // + { + final BTAdapter adapter = server.getAdapter(); + { + final int r = adapter.removeAllStatusListener(); + Assert.assertTrue("Not > 0 removed listener, but "+r, 0 < r ); + } + + DBTEndpoint.startDiscovery(adapter, false /* current_exp_discovering_state */); + + DBTEndpoint.stopDiscovery(adapter, true /* current_exp_discovering_state */); + } + final int count = manager.removeChangedAdapterSetListener(myChangedAdapterSetListener); BTUtils.println(System.err, "****** EOL Removed ChangedAdapterSetCallback " + count); } |