-
Notifications
You must be signed in to change notification settings - Fork 46
/
cf_cfdp.c
1816 lines (1583 loc) · 62.9 KB
/
cf_cfdp.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
/************************************************************************
* NASA Docket No. GSC-18,447-1, and identified as “CFS CFDP (CF)
* Application version 3.0.0”
*
* Copyright (c) 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.
************************************************************************/
/**
* @file
*
* The CF Application main cfdp engine and pdu parsing implementation
*
* This file contains two sets of functions. The first is what is needed
* to deal with CFDP PDUs. Specifically validating them for correctness
* and ensuring the byte-order is correct for the target. The second
* is incoming and outgoing CFDP PDUs pass through here. All receive
* CFDP PDU logic is performed here and the data is passed to the
* R (rx) and S (tx) logic.
*/
#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_utils.h"
#include "cf_cfdp_r.h"
#include "cf_cfdp_s.h"
#include "cf_cfdp_dispatch.h"
#include "cf_cfdp_sbintf.h"
#include <string.h>
#include "cf_assert.h"
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_EncodeStart(CF_EncoderState_t *penc, void *msgbuf, CF_Logical_PduBuffer_t *ph, size_t encap_hdr_size,
size_t total_size)
{
/* Clear the PDU buffer structure to start */
memset(ph, 0, sizeof(*ph));
/* attach encoder object to PDU buffer which is attached to SB (encapsulation) buffer */
penc->base = (uint8 *)msgbuf;
ph->penc = penc;
CF_CFDP_CodecReset(&penc->codec_state, total_size);
/*
* adjust so that the base points to the actual PDU Header, this makes the offset
* refer to the real offset within the CFDP PDU, rather than the offset of the SB
* msg container.
*/
if (total_size > encap_hdr_size)
{
penc->codec_state.max_size -= encap_hdr_size;
penc->base += encap_hdr_size;
}
else
{
CF_CFDP_CodecSetDone(&penc->codec_state);
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_DecodeStart(CF_DecoderState_t *pdec, const void *msgbuf, CF_Logical_PduBuffer_t *ph, size_t encap_hdr_size,
size_t total_size)
{
/* Clear the PDU buffer structure to start */
memset(ph, 0, sizeof(*ph));
/* attach decoder object to PDU buffer which is attached to SB (encapsulation) buffer */
pdec->base = (const uint8 *)msgbuf;
ph->pdec = pdec;
CF_CFDP_CodecReset(&pdec->codec_state, total_size);
/*
* adjust so that the base points to the actual PDU Header, this makes the offset
* refer to the real offset within the CFDP PDU, rather than the offset of the SB
* msg container.
*/
if (total_size > encap_hdr_size)
{
pdec->codec_state.max_size -= encap_hdr_size;
pdec->base += encap_hdr_size;
}
else
{
CF_CFDP_CodecSetDone(&pdec->codec_state);
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_ArmAckTimer(CF_Transaction_t *t)
{
CF_Timer_InitRelSec(&t->ack_timer, CF_AppData.config_table->chan[t->chan_num].ack_timer_s);
t->flags.com.ack_timer_armed = 1;
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline CF_CFDP_Class_t CF_CFDP_GetClass(const CF_Transaction_t *t)
{
CF_Assert(t->flags.com.q_index != CF_QueueIdx_FREE);
return !!((t->state == CF_TxnState_S2) || (t->state == CF_TxnState_R2));
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline int CF_CFDP_IsSender(CF_Transaction_t *t)
{
CF_Assert(t->flags.com.q_index != CF_QueueIdx_FREE);
/* the state could actually be CF_TxnState_IDLE, which is still not a sender. This would
* be an unused transaction in the RX (CF_CFDP_ReceiveMessage) path. */
return !!((t->state == CF_TxnState_S1) || (t->state == CF_TxnState_S2));
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline void CF_CFDP_ArmInactTimer(CF_Transaction_t *t)
{
CF_Timer_InitRelSec(&t->inactivity_timer, CF_AppData.config_table->chan[t->chan_num].inactivity_timer_s);
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_DispatchRecv(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
static const CF_CFDP_TxnRecvDispatchTable_t state_fns = {.rx = {[CF_TxnState_IDLE] = CF_CFDP_RecvIdle,
[CF_TxnState_R1] = CF_CFDP_R1_Recv,
[CF_TxnState_S1] = CF_CFDP_S1_Recv,
[CF_TxnState_R2] = CF_CFDP_R2_Recv,
[CF_TxnState_S2] = CF_CFDP_S2_Recv,
[CF_TxnState_DROP] = CF_CFDP_RecvDrop}};
CF_CFDP_RxStateDispatch(t, ph, &state_fns);
CF_CFDP_ArmInactTimer(t); /* whenever a packet was received by the other size, always arm its inactivity timer */
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static void CF_CFDP_DispatchTx(CF_Transaction_t *t)
{
static const CF_CFDP_TxnSendDispatchTable_t state_fns = {
.tx = {[CF_TxnState_S1] = CF_CFDP_S1_Tx, [CF_TxnState_S2] = CF_CFDP_S2_Tx}};
CF_CFDP_TxStateDispatch(t, &state_fns);
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static CF_ChunkWrapper_t *CF_CFDP_FindUnusedChunks(CF_Channel_t *c, CF_Direction_t dir)
{
CF_ChunkWrapper_t *ret;
CF_Assert(dir < CF_Direction_NUM);
CF_Assert(c->cs[dir]);
ret = container_of(CF_CList_Pop(&c->cs[dir]), CF_ChunkWrapper_t, cl_node);
return ret;
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static void CF_CFDP_SetPduLength(CF_Logical_PduBuffer_t *ph)
{
uint16 final_pos;
/* final position of the encoder state should reflect the entire PDU length */
final_pos = CF_CODEC_GET_POSITION(ph->penc);
if (final_pos >= ph->pdu_header.header_encoded_length)
{
/* the value that goes into the packet is length _after_ header */
ph->pdu_header.data_encoded_length = final_pos - ph->pdu_header.header_encoded_length;
}
CF_CFDP_EncodeHeaderFinalSize(ph->penc, &ph->pdu_header);
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_Logical_PduBuffer_t *CF_CFDP_ConstructPduHeader(const CF_Transaction_t *t, CF_CFDP_FileDirective_t directive_code,
CF_EntityId_t src_eid, CF_EntityId_t dst_eid, bool towards_sender,
CF_TransactionSeq_t tsn, bool silent)
{
/* directive_code == 0 if file data */
CF_Logical_PduBuffer_t *ph;
CF_Logical_PduHeader_t *hdr;
uint8 eid_len;
ph = CF_CFDP_MsgOutGet(t, silent);
if (ph)
{
hdr = &ph->pdu_header;
hdr->version = 1;
hdr->pdu_type = (directive_code == 0); /* set to '1' for file data pdu, '0' for a directive pdu */
hdr->direction = (towards_sender != 0); /* set to '1' for toward sender, '0' for toward receiver */
hdr->txm_mode = (CF_CFDP_GetClass(t) == CF_CFDP_CLASS_1); /* set to '1' for class 1 data, '0' for class 2 */
/* choose the larger of the two EIDs to determine size */
if (src_eid > dst_eid)
{
eid_len = CF_CFDP_GetValueEncodedSize(src_eid);
}
else
{
eid_len = CF_CFDP_GetValueEncodedSize(dst_eid);
}
/*
* This struct holds the "real" length - when assembled into the final packet
* this is encoded as 1 less than this value
*/
hdr->eid_length = eid_len;
hdr->txn_seq_length = CF_CFDP_GetValueEncodedSize(tsn);
hdr->source_eid = src_eid;
hdr->destination_eid = dst_eid;
hdr->sequence_num = tsn;
/*
* encode the known parts so far. total_size field cannot be
* included yet because its value is not known, but the basic
* encoding of the other stuff needs to be done so the position
* of any data fields can be determined.
*/
CF_CFDP_EncodeHeaderWithoutSize(ph->penc, hdr);
/* If directive code is zero, the pdu is a file data pdu which has no directive code field.
* So only set if non-zero, otherwise it will write a 0 to a byte in a file data pdu where we
* don't necessarily want a 0. */
if (directive_code)
{
/* set values which can be determined at this time */
ph->fdirective.directive_code = directive_code;
CF_CFDP_EncodeFileDirectiveHeader(ph->penc, &ph->fdirective);
}
}
return ph;
}
/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline size_t CF_strnlen(const char *s, size_t maxlen)
{
const char *end = memchr(s, 0, maxlen);
if (end != NULL)
{
/* actual length of string is difference */
maxlen = end - s;
}
return maxlen;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendMd(CF_Transaction_t *t)
{
CF_Logical_PduBuffer_t *ph =
CF_CFDP_ConstructPduHeader(t, CF_CFDP_FileDirective_METADATA, CF_AppData.config_table->local_eid,
t->history->peer_eid, 0, t->history->seq_num, 0);
CF_Logical_PduMd_t *md;
CF_SendRet_t sret;
sret = CF_SendRet_SUCCESS;
if (!ph)
{
sret = CF_SendRet_NO_MSG;
}
else
{
md = &ph->int_header.md;
CF_Assert((t->state == CF_TxnState_S1) || (t->state == CF_TxnState_S2));
md->size = t->fsize;
/* at this point, need to append filenames into md packet */
/* this does not actually copy here - that is done during encode */
md->source_filename.length =
CF_strnlen(t->history->fnames.src_filename, sizeof(t->history->fnames.src_filename));
md->source_filename.data_ptr = t->history->fnames.src_filename;
md->dest_filename.length = CF_strnlen(t->history->fnames.dst_filename, sizeof(t->history->fnames.dst_filename));
md->dest_filename.data_ptr = t->history->fnames.dst_filename;
CF_CFDP_EncodeMd(ph->penc, md);
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
}
return sret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendFd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
/* NOTE: SendFd does not need a call to CF_CFDP_MsgOutGet, as the caller already has it */
CF_SendRet_t ret = CF_SendRet_SUCCESS;
/* this should check if any encoding error occurred */
/* update pdu length */
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_AppendTlv(CF_Logical_TlvList_t *ptlv_list, CF_CFDP_TlvType_t tlv_type)
{
CF_Logical_Tlv_t *ptlv;
if (ptlv_list->num_tlv < CF_PDU_MAX_TLV)
{
ptlv = &ptlv_list->tlv[ptlv_list->num_tlv];
++ptlv_list->num_tlv;
}
else
{
ptlv = NULL;
}
if (ptlv)
{
ptlv->type = tlv_type;
if (tlv_type == CF_CFDP_TLV_TYPE_ENTITY_ID)
{
ptlv->data.eid = CF_AppData.config_table->local_eid;
ptlv->length = CF_CFDP_GetValueEncodedSize(ptlv->data.eid);
}
else
{
ptlv->data.data_ptr = NULL;
ptlv->length = 0;
}
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendEof(CF_Transaction_t *t)
{
CF_Logical_PduBuffer_t *ph =
CF_CFDP_ConstructPduHeader(t, CF_CFDP_FileDirective_EOF, CF_AppData.config_table->local_eid,
t->history->peer_eid, 0, t->history->seq_num, 0);
CF_Logical_PduEof_t *eof;
CF_SendRet_t ret = CF_SendRet_SUCCESS;
if (!ph)
{
ret = CF_SendRet_NO_MSG;
}
else
{
eof = &ph->int_header.eof;
eof->cc = CF_TxnStatus_To_ConditionCode(t->history->txn_stat);
eof->crc = t->crc.result;
eof->size = t->fsize;
if (eof->cc != CF_CFDP_ConditionCode_NO_ERROR)
{
CF_CFDP_AppendTlv(&eof->tlv_list, CF_CFDP_TLV_TYPE_ENTITY_ID);
}
CF_CFDP_EncodeEof(ph->penc, eof);
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendAck(CF_Transaction_t *t, CF_CFDP_AckTxnStatus_t ts, CF_CFDP_FileDirective_t dir_code,
CF_CFDP_ConditionCode_t cc, CF_EntityId_t peer_eid, CF_TransactionSeq_t tsn)
{
CF_Logical_PduBuffer_t *ph;
CF_Logical_PduAck_t * ack;
CF_SendRet_t ret = CF_SendRet_SUCCESS;
CF_EntityId_t src_eid;
CF_EntityId_t dst_eid;
CF_Assert((dir_code == CF_CFDP_FileDirective_EOF) || (dir_code == CF_CFDP_FileDirective_FIN));
if (CF_CFDP_IsSender(t))
{
src_eid = CF_AppData.config_table->local_eid;
dst_eid = peer_eid;
}
else
{
src_eid = peer_eid;
dst_eid = CF_AppData.config_table->local_eid;
}
ph = CF_CFDP_ConstructPduHeader(t, CF_CFDP_FileDirective_ACK, src_eid, dst_eid,
(dir_code == CF_CFDP_FileDirective_EOF), tsn, 0);
if (!ph)
{
ret = CF_SendRet_NO_MSG;
}
else
{
ack = &ph->int_header.ack;
ack->ack_directive_code = dir_code;
ack->ack_subtype_code = 1; /* looks like always 1 if not extended features */
ack->cc = cc;
ack->txn_status = ts;
CF_CFDP_EncodeAck(ph->penc, ack);
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendFin(CF_Transaction_t *t, CF_CFDP_FinDeliveryCode_t dc, CF_CFDP_FinFileStatus_t fs,
CF_CFDP_ConditionCode_t cc)
{
CF_Logical_PduBuffer_t *ph =
CF_CFDP_ConstructPduHeader(t, CF_CFDP_FileDirective_FIN, t->history->peer_eid,
CF_AppData.config_table->local_eid, 1, t->history->seq_num, 0);
CF_Logical_PduFin_t *fin;
CF_SendRet_t ret = CF_SendRet_SUCCESS;
if (!ph)
{
ret = CF_SendRet_NO_MSG;
}
else
{
fin = &ph->int_header.fin;
fin->cc = cc;
fin->delivery_code = dc;
fin->file_status = fs;
if (cc != CF_CFDP_ConditionCode_NO_ERROR)
{
CF_CFDP_AppendTlv(&fin->tlv_list, CF_CFDP_TLV_TYPE_ENTITY_ID);
}
CF_CFDP_EncodeFin(ph->penc, fin);
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
CF_SendRet_t CF_CFDP_SendNak(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
CF_Logical_PduNak_t *nak;
CF_SendRet_t ret = CF_SendRet_SUCCESS;
if (!ph)
{
ret = CF_SendRet_NO_MSG;
}
else
{
CF_Assert(CF_CFDP_GetClass(t) == CF_CFDP_CLASS_2);
nak = &ph->int_header.nak;
/*
* NOTE: the caller should have already initialized all the fields.
* This does not need to add anything more to the NAK here
*/
CF_CFDP_EncodeNak(ph->penc, nak);
CF_CFDP_SetPduLength(ph);
CF_CFDP_Send(t->chan_num, ph);
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvPh(uint8 chan_num, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_Assert(chan_num < CF_NUM_CHANNELS);
/*
* If the source eid, destination eid, or sequence number fields
* are larger than the sizes configured in the cf platform config
* file, then reject the PDU.
*/
if (CF_CFDP_DecodeHeader(ph->pdec, &ph->pdu_header) != CFE_SUCCESS)
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_TRUNCATION, CFE_EVS_EventType_ERROR,
"CF: pdu rejected due to eid/seq number field truncation");
++CF_AppData.hk.channel_hk[chan_num].counters.recv.error;
ret = -1;
}
/*
* The "large file" flag is not supported by this implementation yet.
* This means file sizes and offsets will be 64 bits, so codec routines
* will need to be updated to understand this. OSAL also doesn't support
* 64-bit file access yet.
*/
else if (CF_CODEC_IS_OK(ph->pdec) && ph->pdu_header.large_flag)
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_LARGE_FILE, CFE_EVS_EventType_ERROR,
"CF: pdu with large file bit received (unsupported)");
++CF_AppData.hk.channel_hk[chan_num].counters.recv.error;
ret = -1;
}
else
{
if (CF_CODEC_IS_OK(ph->pdec) && ph->pdu_header.pdu_type == 0)
{
CF_CFDP_DecodeFileDirectiveHeader(ph->pdec, &ph->fdirective);
}
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_SHORT_HEADER, CFE_EVS_EventType_ERROR, "CF: pdu too short (%lu received)",
(unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
++CF_AppData.hk.channel_hk[chan_num].counters.recv.error;
ret = -1;
}
else
{
/* pdu is ok, so continue processing */
++CF_AppData.hk.channel_hk[chan_num].counters.recv.pdu;
}
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvMd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
const CF_Logical_PduMd_t *md = &ph->int_header.md;
int lv_ret;
int ret = 0;
CF_CFDP_DecodeMd(ph->pdec, &ph->int_header.md);
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_MD_SHORT, CFE_EVS_EventType_ERROR,
"CF: metadata packet too short: %lu bytes received",
(unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
/* error return path */
ret = -1;
}
else
{
/* store the expected file size in transaction */
t->fsize = md->size;
/*
* store the filenames in transaction.
*
* NOTE: The "CF_CFDP_CopyStringFromLV()" now knows that the data is supposed to be a C string,
* and ensures that the output content is properly terminated, so this only needs to check that
* it worked.
*/
lv_ret = CF_CFDP_CopyStringFromLV(t->history->fnames.src_filename, sizeof(t->history->fnames.src_filename),
&md->source_filename);
if (lv_ret < 0)
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_INVALID_SRC_LEN, CFE_EVS_EventType_ERROR,
"CF: metadata pdu rejected due to invalid length in source filename of 0x%02x",
md->source_filename.length);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
/* error return path */
ret = -1;
}
else
{
lv_ret = CF_CFDP_CopyStringFromLV(t->history->fnames.dst_filename, sizeof(t->history->fnames.dst_filename),
&md->dest_filename);
if (lv_ret < 0)
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_INVALID_DST_LEN, CFE_EVS_EventType_ERROR,
"CF: metadata pdu rejected due to invalid length in dest filename of 0x%02x",
md->dest_filename.length);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
/* error return path */
ret = -1;
}
else
{
CFE_EVS_SendEvent(CF_EID_INF_PDU_MD_RECVD, CFE_EVS_EventType_INFORMATION,
"CF: md received for source: %s, dest: %s", t->history->fnames.src_filename,
t->history->fnames.dst_filename);
}
}
}
/* normal return path */
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvFd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_CFDP_DecodeFileDataHeader(ph->pdec, ph->pdu_header.segment_meta_flag, &ph->int_header.fd);
/* if the CRC flag is set, need to deduct the size of the CRC from the data - always 32 bits */
if (CF_CODEC_IS_OK(ph->pdec) && ph->pdu_header.crc_flag)
{
if (ph->int_header.fd.data_len < sizeof(CF_CFDP_uint32_t))
{
CF_CODEC_SET_DONE(ph->pdec);
}
else
{
ph->int_header.fd.data_len -= sizeof(CF_CFDP_uint32_t);
}
}
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_FD_SHORT, CFE_EVS_EventType_ERROR,
"CF: filedata pdu too short: %lu bytes received", (unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
CF_CFDP_SetTxnStatus(t, CF_TxnStatus_PROTOCOL_ERROR);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
ret = -1;
}
else if (ph->pdu_header.segment_meta_flag)
{
/* If recv PDU has the "segment_meta_flag" set, this is not currently handled in CF. */
CFE_EVS_SendEvent(CF_EID_ERR_PDU_FD_UNSUPPORTED, CFE_EVS_EventType_ERROR,
"CF: filedata pdu with segment metadata received");
CF_CFDP_SetTxnStatus(t, CF_TxnStatus_PROTOCOL_ERROR);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
ret = -1;
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvEof(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_CFDP_DecodeEof(ph->pdec, &ph->int_header.eof);
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_EOF_SHORT, CFE_EVS_EventType_ERROR,
"CF: eof pdu too short: %lu bytes received", (unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
ret = -1;
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvAck(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_CFDP_DecodeAck(ph->pdec, &ph->int_header.ack);
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_ACK_SHORT, CFE_EVS_EventType_ERROR,
"CF: ack pdu too short: %lu bytes received", (unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
ret = -1;
}
/* nothing to do for this one, as all fields are bytes */
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvFin(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_CFDP_DecodeFin(ph->pdec, &ph->int_header.fin);
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_FIN_SHORT, CFE_EVS_EventType_ERROR,
"CF: fin pdu too short: %lu bytes received", (unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
ret = -1;
}
/* NOTE: right now we don't care about the fault location */
/* nothing to do for this one. All fields are bytes */
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int CF_CFDP_RecvNak(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
int ret = 0;
CF_CFDP_DecodeNak(ph->pdec, &ph->int_header.nak);
if (!CF_CODEC_IS_OK(ph->pdec))
{
CFE_EVS_SendEvent(CF_EID_ERR_PDU_NAK_SHORT, CFE_EVS_EventType_ERROR,
"CF: nak pdu too short: %lu bytes received", (unsigned long)CF_CODEC_GET_SIZE(ph->pdec));
ret = -1;
}
return ret;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_RecvDrop(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.dropped;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
void CF_CFDP_RecvIdle(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
CF_Logical_PduFileDirectiveHeader_t *fdh;
int status;
/* only RX transactions dare tread here */
t->history->seq_num = ph->pdu_header.sequence_num;
/* peer_eid is always the remote partner. src_eid is always the transaction source.
* in this case, they are the same */
t->history->peer_eid = ph->pdu_header.source_eid;
t->history->src_eid = ph->pdu_header.source_eid;
t->chunks = CF_CFDP_FindUnusedChunks(&CF_AppData.engine.channels[t->chan_num], CF_Direction_RX);
/* this is an idle transaction, so see if there's a received packet that can
* be bound to the transaction */
if (ph->pdu_header.pdu_type)
{
/* file data PDU */
/* being idle and receiving a file data PDU means that no active transaction knew
* about the transaction in progress, so most likely PDUs were missed. */
/* if class 2, switch into R2 state and let it handle */
/* don't forget to bind the transaction */
if (ph->pdu_header.txm_mode)
{
/* R1, can't do anything without metadata first */
t->state = CF_TxnState_DROP; /* drop all incoming */
/* use inactivity timer to ultimately free the state */
}
else
{
/* R2 can handle missing metadata, so go ahead and create a temp file */
t->state = CF_TxnState_R2;
CF_CFDP_R_Init(t);
CF_CFDP_DispatchRecv(t, ph); /* re-dispatch to enter r2 */
}
}
else
{
fdh = &ph->fdirective;
/* file directive PDU, but we are in an idle state. It only makes sense right now to accept metadata PDU. */
switch (fdh->directive_code)
{
case CF_CFDP_FileDirective_METADATA:
status = CF_CFDP_RecvMd(t, ph);
if (!status)
{
/* NOTE: whether or not class 1 or 2, get a free chunks. It's cheap, and simplifies cleanup path */
t->state = ph->pdu_header.txm_mode ? CF_TxnState_R1 : CF_TxnState_R2;
t->flags.rx.md_recv = 1;
CF_CFDP_R_Init(t); /* initialize R */
}
else
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_IDLE_MD, CFE_EVS_EventType_ERROR,
"CF: got invalid md pdu -- abandoning transaction");
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
/* leave state as idle, which will reset below */
}
break;
default:
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_FD_UNHANDLED, CFE_EVS_EventType_ERROR,
"CF: unhandled file directive code 0x%02x in idle state", fdh->directive_code);
++CF_AppData.hk.channel_hk[t->chan_num].counters.recv.error;
break;
}
}
if (t->state == CF_TxnState_IDLE)
{
/* state was not changed, so free the transaction */
CF_CFDP_ResetTransaction(t, 0);
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in cf_cfdp.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CF_CFDP_InitEngine(void)
{
/* initialize all transaction nodes */
int i;
int j;
int chunk_mem_offset = 0;
CF_Transaction_t * t = CF_AppData.engine.transactions;
CF_ChunkWrapper_t *c = CF_AppData.engine.chunks;
int32 ret = CFE_SUCCESS;
static const int CF_DIR_MAX_CHUNKS[CF_Direction_NUM][CF_NUM_CHANNELS] = {CF_CHANNEL_NUM_RX_CHUNKS_PER_TRANSACTION,
CF_CHANNEL_NUM_TX_CHUNKS_PER_TRANSACTION};
memset(&CF_AppData.engine, 0, sizeof(CF_AppData.engine));
for (i = 0; i < CF_NUM_CHANNELS; ++i)
{
char nbuf[64];
snprintf(nbuf, sizeof(nbuf) - 1, "%s%d", CF_CHANNEL_PIPE_PREFIX, i);
ret = CFE_SB_CreatePipe(&CF_AppData.engine.channels[i].pipe, CF_AppData.config_table->chan[i].pipe_depth_input,
nbuf);
if (ret != CFE_SUCCESS)
{
CFE_EVS_SendEvent(CF_EID_ERR_INIT_PIPE, CFE_EVS_EventType_ERROR,
"CF: failed to create pipe %s, returned 0x%08lx", nbuf, (unsigned long)ret);
break;
}
ret = CFE_SB_SubscribeLocal(CFE_SB_ValueToMsgId(CF_AppData.config_table->chan[i].mid_input),
CF_AppData.engine.channels[i].pipe,
CF_AppData.config_table->chan[i].pipe_depth_input);
if (ret != CFE_SUCCESS)
{
CFE_EVS_SendEvent(CF_EID_ERR_INIT_SUB, CFE_EVS_EventType_ERROR,
"CF: failed to subscribe to MID 0x%lx, returned 0x%08lx",
(unsigned long)CF_AppData.config_table->chan[i].mid_input, (unsigned long)ret);
break;
}
if (CF_AppData.config_table->chan[i].sem_name[0])
{
/*
* There is a start up race condition because CFE starts all apps at the same time,
* and if this sem is instantiated by another app, it may not be created yet.
*
* Therefore if OSAL returns OS_ERR_NAME_NOT_FOUND, assume this is what is going
* on, delay a bit and try again.
*/
ret = OS_ERR_NAME_NOT_FOUND;