forked from nasa/fprime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
917 lines (787 loc) · 29.4 KB
/
Channel.cpp
File metadata and controls
917 lines (787 loc) · 29.4 KB
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
// ======================================================================
// \title Channel.cpp
// \brief CFDP Channel operations implementation
//
// This file is a port of channel-specific functions from the following files
// from the NASA Core Flight System (cFS) CFDP (CF) Application, version 3.0.0,
// adapted for use within the F-Prime (F') framework:
// - cf_cfdp.c (channel processing functions)
// - cf_utils.c (channel transaction and resource management)
//
// ======================================================================
//
// NASA Docket No. GSC-18,447-1
//
// 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.
//
// ======================================================================
#include <string.h>
#include <new>
#include <Fw/FPrimeBasicTypes.hpp>
#include <Svc/Ccsds/CfdpManager/CfdpManager.hpp>
#include <Svc/Ccsds/CfdpManager/Channel.hpp>
#include <Svc/Ccsds/CfdpManager/Engine.hpp>
#include <Svc/Ccsds/CfdpManager/Utils.hpp>
namespace Svc {
namespace Ccsds {
namespace Cfdp {
// ----------------------------------------------------------------------
// Construction
// ----------------------------------------------------------------------
Channel::Channel(Engine* engine, U8 channelId, CfdpManager* cfdpManager) :
m_engine(engine),
m_numCmdTx(0),
m_cur(nullptr),
m_cfdpManager(cfdpManager),
m_tickType(0),
m_channelId(channelId),
m_flowState(Cfdp::Flow::NOT_FROZEN),
m_outgoingCounter(0),
m_transactions(nullptr),
m_histories(nullptr),
m_chunks(nullptr),
m_chunkMem(nullptr)
{
FW_ASSERT(engine != nullptr);
FW_ASSERT(cfdpManager != nullptr);
// Initialize queue pointers
for (U32 i = 0; i < QueueId::NUM; i++) {
m_qs[i] = nullptr;
}
// Initialize command/history lists
for (U32 i = 0; i < DIRECTION_NUM; i++) {
m_cs[i] = nullptr;
}
// Initialize poll directory playback state
for (U32 i = 0; i < CFDP_MAX_POLLING_DIR_PER_CHAN; i++) {
m_polldir[i].pb.busy = false;
m_polldir[i].pb.diropen = false;
m_polldir[i].pb.counted = false;
m_polldir[i].pb.num_ts = 0;
m_polldir[i].pb.pending_file[0] = '\0';
}
// Initialize playback structures
for (U32 i = 0; i < CFDP_MAX_COMMANDED_PLAYBACK_DIRECTORIES_PER_CHAN; i++) {
m_playback[i].busy = false;
m_playback[i].diropen = false;
m_playback[i].counted = false;
m_playback[i].num_ts = 0;
m_playback[i].pending_file[0] = '\0';
}
// Allocate and initialize per-channel resources
U32 j, k;
History* history;
Transaction* txn;
CfdpChunkWrapper* cw;
CListNode** list_head;
U32 chunk_mem_offset = 0;
U32 total_chunks_needed;
// Initialize chunk configuration for this channel
const U32 rxChunksPerChannel[] = CFDP_CHANNEL_NUM_RX_CHUNKS_PER_TRANSACTION;
const U32 txChunksPerChannel[] = CFDP_CHANNEL_NUM_TX_CHUNKS_PER_TRANSACTION;
m_dirMaxChunks[DIRECTION_RX] = rxChunksPerChannel[m_channelId];
m_dirMaxChunks[DIRECTION_TX] = txChunksPerChannel[m_channelId];
// Calculate total chunks needed for this channel
total_chunks_needed = 0;
for (k = 0; k < DIRECTION_NUM; ++k) {
total_chunks_needed += m_dirMaxChunks[k] * CFDP_NUM_TRANSACTIONS_PER_CHANNEL;
}
// Allocate arrays
// Use operator new for raw memory (for types requiring placement new with constructor params)
m_transactions = static_cast<Transaction*>(
::operator new(CFDP_NUM_TRANSACTIONS_PER_CHANNEL * sizeof(Transaction))
);
m_chunks = static_cast<CfdpChunkWrapper*>(
::operator new((CFDP_NUM_TRANSACTIONS_PER_CHANNEL * DIRECTION_NUM) * sizeof(CfdpChunkWrapper))
);
// Regular new for simple types
m_histories = new History[CFDP_NUM_HISTORIES_PER_CHANNEL];
m_chunkMem = new Chunk[total_chunks_needed];
// Initialize transactions using placement new with parameterized constructor
cw = m_chunks;
for (j = 0; j < CFDP_NUM_TRANSACTIONS_PER_CHANNEL; ++j)
{
// Construct transaction in-place with parameterized constructor
txn = new (&m_transactions[j]) Transaction(this, m_channelId, m_engine, m_cfdpManager);
// Put transaction on free list
this->freeTransaction(txn);
// Initialize chunk wrappers for this transaction (TX and RX)
for (k = 0; k < DIRECTION_NUM; ++k, ++cw)
{
list_head = this->getChunkListHead(static_cast<U8>(k));
// Use placement new to construct CfdpChunkWrapper with the new class-based interface
new (cw) CfdpChunkWrapper(static_cast<ChunkIdx>(m_dirMaxChunks[k]), &m_chunkMem[chunk_mem_offset]);
chunk_mem_offset += m_dirMaxChunks[k];
CfdpCListInitNode(&cw->cl_node);
CfdpCListInsertBack(list_head, &cw->cl_node);
}
}
// Initialize histories
for (j = 0; j < CFDP_NUM_HISTORIES_PER_CHANNEL; ++j)
{
history = &m_histories[j];
// Zero-initialize using aggregate initialization
*history = {};
CfdpCListInitNode(&history->cl_node);
this->insertBackInQueue(QueueId::HIST_FREE, &history->cl_node);
}
}
Channel::~Channel()
{
// Free dynamically allocated resources
if (m_transactions != nullptr) {
// Manually call destructors since we used placement new
for (U32 j = 0; j < CFDP_NUM_TRANSACTIONS_PER_CHANNEL; ++j) {
m_transactions[j].~Transaction();
}
// Free raw memory allocated with operator new
::operator delete(m_transactions);
m_transactions = nullptr;
}
if (m_histories != nullptr) {
delete[] m_histories;
m_histories = nullptr;
}
if (m_chunks != nullptr) {
// Manually call destructors since we used placement new
for (U32 j = 0; j < (CFDP_NUM_TRANSACTIONS_PER_CHANNEL * DIRECTION_NUM); ++j) {
m_chunks[j].~CfdpChunkWrapper();
}
// Free raw memory allocated with operator new
::operator delete(m_chunks);
m_chunks = nullptr;
}
if (m_chunkMem != nullptr) {
delete[] m_chunkMem;
m_chunkMem = nullptr;
}
}
// ----------------------------------------------------------------------
// Channel Processing
// ----------------------------------------------------------------------
void Channel::cycleTx()
{
Transaction* txn;
CycleTxArgs args;
if (m_cfdpManager->getDequeueEnabledParam(m_channelId))
{
args.chan = this;
args.ran_one = 0;
// loop through as long as there are pending transactions, and a message buffer to send their PDUs on
// NOTE: tick processing is higher priority than sending new filedata PDUs, so only send however many
// PDUs that can be sent once we get to here
if (!this->m_cur)
{ // don't enter if cur is set, since we need to pick up where we left off on tick processing next scheduler cycle
// TODO BPC: refactor all while loops
while (true)
{
// Attempt to run something on TXA
CfdpCListTraverse(m_qs[QueueId::TXA],
[this](CListNode* node, void* context) -> CListTraverseStatus {
return this->cycleTxFirstActive(node, context);
},
&args);
// Keep going until QueueId::PEND is empty or something is run
if (args.ran_one || m_qs[QueueId::PEND] == NULL)
{
break;
}
txn = container_of_cpp(m_qs[QueueId::PEND], &Transaction::m_cl_node);
// Class 2 transactions need a chunklist for NAK processing, get one now.
// Class 1 transactions don't need chunks since they don't support NAKs.
if (txn->getClass() == Cfdp::Class::CLASS_2)
{
if (txn->m_chunks == NULL)
{
txn->m_chunks = this->findUnusedChunks(DIRECTION_TX);
}
if (txn->m_chunks == NULL)
{
// TODO BPC: Emit EVR
// Leave transaction pending until a chunklist is available.
break;
}
}
m_engine->armInactTimer(txn);
this->moveTransaction(txn, QueueId::TXA);
}
}
// in case the loop exited due to no message buffers, clear it and start from the top next time
this->m_cur = NULL;
}
}
void Channel::tickTransactions()
{
bool reset = true;
void (Transaction::*fns[CFDP_TICK_TYPE_NUM_TYPES])(int*) = {&Transaction::rTick, &Transaction::sTick,
&Transaction::sTickNak};
int qs[CFDP_TICK_TYPE_NUM_TYPES] = {QueueId::RX, QueueId::TXW, QueueId::TXW};
FW_ASSERT(m_tickType < CFDP_TICK_TYPE_NUM_TYPES, m_tickType);
for (; m_tickType < CFDP_TICK_TYPE_NUM_TYPES; ++m_tickType)
{
TickArgs args = {this, fns[m_tickType], 0, 0};
do
{
args.cont = 0;
CfdpCListTraverse(m_qs[qs[m_tickType]],
[this](CListNode* node, void* context) -> CListTraverseStatus {
return this->doTick(node, context);
},
&args);
if (args.early_exit)
{
// early exit means we ran out of available outgoing messages this scheduler cycle.
// If current tick type is NAK response, then reset tick type. It would be
// bad to let NAK response starve out RX or TXW ticks on the next cycle.
//
// If RX ticks use up all available messages, then we pick up where we left
// off on the next cycle. (This causes some RX tick counts to be missed,
// but that's ok. Precise timing isn't required.)
//
// This scheme allows the following priority for use of outgoing messages:
//
// RX state messages
// TXW state messages
// NAK response (could be many)
//
// New file data on TXA
if (m_tickType != CFDP_TICK_TYPE_TXW_NAK)
{
reset = false;
}
break;
}
}
while (args.cont);
if (!reset)
{
break;
}
}
if (reset)
{
m_tickType = CFDP_TICK_TYPE_RX; // reset tick type
}
}
void Channel::processPlaybackDirectories()
{
U32 i;
U8 playback_count = 0;
for (i = 0; i < CFDP_MAX_COMMANDED_PLAYBACK_DIRECTORIES_PER_CHAN; ++i)
{
this->processPlaybackDirectory(&m_playback[i]);
// Count active playback operations
if (m_playback[i].busy)
{
playback_count++;
}
}
// Update playback counter telemetry
Cfdp::ChannelTelemetry& tlm = m_engine->getChannelTelemetryRef(m_channelId);
tlm.set_playbackCounter(playback_count);
}
void Channel::processPollingDirectories()
{
CfdpPollDir* pd;
U32 i;
U8 poll_count = 0;
Status::T status;
for (i = 0; i < CFDP_MAX_POLLING_DIR_PER_CHAN; ++i)
{
pd = &m_polldir[i];
if (pd->enabled)
{
poll_count++;
if ((pd->pb.busy == false) && (pd->pb.num_ts == 0))
{
if ((pd->intervalTimer.getStatus() != Timer::Status::RUNNING) && (pd->intervalSec > 0))
{
// timer was not set, so set it now
pd->intervalTimer.setTimer(pd->intervalSec);
}
else if (pd->intervalTimer.getStatus() == Timer::Status::EXPIRED)
{
// the timer has expired
status = m_engine->playbackDirInitiate(&pd->pb, pd->srcDir, pd->dstDir, pd->cfdpClass,
Cfdp::Keep::DELETE, m_channelId, pd->priority,
pd->destEid);
if (status != Cfdp::Status::SUCCESS)
{
// error occurred in playback directory, so reset the timer
// an event is sent when initiating playback directory so there is no reason to
// to have another here
pd->intervalTimer.setTimer(pd->intervalSec);
}
}
else
{
pd->intervalTimer.run();
}
}
else
{
// playback is active, so step it
this->processPlaybackDirectory(&pd->pb);
}
}
}
// Update poll counter telemetry
Cfdp::ChannelTelemetry& tlm = m_engine->getChannelTelemetryRef(m_channelId);
tlm.set_pollCounter(poll_count);
}
// ----------------------------------------------------------------------
// Transaction Management
// ----------------------------------------------------------------------
Transaction* Channel::findUnusedTransaction(Direction direction)
{
CListNode* node;
Transaction* txn;
QueueId::T q_index; // initialized below in if
if (m_qs[QueueId::FREE])
{
node = m_qs[QueueId::FREE];
txn = container_of_cpp(node, &Transaction::m_cl_node);
this->removeFromQueue(QueueId::FREE, &txn->m_cl_node);
// now that a transaction is acquired, must also acquire a history slot to go along with it
if (m_qs[QueueId::HIST_FREE])
{
q_index = QueueId::HIST_FREE;
}
else
{
// no free history, so take the oldest one from the channel's history queue
FW_ASSERT(m_qs[QueueId::HIST]);
q_index = QueueId::HIST;
}
txn->m_history = container_of_cpp(m_qs[q_index], &History::cl_node);
this->removeFromQueue(q_index, &txn->m_history->cl_node);
// Indicate that this was freshly pulled from the free list
// notably this state is distinguishable from items still on the free list
txn->m_state = TXN_STATE_INIT;
txn->m_history->dir = direction;
txn->m_chan = this; // Set channel pointer
// Re-initialize the linked list node to clear stale pointers from FREE list
CfdpCListInitNode(&txn->m_cl_node);
}
else
{
txn = NULL;
}
return txn;
}
Transaction* Channel::findTransactionBySequenceNumber(TransactionSeq transaction_sequence_number,
EntityId src_eid)
{
// need to find transaction by sequence number. It will either be the active transaction (front of Q_PEND),
// or on Q_TX or Q_RX. Once a transaction moves to history, then it's done.
//
// Let's put QueueId::RX up front, because most RX packets will be file data PDUs
CfdpTraverseTransSeqArg ctx = {transaction_sequence_number, src_eid, NULL};
CListNode* ptrs[] = {m_qs[QueueId::RX], m_qs[QueueId::PEND], m_qs[QueueId::TXA],
m_qs[QueueId::TXW]};
Transaction* ret = NULL;
for (CListNode* head : ptrs)
{
CfdpCListTraverse(head, Transaction::findBySequenceNumberCallback, &ctx);
if (ctx.txn)
{
ret = ctx.txn;
break;
}
}
return ret;
}
I32 Channel::traverseAllTransactions(CfdpTraverseAllTransactionsFunc fn, void* context)
{
CfdpTraverseAllArg args = {fn, context, 0};
for (I32 queueidx = QueueId::PEND; queueidx <= QueueId::RX; ++queueidx)
{
CfdpCListTraverse(m_qs[queueidx],
[&args](CListNode* node, void*) -> CListTraverseStatus {
Transaction* txn = container_of_cpp(node, &Transaction::m_cl_node);
args.fn(txn, args.context);
++args.counter;
return CLIST_TRAVERSE_CONTINUE;
},
nullptr);
}
return args.counter;
}
void Channel::resetHistory(History* history)
{
this->removeFromQueue(QueueId::HIST, &history->cl_node);
this->insertBackInQueue(QueueId::HIST_FREE, &history->cl_node);
}
// ----------------------------------------------------------------------
// Transaction Queue Management
// ----------------------------------------------------------------------
void Channel::dequeueTransaction(Transaction* txn)
{
FW_ASSERT(txn);
CfdpCListRemove(&m_qs[txn->m_flags.com.q_index], &txn->m_cl_node);
// Update queue depth telemetry
Cfdp::ChannelTelemetry& tlm = m_engine->getChannelTelemetryRef(m_channelId);
switch (txn->m_flags.com.q_index) {
case Cfdp::QueueId::FREE:
tlm.set_queueFree(tlm.get_queueFree() - 1);
break;
case Cfdp::QueueId::TXA:
tlm.set_queueTxActive(tlm.get_queueTxActive() - 1);
break;
case Cfdp::QueueId::TXW:
tlm.set_queueTxWaiting(tlm.get_queueTxWaiting() - 1);
break;
case Cfdp::QueueId::RX:
tlm.set_queueRx(tlm.get_queueRx() - 1);
break;
case Cfdp::QueueId::HIST:
tlm.set_queueHistory(tlm.get_queueHistory() - 1);
break;
case Cfdp::QueueId::PEND:
case Cfdp::QueueId::HIST_FREE:
// PEND and HIST_FREE queues are not tracked in telemetry
break;
default:
FW_ASSERT(0, txn->m_flags.com.q_index);
}
}
void Channel::moveTransaction(Transaction* txn, QueueId::T queue)
{
FW_ASSERT(txn);
Cfdp::ChannelTelemetry& tlm = m_engine->getChannelTelemetryRef(m_channelId);
// Decrement old queue
CfdpCListRemove(&m_qs[txn->m_flags.com.q_index], &txn->m_cl_node);
switch (txn->m_flags.com.q_index) {
case Cfdp::QueueId::FREE:
tlm.set_queueFree(tlm.get_queueFree() - 1);
break;
case Cfdp::QueueId::TXA:
tlm.set_queueTxActive(tlm.get_queueTxActive() - 1);
break;
case Cfdp::QueueId::TXW:
tlm.set_queueTxWaiting(tlm.get_queueTxWaiting() - 1);
break;
case Cfdp::QueueId::RX:
tlm.set_queueRx(tlm.get_queueRx() - 1);
break;
case Cfdp::QueueId::HIST:
tlm.set_queueHistory(tlm.get_queueHistory() - 1);
break;
case Cfdp::QueueId::PEND:
case Cfdp::QueueId::HIST_FREE:
// PEND and HIST_FREE queues are not tracked in telemetry
break;
default:
FW_ASSERT(0, txn->m_flags.com.q_index);
}
// Increment new queue
CfdpCListInsertBack(&m_qs[queue], &txn->m_cl_node);
txn->m_flags.com.q_index = queue;
switch (queue) {
case Cfdp::QueueId::FREE:
tlm.set_queueFree(tlm.get_queueFree() + 1);
break;
case Cfdp::QueueId::TXA:
tlm.set_queueTxActive(tlm.get_queueTxActive() + 1);
break;
case Cfdp::QueueId::TXW:
tlm.set_queueTxWaiting(tlm.get_queueTxWaiting() + 1);
break;
case Cfdp::QueueId::RX:
tlm.set_queueRx(tlm.get_queueRx() + 1);
break;
case Cfdp::QueueId::HIST:
tlm.set_queueHistory(tlm.get_queueHistory() + 1);
break;
case Cfdp::QueueId::PEND:
case Cfdp::QueueId::HIST_FREE:
// PEND and HIST_FREE queues are not tracked in telemetry
break;
default:
FW_ASSERT(0, queue);
}
}
void Channel::freeTransaction(Transaction* txn)
{
// Reset transaction to default state (preserves channel context)
txn->reset();
// Initialize the linked list node for the FREE queue
CfdpCListInitNode(&txn->m_cl_node);
this->insertBackInQueue(QueueId::FREE, &txn->m_cl_node);
}
void Channel::recycleTransaction(Transaction *txn)
{
CListNode **chunklist_head;
QueueId::T hist_destq;
// File should have been closed by the state machine, but if
// it still hanging open at this point, close it now so its not leaked.
// This is not normal/expected so log it if this happens.
if (true == txn->m_fd.isOpen())
{
// CFE_ES_WriteToSysLog("%s(): Closing dangling file handle: %lu\n", __func__, OS_ObjectIdToInteger(txn->fd));
txn->m_fd.close();
}
this->dequeueTransaction(txn); // this makes it "float" (not in any queue)
// this should always be
if (txn->m_history != NULL)
{
if (txn->m_chunks != NULL)
{
chunklist_head = this->getChunkListHead(txn->m_history->dir);
if (chunklist_head != NULL)
{
CfdpCListInsertBack(chunklist_head, &txn->m_chunks->cl_node);
txn->m_chunks = NULL;
}
}
if (txn->m_flags.com.keep_history)
{
// move transaction history to history queue
hist_destq = QueueId::HIST;
}
else
{
hist_destq = QueueId::HIST_FREE;
}
this->insertBackInQueue(hist_destq, &txn->m_history->cl_node);
txn->m_history = NULL;
}
// this wipes it and puts it back onto the list to be found by
// Channel::findUnusedTransaction(). Need to preserve the chan_num
// and keep it associated with this channel, though.
this->freeTransaction(txn);
}
void Channel::insertSortPrio(Transaction* txn, QueueId::T queue)
{
bool insert_back = false;
FW_ASSERT(txn);
// look for proper position on PEND queue for this transaction.
// This is a simple priority sort.
if (!m_qs[queue])
{
// list is empty, so just insert
insert_back = true;
}
else
{
CfdpTraversePriorityArg arg = {NULL, txn->getPriority()};
CfdpCListTraverseR(m_qs[queue], Transaction::prioritySearchCallback, &arg);
if (arg.txn)
{
this->insertAfterInQueue(queue, &arg.txn->m_cl_node, &txn->m_cl_node);
}
else
{
insert_back = true;
}
}
if (insert_back)
{
this->insertBackInQueue(queue, &txn->m_cl_node);
}
txn->m_flags.com.q_index = queue;
}
// ----------------------------------------------------------------------
// Channel State Management
// ----------------------------------------------------------------------
void Channel::decrementCmdTxCounter()
{
FW_ASSERT(m_numCmdTx); // sanity check
--m_numCmdTx;
}
void Channel::clearCurrentIfMatch(Transaction* txn)
{
// Done with this TX transaction
if (this->m_cur == txn)
{
this->m_cur = NULL;
}
}
// ----------------------------------------------------------------------
// Resource Management
// ----------------------------------------------------------------------
CListNode** Channel::getChunkListHead(U8 direction)
{
CListNode** result;
if (direction < DIRECTION_NUM)
{
result = &m_cs[direction];
}
else
{
result = NULL;
}
return result;
}
CfdpChunkWrapper* Channel::findUnusedChunks(Direction dir)
{
CfdpChunkWrapper* ret = NULL;
CListNode* node;
CListNode** chunklist_head;
chunklist_head = this->getChunkListHead(dir);
// this should never be null
FW_ASSERT(chunklist_head);
if (*chunklist_head != NULL)
{
node = CfdpCListPop(chunklist_head);
if (node != NULL)
{
ret = container_of_cpp(node, &CfdpChunkWrapper::cl_node);
}
}
return ret;
}
// ----------------------------------------------------------------------
// Private helper methods
// ----------------------------------------------------------------------
void Channel::processPlaybackDirectory(Playback* pb)
{
Transaction* txn;
char path[MaxFilePathSize];
Os::Directory::Status status;
// either there's no transaction (first one) or the last one was finished, so check for a new one
memset(&path, 0, sizeof(path));
while (pb->diropen && (pb->num_ts < CFDP_NUM_TRANSACTIONS_PER_PLAYBACK))
{
if (pb->pending_file[0] == 0)
{
status = pb->dir.read(path, MaxFilePathSize);
if (status == Os::Directory::NO_MORE_FILES)
{
// TODO BPC: Emit playback success EVR
pb->dir.close();
pb->diropen = false;
break;
}
if (status != Os::Directory::OP_OK)
{
// TODO BPC: emit playback error EVR
pb->dir.close();
pb->diropen = false;
break;
}
strncpy(pb->pending_file, path, sizeof(pb->pending_file) - 1);
pb->pending_file[sizeof(pb->pending_file) - 1] = 0;
}
else
{
txn = this->findUnusedTransaction(DIRECTION_TX);
if (txn == NULL)
{
// while not expected this can certainly happen, because
// rx transactions consume in these as well.
// should not need to do anything special, will come back next tick
break;
}
// Append file name to source/destination folders
txn->m_history->fnames.src_filename = pb->fnames.src_filename;
txn->m_history->fnames.src_filename += "/";
txn->m_history->fnames.src_filename += pb->pending_file;
txn->m_history->fnames.dst_filename = pb->fnames.dst_filename;
txn->m_history->fnames.dst_filename += "/";
txn->m_history->fnames.dst_filename += pb->pending_file;
m_engine->txFileInitiate(txn, pb->cfdp_class, pb->keep, m_channelId, pb->priority,
pb->dest_id);
txn->m_pb = pb;
++pb->num_ts;
pb->pending_file[0] = 0; // continue reading dir
}
}
if (!pb->diropen && !pb->num_ts)
{
// the directory has been exhausted, and there are no more active transactions
// for this playback -- so mark it as not busy
pb->busy = false;
}
}
void Channel::updatePollPbCounted(Playback* pb, int up, U8* counter)
{
if (pb->counted != up)
{
// only handle on state change
pb->counted = !!up; // !! ensure 0 or 1, should be optimized out
if (up)
{
++*counter;
}
else
{
FW_ASSERT(*counter); // sanity check it isn't zero
--*counter;
}
}
}
CListTraverseStatus Channel::cycleTxFirstActive(CListNode* node, void* context)
{
CycleTxArgs* args = static_cast<CycleTxArgs*>(context);
Transaction* txn = container_of_cpp(node, &Transaction::m_cl_node);
CListTraverseStatus ret = CLIST_TRAVERSE_EXIT; // default option is exit traversal
if (txn->m_flags.com.suspended)
{
ret = CLIST_TRAVERSE_CONTINUE; // suspended, so move on to next
}
else
{
FW_ASSERT(txn->m_flags.com.q_index == QueueId::TXA); // huh?
// if no more messages, then chan->m_cur will be set.
// If the transaction sent the last filedata PDU and EOF, it will move itself
// off the active queue. Run until either of these occur.
while (!this->m_cur && txn->m_flags.com.q_index == QueueId::TXA)
{
m_engine->dispatchTx(txn);
}
args->ran_one = 1;
}
return ret;
}
CListTraverseStatus Channel::doTick(CListNode* node, void* context)
{
CListTraverseStatus ret = CLIST_TRAVERSE_CONTINUE; // CLIST_TRAVERSE_CONTINUE means don't tick one, keep looking for cur
TickArgs* args = static_cast<TickArgs*>(context);
Transaction* txn = container_of_cpp(node, &Transaction::m_cl_node);
if (!this->m_cur || (this->m_cur == txn))
{
// found where we left off, so clear that and move on
this->m_cur = NULL;
if (!txn->m_flags.com.suspended)
{
(txn->*args->fn)(&args->cont);
}
// if this->m_cur was set to not-NULL above, then exit early
// NOTE: if channel is frozen, then tick processing won't have been entered.
// so there is no need to check it here
if (this->m_cur)
{
ret = CLIST_TRAVERSE_EXIT;
args->early_exit = true;
}
}
return ret; // don't tick one, keep looking for cur
}
Transaction* Channel::getTransaction(U32 index)
{
FW_ASSERT(index < CFDP_NUM_TRANSACTIONS_PER_CHANNEL);
return &m_transactions[index];
}
History* Channel::getHistory(U32 index)
{
FW_ASSERT(index < CFDP_NUM_HISTORIES_PER_CHANNEL);
return &m_histories[index];
}
} // namespace Cfdp
} // namespace Ccsds
} // namespace Svc