forked from nasa/fprime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdSequencerImpl.cpp
More file actions
471 lines (412 loc) · 17.3 KB
/
CmdSequencerImpl.cpp
File metadata and controls
471 lines (412 loc) · 17.3 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
// ======================================================================
// \title CmdSequencerImpl.cpp
// \author Bocchino/Canham
// \brief cpp file for CmdDispatcherComponentBase component implementation class
//
// Copyright (C) 2009-2018 California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
#include <Fw/Com/ComPacket.hpp>
#include <Fw/Types/Assert.hpp>
#include <Fw/Types/SerialBuffer.hpp>
#include <Fw/Types/Serializable.hpp>
#include <Svc/CmdSequencer/CmdSequencerImpl.hpp>
extern "C" {
#include <Utils/Hash/libcrc/lib_crc.h>
}
namespace Svc {
// ----------------------------------------------------------------------
// Construction, initialization, and destruction
// ----------------------------------------------------------------------
CmdSequencerComponentImpl::CmdSequencerComponentImpl(const char* name)
: CmdSequencerComponentBase(name),
m_FPrimeSequence(*this),
m_sequence(&this->m_FPrimeSequence),
m_loadCmdCount(0),
m_cancelCmdCount(0),
m_errorCount(0),
m_runMode(STOPPED),
m_stepMode(AUTO),
m_executedCount(0),
m_totalExecutedCount(0),
m_sequencesCompletedCount(0),
m_timeout(0),
m_blockState(Svc::CmdSequencer_BlockState::NO_BLOCK),
m_opCode(0),
m_cmdSeq(0),
m_join_waiting(false) {}
void CmdSequencerComponentImpl::setTimeout(const U32 timeout) {
this->m_timeout = timeout;
}
void CmdSequencerComponentImpl ::setSequenceFormat(Sequence& sequence) {
this->m_sequence = &sequence;
}
void CmdSequencerComponentImpl ::allocateBuffer(const FwEnumStoreType identifier,
Fw::MemAllocator& allocator,
const FwSizeType bytes) {
this->m_sequence->allocateBuffer(identifier, allocator, bytes);
}
void CmdSequencerComponentImpl ::loadSequence(const Fw::ConstStringBase& fileName) {
FW_ASSERT(this->m_runMode == STOPPED, this->m_runMode);
if (not this->loadFile(fileName)) {
this->m_sequence->clear();
}
}
void CmdSequencerComponentImpl ::deallocateBuffer(Fw::MemAllocator& allocator) {
this->m_sequence->deallocateBuffer(allocator);
}
CmdSequencerComponentImpl::~CmdSequencerComponentImpl() {}
// ----------------------------------------------------------------------
// Handler implementations
// ----------------------------------------------------------------------
void CmdSequencerComponentImpl::CS_RUN_cmdHandler(FwOpcodeType opCode,
U32 cmdSeq,
const Fw::CmdStringArg& fileName,
Svc::CmdSequencer_BlockState block) {
if (not this->requireRunMode(STOPPED)) {
if (m_join_waiting) {
// Inform user previous seq file is not complete
this->log_WARNING_HI_CS_JoinWaitingNotComplete();
}
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
this->m_blockState = block.e;
this->m_cmdSeq = cmdSeq;
this->m_opCode = opCode;
// load commands
if (not this->loadFile(fileName)) {
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
this->m_executedCount = 0;
// Check the step mode. If it is auto, start the sequence
if (AUTO == this->m_stepMode) {
this->m_runMode = RUNNING;
if (this->isConnected_seqStartOut_OutputPort(0)) {
// Create empty SeqArgs as placeholder
// Use parameterized constructor to ensure m_size is initialized to 0
Svc::SeqArgs emptyArgs{0, 0};
this->seqStartOut_out(0, this->m_sequence->getStringFileName(), emptyArgs);
}
this->performCmd_Step();
}
if (Svc::CmdSequencer_BlockState::NO_BLOCK == this->m_blockState) {
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
}
}
void CmdSequencerComponentImpl::CS_VALIDATE_cmdHandler(FwOpcodeType opCode,
U32 cmdSeq,
const Fw::CmdStringArg& fileName) {
if (!this->requireRunMode(STOPPED)) {
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
// load commands
if (not this->loadFile(fileName)) {
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
// clear the buffer
this->m_sequence->clear();
this->log_ACTIVITY_HI_CS_SequenceValid(this->m_sequence->getLogFileName());
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
}
//! Handler for input port seqRunIn
void CmdSequencerComponentImpl::doSequenceRun(const Fw::StringBase& filename) {
if (!this->requireRunMode(STOPPED)) {
this->seqDone_out(0, 0, 0, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
// If file name is non-empty, load a file.
// Empty file name means don't load.
if (filename != "") {
Fw::CmdStringArg cmdStr(filename);
const bool status = this->loadFile(cmdStr);
if (!status) {
this->seqDone_out(0, 0, 0, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
} else if (not this->m_sequence->hasMoreRecords()) {
// No sequence loaded
this->log_WARNING_LO_CS_NoSequenceActive();
this->error();
this->seqDone_out(0, 0, 0, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
this->m_executedCount = 0;
// Check the step mode. If it is auto, start the sequence
if (AUTO == this->m_stepMode) {
this->m_runMode = RUNNING;
if (this->isConnected_seqStartOut_OutputPort(0)) {
// Create empty SeqArgs as placeholder
// Use parameterized constructor to ensure m_size is initialized to 0
Svc::SeqArgs emptyArgs{0, 0};
this->seqStartOut_out(0, this->m_sequence->getStringFileName(), emptyArgs);
}
this->performCmd_Step();
}
this->log_ACTIVITY_HI_CS_PortSequenceStarted(this->m_sequence->getLogFileName());
}
void CmdSequencerComponentImpl::seqRunIn_handler(FwIndexType portNum, const Fw::StringBase& filename, const Svc::SeqArgs& args) {
(void)args; // Suppress unused parameter warning
this->doSequenceRun(filename);
}
void CmdSequencerComponentImpl::seqDispatchIn_handler(FwIndexType portNum, Fw::StringBase& file_name) {
this->doSequenceRun(file_name);
}
void CmdSequencerComponentImpl ::seqCancelIn_handler(const FwIndexType portNum) {
if (RUNNING == this->m_runMode) {
this->performCmd_Cancel();
this->log_ACTIVITY_HI_CS_SequenceCanceled(this->m_sequence->getLogFileName());
++this->m_cancelCmdCount;
this->tlmWrite_CS_CancelCommands(this->m_cancelCmdCount);
} else {
this->log_WARNING_LO_CS_NoSequenceActive();
}
}
void CmdSequencerComponentImpl::CS_CANCEL_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
if (RUNNING == this->m_runMode) {
this->performCmd_Cancel();
this->log_ACTIVITY_HI_CS_SequenceCanceled(this->m_sequence->getLogFileName());
++this->m_cancelCmdCount;
this->tlmWrite_CS_CancelCommands(this->m_cancelCmdCount);
} else {
this->log_WARNING_LO_CS_NoSequenceActive();
}
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
}
void CmdSequencerComponentImpl::CS_JOIN_WAIT_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq) {
// If there is no running sequence do not wait
if (m_runMode != RUNNING) {
this->log_WARNING_LO_CS_NoSequenceActive();
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
return;
} else {
m_join_waiting = true;
Fw::LogStringArg& logFileName = this->m_sequence->getLogFileName();
this->log_ACTIVITY_HI_CS_JoinWaiting(logFileName, m_cmdSeq, m_opCode);
m_cmdSeq = cmdSeq;
m_opCode = opCode;
}
}
// ----------------------------------------------------------------------
// Private helper methods
// ----------------------------------------------------------------------
bool CmdSequencerComponentImpl ::loadFile(const Fw::ConstStringBase& fileName) {
const bool status = this->m_sequence->loadFile(fileName);
if (status) {
Fw::LogStringArg& logFileName = this->m_sequence->getLogFileName();
this->log_ACTIVITY_LO_CS_SequenceLoaded(logFileName);
++this->m_loadCmdCount;
this->tlmWrite_CS_LoadCommands(this->m_loadCmdCount);
}
return status;
}
void CmdSequencerComponentImpl::error() {
++this->m_errorCount;
this->tlmWrite_CS_Errors(m_errorCount);
}
void CmdSequencerComponentImpl::performCmd_Cancel() {
this->m_sequence->reset();
this->m_runMode = STOPPED;
this->m_cmdTimer.clear();
this->m_cmdTimeoutTimer.clear();
this->m_executedCount = 0;
// write sequence done port with error, if connected
if (this->isConnected_seqDone_OutputPort(0)) {
this->seqDone_out(0, 0, 0, Fw::CmdResponse::EXECUTION_ERROR);
}
if (Svc::CmdSequencer_BlockState::BLOCK == this->m_blockState || m_join_waiting) {
// Do not wait if sequence was canceled or a cmd failed
this->m_join_waiting = false;
this->cmdResponse_out(this->m_opCode, this->m_cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
}
this->m_blockState = Svc::CmdSequencer_BlockState::NO_BLOCK;
}
void CmdSequencerComponentImpl ::cmdResponseIn_handler(FwIndexType portNum,
FwOpcodeType opcode,
U32 cmdSeq,
const Fw::CmdResponse& response) {
if (this->m_runMode == STOPPED) {
// Sequencer is not running
this->log_WARNING_HI_CS_UnexpectedCompletion(opcode);
} else {
// clear command timeout
this->m_cmdTimeoutTimer.clear();
if (response != Fw::CmdResponse::OK) {
this->commandError(this->m_executedCount, opcode, response.e);
this->performCmd_Cancel();
} else if (this->m_runMode == RUNNING && this->m_stepMode == AUTO) {
// Auto mode
this->commandComplete(opcode);
if (not this->m_sequence->hasMoreRecords()) {
// No data left
this->m_runMode = STOPPED;
this->sequenceComplete();
} else {
this->performCmd_Step();
}
} else {
// Manual step mode
this->commandComplete(opcode);
if (not this->m_sequence->hasMoreRecords()) {
this->m_runMode = STOPPED;
this->sequenceComplete();
}
}
}
}
void CmdSequencerComponentImpl ::schedIn_handler(FwIndexType portNum, U32 order) {
Fw::Time currTime = this->getTime();
// check to see if a command time is pending
if (this->m_cmdTimer.isExpiredAt(currTime)) {
this->comCmdOut_out(0, m_record.m_command, 0);
this->m_cmdTimer.clear();
// start command timeout timer
this->setCmdTimeout(currTime);
} else if (this->m_cmdTimeoutTimer.isExpiredAt(this->getTime())) { // check for command timeout
this->log_WARNING_HI_CS_SequenceTimeout(m_sequence->getLogFileName(), this->m_executedCount);
// If there is a command timeout, cancel the sequence
this->performCmd_Cancel();
}
}
void CmdSequencerComponentImpl ::CS_START_cmdHandler(FwOpcodeType opcode, U32 cmdSeq) {
if (not this->m_sequence->hasMoreRecords()) {
// No sequence loaded
this->log_WARNING_LO_CS_NoSequenceActive();
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
if (!this->requireRunMode(STOPPED)) {
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
return;
}
this->m_blockState = Svc::CmdSequencer_BlockState::NO_BLOCK;
this->m_runMode = RUNNING;
this->performCmd_Step();
this->log_ACTIVITY_HI_CS_CmdStarted(this->m_sequence->getLogFileName());
if (this->isConnected_seqStartOut_OutputPort(0)) {
// Create empty SeqArgs as placeholder
Svc::SeqArgs emptyArgs{0, 0};
this->seqStartOut_out(0, this->m_sequence->getStringFileName(), emptyArgs);
}
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::OK);
}
void CmdSequencerComponentImpl ::CS_STEP_cmdHandler(FwOpcodeType opcode, U32 cmdSeq) {
if (this->requireRunMode(RUNNING)) {
this->performCmd_Step();
// check for special case where end of sequence entry was encountered
if (this->m_runMode != STOPPED) {
this->log_ACTIVITY_HI_CS_CmdStepped(this->m_sequence->getLogFileName(), this->m_executedCount);
}
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::OK);
} else {
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
}
}
void CmdSequencerComponentImpl ::CS_AUTO_cmdHandler(FwOpcodeType opcode, U32 cmdSeq) {
if (this->requireRunMode(STOPPED)) {
this->m_stepMode = AUTO;
this->log_ACTIVITY_HI_CS_ModeSwitched(CmdSequencer_SeqMode::AUTO);
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::OK);
} else {
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
}
}
void CmdSequencerComponentImpl ::CS_MANUAL_cmdHandler(FwOpcodeType opcode, U32 cmdSeq) {
if (this->requireRunMode(STOPPED)) {
this->m_stepMode = MANUAL;
this->log_ACTIVITY_HI_CS_ModeSwitched(CmdSequencer_SeqMode::STEP);
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::OK);
} else {
this->cmdResponse_out(opcode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
}
}
// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
bool CmdSequencerComponentImpl::requireRunMode(RunMode mode) {
if (this->m_runMode == mode) {
return true;
} else {
this->log_WARNING_HI_CS_InvalidMode();
return false;
}
}
void CmdSequencerComponentImpl ::commandError(const U32 number, const FwOpcodeType opCode, const U32 error) {
this->log_WARNING_HI_CS_CommandError(this->m_sequence->getLogFileName(), number, opCode, error);
this->error();
}
void CmdSequencerComponentImpl::performCmd_Step() {
this->m_sequence->nextRecord(m_record);
// set clock time base and context from value set when sequence was loaded
const Sequence::Header& header = this->m_sequence->getHeader();
this->m_record.m_timeTag.setTimeBase(header.m_timeBase);
this->m_record.m_timeTag.setTimeContext(header.m_timeContext);
Fw::Time currentTime = this->getTime();
switch (this->m_record.m_descriptor) {
case Sequence::Record::END_OF_SEQUENCE:
this->m_runMode = STOPPED;
this->sequenceComplete();
break;
case Sequence::Record::RELATIVE:
this->performCmd_Step_RELATIVE(currentTime);
break;
case Sequence::Record::ABSOLUTE:
this->performCmd_Step_ABSOLUTE(currentTime);
break;
default:
FW_ASSERT(0, m_record.m_descriptor);
}
}
void CmdSequencerComponentImpl::sequenceComplete() {
++this->m_sequencesCompletedCount;
// reset buffer
this->m_sequence->clear();
this->log_ACTIVITY_HI_CS_SequenceComplete(this->m_sequence->getLogFileName());
this->tlmWrite_CS_SequencesCompleted(this->m_sequencesCompletedCount);
this->m_executedCount = 0;
// write sequence done port, if connected
if (this->isConnected_seqDone_OutputPort(0)) {
this->seqDone_out(0, 0, 0, Fw::CmdResponse::OK);
}
if (Svc::CmdSequencer_BlockState::BLOCK == this->m_blockState || m_join_waiting) {
this->cmdResponse_out(this->m_opCode, this->m_cmdSeq, Fw::CmdResponse::OK);
}
m_join_waiting = false;
this->m_blockState = Svc::CmdSequencer_BlockState::NO_BLOCK;
}
void CmdSequencerComponentImpl::commandComplete(const FwOpcodeType opcode) {
this->log_ACTIVITY_LO_CS_CommandComplete(this->m_sequence->getLogFileName(), this->m_executedCount, opcode);
++this->m_executedCount;
++this->m_totalExecutedCount;
this->tlmWrite_CS_CommandsExecuted(this->m_totalExecutedCount);
}
void CmdSequencerComponentImpl ::performCmd_Step_RELATIVE(Fw::Time& currentTime) {
this->m_record.m_timeTag.add(currentTime.getSeconds(), currentTime.getUSeconds());
this->performCmd_Step_ABSOLUTE(currentTime);
}
void CmdSequencerComponentImpl ::performCmd_Step_ABSOLUTE(Fw::Time& currentTime) {
if (currentTime >= this->m_record.m_timeTag) {
this->comCmdOut_out(0, m_record.m_command, 0);
this->setCmdTimeout(currentTime);
} else {
this->m_cmdTimer.set(this->m_record.m_timeTag);
}
}
void CmdSequencerComponentImpl ::pingIn_handler(FwIndexType portNum, /*!< The port number*/
U32 key /*!< Value to return to pinger*/
) {
// send ping response
this->pingOut_out(0, key);
}
void CmdSequencerComponentImpl ::setCmdTimeout(const Fw::Time& currentTime) {
// start timeout timer if enabled and not in step mode
if ((this->m_timeout > 0) and (AUTO == this->m_stepMode)) {
Fw::Time expTime = currentTime;
expTime.add(this->m_timeout, 0);
this->m_cmdTimeoutTimer.set(expTime);
}
}
} // namespace Svc