spacepackets 0.17.0

Generic implementations for various CCSDS and ECSS packet standards
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
//! This module contains all components required to create ECSS PUS A legacy telecommands.
//!
//! # Examples
//!
//! ```rust
//! use spacepackets::{CcsdsPacket, SpHeader};
//! use spacepackets::ecss::{PusPacket, WritablePusPacket};
//! use spacepackets::ecss::tc_pus_a::{PusTcCreator, PusTcReader, PusTcSecondaryHeader};
//! use arbitrary_int::u11;
//!
//! // Create a ping telecommand with no user application data
//! let pus_tc = PusTcCreator::new_no_app_data(
//!     SpHeader::new_from_apid(u11::new(0x02)),
//!     PusTcSecondaryHeader::new_simple(17, 1),
//!     true
//! );
//! println!("{:?}", pus_tc);
//! assert_eq!(pus_tc.service_type_id(), 17);
//! assert_eq!(pus_tc.message_subtype_id(), 1);
//! assert_eq!(pus_tc.apid().value(), 0x02);
//!
//! // Serialize TC into a raw buffer
//! let mut test_buf: [u8; 32] = [0; 32];
//! let size = pus_tc
//!     .write_to_bytes(test_buf.as_mut_slice())
//!     .expect("Error writing TC to buffer");
//! assert_eq!(size, 11);
//! println!("{:?}", &test_buf[0..size]);
//!
//! // Deserialize from the raw byte representation. No source ID, 0 spare bytes.
//! let pus_tc_deserialized = PusTcReader::new(&test_buf, None, 0).expect("Deserialization failed");
//! assert_eq!(pus_tc.service_type_id(), 17);
//! assert_eq!(pus_tc.message_subtype_id(), 1);
//! assert_eq!(pus_tc.apid().value(), 0x02);
//! ```
use crate::crc::{CRC_CCITT_FALSE, CRC_CCITT_FALSE_NO_TABLE};
use crate::ecss::tc::{AckFlags, ACK_ALL};
use crate::ecss::{
    crc_from_raw_data, sp_header_impls, user_data_from_raw,
    verify_crc16_ccitt_false_from_raw_to_pus_error, MessageTypeId, PusError, PusPacket, PusVersion,
    WritablePusPacket,
};
use crate::util::{UnsignedByteField, UnsignedEnum};
use crate::SpHeader;
use crate::{ByteConversionError, CcsdsPacket, PacketType, SequenceFlags, CCSDS_HEADER_LEN};
use arbitrary_int::{u11, u14, u3, u4};
use core::mem::size_of;
use delegate::delegate;
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use zerocopy::IntoBytes;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use super::verify_crc16_ccitt_false_from_raw_to_pus_error_no_table;

const PUS_VERSION: PusVersion = PusVersion::PusA;

/// Marker trait for PUS telecommand structures.
pub trait IsPusTelecommand {}

#[derive(Debug, Eq, PartialEq, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
enum AckOpts {
    Acceptance = 0b1000,
    Start = 0b0100,
    Progress = 0b0010,
    Completion = 0b0001,
}

/// Assuming 8 bytes of source ID and 7 bytes of spare.
pub const MAX_SEC_HEADER_LEN: usize = 18;
/// Maximum allowed number of space bytes.
pub const MAX_SPARE_BYTES: usize = 7;

/// Invalid number of spare bytes which must be between 0 and 7.
#[derive(Debug, Eq, PartialEq, Copy, Clone, thiserror::Error)]
#[error("invalid number of spare bytes, must be between 0 and 7")]
pub struct InvalidNumberOfSpareBytesError;

/// PUS version error.
#[derive(Debug, Eq, PartialEq, Copy, Clone, thiserror::Error)]
#[error("invalid version, expected PUS A (1), got {0}")]
pub struct VersionError(pub u8);

/// Trait for accessing PUS Telecommand secondary header fields.
pub trait GenericPusTcSecondaryHeader {
    /// PUS version number.
    fn pus_version(&self) -> Result<PusVersion, u4>;
    /// Acknowledgment flags.
    fn ack_flags(&self) -> AckFlags;
    /// Service type identifier.
    fn service(&self) -> u8;
    /// Subservice type identifier.
    fn subservice(&self) -> u8;
    /// Source ID.
    fn source_id(&self) -> Option<UnsignedByteField>;
    /// Number of spare bytes.
    fn spare_bytes(&self) -> usize;
}

/// PUS TC secondary header.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PusTcSecondaryHeader {
    /// Service type identifier.
    pub service: u8,
    /// Subservice type identifier.
    pub subservice: u8,
    /// Source ID.
    pub source_id: Option<UnsignedByteField>,
    /// Acknowledgment flags.
    pub ack: AckFlags,
    /// PUS version.
    pub version: PusVersion,
    spare_bytes: usize,
}

impl GenericPusTcSecondaryHeader for PusTcSecondaryHeader {
    #[inline]
    fn pus_version(&self) -> Result<PusVersion, u4> {
        Ok(self.version)
    }

    #[inline]
    fn ack_flags(&self) -> AckFlags {
        self.ack
    }

    #[inline]
    fn service(&self) -> u8 {
        self.service
    }

    #[inline]
    fn subservice(&self) -> u8 {
        self.subservice
    }

    #[inline]
    fn source_id(&self) -> Option<UnsignedByteField> {
        self.source_id
    }

    #[inline]
    fn spare_bytes(&self) -> usize {
        self.spare_bytes
    }
}

impl PusTcSecondaryHeader {
    /// Constructor which allows modifying the service and subservice and sets
    /// default values for the other fields.
    #[inline]
    pub fn new_simple(service: u8, subservice: u8) -> Self {
        PusTcSecondaryHeader {
            service,
            subservice,
            ack: ACK_ALL,
            source_id: None,
            version: PUS_VERSION,
            spare_bytes: 0,
        }
    }

    /// Generic constructor.
    #[inline]
    pub fn new(
        service: u8,
        subservice: u8,
        ack: AckFlags,
        source_id: Option<UnsignedByteField>,
        spare_bytes: usize,
    ) -> Self {
        PusTcSecondaryHeader {
            service,
            subservice,
            ack,
            source_id,
            version: PUS_VERSION,
            spare_bytes,
        }
    }

    /// Set number of spare bytes. Any value larger than 7 will be ignored.
    pub fn set_spare_bytes(&mut self, spare_bytes: usize) {
        if spare_bytes > 7 {
            return;
        }
        self.spare_bytes = spare_bytes;
    }

    /// Length of the written secondary header in bytes.
    pub fn written_len(&self) -> usize {
        let mut len = 3 + self.spare_bytes;
        if let Some(source_id) = self.source_id {
            len += source_id.size();
        }
        len
    }

    /// Converts the secondary header into a vector of bytes.
    #[cfg(feature = "alloc")]
    pub fn to_vec(&self) -> Result<Vec<u8>, ByteConversionError> {
        let mut buf = alloc::vec![0; self.written_len()];
        self.write_to_be_bytes(&mut buf)?;
        Ok(buf)
    }

    /// Write the secondary header to a provided buffer.
    pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
        if buf.len() < self.written_len() {
            return Err(ByteConversionError::ToSliceTooSmall {
                found: buf.len(),
                expected: self.written_len(),
            });
        }
        let mut current_idx = 0;
        buf[0] = ((PUS_VERSION as u8) << 4) | self.ack.raw_value().value();
        buf[1] = self.service;
        buf[2] = self.subservice;
        current_idx += 3;
        if let Some(source_id) = self.source_id {
            current_idx += source_id
                .write_to_be_bytes(&mut buf[current_idx..current_idx + source_id.size()])
                .unwrap();
        }
        if self.spare_bytes > 0 {
            buf[current_idx..current_idx + self.spare_bytes].fill(0);
            current_idx += self.spare_bytes;
        }

        Ok(current_idx)
    }

    /// Read the secondary header from a raw byte slice.
    pub fn from_bytes(
        data: &[u8],
        source_id_size: Option<usize>,
        spare_bytes: usize,
    ) -> Result<Self, PusError> {
        let expected_len = 3 + source_id_size.unwrap_or(0) + spare_bytes;
        if data.len() < expected_len {
            return Err(PusError::ByteConversion(
                ByteConversionError::FromSliceTooSmall {
                    found: data.len(),
                    expected: expected_len,
                },
            ));
        }
        let version = u4::new((data[0] >> 4) & 0b111);
        if version != PusVersion::PusA.raw_value() {
            return Err(PusError::VersionNotSupported(version));
        }
        let ack_flags_raw = u4::new(data[0] & 0b1111);
        let service = data[1];
        let subservice = data[2];
        let mut source_id = None;
        if let Some(source_id_len) = source_id_size {
            source_id = Some(
                UnsignedByteField::new_from_be_bytes(source_id_len, &data[3..3 + source_id_len])
                    .unwrap(),
            );
        }
        Ok(Self {
            service,
            subservice,
            source_id,
            ack: AckFlags::new_with_raw_value(ack_flags_raw),
            version: PusVersion::PusA,
            spare_bytes,
        })
    }
}

/// This class can be used to create PUS C telecommand packet. It is the primary data structure to
/// generate the raw byte representation of a PUS telecommand.
///
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait if the
/// [serde] feature is used, which allows to send around TC packets in a raw byte format using a
/// serde provider like [postcard](https://docs.rs/postcard/latest/postcard/).
///
/// There is no spare bytes support yet.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PusTcCreator<'app_data> {
    sp_header: SpHeader,
    /// Secondary header.
    pub sec_header: PusTcSecondaryHeader,
    app_data: &'app_data [u8],
}

impl<'app_data> PusTcCreator<'app_data> {
    /// Generates a new struct instance.
    ///
    /// # Arguments
    ///
    /// * `sp_header` - Space packet header information. The correct packet type and the secondary
    ///   header flag are set correctly by the constructor.
    /// * `sec_header` - Information contained in the data field header, including the service
    ///   and subservice type
    /// * `app_data` - Custom application data
    /// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length
    ///   field. If this is not set to true, [Self::update_ccsds_data_len] can be called to set
    ///   the correct value to this field manually
    #[inline]
    pub fn new(
        mut sp_header: SpHeader,
        sec_header: PusTcSecondaryHeader,
        app_data: &'app_data [u8],
        set_ccsds_len: bool,
    ) -> Self {
        sp_header.set_packet_type(PacketType::Tc);
        sp_header.set_sec_header_flag();
        let mut pus_tc = Self {
            sp_header,
            app_data,
            sec_header,
        };
        if set_ccsds_len {
            pus_tc.update_ccsds_data_len();
        }
        pus_tc
    }

    /// Simplified version of the [Self::new] function which allows to only specify service
    /// and subservice instead of the full PUS TC secondary header.
    #[inline]
    pub fn new_simple(
        sph: SpHeader,
        service: u8,
        subservice: u8,
        app_data: &'app_data [u8],
        set_ccsds_len: bool,
    ) -> Self {
        Self::new(
            sph,
            PusTcSecondaryHeader::new(service, subservice, ACK_ALL, None, 0),
            app_data,
            set_ccsds_len,
        )
    }

    /// Constructor without application data.
    #[inline]
    pub fn new_no_app_data(
        sp_header: SpHeader,
        sec_header: PusTcSecondaryHeader,
        set_ccsds_len: bool,
    ) -> Self {
        Self::new(sp_header, sec_header, &[], set_ccsds_len)
    }

    /// Space packet header.
    #[inline]
    pub fn sp_header(&self) -> &SpHeader {
        &self.sp_header
    }

    /// Mutable access to the space packet header.
    #[inline]
    pub fn sp_header_mut(&mut self) -> &mut SpHeader {
        &mut self.sp_header
    }

    /// Set the acknowledgment flags.
    #[inline]
    pub fn set_ack_field(&mut self, ack: AckFlags) {
        self.sec_header.ack = ack;
    }

    /// Set the source ID.
    #[inline]
    pub fn set_source_id(&mut self, source_id: Option<UnsignedByteField>) {
        self.sec_header.source_id = source_id;
    }

    /// Application data slice.
    #[inline]
    pub fn app_data(&'app_data self) -> &'app_data [u8] {
        self.user_data()
    }

    sp_header_impls!();

    /// Calculate the CCSDS space packet data length field and sets it
    /// This is called automatically if the `set_ccsds_len` argument in the [Self::new] call was
    /// used.
    /// If this was not done or the application data is set or changed after construction,
    /// this function needs to be called to ensure that the data length field of the CCSDS header
    /// is set correctly.
    #[inline]
    pub fn update_ccsds_data_len(&mut self) {
        self.sp_header.data_len =
            self.len_written() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
    }

    /// This function calculates and returns the CRC16 for the current packet.
    pub fn calc_own_crc16(&self) -> u16 {
        let mut digest = CRC_CCITT_FALSE.digest();
        let sph_zc = crate::zc::SpHeader::from(self.sp_header);
        digest.update(sph_zc.as_bytes());
        let mut buf: [u8; MAX_SEC_HEADER_LEN] = [0; MAX_SEC_HEADER_LEN];
        // Can not fail.
        let sec_header_len = self.sec_header.write_to_be_bytes(&mut buf).unwrap();
        digest.update(&buf[0..sec_header_len]);
        digest.update(self.app_data);
        digest.finalize()
    }

    /// This function calculates and returns the CRC16 for the current packet.
    pub fn calc_own_crc16_no_table(&self) -> u16 {
        let mut digest = CRC_CCITT_FALSE_NO_TABLE.digest();
        let sph_zc = crate::zc::SpHeader::from(self.sp_header);
        digest.update(sph_zc.as_bytes());
        let mut buf: [u8; MAX_SEC_HEADER_LEN] = [0; MAX_SEC_HEADER_LEN];
        // Can not fail.
        let sec_header_len = self.sec_header.write_to_be_bytes(&mut buf).unwrap();
        digest.update(&buf[0..sec_header_len]);
        digest.update(self.app_data);
        digest.finalize()
    }

    /// Append the raw PUS byte representation to a provided vector.
    #[cfg(feature = "alloc")]
    pub fn append_to_vec(&self, vec: &mut Vec<u8>) -> usize {
        let sph_zc = crate::zc::SpHeader::from(self.sp_header);
        let start_idx = vec.len();
        vec.extend_from_slice(sph_zc.as_bytes());
        vec.extend_from_slice(self.sec_header.to_vec().unwrap().as_slice());
        vec.extend_from_slice(self.app_data);
        let mut digest = CRC_CCITT_FALSE.digest();
        let mut appended_len =
            CCSDS_HEADER_LEN + self.sec_header.written_len() + self.app_data.len();
        digest.update(&vec[start_idx..start_idx + appended_len]);
        vec.extend_from_slice(&digest.finalize().to_be_bytes());
        appended_len += 2;
        appended_len
    }

    /// Write the raw PUS byte representation to a provided buffer.
    pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, ByteConversionError> {
        let writer_unfinalized = self.common_write(slice)?;
        Ok(writer_unfinalized.finalize())
    }

    /// Write the raw PUS byte representation to a provided buffer.
    pub fn write_to_bytes_crc_no_table(
        &self,
        slice: &mut [u8],
    ) -> Result<usize, ByteConversionError> {
        let writer_unfinalized = self.common_write(slice)?;
        Ok(writer_unfinalized.finalize_crc_no_table())
    }

    /// Write the raw PUS byte representation to a provided buffer.
    pub fn write_to_bytes_no_crc(&self, slice: &mut [u8]) -> Result<usize, ByteConversionError> {
        let writer_unfinalized = self.common_write(slice)?;
        Ok(writer_unfinalized.finalize_no_crc())
    }

    fn common_write<'a>(
        &self,
        slice: &'a mut [u8],
    ) -> Result<PusTcCreatorWithReservedAppData<'a>, ByteConversionError> {
        if self.len_written() > slice.len() {
            return Err(ByteConversionError::ToSliceTooSmall {
                found: slice.len(),
                expected: self.len_written(),
            });
        }
        let mut writer_unfinalized = PusTcCreatorWithReservedAppData::write_to_bytes_partially(
            slice,
            self.sp_header,
            self.sec_header,
            self.app_data.len(),
        )?;
        writer_unfinalized
            .app_data_mut()
            .copy_from_slice(self.app_data);
        Ok(writer_unfinalized)
    }
}

impl WritablePusPacket for PusTcCreator<'_> {
    #[inline]
    fn len_written(&self) -> usize {
        CCSDS_HEADER_LEN + self.sec_header.written_len() + self.app_data.len() + 2
    }

    /// Currently, checksum is always added.
    fn has_checksum(&self) -> bool {
        true
    }

    /// Write the raw PUS byte representation to a provided buffer.
    fn write_to_bytes_no_checksum(&self, slice: &mut [u8]) -> Result<usize, PusError> {
        Ok(Self::write_to_bytes_no_crc(self, slice)?)
    }

    fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
        Ok(Self::write_to_bytes(self, slice)?)
    }

    fn write_to_bytes_checksum_no_table(&self, slice: &mut [u8]) -> Result<usize, PusError> {
        Ok(Self::write_to_bytes_crc_no_table(self, slice)?)
    }
}

impl CcsdsPacket for PusTcCreator<'_> {
    delegate!(to self.sp_header {
        #[inline]
        fn ccsds_version(&self) -> u3;
        #[inline]
        fn packet_id(&self) -> crate::PacketId;
        #[inline]
        fn psc(&self) -> crate::PacketSequenceControl;
        #[inline]
        fn data_len(&self) -> u16;
    });
}

impl PusPacket for PusTcCreator<'_> {
    delegate!(to self.sec_header {
        #[inline]
        fn pus_version(&self) -> Result<PusVersion, u4>;
    });
    #[inline]
    fn message_type_id(&self) -> MessageTypeId {
        MessageTypeId {
            type_id: self.service_type_id(),
            subtype_id: self.message_subtype_id(),
        }
    }
    #[inline]
    fn service_type_id(&self) -> u8 {
        self.sec_header.service
    }

    #[inline]
    fn message_subtype_id(&self) -> u8 {
        self.sec_header.subservice
    }

    #[inline]
    fn user_data(&self) -> &[u8] {
        self.app_data
    }

    #[inline]
    fn checksum(&self) -> Option<u16> {
        Some(self.calc_own_crc16())
    }
}

impl GenericPusTcSecondaryHeader for PusTcCreator<'_> {
    delegate!(to self.sec_header {
        #[inline]
        fn pus_version(&self) -> Result<PusVersion, u4>;
        #[inline]
        fn service(&self) -> u8;
        #[inline]
        fn subservice(&self) -> u8;
        #[inline]
        fn source_id(&self) -> Option<UnsignedByteField>;
        #[inline]
        fn ack_flags(&self) -> AckFlags;
        #[inline]
        fn spare_bytes(&self) -> usize;
    });
}

impl IsPusTelecommand for PusTcCreator<'_> {}

/// A specialized variant of [PusTcCreator] designed for efficiency when handling large source
/// data.
///
/// Unlike [PusTcCreator], this type does not require the user to provide the application data
/// as a separate slice. Instead, it allows writing the application data directly into the provided
/// serialization buffer. This eliminates the need for an intermediate buffer and the associated
/// memory copy, improving performance, particularly when working with large payloads.
///
/// **Important:** The total length of the source data must be known and specified in advance
/// to ensure correct serialization behavior.
///
/// Note that this abstraction intentionally omits certain trait implementations that are available
/// on [PusTcCreator], as they are not applicable in this optimized usage pattern.
pub struct PusTcCreatorWithReservedAppData<'buf> {
    buf: &'buf mut [u8],
    app_data_offset: usize,
    full_len: usize,
}

impl<'buf> PusTcCreatorWithReservedAppData<'buf> {
    /// Generates a new instance with reserved space for the user application data.
    ///
    /// # Arguments
    ///
    /// * `sp_header` - Space packet header information. The correct packet type and the secondary
    ///   header flag are set correctly by the constructor.
    /// * `sec_header` - Information contained in the secondary header, including the service
    ///   and subservice type
    /// * `app_data_len` - Custom application data length
    #[inline]
    pub fn new(
        buf: &'buf mut [u8],
        mut sp_header: SpHeader,
        sec_header: PusTcSecondaryHeader,
        app_data_len: usize,
    ) -> Result<Self, ByteConversionError> {
        sp_header.set_packet_type(PacketType::Tc);
        sp_header.set_sec_header_flag();
        let len_written = CCSDS_HEADER_LEN + sec_header.written_len() + app_data_len + 2;
        if len_written > buf.len() {
            return Err(ByteConversionError::ToSliceTooSmall {
                found: buf.len(),
                expected: len_written,
            });
        }
        sp_header.data_len = len_written as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
        Self::write_to_bytes_partially(buf, sp_header, sec_header, app_data_len)
    }

    fn write_to_bytes_partially(
        buf: &'buf mut [u8],
        sp_header: SpHeader,
        sec_header: PusTcSecondaryHeader,
        app_data_len: usize,
    ) -> Result<Self, ByteConversionError> {
        let mut curr_idx = 0;
        sp_header.write_to_be_bytes(&mut buf[0..CCSDS_HEADER_LEN])?;
        curr_idx += CCSDS_HEADER_LEN;
        curr_idx += sec_header
            .write_to_be_bytes(&mut buf[curr_idx..curr_idx + sec_header.written_len()])?;
        let app_data_offset = curr_idx;
        curr_idx += app_data_len;
        Ok(Self {
            buf,
            app_data_offset,
            full_len: curr_idx + 2,
        })
    }

    /// Length of the written TC packet.
    #[inline]
    pub const fn len_written(&self) -> usize {
        self.full_len
    }

    /// Mutable access to the application data buffer.
    #[inline]
    pub fn app_data_mut(&mut self) -> &mut [u8] {
        &mut self.buf[self.app_data_offset..self.full_len - 2]
    }

    /// Access to the source data buffer.
    #[inline]
    pub fn app_data(&self) -> &[u8] {
        &self.buf[self.app_data_offset..self.full_len - 2]
    }

    /// Application data length.
    #[inline]
    pub fn app_data_len(&self) -> usize {
        self.full_len - 2 - self.app_data_offset
    }

    /// Finalize the TC packet by calculating and writing the CRC16.
    ///
    /// Returns the full packet length.
    pub fn finalize(self) -> usize {
        let mut digest = CRC_CCITT_FALSE.digest();
        digest.update(&self.buf[0..self.full_len - 2]);
        self.buf[self.full_len - 2..self.full_len]
            .copy_from_slice(&digest.finalize().to_be_bytes());
        self.full_len
    }

    /// Finalize the TC packet by calculating and writing the CRC16 using a table-less
    /// implementation.
    ///
    /// Returns the full packet length.
    pub fn finalize_crc_no_table(self) -> usize {
        let mut digest = CRC_CCITT_FALSE_NO_TABLE.digest();
        digest.update(&self.buf[0..self.full_len - 2]);
        self.buf[self.full_len - 2..self.full_len]
            .copy_from_slice(&digest.finalize().to_be_bytes());
        self.full_len
    }

    /// Finalize the TC packet without writing the CRC16.
    ///
    /// Returns the length WITHOUT the CRC16.
    #[inline]
    pub fn finalize_no_crc(self) -> usize {
        self.full_len - 2
    }
}

/// This class can be used to read a PUS TC telecommand from raw memory.
///
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait if the
/// [serde] feature is used, which allows to send around TC packets in a raw byte format using a
/// serde provider like [postcard](https://docs.rs/postcard/latest/postcard/).
///
/// There is no spare bytes support yet.
///
/// # Lifetimes
///
/// * `'raw_data` - Lifetime of the provided raw slice.
#[derive(Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PusTcReader<'raw_data> {
    #[cfg_attr(feature = "serde", serde(skip))]
    raw_data: &'raw_data [u8],
    sp_header: SpHeader,
    sec_header: PusTcSecondaryHeader,
    app_data: &'raw_data [u8],
    crc16: u16,
}

impl<'raw_data> PusTcReader<'raw_data> {
    /// Create a [PusTcReader] instance from a raw slice. On success, it returns a tuple containing
    /// the instance and the found byte length of the packet. This function also performs a CRC
    /// check and will return an appropriate [PusError] if the check fails.
    pub fn new(
        slice: &'raw_data [u8],
        source_id_size: Option<usize>,
        spare_bytes: usize,
    ) -> Result<Self, PusError> {
        let pus_tc = Self::new_no_crc_check(slice, source_id_size, spare_bytes)?;
        verify_crc16_ccitt_false_from_raw_to_pus_error(pus_tc.raw_data(), pus_tc.crc16())?;
        Ok(pus_tc)
    }

    /// Similar to [PusTcReader::new], but uses a table-less CRC16 algorithm which can reduce
    /// binary size and memory usage.
    pub fn new_crc_no_table(
        slice: &'raw_data [u8],
        source_id_size: Option<usize>,
        spare_bytes: usize,
    ) -> Result<Self, PusError> {
        let pus_tc = Self::new_no_crc_check(slice, source_id_size, spare_bytes)?;
        verify_crc16_ccitt_false_from_raw_to_pus_error_no_table(pus_tc.raw_data(), pus_tc.crc16())?;
        Ok(pus_tc)
    }

    /// Creates a new instance without performing a CRC check.
    pub fn new_no_crc_check(
        slice: &'raw_data [u8],
        source_id_size: Option<usize>,
        spare_bytes: usize,
    ) -> Result<Self, PusError> {
        let raw_data_len = slice.len();
        if raw_data_len < CCSDS_HEADER_LEN {
            return Err(ByteConversionError::FromSliceTooSmall {
                found: raw_data_len,
                expected: CCSDS_HEADER_LEN,
            }
            .into());
        }
        let mut current_idx = 0;
        let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
        current_idx += CCSDS_HEADER_LEN;
        let total_len = sp_header.packet_len();
        if raw_data_len < total_len {
            return Err(ByteConversionError::FromSliceTooSmall {
                found: raw_data_len,
                expected: total_len,
            }
            .into());
        }
        let sec_header =
            PusTcSecondaryHeader::from_bytes(&slice[current_idx..], source_id_size, spare_bytes)?;
        current_idx += sec_header.written_len();
        let raw_data = &slice[0..total_len];
        Ok(Self {
            sp_header,
            sec_header,
            raw_data,
            app_data: user_data_from_raw(current_idx, total_len, slice, true)?,
            crc16: crc_from_raw_data(raw_data)?,
        })
    }

    /// Application data slice.
    #[inline]
    pub fn app_data(&self) -> &[u8] {
        self.user_data()
    }

    /// Full raw data slice.
    #[inline]
    pub fn raw_data(&self) -> &[u8] {
        self.raw_data
    }

    /// Length of the packed PUS TC packet in bytes.
    #[inline]
    pub fn len_packed(&self) -> usize {
        self.sp_header.packet_len()
    }

    /// Space packet header.
    #[inline]
    pub fn sp_header(&self) -> &SpHeader {
        &self.sp_header
    }

    /// CRC16 checksum value.
    #[inline]
    pub fn crc16(&self) -> u16 {
        self.crc16
    }
}

impl PartialEq for PusTcReader<'_> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.raw_data == other.raw_data
    }
}

impl CcsdsPacket for PusTcReader<'_> {
    delegate!(to self.sp_header {
        #[inline]
        fn ccsds_version(&self) -> u3;
        #[inline]
        fn packet_id(&self) -> crate::PacketId;
        #[inline]
        fn psc(&self) -> crate::PacketSequenceControl;
        #[inline]
        fn data_len(&self) -> u16;
    });
}

impl PusPacket for PusTcReader<'_> {
    delegate!(to self.sec_header {
        #[inline]
        fn pus_version(&self) -> Result<PusVersion, u4>;
    });

    #[inline]
    fn message_type_id(&self) -> MessageTypeId {
        MessageTypeId {
            type_id: self.service_type_id(),
            subtype_id: self.message_subtype_id(),
        }
    }

    #[inline]
    fn service_type_id(&self) -> u8 {
        self.sec_header.service
    }

    #[inline]
    fn message_subtype_id(&self) -> u8 {
        self.sec_header.subservice
    }

    #[inline]
    fn has_checksum(&self) -> bool {
        true
    }

    #[inline]
    fn user_data(&self) -> &[u8] {
        self.app_data
    }

    #[inline]
    fn checksum(&self) -> Option<u16> {
        Some(self.crc16)
    }
}

impl GenericPusTcSecondaryHeader for PusTcReader<'_> {
    delegate!(to self.sec_header {
        #[inline]
        fn pus_version(&self) -> Result<PusVersion, u4>;
        #[inline]
        fn service(&self) -> u8;
        #[inline]
        fn subservice(&self) -> u8;
        #[inline]
        fn source_id(&self) -> Option<UnsignedByteField>;
        #[inline]
        fn ack_flags(&self) -> AckFlags;
        #[inline]
        fn spare_bytes(&self) -> usize;
    });
}

impl IsPusTelecommand for PusTcReader<'_> {}

impl PartialEq<PusTcCreator<'_>> for PusTcReader<'_> {
    fn eq(&self, other: &PusTcCreator) -> bool {
        self.sp_header == other.sp_header
            && self.sec_header == other.sec_header
            && self.app_data == other.app_data
    }
}

impl PartialEq<PusTcReader<'_>> for PusTcCreator<'_> {
    fn eq(&self, other: &PusTcReader) -> bool {
        self.sp_header == other.sp_header
            && self.sec_header == other.sec_header
            && self.app_data == other.app_data
    }
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use std::println;

    use super::*;
    use crate::ecss::{PusError, PusPacket, WritablePusPacket};
    use crate::util::{UnsignedByteFieldU16, UnsignedByteFieldU8};
    use crate::{ByteConversionError, SpHeader};
    use crate::{CcsdsPacket, SequenceFlags};
    use alloc::string::ToString;
    use alloc::vec::Vec;
    use arbitrary_int::{u11, u14};
    #[cfg(feature = "serde")]
    use postcard::{from_bytes, to_allocvec};

    fn base_ping_tc_full_ctor() -> PusTcCreator<'static> {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
        PusTcCreator::new_no_app_data(sph, tc_header, true)
    }

    fn base_ping_tc_simple_ctor() -> PusTcCreator<'static> {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        PusTcCreator::new_simple(sph, 17, 1, &[], true)
    }

    fn base_ping_tc_simple_ctor_with_app_data(app_data: &'static [u8]) -> PusTcCreator<'static> {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        PusTcCreator::new_simple(sph, 17, 1, app_data, true)
    }

    #[test]
    fn test_tc_fields() {
        let pus_tc = base_ping_tc_full_ctor();
        verify_test_tc(&pus_tc, false, 11);
    }

    #[test]
    fn test_serialization() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(size, 11);
        assert_eq!(
            pus_tc.checksum().unwrap(),
            u16::from_be_bytes(test_buf[size - 2..size].try_into().unwrap())
        );
    }

    #[test]
    fn test_serialization_with_trait_1() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = WritablePusPacket::write_to_bytes(&pus_tc, test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(size, 11);
        assert_eq!(
            pus_tc.checksum().unwrap(),
            u16::from_be_bytes(test_buf[size - 2..size].try_into().unwrap())
        );
    }

    #[test]
    fn test_serialization_with_trait_2() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size =
            WritablePusPacket::write_to_bytes_checksum_no_table(&pus_tc, test_buf.as_mut_slice())
                .expect("Error writing TC to buffer");
        assert_eq!(size, 11);
        assert_eq!(
            pus_tc.checksum().unwrap(),
            u16::from_be_bytes(test_buf[size - 2..size].try_into().unwrap())
        );
    }

    #[test]
    fn test_serialization_crc_no_table() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes_crc_no_table(test_buf.as_mut_slice())
            .expect("error writing tc to buffer");
        assert_eq!(size, 11);
        assert_eq!(
            pus_tc.checksum().unwrap(),
            u16::from_be_bytes(test_buf[size - 2..size].try_into().unwrap())
        );
    }

    #[test]
    fn test_serialization_no_crc() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes_no_crc(test_buf.as_mut_slice())
            .expect("error writing tc to buffer");
        assert_eq!(size, 9);
        assert_eq!(test_buf[10], 0);
        assert_eq!(test_buf[11], 0);
    }

    #[test]
    fn test_serialization_no_crc_with_trait() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = WritablePusPacket::write_to_bytes_no_checksum(&pus_tc, test_buf.as_mut_slice())
            .expect("error writing tc to buffer");
        assert_eq!(size, 9);
        assert_eq!(test_buf[9], 0);
        assert_eq!(test_buf[10], 0);
    }

    #[test]
    fn test_deserialization() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(size, 11);
        let tc_from_raw = PusTcReader::new(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw.packet_len(), 11);
        verify_test_tc_with_reader(&tc_from_raw, false, 11);
        assert!(tc_from_raw.user_data().is_empty());
        verify_test_tc_raw(&test_buf);
        verify_crc_no_app_data(&test_buf);
    }

    #[test]
    fn test_deserialization_alt_ctor() {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
        let mut test_buf: [u8; 32] = [0; 32];
        let mut pus_tc =
            PusTcCreatorWithReservedAppData::new(&mut test_buf, sph, tc_header, 0).unwrap();
        assert_eq!(pus_tc.len_written(), 11);
        assert_eq!(pus_tc.app_data_len(), 0);
        assert_eq!(pus_tc.app_data(), &[]);
        assert_eq!(pus_tc.app_data_mut(), &[]);
        let size = pus_tc.finalize();
        assert_eq!(size, 11);
        let tc_from_raw = PusTcReader::new(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw.packet_len(), 11);
        verify_test_tc_with_reader(&tc_from_raw, false, 11);
        assert!(tc_from_raw.user_data().is_empty());
        verify_test_tc_raw(&test_buf);
        verify_crc_no_app_data(&test_buf);
    }

    #[test]
    fn test_deserialization_no_table() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(size, 11);
        let tc_from_raw = PusTcReader::new_crc_no_table(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw.packet_len(), 11);
        verify_test_tc_with_reader(&tc_from_raw, false, 11);
        assert!(tc_from_raw.user_data().is_empty());
        verify_test_tc_raw(&test_buf);
        verify_crc_no_app_data(&test_buf);
    }

    #[test]
    fn test_writing_into_vec() {
        let pus_tc = base_ping_tc_simple_ctor();
        let tc_vec = pus_tc.to_vec().expect("Error writing TC to buffer");
        assert_eq!(tc_vec.len(), 11);
        let tc_from_raw = PusTcReader::new(tc_vec.as_slice(), None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw.packet_len(), 11);
        verify_test_tc_with_reader(&tc_from_raw, false, 11);
        assert!(tc_from_raw.user_data().is_empty());
        verify_test_tc_raw(&tc_vec);
        verify_crc_no_app_data(&tc_vec);
    }

    #[test]
    fn test_update_func() {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        let mut tc = PusTcCreator::new_simple(sph, 17, 1, &[], false);
        assert_eq!(tc.data_len(), 0);
        tc.update_ccsds_data_len();
        assert_eq!(tc.data_len(), 4);
    }

    #[test]
    fn test_deserialization_with_app_data() {
        let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(size, 14);
        let tc_from_raw = PusTcReader::new(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw.packet_len(), 14);
        verify_test_tc_with_reader(&tc_from_raw, true, 14);
        let user_data = tc_from_raw.user_data();
        assert_eq!(tc_from_raw.user_data(), tc_from_raw.app_data());
        assert_eq!(tc_from_raw.raw_data(), &test_buf[..size]);
        assert_eq!(
            tc_from_raw.checksum().unwrap(),
            u16::from_be_bytes(test_buf[size - 2..size].try_into().unwrap())
        );
        assert_eq!(user_data[0], 1);
        assert_eq!(user_data[1], 2);
        assert_eq!(user_data[2], 3);
    }

    #[test]
    fn test_reader_eq() {
        let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
        let mut test_buf: [u8; 32] = [0; 32];
        pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        let tc_from_raw_0 = PusTcReader::new(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        let tc_from_raw_1 = PusTcReader::new(&test_buf, None, 0)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_from_raw_0, tc_from_raw_1);
    }

    #[test]
    fn test_vec_ser_deser() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_vec = Vec::new();
        let size = pus_tc.append_to_vec(&mut test_vec);
        assert_eq!(size, 11);
        println!("Test vector: {test_vec:x?}");
        verify_test_tc_raw(&test_vec.as_slice());
        verify_crc_no_app_data(&test_vec.as_slice());
    }

    #[test]
    fn test_incorrect_crc() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        test_buf[9] = 0;
        test_buf[10] = 0;
        let res = PusTcReader::new(&test_buf, None, 0);
        assert!(res.is_err());
        let err = res.unwrap_err();
        if let PusError::ChecksumFailure(crc) = err {
            assert_eq!(crc, 0);
            assert_eq!(
                err.to_string(),
                "checksum verification for crc16 0x0000 failed"
            );
        } else {
            panic!("unexpected error {err}");
        }
    }

    #[test]
    fn test_manual_crc_calculation() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        pus_tc.calc_own_crc16();
        pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        verify_test_tc_raw(&test_buf);
        verify_crc_no_app_data(&test_buf);
    }

    #[test]
    fn test_with_application_data_vec() {
        let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
        verify_test_tc(&pus_tc, true, 14);
        let mut test_vec = Vec::new();
        let size = pus_tc.append_to_vec(&mut test_vec);
        assert_eq!(test_vec[9], 1);
        assert_eq!(test_vec[10], 2);
        assert_eq!(test_vec[11], 3);
        assert_eq!(size, 14);
    }

    #[test]
    fn test_write_buf_too_small() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf = [0; 10];
        let res = pus_tc.write_to_bytes(test_buf.as_mut_slice());
        assert!(res.is_err());
        let err = res.unwrap_err();
        assert_eq!(
            err,
            ByteConversionError::ToSliceTooSmall {
                found: 10,
                expected: 11
            }
        );
        assert_eq!(
            err.to_string(),
            "target slice with size 10 is too small, expected size of at least 11"
        );
    }

    #[test]
    fn test_with_application_data_buf() {
        let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
        verify_test_tc(&pus_tc, true, 14);
        let mut test_buf: [u8; 32] = [0; 32];
        let size = pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(test_buf[9], 1);
        assert_eq!(test_buf[10], 2);
        assert_eq!(test_buf[11], 3);
        assert_eq!(size, 14);
    }

    #[test]
    fn test_custom_setters() {
        let mut pus_tc = base_ping_tc_simple_ctor();
        let mut test_buf: [u8; 32] = [0; 32];
        pus_tc.set_apid(u11::new(0x7ff));
        pus_tc.set_seq_count(u14::new(0x3fff));
        pus_tc.set_ack_field(AckFlags::new_with_raw_value(u4::new(0b11)));
        let source_id = UnsignedByteFieldU16::new(0xffff).into();
        pus_tc.set_source_id(Some(source_id));
        pus_tc.set_seq_flags(SequenceFlags::Unsegmented);
        assert_eq!(pus_tc.source_id(), Some(source_id));
        assert_eq!(pus_tc.seq_count().value(), 0x3fff);
        assert_eq!(pus_tc.ack_flags().raw_value().value(), 0b11);
        assert_eq!(pus_tc.apid().value(), 0x7ff);
        assert_eq!(pus_tc.sequence_flags(), SequenceFlags::Unsegmented);
        pus_tc.calc_own_crc16();
        pus_tc
            .write_to_bytes(test_buf.as_mut_slice())
            .expect("Error writing TC to buffer");
        assert_eq!(test_buf[0], 0x1f);
        assert_eq!(test_buf[1], 0xff);
        assert_eq!(test_buf[2], 0xff);
        assert_eq!(test_buf[3], 0xff);
        assert_eq!(test_buf[6], 0x13);
        // Source ID 0
        assert_eq!(test_buf[9], 0xff);
        assert_eq!(test_buf[10], 0xff);
    }

    fn verify_test_tc(tc: &PusTcCreator, has_user_data: bool, exp_full_len: usize) {
        verify_test_tc_generic(tc);
        if !has_user_data {
            assert!(tc.user_data().is_empty());
        }
        let mut comp_header =
            SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), exp_full_len as u16 - 7);
        comp_header.set_sec_header_flag();
        assert_eq!(*tc.sp_header(), comp_header);
    }

    fn verify_test_tc_with_reader(tc: &PusTcReader, has_user_data: bool, exp_full_len: usize) {
        verify_test_tc_generic(tc);
        if !has_user_data {
            assert!(tc.user_data().is_empty());
        }
        assert_eq!(tc.len_packed(), exp_full_len);
        let mut comp_header =
            SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), exp_full_len as u16 - 7);
        comp_header.set_sec_header_flag();
        assert_eq!(*tc.sp_header(), comp_header);
    }

    fn verify_test_tc_generic(tc: &(impl PusPacket + GenericPusTcSecondaryHeader)) {
        assert_eq!(PusPacket::service_type_id(tc), 17);
        assert_eq!(GenericPusTcSecondaryHeader::service(tc), 17);
        assert_eq!(PusPacket::message_subtype_id(tc), 1);
        assert_eq!(GenericPusTcSecondaryHeader::subservice(tc), 1);
        assert!(tc.sec_header_flag());
        assert_eq!(PusPacket::pus_version(tc).unwrap(), PusVersion::PusA);
        assert_eq!(tc.seq_count().value(), 0x34);
        assert!(tc.source_id().is_none());
        assert_eq!(tc.apid().value(), 0x02);
        assert_eq!(tc.ack_flags(), ACK_ALL);
        assert_eq!(PusPacket::pus_version(tc).unwrap(), PusVersion::PusA);
        assert_eq!(
            GenericPusTcSecondaryHeader::pus_version(tc).unwrap(),
            PusVersion::PusA
        );
    }
    fn verify_test_tc_raw(slice: &impl AsRef<[u8]>) {
        // Reference comparison implementation:
        // https://github.com/us-irs/py-spacepackets/blob/v0.13.0/tests/ecss/test_pus_tc.py
        let slice = slice.as_ref();
        // 0x1801 is the generic
        assert_eq!(slice[0], 0x18);
        // APID is 0x01
        assert_eq!(slice[1], 0x02);
        // Unsegmented packets
        assert_eq!(slice[2], 0xc0);
        // Sequence count 0x34
        assert_eq!(slice[3], 0x34);
        assert_eq!(slice[4], 0x00);
        // Space data length of 4 equals total packet length of 11
        assert_eq!(slice[5], 0x04);
        // PUS Version A 0b0001 and ACK flags 0b1111
        assert_eq!(slice[6], 0x1f);
        // Service 17
        assert_eq!(slice[7], 0x11);
        // Subservice 1
        assert_eq!(slice[8], 0x01);
    }

    fn verify_crc_no_app_data(slice: &impl AsRef<[u8]>) {
        // Reference comparison implementation:
        // https://github.com/us-irs/py-spacepackets/blob/v0.13.0/tests/ecss/test_pus_tc.py
        let slice = slice.as_ref();
        assert_eq!(slice[9], 0x37);
        assert_eq!(slice[10], 0x2d);
    }

    #[test]
    fn partial_eq_pus_tc() {
        // new vs new simple
        let pus_tc_1 = base_ping_tc_simple_ctor();
        let pus_tc_2 = base_ping_tc_full_ctor();
        assert_eq!(pus_tc_1, pus_tc_2);
    }

    #[test]
    fn partial_eq_serialized_vs_derialized() {
        let pus_tc = base_ping_tc_simple_ctor();
        let mut buf = [0; 32];
        pus_tc.write_to_bytes(&mut buf).unwrap();
        assert_eq!(pus_tc, PusTcReader::new(&buf, None, 0).unwrap());
        assert_eq!(PusTcReader::new(&buf, None, 0).unwrap(), pus_tc);
    }

    #[test]
    fn test_ack_opts_from_raw() {
        let ack_opts_raw = AckOpts::Start as u8;
        let ack_opts = AckOpts::try_from(ack_opts_raw).unwrap();
        assert_eq!(ack_opts, AckOpts::Start);
    }

    #[test]
    fn test_reader_buf_too_small() {
        let app_data = &[1, 2, 3, 4];
        let pus_tc = base_ping_tc_simple_ctor_with_app_data(app_data);
        let mut buf = [0; 32];
        let written_len = pus_tc.write_to_bytes(&mut buf).unwrap();
        let error = PusTcReader::new(&buf[0..7], None, 0);
        assert!(error.is_err());
        let error = error.unwrap_err();
        if let PusError::ByteConversion(ByteConversionError::FromSliceTooSmall {
            found,
            expected,
        }) = error
        {
            assert_eq!(found, 7);
            assert_eq!(expected, written_len);
        } else {
            panic!("unexpected error {error}")
        }
    }

    #[test]
    fn test_reader_input_too_small() {
        let buf: [u8; 5] = [0; 5];
        let error = PusTcReader::new(&buf, None, 0);
        assert!(error.is_err());
        let error = error.unwrap_err();
        if let PusError::ByteConversion(ByteConversionError::FromSliceTooSmall {
            found,
            expected,
        }) = error
        {
            assert_eq!(found, 5);
            assert_eq!(expected, 6);
        } else {
            panic!("unexpected error {error}")
        }
    }

    #[test]
    fn test_with_source_id_and_spare_bytes() {
        let sph = SpHeader::new_for_unseg_tc(u11::new(0x02), u14::new(0x34), 0);
        let source_id = UnsignedByteFieldU8::new(5).into();
        let tc_header = PusTcSecondaryHeader::new(17, 1, AckFlags::ALL, Some(source_id), 2);
        let creator = PusTcCreator::new(sph, tc_header, &[1, 2, 3], true);
        assert_eq!(creator.len_written(), 17);
        let mut buf: [u8; 32] = [0; 32];
        assert_eq!(creator.write_to_bytes(&mut buf).unwrap(), 17);
        // Source ID
        assert_eq!(buf[9], 5);
        // Two spare bytes which should be 0
        assert_eq!(buf[10], 0);
        assert_eq!(buf[11], 0);
        // App data.
        assert_eq!(buf[12], 1);
        assert_eq!(buf[13], 2);
        assert_eq!(buf[14], 3);
        let tc_reader = PusTcReader::new(&buf, Some(1), 2)
            .expect("Creating PUS TC struct from raw buffer failed");
        assert_eq!(tc_reader.sp_header(), creator.sp_header());
        assert_eq!(tc_reader.app_data(), creator.app_data());
        assert_eq!(tc_reader.source_id(), Some(source_id));
        assert_eq!(creator.spare_bytes(), 2);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn test_serialization_tc_serde() {
        let pus_tc = base_ping_tc_simple_ctor();
        let output = to_allocvec(&pus_tc).unwrap();
        let output_converted_back: PusTcCreator = from_bytes(&output).unwrap();
        assert_eq!(output_converted_back, pus_tc);
    }
}