polishing only

This commit is contained in:
Priec
2025-10-05 23:11:28 +02:00
parent b4e86bded6
commit 83b6465dc6

View File

@@ -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<TOPIC_MAX>,
@@ -53,22 +52,17 @@ enum MqttCommand {
Subscribe(HString<TOPIC_MAX>),
}
static CMD_CHAN: Channel<CriticalSectionRawMutex, MqttCommand, COMMAND_QUEUE> =
Channel::new();
static CMD_CHAN: Channel<CriticalSectionRawMutex, MqttCommand, COMMAND_QUEUE> = Channel::new();
// ===== Events (messages) from MQTT task to main =====
#[derive(Clone)]
pub struct IncomingMsg {
pub topic: HString<TOPIC_MAX>,
pub payload: HVec<u8, PAYLOAD_MAX>,
}
static EVT_CHAN: Channel<CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> =
Channel::new();
static EVT_CHAN: Channel<CriticalSectionRawMutex, IncomingMsg, EVENT_QUEUE> = 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<TOPIC_MAX> = 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?;
}