8 Commits

Author SHA1 Message Date
mattbk
863657f61f Cache locations shared in packets. 2025-10-27 20:59:58 -05:00
mattbk
f1d0f6dc31 Clean up APRS location logic and add spotter location. 2025-10-25 22:46:43 -05:00
mattbk
d353b1b055 Parse APRS location from packets that have that information. 2025-10-24 23:16:19 -05:00
f452e72ef9 Merge pull request 'Spot UI frames to spothole.app' (#13) from spothole into dev
Reviewed-on: #13
2025-10-22 02:17:12 +00:00
mattbk
796035ac59 Fix command. 2025-10-21 21:13:48 -05:00
mattbk
b14f4eee6a Add detail. 2025-10-21 21:12:51 -05:00
mattbk
5cd273a476 Update readme. 2025-10-21 21:10:43 -05:00
mattbk
a62cb8630b Use alternative spothole URL if desired. 2025-10-19 21:57:14 -05:00
3 changed files with 170 additions and 36 deletions

View File

@@ -8,6 +8,8 @@ authors = ["Chris, N6CTA <mail@n6cta.com>"]
anyhow = "1.0" anyhow = "1.0"
chrono = "0.4" chrono = "0.4"
clap = { version = "4", features = ["derive"] } clap = { version = "4", features = ["derive"] }
rand = "0.9.2"
regex = "1.12.2"
reqwest = { version = "0.12.24", features = ["json", "blocking"] } reqwest = { version = "0.12.24", features = ["json", "blocking"] }
serde_json = "1.0.145" serde_json = "1.0.145"
socket2 = "0.5" socket2 = "0.5"

View File

@@ -37,7 +37,7 @@ The compiled executable will be located in the `target/release` directory.
Run the executable with the required arguments: Run the executable with the required arguments:
```bash ```bash
mwtchahrd -i <IP> -p <PORT> [-c <CHANNEL>] [-d] [-u] mwtchahrd -i <IP> -p <PORT> [-c <CHANNEL>] [-d] [-u]
``` ```
### CommandLine Arguments ### CommandLine Arguments
@@ -56,6 +56,30 @@ mwtchahrd -i <IP> -p <PORT> [-c <CHANNEL>] [-d] [-u]
- `-u, --ui_only` - `-u, --ui_only`
Only display frames with a UI payload. When this flag is set, frames that are not UI are skipped. Only display frames with a UI payload. When this flag is set, frames that are not UI are skipped.
- `-b, --uip <UIP>`
Send UDP to what IP address (e.g. 127.0.0.1) [default: 127.0.0.1].
- `-k, --uport <UPORT>`
Send UDP to what port (e.g. 8000; 55555 disables UDP) [default: 55555].
- `-s, --spotter <SPOTTER>`
Spotter callsign (e.g. W1CDN).
- `-o, --spothole`
Spot UI frames to a Spothole server.
- `-O, --spothole-alt <SPOTHOLE_ALT>`
Provide a different URL than https://spothole.app/api/v1/spot.
- `-f, --freq <FREQ>`
Spotting frequency [default: 14105000].
- `-h, --help`
Print help.
- `-V, --version`
Print version.
## Examples ## Examples
@@ -83,6 +107,14 @@ Monitor only UI frames:
mwtchahrd -i 127.0.0.1 -p 8000 -u mwtchahrd -i 127.0.0.1 -p 8000 -u
``` ```
### Spotting to [Spothole.app](https://spothole.app) API
Sends only UI frames and mode is hardcoded as `PKT`. Spot from callsign `W1CDN` and report fixed frequency 14.105 MHz:
```bash
mwtchahrd --ip 192.168.0.6 --port 8000 --spotter W1CDN --spothole --freq 14105000
```
## Code Overview ## Code Overview
- **Validation Functions:** - **Validation Functions:**

View File

@@ -12,6 +12,9 @@ use std::net::UdpSocket;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use serde_json::json; use serde_json::json;
use reqwest; use reqwest;
use regex::Regex;
use rand::Rng;
/// Validate that the provided port string can be parsed into a u16 and is nonzero. /// Validate that the provided port string can be parsed into a u16 and is nonzero.
fn validate_port(port: &str) -> Result<u16, String> { fn validate_port(port: &str) -> Result<u16, String> {
@@ -35,7 +38,7 @@ struct Cli {
/// AGWPE server IP address (e.g. 127.0.0.1) /// AGWPE server IP address (e.g. 127.0.0.1)
#[arg(short = 'i', long)] #[arg(short = 'i', long)]
ip: std::net::IpAddr, ip: std::net::IpAddr,
/// AGWPE server TCP port (e.g. 8000) /// AGWPE server TCP port (e.g. 8000)
#[arg(short = 'p', long, value_parser = validate_port)] #[arg(short = 'p', long, value_parser = validate_port)]
port: u16, port: u16,
@@ -62,16 +65,28 @@ struct Cli {
/// Spotter callsign (e.g. W1CDN) /// Spotter callsign (e.g. W1CDN)
#[arg(short = 's', long)] #[arg(short = 's', long)]
spotter: Option<String>, my_call: Option<String>,
/// Spot UI frames to Spothole /// Spot UI frames to a Spothole server
#[arg(short = 'o', long, default_value_t = false)] #[arg(short = 'o', long, default_value_t = false)]
spothole: bool, spothole: bool,
/// Provide a different URL than https://spothole.app/api/v1/spot
#[arg(short = 'O', long)]
spothole_alt: Option<String>,
/// Spotting frequency /// Spotting frequency
#[arg(short = 'f', long, default_value_t = 14105000)] #[arg(short = 'f', long, default_value_t = 14105000)]
freq: u32, freq: u32,
/// Spotter latitude DD.dddd
#[arg(short = 'y', long, default_value_t = -9999.0_f64)]
my_lat: f64,
/// Spotter longitude DD.dddd; to send negaitve (W or S), use = e.g. --my-lon=-97.1
#[arg(short = 'x', long, default_value_t = -9999.0_f64)]
my_lon: f64,
} }
/// Convert a byte slice into a hex-dump string for debugging purposes. /// Convert a byte slice into a hex-dump string for debugging purposes.
@@ -363,7 +378,7 @@ impl BufferManager {
/// - Filters out frames destined for "NODES" and frames with an XID payload. /// - Filters out frames destined for "NODES" and frames with an XID payload.
/// - Optionally filters to only UI frames if requested. /// - Optionally filters to only UI frames if requested.
/// - Buffers multi-line frames and prints a formatted session line. /// - Buffers multi-line frames and prints a formatted session line.
fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) { fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager, loc_store: &mut HashMap<String, Vec<String>>) {
let hdr = &frame.header; let hdr = &frame.header;
// Process only frames on the specified channel. // Process only frames on the specified channel.
if hdr.port != cli.channel as i32 { if hdr.port != cli.channel as i32 {
@@ -372,8 +387,16 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
let source = hdr.callfrom_str(); let source = hdr.callfrom_str();
let basic_destination = hdr.callto_str(); let basic_destination = hdr.callto_str();
let timestamp = Local::now().format("%H:%M:%S").to_string(); let timestamp = Local::now().format("%H:%M:%S").to_string();
let spotter = &cli.spotter; let my_call = &cli.my_call;
let freq = &cli.freq; let freq = &cli.freq;
let my_lat = &cli.my_lat;
let my_lon = &cli.my_lon;
// If user provides an alternate Spothole URL, use that one
let spothole_url = match &cli.spothole_alt {
Some(spothole_alt) => spothole_alt,
None => &"https://spothole.app/api/v1/spot".to_string(),
};
// Filter and compute the text from the payload only once. // Filter and compute the text from the payload only once.
let text = filter_text(&frame.payload); let text = filter_text(&frame.payload);
@@ -385,6 +408,29 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
} else { } else {
basic_destination.clone() basic_destination.clone()
}; };
// Extract location from APRS format
let (lat, lon) = aprs_loc(&text);
// Store the location
// Only update location store if there is a real location
if lat > -9999.0_f64 && lon > -9999.0_f64 {
let loc = vec![lat.to_string(), lon.to_string()];
loc_store.insert(source.clone(), loc);
}
// Look up a stored location
// If it doesn't exist, set to empty
let stored_loc = match loc_store.get(&source) {
Some(loc_value) => loc_value,
None => &vec!["".to_string(), "".to_string()],
};
// Only send good locations on
let json_lat = stored_loc[0].clone() ;
let json_lon = stored_loc[1].clone() ;
//let json_lat = if lat > -9999.0_f64 && lon > -9999.0_f64 { lat.to_string() } else { old_loc[0].clone() };
//let json_lon = if lat > -9999.0_f64 && lon > -9999.0_f64 { lon.to_string() } else { old_loc[1].clone() };
// Ignore frames where the basic destination contains "NODES" (caseinsensitive). // Ignore frames where the basic destination contains "NODES" (caseinsensitive).
if basic_destination.to_uppercase().contains("NODES") { if basic_destination.to_uppercase().contains("NODES") {
@@ -411,30 +457,41 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
return; return;
} }
// In non-debug mode, print the session line and any additional lines.
if !cli.debug {
print_session_line(&timestamp, &source, &final_destination, &summary);
if lines.len() > 1 {
for line in &lines[1..] {
println!("{}", line);
}
}
}
println!("Stored location: {} {}", stored_loc[0].clone(), stored_loc[1].clone());
// If Spothole is enabled // If Spothole is enabled
if summary == "UI" && cli.spothole { if summary == "UI" && cli.spothole {
// curl --request POST --header "Content-Type: application/json" // POST JSON
// --data '{"dx_call":"M0TRT","time":1760019539, "freq":14200000, let packet = json!({
// "comment":"Test spot please ignore", "de_call":"M0TRT"}' https://spothole.app/api/v1/spot "dx_call": &source,
"de_call": &my_call,
// POST JSON "de_latitude": &my_lat,
let packet = json!({ "de_longitude": &my_lon,
"dx_call": &source, "freq": &freq,
"de_call": &spotter, "comment": &text,
"freq": &freq, "dx_latitude": &json_lat,
"comment": &text, "dx_longitude": &json_lon,
"mode": "PKT", "mode": "PKT",
"mode_type": "DATA", "mode_type": "DATA",
"mode_source": "SPOT", "mode_source": "SPOT",
"time": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(), "time": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
}); });
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let res = client.post("https://spothole.app/api/v1/spot") let res = client.post(spothole_url)
.json(&packet) .json(&packet)
.send(); .send();
println!("res = {res:?}"); println!("sent to {} = {}", spothole_url, packet);
println!("res = {res:?}");
} }
// Send UDP // Send UDP
@@ -445,25 +502,64 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
let packet = json!({ let packet = json!({
"final_destination": &final_destination, "final_destination": &final_destination,
"source": &source, "source": &source,
"spotter": &spotter, "spotter": &my_call,
"spotter_latitude": &my_lat,
"spotter_longitude": &my_lon,
"summary": &summary, "summary": &summary,
"text": &text, "text": &text,
"dx_latitude": &json_lat,
"dx_longitude": &json_lon,
"freq": &freq, "freq": &freq,
"timestamp": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(), "timestamp": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
"type": "data" "type": "data"
}); });
let _ = socket.expect("REASON").send_to(packet.to_string().as_bytes(), uaddr); let _ = socket.expect("REASON").send_to(packet.to_string().as_bytes(), uaddr);
} }
}
// In non-debug mode, print the session line and any additional lines.
if !cli.debug { fn aprs_loc(packet: &str) -> (f64, f64) {
print_session_line(&timestamp, &source, &final_destination, &summary); // Capture different pieces of the location stringf
if lines.len() > 1 { let re_loc = Regex::new(r"(?P<latd>\d{2})(?P<latm>[\d ]{2}\.[\d ]{2})(?P<ns>[nsNS])/(?P<lond>\d{3})(?P<lonm>[\d ]{2}\.[\d ]{2})(?P<ew>[ewEW])").unwrap();
for line in &lines[1..] {
println!("{}", line); // Only proceed if there were captures
} match re_loc.captures(&packet) {
Some(_caps) => {
// Break captures into named values
let loc = re_loc.captures(&packet).unwrap();
// Initiate randomness for ambiguity..
let mut rng = rand::rng();
// Convert to decimal degrees. If ambiguity spaces are included (see APRS spec), replace them with random digits.
let mut lat_dec: f64 = &loc["latd"].trim().parse().expect("Expects a number!") + (&loc["latm"].replace(" ", &rng.random_range(0..9).to_string()).trim().parse().expect("Expects a number!") / 60.0);
// If south, make negative
if &loc["ns"] == "S" {
lat_dec = lat_dec * -1.0
} }
// TODO if there are spaces in loc["latm"], the same spaces need to be in loc["lonm"] for proper ambiguity according to APRS spec
let mut lon_dec: f64 = &loc["lond"].trim().parse().expect("Expects a number!") + (&loc["lonm"].replace(" ", &rng.random_range(0..9).to_string()).trim().parse().expect("Expects a number!") / 60.0);
// If west, make negative
if &loc["ew"] == "W" {
lon_dec = lon_dec * -1.0
}
// String to paste into map for testing
//println!("{}, {}", lat_dec, lon_dec);
// Return
(lat_dec, lon_dec)
} }
// Otherwise if there were no captures, return bad data
None => {
// The regex did not match. Deal with it here!
(-9999.0_f64, -9999.0_f64)
}
}
} }
/// Main entry point: /// Main entry point:
@@ -477,6 +573,10 @@ fn main() -> Result<()> {
let uaddr = format!("{}:{}", cli.uip, cli.uport); let uaddr = format!("{}:{}", cli.uip, cli.uport);
let reconnect_delay_ms = 5000; let reconnect_delay_ms = 5000;
// Set up the location store
//let mut loc_store = HashMap:: new();
let mut loc_store: HashMap<String, Vec<String>> = HashMap::new();
if cli.uport != 55555 { if cli.uport != 55555 {
// Bind the client socket to any available address and port // Bind the client socket to any available address and port
let socket = UdpSocket::bind("0.0.0.0:0")?; let socket = UdpSocket::bind("0.0.0.0:0")?;
@@ -524,7 +624,7 @@ fn main() -> Result<()> {
while buffer.len() >= 36 { while buffer.len() >= 36 {
match parse_frame(&buffer, cli.debug) { match parse_frame(&buffer, cli.debug) {
Ok((consumed, frame)) => { Ok((consumed, frame)) => {
handle_frame(&frame, &cli, &mut buffers); handle_frame(&frame, &cli, &mut buffers, &mut loc_store);
// Remove the processed frame from the buffer. // Remove the processed frame from the buffer.
buffer.drain(0..consumed); buffer.drain(0..consumed);
} }