Skip to content

Commit

Permalink
Complete implementation of APRS beaconing, as well as fixing chat mes…
Browse files Browse the repository at this point in the history
…sage ack, and improving APRS decode success rate. Resolves issue #5.
  • Loading branch information
VanceVagell committed Dec 30, 2024
1 parent 6f4c6c0 commit d3387ab
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 27 deletions.
3 changes: 2 additions & 1 deletion android-src/KV4PHT/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// implementation 'com.github.mik3y:usb-serial-for-android:3.7.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
testImplementation 'junit:junit:4.13.2'
Expand All @@ -66,4 +65,6 @@ dependencies {
implementation 'com.google.zxing:core:3.4.1'

implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))

implementation 'com.google.android.gms:play-services-location:21.3.0'
}
4 changes: 3 additions & 1 deletion android-src/KV4PHT/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
Expand Down Expand Up @@ -46,7 +48,7 @@
android:name=".ui.AddEditMemoryActivity"
android:exported="true"
android:theme="@style/Theme.KV4PHT"
android:windowSoftInputMode="stateVisible|adjustResize"> <!-- There's an issue where fields are not scrollable, likely caused by an Android bug not honoring adjustResize on fullscreen apps, see similar bug in another project: https://github.com/dotnet/maui/issues/14281 -->
android:windowSoftInputMode="stateVisible|adjustResize">
<intent-filter>
<action android:name="com.vagell.kv4pht.ADD_MEMORY_ACTION" />
<action android:name="com.vagell.kv4pht.EDIT_MEMORY_ACTION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public PositionField(Position position, String comment) {
this.type = APRSTypes.T_POSITION;
// this.comment = comment;
compressedFormat = false;
// TODOV
}

public PositionField(Position position, String comment, boolean msgCapable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,48 @@ kv4p HT (see http://kv4p.com)
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.location.Location;
import android.location.LocationManager;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Binder;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.lifecycle.LiveData;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.CancellationTokenSource;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.hoho.android.usbserial.driver.SerialTimeoutException;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.vagell.kv4pht.R;
import com.vagell.kv4pht.aprs.parser.APRSPacket;
import com.vagell.kv4pht.aprs.parser.APRSTypes;
import com.vagell.kv4pht.aprs.parser.Digipeater;
import com.vagell.kv4pht.aprs.parser.InformationField;
import com.vagell.kv4pht.aprs.parser.MessagePacket;
import com.vagell.kv4pht.aprs.parser.Parser;
import com.vagell.kv4pht.aprs.parser.Position;
import com.vagell.kv4pht.aprs.parser.PositionField;
import com.vagell.kv4pht.data.ChannelMemory;
import com.vagell.kv4pht.firmware.FirmwareUtils;
import com.vagell.kv4pht.javAX25.ax25.Afsk1200Modulator;
Expand Down Expand Up @@ -115,10 +130,10 @@ public class RadioAudioService extends Service {
private RadioAudioServiceCallbacks callbacks = null;

// For transmitting audio to ESP32 / radio
public static final int AUDIO_SAMPLE_RATE = 16000;
public static final int AUDIO_SAMPLE_RATE = 22050;
public static final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public static final int audioFormat = AudioFormat.ENCODING_PCM_8BIT;
public static final int minBufferSize = AudioRecord.getMinBufferSize(AUDIO_SAMPLE_RATE, channelConfig, audioFormat) * 2;
public static final int minBufferSize = AudioRecord.getMinBufferSize(AUDIO_SAMPLE_RATE, channelConfig, audioFormat) * 4;
private UsbManager usbManager;
private UsbDevice esp32Device;
private static UsbSerialPort serialPort;
Expand Down Expand Up @@ -150,6 +165,15 @@ public class RadioAudioService extends Service {
private static final int MS_SILENCE_AFTER_DATA = 700;
private static final int APRS_MAX_MESSAGE_NUM = 99999;

// APRS position settings
public static final int APRS_POSITION_EXACT = 0;
public static final int APRS_POSITION_APPROX = 1;
public static final int APRS_BEACON_MINS = 1;
private boolean aprsBeaconPosition = false;
private int aprsPositionAccuracy = APRS_POSITION_EXACT;
private Handler aprsBeaconHandler = null;
private Runnable aprsBeaconRunnable = null;

// Radio params and related settings
private String activeFrequencyStr = "144.000";
private int squelch = 0;
Expand Down Expand Up @@ -262,6 +286,53 @@ public static void setMaxFreq(int newMaxFreq) {
maxFreq = newMaxFreq;
}

public void setAprsBeaconPosition(boolean aprsBeaconPosition) {
if (!this.aprsBeaconPosition && aprsBeaconPosition) { // If it was off, and now turned on...
Log.d("DEBUG", "Starting APRS position beaconing every " + APRS_BEACON_MINS + " mins");
// Start beaconing
aprsBeaconHandler = new Handler(Looper.getMainLooper());
aprsBeaconRunnable = new Runnable() {
@Override
public void run() {
sendPositionBeacon();
aprsBeaconHandler.postDelayed(this, 60 * APRS_BEACON_MINS * 1000);
}
};
aprsBeaconHandler.postDelayed(aprsBeaconRunnable, 60 * APRS_BEACON_MINS * 1000);

// Tell callback we started (e.g. so it can show a snackbar letting user know)
callbacks.aprsBeaconing(true, aprsPositionAccuracy);
}

if (!aprsBeaconPosition) {
Log.d("DEBUG", "Stopping APRS position beaconing");

// Stop beaconing
if (null != aprsBeaconHandler) {
aprsBeaconHandler.removeCallbacks(aprsBeaconRunnable);
}
aprsBeaconHandler = null;
aprsBeaconRunnable = null;
}

this.aprsBeaconPosition = aprsBeaconPosition;
}

public boolean getAprsBeaconPosition() {
return aprsBeaconPosition;
}

/**
* @param aprsPositionAccuracy APRS_POSITION_EXACT or APRS_POSITION_APPROX
*/
public void setAprsPositionAccuracy(int aprsPositionAccuracy) {
this.aprsPositionAccuracy = aprsPositionAccuracy;
}

public int getAprsPositionAccuracy() {
return aprsPositionAccuracy;
}

public void setMode(int mode) {
switch (mode) {
case MODE_FLASHING:
Expand Down Expand Up @@ -357,6 +428,9 @@ public interface RadioAudioServiceCallbacks {
public void txEnded();
public void chatError(String snackbarText);
public void sMeterUpdate(int value);
public void aprsBeaconing(boolean beaconing, int accuracy);
public void sentAprsBeacon(double latitude, double longitude);
public void unknownLocation();
}

public void setCallbacks(RadioAudioServiceCallbacks callbacks) {
Expand Down Expand Up @@ -1422,7 +1496,15 @@ public void handlePacket(byte[] data) {
if (!messagePacket.isAck() && messagePacket.getTargetCallsign().trim().toUpperCase().equals(callsign.toUpperCase())) {
showNotification(MESSAGE_NOTIFICATION_CHANNEL_ID, MESSAGE_NOTIFICATION_TO_YOU_ID,
aprsPacket.getSourceCall() + " messaged you", messagePacket.getMessageBody(), MainActivity.INTENT_OPEN_CHAT);
sendAckMessage(aprsPacket.getSourceCall().toUpperCase(), messagePacket.getMessageNumber());

// Send ack after a brief delay (to let the sender keyup and start decooding again)
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
sendAckMessage(aprsPacket.getSourceCall().toUpperCase(), messagePacket.getMessageNumber());
}
}, 1000);
}
}
} catch (Exception e) {
Expand All @@ -1445,9 +1527,87 @@ public void handlePacket(byte[] data) {
}
}

public void sendAckMessage(String targetCallsign, String remoteMessageNum) {
// TODOV
public void sendPositionBeacon() {
if (getMode() != MODE_RX) { // Can only beacon in rx mode (e.g. not tx or scan)
Log.d("DEBUG", "Skipping position beacon because not in RX mode");
return;
}

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); // Try to get cached location (fast)

if (location == null) {
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getBaseContext()) != ConnectionResult.SUCCESS) {
Log.d("DEBUG", "Unable to beacon position because Android device is missing Google Play Services, needed to get GPS location.");
callbacks.unknownLocation();
return;
}

// Otherwise, manually retrieve a new location for user.
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, cancellationTokenSource.getToken())
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
// Use the location
double latitude = location.getLatitude();
double longitude = location.getLongitude();
sendPositionBeacon(latitude, longitude);
} else {
callbacks.unknownLocation();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
callbacks.unknownLocation();
}
});
return;
}

double latitude = location.getLatitude();
double longitude = location.getLongitude();
sendPositionBeacon(latitude, longitude);
}

private void sendPositionBeacon(double latitude, double longitude) {
if (getMode() != MODE_RX) { // Can only beacon in rx mode (e.g. not tx or scan)
Log.d("DEBUG", "Skipping position beacon because not in RX mode");
return;
}

Log.d("DEBUG", "Beaconing position via APRS now");

if (aprsPositionAccuracy == APRS_POSITION_APPROX) {
// Fuzz the location (2 decimal places gives a spot in the neighborhood)
longitude = Double.valueOf(String.format("%.2f", longitude));
latitude = Double.valueOf(String.format("%.2f", latitude));
}

ArrayList<Digipeater> digipeaters = new ArrayList<>();
digipeaters.add(new Digipeater("WIDE1*"));
digipeaters.add(new Digipeater("WIDE2-1"));
Position myPos = new Position(latitude, longitude);
try {
PositionField posField = new PositionField(("=" + myPos.toCompressedString()).getBytes(), "", 1);
APRSPacket aprsPacket = new APRSPacket(callsign, "BEACON", digipeaters, posField.getRawBytes());
aprsPacket.getAprsInformation().addAprsData(APRSTypes.T_POSITION, posField);
Packet ax25Packet = new Packet(aprsPacket.toAX25Frame());

txAX25Packet(ax25Packet);

callbacks.sentAprsBeacon(latitude, longitude);
} catch (Exception e) {
Log.d("DEBUG", "Exception while trying to beacon APRS location.");
e.printStackTrace();
}
}

public void sendAckMessage(String targetCallsign, String remoteMessageNum) {
// Prepare APRS packet, and use its bytes to populate an AX.25 packet.
MessagePacket msgPacket = new MessagePacket(targetCallsign, "ack" + remoteMessageNum, remoteMessageNum);
ArrayList<Digipeater> digipeaters = new ArrayList<>();
Expand All @@ -1459,7 +1619,12 @@ public void sendAckMessage(String targetCallsign, String remoteMessageNum) {
txAX25Packet(ax25Packet);
}

public void sendChatMessage(String targetCallsign, String outText) {
/**
* @param targetCallsign
* @param outText
* @return The message number that was used for the message, or -1 if there was a problem.
*/
public int sendChatMessage(String targetCallsign, String outText) {
// Remove reserved APRS characters.
outText = outText.replace('|', ' ');
outText = outText.replace('~', ' ');
Expand All @@ -1475,7 +1640,7 @@ public void sendChatMessage(String targetCallsign, String outText) {
digipeaters.add(new Digipeater("WIDE2-1"));
if (null == callsign || callsign.trim().equals("")) {
Log.d("DEBUG", "Error: Tried to send a chat message with no sender callsign.");
return;
return -1;
}
if (null == targetCallsign || targetCallsign.trim().equals("")) {
Log.d("DEBUG", "Warning: Tried to send a chat message with no recipient callsign, defaulted to 'CQ'.");
Expand All @@ -1488,11 +1653,13 @@ public void sendChatMessage(String targetCallsign, String outText) {
ax25Packet = new Packet(aprsPacket.toAX25Frame());
} catch (IllegalArgumentException iae) {
callbacks.chatError("Error in your callsign or To: callsign.");
return;
return -1;
}

// TODO start a timer to re-send this packet (up to a few times) if we don't receive an ACK for it.
txAX25Packet(ax25Packet);

return messageNumber - 1;
}

private void txAX25Packet(Packet ax25Packet) {
Expand Down
Loading

0 comments on commit d3387ab

Please sign in to comment.