Skip to content

Commit ad6e790

Browse files
committed
Add option to prefix USB packet with transmission target
1 parent 83cffea commit ad6e790

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

codec2talkie/src/main/java/com/radio/codec2talkie/settings/PreferenceKeys.java

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public final class PreferenceKeys {
99
public static String PORTS_USB_PARITY = "ports_usb_parity";
1010
public static String PORTS_USB_DTR = "ports_usb_dtr";
1111
public static String PORTS_USB_RTS = "ports_usb_rts";
12+
public static String PORTS_USB_IS_PREFIX_ENABLED = "ports_usb_is_prefix_enabled";
13+
public static String PORTS_USB_PREFIX = "ports_usb_prefix";
1214

1315
public static String PORTS_BT_CLIENT_NAME = "ports_bt_client_name";
1416

codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java

+10
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ public static String getString(ByteBuffer byteBuffer) {
5353
}
5454
return result.toString();
5555
}
56+
57+
public static byte[] hexStringToByteArray(String s) {
58+
int len = s.length();
59+
byte[] data = new byte[len / 2];
60+
for (int i = 0; i < len; i += 2) {
61+
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
62+
+ Character.digit(s.charAt(i+1), 16));
63+
}
64+
return data;
65+
}
5666
}

codec2talkie/src/main/java/com/radio/codec2talkie/transport/TransportFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static Transport create(TransportType transportType, Context context) thr
4040

4141
switch (transportType) {
4242
case USB:
43-
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName());
43+
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName(), context);
4444
case BLUETOOTH:
4545
return new Bluetooth(BluetoothSocketHandler.getSocket(), BluetoothSocketHandler.getName());
4646
case TCP_IP:

codec2talkie/src/main/java/com/radio/codec2talkie/transport/UsbSerial.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.radio.codec2talkie.transport;
22

3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
import androidx.preference.PreferenceManager;
7+
38
import com.hoho.android.usbserial.driver.SerialTimeoutException;
49
import com.hoho.android.usbserial.driver.UsbSerialPort;
10+
import com.radio.codec2talkie.settings.PreferenceKeys;
11+
import com.radio.codec2talkie.tools.TextTools;
512

613
import java.io.IOException;
14+
import java.nio.ByteBuffer;
715

816
public class UsbSerial implements Transport {
917

@@ -13,9 +21,18 @@ public class UsbSerial implements Transport {
1321
private final UsbSerialPort _usbPort;
1422
private final String _name;
1523

16-
public UsbSerial(UsbSerialPort usbPort, String name) {
24+
private final boolean _isPrefixEnabled;
25+
private final byte[] _bytePrefix;
26+
27+
protected SharedPreferences _sharedPreferences;
28+
29+
public UsbSerial(UsbSerialPort usbPort, String name, Context context) {
1730
_usbPort = usbPort;
1831
_name = name;
32+
_sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
33+
_isPrefixEnabled = _sharedPreferences.getBoolean(PreferenceKeys.PORTS_USB_IS_PREFIX_ENABLED, false);
34+
String prefix = _sharedPreferences.getString(PreferenceKeys.PORTS_USB_PREFIX, "");
35+
_bytePrefix = TextTools.hexStringToByteArray(prefix);
1936
}
2037

2138
@Override
@@ -31,7 +48,15 @@ public int read(byte[] data) throws IOException {
3148
@Override
3249
public int write(byte[] data) throws IOException {
3350
try {
34-
_usbPort.write(data, TX_TIMEOUT);
51+
if (_isPrefixEnabled) {
52+
byte[] pkt = ByteBuffer.allocate(_bytePrefix.length + data.length)
53+
.put(_bytePrefix)
54+
.put(data)
55+
.array();
56+
_usbPort.write(pkt, TX_TIMEOUT);
57+
} else {
58+
_usbPort.write(data, TX_TIMEOUT);
59+
}
3560
return data.length;
3661
} catch (SerialTimeoutException e) {
3762
e.printStackTrace();

codec2talkie/src/main/res/values/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@
192192
<string name="usb_stop_bits_title">Serial stop bits</string>
193193
<string name="usb_serial_title">USB serial settings</string>
194194
<string name="usb_serial_summary">Set USB serial settings, such as speed, bits, parity, etc.</string>
195+
<string name="usb_is_prefix_enabled_title">Enable USB packet prefix</string>
196+
<string name="usb_is_prefix_enabled_summary">Prefix USB data with the HEX string for LoRA UART modems</string>
197+
<string name="usb_prefix_title">USB packet prefix value as HEX string</string>
198+
<string name="usb_prefix_summary">Prefix sent content with a hex string (so called transmission target in UART modems), e.g. C0FFEE</string>
195199

196200
<string name="app_audio_output_speaker_title">Play audio through the speaker</string>
197201
<string name="app_audio_output_speaker_summary">Output incoming audio through the speaker</string>

codec2talkie/src/main/res/xml/preferences_usb.xml

+16
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,21 @@
5454
app:summary="@string/usb_rts_summary"
5555
app:defaultValue="false">
5656
</CheckBoxPreference>
57+
58+
<CheckBoxPreference
59+
app:key="ports_usb_is_prefix_enabled"
60+
app:title="@string/usb_is_prefix_enabled_title"
61+
app:summary="@string/usb_is_prefix_enabled_summary"
62+
app:defaultValue="false">
63+
</CheckBoxPreference>
5764
</PreferenceCategory>
65+
66+
<EditTextPreference
67+
app:key="ports_usb_prefix"
68+
app:title="@string/usb_prefix_title"
69+
app:summary = "@string/usb_prefix_summary"
70+
app:dependency="ports_usb_is_prefix_enabled"
71+
app:defaultValue="C0FFEE">
72+
</EditTextPreference>
73+
5874
</PreferenceScreen>

0 commit comments

Comments
 (0)