mqttrust_core 0.6.0

MQTT Client
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
use crate::packet::SerializedPacket;
use crate::Notification;
#[cfg(not(feature = "std"))]
use crate::PublishNotification;
use core::convert::TryInto;
use fugit::TimerDurationU32;
use fugit::TimerInstantU32;
#[cfg(not(feature = "std"))]
use heapless::{pool, pool::singleton::Pool};
use heapless::{FnvIndexMap, FnvIndexSet, IndexMap, IndexSet};
use mqttrust::encoding::v4::*;

#[allow(unused)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
pub enum MqttConnectionStatus {
    Handshake,
    Connected,
    Disconnected,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
pub enum StateError {
    /// Broker's error reply to client's connect packet
    Connect(ConnectReturnCode),
    /// Invalid state for a given operation
    InvalidState,
    /// Received a packet (ack) which isn't asked for
    Unsolicited,
    /// Last pingreq isn't acked
    AwaitPingResp,
    /// Received a wrong packet while waiting for another packet
    WrongPacket,
    PayloadEncoding,
    InvalidUtf8,
    /// The maximum number of messages allowed to be simultaneously in-flight has been reached.
    MaxMessagesInflight,
    /// Non-zero QoS publications require PID
    PidMissing,
    InvalidHeader,
}

#[cfg(not(feature = "std"))]
pool!(
    #[allow(non_upper_case_globals)]
    BoxedPublish: PublishNotification
);

/// State of the mqtt connection.
///
/// Methods will just modify the state of the object without doing any network
/// operations This abstracts the functionality better so that it's easy to
/// switch between synchronous code, tokio (or) async/await
pub struct MqttState<const TIMER_HZ: u32> {
    /// Connection status
    pub connection_status: MqttConnectionStatus,
    /// Status of last ping
    pub await_pingresp: bool,
    /// Packet id of the last outgoing packet
    pub last_pid: Pid,
    /// Outgoing QoS 1, 2 publishes which aren't acked yet
    pub(crate) outgoing_pub: FnvIndexMap<u16, Inflight<TIMER_HZ, 1536>, 2>,
    /// Packet ids of released QoS 2 publishes
    pub outgoing_rel: FnvIndexSet<u16, 2>,
    /// Packet ids on incoming QoS 2 publishes
    pub incoming_pub: FnvIndexSet<u16, 2>,
    last_ping: StartTime<TIMER_HZ>,
}

impl<const TIMER_HZ: u32> MqttState<TIMER_HZ> {
    /// Creates new mqtt state. Same state should be used during a
    /// connection for persistent sessions while new state should
    /// instantiated for clean sessions
    pub fn new() -> Self {
        #[cfg(not(feature = "std"))]
        {
            const LEN: usize = core::mem::size_of::<heapless::pool::Node<PublishNotification>>()
                + core::mem::align_of::<heapless::pool::Node<PublishNotification>>()
                - (core::mem::size_of::<heapless::pool::Node<PublishNotification>>()
                    % core::mem::align_of::<heapless::pool::Node<PublishNotification>>());

            static mut PUBLISH_MEM: [u8; LEN] = [0u8; LEN];
            BoxedPublish::grow(unsafe { &mut PUBLISH_MEM });
        }

        MqttState {
            connection_status: MqttConnectionStatus::Disconnected,
            await_pingresp: false,
            last_pid: Pid::new(),

            outgoing_pub: IndexMap::new(),
            outgoing_rel: IndexSet::new(),
            incoming_pub: IndexSet::new(),
            last_ping: StartTime::default(),
        }
    }

    /// Consolidates handling of all outgoing mqtt packet logic. Returns a
    /// packet which should be put on to the network by the eventloop
    pub fn handle_outgoing_packet<'b>(
        &mut self,
        packet: Packet<'b>,
    ) -> Result<Packet<'b>, StateError> {
        match packet {
            Packet::Pingreq => self.handle_outgoing_ping(),
            _ => unreachable!(),
        }
    }

    /// Consolidates handling of all outgoing mqtt packet logic. Returns a
    /// packet which should be put on to the network by the eventloop
    pub fn handle_outgoing_request(
        &mut self,
        request: &mut SerializedPacket<'_>,
        now: &TimerInstantU32<TIMER_HZ>,
    ) -> Result<(), StateError> {
        match request.header()?.typ {
            PacketType::Publish => self.handle_outgoing_publish(request, now)?,
            PacketType::Subscribe => {
                let pid = self.next_pid();
                trace!("Sending Subscribe({:?})", pid);
                request.set_pid(pid)?
            }
            PacketType::Unsubscribe => {
                let pid = self.next_pid();
                trace!("Sending Unsubscribe({:?})", pid);
                request.set_pid(pid)?
            }
            _ => unreachable!(),
        }

        Ok(())
    }

    /// Consolidates handling of all incoming mqtt packets. Returns a
    /// `Notification` which for the user to consume and `Packet` which for the
    /// eventloop to put on the network E.g For incoming QoS1 publish packet,
    /// this method returns (Publish, Puback). Publish packet will be forwarded
    /// to user and Pubck packet will be written to network
    pub fn handle_incoming_packet<'b>(
        &mut self,
        packet: Packet<'b>,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        match packet {
            Packet::Connack(connack) => self
                .handle_incoming_connack(connack)
                .map(|()| (Notification::ConnAck.into(), None)),
            Packet::Pingresp => self.handle_incoming_pingresp(),
            Packet::Publish(publish) => self.handle_incoming_publish(publish),
            Packet::Suback(suback) => self.handle_incoming_suback(suback),
            Packet::Unsuback(pid) => self.handle_incoming_unsuback(pid),
            Packet::Puback(pid) => self.handle_incoming_puback(pid),
            Packet::Pubrec(pid) => self.handle_incoming_pubrec(pid),
            Packet::Pubrel(pid) => self.handle_incoming_pubrel(pid),
            Packet::Pubcomp(pid) => self.handle_incoming_pubcomp(pid),
            _ => {
                error!("Invalid incoming packet!");
                Ok((None, None))
            }
        }
    }

    /// Adds next packet identifier to QoS 1 and 2 publish packets and returns
    /// it by wrapping publish in packet
    fn handle_outgoing_publish(
        &mut self,
        request: &mut SerializedPacket<'_>,
        now: &TimerInstantU32<TIMER_HZ>,
    ) -> Result<(), StateError> {
        match request.header()?.qos {
            QoS::AtMostOnce => {
                trace!("Sending Publish({:?})", QoS::AtMostOnce);
            }
            QoS::AtLeastOnce => {
                let pid = self.next_pid();
                trace!("Sending Publish({:?}, {:?})", pid, QoS::AtLeastOnce);
                self.outgoing_pub
                    .insert(pid.get(), Inflight::new(StartTime::new(*now), &request.0))
                    .map_err(|_| StateError::MaxMessagesInflight)?;
                request.set_pid(pid)?;
            }
            QoS::ExactlyOnce => {
                let pid = self.next_pid();
                trace!("Sending Publish({:?}, {:?})", pid, QoS::ExactlyOnce);
                self.outgoing_pub
                    .insert(pid.get(), Inflight::new(StartTime::new(*now), &request.0))
                    .map_err(|_| StateError::MaxMessagesInflight)?;
                request.set_pid(pid)?;
            }
        }
        Ok(())
    }

    /// Iterates through the list of stored publishes and removes the publish
    /// with the matching packet identifier. Removal is now a O(n) operation.
    /// This should be usually ok in case of acks due to ack ordering in normal
    /// conditions. But in cases where the broker doesn't guarantee the order of
    /// acks, the performance won't be optimal
    fn handle_incoming_puback(
        &mut self,
        pid: Pid,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        if self.outgoing_pub.contains_key(&pid.get()) {
            let _publish = self.outgoing_pub.remove(&pid.get());

            let request = None;
            let notification = Some(Notification::Puback(pid));
            trace!("Received Puback({:?})", pid);
            Ok((notification, request))
        } else {
            error!("Unsolicited puback packet: {:?}", pid.get());
            // Err(StateError::Unsolicited)
            Ok((None, None))
        }
    }

    fn handle_incoming_suback<'a>(
        &mut self,
        suback: Suback<'a>,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        let request = None;
        trace!("Received Suback({:?})", suback.pid);
        // TODO: Add suback packet info here
        let notification = Some(Notification::Suback(suback.pid));
        Ok((notification, request))
    }

    fn handle_incoming_unsuback(
        &mut self,
        pid: Pid,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        let request = None;
        trace!("Received Unsuback({:?})", pid);
        let notification = Some(Notification::Unsuback(pid));
        Ok((notification, request))
    }

    /// Iterates through the list of stored publishes and removes the publish with the
    /// matching packet identifier. Removal is now a O(n) operation. This should be
    /// usually ok in case of acks due to ack ordering in normal conditions. But in cases
    /// where the broker doesn't guarantee the order of acks, the performance won't be optimal
    fn handle_incoming_pubrec(
        &mut self,
        pid: Pid,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        if self.outgoing_pub.contains_key(&pid.get()) {
            self.outgoing_pub.remove(&pid.get());
            self.outgoing_rel
                .insert(pid.get())
                .map_err(|_| StateError::InvalidState)?;

            let reply = Some(Packet::Pubrel(pid));
            let notification = Some(Notification::Pubrec(pid));
            Ok((notification, reply))
        } else {
            error!("Unsolicited pubrec packet: {:?}", pid.get());
            // Err(StateError::Unsolicited)
            Ok((None, None))
        }
    }

    /// Results in a publish notification in all the QoS cases. Replys with an ack
    /// in case of QoS1 and Replys rec in case of QoS while also storing the message
    fn handle_incoming_publish<'b>(
        &mut self,
        publish: Publish<'b>,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        let qospid = (publish.qos, publish.pid);

        #[cfg(not(feature = "std"))]
        let boxed_publish = BoxedPublish::alloc().unwrap();
        #[cfg(not(feature = "std"))]
        let notification = Notification::Publish(boxed_publish.init(publish.try_into().unwrap()));

        #[cfg(feature = "std")]
        let notification = Notification::Publish(std::boxed::Box::new(publish.try_into().unwrap()));

        let request = match qospid {
            (QoS::AtMostOnce, _) => None,
            (QoS::AtLeastOnce, Some(pid)) => Some(Packet::Puback(pid)),
            (QoS::ExactlyOnce, Some(pid)) => {
                self.incoming_pub.insert(pid.get()).map_err(|_| {
                    error!("Failed to insert incoming pub!");
                    StateError::InvalidState
                })?;

                Some(Packet::Pubrec(pid))
            }
            _ => return Err(StateError::InvalidHeader),
        };
        Ok((Some(notification), request))
    }

    fn handle_incoming_pubrel(
        &mut self,
        pid: Pid,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        if self.incoming_pub.contains(&pid.get()) {
            self.incoming_pub.remove(&pid.get());
            let reply = Packet::Pubcomp(pid);
            Ok((None, Some(reply)))
        } else {
            error!("Unsolicited pubrel packet: {:?}", pid.get());
            // Err(StateError::Unsolicited)
            Ok((None, None))
        }
    }

    fn handle_incoming_pubcomp(
        &mut self,
        pid: Pid,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        if self.outgoing_rel.contains(&pid.get()) {
            self.outgoing_rel.remove(&pid.get());
            let notification = Some(Notification::Pubcomp(pid));
            let reply = None;
            Ok((notification, reply))
        } else {
            error!("Unsolicited pubcomp packet: {:?}", pid.get());
            // Err(StateError::Unsolicited)
            Ok((None, None))
        }
    }

    /// check when the last control packet/pingreq packet is received and return
    /// the status which tells if keep alive time has exceeded
    /// NOTE: status will be checked for zero keepalive times also
    fn handle_outgoing_ping<'b>(&mut self) -> Result<Packet<'b>, StateError> {
        // raise error if last ping didn't receive ack
        if self.await_pingresp {
            error!("Error awaiting for last ping response");
            return Err(StateError::AwaitPingResp);
        }

        self.await_pingresp = true;

        trace!("Sending Pingreq");

        Ok(Packet::Pingreq)
    }

    fn handle_incoming_pingresp(
        &mut self,
    ) -> Result<(Option<Notification>, Option<Packet<'static>>), StateError> {
        self.await_pingresp = false;
        trace!("Received Pingresp");
        Ok((None, None))
    }

    pub(crate) fn handle_outgoing_connect(&mut self) {
        self.connection_status = MqttConnectionStatus::Handshake;
    }

    pub fn handle_incoming_connack(&mut self, connack: Connack) -> Result<(), StateError> {
        match connack.code {
            ConnectReturnCode::Accepted
                if self.connection_status == MqttConnectionStatus::Handshake =>
            {
                debug!("MQTT connected!");
                self.connection_status = MqttConnectionStatus::Connected;
                Ok(())
            }
            ConnectReturnCode::Accepted
                if self.connection_status != MqttConnectionStatus::Handshake =>
            {
                error!(
                    "Invalid state. Expected = {:?}, Current = {:?}",
                    MqttConnectionStatus::Handshake,
                    self.connection_status
                );
                self.connection_status = MqttConnectionStatus::Disconnected;
                Err(StateError::InvalidState)
            }
            code => {
                error!("Connection failed. Connection error = {:?}", code as u8);
                self.connection_status = MqttConnectionStatus::Disconnected;
                Err(StateError::Connect(code))
            }
        }
    }

    fn next_pid(&mut self) -> Pid {
        self.last_pid = self.last_pid + 1;
        self.last_pid
    }

    pub(crate) fn last_ping_entry(&mut self) -> &mut StartTime<TIMER_HZ> {
        &mut self.last_ping
    }

    pub(crate) fn retries(
        &mut self,
        now: TimerInstantU32<TIMER_HZ>,
        interval: TimerDurationU32<TIMER_HZ>,
    ) -> impl Iterator<Item = (&u16, &mut Inflight<TIMER_HZ, 1536>)> + '_ {
        self.outgoing_pub
            .iter_mut()
            .filter(move |(_, inflight)| inflight.last_touch.has_elapsed(&now, interval))
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StartTime<const TIMER_HZ: u32>(Option<TimerInstantU32<TIMER_HZ>>);

impl<const TIMER_HZ: u32> Default for StartTime<TIMER_HZ> {
    fn default() -> Self {
        Self(None)
    }
}

impl<const TIMER_HZ: u32> StartTime<TIMER_HZ> {
    pub fn new(start_time: TimerInstantU32<TIMER_HZ>) -> Self {
        Self(start_time.into())
    }

    pub fn or_insert(&mut self, now: TimerInstantU32<TIMER_HZ>) -> &mut Self {
        self.0.get_or_insert(now);
        self
    }

    pub fn insert(&mut self, now: TimerInstantU32<TIMER_HZ>) {
        self.0.replace(now);
    }
}

impl<const TIMER_HZ: u32> StartTime<TIMER_HZ> {
    /// Check whether an interval has elapsed since this start time.
    pub fn has_elapsed(
        &self,
        now: &TimerInstantU32<TIMER_HZ>,
        interval: TimerDurationU32<TIMER_HZ>,
    ) -> bool {
        if let Some(start_time) = self.0 {
            let elapse_time = start_time + interval;
            elapse_time <= *now
        } else {
            false
        }
    }
}

/// Client publication message data.
#[derive(Debug)]
pub(crate) struct Inflight<const TIMER_HZ: u32, const L: usize> {
    /// A publish of non-zero QoS.
    publish: heapless::Vec<u8, L>,
    /// A timestmap used for retry and expiry.
    last_touch: StartTime<TIMER_HZ>,
}

impl<const TIMER_HZ: u32, const L: usize> Inflight<TIMER_HZ, L> {
    pub(crate) fn new(last_touch: StartTime<TIMER_HZ>, publish: &[u8]) -> Self {
        assert!(
            !matches!(
                decoder::Header::new(publish[0]).unwrap().qos,
                QoS::AtMostOnce
            ),
            "Only non-zero QoSs are allowed."
        );
        Self {
            publish: heapless::Vec::from_slice(publish).unwrap(),
            last_touch,
        }
    }

    pub(crate) fn last_touch_entry(&mut self) -> &mut StartTime<TIMER_HZ> {
        &mut self.last_touch
    }
}

impl<const TIMER_HZ: u32, const L: usize> Inflight<TIMER_HZ, L> {
    pub(crate) fn packet<'b>(&'b mut self, pid: u16) -> Result<&'b [u8], StateError> {
        let pid = pid.try_into().map_err(|_| StateError::PayloadEncoding)?;
        let mut packet = SerializedPacket(self.publish.as_mut());
        packet.set_pid(pid)?;
        Ok(packet.to_inner())
    }
}

#[cfg(test)]
mod test {
    use super::{BoxedPublish, MqttConnectionStatus, MqttState, Packet, StateError};
    use crate::{packet::SerializedPacket, Notification};
    use core::convert::TryFrom;
    use fugit::TimerInstantU32;
    use heapless::pool::singleton::Pool;
    use mqttrust::{
        encoding::v4::{decode_slice, encode_slice, Pid},
        Publish, QoS,
    };

    fn build_publish<'a>(qos: QoS, pid: Option<u16>) -> Publish<'a> {
        let topic = "hello/world";
        let payload = &[1, 2, 3];

        let pid = match qos {
            QoS::AtMostOnce => None,
            QoS::AtLeastOnce => pid.and_then(|p| Pid::try_from(p).ok()),
            QoS::ExactlyOnce => pid.and_then(|p| Pid::try_from(p).ok()),
        };

        Publish {
            qos,
            pid,
            payload,
            dup: false,
            retain: false,
            topic_name: topic,
        }
    }

    fn build_mqttstate() -> MqttState<1000> {
        let state = MqttState::new();
        const LEN: usize = 1024 * 10;
        static mut PUBLISH_MEM: [u8; LEN] = [0u8; LEN];
        BoxedPublish::grow(unsafe { &mut PUBLISH_MEM });
        state
    }

    #[test]
    fn handle_outgoing_requests() {
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);
        let mut mqtt = build_mqttstate();

        // Publish
        let publish = Packet::Publish(build_publish(QoS::AtMostOnce, None));

        let len = encode_slice(&publish, buf).unwrap();

        // Packet id shouldn't be set and publish shouldn't be saved in queue
        mqtt.handle_outgoing_request(&mut SerializedPacket(&mut buf[..len]), &now)
            .unwrap();
        // assert_eq!(publish_out.qos, QoS::AtMostOnce);
        // assert_eq!(mqtt.outgoing_pub.len(), 0);

        // // Subscribe
        // let subscribe = SubscribeRequest {
        //     topics: Vec::from_slice(&[
        //         SubscribeTopic {
        //             topic_path: String::from("some/topic"),
        //             qos: QoS::AtLeastOnce,
        //         },
        //         SubscribeTopic {
        //             topic_path: String::from("some/other/topic"),
        //             qos: QoS::ExactlyOnce,
        //         },
        //     ])
        //     .unwrap(),
        // };

        // // Packet id should be set and subscribe shouldn't be saved in publish queue
        // mqtt.handle_outgoing_request(subscribe.try_into().unwrap(), buf, &now)
        //     .unwrap();
        // let mut topics_iter = subscribe_out.topics.iter();

        // assert_eq!(subscribe_out.pid, Pid::try_from(2).unwrap());
        // assert_eq!(
        //     topics_iter.next(),
        //     Some(&SubscribeTopic {
        //         qos: QoS::AtLeastOnce,
        //         topic_path: String::from("some/topic")
        //     })
        // );
        // assert_eq!(
        //     topics_iter.next(),
        //     Some(&SubscribeTopic {
        //         qos: QoS::ExactlyOnce,
        //         topic_path: String::from("some/other/topic")
        //     })
        // );
        // assert_eq!(topics_iter.next(), None);
        // assert_eq!(mqtt.outgoing_pub.len(), 0);

        // // Unsubscribe
        // let unsubscribe = UnsubscribeRequest {
        //     topics: Vec::from_slice(&[
        //         String::from("some/topic"),
        //         String::from("some/other/topic"),
        //     ])
        //     .unwrap(),
        // };

        // // Packet id should be set and subscribe shouldn't be saved in publish queue
        // let unsubscribe_out =
        //     match mqtt.handle_outgoing_request(unsubscribe.try_into().unwrap(), buf, &now) {
        //         Ok(Packet::Unsubscribe(p)) => p,
        //         _ => panic!("Invalid packet. Should've been a unsubscribe packet"),
        //     };
        // let mut topics_iter = unsubscribe_out.topics.iter();

        // assert_eq!(unsubscribe_out.pid, Pid::try_from(3).unwrap());
        // assert_eq!(topics_iter.next(), Some(&String::from("some/topic")));
        // assert_eq!(topics_iter.next(), Some(&String::from("some/other/topic")));
        // assert_eq!(topics_iter.next(), None);
        // assert_eq!(mqtt.outgoing_pub.len(), 0);
    }

    #[test]
    fn outgoing_publish_handle_should_set_pid_correctly_and_add_publish_to_queue_correctly() {
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);

        let mut mqtt = build_mqttstate();

        // QoS0 Publish
        let publish = Packet::Publish(build_publish(QoS::AtMostOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);

        // Packet id shouldn't be set and publish shouldn't be saved in queue
        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();

        let publish_out = match decode_slice(pkg.to_inner()).unwrap() {
            Some(Packet::Publish(p)) => p,
            _ => panic!(),
        };
        assert_eq!(publish_out.qos, QoS::AtMostOnce);
        assert_eq!(mqtt.outgoing_pub.len(), 0);

        // QoS1 Publish
        let publish = Packet::Publish(build_publish(QoS::AtLeastOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);

        // Packet id should be set and publish should be saved in queue
        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();
        let publish_out = match decode_slice(pkg.to_inner()).unwrap() {
            Some(Packet::Publish(p)) => p,
            _ => panic!(),
        };
        assert_eq!(publish_out.qos, QoS::AtLeastOnce);
        assert_eq!(publish_out.pid, Some(Pid::try_from(2).unwrap()));
        assert_eq!(mqtt.outgoing_pub.len(), 1);
    }

    #[test]
    fn incoming_publish_should_be_added_to_queue_correctly() {
        let mut mqtt = build_mqttstate();

        // QoS0, 1, 2 Publishes
        let publish1 = build_publish(QoS::AtMostOnce, Some(1));
        let publish2 = build_publish(QoS::AtLeastOnce, Some(2));
        let publish3 = build_publish(QoS::ExactlyOnce, Some(3));

        mqtt.handle_incoming_publish(publish1).unwrap();
        mqtt.handle_incoming_publish(publish2).unwrap();
        mqtt.handle_incoming_publish(publish3).unwrap();

        // only qos2 publish should be add to queue
        assert_eq!(mqtt.incoming_pub.len(), 1);
        assert!(mqtt.incoming_pub.contains(&3));
    }

    #[test]
    fn incoming_qos2_publish_should_send_rec_to_network_and_publish_to_user() {
        let mut mqtt = build_mqttstate();
        let publish = build_publish(QoS::ExactlyOnce, Some(1));

        let (notification, request) = mqtt.handle_incoming_publish(publish).unwrap();

        match notification {
            Some(Notification::Publish(publish)) => assert_eq!(publish.qospid, QoS::ExactlyOnce),
            _ => panic!("Invalid notification: {:?}", notification),
        }

        match request {
            Some(Packet::Pubrec(pid)) => assert_eq!(pid.get(), 1),
            _ => panic!("Invalid network request: {:?}", request),
        }
    }

    #[test]
    fn incoming_puback_should_remove_correct_publish_from_queue() {
        let mut mqtt = build_mqttstate();
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);

        let publish1 = Packet::Publish(build_publish(QoS::AtLeastOnce, None));
        let len = encode_slice(&publish1, buf).unwrap();
        let mut pkg1 = SerializedPacket(&mut buf[..len]);
        mqtt.handle_outgoing_publish(&mut pkg1, &now).unwrap();

        assert_eq!(mqtt.outgoing_pub.len(), 1);

        let backup = mqtt.outgoing_pub.get_mut(&2).unwrap().packet(1).unwrap();
        let publish_out = match decode_slice(backup).unwrap() {
            Some(Packet::Publish(p)) => p,
            _ => panic!(),
        };
        assert_eq!(publish_out.qos, QoS::AtLeastOnce);

        mqtt.handle_incoming_puback(Pid::try_from(2).unwrap())
            .unwrap();
        assert_eq!(mqtt.outgoing_pub.len(), 0);
    }

    #[test]
    fn incoming_pubrec_should_release_correct_publish_from_queue_and_add_releaseid_to_rel_queue() {
        let mut mqtt = build_mqttstate();
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);

        let publish = Packet::Publish(build_publish(QoS::ExactlyOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);
        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();

        mqtt.handle_incoming_pubrec(Pid::try_from(2).unwrap())
            .unwrap();
        assert_eq!(mqtt.outgoing_pub.len(), 0);
        assert_eq!(mqtt.outgoing_rel.len(), 1);

        // check if the  element's pid is 2
        assert!(mqtt.outgoing_rel.contains(&2));
    }

    #[test]
    fn incoming_pubrec_should_send_release_to_network_and_nothing_to_user() {
        let mut mqtt = build_mqttstate();
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);
        let pid = Pid::try_from(2).unwrap();
        assert_eq!(pid.get(), 2);

        let publish = Packet::Publish(build_publish(QoS::ExactlyOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);
        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();

        let (notification, request) = mqtt.handle_incoming_pubrec(pid).unwrap();

        assert_eq!(notification, Some(Notification::Pubrec(pid)));
        assert_eq!(request, Some(Packet::Pubrel(pid)));
    }

    #[test]
    fn incoming_pubrel_should_send_comp_to_network_and_nothing_to_user() {
        let mut mqtt = build_mqttstate();
        let publish = build_publish(QoS::ExactlyOnce, Some(1));

        let pid = Pid::try_from(1).unwrap();
        assert_eq!(pid.get(), 1);

        mqtt.handle_incoming_publish(publish).unwrap();

        let (notification, request) = mqtt.handle_incoming_pubrel(pid).unwrap();
        assert_eq!(notification, None);
        assert_eq!(request, Some(Packet::Pubcomp(pid)));
    }

    #[test]
    fn incoming_pubcomp_should_release_correct_pid_from_release_queue() {
        let mut mqtt = build_mqttstate();
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);
        let publish = Packet::Publish(build_publish(QoS::ExactlyOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);

        let pid = Pid::try_from(2).unwrap();

        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();
        mqtt.handle_incoming_pubrec(pid).unwrap();

        mqtt.handle_incoming_pubcomp(pid).unwrap();
        assert_eq!(mqtt.outgoing_pub.len(), 0);
    }

    #[test]
    fn outgoing_ping_handle_should_throw_errors_for_no_pingresp() {
        let mut mqtt = build_mqttstate();
        let buf = &mut [0u8; 256];
        let now = TimerInstantU32::from_ticks(0);
        mqtt.connection_status = MqttConnectionStatus::Connected;
        assert_eq!(mqtt.handle_outgoing_ping(), Ok(Packet::Pingreq));
        assert!(mqtt.await_pingresp);

        // network activity other than pingresp
        let publish = Packet::Publish(build_publish(QoS::AtLeastOnce, None));
        let len = encode_slice(&publish, buf).unwrap();
        let mut pkg = SerializedPacket(&mut buf[..len]);

        mqtt.handle_outgoing_publish(&mut pkg, &now).unwrap();
        mqtt.handle_incoming_packet(Packet::Puback(Pid::try_from(2).unwrap()))
            .unwrap();

        // should throw error because we didn't get pingresp for previous ping
        assert_eq!(mqtt.handle_outgoing_ping(), Err(StateError::AwaitPingResp));
    }

    #[test]
    fn outgoing_ping_handle_should_succeed_if_pingresp_is_received() {
        let mut mqtt = build_mqttstate();

        mqtt.connection_status = MqttConnectionStatus::Connected;

        // should ping
        assert_eq!(mqtt.handle_outgoing_ping(), Ok(Packet::Pingreq));
        assert!(mqtt.await_pingresp);
        assert_eq!(
            mqtt.handle_incoming_packet(Packet::Pingresp),
            Ok((None, None))
        );
        assert!(!mqtt.await_pingresp);

        // should ping
        assert_eq!(mqtt.handle_outgoing_ping(), Ok(Packet::Pingreq));
        assert!(mqtt.await_pingresp);
    }
}