Skip to content

Commit

Permalink
Add option to prefix USB packet with transmission target
Browse files Browse the repository at this point in the history
  • Loading branch information
sh123 committed Feb 19, 2024
1 parent 83cffea commit ad6e790
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public final class PreferenceKeys {
public static String PORTS_USB_PARITY = "ports_usb_parity";
public static String PORTS_USB_DTR = "ports_usb_dtr";
public static String PORTS_USB_RTS = "ports_usb_rts";
public static String PORTS_USB_IS_PREFIX_ENABLED = "ports_usb_is_prefix_enabled";
public static String PORTS_USB_PREFIX = "ports_usb_prefix";

public static String PORTS_BT_CLIENT_NAME = "ports_bt_client_name";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public static String getString(ByteBuffer byteBuffer) {
}
return result.toString();
}

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static Transport create(TransportType transportType, Context context) thr

switch (transportType) {
case USB:
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName());
return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName(), context);
case BLUETOOTH:
return new Bluetooth(BluetoothSocketHandler.getSocket(), BluetoothSocketHandler.getName());
case TCP_IP:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.radio.codec2talkie.transport;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

import com.hoho.android.usbserial.driver.SerialTimeoutException;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.radio.codec2talkie.settings.PreferenceKeys;
import com.radio.codec2talkie.tools.TextTools;

import java.io.IOException;
import java.nio.ByteBuffer;

public class UsbSerial implements Transport {

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

public UsbSerial(UsbSerialPort usbPort, String name) {
private final boolean _isPrefixEnabled;
private final byte[] _bytePrefix;

protected SharedPreferences _sharedPreferences;

public UsbSerial(UsbSerialPort usbPort, String name, Context context) {
_usbPort = usbPort;
_name = name;
_sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
_isPrefixEnabled = _sharedPreferences.getBoolean(PreferenceKeys.PORTS_USB_IS_PREFIX_ENABLED, false);
String prefix = _sharedPreferences.getString(PreferenceKeys.PORTS_USB_PREFIX, "");
_bytePrefix = TextTools.hexStringToByteArray(prefix);
}

@Override
Expand All @@ -31,7 +48,15 @@ public int read(byte[] data) throws IOException {
@Override
public int write(byte[] data) throws IOException {
try {
_usbPort.write(data, TX_TIMEOUT);
if (_isPrefixEnabled) {
byte[] pkt = ByteBuffer.allocate(_bytePrefix.length + data.length)
.put(_bytePrefix)
.put(data)
.array();
_usbPort.write(pkt, TX_TIMEOUT);
} else {
_usbPort.write(data, TX_TIMEOUT);
}
return data.length;
} catch (SerialTimeoutException e) {
e.printStackTrace();
Expand Down
4 changes: 4 additions & 0 deletions codec2talkie/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@
<string name="usb_stop_bits_title">Serial stop bits</string>
<string name="usb_serial_title">USB serial settings</string>
<string name="usb_serial_summary">Set USB serial settings, such as speed, bits, parity, etc.</string>
<string name="usb_is_prefix_enabled_title">Enable USB packet prefix</string>
<string name="usb_is_prefix_enabled_summary">Prefix USB data with the HEX string for LoRA UART modems</string>
<string name="usb_prefix_title">USB packet prefix value as HEX string</string>
<string name="usb_prefix_summary">Prefix sent content with a hex string (so called transmission target in UART modems), e.g. C0FFEE</string>

<string name="app_audio_output_speaker_title">Play audio through the speaker</string>
<string name="app_audio_output_speaker_summary">Output incoming audio through the speaker</string>
Expand Down
16 changes: 16 additions & 0 deletions codec2talkie/src/main/res/xml/preferences_usb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,21 @@
app:summary="@string/usb_rts_summary"
app:defaultValue="false">
</CheckBoxPreference>

<CheckBoxPreference
app:key="ports_usb_is_prefix_enabled"
app:title="@string/usb_is_prefix_enabled_title"
app:summary="@string/usb_is_prefix_enabled_summary"
app:defaultValue="false">
</CheckBoxPreference>
</PreferenceCategory>

<EditTextPreference
app:key="ports_usb_prefix"
app:title="@string/usb_prefix_title"
app:summary = "@string/usb_prefix_summary"
app:dependency="ports_usb_is_prefix_enabled"
app:defaultValue="C0FFEE">
</EditTextPreference>

</PreferenceScreen>

0 comments on commit ad6e790

Please sign in to comment.