1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
// #![cfg_attr(not(test), no_std)] // extern crate alloc; // extern crate embedded_nal; // extern crate mqttrs; // #[macro_use] // extern crate nb; // use embedded_timeout_macros::{block_timeout, TimeoutError}; // use bytes::BytesMut; // use embedded_nal::TcpStack; // use mqttrs::{decode, encode, ConnectReturnCode, Packet, PacketType, Pid, Publish, QosPid}; // pub use mqttrs::{Connect, Protocol, QoS}; // use alloc::{string::String, vec::Vec}; // #[derive(Debug, Clone, PartialEq)] // pub enum Error { // Network, // Encoding(mqttrs::Error), // Timeout, // ConnectionDenied, // InvalidPacket, // PongTimeout, // Unknown, // } // impl<E> From<TimeoutError<E>> for Error { // fn from(_e: TimeoutError<E>) -> Self { // Error::Timeout // } // } // impl From<mqttrs::Error> for Error { // fn from(e: mqttrs::Error) -> Self { // Error::Encoding(e) // } // } // pub struct MQTTClient<'a, N, T> // where // N: TcpStack, // { // pub last_pid: Pid, // pub keep_alive_interval: u32, // pub pong_pending: bool, // pub(crate) rx_buf: BytesMut, // pub(crate) tx_buf: BytesMut, // pub(crate) network: &'a N, // pub(crate) socket: N::TcpSocket, // pub(crate) keep_alive_timer: T, // pub(crate) command_timer: T, // } // impl<'a, N, T> MQTTClient<'a, N, T> // where // N: TcpStack, // T: embedded_hal::timer::CountDown, // T::Time: From<u32>, // { // pub fn new( // network: &'a N, // socket: N::TcpSocket, // keep_alive_timer: T, // command_timer: T, // rx_size: usize, // tx_size: usize, // ) -> Self { // MQTTClient { // last_pid: Pid::new(), // keep_alive_interval: 0, // pong_pending: false, // rx_buf: BytesMut::with_capacity(rx_size), // tx_buf: BytesMut::with_capacity(tx_size), // network, // socket, // keep_alive_timer, // command_timer, // } // } // fn next_pid(&mut self) -> Pid { // self.last_pid = self.last_pid + 1; // self.last_pid // } // fn send_buffer(&mut self) -> Result<(), Error> { // block!(self.network.write(&mut self.socket, &self.tx_buf)).map_err(|_| Error::Network)?; // // reset keep alive timer // self.keep_alive_timer.start(self.keep_alive_interval); // Ok(()) // } // fn cycle(&mut self) -> nb::Result<Packet, Error> { // let size = self // .network // .read(&mut self.socket, &mut self.rx_buf) // .map_err(|_| Error::Network)?; // if size == 0 { // // No data available // return Err(nb::Error::WouldBlock); // } // let packet = decode(&mut self.rx_buf) // .map_err(|e| Error::Encoding(e))? // .ok_or(nb::Error::WouldBlock)?; // match &packet { // Packet::Publish(p) => { // // Handle callbacks! // match p.qospid { // QosPid::AtMostOnce => {} // QosPid::AtLeastOnce(pid) => { // let pkt = Packet::Puback(pid).into(); // encode(&pkt, &mut self.tx_buf).map_err(|e| Error::Encoding(e))?; // self.send_buffer()?; // } // QosPid::ExactlyOnce(pid) => { // let pkt = Packet::Pubrec(pid).into(); // encode(&pkt, &mut self.tx_buf).map_err(|e| Error::Encoding(e))?; // self.send_buffer()?; // } // } // } // Packet::Pubrec(pid) => { // let pkt = Packet::Pubrel(pid.clone()).into(); // encode(&pkt, &mut self.tx_buf).map_err(|e| Error::Encoding(e))?; // self.send_buffer()?; // } // Packet::Pubrel(pid) => { // let pkt = Packet::Pubcomp(pid.clone()).into(); // encode(&pkt, &mut self.tx_buf).map_err(|e| Error::Encoding(e))?; // self.send_buffer()?; // } // Packet::Pingresp => { // self.pong_pending = false; // } // _ => {} // }; // Ok(packet) // } // fn cycle_until(&mut self, packet_type: PacketType) -> nb::Result<Packet, Error> { // let packet = self.cycle()?; // if packet.get_type() == packet_type { // Ok(packet) // } else { // Err(nb::Error::Other(Error::InvalidPacket)) // } // } // pub fn publish(&mut self, qos: QoS, topic_name: String, payload: Vec<u8>) -> Result<(), Error> { // let pid = self.next_pid(); // let qospid = match qos { // QoS::AtMostOnce => QosPid::AtMostOnce, // QoS::AtLeastOnce => QosPid::AtLeastOnce(pid), // QoS::ExactlyOnce => QosPid::ExactlyOnce(pid), // }; // let pkt = Publish { // dup: false, // qospid, // retain: false, // topic_name, // payload, // }; // encode(&pkt.into(), &mut self.tx_buf)?; // self.send_buffer()?; // match qospid.qos() { // QoS::AtMostOnce => {} // QoS::AtLeastOnce => { // block!(self.cycle_until(PacketType::Puback))?; // } // QoS::ExactlyOnce => { // block!(self.cycle_until(PacketType::Pubcomp))?; // } // }; // Ok(()) // } // pub fn yield_client(&mut self) -> Result<(), Error> { // // self.cycle_until() // Ok(()) // } // pub fn keep_alive(&mut self) -> Result<(), Error> { // // return immediately if keep alive interval is zero // if self.keep_alive_interval == 0 { // return Ok(()); // }; // // return immediately if no ping is due // if self.keep_alive_timer.wait().is_ok() { // return Ok(()); // } // // a ping is due // // fail immediately if a pong is already pending // if self.pong_pending { // return Err(Error::PongTimeout); // }; // // encode & send pingreq packet // let pkt = Packet::Pingreq.into(); // encode(&pkt, &mut self.tx_buf)?; // self.send_buffer()?; // // set flag // self.pong_pending = true; // Ok(()) // } // pub fn disconnect(&mut self) -> Result<(), Error> { // // encode & send disconnect packet // let pkt = Packet::Disconnect.into(); // encode(&pkt, &mut self.tx_buf)?; // self.send_buffer()?; // Ok(()) // } // pub fn connect(&mut self, options: Connect) -> Result<(), Error> { // // save keep alive interval // self.keep_alive_interval = options.keep_alive as u32; // // set keep alive timer // self.keep_alive_timer.start(self.keep_alive_interval); // // reset pong pending flag // self.pong_pending = false; // // encode connect packet // encode(&options.into(), &mut self.tx_buf)?; // // send packet // self.send_buffer()?; // // wait for connack packet // if let Packet::Connack(c) = block_timeout!( // &mut self.command_timer, // self.cycle_until(PacketType::Connack) // )? { // if c.code != ConnectReturnCode::Accepted { // Err(Error::ConnectionDenied) // } else { // Ok(()) // } // } else { // Err(Error::InvalidPacket) // } // } // }