-
Notifications
You must be signed in to change notification settings - Fork 43
/
sample_app.c
554 lines (461 loc) · 18.8 KB
/
sample_app.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
/*******************************************************************************
**
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-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: sample_app.c
**
** Purpose:
** This file contains the source code for the Sample App.
**
*******************************************************************************/
/*
** Include Files:
*/
#include "sample_app_events.h"
#include "sample_app_version.h"
#include "sample_app.h"
#include "sample_table.h"
/* The sample_lib module provides the SAMPLE_Function() prototype */
#include <string.h>
#include "sample_lib.h"
/*
** global data
*/
SAMPLE_AppData_t SAMPLE_AppData;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* SAMPLE_AppMain() -- Application entry point and main process loop */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
void SAMPLE_AppMain( void )
{
int32 status;
/*
** Register the app with Executive services
*/
CFE_ES_RegisterApp();
/*
** Create the first Performance Log entry
*/
CFE_ES_PerfLogEntry(SAMPLE_APP_PERF_ID);
/*
** Perform application specific initialization
** If the Initialization fails, set the RunStatus to
** CFE_ES_RunStatus_APP_ERROR and the App will not enter the RunLoop
*/
status = SAMPLE_AppInit();
if (status != CFE_SUCCESS)
{
SAMPLE_AppData.RunStatus = CFE_ES_RunStatus_APP_ERROR;
}
/*
** SAMPLE Runloop
*/
while (CFE_ES_RunLoop(&SAMPLE_AppData.RunStatus) == true)
{
/*
** Performance Log Exit Stamp
*/
CFE_ES_PerfLogExit(SAMPLE_APP_PERF_ID);
/* Pend on receipt of command packet */
status = CFE_SB_RcvMsg(&SAMPLE_AppData.MsgPtr,
SAMPLE_AppData.CommandPipe,
CFE_SB_PEND_FOREVER);
/*
** Performance Log Entry Stamp
*/
CFE_ES_PerfLogEntry(SAMPLE_APP_PERF_ID);
if (status == CFE_SUCCESS)
{
SAMPLE_ProcessCommandPacket(SAMPLE_AppData.MsgPtr);
}
else
{
CFE_EVS_SendEvent(SAMPLE_PIPE_ERR_EID,
CFE_EVS_EventType_ERROR,
"SAMPLE APP: SB Pipe Read Error, App Will Exit");
SAMPLE_AppData.RunStatus = CFE_ES_RunStatus_APP_ERROR;
}
}
/*
** Performance Log Exit Stamp
*/
CFE_ES_PerfLogExit(SAMPLE_APP_PERF_ID);
CFE_ES_ExitApp(SAMPLE_AppData.RunStatus);
} /* End of SAMPLE_AppMain() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* SAMPLE_AppInit() -- initialization */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
int32 SAMPLE_AppInit( void )
{
int32 status;
SAMPLE_AppData.RunStatus = CFE_ES_RunStatus_APP_RUN;
/*
** Initialize app command execution counters
*/
SAMPLE_AppData.CmdCounter = 0;
SAMPLE_AppData.ErrCounter = 0;
/*
** Initialize app configuration data
*/
SAMPLE_AppData.PipeDepth = SAMPLE_PIPE_DEPTH;
strcpy(SAMPLE_AppData.PipeName, "SAMPLE_CMD_PIPE");
/*
** Initialize event filter table...
*/
SAMPLE_AppData.EventFilters[0].EventID = SAMPLE_STARTUP_INF_EID;
SAMPLE_AppData.EventFilters[0].Mask = 0x0000;
SAMPLE_AppData.EventFilters[1].EventID = SAMPLE_COMMAND_ERR_EID;
SAMPLE_AppData.EventFilters[1].Mask = 0x0000;
SAMPLE_AppData.EventFilters[2].EventID = SAMPLE_COMMANDNOP_INF_EID;
SAMPLE_AppData.EventFilters[2].Mask = 0x0000;
SAMPLE_AppData.EventFilters[3].EventID = SAMPLE_COMMANDRST_INF_EID;
SAMPLE_AppData.EventFilters[3].Mask = 0x0000;
SAMPLE_AppData.EventFilters[4].EventID = SAMPLE_INVALID_MSGID_ERR_EID;
SAMPLE_AppData.EventFilters[4].Mask = 0x0000;
SAMPLE_AppData.EventFilters[5].EventID = SAMPLE_LEN_ERR_EID;
SAMPLE_AppData.EventFilters[5].Mask = 0x0000;
SAMPLE_AppData.EventFilters[6].EventID = SAMPLE_PIPE_ERR_EID;
SAMPLE_AppData.EventFilters[6].Mask = 0x0000;
/*
** Register the events
*/
status = CFE_EVS_Register(SAMPLE_AppData.EventFilters,
SAMPLE_EVENT_COUNTS,
CFE_EVS_EventFilter_BINARY);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Error Registering Events, RC = 0x%08lX\n",
(unsigned long)status);
return ( status );
}
/*
** Initialize housekeeping packet (clear user data area).
*/
CFE_SB_InitMsg(&SAMPLE_AppData.HkBuf.MsgHdr,
SAMPLE_APP_HK_TLM_MID,
sizeof(SAMPLE_AppData.HkBuf),
true);
/*
** Create Software Bus message pipe.
*/
status = CFE_SB_CreatePipe(&SAMPLE_AppData.CommandPipe,
SAMPLE_AppData.PipeDepth,
SAMPLE_AppData.PipeName);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Error creating pipe, RC = 0x%08lX\n",
(unsigned long)status);
return ( status );
}
/*
** Subscribe to Housekeeping request commands
*/
status = CFE_SB_Subscribe(SAMPLE_APP_SEND_HK_MID,
SAMPLE_AppData.CommandPipe);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Error Subscribing to HK request, RC = 0x%08lX\n",
(unsigned long)status);
return ( status );
}
/*
** Subscribe to ground command packets
*/
status = CFE_SB_Subscribe(SAMPLE_APP_CMD_MID,
SAMPLE_AppData.CommandPipe);
if (status != CFE_SUCCESS )
{
CFE_ES_WriteToSysLog("Sample App: Error Subscribing to Command, RC = 0x%08lX\n",
(unsigned long)status);
return ( status );
}
/*
** Register Table(s)
*/
status = CFE_TBL_Register(&SAMPLE_AppData.TblHandles[0],
"SampleTable",
sizeof(SAMPLE_Table_t),
CFE_TBL_OPT_DEFAULT,
SAMPLE_TblValidationFunc);
if ( status != CFE_SUCCESS )
{
CFE_ES_WriteToSysLog("Sample App: Error Registering \
Table, RC = 0x%08lX\n", (unsigned long)status);
return ( status );
}
else
{
status = CFE_TBL_Load(SAMPLE_AppData.TblHandles[0],
CFE_TBL_SRC_FILE,
SAMPLE_TABLE_FILE);
}
CFE_EVS_SendEvent (SAMPLE_STARTUP_INF_EID,
CFE_EVS_EventType_INFORMATION,
"SAMPLE App Initialized. Version %d.%d.%d.%d",
SAMPLE_APP_MAJOR_VERSION,
SAMPLE_APP_MINOR_VERSION,
SAMPLE_APP_REVISION,
SAMPLE_APP_MISSION_REV);
return ( CFE_SUCCESS );
} /* End of SAMPLE_AppInit() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* Name: SAMPLE_ProcessCommandPacket */
/* */
/* Purpose: */
/* This routine will process any packet that is received on the SAMPLE */
/* command pipe. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void SAMPLE_ProcessCommandPacket( CFE_SB_MsgPtr_t Msg )
{
CFE_SB_MsgId_t MsgId;
MsgId = CFE_SB_GetMsgId(Msg);
switch (MsgId)
{
case SAMPLE_APP_CMD_MID:
SAMPLE_ProcessGroundCommand(Msg);
break;
case SAMPLE_APP_SEND_HK_MID:
SAMPLE_ReportHousekeeping((CCSDS_CommandPacket_t *)Msg);
break;
default:
CFE_EVS_SendEvent(SAMPLE_INVALID_MSGID_ERR_EID,
CFE_EVS_EventType_ERROR,
"SAMPLE: invalid command packet,MID = 0x%x",
MsgId);
break;
}
return;
} /* End SAMPLE_ProcessCommandPacket */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* */
/* SAMPLE_ProcessGroundCommand() -- SAMPLE ground commands */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
void SAMPLE_ProcessGroundCommand( CFE_SB_MsgPtr_t Msg )
{
uint16 CommandCode;
CommandCode = CFE_SB_GetCmdCode(Msg);
/*
** Process "known" SAMPLE app ground commands
*/
switch (CommandCode)
{
case SAMPLE_APP_NOOP_CC:
if (SAMPLE_VerifyCmdLength(Msg, sizeof(SAMPLE_Noop_t)))
{
SAMPLE_Noop((SAMPLE_Noop_t *)Msg);
}
break;
case SAMPLE_APP_RESET_COUNTERS_CC:
if (SAMPLE_VerifyCmdLength(Msg, sizeof(SAMPLE_ResetCounters_t)))
{
SAMPLE_ResetCounters((SAMPLE_ResetCounters_t *)Msg);
}
break;
case SAMPLE_APP_PROCESS_CC:
if (SAMPLE_VerifyCmdLength(Msg, sizeof(SAMPLE_Process_t)))
{
SAMPLE_Process((SAMPLE_Process_t *)Msg);
}
break;
/* default case already found during FC vs length test */
default:
CFE_EVS_SendEvent(SAMPLE_COMMAND_ERR_EID,
CFE_EVS_EventType_ERROR,
"Invalid ground command code: CC = %d",
CommandCode);
break;
}
return;
} /* End of SAMPLE_ProcessGroundCommand() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* Name: SAMPLE_ReportHousekeeping */
/* */
/* Purpose: */
/* This function is triggered in response to a task telemetry request */
/* from the housekeeping task. This function will gather the Apps */
/* telemetry, packetize it and send it to the housekeeping task via */
/* the software bus */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_ReportHousekeeping( const CCSDS_CommandPacket_t *Msg )
{
int i;
/*
** Get command execution counters...
*/
SAMPLE_AppData.HkBuf.HkTlm.Payload.CommandErrorCounter = SAMPLE_AppData.ErrCounter;
SAMPLE_AppData.HkBuf.HkTlm.Payload.CommandCounter = SAMPLE_AppData.CmdCounter;
/*
** Send housekeeping telemetry packet...
*/
CFE_SB_TimeStampMsg(&SAMPLE_AppData.HkBuf.MsgHdr);
CFE_SB_SendMsg(&SAMPLE_AppData.HkBuf.MsgHdr);
/*
** Manage any pending table loads, validations, etc.
*/
for (i=0; i<SAMPLE_NUMBER_OF_TABLES; i++)
{
CFE_TBL_Manage(SAMPLE_AppData.TblHandles[i]);
}
return CFE_SUCCESS;
} /* End of SAMPLE_ReportHousekeeping() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* */
/* SAMPLE_Noop -- SAMPLE NOOP commands */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
int32 SAMPLE_Noop( const SAMPLE_Noop_t *Msg )
{
SAMPLE_AppData.CmdCounter++;
CFE_EVS_SendEvent(SAMPLE_COMMANDNOP_INF_EID,
CFE_EVS_EventType_INFORMATION,
"SAMPLE: NOOP command Version %d.%d.%d.%d",
SAMPLE_APP_MAJOR_VERSION,
SAMPLE_APP_MINOR_VERSION,
SAMPLE_APP_REVISION,
SAMPLE_APP_MISSION_REV);
return CFE_SUCCESS;
} /* End of SAMPLE_Noop */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* Name: SAMPLE_ResetCounters */
/* */
/* Purpose: */
/* This function resets all the global counter variables that are */
/* part of the task telemetry. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_ResetCounters( const SAMPLE_ResetCounters_t *Msg )
{
SAMPLE_AppData.CmdCounter = 0;
SAMPLE_AppData.ErrCounter = 0;
CFE_EVS_SendEvent(SAMPLE_COMMANDRST_INF_EID,
CFE_EVS_EventType_INFORMATION,
"SAMPLE: RESET command");
return CFE_SUCCESS;
} /* End of SAMPLE_ResetCounters() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* Name: SAMPLE_Process */
/* */
/* Purpose: */
/* This function Process Ground Station Command */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_Process( const SAMPLE_Process_t *Msg )
{
int32 status;
SAMPLE_Table_t *TblPtr;
const char *TableName = "SAMPLE_APP.SampleTable";
/* Sample Use of Table */
status = CFE_TBL_GetAddress((void *)&TblPtr,
SAMPLE_AppData.TblHandles[0]);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Fail to get table address: 0x%08lx",
(unsigned long)status);
return status;
}
CFE_ES_WriteToSysLog("Sample App: Table Value 1: %d Value 2: %d",
TblPtr->Int1,
TblPtr->Int2);
SAMPLE_GetCrc(TableName);
status = CFE_TBL_ReleaseAddress(SAMPLE_AppData.TblHandles[0]);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Fail to release table address: 0x%08lx",
(unsigned long)status);
return status;
}
/* Invoke a function provided by SAMPLE_LIB */
SAMPLE_Function();
return CFE_SUCCESS;
} /* End of SAMPLE_ProcessCC */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
/* */
/* SAMPLE_VerifyCmdLength() -- Verify command packet length */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
bool SAMPLE_VerifyCmdLength( CFE_SB_MsgPtr_t Msg, uint16 ExpectedLength )
{
bool result = true;
uint16 ActualLength = CFE_SB_GetTotalMsgLength(Msg);
/*
** Verify the command packet length.
*/
if (ExpectedLength != ActualLength)
{
CFE_SB_MsgId_t MessageID = CFE_SB_GetMsgId(Msg);
uint16 CommandCode = CFE_SB_GetCmdCode(Msg);
CFE_EVS_SendEvent(SAMPLE_LEN_ERR_EID,
CFE_EVS_EventType_ERROR,
"Invalid Msg length: ID = 0x%X, CC = %d, Len = %d, Expected = %d",
MessageID,
CommandCode,
ActualLength,
ExpectedLength);
result = false;
SAMPLE_AppData.ErrCounter++;
}
return( result );
} /* End of SAMPLE_VerifyCmdLength() */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* SAMPLE_TblValidationFunc -- Verify contents of First Table */
/* buffer contents */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_TblValidationFunc( void *TblData )
{
int32 ReturnCode = CFE_SUCCESS;
SAMPLE_Table_t *TblDataPtr = (SAMPLE_Table_t *)TblData;
/*
** Sample Table Validation
*/
if (TblDataPtr->Int1 > SAMPLE_TBL_ELEMENT_1_MAX)
{
/* First element is out of range, return an appropriate error code */
ReturnCode = SAMPLE_TABLE_OUT_OF_RANGE_ERR_CODE;
}
return ReturnCode;
} /* End of Sample_TblValidationFunc*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* SAMPLE_GetCrc -- Output CRC */
/* */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void SAMPLE_GetCrc( const char *TableName )
{
int32 status;
uint32 Crc;
CFE_TBL_Info_t TblInfoPtr;
status = CFE_TBL_GetInfo(&TblInfoPtr, TableName);
if (status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("Sample App: Error Getting Table Info");
}
else
{
Crc = TblInfoPtr.Crc;
CFE_ES_WriteToSysLog("Sample App: CRC: 0x%08lX\n\n", (unsigned long)Crc);
}
return;
} /* End of SAMPLE_GetCrc */