95 lines
3.2 KiB
Rust
95 lines
3.2 KiB
Rust
use core::{cell::RefCell, str::from_utf8};
|
|
|
|
use common::{Name, Request};
|
|
use embassy_net::tcp::TcpSocket;
|
|
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
|
|
use embassy_time::Duration;
|
|
use embedded_io_async::Write;
|
|
use log::*;
|
|
use postcard::from_bytes;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{arraymap::ArrayMap, music::{COMMANDS}, scanner::READ_CARD};
|
|
|
|
pub static NAMES: Mutex<CriticalSectionRawMutex, RefCell<ArrayMap<64, u64, Name>>> =
|
|
Mutex::new(RefCell::new(ArrayMap::new()));
|
|
|
|
#[embassy_executor::task]
|
|
pub async fn server_task(stack: embassy_net::Stack<'static>) {
|
|
let mut rx_buffer = [0; 4096];
|
|
let mut tx_buffer = [0; 4096];
|
|
let mut buf = [0; 4096];
|
|
|
|
loop {
|
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
|
socket.set_timeout(Some(Duration::from_secs(3)));
|
|
info!("Listening on TCP:1234...");
|
|
if let Err(e) = socket.accept(1234).await {
|
|
warn!("accept error: {:?}", e);
|
|
continue;
|
|
}
|
|
|
|
info!("Received connection from {:?}", socket.remote_endpoint());
|
|
|
|
loop {
|
|
let n = match socket.read(&mut buf).await {
|
|
Ok(0) => {
|
|
warn!("read EOF");
|
|
break;
|
|
}
|
|
Ok(n) => n,
|
|
Err(e) => {
|
|
warn!("read error: {:?}", e);
|
|
break;
|
|
}
|
|
};
|
|
|
|
match from_bytes::<Request>(&buf[..n]) {
|
|
Ok(Request::RecentBadge) => {
|
|
let card = critical_section::with(|cs| {
|
|
READ_CARD.borrow(cs).clone().into_inner()
|
|
});
|
|
|
|
socket.write_all(itoa::Buffer::new().format(card).as_bytes()).await.unwrap();
|
|
},
|
|
Ok(Request::SetRecentBadge(name)) => {
|
|
let card = critical_section::with(|cs| {
|
|
READ_CARD.borrow(cs).clone().into_inner()
|
|
});
|
|
|
|
NAMES.lock().await.borrow_mut().insert(card, name);
|
|
}
|
|
Ok(Request::SetVolume(vol)) => {
|
|
COMMANDS.send(crate::music::MusicCommand::SetVolume(vol)).await;
|
|
}
|
|
Err(e) => { info!("parse error: {e}") }
|
|
}
|
|
|
|
|
|
info!("rxd {}", from_utf8(&buf[..n]).unwrap());
|
|
|
|
/*let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8);
|
|
|
|
match char::from_u32(segs.next().unwrap()[0] as u32).unwrap() {
|
|
'B' => {
|
|
let card = critical_section::with(|cs| {
|
|
READ_CARD.borrow(cs).clone().into_inner()
|
|
});
|
|
|
|
socket.write_all(itoa::Buffer::new().format(card).as_bytes()).await.unwrap();
|
|
},
|
|
'R' => {
|
|
let card = critical_section::with(|cs| {
|
|
READ_CARD.borrow(cs).clone().into_inner()
|
|
});
|
|
|
|
NAMES.lock().await.borrow_mut().insert(card, Name::Unknown);
|
|
|
|
},
|
|
_ => {}
|
|
|
|
}
|
|
*/
|
|
}
|
|
}
|
|
}
|