codec2_talkie/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/ax25/AX25Callsign.java

143 wiersze
3.9 KiB
Java
Czysty Zwykły widok Historia

2022-06-26 20:33:22 +00:00
package com.radio.codec2talkie.protocol.ax25;
2022-06-28 21:07:56 +00:00
import androidx.annotation.NonNull;
2022-06-27 20:50:09 +00:00
import java.nio.ByteBuffer;
2022-06-26 20:33:22 +00:00
public class AX25Callsign {
2022-06-28 21:07:56 +00:00
public static int CallsignMaxSize = 7;
2022-06-27 20:50:09 +00:00
2022-06-26 20:33:22 +00:00
public String callsign;
2022-06-27 20:50:09 +00:00
public int ssid;
2022-06-26 20:33:22 +00:00
public boolean isValid;
2022-06-28 21:07:56 +00:00
public boolean isLast = false;
2022-06-26 20:33:22 +00:00
2023-07-02 20:09:05 +00:00
public AX25Callsign() {}
public AX25Callsign(String callsign, String ssid) {
this.callsign = callsign;
this.ssid = Integer.parseInt(ssid);
}
public static String formatCallsign(String callsign, String ssid) {
return String.format("%s-%s", callsign, ssid);
}
2022-07-09 17:05:03 +00:00
public void fromString(String inputCallsignWithSsid) {
2022-06-27 20:50:09 +00:00
isValid = false;
2022-07-09 17:24:16 +00:00
if (inputCallsignWithSsid == null) return;
2022-07-09 17:05:03 +00:00
// WIDE1*
String callsignWithSsid = inputCallsignWithSsid.replace("*", "");
2022-06-27 20:50:09 +00:00
// ABCDEF-XX
if (callsignWithSsid.length() > CallsignMaxSize + 2 || callsignWithSsid.length() == 0) return;
int delimiterIndex = callsignWithSsid.indexOf('-');
// ABCDEF-
if (delimiterIndex != -1 && delimiterIndex == callsignWithSsid.length() - 1) return;
callsign = callsignWithSsid;
ssid = 0;
if (delimiterIndex == -1) {
// ABCDEF
if (callsign.length() >= CallsignMaxSize) return;
2022-06-26 20:33:22 +00:00
} else {
2022-06-27 20:50:09 +00:00
callsign = callsignWithSsid.substring(0, delimiterIndex);
try {
ssid = Integer.parseInt(callsignWithSsid.substring(delimiterIndex + 1));
} catch(NumberFormatException e) {
return;
}
2022-06-26 20:33:22 +00:00
}
2022-06-27 20:50:09 +00:00
isValid = true;
2022-06-26 20:33:22 +00:00
}
public void fromBinary(byte[] data) {
2022-06-27 20:50:09 +00:00
isValid = false;
if (data == null) return;
2022-06-27 20:50:09 +00:00
if (data.length != CallsignMaxSize) return;
2022-06-29 21:06:59 +00:00
StringBuilder buffer = new StringBuilder();
2022-06-27 20:50:09 +00:00
for (int i = 0; i < data.length - 1; i++) {
2022-06-29 21:06:59 +00:00
int d = (data[i] & 0xff) >>> 1;
char c = (char)d;
2022-06-27 20:50:09 +00:00
if (c == ' '){
2022-06-29 21:06:59 +00:00
break;
2022-06-27 20:50:09 +00:00
} else {
2022-06-29 21:06:59 +00:00
buffer.append(c);
2022-06-27 20:50:09 +00:00
}
}
2022-06-29 21:06:59 +00:00
callsign = buffer.toString();
2022-06-28 21:07:56 +00:00
byte lastByte = data[data.length - 1];
isLast = (lastByte & 0x01) == 1;
2022-06-29 21:06:59 +00:00
ssid = (lastByte >>> 1) & 0x0f;
2022-06-27 20:50:09 +00:00
if (callsign.length() == 0) return;
isValid = true;
2022-06-26 20:33:22 +00:00
}
2022-06-28 21:07:56 +00:00
@NonNull
public String toString() {
2022-06-30 16:49:27 +00:00
String callsignPlusSsid = callsign;
2022-07-09 20:12:59 +00:00
if (ssid == 0) {
if (isWide())
callsignPlusSsid += "*";
2022-07-09 17:05:03 +00:00
} else {
2022-06-30 16:49:27 +00:00
callsignPlusSsid += "-" + ssid;
2022-07-09 17:05:03 +00:00
}
2022-06-30 16:49:27 +00:00
return callsignPlusSsid;
2022-06-26 20:33:22 +00:00
}
public byte[] toBinary() {
2022-06-27 20:50:09 +00:00
ByteBuffer buffer = ByteBuffer.allocate(CallsignMaxSize);
for (int i = 0; i < CallsignMaxSize - 1; i++) {
if (i < callsign.length()) {
byte c = (byte) callsign.charAt(i);
2022-06-29 21:06:59 +00:00
buffer.put((byte)(c << 1));
2022-06-27 20:50:09 +00:00
} else {
// append ' ' for short callsigns
buffer.put((byte)(0x20 << 1));
}
}
2022-06-29 21:06:59 +00:00
byte binSsid = (byte)(ssid << 1);
if (isLast) {
binSsid |= 1;
}
buffer.put(binSsid);
2022-06-28 21:07:56 +00:00
// return
buffer.flip();
byte[] b = new byte[buffer.remaining()];
buffer.get(b);
return b;
2022-06-26 20:33:22 +00:00
}
2022-07-09 13:45:07 +00:00
public boolean isWide() {
return callsign.toUpperCase().startsWith("WIDE");
}
public boolean isTrace() {
return callsign.toUpperCase().startsWith("TRACE");
}
public boolean isSoftware() {
return callsign.toUpperCase().matches("^(AP)[A-Z]{1,4}$");
}
public boolean isPath() {
return isWide();
}
2022-07-10 11:05:41 +00:00
public boolean digiRepeatCallsign() {
if (ssid > 0) {
ssid -= 1;
return true;
}
return false;
}
2022-07-09 13:45:07 +00:00
public boolean digiRepeat() {
if (isPath()) {
2022-07-10 11:05:41 +00:00
return digiRepeatCallsign();
2022-07-09 13:45:07 +00:00
}
return false;
}
2022-06-26 20:33:22 +00:00
}