diff --git a/final/src/mqtt/client.rs b/final/src/mqtt/client.rs index ae81fbe..c1cf552 100644 --- a/final/src/mqtt/client.rs +++ b/final/src/mqtt/client.rs @@ -38,7 +38,6 @@ static MQTT_RX_BUF: ConstStaticCell<[u8; 1024]> = ConstStaticCell::new([0; 1024] // Tie TcpSocket lifetime to session, MQTT buffers to 'a (fixes E0521). type Client<'a, 'net> = MqttClient<'a, TcpSocket<'net>, 8, CountingRng>; -// ===== Commands from main to MQTT task ===== #[derive(Clone)] struct PublishMsg { topic: HString, @@ -53,22 +52,17 @@ enum MqttCommand { Subscribe(HString), } -static CMD_CHAN: Channel = - Channel::new(); +static CMD_CHAN: Channel = Channel::new(); -// ===== Events (messages) from MQTT task to main ===== #[derive(Clone)] pub struct IncomingMsg { pub topic: HString, pub payload: HVec, } -static EVT_CHAN: Channel = - Channel::new(); +static EVT_CHAN: Channel = Channel::new(); -// === Public API for main === - -// Enqueue a publish (non-alloc, copies into small heapless buffers) +// Public API for main pub async fn mqtt_publish( topic: &str, payload: &[u8], @@ -91,14 +85,13 @@ pub async fn mqtt_publish( .await; } -// Enqueue a subscribe request pub async fn mqtt_subscribe(topic: &str) { let mut t: HString = HString::new(); let _ = t.push_str(&topic[..core::cmp::min(topic.len(), TOPIC_MAX)]); CMD_CHAN.send(MqttCommand::Subscribe(t)).await; } -// Receiver for incoming MQTT messages (use in main) +// Receiver for incoming MQTT messages pub fn mqtt_events( ) -> Receiver<'static, CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> { EVT_CHAN.receiver() @@ -205,12 +198,10 @@ async fn connected_loop(client: &mut Client<'_, '_>) -> Result<(), ReasonCode> { .send(IncomingMsg { topic: t, payload: p }) .await; } - // Error receiving => reconnect Either::First(Err(e)) => { info!("MQTT receive error (reconnect): {:?}", e); return Err(e); } - // Ping timer fired Either::Second(_) => { client.send_ping().await?; }