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
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
//! This module contains the implementation of the CCSDS File Delivery Protocol (CFDP) high level
//! abstractions as specified in CCSDS 727.0-B-5.
//!
//! The basic idea of CFDP is to convert files of any size into a stream of packets called packet
//! data units (PDU). CFPD has an unacknowledged and acknowledged mode, with the option to request
//! a transaction closure for the unacknowledged mode. Using the unacknowledged mode with no
//! transaction closure is applicable for simplex communication paths, while the unacknowledged
//! mode with closure is the easiest way to get a confirmation of a successful file transfer,
//! including a CRC check on the remote side to verify file integrity. The acknowledged mode is
//! the most complex mode which includes multiple mechanism to ensure succesfull packet transaction
//! even for unreliable connections, including lost segment detection. As such, it can be compared
//! to a specialized TCP for file transfers with remote systems.
//!
//! The goal of this library is to be flexible enough to support the use-cases of both on-board
//! software and of ground software. It has support to make integration on [std] systems as simple
//! as possible, but also has sufficient abstraction to allow for integration on `no_std`
//! environments. Currently, the handlers still require the [std] feature until
//! [thiserror supports `error_in_core`](https://github.com/dtolnay/thiserror/pull/304).
//! It is recommended to activate the `alloc` feature at the very least to allow using the primary
//! components provided by this crate. These components will only allocate memory at initialization
//! time and thus are still viable for systems where run-time allocation is prohibited.
//!
//! The core of this library are the [crate::dest::DestinationHandler] and the
//! [crate::source::SourceHandler] components which model the CFDP destination and source entity
//! respectively. You can find high-level and API documentation for both handlers in the respective
//! [crate::dest] and [crate::source] module.
//!
//! # Examples
//!
//! This library currently features two example application which showcase how the provided
//! components could be used to provide CFDP services.
//!
//! The [end-to-end test](https://egit.irs.uni-stuttgart.de/rust/cfdp/src/branch/main/tests/end-to-end.rs)
//! is an integration tests which spawns a CFDP source entity and a CFDP destination entity,
//! moves them to separate threads and then performs a small file copy operation.
//! You can run the integration test for a transfer with no closure and with printout to the
//! standard console by running:
//!
//! ```sh
//! cargo test end_to_end_test_no_closure -- --nocapture
//! ```
//!
//! or with closure:
//!
//! ```sh
//! cargo test end_to_end_test_with_closure -- --nocapture
//! ```
//!
//! The [Python Interoperability](https://egit.irs.uni-stuttgart.de/rust/cfdp/src/branch/main/examples/python-interop)
//! example showcases the interoperability of the CFDP handlers written in Rust with a Python
//! implementation. The dedicated example documentation shows how to run this example.
//!
//! # Notes on the user hooks and scheduling
//!
//! Both examples feature implementations of the [UserFaultHookProvider] and the [user::CfdpUser]
//! trait which simply print some information to the console to monitor the progress of a file
//! copy operation. These implementations could be adapted for other handler integrations. For
//! example, they could signal a GUI application to display some information for the user.
//!
//! Even though both examples move the newly spawned handlers to dedicated threads, this is not
//! the only way they could be scheduled. For example, to support an arbitrary (or bounded)
//! amount of file copy operations on either source or destination side, those handlers could be
//! moved into a [std::collections::HashMap] structure which is then scheduled inside a thread, or
//! you could schedule a fixed amount of handlers inside a
//! [threadpool](https://docs.rs/threadpool/latest/threadpool/).
#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;

#[cfg(feature = "std")]
pub mod dest;
#[cfg(feature = "alloc")]
pub mod filestore;
pub mod request;
#[cfg(feature = "std")]
pub mod source;
pub mod time;
pub mod user;

use crate::time::CountdownProvider;
use core::{cell::RefCell, fmt::Debug, hash::Hash};
use crc::{Crc, CRC_32_ISCSI, CRC_32_ISO_HDLC};
#[cfg(feature = "std")]
use hashbrown::HashMap;

#[cfg(feature = "alloc")]
pub use alloc_mod::*;
use core::time::Duration;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use spacepackets::{
    cfdp::{
        pdu::{FileDirectiveType, PduError, PduHeader},
        ChecksumType, ConditionCode, FaultHandlerCode, PduType, TransmissionMode,
    },
    util::{UnsignedByteField, UnsignedEnum},
};
#[cfg(feature = "std")]
pub use std_mod::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EntityType {
    Sending,
    Receiving,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TimerContext {
    CheckLimit {
        local_id: UnsignedByteField,
        remote_id: UnsignedByteField,
        entity_type: EntityType,
    },
    NakActivity {
        expiry_time: Duration,
    },
    PositiveAck {
        expiry_time: Duration,
    },
}

/// A generic trait which allows CFDP entities to create check timers which are required to
/// implement special procedures in unacknowledged transmission mode, as specified in 4.6.3.2
/// and 4.6.3.3.
///
/// This trait also allows the creation of different check timers depending on context and purpose
/// of the timer, the runtime environment (e.g. standard clock timer vs. timer using a RTC) or
/// other factors.
///
/// The countdown timer is used by 3 mechanisms of the CFDP protocol.
///
/// ## 1. Check limit handling
///
/// The first mechanism is the check limit handling for unacknowledged transfers as specified
/// in 4.6.3.2 and 4.6.3.3 of the CFDP standard.
/// For this mechanism, the timer has different functionality depending on whether
/// the using entity is the sending entity or the receiving entity for the unacknowledged
/// transmission mode.
///
/// For the sending entity, this timer determines the expiry period for declaring a check limit
/// fault after sending an EOF PDU with requested closure. This allows a timeout of the transfer.
/// Also see 4.6.3.2 of the CFDP standard.
///
/// For the receiving entity, this timer determines the expiry period for incrementing a check
/// counter after an EOF PDU is received for an incomplete file transfer. This allows out-of-order
/// reception of file data PDUs and EOF PDUs. Also see 4.6.3.3 of the CFDP standard.
///
/// ## 2. NAK activity limit
///
/// The timer will be used to perform the NAK activity check as specified in 4.6.4.7 of the CFDP
/// standard. The expiration period will be provided by the NAK timer expiration limit of the
/// remote entity configuration.
///
/// ## 3. Positive ACK procedures
///
/// The timer will be used to perform the Positive Acknowledgement Procedures as specified in
/// 4.7. 1of the CFDP standard. The expiration period will be provided by the Positive ACK timer
/// interval of the remote entity configuration.
pub trait TimerCreatorProvider {
    type Countdown: CountdownProvider;

    fn create_countdown(&self, timer_context: TimerContext) -> Self::Countdown;
}

/// This structure models the remote entity configuration information as specified in chapter 8.3
/// of the CFDP standard.

/// Some of the fields which were not considered necessary for the Rust implementation
/// were omitted. Some other fields which are not contained inside the standard but are considered
/// necessary for the Rust implementation are included.
///
/// ## Notes on Positive Acknowledgment Procedures
///
/// The `positive_ack_timer_interval_seconds` and `positive_ack_timer_expiration_limit` will
/// be used for positive acknowledgement procedures as specified in CFDP chapter 4.7. The sending
/// entity will start the timer for any PDUs where an acknowledgment is required (e.g. EOF PDU).
/// Once the expected ACK response has not been received for that interval, as counter will be
/// incremented and the timer will be reset. Once the counter exceeds the
/// `positive_ack_timer_expiration_limit`, a Positive ACK Limit Reached fault will be declared.
///
/// ## Notes on Deferred Lost Segment Procedures
///
/// This procedure will be active if an EOF (No Error) PDU is received in acknowledged mode. After
/// issuing the NAK sequence which has the whole file scope, a timer will be started. The timer is
/// reset when missing segments or missing metadata is received. The timer will be deactivated if
/// all missing data is received. If the timer expires, a new NAK sequence will be issued and a
/// counter will be incremented, which can lead to a NAK Limit Reached fault being declared.
///
/// ## Fields
///
/// * `entity_id` - The ID of the remote entity.
/// * `max_packet_len` - This determines of all PDUs generated for that remote entity in addition
///    to the `max_file_segment_len` attribute which also determines the size of file data PDUs.
/// * `max_file_segment_len` The maximum file segment length which determines the maximum size
///   of file data PDUs in addition to the `max_packet_len` attribute. If this field is set
///   to None, the maximum file segment length will be derived from the maximum packet length.
///   If this has some value which is smaller than the segment value derived from
///   `max_packet_len`, this value will be picked.
/// * `closure_requested_by_default` - If the closure requested field is not supplied as part of
///    the Put Request, it will be determined from this field in the remote configuration.
/// * `crc_on_transmission_by_default` - If the CRC option is not supplied as part of the Put
///    Request, it will be determined from this field in the remote configuration.
/// * `default_transmission_mode` - If the transmission mode is not supplied as part of the
///   Put Request, it will be determined from this field in the remote configuration.
/// * `disposition_on_cancellation` - Determines whether an incomplete received file is discard on
///   transaction cancellation. Defaults to False.
/// * `default_crc_type` - Default checksum type used to calculate for all file transmissions to
///   this remote entity.
/// * `check_limit` - This timer determines the expiry period for incrementing a check counter
///   after an EOF PDU is received for an incomplete file transfer. This allows out-of-order
///   reception of file data PDUs and EOF PDUs. Also see 4.6.3.3 of the CFDP standard. Defaults to
///   2, so the check limit timer may expire twice.
/// * `positive_ack_timer_interval_seconds`- See the notes on the Positive Acknowledgment
///    Procedures inside the class documentation. Expected as floating point seconds. Defaults to
///    10 seconds.
/// * `positive_ack_timer_expiration_limit` - See the notes on the Positive Acknowledgment
///    Procedures inside the class documentation. Defaults to 2, so the timer may expire twice.
/// * `immediate_nak_mode` - Specifies whether a NAK sequence should be issued immediately when a
///    file data gap or lost metadata is detected in the acknowledged mode. Defaults to True.
/// * `nak_timer_interval_seconds` -  See the notes on the Deferred Lost Segment Procedure inside
///    the class documentation. Expected as floating point seconds. Defaults to 10 seconds.
/// * `nak_timer_expiration_limit` - See the notes on the Deferred Lost Segment Procedure inside
///    the class documentation. Defaults to 2, so the timer may expire two times.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RemoteEntityConfig {
    pub entity_id: UnsignedByteField,
    pub max_packet_len: usize,
    pub max_file_segment_len: Option<usize>,
    pub closure_requested_by_default: bool,
    pub crc_on_transmission_by_default: bool,
    pub default_transmission_mode: TransmissionMode,
    pub default_crc_type: ChecksumType,
    pub positive_ack_timer_interval_seconds: f32,
    pub positive_ack_timer_expiration_limit: u32,
    pub check_limit: u32,
    pub disposition_on_cancellation: bool,
    pub immediate_nak_mode: bool,
    pub nak_timer_interval_seconds: f32,
    pub nak_timer_expiration_limit: u32,
}

impl RemoteEntityConfig {
    pub fn new_with_default_values(
        entity_id: UnsignedByteField,
        max_packet_len: usize,
        closure_requested_by_default: bool,
        crc_on_transmission_by_default: bool,
        default_transmission_mode: TransmissionMode,
        default_crc_type: ChecksumType,
    ) -> Self {
        Self {
            entity_id,
            max_file_segment_len: None,
            max_packet_len,
            closure_requested_by_default,
            crc_on_transmission_by_default,
            default_transmission_mode,
            default_crc_type,
            check_limit: 2,
            positive_ack_timer_interval_seconds: 10.0,
            positive_ack_timer_expiration_limit: 2,
            disposition_on_cancellation: false,
            immediate_nak_mode: true,
            nak_timer_interval_seconds: 10.0,
            nak_timer_expiration_limit: 2,
        }
    }
}

pub trait RemoteEntityConfigProvider {
    /// Retrieve the remote entity configuration for the given remote ID.
    fn get(&self, remote_id: u64) -> Option<&RemoteEntityConfig>;
    fn get_mut(&mut self, remote_id: u64) -> Option<&mut RemoteEntityConfig>;
    /// Add a new remote configuration. Return [true] if the configuration was
    /// inserted successfully, and [false] if a configuration already exists.
    fn add_config(&mut self, cfg: &RemoteEntityConfig) -> bool;
    /// Remote a configuration. Returns [true] if the configuration was removed successfully,
    /// and [false] if no configuration exists for the given remote ID.
    fn remove_config(&mut self, remote_id: u64) -> bool;
}

/// This is a thin wrapper around a [HashMap] to store remote entity configurations.
/// It implements the full [RemoteEntityConfigProvider] trait.
#[cfg(feature = "std")]
#[derive(Default, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StdRemoteEntityConfigProvider(pub HashMap<u64, RemoteEntityConfig>);

#[cfg(feature = "std")]
impl RemoteEntityConfigProvider for StdRemoteEntityConfigProvider {
    fn get(&self, remote_id: u64) -> Option<&RemoteEntityConfig> {
        self.0.get(&remote_id)
    }
    fn get_mut(&mut self, remote_id: u64) -> Option<&mut RemoteEntityConfig> {
        self.0.get_mut(&remote_id)
    }
    fn add_config(&mut self, cfg: &RemoteEntityConfig) -> bool {
        self.0.insert(cfg.entity_id.value(), *cfg).is_some()
    }
    fn remove_config(&mut self, remote_id: u64) -> bool {
        self.0.remove(&remote_id).is_some()
    }
}

/// This is a thin wrapper around a [alloc::vec::Vec] to store remote entity configurations.
/// It implements the full [RemoteEntityConfigProvider] trait.
#[cfg(feature = "alloc")]
#[derive(Default, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct VecRemoteEntityConfigProvider(pub alloc::vec::Vec<RemoteEntityConfig>);

#[cfg(feature = "alloc")]
impl RemoteEntityConfigProvider for VecRemoteEntityConfigProvider {
    fn get(&self, remote_id: u64) -> Option<&RemoteEntityConfig> {
        self.0
            .iter()
            .find(|&cfg| cfg.entity_id.value() == remote_id)
    }

    fn get_mut(&mut self, remote_id: u64) -> Option<&mut RemoteEntityConfig> {
        self.0
            .iter_mut()
            .find(|cfg| cfg.entity_id.value() == remote_id)
    }

    fn add_config(&mut self, cfg: &RemoteEntityConfig) -> bool {
        self.0.push(*cfg);
        true
    }

    fn remove_config(&mut self, remote_id: u64) -> bool {
        for (idx, cfg) in self.0.iter().enumerate() {
            if cfg.entity_id.value() == remote_id {
                self.0.remove(idx);
                return true;
            }
        }
        false
    }
}

/// A remote entity configurations also implements the [RemoteEntityConfigProvider], but the
/// [RemoteEntityConfigProvider::add_config] and [RemoteEntityConfigProvider::remove_config]
/// are no-ops and always returns [false].
impl RemoteEntityConfigProvider for RemoteEntityConfig {
    fn get(&self, remote_id: u64) -> Option<&RemoteEntityConfig> {
        if remote_id == self.entity_id.value() {
            return Some(self);
        }
        None
    }

    fn get_mut(&mut self, remote_id: u64) -> Option<&mut RemoteEntityConfig> {
        if remote_id == self.entity_id.value() {
            return Some(self);
        }
        None
    }

    fn add_config(&mut self, _cfg: &RemoteEntityConfig) -> bool {
        false
    }

    fn remove_config(&mut self, _remote_id: u64) -> bool {
        false
    }
}

/// This trait introduces some callbacks which will be called when a particular CFDP fault
/// handler is called.
///
/// It is passed into the CFDP handlers as part of the [UserFaultHookProvider] and the local entity
/// configuration and provides a way to specify custom user error handlers. This allows to
/// implement some CFDP features like fault handler logging, which would not be possible
/// generically otherwise.
///
/// For each error reported by the [FaultHandler], the appropriate fault handler callback
/// will be called depending on the [FaultHandlerCode].
pub trait UserFaultHookProvider {
    fn notice_of_suspension_cb(
        &mut self,
        transaction_id: TransactionId,
        cond: ConditionCode,
        progress: u64,
    );

    fn notice_of_cancellation_cb(
        &mut self,
        transaction_id: TransactionId,
        cond: ConditionCode,
        progress: u64,
    );

    fn abandoned_cb(&mut self, transaction_id: TransactionId, cond: ConditionCode, progress: u64);

    fn ignore_cb(&mut self, transaction_id: TransactionId, cond: ConditionCode, progress: u64);
}

/// Dummy fault hook which implements [UserFaultHookProvider] but only provides empty
/// implementations.
#[derive(Default, Debug, PartialEq, Eq, Copy, Clone)]
pub struct DummyFaultHook {}

impl UserFaultHookProvider for DummyFaultHook {
    fn notice_of_suspension_cb(
        &mut self,
        _transaction_id: TransactionId,
        _cond: ConditionCode,
        _progress: u64,
    ) {
    }

    fn notice_of_cancellation_cb(
        &mut self,
        _transaction_id: TransactionId,
        _cond: ConditionCode,
        _progress: u64,
    ) {
    }

    fn abandoned_cb(
        &mut self,
        _transaction_id: TransactionId,
        _cond: ConditionCode,
        _progress: u64,
    ) {
    }

    fn ignore_cb(&mut self, _transaction_id: TransactionId, _cond: ConditionCode, _progress: u64) {}
}

/// This structure is used to implement the fault handling as specified in chapter 4.8 of the CFDP
/// standard.
///
/// It does so by mapping each applicable [spacepackets::cfdp::ConditionCode] to a fault handler
/// which is denoted by the four [spacepackets::cfdp::FaultHandlerCode]s. This code is used
/// to select the error handling inside the CFDP handler itself in addition to dispatching to a
/// user-provided callback function provided by the [UserFaultHookProvider].
///
/// Some note on the provided default settings:
///
/// - Checksum failures will be ignored by default. This is because for unacknowledged transfers,
///   cancelling the transfer immediately would interfere with the check limit mechanism specified
///   in chapter 4.6.3.3.
/// - Unsupported checksum types will also be ignored by default. Even if the checksum type is
///   not supported the file transfer might still have worked properly.
///
/// For all other faults, the default fault handling operation will be to cancel the transaction.
/// These defaults can be overriden by using the [Self::set_fault_handler] method.
/// Please note that in any case, fault handler overrides can be specified by the sending CFDP
/// entity.
pub struct FaultHandler<UserHandler: UserFaultHookProvider> {
    handler_array: [FaultHandlerCode; 10],
    // Could also change the user fault handler trait to have non mutable methods, but that limits
    // flexbility on the user side..
    pub user_hook: RefCell<UserHandler>,
}

impl<UserHandler: UserFaultHookProvider> FaultHandler<UserHandler> {
    fn condition_code_to_array_index(conditon_code: ConditionCode) -> Option<usize> {
        Some(match conditon_code {
            ConditionCode::PositiveAckLimitReached => 0,
            ConditionCode::KeepAliveLimitReached => 1,
            ConditionCode::InvalidTransmissionMode => 2,
            ConditionCode::FilestoreRejection => 3,
            ConditionCode::FileChecksumFailure => 4,
            ConditionCode::FileSizeError => 5,
            ConditionCode::NakLimitReached => 6,
            ConditionCode::InactivityDetected => 7,
            ConditionCode::CheckLimitReached => 8,
            ConditionCode::UnsupportedChecksumType => 9,
            _ => return None,
        })
    }

    pub fn set_fault_handler(
        &mut self,
        condition_code: ConditionCode,
        fault_handler: FaultHandlerCode,
    ) {
        let array_idx = Self::condition_code_to_array_index(condition_code);
        if array_idx.is_none() {
            return;
        }
        self.handler_array[array_idx.unwrap()] = fault_handler;
    }

    pub fn new(user_fault_handler: UserHandler) -> Self {
        let mut init_array = [FaultHandlerCode::NoticeOfCancellation; 10];
        init_array
            [Self::condition_code_to_array_index(ConditionCode::FileChecksumFailure).unwrap()] =
            FaultHandlerCode::IgnoreError;
        init_array[Self::condition_code_to_array_index(ConditionCode::UnsupportedChecksumType)
            .unwrap()] = FaultHandlerCode::IgnoreError;
        Self {
            handler_array: init_array,
            user_hook: RefCell::new(user_fault_handler),
        }
    }

    pub fn get_fault_handler(&self, condition_code: ConditionCode) -> FaultHandlerCode {
        let array_idx = Self::condition_code_to_array_index(condition_code);
        if array_idx.is_none() {
            return FaultHandlerCode::IgnoreError;
        }
        self.handler_array[array_idx.unwrap()]
    }

    pub fn report_fault(
        &self,
        transaction_id: TransactionId,
        condition: ConditionCode,
        progress: u64,
    ) -> FaultHandlerCode {
        let array_idx = Self::condition_code_to_array_index(condition);
        if array_idx.is_none() {
            return FaultHandlerCode::IgnoreError;
        }
        let fh_code = self.handler_array[array_idx.unwrap()];
        let mut handler_mut = self.user_hook.borrow_mut();
        match fh_code {
            FaultHandlerCode::NoticeOfCancellation => {
                handler_mut.notice_of_cancellation_cb(transaction_id, condition, progress);
            }
            FaultHandlerCode::NoticeOfSuspension => {
                handler_mut.notice_of_suspension_cb(transaction_id, condition, progress);
            }
            FaultHandlerCode::IgnoreError => {
                handler_mut.ignore_cb(transaction_id, condition, progress);
            }
            FaultHandlerCode::AbandonTransaction => {
                handler_mut.abandoned_cb(transaction_id, condition, progress);
            }
        }
        fh_code
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IndicationConfig {
    pub eof_sent: bool,
    pub eof_recv: bool,
    pub file_segment_recv: bool,
    pub transaction_finished: bool,
    pub suspended: bool,
    pub resumed: bool,
}

impl Default for IndicationConfig {
    fn default() -> Self {
        Self {
            eof_sent: true,
            eof_recv: true,
            file_segment_recv: true,
            transaction_finished: true,
            suspended: true,
            resumed: true,
        }
    }
}

/// Each CFDP entity handler has a [LocalEntityConfig]uration.
pub struct LocalEntityConfig<UserFaultHook: UserFaultHookProvider> {
    pub id: UnsignedByteField,
    pub indication_cfg: IndicationConfig,
    pub fault_handler: FaultHandler<UserFaultHook>,
}

impl<UserFaultHook: UserFaultHookProvider> LocalEntityConfig<UserFaultHook> {
    pub fn new(
        id: UnsignedByteField,
        indication_cfg: IndicationConfig,
        hook: UserFaultHook,
    ) -> Self {
        Self {
            id,
            indication_cfg,
            fault_handler: FaultHandler::new(hook),
        }
    }
}

impl<UserFaultHook: UserFaultHookProvider> LocalEntityConfig<UserFaultHook> {
    pub fn user_fault_hook_mut(&mut self) -> &mut RefCell<UserFaultHook> {
        &mut self.fault_handler.user_hook
    }

    pub fn user_fault_hook(&self) -> &RefCell<UserFaultHook> {
        &self.fault_handler.user_hook
    }
}

/// Generic error type for sending a PDU via a message queue.
#[cfg(feature = "std")]
#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum GenericSendError {
    #[error("RX disconnected")]
    RxDisconnected,
    #[error("queue is full, fill count {0:?}")]
    QueueFull(Option<u32>),
    #[error("other send error")]
    Other,
}

#[cfg(feature = "std")]
pub trait PduSendProvider {
    fn send_pdu(
        &self,
        pdu_type: PduType,
        file_directive_type: Option<FileDirectiveType>,
        raw_pdu: &[u8],
    ) -> Result<(), GenericSendError>;
}

#[cfg(feature = "std")]
pub mod std_mod {
    use std::sync::mpsc;

    use super::*;

    impl PduSendProvider for mpsc::Sender<PduOwnedWithInfo> {
        fn send_pdu(
            &self,
            pdu_type: PduType,
            file_directive_type: Option<FileDirectiveType>,
            raw_pdu: &[u8],
        ) -> Result<(), GenericSendError> {
            self.send(PduOwnedWithInfo::new(
                pdu_type,
                file_directive_type,
                raw_pdu.to_vec(),
            ))
            .map_err(|_| GenericSendError::RxDisconnected)?;
            Ok(())
        }
    }

    /// Simple implementation of the [CountdownProvider] trait assuming a standard runtime.
    #[derive(Debug)]
    pub struct StdCountdown {
        expiry_time: Duration,
        start_time: std::time::Instant,
    }

    impl StdCountdown {
        pub fn new(expiry_time: Duration) -> Self {
            Self {
                expiry_time,
                start_time: std::time::Instant::now(),
            }
        }

        pub fn expiry_time_seconds(&self) -> u64 {
            self.expiry_time.as_secs()
        }
    }

    impl CountdownProvider for StdCountdown {
        fn has_expired(&self) -> bool {
            if self.start_time.elapsed() > self.expiry_time {
                return true;
            }
            false
        }

        fn reset(&mut self) {
            self.start_time = std::time::Instant::now();
        }
    }

    pub struct StdTimerCreator {
        pub check_limit_timeout: Duration,
    }

    impl StdTimerCreator {
        pub const fn new(check_limit_timeout: Duration) -> Self {
            Self {
                check_limit_timeout,
            }
        }
    }

    impl Default for StdTimerCreator {
        fn default() -> Self {
            Self::new(Duration::from_secs(5))
        }
    }

    impl TimerCreatorProvider for StdTimerCreator {
        type Countdown = StdCountdown;

        fn create_countdown(&self, timer_context: TimerContext) -> Self::Countdown {
            match timer_context {
                TimerContext::CheckLimit {
                    local_id: _,
                    remote_id: _,
                    entity_type: _,
                } => StdCountdown::new(self.check_limit_timeout),
                TimerContext::NakActivity { expiry_time } => StdCountdown::new(expiry_time),
                TimerContext::PositiveAck { expiry_time } => StdCountdown::new(expiry_time),
            }
        }
    }
}

/// The CFDP transaction ID of a CFDP transaction consists of the source entity ID and the sequence
/// number of that transfer which is also determined by the CFDP source entity.
#[derive(Debug, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TransactionId {
    source_id: UnsignedByteField,
    seq_num: UnsignedByteField,
}

impl TransactionId {
    pub fn new(source_id: UnsignedByteField, seq_num: UnsignedByteField) -> Self {
        Self { source_id, seq_num }
    }

    pub fn source_id(&self) -> &UnsignedByteField {
        &self.source_id
    }

    pub fn seq_num(&self) -> &UnsignedByteField {
        &self.seq_num
    }
}

impl Hash for TransactionId {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.source_id.value().hash(state);
        self.seq_num.value().hash(state);
    }
}

impl PartialEq for TransactionId {
    fn eq(&self, other: &Self) -> bool {
        self.source_id.value() == other.source_id.value()
            && self.seq_num.value() == other.seq_num.value()
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum State {
    Idle = 0,
    Busy = 1,
    Suspended = 2,
}

/// [crc::Crc] instance using [crc::CRC_32_ISO_HDLC].
///
/// SANA registry entry: <https://sanaregistry.org/r/checksum_identifiers/records/4>,
/// Entry in CRC catalogue: <https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32>
pub const CRC_32: Crc<u32> = Crc::<u32>::new(&CRC_32_ISO_HDLC);
/// [crc::Crc] instance using [crc::CRC_32_ISCSI].
///
/// SANA registry entry: <https://sanaregistry.org/r/checksum_identifiers/records/3>,
/// Entry in CRC catalogue: <https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iscsi>
pub const CRC_32C: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PacketTarget {
    SourceEntity,
    DestEntity,
}

/// Generic trait which models a raw CFDP packet data unit (PDU) block with some additional context
/// information.
pub trait PduProvider {
    fn pdu_type(&self) -> PduType;
    fn file_directive_type(&self) -> Option<FileDirectiveType>;
    fn pdu(&self) -> &[u8];
    fn packet_target(&self) -> Result<PacketTarget, PduError>;
}

pub struct DummyPduProvider(());

impl PduProvider for DummyPduProvider {
    fn pdu_type(&self) -> PduType {
        PduType::FileData
    }

    fn file_directive_type(&self) -> Option<FileDirectiveType> {
        None
    }

    fn pdu(&self) -> &[u8] {
        &[]
    }

    fn packet_target(&self) -> Result<PacketTarget, PduError> {
        Ok(PacketTarget::SourceEntity)
    }
}

/// This is a helper struct which contains base information about a particular PDU packet.
/// This is also necessary information for CFDP packet routing. For example, some packet types
/// like file data PDUs can only be used by CFDP source entities.
pub struct PduRawWithInfo<'raw_packet> {
    pdu_type: PduType,
    file_directive_type: Option<FileDirectiveType>,
    packet_len: usize,
    raw_packet: &'raw_packet [u8],
}

pub fn determine_packet_target(raw_pdu: &[u8]) -> Result<PacketTarget, PduError> {
    let (header, header_len) = PduHeader::from_bytes(raw_pdu)?;
    if header.pdu_type() == PduType::FileData {
        return Ok(PacketTarget::DestEntity);
    }
    let file_directive_type = FileDirectiveType::try_from(raw_pdu[header_len]).map_err(|_| {
        PduError::InvalidDirectiveType {
            found: raw_pdu[header_len],
            expected: None,
        }
    })?;
    let packet_target =
        match file_directive_type {
            // Section c) of 4.5.3: These PDUs should always be targeted towards the file sender a.k.a.
            // the source handler
            FileDirectiveType::NakPdu
            | FileDirectiveType::FinishedPdu
            | FileDirectiveType::KeepAlivePdu => PacketTarget::SourceEntity,
            // Section b) of 4.5.3: These PDUs should always be targeted towards the file receiver a.k.a.
            // the destination handler
            FileDirectiveType::MetadataPdu
            | FileDirectiveType::EofPdu
            | FileDirectiveType::PromptPdu => PacketTarget::DestEntity,
            // Section a): Recipient depends of the type of PDU that is being acknowledged. We can simply
            // extract the PDU type from the raw stream. If it is an EOF PDU, this packet is passed to
            // the source handler, for a Finished PDU, it is passed to the destination handler.
            FileDirectiveType::AckPdu => {
                let acked_directive = FileDirectiveType::try_from(raw_pdu[header_len + 1])
                    .map_err(|_| PduError::InvalidDirectiveType {
                        found: raw_pdu[header_len],
                        expected: None,
                    })?;
                if acked_directive == FileDirectiveType::EofPdu {
                    PacketTarget::SourceEntity
                } else if acked_directive == FileDirectiveType::FinishedPdu {
                    PacketTarget::DestEntity
                } else {
                    // TODO: Maybe a better error? This might be confusing..
                    return Err(PduError::InvalidDirectiveType {
                        found: raw_pdu[header_len + 1],
                        expected: None,
                    });
                }
            }
        };
    Ok(packet_target)
}

impl<'raw> PduRawWithInfo<'raw> {
    pub fn new(raw_packet: &'raw [u8]) -> Result<Self, PduError> {
        let (pdu_header, header_len) = PduHeader::from_bytes(raw_packet)?;
        if pdu_header.pdu_type() == PduType::FileData {
            return Ok(Self {
                pdu_type: pdu_header.pdu_type(),
                file_directive_type: None,
                packet_len: pdu_header.pdu_len(),
                raw_packet,
            });
        }
        if pdu_header.pdu_datafield_len() < 1 {
            return Err(PduError::FormatError);
        }
        // Route depending on PDU type and directive type if applicable. Retrieve directive type
        // from the raw stream for better performance (with sanity and directive code check).
        // The routing is based on section 4.5 of the CFDP standard which specifies the PDU forwarding
        // procedure.
        let directive = FileDirectiveType::try_from(raw_packet[header_len]).map_err(|_| {
            PduError::InvalidDirectiveType {
                found: raw_packet[header_len],
                expected: None,
            }
        })?;
        Ok(Self {
            pdu_type: pdu_header.pdu_type(),
            file_directive_type: Some(directive),
            packet_len: pdu_header.pdu_len(),
            raw_packet,
        })
    }

    pub fn raw_packet(&self) -> &[u8] {
        &self.raw_packet[0..self.packet_len]
    }
}

impl PduProvider for PduRawWithInfo<'_> {
    fn pdu_type(&self) -> PduType {
        self.pdu_type
    }

    fn file_directive_type(&self) -> Option<FileDirectiveType> {
        self.file_directive_type
    }

    fn pdu(&self) -> &[u8] {
        self.raw_packet
    }

    fn packet_target(&self) -> Result<PacketTarget, PduError> {
        determine_packet_target(self.raw_packet)
    }
}

#[cfg(feature = "alloc")]
pub mod alloc_mod {
    use spacepackets::cfdp::{
        pdu::{FileDirectiveType, PduError},
        PduType,
    };

    use crate::{determine_packet_target, PacketTarget, PduProvider, PduRawWithInfo};

    #[derive(Debug, PartialEq, Eq, Clone)]
    pub struct PduOwnedWithInfo {
        pub pdu_type: PduType,
        pub file_directive_type: Option<FileDirectiveType>,
        pub pdu: alloc::vec::Vec<u8>,
    }

    impl PduOwnedWithInfo {
        pub fn new_from_raw_packet(raw_packet: &[u8]) -> Result<Self, PduError> {
            Ok(PduRawWithInfo::new(raw_packet)?.into())
        }

        pub fn new(
            pdu_type: PduType,
            file_directive_type: Option<FileDirectiveType>,
            pdu: alloc::vec::Vec<u8>,
        ) -> Self {
            Self {
                pdu_type,
                file_directive_type,
                pdu,
            }
        }
    }

    impl From<PduRawWithInfo<'_>> for PduOwnedWithInfo {
        fn from(value: PduRawWithInfo) -> Self {
            Self::new(
                value.pdu_type(),
                value.file_directive_type(),
                value.raw_packet().to_vec(),
            )
        }
    }

    impl PduProvider for PduOwnedWithInfo {
        fn pdu_type(&self) -> PduType {
            self.pdu_type
        }

        fn file_directive_type(&self) -> Option<FileDirectiveType> {
            self.file_directive_type
        }

        fn pdu(&self) -> &[u8] {
            &self.pdu
        }

        fn packet_target(&self) -> Result<PacketTarget, PduError> {
            determine_packet_target(&self.pdu)
        }
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use core::cell::RefCell;

    use alloc::{collections::VecDeque, string::String, vec::Vec};
    use spacepackets::{
        cfdp::{
            lv::Lv,
            pdu::{
                eof::EofPdu,
                file_data::FileDataPdu,
                metadata::{MetadataGenericParams, MetadataPduCreator},
                CommonPduConfig, FileDirectiveType, PduHeader, WritablePduPacket,
            },
            ChecksumType, ConditionCode, PduType, TransmissionMode,
        },
        util::{UnsignedByteField, UnsignedByteFieldU16, UnsignedByteFieldU8, UnsignedEnum},
    };
    use user::{CfdpUser, OwnedMetadataRecvdParams, TransactionFinishedParams};

    use crate::{PacketTarget, StdCountdown};

    use super::*;

    pub const LOCAL_ID: UnsignedByteFieldU16 = UnsignedByteFieldU16::new(1);
    pub const REMOTE_ID: UnsignedByteFieldU16 = UnsignedByteFieldU16::new(2);

    pub struct FileSegmentRecvdParamsNoSegMetadata {
        #[allow(dead_code)]
        pub id: TransactionId,
        pub offset: u64,
        pub length: usize,
    }

    #[derive(Default)]
    pub struct TestCfdpUser {
        pub next_expected_seq_num: u64,
        pub expected_full_src_name: String,
        pub expected_full_dest_name: String,
        pub expected_file_size: u64,
        pub transaction_indication_call_count: u32,
        pub eof_sent_call_count: u32,
        pub eof_recvd_call_count: u32,
        pub finished_indic_queue: VecDeque<TransactionFinishedParams>,
        pub metadata_recv_queue: VecDeque<OwnedMetadataRecvdParams>,
        pub file_seg_recvd_queue: VecDeque<FileSegmentRecvdParamsNoSegMetadata>,
    }

    impl TestCfdpUser {
        pub fn new(
            next_expected_seq_num: u64,
            expected_full_src_name: String,
            expected_full_dest_name: String,
            expected_file_size: u64,
        ) -> Self {
            Self {
                next_expected_seq_num,
                expected_full_src_name,
                expected_full_dest_name,
                expected_file_size,
                transaction_indication_call_count: 0,
                eof_recvd_call_count: 0,
                eof_sent_call_count: 0,
                finished_indic_queue: VecDeque::new(),
                metadata_recv_queue: VecDeque::new(),
                file_seg_recvd_queue: VecDeque::new(),
            }
        }

        pub fn generic_id_check(&self, id: &crate::TransactionId) {
            assert_eq!(id.source_id, LOCAL_ID.into());
            assert_eq!(id.seq_num().value(), self.next_expected_seq_num);
        }
    }

    impl CfdpUser for TestCfdpUser {
        fn transaction_indication(&mut self, id: &crate::TransactionId) {
            self.generic_id_check(id);
            self.transaction_indication_call_count += 1;
        }

        fn eof_sent_indication(&mut self, id: &crate::TransactionId) {
            self.generic_id_check(id);
            self.eof_sent_call_count += 1;
        }

        fn transaction_finished_indication(
            &mut self,
            finished_params: &crate::user::TransactionFinishedParams,
        ) {
            self.generic_id_check(&finished_params.id);
            self.finished_indic_queue.push_back(*finished_params);
        }

        fn metadata_recvd_indication(
            &mut self,
            md_recvd_params: &crate::user::MetadataReceivedParams,
        ) {
            self.generic_id_check(&md_recvd_params.id);
            assert_eq!(
                String::from(md_recvd_params.src_file_name),
                self.expected_full_src_name
            );
            assert_eq!(
                String::from(md_recvd_params.dest_file_name),
                self.expected_full_dest_name
            );
            assert_eq!(md_recvd_params.msgs_to_user.len(), 0);
            assert_eq!(md_recvd_params.source_id, LOCAL_ID.into());
            assert_eq!(md_recvd_params.file_size, self.expected_file_size);
            self.metadata_recv_queue.push_back(md_recvd_params.into());
        }

        fn file_segment_recvd_indication(
            &mut self,
            segment_recvd_params: &crate::user::FileSegmentRecvdParams,
        ) {
            self.generic_id_check(&segment_recvd_params.id);
            self.file_seg_recvd_queue
                .push_back(FileSegmentRecvdParamsNoSegMetadata {
                    id: segment_recvd_params.id,
                    offset: segment_recvd_params.offset,
                    length: segment_recvd_params.length,
                })
        }

        fn report_indication(&mut self, _id: &crate::TransactionId) {}

        fn suspended_indication(
            &mut self,
            _id: &crate::TransactionId,
            _condition_code: ConditionCode,
        ) {
            panic!("unexpected suspended indication");
        }

        fn resumed_indication(&mut self, _id: &crate::TransactionId, _progresss: u64) {}

        fn fault_indication(
            &mut self,
            _id: &crate::TransactionId,
            _condition_code: ConditionCode,
            _progress: u64,
        ) {
            panic!("unexpected fault indication");
        }

        fn abandoned_indication(
            &mut self,
            _id: &crate::TransactionId,
            _condition_code: ConditionCode,
            _progress: u64,
        ) {
            panic!("unexpected abandoned indication");
        }

        fn eof_recvd_indication(&mut self, id: &crate::TransactionId) {
            self.generic_id_check(id);
            self.eof_recvd_call_count += 1;
        }
    }

    #[derive(Default, Debug)]
    pub(crate) struct TestFaultHandler {
        pub notice_of_suspension_queue: VecDeque<(TransactionId, ConditionCode, u64)>,
        pub notice_of_cancellation_queue: VecDeque<(TransactionId, ConditionCode, u64)>,
        pub abandoned_queue: VecDeque<(TransactionId, ConditionCode, u64)>,
        pub ignored_queue: VecDeque<(TransactionId, ConditionCode, u64)>,
    }

    impl UserFaultHookProvider for TestFaultHandler {
        fn notice_of_suspension_cb(
            &mut self,
            transaction_id: TransactionId,
            cond: ConditionCode,
            progress: u64,
        ) {
            self.notice_of_suspension_queue
                .push_back((transaction_id, cond, progress))
        }

        fn notice_of_cancellation_cb(
            &mut self,
            transaction_id: TransactionId,
            cond: ConditionCode,
            progress: u64,
        ) {
            self.notice_of_cancellation_queue
                .push_back((transaction_id, cond, progress))
        }

        fn abandoned_cb(
            &mut self,
            transaction_id: TransactionId,
            cond: ConditionCode,
            progress: u64,
        ) {
            self.abandoned_queue
                .push_back((transaction_id, cond, progress))
        }

        fn ignore_cb(&mut self, transaction_id: TransactionId, cond: ConditionCode, progress: u64) {
            self.ignored_queue
                .push_back((transaction_id, cond, progress))
        }
    }

    impl TestFaultHandler {
        pub(crate) fn suspension_queue_empty(&self) -> bool {
            self.notice_of_suspension_queue.is_empty()
        }
        pub(crate) fn cancellation_queue_empty(&self) -> bool {
            self.notice_of_cancellation_queue.is_empty()
        }
        pub(crate) fn ignored_queue_empty(&self) -> bool {
            self.ignored_queue.is_empty()
        }
        pub(crate) fn abandoned_queue_empty(&self) -> bool {
            self.abandoned_queue.is_empty()
        }
        pub(crate) fn all_queues_empty(&self) -> bool {
            self.suspension_queue_empty()
                && self.cancellation_queue_empty()
                && self.ignored_queue_empty()
                && self.abandoned_queue_empty()
        }
    }

    pub struct SentPdu {
        pub pdu_type: PduType,
        pub file_directive_type: Option<FileDirectiveType>,
        pub raw_pdu: Vec<u8>,
    }

    #[derive(Default)]
    pub struct TestCfdpSender {
        pub packet_queue: RefCell<VecDeque<SentPdu>>,
    }

    impl PduSendProvider for TestCfdpSender {
        fn send_pdu(
            &self,
            pdu_type: PduType,
            file_directive_type: Option<FileDirectiveType>,
            raw_pdu: &[u8],
        ) -> Result<(), GenericSendError> {
            self.packet_queue.borrow_mut().push_back(SentPdu {
                pdu_type,
                file_directive_type,
                raw_pdu: raw_pdu.to_vec(),
            });
            Ok(())
        }
    }

    impl TestCfdpSender {
        pub fn retrieve_next_pdu(&self) -> Option<SentPdu> {
            self.packet_queue.borrow_mut().pop_front()
        }
        pub fn queue_empty(&self) -> bool {
            self.packet_queue.borrow_mut().is_empty()
        }
    }

    pub fn basic_remote_cfg_table(
        dest_id: impl Into<UnsignedByteField>,
        max_packet_len: usize,
        crc_on_transmission_by_default: bool,
    ) -> StdRemoteEntityConfigProvider {
        let mut table = StdRemoteEntityConfigProvider::default();
        let remote_entity_cfg = RemoteEntityConfig::new_with_default_values(
            dest_id.into(),
            max_packet_len,
            true,
            crc_on_transmission_by_default,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        table.add_config(&remote_entity_cfg);
        table
    }

    fn generic_pdu_header() -> PduHeader {
        let pdu_conf = CommonPduConfig::default();
        PduHeader::new_no_file_data(pdu_conf, 0)
    }

    #[test]
    fn test_transaction_id() {
        let transaction_id = TransactionId::new(
            UnsignedByteFieldU16::new(1).into(),
            UnsignedByteFieldU16::new(2).into(),
        );
        assert_eq!(transaction_id.source_id().value(), 1);
        assert_eq!(transaction_id.seq_num().value(), 2);
    }

    #[test]
    fn test_metadata_pdu_info() {
        let mut buf: [u8; 128] = [0; 128];
        let pdu_header = generic_pdu_header();
        let metadata_params = MetadataGenericParams::default();
        let src_file_name = "hello.txt";
        let dest_file_name = "hello-dest.txt";
        let src_lv = Lv::new_from_str(src_file_name).unwrap();
        let dest_lv = Lv::new_from_str(dest_file_name).unwrap();
        let metadata_pdu =
            MetadataPduCreator::new_no_opts(pdu_header, metadata_params, src_lv, dest_lv);
        metadata_pdu
            .write_to_bytes(&mut buf)
            .expect("writing metadata PDU failed");

        let packet_info = PduRawWithInfo::new(&buf).expect("creating packet info failed");
        assert_eq!(packet_info.pdu_type(), PduType::FileDirective);
        assert!(packet_info.file_directive_type().is_some());
        assert_eq!(
            packet_info.file_directive_type().unwrap(),
            FileDirectiveType::MetadataPdu
        );
        assert_eq!(
            packet_info.raw_packet(),
            &buf[0..metadata_pdu.len_written()]
        );
        assert_eq!(
            packet_info.packet_target().unwrap(),
            PacketTarget::DestEntity
        );
    }

    #[test]
    fn test_filedata_pdu_info() {
        let mut buf: [u8; 128] = [0; 128];
        let pdu_header = generic_pdu_header();
        let file_data_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 0, &[]);
        file_data_pdu
            .write_to_bytes(&mut buf)
            .expect("writing file data PDU failed");
        let packet_info = PduRawWithInfo::new(&buf).expect("creating packet info failed");
        assert_eq!(
            packet_info.raw_packet(),
            &buf[0..file_data_pdu.len_written()]
        );
        assert_eq!(packet_info.pdu_type(), PduType::FileData);
        assert!(packet_info.file_directive_type().is_none());
        assert_eq!(
            packet_info.packet_target().unwrap(),
            PacketTarget::DestEntity
        );
    }

    #[test]
    fn test_eof_pdu_info() {
        let mut buf: [u8; 128] = [0; 128];
        let pdu_header = generic_pdu_header();
        let eof_pdu = EofPdu::new_no_error(pdu_header, 0, 0);
        eof_pdu
            .write_to_bytes(&mut buf)
            .expect("writing file data PDU failed");
        let packet_info = PduRawWithInfo::new(&buf).expect("creating packet info failed");
        assert_eq!(packet_info.pdu_type(), PduType::FileDirective);
        assert!(packet_info.file_directive_type().is_some());
        assert_eq!(packet_info.raw_packet(), &buf[0..eof_pdu.len_written()]);
        assert_eq!(
            packet_info.file_directive_type().unwrap(),
            FileDirectiveType::EofPdu
        );
    }

    #[test]
    fn test_std_check_timer() {
        let mut std_check_timer = StdCountdown::new(Duration::from_secs(1));
        assert!(!std_check_timer.has_expired());
        assert_eq!(std_check_timer.expiry_time_seconds(), 1);
        std::thread::sleep(Duration::from_millis(800));
        assert!(!std_check_timer.has_expired());
        std::thread::sleep(Duration::from_millis(205));
        assert!(std_check_timer.has_expired());
        std_check_timer.reset();
        assert!(!std_check_timer.has_expired());
    }

    #[test]
    fn test_std_check_timer_creator() {
        let std_check_timer_creator = StdTimerCreator::new(Duration::from_secs(1));
        let check_timer = std_check_timer_creator.create_countdown(TimerContext::NakActivity {
            expiry_time: Duration::from_secs(1),
        });
        assert_eq!(check_timer.expiry_time_seconds(), 1);
    }

    #[test]
    fn test_remote_cfg_provider_single() {
        let mut remote_entity_cfg = RemoteEntityConfig::new_with_default_values(
            REMOTE_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        let remote_entity_retrieved = remote_entity_cfg.get(REMOTE_ID.value()).unwrap();
        assert_eq!(remote_entity_retrieved.entity_id, REMOTE_ID.into());
        assert_eq!(remote_entity_retrieved.max_packet_len, 1024);
        assert!(remote_entity_retrieved.closure_requested_by_default);
        assert!(!remote_entity_retrieved.crc_on_transmission_by_default);
        assert_eq!(
            remote_entity_retrieved.default_crc_type,
            ChecksumType::Crc32
        );
        let remote_entity_mut = remote_entity_cfg.get_mut(REMOTE_ID.value()).unwrap();
        assert_eq!(remote_entity_mut.entity_id, REMOTE_ID.into());
        let dummy = RemoteEntityConfig::new_with_default_values(
            LOCAL_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        assert!(!remote_entity_cfg.add_config(&dummy));
        // Removal is no-op.
        assert!(!remote_entity_cfg.remove_config(REMOTE_ID.value()));
        let remote_entity_retrieved = remote_entity_cfg.get(REMOTE_ID.value()).unwrap();
        assert_eq!(remote_entity_retrieved.entity_id, REMOTE_ID.into());
        // Does not exist.
        assert!(remote_entity_cfg.get(LOCAL_ID.value()).is_none());
        assert!(remote_entity_cfg.get_mut(LOCAL_ID.value()).is_none());
    }

    #[test]
    fn test_remote_cfg_provider_std() {
        let remote_entity_cfg = RemoteEntityConfig::new_with_default_values(
            REMOTE_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        let mut remote_cfg_provider = StdRemoteEntityConfigProvider::default();
        assert!(remote_cfg_provider.0.is_empty());
        remote_cfg_provider.add_config(&remote_entity_cfg);
        assert_eq!(remote_cfg_provider.0.len(), 1);
        let remote_entity_cfg_2 = RemoteEntityConfig::new_with_default_values(
            LOCAL_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        let cfg_0 = remote_cfg_provider.get(REMOTE_ID.value()).unwrap();
        assert_eq!(cfg_0.entity_id, REMOTE_ID.into());
        remote_cfg_provider.add_config(&remote_entity_cfg_2);
        assert_eq!(remote_cfg_provider.0.len(), 2);
        let cfg_1 = remote_cfg_provider.get(LOCAL_ID.value()).unwrap();
        assert_eq!(cfg_1.entity_id, LOCAL_ID.into());
        assert!(remote_cfg_provider.remove_config(REMOTE_ID.value()));
        assert_eq!(remote_cfg_provider.0.len(), 1);
        let cfg_1_mut = remote_cfg_provider.get_mut(LOCAL_ID.value()).unwrap();
        cfg_1_mut.default_crc_type = ChecksumType::Crc32C;
        assert!(!remote_cfg_provider.remove_config(REMOTE_ID.value()));
        assert!(remote_cfg_provider.get_mut(REMOTE_ID.value()).is_none());
    }

    #[test]
    fn test_remote_cfg_provider_vector() {
        let mut remote_cfg_provider = VecRemoteEntityConfigProvider::default();
        let remote_entity_cfg = RemoteEntityConfig::new_with_default_values(
            REMOTE_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        assert!(remote_cfg_provider.0.is_empty());
        remote_cfg_provider.add_config(&remote_entity_cfg);
        assert_eq!(remote_cfg_provider.0.len(), 1);
        let remote_entity_cfg_2 = RemoteEntityConfig::new_with_default_values(
            LOCAL_ID.into(),
            1024,
            true,
            false,
            TransmissionMode::Unacknowledged,
            ChecksumType::Crc32,
        );
        let cfg_0 = remote_cfg_provider.get(REMOTE_ID.value()).unwrap();
        assert_eq!(cfg_0.entity_id, REMOTE_ID.into());
        remote_cfg_provider.add_config(&remote_entity_cfg_2);
        assert_eq!(remote_cfg_provider.0.len(), 2);
        let cfg_1 = remote_cfg_provider.get(LOCAL_ID.value()).unwrap();
        assert_eq!(cfg_1.entity_id, LOCAL_ID.into());
        assert!(remote_cfg_provider.remove_config(REMOTE_ID.value()));
        assert_eq!(remote_cfg_provider.0.len(), 1);
        let cfg_1_mut = remote_cfg_provider.get_mut(LOCAL_ID.value()).unwrap();
        cfg_1_mut.default_crc_type = ChecksumType::Crc32C;
        assert!(!remote_cfg_provider.remove_config(REMOTE_ID.value()));
        assert!(remote_cfg_provider.get_mut(REMOTE_ID.value()).is_none());
    }

    #[test]
    fn dummy_fault_hook_test() {
        let mut user_hook_dummy = DummyFaultHook::default();
        let transaction_id = TransactionId::new(
            UnsignedByteFieldU8::new(0).into(),
            UnsignedByteFieldU8::new(0).into(),
        );
        user_hook_dummy.notice_of_cancellation_cb(transaction_id, ConditionCode::NoError, 0);
        user_hook_dummy.notice_of_suspension_cb(transaction_id, ConditionCode::NoError, 0);
        user_hook_dummy.abandoned_cb(transaction_id, ConditionCode::NoError, 0);
        user_hook_dummy.ignore_cb(transaction_id, ConditionCode::NoError, 0);
    }

    #[test]
    fn dummy_pdu_provider_test() {
        let dummy_pdu_provider = DummyPduProvider(());
        assert_eq!(dummy_pdu_provider.pdu_type(), PduType::FileData);
        assert!(dummy_pdu_provider.file_directive_type().is_none());
        assert_eq!(dummy_pdu_provider.pdu(), &[]);
        assert_eq!(
            dummy_pdu_provider.packet_target(),
            Ok(PacketTarget::SourceEntity)
        );
    }

    #[test]
    fn test_fault_handler_checksum_error_ignored_by_default() {
        let fault_handler = FaultHandler::new(TestFaultHandler::default());
        assert_eq!(
            fault_handler.get_fault_handler(ConditionCode::FileChecksumFailure),
            FaultHandlerCode::IgnoreError
        );
    }

    #[test]
    fn test_fault_handler_unsupported_checksum_ignored_by_default() {
        let fault_handler = FaultHandler::new(TestFaultHandler::default());
        assert_eq!(
            fault_handler.get_fault_handler(ConditionCode::UnsupportedChecksumType),
            FaultHandlerCode::IgnoreError
        );
    }

    #[test]
    fn test_fault_handler_basic() {
        let mut fault_handler = FaultHandler::new(TestFaultHandler::default());
        assert_eq!(
            fault_handler.get_fault_handler(ConditionCode::FileChecksumFailure),
            FaultHandlerCode::IgnoreError
        );
        fault_handler.set_fault_handler(
            ConditionCode::FileChecksumFailure,
            FaultHandlerCode::NoticeOfCancellation,
        );
        assert_eq!(
            fault_handler.get_fault_handler(ConditionCode::FileChecksumFailure),
            FaultHandlerCode::NoticeOfCancellation
        );
    }

    #[test]
    fn transaction_id_hashable_usable_as_map_key() {
        let mut map = HashMap::new();
        let transaction_id_0 = TransactionId::new(
            UnsignedByteFieldU8::new(1).into(),
            UnsignedByteFieldU8::new(2).into(),
        );
        map.insert(transaction_id_0, 5_u32);
    }
}