Added extension piece of code for handling the mavlink mocker

This commit is contained in:
2026-05-05 12:19:14 -03:00
parent a43aa60f45
commit a9f09b6ce6
10 changed files with 1098 additions and 96 deletions

View File

@@ -15,15 +15,14 @@ pub enum UdpCommand {
pub struct UdpClient {
pub(crate) tx: Sender<UdpCommand>,
pub(crate) address: String,
}
impl UdpClient {
pub fn start(&self, address: String, rx: Receiver<UdpCommand>, ctx: Context) {
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
client.stop();
}
thread::spawn(move || {
info!("Starting UDP client thread for destination {}", address);
let socket = match UdpSocket::bind("0.0.0.0:0") {
Ok(s) => s,
Err(e) => {
@@ -32,19 +31,28 @@ impl UdpClient {
"Failed to bind UDP socket",
e.to_string(),
);
info!("Failed to bind UDP socket: {}", e);
info!("Failed to bind UDP socket for {}: {}", address, e);
return;
}
};
if let Ok(local_addr) = socket.local_addr() {
info!(
"UDP client bound local socket {} for destination {}",
local_addr, address
);
}
let _ = ctx.callback_data("UDP SOCKET", "EUD Connected", address.clone());
info!("UDP client reported EUD Connected for {}", address);
let mut running = true;
while running {
match rx.recv() {
Ok(UdpCommand::SendMessage(message, context)) => {
info!("UDP client sending {} bytes to {}", message.len(), address);
if let Err(e) = socket.send_to(message.as_bytes(), &address) {
info!("Failed to send UDP message: {}", e);
info!("Failed to send UDP message to {}: {}", address, e);
let _ = context.callback_data(
"UDP SOCKET ERROR",
"Failed to send UDP message",
@@ -54,13 +62,15 @@ impl UdpClient {
}
Ok(UdpCommand::Stop) => {
running = false;
info!("Stopping UDP client.");
info!("Stopping UDP client for {}", address);
}
Err(error) => {
info!("Error receiving command: {}", error.to_string());
info!("Error receiving UDP command for {}: {}", address, error);
}
}
}
info!("UDP client thread exited for {}", address);
});
}
@@ -73,7 +83,9 @@ impl UdpClient {
pub fn stop(&self) {
let tx = self.tx.clone();
let address = self.address.clone();
thread::spawn(move || {
info!("Queueing stop for UDP client {}", address);
tx.send(UdpCommand::Stop).unwrap();
});
}
@@ -84,13 +96,31 @@ lazy_static! {
}
pub fn start(ctx: Context, address: String) -> &'static str {
info!("UDP socket start requested for {}", address);
let (tx, rx): (Sender<UdpCommand>, Receiver<UdpCommand>) = mpsc::channel();
let client = UdpClient { tx };
client.start(address, rx, ctx);
let client = UdpClient {
tx,
address: address.clone(),
};
let mut client_guard = UDP_CLIENT.lock().unwrap();
*client_guard = Some(client);
{
let mut client_guard = UDP_CLIENT.lock().unwrap();
if let Some(ref existing_client) = *client_guard {
info!(
"Stopping previous UDP client {} before starting {}",
existing_client.address, address
);
existing_client.stop();
}
*client_guard = Some(UdpClient {
tx: client.tx.clone(),
address: client.address.clone(),
});
}
client.start(address, rx, ctx);
"Starting UDP Client"
}
@@ -100,6 +130,7 @@ pub fn send_payload(ctx: Context, payload: String) -> &'static str {
client.send_payload(ctx, payload);
} else {
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
info!("UDP send requested while socket was not running");
}
"Sending payload to UDP server"
@@ -117,10 +148,12 @@ pub fn send_gps_cot(
pub fn stop(ctx: Context) -> &'static str {
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
info!("UDP socket stop requested for {}", client.address);
client.stop();
let _ = ctx.callback_null("UDP SOCKET", "EUD Disconnected");
} else {
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
info!("UDP stop requested while socket was not running");
}
"Stopping UDP Client"