Skip to content

Commit

Permalink
Added custom prefix as protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sh123 committed Feb 19, 2024
1 parent 579c27f commit 8edf786
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.radio.codec2talkie.protocol;

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

import com.radio.codec2talkie.protocol.message.TextMessage;
import com.radio.codec2talkie.protocol.position.Position;
import com.radio.codec2talkie.settings.PreferenceKeys;
import com.radio.codec2talkie.tools.TextTools;
import com.radio.codec2talkie.transport.Transport;

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

public class CustomDataPrefix implements Protocol {

private final Protocol _childProtocol;
private ProtocolCallback _parentProtocolCallback;
private final byte[] _bytePrefix;

public CustomDataPrefix(Protocol childProtocol, SharedPreferences sharedPreferences) {
_childProtocol = childProtocol;
String prefix = sharedPreferences.getString(PreferenceKeys.CUSTOM_PREFIX, "");
_bytePrefix = TextTools.hexStringToByteArray(prefix);
}

@Override
public void initialize(Transport transport, Context context, ProtocolCallback protocolCallback) throws IOException {
_parentProtocolCallback = protocolCallback;
_childProtocol.initialize(transport, context, _protocolCallback);
}

@Override
public int getPcmAudioRecordBufferSize() {
return -1;
}

@Override
public void sendCompressedAudio(String src, String dst, byte[] frame) throws IOException {
byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + frame.length)
.put(_bytePrefix)
.put(frame)
.array();
_childProtocol.sendCompressedAudio(src, dst, prefixedData);
}

@Override
public void sendTextMessage(TextMessage textMessage) throws IOException {
_childProtocol.sendTextMessage(textMessage);
}

@Override
public void sendPcmAudio(String src, String dst, short[] pcmFrame) throws IOException {
_childProtocol.sendPcmAudio(src, dst, pcmFrame);
}

@Override
public void sendData(String src, String dst, String path, byte[] dataPacket) throws IOException {
byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + dataPacket.length)
.put(_bytePrefix)
.put(dataPacket)
.array();
_childProtocol.sendData(src, dst, path, prefixedData);
}

@Override
public boolean receive() throws IOException {
return _childProtocol.receive();
}

@Override
public void sendPosition(Position position) throws IOException {
_childProtocol.sendPosition(position);
}

@Override
public void flush() throws IOException {
_childProtocol.flush();
}

@Override
public void close() {
_childProtocol.close();
}

ProtocolCallback _protocolCallback = new ProtocolCallback() {
@Override
protected void onReceivePosition(Position position) {
_parentProtocolCallback.onReceivePosition(position);
}

@Override
protected void onReceivePcmAudio(String src, String dst, short[] pcmFrame) {
_parentProtocolCallback.onReceivePcmAudio(src, dst, pcmFrame);
}

@Override
protected void onReceiveCompressedAudio(String src, String dst, byte[] audioFrame) {
_parentProtocolCallback.onReceiveCompressedAudio(src, dst, audioFrame);
}

@Override
protected void onReceiveTextMessage(TextMessage textMessage) {
_parentProtocolCallback.onReceiveTextMessage(textMessage);
}

@Override
protected void onReceiveData(String src, String dst, String path, byte[] data) {
_parentProtocolCallback.onReceiveData(src, dst, path, data);
}

@Override
protected void onReceiveSignalLevel(short rssi, short snr) {
_parentProtocolCallback.onReceiveSignalLevel(rssi, snr);
}

@Override
protected void onReceiveTelemetry(int batVoltage) {
_parentProtocolCallback.onReceiveTelemetry(batVoltage);
}

@Override
protected void onReceiveLog(String logData) {
_parentProtocolCallback.onReceiveLog(logData);
}

@Override
protected void onTransmitPcmAudio(String src, String dst, short[] frame) {
_parentProtocolCallback.onTransmitPcmAudio(src, dst, frame);
}

@Override
protected void onTransmitCompressedAudio(String src, String dst, byte[] frame) {
_parentProtocolCallback.onTransmitCompressedAudio(src, dst, frame);
}

@Override
protected void onTransmitTextMessage(TextMessage textMessage) {
_parentProtocolCallback.onTransmitTextMessage(textMessage);
}

@Override
protected void onTransmitPosition(Position position) {
_parentProtocolCallback.onTransmitPosition(position);
}

@Override
protected void onTransmitData(String src, String dst, String path, byte[] data) {
_parentProtocolCallback.onTransmitData(src, dst, path, data);
}

@Override
protected void onTransmitLog(String logData) {
_parentProtocolCallback.onTransmitLog(logData);
}

@Override
protected void onProtocolRxError() {
_parentProtocolCallback.onProtocolRxError();
}

@Override
protected void onProtocolTxError() {
_parentProtocolCallback.onProtocolTxError();
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static Protocol create(Context context) {
boolean aprsIsEnabled = SettingsWrapper.isAprsIsEnabled(sharedPreferences);
boolean freedvEnabled = SettingsWrapper.isFreeDvSoundModemModulation(sharedPreferences);
boolean codec2Enabled = SettingsWrapper.isCodec2Enabled(sharedPreferences);
boolean isCustomPrefixEnabled = SettingsWrapper.isCustomPrefixEnabled(sharedPreferences);

// "root" protocol
Protocol proto;
Expand All @@ -97,6 +98,9 @@ public static Protocol create(Context context) {
break;
}

if (isCustomPrefixEnabled) {
proto = new CustomDataPrefix(proto, sharedPreferences);
}
if (scramblingEnabled) {
proto = new Scrambler(proto, scramblingKey);
}
Expand All @@ -108,7 +112,6 @@ public static Protocol create(Context context) {
if (recordingEnabled) {
proto = new Recorder(proto, sharedPreferences);
}

proto = new AudioCodec2FrameAggregator(proto, sharedPreferences);
proto = new AudioCodec2(proto, sharedPreferences);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public final class PreferenceKeys {
public static String PORTS_SOUND_MODEM_FREEDV_SQUELCH_SNR="ports_sound_modem_freedv_squelch_snr";
public static String PORTS_SOUND_MODEM_FREEDV_DATA_MODE="ports_sound_modem_freedv_data_mode";

public static String CUSTOM_PREFIX_ENABLED = "custom_prefix_enabled";
public static String CUSTOM_PREFIX = "custom_prefix";

public static String CODEC_TYPE = "codec_type";
public static String CODEC2_RECORDING_ENABLED = "codec2_recording_enabled";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
}

public static class SettingsTncExtendedFragment extends PreferenceFragmentCompat
{
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences_tnc_extended, null);
setNumberInputType(getPreferenceManager());
}
}

public static class SettingsTcpIpFragment extends PreferenceFragmentCompat
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static boolean isCodec2Enabled(SharedPreferences sharedPreferences) {
return sharedPreferences.getString(PreferenceKeys.CODEC_TYPE, "Codec2").equals("Codec2");
}

public static boolean isCustomPrefixEnabled(SharedPreferences sharedPreferences) {
return sharedPreferences.getBoolean(PreferenceKeys.CUSTOM_PREFIX_ENABLED, false);
}

public static int getFreeDvSoundModemModulation(SharedPreferences sharedPreferences) {
String modemType = sharedPreferences.getString(PreferenceKeys.PORTS_SOUND_MODEM_TYPE, "1200");
if (modemType.startsWith("F")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
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 Down
11 changes: 9 additions & 2 deletions codec2talkie/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,15 @@
<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="tnc_extended_title">Extended TNC settings</string>
<string name="tnc_extended_summary">Additional TNC specific settings</string>
<string name="tnc_extended_uart_title">UART modem prefix</string>

<string name="custom_prefix_enabled_title">Enable UART prefix</string>
<string name="custom_prefix_enabled_summary">Prefix USB data with the HEX string for some LoRA UART modems</string>
<string name="custom_prefix_title">USB packet prefix value as a HEX string</string>
<string name="custom_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
6 changes: 6 additions & 0 deletions codec2talkie/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
app:fragment="com.radio.codec2talkie.settings.SettingsActivity$SettingsSoundModemFragment">
</Preference>

<Preference
app:key="ports_tnc_extended"
app:title="@string/tnc_extended_title"
app:summary="@string/tnc_extended_summary"
app:fragment="com.radio.codec2talkie.settings.SettingsActivity$SettingsTncExtendedFragment">
</Preference>
</PreferenceCategory>

<PreferenceCategory
Expand Down
25 changes: 25 additions & 0 deletions codec2talkie/src/main/res/xml/preferences_tnc_extended.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:key="tnc_extended_uart"
app:title="@string/tnc_extended_uart_title">

<CheckBoxPreference
app:key="custom_prefix_enabled"
app:title="@string/custom_prefix_enabled_title"
app:summary="@string/custom_prefix_enabled_summary"
app:defaultValue="false">
</CheckBoxPreference>

<EditTextPreference
app:key="custom_prefix"
app:title="@string/custom_prefix_title"
app:summary = "@string/custom_prefix_summary"
app:dependency="custom_prefix_enabled"
app:defaultValue="C0FFEE">
</EditTextPreference>

</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 8edf786

Please sign in to comment.