Skip to content

Commit cb024c2

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 11a9679 + 074ca6d commit cb024c2

5 files changed

Lines changed: 178 additions & 7 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package net.sharksystem.asap.android.lora;
2+
3+
import android.bluetooth.BluetoothAdapter;
4+
import android.bluetooth.BluetoothDevice;
5+
import android.bluetooth.BluetoothSocket;
6+
import android.content.Context;
7+
8+
import org.junit.AfterClass;
9+
import org.junit.BeforeClass;
10+
import org.junit.FixMethodOrder;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.MethodSorters;
14+
15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStreamReader;
18+
import java.util.UUID;
19+
20+
import androidx.test.ext.junit.runners.AndroidJUnit4;
21+
import androidx.test.platform.app.InstrumentationRegistry;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
/**
26+
* Instrumented test, which will execute on an Android device.
27+
*
28+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
29+
*/
30+
@RunWith(AndroidJUnit4.class)
31+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
32+
public class RoundTripTimeTest {
33+
34+
public static String LONGEST_MESSAGE = "This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Charact";
35+
36+
public static BluetoothDevice Alice;
37+
public static BluetoothDevice Bob;
38+
public static BluetoothSocket AliceSocket;
39+
public static BluetoothSocket BobSocket;
40+
41+
@BeforeClass
42+
public static void setup() throws IOException, InterruptedException {
43+
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
44+
btAdapter.cancelDiscovery();
45+
46+
for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) {
47+
if (btDevice.getName().indexOf("ASAP-LoRa-1") == 0) {
48+
RoundTripTimeTest.Alice = btDevice;
49+
}
50+
if (btDevice.getName().indexOf("ASAP-LoRa-2") == 0) {
51+
RoundTripTimeTest.Bob = btDevice;
52+
}
53+
}
54+
if (RoundTripTimeTest.Alice == null || RoundTripTimeTest.Bob == null)
55+
throw new IOException("Please Pair BT Modules ASAP-LoRa-1 and ASAP-LoRa-2 to this device!");
56+
57+
RoundTripTimeTest.AliceSocket = RoundTripTimeTest.Alice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
58+
RoundTripTimeTest.BobSocket = RoundTripTimeTest.Bob.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
59+
60+
RoundTripTimeTest.AliceSocket.connect();
61+
RoundTripTimeTest.BobSocket.connect();
62+
63+
Thread.sleep(2000); //Give the BT Modules some time to stabilize
64+
}
65+
66+
@AfterClass
67+
public static void teardown() throws InterruptedException, IOException {
68+
RoundTripTimeTest.AliceSocket.close();
69+
RoundTripTimeTest.BobSocket.close();
70+
Thread.sleep(2000); //Give the BT Modules some time to stabilize
71+
}
72+
@Test
73+
public void usesAppContext() {
74+
// Test if we are running in App Context
75+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
76+
assertEquals("net.sharksystem.asap.example", appContext.getPackageName());
77+
}
78+
79+
@Test(timeout = 30000)
80+
public void checkLongestMessageTime() throws IOException {
81+
this.AliceSocket.getOutputStream().write(("MSSGE:1001:"+LONGEST_MESSAGE+"\n").getBytes());
82+
this.AliceSocket.getOutputStream().flush();
83+
84+
while (true) {
85+
if (this.BobSocket.getInputStream().available() > 0) {
86+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
87+
String deviceResponse = br.readLine().trim();
88+
System.out.print("ASAP LoRaEngine Test Device Response: ");
89+
System.out.println(deviceResponse);
90+
assertEquals(("MSSGE:1000:"+LONGEST_MESSAGE), deviceResponse);
91+
break;
92+
}
93+
}
94+
}
95+
96+
@Test(timeout = 380000)
97+
public void simultaneousLongMessageTest() throws IOException {
98+
this.BobSocket.getOutputStream().write(("MSSGE:1000:"+LONGEST_MESSAGE+"\n").getBytes());
99+
this.AliceSocket.getOutputStream().write(("MSSGE:1001:"+LONGEST_MESSAGE+"\n").getBytes());
100+
this.BobSocket.getOutputStream().flush();
101+
this.AliceSocket.getOutputStream().flush();
102+
103+
while (true) {
104+
if (this.BobSocket.getInputStream().available() > 0) {
105+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
106+
String deviceResponse = br.readLine().trim();
107+
System.out.print("ASAP LoRaEngine Test Device Response: ");
108+
System.out.println(deviceResponse);
109+
assertEquals("MSSGE:1000:"+LONGEST_MESSAGE, deviceResponse);
110+
break;
111+
}
112+
}
113+
114+
while (true) {
115+
if (this.AliceSocket.getInputStream().available() > 0) {
116+
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
117+
String deviceResponse = br.readLine().trim();
118+
System.out.print("ASAP LoRaEngine Test Device Response: ");
119+
System.out.println(deviceResponse);
120+
assertEquals("MSSGE:1001:"+LONGEST_MESSAGE, deviceResponse);
121+
break;
122+
}
123+
}
124+
}
125+
126+
@Test(timeout = 380000)
127+
public void simultaneousLongMessageTest2() throws IOException {
128+
simultaneousLongMessageTest();
129+
}
130+
@Test(timeout = 380000)
131+
public void simultaneousLongMessageTest3() throws IOException {
132+
simultaneousLongMessageTest();
133+
}
134+
@Test(timeout = 380000)
135+
public void simultaneousLongMessageTest4() throws IOException {
136+
simultaneousLongMessageTest();
137+
}
138+
@Test(timeout = 380000)
139+
public void simultaneousLongMessageTest5() throws IOException {
140+
simultaneousLongMessageTest();
141+
}
142+
143+
}

app/src/main/java/net/sharksystem/asap/android/lora/LoRaBTInputOutputStream.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ public LoRaASAPInputStream getASAPInputStream(String mac) {
113113
return this.getASAPInputStream(mac);
114114
}
115115

116+
/**
117+
* Checks if there is an active {@link LoRaASAPInputStream} for this mac
118+
* @param macAddress
119+
* @return
120+
*/
121+
public boolean hasASAPInputStream(String macAddress) {
122+
return this.loRaASAPInputStreams.containsKey(macAddress);
123+
}
124+
116125
public LoRaBTInputStream getInputStream() {
117126
return is;
118127
}
@@ -268,17 +277,19 @@ public int available() throws IOException {
268277
*/
269278
@Override
270279
public void close() {
271-
this.inputStreams.clear();
272280
this.shouldClose = true;
273281

274282
// Check if someone is currently reading.
275283
// If so, notify, else just assume we are closed
276-
if (this.isReading)
284+
if (this.isReading) {
277285
synchronized (this.threadLock) {
286+
this.inputStreams.clear();
278287
this.threadLock.notify();
279288
}
280-
else
289+
} else {
290+
this.inputStreams.clear();
281291
this.wasClosed = true;
292+
}
282293
}
283294

284295
/**
@@ -332,7 +343,7 @@ public int read() throws IOException {
332343
*/
333344
class LoRaASAPOutputStream extends OutputStream {
334345
private final String LoRaAddress;
335-
private byte chunk[] = new byte[174]; //we can't deliver messages that are longer over LoRa
346+
private byte chunk[] = new byte[174]; //we can't deliver messages that are longer than 174 chars over LoRa
336347
private int count = 0;
337348

338349
public LoRaASAPOutputStream(String mac) {

app/src/main/java/net/sharksystem/asap/android/lora/LoRaCommunicationManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class LoRaCommunicationManager extends Thread {
2525
private static final String CLASS_LOG_TAG = "ASAPLoRaCommManager";
2626
private static final long FLUSH_BUFFER_TIMEOUT = 250;
27-
private static final long DISCOVER_MESSAGE_TIMEOUT = 60 * 1000; //60s in ms
27+
private static final long DISCOVER_MESSAGE_TIMEOUT = 5 * 60 * 1000; //5 minutes in ms
2828
private static final long CONNECTION_ACTIVE_TIMEOUT = DISCOVER_MESSAGE_TIMEOUT * 10; //60s in ms
2929
private static LoRaBTInputOutputStream ioStream = null;
3030
private BluetoothDevice btDevice;
@@ -68,6 +68,10 @@ public OutputStream getASAPOutputStream(String mac) {
6868
return this.ioStream.getASAPOutputStream(mac);
6969
}
7070

71+
public boolean hasASAPInputStream(String macAddress) {
72+
return this.ioStream.hasASAPInputStream(macAddress);
73+
}
74+
7175
public InputStream getASAPInputStream(String mac) {
7276
return this.ioStream.getASAPInputStream(mac);
7377
}

app/src/main/java/net/sharksystem/asap/android/lora/LoRaEngine.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,24 @@ public void checkConnectionStatus() {
9999
* @param macAddress
100100
*/
101101
void tryConnect(String macAddress) {
102-
if (this.shouldConnectToMACPeer(macAddress)) {
102+
if (this.shouldConnectToMACPeer(macAddress) && !this.hasActiveConnectionToMACPeer(macAddress)) {
103+
Log.d(this.CLASS_LOG_TAG, "Connection to "
104+
+ macAddress + " not yet established. Trying to start an ASAPSession.");
103105
this.launchASAPConnection(macAddress, this.loRaCommunicationManager.getASAPInputStream(macAddress), this.loRaCommunicationManager.getASAPOutputStream(macAddress));
104106
} else {
105107
Log.d(this.CLASS_LOG_TAG, "Connection to "
106108
+ macAddress + " not needed.");
107109
}
108110
}
111+
112+
/**
113+
* Test if there is a currently active Connection to a macAddress.
114+
* @param macAddress
115+
* @return
116+
*/
117+
private boolean hasActiveConnectionToMACPeer(String macAddress) {
118+
if(this.loRaCommunicationManager != null && this.loRaCommunicationManager.isAlive())
119+
return this.loRaCommunicationManager.hasASAPInputStream(macAddress);
120+
return false;
121+
}
109122
}

app/src/main/java/net/sharksystem/asap/android/lora/messages/AbstractASAPLoRaMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void handleMessage(LoRaCommunicationManager loRaCommunicationManager) thr
3939
}
4040

4141
/**
42-
* Abstract Factory for Messages, creates @{@link ASAPLoRaMessageInterface}-Instances from raw
42+
* Static Factory for Messages, creates @{@link ASAPLoRaMessageInterface}-Instances from raw
4343
* messages received by the ASAPLoRaBTModule.
4444
*
4545
* @param rawMessage

0 commit comments

Comments
 (0)