25package trial.org.direct_bt;
27import java.lang.reflect.InvocationTargetException;
28import java.util.Arrays;
31import org.direct_bt.BTMode;
32import org.direct_bt.BTSecurityLevel;
33import org.direct_bt.AdapterSettings;
34import org.direct_bt.AdapterStatusListener;
35import org.direct_bt.BTAdapter;
36import org.direct_bt.BTDevice;
37import org.direct_bt.BTDeviceRegistry;
38import org.direct_bt.BTException;
39import org.direct_bt.BTFactory;
40import org.direct_bt.BTManager;
41import org.direct_bt.BTSecurityRegistry;
42import org.direct_bt.DiscoveryPolicy;
43import org.direct_bt.EIRDataTypeSet;
44import org.direct_bt.EInfoReport;
45import org.direct_bt.HCIStatusCode;
46import org.direct_bt.PairingMode;
47import org.direct_bt.SMPKeyBin;
48import org.jau.io.PrintUtil;
49import org.jau.net.EUI48;
50import org.jau.sys.Clock;
51import org.junit.Assert;
65 static final long timeout_preempt_diff = 500;
67 final Object mtx_sync =
new Object();
72 int client_power_down_count = 0;
73 int client_power_up_count = 0;
74 boolean client_reset_at_ready =
false;
75 boolean server_reset_at_ready =
false;
76 boolean client_reset_test =
false;
77 boolean server_reset_test =
false;
79 final void set_client_reset_at_ready(
final boolean v) { client_reset_at_ready = v; client_reset_test = v; }
80 final void set_server_reset_at_ready(
final boolean v) { server_reset_at_ready = v; server_reset_test = v; }
82 final void test8x_fullCycle(
final long timeout_value,
83 final String suffix,
final int protocolSessionCount,
final boolean server_client_order,
97 test8x_fullCycle(timeout_value, suffix,
100 secLevelServer, serverExpPairing, client,
101 secLevelClient, clientExpPairing);
104 final void test8x_fullCycle(
final long timeout_value,
final String suffix,
105 final int max_connections_per_session,
final boolean expSuccess,
final boolean server_client_order,
111 final long t0 = Clock.currentTimeMillis();
116 }
catch (
BTException | NoSuchMethodException | SecurityException
117 | IllegalAccessException | IllegalArgumentException
118 | InvocationTargetException | ClassNotFoundException e) {
120 Assert.assertNull(
"Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage(), e);
122 if(
null == manager ) {
126 final List<BTAdapter> adapters = manager.
getAdapters();
127 PrintUtil.println(System.err,
"Adapter: Count "+adapters.size()+
": "+adapters.toString());
128 Assert.assertTrue(
"Adapter count not >= 2 but "+adapters.size(), adapters.size() >= 2);
131 final DBTEndpoint.ChangedAdapterSetListener myChangedAdapterSetListener =
134 final String serverName = server.
getName();
138 sec.sec_level = secLevelClient;
141 synchronized( mtx_sync ) {
142 lastCompletedDevice =
null;
145 lastCompletedDeviceEIR =
null;
151 final boolean initialSetting = oldmask.
isEmpty();
153 synchronized( mtx_sync ) {
155 ++client_power_up_count;
157 ++client_power_down_count;
164 public void deviceReady(
final BTDevice device,
final long timestamp) {
165 synchronized( mtx_sync ) {
166 lastCompletedDevice = device;
170 PrintUtil.println(System.err,
"XXXXXX Client Ready: "+device);
171 if( client_reset_at_ready ) {
172 client_reset_at_ready =
false;
173 PrintUtil.fprintf_td(System.err,
"XXXXXX Client Reset.0: %s\n", device.
toString());
175 PrintUtil.fprintf_td(System.err,
"XXXXXX Client Reset.X: %s: %s\n", rr.toString(), device.
toString());
194 long test_duration = 0;
195 boolean done =
false;
196 boolean timeout =
false;
197 boolean max_connections_hit =
false;
199 synchronized( mtx_sync ) {
202 null == lastCompletedDevice ||
205 max_connections_hit = ( protocolSessionCount * max_connections_per_session ) <= server.
getDisconnectCount();
206 test_duration = Clock.currentTimeMillis() - t0;
207 timeout = 0 < timeout_value && timeout_value <= test_duration + timeout_preempt_diff;
208 if( !done && !max_connections_hit && !timeout ) {
209 try { Thread.sleep(88); }
catch (
final InterruptedException e) { e.printStackTrace(); }
211 }
while( !done && !max_connections_hit && !timeout );
212 test_duration = Clock.currentTimeMillis() - t0;
214 PrintUtil.fprintf_td(System.err,
"\n\n");
215 PrintUtil.fprintf_td(System.err,
"****** Test Stats: duration %d ms, timeout[hit %b, value %d ms], max_connections hit %b\n",
216 test_duration, timeout, timeout_value, max_connections_hit);
217 PrintUtil.fprintf_td(System.err,
" Server ProtocolSessions[success %d/%d total, requested %d], disconnects %d of %d max\n",
220 PrintUtil.fprintf_td(System.err,
" Client ProtocolSessions[success %d/%d total, requested %d], disconnects %d of %d max, power[down %d, up %d]\n",
223 client_power_down_count, client_power_up_count);
224 PrintUtil.fprintf_td(System.err,
"\n\n");
227 Assert.assertFalse( max_connections_hit );
228 Assert.assertFalse( timeout );
230 synchronized( mtx_sync ) {
235 Assert.assertNotNull(lastCompletedDevice);
236 Assert.assertNotNull(lastCompletedDeviceEIR);
238 Assert.assertTrue( ( 1 * max_connections_per_session ) > server.
getDisconnectCount() );
239 if( client_reset_test ) {
240 Assert.assertEquals( 1, client_power_down_count );
241 Assert.assertEquals( 1, client_power_up_count );
243 Assert.assertEquals( 0, client_power_down_count );
244 Assert.assertEquals( 0, client_power_up_count );
254 client.
close(
"test"+suffix+
"_close");
268 Assert.assertEquals(secLevelClient, clientKeysSecLevel);
277 Assert.assertTrue(
"PairingMode client "+lastCompletedDevicePairingMode+
" not > NONE",
PairingMode.
NONE.value < lastCompletedDevicePairingMode.
value);
278 Assert.assertTrue(
"SecurityLevel client "+lastCompletedDeviceSecurityLevel+
" not >= "+secLevelClient, secLevelClient.
value <= lastCompletedDeviceSecurityLevel.
value);
281 Assert.assertTrue(
"PairingMode client "+lastCompletedDevicePairingMode+
" not > NONE",
PairingMode.
NONE.value < lastCompletedDevicePairingMode.
value);
282 Assert.assertTrue(
"SecurityLevel client "+lastCompletedDeviceSecurityLevel+
" not >= "+secLevelClient, secLevelClient.
value <= lastCompletedDeviceSecurityLevel.
value);
293 synchronized( mtx_sync ) {
294 PrintUtil.println(System.err,
"lastCompletedDevice.connectedEIR: "+lastCompletedDeviceEIR.
toString());
300 Assert.assertEquals(serverName, lastCompletedDeviceEIR.
getName());
303 PrintUtil.println(System.err,
"lastCompletedDevice.currentEIR: "+eir.
toString());
305 Assert.assertEquals(0, eir.
getName().length());
324 PrintUtil.println(System.err,
"****** EOL Removed ChangedAdapterSetCallback " + count);
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2020 Gothel Software e.K.
boolean isSet(final SettingType bit)
BTAdapter status listener for remote BTDevice discovery events: Added, updated and removed; as well a...
Application toolkit providing BT device registration of processed and awaited devices.
static void addToWaitForDevices(final String addrOrNameSub)
One stop BTManager API entry point.
static synchronized BTManager getDirectBTManager()
Returns an initialized BluetoothManager instance using the DirectBT implementation.
Application toolkit providing BT security setup and its device association on a pattern matching basi...
static BTSecurityRegistry.Entry getOrCreate(final String addrOrNameSub)
Determines whether the given addrOrNameSub is a EUI48Sub or just a name and retrieves an entry.
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
Collection of 'Extended Advertising Data' (EAD), 'Advertising Data' (AD) or 'Extended Inquiry Respons...
final boolean isSet(final EIRDataTypeSet.DataType bit)
final EIRDataTypeSet getEIRDataMask()
native String toString(final boolean includeServices)
Storage for SMP keys including required connection parameter per local adapter and remote device.
static SMPKeyBin read(final String fname, final boolean verbose_)
Create a new SMPKeyBin instance based upon stored file denoted by fname.
final BTSecurityLevel getSecLevel()
This central BTRole::Master participant works with DBTServer00.
void setRemoveDevice(final boolean v)
Set remove device when disconnecting.
void setDisconnectDeviceed(final boolean v)
Set disconnect after processing.
void setDiscoveryPolicy(final DiscoveryPolicy v)
Set DiscoveryPolicy.
void setProtocolSessionsLeft(final int v)
Testing a full Bluetooth server and client lifecycle of operations, requiring two BT adapter:
static final int max_connections_per_session
static final String CLIENT_KEY_PATH
This peripheral BTRole::Slave test participant works with DBTClient00.
void setProtocolSessionsLeft(final int v)
Bits representing 'BTAdapter setting' data fields.
Bluetooth adapter operating mode.
DUAL
Dual Bluetooth mode, i.e.
Bluetooth Security Level.
NONE
No encryption and no authentication.
ENC_ONLY
Encryption and no authentication (no MITM).
Discovery policy defines the BTAdapter discovery mode after connecting a remote BTDevice:
PAUSE_CONNECTED_UNTIL_DISCONNECTED
Pause discovery until all connected BTDevice become disconnected, effectively until AdapterStatusList...
Each enum represents a 'Extended Inquiry Response' (EIR) data field type bit.
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
Bluetooth secure pairing mode.
NONE
No pairing mode, implying no secure connections, no encryption and no MITM protection.
PRE_PAIRED
Reusing encryption keys from previous pairing.
BTAdapter represents one local Bluetooth Controller.
HCIStatusCode reset()
Reset the adapter.
int removeAllStatusListener()
Remove all AdapterStatusListener from the list.
boolean addStatusListener(final AdapterStatusListener listener)
Add the given AdapterStatusListener to the list if not already present.
boolean isDiscovering()
Returns true if the meta discovering state is not ScanType#NONE.
BTDevice represents one remote Bluetooth device.
BTAdapter getAdapter()
Returns the adapter on which this device was discovered or connected.
BTSecurityLevel getConnSecurityLevel()
Return the BTSecurityLevel, determined when the connection is established.
boolean getConnected()
Returns the connected state of the device.
EInfoReport getEIR()
Return the merged advertised EInfoReport for this remote device.
PairingMode getPairingMode()
Returns the current PairingMode used by the device.
A thread safe singleton handler of the BTAdapter manager, e.g.
List< BTAdapter > getAdapters()
Returns a list of BluetoothAdapters available in the system.
int removeChangedAdapterSetListener(final ChangedAdapterSetListener l)
Remove the given ChangedAdapterSetListener from this manager.
HCIStatusCode startDiscovery(String msg)
HCIStatusCode stopDiscovery(String msg)
static void startDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
int getProtocolSessionsLeft()
String getName()
Return name of this endpoint, which becomes the adapter's name.
static void checkInitializedState(final DBTEndpoint endp)
void close(final String msg)
int getProtocolSessionsDoneTotal()
int getProtocolSessionsDoneSuccess()
static void stopDiscovery(final BTAdapter adapter, final boolean current_exp_discovering_state)
BTAdapter getAdapter()
Return the adapter for this endpoint.
static ChangedAdapterSetListener initChangedAdapterSetListener(final BTManager manager, final List< DBTEndpoint > endpts)
HCIStatusCode startAdvertising(String msg)
static void stop(final DBTServerTest server, final String msg)