Skip to content

Commit 9035b6c

Browse files
committed
First Message Passing Structs
Added polymorph asapmessage, moved bt handling to commManager Thread, sent first test Message
1 parent 7046d58 commit 9035b6c

File tree

10 files changed

+241
-53
lines changed

10 files changed

+241
-53
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ public class ASAPLoRaException extends ASAPException {
77
public ASAPLoRaException(String message) {
88
super(message);
99
}
10+
11+
public ASAPLoRaException(Exception e){
12+
super(e.getMessage(), e.getCause());
13+
}
1014
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package net.sharksystem.asap.android.lora;
2+
3+
import android.bluetooth.BluetoothSocket;
4+
import android.util.Log;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
8+
9+
import net.sharksystem.asap.android.lora.messages.AbstractASAPLoRaMessage;
10+
import net.sharksystem.asap.android.lora.messages.RawASAPLoRaMessage;
11+
12+
import java.io.BufferedReader;
13+
import java.io.FilterInputStream;
14+
import java.io.FilterOutputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.io.OutputStream;
19+
20+
21+
public class LoRaBTInputOutputStream {
22+
/**
23+
* Die IS / OS in dieser Klasse schreiben/lesen auf einem BluetoothSocket
24+
* Alle Adressen nutzen I/O Stream des BluetoothSocket, daher kapseln wir diesen in einel LoraBTInputStream
25+
* --> Adresse wird mit an uC gesendet, damit dieser an die richtige Stelle schreibt.
26+
* Syntax (erstidee): "ADDR:datadatadatadata"
27+
*/
28+
private static final String CLASS_LOG_TAG = "ASAPLoRaBTIOStream";
29+
private final ObjectMapper objectMapper = new ObjectMapper();
30+
private BluetoothSocket btSocket;
31+
private LoRaBTInputStream is;
32+
private LoRaBTOutputStream os;
33+
34+
LoRaBTInputOutputStream(/*String mac, */BluetoothSocket btSocket) throws IOException {
35+
//this.LoRaAddress = mac;
36+
this.btSocket = btSocket;
37+
this.is = new LoRaBTInputStream(btSocket.getInputStream());
38+
this.os = new LoRaBTOutputStream(btSocket.getOutputStream());
39+
40+
//Use Polymorphic Type Detection for JSON Object Mapping
41+
objectMapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(), ObjectMapper.DefaultTyping.NON_FINAL);
42+
}
43+
44+
public void close() {
45+
try {
46+
if (this.btSocket != null)
47+
btSocket.close();
48+
}catch (IOException e){
49+
Log.e(this.CLASS_LOG_TAG, e.getMessage());
50+
}
51+
}
52+
53+
public LoRaBTInputStream getInputStream() {
54+
return is;
55+
}
56+
57+
public LoRaBTOutputStream getOutputStream() {
58+
return os;
59+
}
60+
61+
class LoRaBTInputStream extends FilterInputStream {
62+
63+
public AbstractASAPLoRaMessage readASAPLoRaMessage() throws IOException {
64+
BufferedReader br = new BufferedReader(new InputStreamReader(this));
65+
return objectMapper.readValue(br.readLine(), AbstractASAPLoRaMessage.class);
66+
}
67+
68+
public LoRaBTInputStream(InputStream in) {
69+
super(in);
70+
}
71+
}
72+
73+
class LoRaBTOutputStream extends FilterOutputStream {
74+
public LoRaBTOutputStream(OutputStream out) {
75+
super(out);
76+
}
77+
78+
public void write(AbstractASAPLoRaMessage msg) throws IOException {
79+
if (msg instanceof RawASAPLoRaMessage)
80+
this.write(msg.toString().getBytes());
81+
else
82+
this.write(objectMapper.writeValueAsString(msg).getBytes());
83+
}
84+
}
85+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.util.Log;
7+
8+
import net.sharksystem.asap.android.lora.messages.ASAPLoRaMessage;
9+
import net.sharksystem.asap.android.lora.messages.AbstractASAPLoRaMessage;
10+
import net.sharksystem.asap.android.lora.messages.DeviceDiscoveredASAPLoRaMessage;
11+
import net.sharksystem.asap.android.lora.messages.DiscoverASAPLoRaMessage;
12+
import net.sharksystem.asap.android.lora.messages.ErrorASAPLoRaMessage;
13+
import net.sharksystem.asap.android.lora.messages.RawASAPLoRaMessage;
14+
15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStreamReader;
18+
import java.util.UUID;
19+
20+
public class LoRaCommunicationManager extends Thread {
21+
/**
22+
* Diese Klasse bildet 3 Zwecke ab:
23+
* - die Kommunikation mit dem SX1278 via {@link LoRaBTInputOutputStream}
24+
* - Ersatz der "isConnected" Logiken aus WiFi und BT (vgl SYN/ACK?)
25+
* --> Damit Verwaltung der versch. Input/OutputStreams pro discovertem Peer
26+
* - Discovery neuer Peers und Benachrichtigung der @{@link LoRaEngine}
27+
*/
28+
private static final String CLASS_LOG_TAG = "ASAPLoRaCommManager";
29+
private static LoRaBTInputOutputStream ioStream = null;
30+
private BluetoothDevice btDevice = null;
31+
32+
public LoRaCommunicationManager() throws ASAPLoRaException {
33+
34+
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
35+
btAdapter.cancelDiscovery();
36+
37+
//TODO - move to Parameter / initialize Selection-Dialog
38+
for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) {
39+
if (btDevice.getName().indexOf("ASAP-LoRa") == 0) { //TODO: What about more than 1 paired ASAP-LoRa Board? Or 1 avail and 1 unavail?
40+
this.btDevice = btDevice;
41+
break;
42+
}
43+
}
44+
if (this.btDevice == null)
45+
throw new ASAPLoRaException("Please pair to an ASAP-LoRa Board before Starting LoRa!");
46+
47+
/**
48+
* uses UUID of Serial Devices for now - https://www.bluetooth.com/specifications/assigned-numbers/
49+
* Might be a good idea to move another uuid to define a ASAP-LoRa Node
50+
*/
51+
try {
52+
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
53+
btSocket.connect();
54+
this.ioStream = new LoRaBTInputOutputStream(btSocket);
55+
} catch (IOException e) {
56+
throw new ASAPLoRaException(e);
57+
}
58+
}
59+
60+
@Override
61+
public void run() {
62+
super.run();
63+
64+
try {
65+
//this.ioStream.getOutputStream().write(new RawASAPLoRaMessage("AT"));
66+
//this.ioStream.getOutputStream().write(new DiscoverASAPLoRaMessage());
67+
this.ioStream.getOutputStream().write(new ASAPLoRaMessage("A2FF", "Hi there!"));
68+
69+
while (!this.isInterrupted()) {
70+
if (this.ioStream.getInputStream().available() > 0) {
71+
AbstractASAPLoRaMessage asapLoRaMessage = this.ioStream.getInputStream().readASAPLoRaMessage();
72+
73+
//TODO, this is smelly... visitorpattern? handleMessage() in abstract?
74+
if(asapLoRaMessage instanceof ASAPLoRaMessage){
75+
//New Message inbound, write to corresponding inputstream of ASAPPeer
76+
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString());
77+
} else if(asapLoRaMessage instanceof DeviceDiscoveredASAPLoRaMessage){
78+
//New Device in Range found
79+
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString());
80+
} else if(asapLoRaMessage instanceof ErrorASAPLoRaMessage){
81+
//LoRa Error occured
82+
Log.i(this.CLASS_LOG_TAG, asapLoRaMessage.toString());
83+
}
84+
}
85+
}
86+
} catch (IOException e) {
87+
Log.e(this.CLASS_LOG_TAG, e.getMessage());
88+
//throw new ASAPLoRaException(e);
89+
} finally {
90+
this.ioStream.close(); //cleanup after ourselves
91+
}
92+
}
93+
}

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

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@
66
import net.sharksystem.asap.android.service.ASAPService;
77
import net.sharksystem.asap.android.service.MacLayerEngine;
88

9-
import android.bluetooth.BluetoothAdapter;
10-
import android.bluetooth.BluetoothDevice;
11-
import android.bluetooth.BluetoothSocket;
12-
13-
import java.io.BufferedReader;
14-
import java.io.IOException;
15-
import java.io.InputStreamReader;
16-
import java.util.UUID;
17-
189
public class LoRaEngine extends MacLayerEngine {
1910

2011
private static final String CLASS_LOG_TAG = "ASAPLoRaEngine";
21-
22-
2312
private static LoRaEngine engine = null;
24-
private BluetoothDevice btDevice = null;
13+
private LoRaCommunicationManager loRaCommunicationManager;
2514

2615
/**
2716
* - Aufbau Verbindung zu BLE o. Bluetooth UART Schnittstelle
@@ -48,58 +37,23 @@ public LoRaEngine(ASAPService asapService, Context context) {
4837
super(asapService, context);
4938
}
5039

51-
private void initBluetooth() throws ASAPLoRaException {
52-
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
53-
btAdapter.cancelDiscovery();
54-
55-
for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) {
56-
if (btDevice.getName().indexOf("ASAP-LoRa") == 0) { //TODO: What about more than 1 paired ASAP-LoRa Board? Or 1 avail and 1 unavail?
57-
this.btDevice = btDevice;
58-
break;
59-
}
60-
}
61-
if (this.btDevice == null)
62-
throw new ASAPLoRaException("Please pair to an ASAP-LoRa Board before Starting LoRa!");
63-
64-
/**
65-
* uses UUID of Serial Devices for now - https://www.bluetooth.com/specifications/assigned-numbers/
66-
* Might be a good idea to move another uuid to define a ASAP-LoRa Node
67-
*/
68-
try {
69-
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
70-
btSocket.connect();
71-
btSocket.getOutputStream().write("AT".getBytes());
72-
73-
while(true){ //TODO: Do not Activewait...
74-
if(btSocket.getInputStream().available() > 0) {
75-
BufferedReader br = new BufferedReader(new InputStreamReader(btSocket.getInputStream()));
76-
StringBuilder sb = new StringBuilder(btSocket.getInputStream().available());
77-
do {
78-
sb.append(br.readLine()).append("\n");
79-
} while(br.ready());
80-
Log.i(this.CLASS_LOG_TAG, "LoRa Board said: "+sb.toString());
81-
break;
82-
}
83-
}
84-
} catch (IOException e) {
85-
e.printStackTrace();
86-
}
87-
}
88-
8940
@Override
9041
public void start() {
9142
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.start() called");
9243
try {
93-
this.initBluetooth();
44+
loRaCommunicationManager = new LoRaCommunicationManager();
45+
loRaCommunicationManager.start();
9446
} catch (ASAPLoRaException e) {
95-
Log.e(this.CLASS_LOG_TAG, e.getMessage());
47+
e.printStackTrace(); //TODO
9648
}
9749
}
9850

9951
@Override
10052
public void stop() {
10153
Log.i(this.CLASS_LOG_TAG, "MacLayerEngine.stop() called");
102-
54+
if(loRaCommunicationManager != null){
55+
loRaCommunicationManager.interrupt();
56+
}
10357
}
10458

10559
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
public class ASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
//these need to be public to be picked up by the json automapper
5+
public String message = "";
6+
public String address = "";
7+
8+
public ASAPLoRaMessage(String address, String message){
9+
this.address = address;
10+
this.message = message;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return this.address + ": " + this.message;
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
@JsonTypeInfo(use=JsonTypeInfo.Id.MINIMAL_CLASS, property="COMMAND")
7+
@JsonSubTypes( {@JsonSubTypes.Type(DiscoverASAPLoRaMessage.class), @JsonSubTypes.Type(ASAPLoRaMessage.class)})
8+
public abstract class AbstractASAPLoRaMessage {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
public class DeviceDiscoveredASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
public class DiscoverASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
public class ErrorASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.sharksystem.asap.android.lora.messages;
2+
3+
public class RawASAPLoRaMessage extends AbstractASAPLoRaMessage {
4+
5+
private String rawMessage = "";
6+
7+
public RawASAPLoRaMessage(String msg){
8+
this.rawMessage = msg;
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return this.rawMessage;
14+
}
15+
}

0 commit comments

Comments
 (0)