aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2024-01-03 21:57:27 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2024-01-03 21:57:27 +0100
commitc2742cde3d034b2af9bbcee90765338ee094e6cc (patch)
treec35399d5d52725efbf3ebaaee83752574bf2ba1c /src/main.rs
parent43201470d4092f54c176dd5ebd2d7b0bbf15192e (diff)
downloadcats-radio-node-c2742cde3d034b2af9bbcee90765338ee094e6cc.tar.gz
cats-radio-node-c2742cde3d034b2af9bbcee90765338ee094e6cc.tar.bz2
cats-radio-node-c2742cde3d034b2af9bbcee90765338ee094e6cc.zip
Add src/radio and freq config
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs64
1 files changed, 61 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 20fedf6..9a4ae2b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,11 @@
+use anyhow::Context;
+use log::{debug, info, warn, error};
use std::sync::{Arc, Mutex};
+use tokio::sync::mpsc;
use sqlx::{Connection, SqliteConnection};
+use radio::{RadioManager, MAX_PACKET_LEN};
+mod radio;
mod config;
mod ui;
@@ -13,8 +18,7 @@ type SharedState = Arc<Mutex<AppState>>;
#[tokio::main]
async fn main() -> std::io::Result<()> {
-
- // simple_logger::
+ simple_logger::SimpleLogger::new().env().init().unwrap();
let mut conn = SqliteConnection::connect("sqlite:cats-radio-node.db").await.unwrap();
sqlx::migrate!()
@@ -24,12 +28,66 @@ async fn main() -> std::io::Result<()> {
let conf = config::Config::load().expect("Could not load config");
+ if conf.freq == 0 {
+ warn!("Frequency {0} is zero, disabling radio", conf.freq);
+ }
+ else if !(430000..=436380).contains(&conf.freq) {
+ error!("Frequency {} kHz out of range (430MHz - 436.375MHz), skipping radio setup", conf.freq);
+ }
+ else {
+ info!("Setting up radio");
+ let (packet_tx, mut packet_receive) = mpsc::channel(16);
+ let (packet_send, packet_rx) = mpsc::channel(16);
+ let mut radio = RadioManager::new(packet_tx, packet_rx).expect("Could not initialize radio");
+
+ let channel = ((conf.freq - 430000) / 25) as u8;
+ radio.set_channel(channel);
+ let actual_freq = 430000 + 25 * channel as u32;
+ info!("Setting up radio on {actual_freq} kHz...");
+
+ tokio::task::spawn(async move {
+ loop {
+ if let Err(e) = radio.process_forever().await {
+ error!("Radio error: {e}")
+ }
+ }
+ });
+
+ tokio::task::spawn(async move {
+ loop {
+ match packet_receive
+ .recv()
+ .await
+ .context("Packet receive channel died") {
+ Ok((packet, rssi)) => {
+ debug!("RX RSSI {} len {}", rssi, packet.len());
+ let mut buf = [0; MAX_PACKET_LEN];
+ match ham_cats::packet::Packet::fully_decode(&packet, &mut buf) {
+ Ok(packet) => {
+ if let Some(ident) = packet.identification() {
+ debug!(" From {}-{}", ident.callsign, ident.ssid);
+ }
+ // TODO save to db
+ }
+ Err(e) => {
+ warn!("Failed to decode packet: {}", e);
+ }
+ }
+ },
+ Err(e) => warn!("Failed to decode packet: {}", e),
+ }
+ }
+ });
+ }
+
let shared_state = Arc::new(Mutex::new(AppState {
conf,
db: Mutex::new(conn)
}));
- ui::serve(3000, shared_state).await;
+ let port = 3000;
+ info!("Setting up listener on port {port}");
+ ui::serve(port, shared_state).await;
Ok(())
}