-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcf_cfdp_r.c
1206 lines (1103 loc) · 45.5 KB
/
cf_cfdp_r.c
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
/************************************************************************
** File: cf_cfdp_r.c
**
** NASA Docket No. GSC-18,447-1, and identified as “CFS CFDP (CF)
** Application version 3.0.0”
** Copyright © 2019 United States Government as represented by the
** Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
** Licensed under the Apache License, Version 2.0 (the "License"); you may
** not use this file except in compliance with the License. You may obtain
** a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
**
** Purpose:
** The CF Application CFDP receive logic source file
**
** Handles all CFDP engine functionality specific to RX transactions.
**
**
**
*************************************************************************/
#include "cfe.h"
#include "cf_verify.h"
#include "cf_app.h"
#include "cf_events.h"
#include "cf_perfids.h"
#include "cf_cfdp.h"
#include "cf_cfdp_helpers.h"
#include "cf_utils.h"
#include <stdio.h>
#include <string.h>
#include "cf_assert.h"
typedef struct
{
CF_Transaction_t *t;
CF_CFDP_PduHeader_t *ph;
uint32 gap_counter;
} gap_compute_args_t;
/**
* @brief A dispatch table for receive file transactions, recieve side
*
* This is used for "receive file" transactions upon receipt of a directive PDU.
* Depending on the sub-state of the transaction, a different action may be taken.
*/
typedef struct
{
const CF_CFDP_FileDirectiveDispatchTable_t *state[CF_RxSubState_NUM_STATES];
} CF_CFDP_R_SubstateDispatchTable_t;
/************************************************************************/
/** \brief Helper function to store condition code set send_fin flag.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_SetCc(CF_Transaction_t *t, CF_CFDP_ConditionCode_t cc)
{
t->history->cc = cc;
t->flags.rx.send_fin = 1;
}
/************************************************************************/
/** \brief CFDP R1 transaction reset function.
**
** \par Description
** All R transactions use this call to indicate the transaction
** state can be returned to the system. While this function currently
** only calls CF_CFDP_ResetTransaction(), it is here as a placeholder.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
*************************************************************************/
static inline void CF_CFDP_R1_Reset(CF_Transaction_t *t)
{
CF_CFDP_ResetTransaction(t, 1);
}
/************************************************************************/
/** \brief CFDP R2 transaction reset function.
**
** \par Description
** Handles reset logic for R2, then calls R1 reset logic.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_Reset(CF_Transaction_t *t)
{
if ((t->state_data.r.sub_state == CF_RxSubState_WAIT_FOR_FIN_ACK) ||
(t->state_data.r.r2.eof_cc != CF_CFDP_ConditionCode_NO_ERROR) ||
(t->history->cc != CF_CFDP_ConditionCode_NO_ERROR) || t->flags.com.canceled)
{
CF_CFDP_R1_Reset(t); /* it's done */
}
else
{
/* not waiting for fin ack, so trigger send fin */
t->flags.rx.send_fin = 1;
}
}
/************************************************************************/
/** \brief Checks that the transaction file's CRC matches expected.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
** \returns
** \retstmt 0 on CRC match, otherwise error. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R_CheckCrc(CF_Transaction_t *t, uint32 expected_crc)
{
int ret = 0;
CF_CRC_Finalize(&t->crc);
if (t->crc.result != expected_crc)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_CRC, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): crc mismatch for R trans. got 0x%x expected 0x%x",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num, t->crc.result,
expected_crc);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.crc_mismatch;
ret = 1;
}
return ret;
}
/************************************************************************/
/** \brief Checks R2 transaction state for transaction completion status.
**
** \par Description
** This function is called anywhere there's a desire to know if the
** transaction has completed. It may trigger other actions by setting
** flags to be handled during tick processing. In order for a
** transaction to be complete, it must have had its meta-data PDU
** received, the EOF must have been received, and there must be
** no gaps in the file. EOF is not checked in this function, because
** it's only called from functions after EOF is received.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_Complete(CF_Transaction_t *t, int ok_to_send_nak)
{
int send_nak = 0;
int send_fin = 0;
/* checking if r2 is complete. check nak list, and send NAK if appropriate */
/* if all data is present, then there will be no gaps in the chunk */
if (t->history->cc != CF_CFDP_ConditionCode_NO_ERROR)
{
goto err_out; /* nothing to do here if error cc is set */
}
/* first, check if md is received. if not, send specialized nak */
if (!t->flags.rx.md_recv)
{
send_nak = 1;
}
else
{
/* only look for 1 gap, since the goal here is just to know that there are gaps */
uint32 ret = CF_ChunkList_ComputeGaps(&t->chunks->chunks, 1, t->fsize, 0, NULL, NULL);
if (ret)
{
/* there is at least 1 gap, so send a nak */
send_nak = 1;
}
else if (t->flags.rx.eof_recv)
{
/* the eof was received, and there are no NAKs -- process completion in send fin state */
send_fin = 1;
}
}
if (send_nak && ok_to_send_nak)
{
if (++t->state_data.r.r2.acknak_count >= CF_AppData.config_table->nak_limit)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_NAK_LIMIT, CFE_EVS_EventType_ERROR, "CF R%d(%u:%u): nak limited reach",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num);
send_fin = 1;
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.nak_limit;
t->history->cc = CF_CFDP_ConditionCode_NAK_LIMIT_REACHED; /* don't use CF_CFDP_R2_SetCc because many places
in this function set send_fin */
t->state_data.r.r2.acknak_count = 0; /* reset for fin/ack */
}
else
{
t->flags.rx.send_nak = 1;
}
}
if (send_fin)
{
t->flags.rx.send_fin = 1;
t->flags.rx.complete = 1; /* latch completeness, since send_fin is cleared later */
}
/* always go to CF_RxSubState_FILEDATA, and let tick change state */
t->state_data.r.sub_state = CF_RxSubState_FILEDATA;
err_out:;
}
/************************************************************************/
/** \brief Process a filedata PDU on a transaction.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. bytes_received must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R_ProcessFd(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph, CFE_MSG_Size_t *bytes_received)
{
*bytes_received = CF_AppData.engine.in.bytes_received;
int ret = -1;
/* take out the variable PDU header size */
*bytes_received -= CF_HeaderSize(ph);
/* if crc field is present in the pdu header, subtract that from bytes_received */
if (FGV(ph->flags, CF_CFDP_PduHeader_FLAGS_CRC))
{
*bytes_received -= 4;
}
/* bytes_received now contains the number of bytes of file data in the pdu */
if (*bytes_received > sizeof(CF_CFDP_PduFileDataHeader_t))
{
CF_CFDP_PduFd_t *fd = STATIC_CAST(ph, CF_CFDP_PduFd_t);
int fret;
uint32 offset;
cfdp_ldst_uint32(offset, fd->fdh.offset);
if (t->state_data.r.cached_pos != offset)
{
fret = CF_WrappedLseek(t->fd, offset, OS_SEEK_SET);
if (fret != offset)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_SEEK_FD, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to seek offset %u, got 0x%08x", (t->state == CF_TxnState_R2),
t->history->src_eid, t->history->seq_num, offset, fret);
t->history->cc = CF_CFDP_ConditionCode_FILE_SIZE_ERROR;
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_seek;
goto err_out; /* connection will reset in caller */
}
}
*bytes_received -= sizeof(CF_CFDP_PduFileDataHeader_t);
fret = CF_WrappedWrite(t->fd, fd->fdd.data, *bytes_received);
if (fret != *bytes_received)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_WRITE, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): OS_write returned 0x%08x, got 0x%08x", (t->state == CF_TxnState_R2),
t->history->src_eid, t->history->seq_num, offset, fret);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_write;
t->history->cc = CF_CFDP_ConditionCode_FILESTORE_REJECTION;
goto err_out; /* connection will reset in caller */
}
t->state_data.r.cached_pos = (*bytes_received + offset);
CF_AppData.hk.channel_hk[t->chan_num].counters.recv.file_data_bytes += *bytes_received;
ret = 0;
}
else
{
/* file data PDU has 0 bytes -- so drop it */
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.dropped;
}
err_out:
return ret;
}
/************************************************************************/
/** \brief Processing receive EOF common functionality for R1/R2.
**
** \par Description
** This function is used for both R1 and R2 eof receive. It calls
** the unmarshaling function and then checks known transaction
** data against the PDU.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R_SubstateRecvEof(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
int ret = CF_RxEofRet_SUCCESS;
if (!CF_CFDP_RecvEof(t, ph))
{
uint32 size;
cfdp_ldst_uint32(size, STATIC_CAST(ph, CF_CFDP_PduEof_t)->size);
/* only check size if MD received, otherwise it's still OK */
if (t->flags.rx.md_recv && (size != t->fsize))
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_SIZE_MISMATCH, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): eof file size mismatch: got %u expected %u", (t->state == CF_TxnState_R2),
t->history->src_eid, t->history->seq_num, size, t->fsize);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_size_mismatch;
ret = CF_RxEofRet_FSIZE_MISMATCH;
goto err_out;
}
}
else
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_PDU_EOF, CFE_EVS_EventType_ERROR, "CF R%d(%u:%u): invalid eof packet",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
ret = CF_RxEofRet_BAD_EOF;
goto err_out;
}
err_out:
return ret;
}
/************************************************************************/
/** \brief Process receive EOF for R1.
**
** \par Description
** Only need to confirm crc for R1.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static void CF_CFDP_R1_SubstateRecvEof(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
int ret = CF_CFDP_R_SubstateRecvEof(t, ph);
uint32 crc;
cfdp_ldst_uint32(crc, STATIC_CAST(ph, CF_CFDP_PduEof_t)->crc);
if ((ret == CF_RxEofRet_SUCCESS) && !CF_CFDP_R_CheckCrc(t, crc))
{
/* successfully processed the file */
t->keep = 1; /* save the file */
}
/* if file failed to process, there's nothing to do. CF_CFDP_R_CheckCrc() generates an event on failure */
/* after exit, always reset since we are done */
/* reset even if the eof failed -- class 1, so it won't come again! */
CF_CFDP_R1_Reset(t);
}
/************************************************************************/
/** \brief Process receive EOF for R2.
**
** \par Description
** For R2, need to trigger the send of EOF-ACK and then call the
** check complete function which will either send NAK or FIN.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static void CF_CFDP_R2_SubstateRecvEof(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
if (!t->flags.rx.eof_recv)
{
int ret = CF_CFDP_R_SubstateRecvEof(t, ph);
/* did receiving eof succeed? */
if (ret == CF_RxEofRet_SUCCESS)
{
CF_CFDP_PduEof_t *eof = STATIC_CAST(ph, CF_CFDP_PduEof_t);
t->flags.rx.eof_recv = 1;
/* need to remember the eof crc for later */
cfdp_ldst_uint32(t->state_data.r.r2.eof_crc, eof->crc);
cfdp_ldst_uint32(t->state_data.r.r2.eof_size, eof->size);
/* always ack the EOF, even if we're not done */
t->state_data.r.r2.eof_cc = FGV(eof->cc, CF_CFDP_PduEof_FLAGS_CC);
t->flags.rx.send_ack = 1; /* defer sending ack to tick handling */
/* only check for complete if EOF with no errors */
if (t->state_data.r.r2.eof_cc == CF_CFDP_ConditionCode_NO_ERROR)
{
CF_CFDP_R2_Complete(t, 1); /* CF_CFDP_R2_Complete() will change state */
}
else
{
CF_CFDP_R2_Reset(t);
}
}
else
{
/* bad eof sent? */
if (ret == CF_RxEofRet_FSIZE_MISMATCH)
{
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILE_SIZE_ERROR);
}
else
{
/* can't do anything with this bad EOF, so return to FILEDATA */
t->state_data.r.sub_state = CF_RxSubState_FILEDATA;
}
}
}
}
/************************************************************************/
/** \brief Process received file data for R1.
**
** \par Description
** For R1, only need to digest the CRC.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R1_SubstateRecvFileData(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
CFE_MSG_Size_t bytes_received; /* initialized in CF_CFDP_R_ProcessFd() */
/* got file data pdu? */
if (CF_CFDP_RecvFd(t, ph) || CF_CFDP_R_ProcessFd(t, ph, &bytes_received))
{
goto err_out;
}
/* class 1 digests crc */
CF_CRC_Digest(&t->crc, STATIC_CAST(ph, CF_CFDP_PduFd_t)->fdd.data, (uint32)bytes_received);
return;
err_out:
CF_CFDP_R1_Reset(t);
}
/************************************************************************/
/** \brief Process received file data for R2.
**
** \par Description
** For R2, the CRC is checked after the whole file is received
** since there may be gaps. Instead, insert file received range
** data into chunks. Once NAK has been received, this function
** always checks for completion. This function also re-arms
** the ack timer.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_SubstateRecvFileData(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
CFE_MSG_Size_t bytes_received; /* initialized in CF_CFDP_R_ProcessFd() */
uint32 offset;
/* got file data pdu? */
if (CF_CFDP_RecvFd(t, ph) || CF_CFDP_R_ProcessFd(t, ph, &bytes_received))
{
goto err_out;
}
cfdp_ldst_uint32(offset, STATIC_CAST(ph, CF_CFDP_PduFd_t)->fdh.offset);
/* class 2 does crc at FIN, but track gaps */
CF_ChunkListAdd(&t->chunks->chunks, offset, (uint32)bytes_received);
if (t->flags.rx.fd_nak_sent)
{
CF_CFDP_R2_Complete(t, 0); /* once nak-retransmit received, start checking for completion at each fd */
}
if (!t->flags.rx.complete)
{
CF_CFDP_ArmAckTimer(t); /* re-arm ack timer, since we got data */
}
t->state_data.r.r2.acknak_count = 0;
return;
err_out:
CF_CFDP_R2_Reset(t);
}
/************************************************************************/
/** \brief Loads a single NAK segment request.
**
** \par Description
** This is a function callback from cf_chunks_compuete_gaps().
**
** \par Assumptions, External Events, and Notes:
** chunks must not be NULL. c must not be NULL. opaque must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static void CF_CFDP_R2_GapCompute(const CF_ChunkList_t *chunks, const CF_Chunk_t *c, void *opaque)
{
gap_compute_args_t *args = (gap_compute_args_t *)opaque;
CF_CFDP_PduNak_t *nak = STATIC_CAST(args->ph, CF_CFDP_PduNak_t);
CF_Assert(c->size > 0);
/* it seems that scope in the old engine is not used the way I read it in the spec, so
* leave this code here for now for future reference */
cfdp_ldst_uint32(nak->segment_requests[args->gap_counter].offset_start, (c->offset - nak->scope_start));
cfdp_ldst_uint32(nak->segment_requests[args->gap_counter].offset_end,
(nak->segment_requests[args->gap_counter].offset_start + c->size));
++args->gap_counter;
}
/************************************************************************/
/** \brief Send a NAK pdu for R2.
**
** \par Description
** NAK pdu is sent when there are gaps in the received data. The
** chunks class tracks this and generates the nak pdu by calculating
** gaps internally and calling CF_CFDP_R2_GapCompute(). There is a special
** case where if a metadata pdu has not been received, then a nak
** packet will be sent to request another.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R_SubstateSendNak(CF_Transaction_t *t)
{
CF_CFDP_PduHeader_t *ph = CF_CFDP_ConstructPduHeader(t, CF_CFDP_FileDirective_NAK, t->history->peer_eid,
CF_AppData.config_table->local_eid, 1, t->history->seq_num, 1);
CF_CFDP_PduNak_t *nak = STATIC_CAST(ph, CF_CFDP_PduNak_t);
CF_SendRet_t sret;
int ret = -1;
if (ph)
{
if (t->flags.rx.md_recv)
{
/* we have metadata, so send valid nak */
gap_compute_args_t args = {t, ph, 0};
uint32 cret;
cfdp_ldst_uint32(nak->scope_start, 0);
cret = CF_ChunkList_ComputeGaps(&t->chunks->chunks,
(t->chunks->chunks.count < t->chunks->chunks.CF_max_chunks)
? t->chunks->chunks.CF_max_chunks
: (t->chunks->chunks.CF_max_chunks - 1),
t->fsize, 0, CF_CFDP_R2_GapCompute, &args);
if (!cret)
{
/* no gaps left, so go ahead and check for completion */
t->flags.rx.complete = 1; /* we know md was received, and there's no gaps -- it's complete */
ret = 0;
}
else
{
/* gaps are present, so let's send the nak pdu */
cfdp_ldst_uint32(nak->scope_end, 0);
sret = CF_CFDP_SendNak(t, ph, cret);
t->flags.rx.fd_nak_sent = 1; /* latch that at least one nak has been sent requesting filedata */
CF_Assert(sret != CF_SendRet_ERROR); /* NOTE: this CF_Assert is here because CF_CFDP_SendNak() does not
return CF_SendRet_ERROR, so if it's ever added to that function we
need to test handling it here */
if (sret == CF_SendRet_SUCCESS)
{
CF_AppData.hk.channel_hk[t->chan_num].counters.sent.nak_segment_requests += cret;
ret = 0;
}
}
}
else
{
/* need to send simple nak packet to request metadata pdu again */
/* after doing so, transition to recv md state */
CFE_EVS_SendEvent(CF_EID_INF_CFDP_R_REQUEST_MD, CFE_EVS_EventType_INFORMATION,
"CF R%d(%u:%u): requesting MD", (t->state == CF_TxnState_R2), t->history->src_eid,
t->history->seq_num);
/* scope start/end, and sr[0] start/end == 0 special value to request metadata */
cfdp_ldst_uint32(nak->scope_start, 0);
cfdp_ldst_uint32(nak->scope_end, 0);
cfdp_ldst_uint32(nak->segment_requests[0].offset_start, 0);
cfdp_ldst_uint32(nak->segment_requests[0].offset_end, 0);
sret = CF_CFDP_SendNak(t, ph, 1);
CF_Assert(sret != CF_SendRet_ERROR); /* this CF_Assert is here because CF_CFDP_SendNak() does not return
CF_SendRet_ERROR */
if (sret == CF_SendRet_SUCCESS)
{
ret = 0;
}
}
}
return ret;
}
/************************************************************************/
/** \brief Initialize a transaction structure for R.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
*************************************************************************/
void CF_CFDP_R_Init(CF_Transaction_t *t)
{
int32 ret;
if (t->state == CF_TxnState_R2)
{
if (!t->flags.rx.md_recv)
{
/* we need to make a temp file and then do a NAK for md pdu */
/* the transaction already has a history, and that has a buffer that we can use to
* hold the temp filename */
/* the -1 below is to make room for the slash */
snprintf(t->history->fnames.dst_filename, sizeof(t->history->fnames.dst_filename) - 1, "%.*s/%d.tmp",
CF_FILENAME_MAX_PATH - 1, CF_AppData.config_table->tmp_dir, t->history->seq_num);
CFE_EVS_SendEvent(CF_EID_INF_CFDP_R_TEMP_FILE, CFE_EVS_EventType_INFORMATION,
"CF R%d(%u:%u): making temp file %s for transaction without MD",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num,
t->history->fnames.dst_filename);
}
CF_CFDP_ArmAckTimer(t);
}
ret = CF_WrappedOpenCreate(&t->fd, t->history->fnames.dst_filename, OS_FILE_FLAG_CREATE, OS_READ_WRITE);
if (ret < 0)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_CREAT, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to create file %s for writing, error=0x%08x",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num,
t->history->fnames.dst_filename, ret);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_open;
t->fd = OS_OBJECT_ID_UNDEFINED; /* just in case */
if (t->state == CF_TxnState_R2)
{
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILESTORE_REJECTION);
}
else
{
CF_CFDP_R1_Reset(t);
}
}
else
{
t->state_data.r.sub_state = CF_RxSubState_FILEDATA;
}
}
/************************************************************************/
/** \brief Calculate up to the configured amount of bytes of CRC.
**
** \par Description
** The configuration table has a number of bytes to calculate per
** transaction per wakeup. At each wakeup, the file is read and
** this number of bytes are calculated. This function will set
** the checksum error condition code if the final crc does not match.
**
** \par PTFO
** Increase throughput by consuming all crc bytes per wakeup in
** transaction-order. This would require a change to the meaning
** of the value in the configuration table.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
** \returns
** \retstmt 0 on completion, and -1 on non-completion. Error status is stored in condition code. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R2_CalcCrcChunk(CF_Transaction_t *t)
{
int ret = -1;
uint8 buf[CF_R2_CRC_CHUNK_SIZE];
uint32 count_bytes = 0;
#define RXC t->state_data.r.r2.rx_crc_calc_bytes
if (!RXC)
{
CF_CRC_Start(&t->crc);
}
while ((count_bytes < CF_AppData.config_table->rx_crc_calc_bytes_per_wakeup) && (RXC < t->fsize))
{
const uint32 want_offs_size = (RXC + sizeof(buf));
const uint32 read_size = (want_offs_size > t->fsize ? (t->fsize - RXC) : sizeof(buf));
int fret;
if (t->state_data.r.cached_pos != RXC)
{
fret = CF_WrappedLseek(t->fd, RXC, OS_SEEK_SET);
if (fret != RXC)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_SEEK_CRC, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to seek offset %u, got 0x%08x", (t->state == CF_TxnState_R2),
t->history->src_eid, t->history->seq_num, RXC, fret);
t->history->cc = CF_CFDP_ConditionCode_FILE_SIZE_ERROR; /* should be ok to use this one */
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_seek;
goto err_out;
}
}
fret = CF_WrappedRead(t->fd, buf, read_size);
if (fret != read_size)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_READ, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to read file expected %u, got 0x%08x",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num, read_size, fret);
t->history->cc = CF_CFDP_ConditionCode_FILE_SIZE_ERROR; /* should be ok to use this one */
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_read;
goto err_out;
}
CF_CRC_Digest(&t->crc, buf, read_size);
RXC += read_size;
t->state_data.r.cached_pos = RXC; /* should only call Lseek once */
}
if (RXC == t->fsize)
{
/* all bytes calculated, so now check */
if (!CF_CFDP_R_CheckCrc(t, t->state_data.r.r2.eof_crc))
{
/* crc matched! we are happy */
t->keep = 1; /* save the file */
/* set fin pdu status */
t->state_data.r.r2.dc = CF_CFDP_FinDeliveryCode_COMPLETE;
t->state_data.r.r2.fs = CF_CFDP_FinFileStatus_RETAINED;
}
else
{
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILE_CHECKSUM_FAILURE);
}
t->flags.com.crc_calc = 1;
ret = 0;
}
err_out:
return ret;
}
/************************************************************************/
/** \brief Send a FIN pdu.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL.
**
** \returns
** \retstmt 0 on success. Returns anything else on error. \endcode
** \endreturns
**
*************************************************************************/
static int CF_CFDP_R2_SubstateSendFin(CF_Transaction_t *t)
{
CF_SendRet_t sret;
int ret = -1;
if (t->history->cc == CF_CFDP_ConditionCode_NO_ERROR && !t->flags.com.crc_calc)
{
/* no error, and haven't checked crc -- so start checking it */
if (CF_CFDP_R2_CalcCrcChunk(t))
{
goto err_out; /* signal to caller to re-enter next tick */
}
}
sret = CF_CFDP_SendFin(t, t->state_data.r.r2.dc, t->state_data.r.r2.fs, t->history->cc);
CF_Assert(sret != CF_SendRet_ERROR); /* CF_CFDP_SendFin does not return CF_SendRet_ERROR */
t->state_data.r.sub_state =
CF_RxSubState_WAIT_FOR_FIN_ACK; /* whether or not fin send successful, ok to transition state */
if (sret == CF_SendRet_SUCCESS)
{
ret = 0;
goto err_out;
}
/* if no message, then try again next time */
err_out:
return ret;
}
/************************************************************************/
/** \brief Process receive FIN-ACK pdu.
**
** \par Description
** This is the end of an R2 transaction. Simply reset the transaction
** state.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_Recv_fin_ack(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
if (!CF_CFDP_RecvAck(t, ph))
{
/* got fin ack, so time to close the state */
CF_CFDP_R2_Reset(t);
}
else
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_PDU_FINACK, CFE_EVS_EventType_ERROR, "CF R%d(%u:%u): invalid fin-ack",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
}
}
/************************************************************************/
/** \brief Process receive metadata pdu for R2.
**
** \par Description
** It's possible that metadata PDU was missed in cf_cfdp.c, or that
** it was re-sent. This function checks if it was already processed,
** and if not, handles it. If there was a temp file opened due to
** missed metadata pdu, it will move the file to the correct
** destination according to the metadata pdu.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. ph must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R2_RecvMd(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph)
{
/* it isn't an error to get another MD pdu, right? */
if (!t->flags.rx.md_recv)
{
/* NOTE: t->flags.rx.md_recv always 1 in R1, so this is R2 only */
/* parse the md pdu. this will overwrite the transaction's history, which contains our filename. so let's
* save the filename in a local buffer so it can be used with OS_rename upon successful parsing of
* the md pdu */
char fname[CF_FILENAME_MAX_LEN];
int status;
strcpy(
fname,
t->history->fnames.dst_filename); /* strcpy is ok, since fname is CF_FILENAME_MAX_LEN like dst_filename */
status = CF_CFDP_RecvMd(t, ph);
if (!status)
{
/* successfully obtained md pdu */
if (t->flags.rx.eof_recv)
{
/* eof was received, so check that md and eof sizes match */
if (t->state_data.r.r2.eof_size != t->fsize)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_EOF_MD_SIZE, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): eof/md size mismatch md: %d, eof: %d",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num, t->fsize,
t->state_data.r.r2.eof_size);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_size_mismatch;
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILE_SIZE_ERROR);
goto err_out;
}
}
/* close and rename file */
CF_WrappedClose(t->fd);
CFE_ES_PerfLogEntry(CF_PERF_ID_RENAME);
status = OS_rename(fname, t->history->fnames.dst_filename);
CFE_ES_PerfLogExit(CF_PERF_ID_RENAME);
if (status != OS_SUCCESS)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_RENAME, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to rename file in R2, error=0x%08x",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num, status);
t->fd = OS_OBJECT_ID_UNDEFINED;
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILESTORE_REJECTION);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_rename;
goto err_out;
}
else
{
int32 ret =
CF_WrappedOpenCreate(&t->fd, t->history->fnames.dst_filename, OS_FILE_FLAG_NONE, OS_READ_WRITE);
if (ret < 0)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_OPEN, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): failed to open renamed file in R2, error=0x%08x",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num, ret);
CF_CFDP_R2_SetCc(t, CF_CFDP_ConditionCode_FILESTORE_REJECTION);
++CF_AppData.hk.channel_hk[t->chan_num].counters.fault.file_open;
t->fd = OS_OBJECT_ID_UNDEFINED; /* just in case */
goto err_out;
}
}
t->flags.rx.md_recv = 1;
t->state_data.r.r2.acknak_count = 0; /* in case part of nak */
CF_CFDP_R2_Complete(t, 1); /* check for completion now that md is received */
}
else
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_PDU_MD, CFE_EVS_EventType_ERROR, "CF R%d(%u:%u): invalid md received",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
/* do nothing here, since it will be nak'd again later */
}
}
err_out:;
}
/************************************************************************/
/** \brief Dispatch function for all received packets.
**
** \par Description
** For either R1 or R2 this function handles common logic for
** state processing based on current sub-state and the received
** pdu type.
**
** \par Assumptions, External Events, and Notes:
** t must not be NULL. fns must not be NULL.
**
*************************************************************************/
static void CF_CFDP_R_DispatchRecv(CF_Transaction_t *t, CF_CFDP_PduHeader_t *ph,
const CF_CFDP_R_SubstateDispatchTable_t *dispatch, CF_CFDP_StateRecvFunc_t fd_fn)
{
CF_Assert(t->state_data.r.sub_state < CF_RxSubState_NUM_STATES);
CF_CFDP_StateRecvFunc_t selected_handler;
selected_handler = NULL;
/* the 2d jump table is only used with file directive pdu */
if (!FGV(ph->flags, CF_CFDP_PduHeader_FLAGS_TYPE))
{
CF_CFDP_PduFileDirectiveHeader_t *fdh = STATIC_CAST(ph, CF_CFDP_PduFileDirectiveHeader_t);
if (fdh->directive_code < CF_CFDP_FileDirective_INVALID_MAX)
{
if (dispatch->state[t->state_data.r.sub_state] != NULL)
{
selected_handler = dispatch->state[t->state_data.r.sub_state]->fdirective[fdh->directive_code];
}
}
else
{
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.spurious;
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_R_DC_INV, CFE_EVS_EventType_ERROR,
"CF R%d(%u:%u): received pdu with invalid directive code %d for sub-state %d",
(t->state == CF_TxnState_R2), t->history->src_eid, t->history->seq_num,
fdh->directive_code, t->state_data.r.sub_state);
}
}
else
{
if (t->history->cc == CF_CFDP_ConditionCode_NO_ERROR)
{
selected_handler = fd_fn;
}
else
{
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.dropped;
}
}
/*
* NOTE: if no handler is selected, this will drop packets on the floor here,
* without incrementing any counter. This was existing behavior.
*/