Skip to content

Commit 8edf786

Browse files
committed
Added custom prefix as protocol
1 parent 579c27f commit 8edf786

File tree

9 files changed

+227
-11
lines changed

9 files changed

+227
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package com.radio.codec2talkie.protocol;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
import com.radio.codec2talkie.protocol.message.TextMessage;
7+
import com.radio.codec2talkie.protocol.position.Position;
8+
import com.radio.codec2talkie.settings.PreferenceKeys;
9+
import com.radio.codec2talkie.tools.TextTools;
10+
import com.radio.codec2talkie.transport.Transport;
11+
12+
import java.io.IOException;
13+
import java.nio.ByteBuffer;
14+
15+
public class CustomDataPrefix implements Protocol {
16+
17+
private final Protocol _childProtocol;
18+
private ProtocolCallback _parentProtocolCallback;
19+
private final byte[] _bytePrefix;
20+
21+
public CustomDataPrefix(Protocol childProtocol, SharedPreferences sharedPreferences) {
22+
_childProtocol = childProtocol;
23+
String prefix = sharedPreferences.getString(PreferenceKeys.CUSTOM_PREFIX, "");
24+
_bytePrefix = TextTools.hexStringToByteArray(prefix);
25+
}
26+
27+
@Override
28+
public void initialize(Transport transport, Context context, ProtocolCallback protocolCallback) throws IOException {
29+
_parentProtocolCallback = protocolCallback;
30+
_childProtocol.initialize(transport, context, _protocolCallback);
31+
}
32+
33+
@Override
34+
public int getPcmAudioRecordBufferSize() {
35+
return -1;
36+
}
37+
38+
@Override
39+
public void sendCompressedAudio(String src, String dst, byte[] frame) throws IOException {
40+
byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + frame.length)
41+
.put(_bytePrefix)
42+
.put(frame)
43+
.array();
44+
_childProtocol.sendCompressedAudio(src, dst, prefixedData);
45+
}
46+
47+
@Override
48+
public void sendTextMessage(TextMessage textMessage) throws IOException {
49+
_childProtocol.sendTextMessage(textMessage);
50+
}
51+
52+
@Override
53+
public void sendPcmAudio(String src, String dst, short[] pcmFrame) throws IOException {
54+
_childProtocol.sendPcmAudio(src, dst, pcmFrame);
55+
}
56+
57+
@Override
58+
public void sendData(String src, String dst, String path, byte[] dataPacket) throws IOException {
59+
byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + dataPacket.length)
60+
.put(_bytePrefix)
61+
.put(dataPacket)
62+
.array();
63+
_childProtocol.sendData(src, dst, path, prefixedData);
64+
}
65+
66+
@Override
67+
public boolean receive() throws IOException {
68+
return _childProtocol.receive();
69+
}
70+
71+
@Override
72+
public void sendPosition(Position position) throws IOException {
73+
_childProtocol.sendPosition(position);
74+
}
75+
76+
@Override
77+
public void flush() throws IOException {
78+
_childProtocol.flush();
79+
}
80+
81+
@Override
82+
public void close() {
83+
_childProtocol.close();
84+
}
85+
86+
ProtocolCallback _protocolCallback = new ProtocolCallback() {
87+
@Override
88+
protected void onReceivePosition(Position position) {
89+
_parentProtocolCallback.onReceivePosition(position);
90+
}
91+
92+
@Override
93+
protected void onReceivePcmAudio(String src, String dst, short[] pcmFrame) {
94+
_parentProtocolCallback.onReceivePcmAudio(src, dst, pcmFrame);
95+
}
96+
97+
@Override
98+
protected void onReceiveCompressedAudio(String src, String dst, byte[] audioFrame) {
99+
_parentProtocolCallback.onReceiveCompressedAudio(src, dst, audioFrame);
100+
}
101+
102+
@Override
103+
protected void onReceiveTextMessage(TextMessage textMessage) {
104+
_parentProtocolCallback.onReceiveTextMessage(textMessage);
105+
}
106+
107+
@Override
108+
protected void onReceiveData(String src, String dst, String path, byte[] data) {
109+
_parentProtocolCallback.onReceiveData(src, dst, path, data);
110+
}
111+
112+
@Override
113+
protected void onReceiveSignalLevel(short rssi, short snr) {
114+
_parentProtocolCallback.onReceiveSignalLevel(rssi, snr);
115+
}
116+
117+
@Override
118+
protected void onReceiveTelemetry(int batVoltage) {
119+
_parentProtocolCallback.onReceiveTelemetry(batVoltage);
120+
}
121+
122+
@Override
123+
protected void onReceiveLog(String logData) {
124+
_parentProtocolCallback.onReceiveLog(logData);
125+
}
126+
127+
@Override
128+
protected void onTransmitPcmAudio(String src, String dst, short[] frame) {
129+
_parentProtocolCallback.onTransmitPcmAudio(src, dst, frame);
130+
}
131+
132+
@Override
133+
protected void onTransmitCompressedAudio(String src, String dst, byte[] frame) {
134+
_parentProtocolCallback.onTransmitCompressedAudio(src, dst, frame);
135+
}
136+
137+
@Override
138+
protected void onTransmitTextMessage(TextMessage textMessage) {
139+
_parentProtocolCallback.onTransmitTextMessage(textMessage);
140+
}
141+
142+
@Override
143+
protected void onTransmitPosition(Position position) {
144+
_parentProtocolCallback.onTransmitPosition(position);
145+
}
146+
147+
@Override
148+
protected void onTransmitData(String src, String dst, String path, byte[] data) {
149+
_parentProtocolCallback.onTransmitData(src, dst, path, data);
150+
}
151+
152+
@Override
153+
protected void onTransmitLog(String logData) {
154+
_parentProtocolCallback.onTransmitLog(logData);
155+
}
156+
157+
@Override
158+
protected void onProtocolRxError() {
159+
_parentProtocolCallback.onProtocolRxError();
160+
}
161+
162+
@Override
163+
protected void onProtocolTxError() {
164+
_parentProtocolCallback.onProtocolTxError();
165+
}
166+
};
167+
}

codec2talkie/src/main/java/com/radio/codec2talkie/protocol/ProtocolFactory.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static Protocol create(Context context) {
7272
boolean aprsIsEnabled = SettingsWrapper.isAprsIsEnabled(sharedPreferences);
7373
boolean freedvEnabled = SettingsWrapper.isFreeDvSoundModemModulation(sharedPreferences);
7474
boolean codec2Enabled = SettingsWrapper.isCodec2Enabled(sharedPreferences);
75+
boolean isCustomPrefixEnabled = SettingsWrapper.isCustomPrefixEnabled(sharedPreferences);
7576

7677
// "root" protocol
7778
Protocol proto;
@@ -97,6 +98,9 @@ public static Protocol create(Context context) {
9798
break;
9899
}
99100

101+
if (isCustomPrefixEnabled) {
102+
proto = new CustomDataPrefix(proto, sharedPreferences);
103+
}
100104
if (scramblingEnabled) {
101105
proto = new Scrambler(proto, scramblingKey);
102106
}
@@ -108,7 +112,6 @@ public static Protocol create(Context context) {
108112
if (recordingEnabled) {
109113
proto = new Recorder(proto, sharedPreferences);
110114
}
111-
112115
proto = new AudioCodec2FrameAggregator(proto, sharedPreferences);
113116
proto = new AudioCodec2(proto, sharedPreferences);
114117
} else {

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

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public final class PreferenceKeys {
2929
public static String PORTS_SOUND_MODEM_FREEDV_SQUELCH_SNR="ports_sound_modem_freedv_squelch_snr";
3030
public static String PORTS_SOUND_MODEM_FREEDV_DATA_MODE="ports_sound_modem_freedv_data_mode";
3131

32+
public static String CUSTOM_PREFIX_ENABLED = "custom_prefix_enabled";
33+
public static String CUSTOM_PREFIX = "custom_prefix";
34+
3235
public static String CODEC_TYPE = "codec_type";
3336
public static String CODEC2_RECORDING_ENABLED = "codec2_recording_enabled";
3437

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

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
117117
}
118118
}
119119

120+
public static class SettingsTncExtendedFragment extends PreferenceFragmentCompat
121+
{
122+
@Override
123+
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
124+
setPreferencesFromResource(R.xml.preferences_tnc_extended, null);
125+
setNumberInputType(getPreferenceManager());
126+
}
127+
}
128+
120129
public static class SettingsTcpIpFragment extends PreferenceFragmentCompat
121130
{
122131
@Override

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

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static boolean isCodec2Enabled(SharedPreferences sharedPreferences) {
4444
return sharedPreferences.getString(PreferenceKeys.CODEC_TYPE, "Codec2").equals("Codec2");
4545
}
4646

47+
public static boolean isCustomPrefixEnabled(SharedPreferences sharedPreferences) {
48+
return sharedPreferences.getBoolean(PreferenceKeys.CUSTOM_PREFIX_ENABLED, false);
49+
}
50+
4751
public static int getFreeDvSoundModemModulation(SharedPreferences sharedPreferences) {
4852
String modemType = sharedPreferences.getString(PreferenceKeys.PORTS_SOUND_MODEM_TYPE, "1200");
4953
if (modemType.startsWith("F")) {

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

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

3-
import android.content.Context;
4-
import android.content.SharedPreferences;
5-
6-
import androidx.preference.PreferenceManager;
7-
83
import com.hoho.android.usbserial.driver.SerialTimeoutException;
94
import com.hoho.android.usbserial.driver.UsbSerialPort;
10-
import com.radio.codec2talkie.settings.PreferenceKeys;
11-
import com.radio.codec2talkie.tools.TextTools;
125

136
import java.io.IOException;
14-
import java.nio.ByteBuffer;
157

168
public class UsbSerial implements Transport {
179

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,15 @@
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>
195+
196+
<string name="tnc_extended_title">Extended TNC settings</string>
197+
<string name="tnc_extended_summary">Additional TNC specific settings</string>
198+
<string name="tnc_extended_uart_title">UART modem prefix</string>
199+
200+
<string name="custom_prefix_enabled_title">Enable UART prefix</string>
201+
<string name="custom_prefix_enabled_summary">Prefix USB data with the HEX string for some LoRA UART modems</string>
202+
<string name="custom_prefix_title">USB packet prefix value as a HEX string</string>
203+
<string name="custom_prefix_summary">Prefix sent content with a hex string (so called transmission target in UART modems), e.g. C0FFEE</string>
197204

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

codec2talkie/src/main/res/xml/preferences.xml

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
app:fragment="com.radio.codec2talkie.settings.SettingsActivity$SettingsSoundModemFragment">
113113
</Preference>
114114

115+
<Preference
116+
app:key="ports_tnc_extended"
117+
app:title="@string/tnc_extended_title"
118+
app:summary="@string/tnc_extended_summary"
119+
app:fragment="com.radio.codec2talkie.settings.SettingsActivity$SettingsTncExtendedFragment">
120+
</Preference>
115121
</PreferenceCategory>
116122

117123
<PreferenceCategory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
5+
<PreferenceCategory
6+
app:key="tnc_extended_uart"
7+
app:title="@string/tnc_extended_uart_title">
8+
9+
<CheckBoxPreference
10+
app:key="custom_prefix_enabled"
11+
app:title="@string/custom_prefix_enabled_title"
12+
app:summary="@string/custom_prefix_enabled_summary"
13+
app:defaultValue="false">
14+
</CheckBoxPreference>
15+
16+
<EditTextPreference
17+
app:key="custom_prefix"
18+
app:title="@string/custom_prefix_title"
19+
app:summary = "@string/custom_prefix_summary"
20+
app:dependency="custom_prefix_enabled"
21+
app:defaultValue="C0FFEE">
22+
</EditTextPreference>
23+
24+
</PreferenceCategory>
25+
</PreferenceScreen>

0 commit comments

Comments
 (0)