-
Notifications
You must be signed in to change notification settings - Fork 204
/
cfe_es_api.c
1977 lines (1682 loc) · 58.4 KB
/
cfe_es_api.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
/*
** 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:
** cfe_es_api.c
**
** Purpose:
** This file implements the cFE Executive Services API functions.
**
** References:
** Flight Software Branch C Coding Standard Version 1.0a
** cFE Flight Software Application Developers Guide
**
** Notes:
**
*/
/*
** Required header files.
*/
#include "private/cfe_private.h"
#include "cfe_es.h"
#include "cfe_es_apps.h"
#include "cfe_es_global.h"
#include "cfe_es_events.h"
#include "cfe_es_cds.h"
#include "cfe_es_cds_mempool.h"
#include "cfe_psp.h"
#include "cfe_es_log.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
/*
** Function: CFE_ES_GetResetType
**
** Purpose: Return The Type of reset the cFE had.
** The function will return the start type
** which is CFE_PSP_RST_TYPE_POWERON or CFE_PSP_RST_TYPE_PROCESSOR.
** The sub-type is optional and will be returned if a non-NULL pointer
** is passed in to the function.
*/
int32 CFE_ES_GetResetType(uint32 *ResetSubtypePtr)
{
if ( ResetSubtypePtr != NULL )
{
*ResetSubtypePtr = CFE_ES_ResetDataPtr->ResetVars.ResetSubtype;
}
return(CFE_ES_ResetDataPtr->ResetVars.ResetType);
} /* End of CFE_ES_GetResetType() */
/*
** Function: CFE_ES_ResetCFE
**
** Purpose: Reset the cFE core and all apps.
**
*/
int32 CFE_ES_ResetCFE(uint32 ResetType)
{
int32 ReturnCode;
if ( ResetType == CFE_PSP_RST_TYPE_PROCESSOR )
{
/*
** Increment the processor reset count
*/
CFE_ES_ResetDataPtr->ResetVars.ProcessorResetCount++;
/*
** Before doing a Processor reset, check to see
** if the maximum number has been exceeded
*/
if ( CFE_ES_ResetDataPtr->ResetVars.ProcessorResetCount >
CFE_ES_ResetDataPtr->ResetVars.MaxProcessorResetCount )
{
CFE_ES_WriteToSysLog("POWER ON RESET due to max proc resets (Commanded).\n");
/*
** Log the reset in the ER Log. The log will be wiped out, but it's good to have
** the entry just in case something fails.
*/
CFE_ES_WriteToERLog(CFE_ES_LogEntryType_CORE, CFE_PSP_RST_TYPE_POWERON,
CFE_PSP_RST_SUBTYPE_RESET_COMMAND,
"POWER ON RESET due to max proc resets (Commanded).", NULL,0 );
/*
** Call the BSP reset routine
*/
CFE_PSP_Restart(CFE_PSP_RST_TYPE_POWERON);
}
else
{
CFE_ES_WriteToSysLog("PROCESSOR RESET called from CFE_ES_ResetCFE (Commanded).\n");
/*
** Update the reset variables
*/
CFE_ES_ResetDataPtr->ResetVars.ES_CausedReset = true;
/*
** Log the reset in the ER Log
*/
CFE_ES_WriteToERLog(CFE_ES_LogEntryType_CORE, CFE_PSP_RST_TYPE_PROCESSOR,
CFE_PSP_RST_SUBTYPE_RESET_COMMAND,
"PROCESSOR RESET called from CFE_ES_ResetCFE (Commanded).", NULL,0 );
/*
** Call the BSP reset routine
*/
CFE_PSP_Restart(CFE_PSP_RST_TYPE_PROCESSOR);
} /* end if */
/*
** If the BSP routine is not implemented,
** it will return.
*/
ReturnCode = CFE_ES_NOT_IMPLEMENTED;
}
else if ( ResetType == CFE_PSP_RST_TYPE_POWERON )
{
CFE_ES_WriteToSysLog("POWERON RESET called from CFE_ES_ResetCFE (Commanded).\n");
/*
** Log the reset in the ER Log. The log will be wiped out, but it's good to have
** the entry just in case something fails.
*/
CFE_ES_WriteToERLog(CFE_ES_LogEntryType_CORE, CFE_PSP_RST_TYPE_POWERON,
CFE_PSP_RST_SUBTYPE_RESET_COMMAND,
"POWERON RESET called from CFE_ES_ResetCFE (Commanded).", NULL,0 );
/*
** Call the BSP reset routine
*/
CFE_PSP_Restart(CFE_PSP_RST_TYPE_POWERON);
/*
** If the BSP routine is not implemented,
** it will return.
*/
ReturnCode = CFE_ES_NOT_IMPLEMENTED;
}
else
{
CFE_ES_WriteToSysLog("ES ResetCFE: Invalid Reset Type: %d.\n",(int)ResetType);
ReturnCode = CFE_ES_BAD_ARGUMENT;
}
return(ReturnCode);
} /* End of CFE_ES_ResetCFE() */
/*
* Function: CFE_ES_SetAppState
*
* Purpose: Internal ES function to set the state of an app. This performs
* any necessary internal housekeeping related to the state change,
* and provides a single place to keep logic for state entry/exit.
*
* The typical progression of APP states:
*
* UNDEFINED -> EARLY_INIT -> LATE_INIT -> RUNNING -> WAITING
*
* State can go to "STOPPED" (the last state) from any state. This is used for error conditions.
*
* NOTE: This is an ES internal function and must only be called when the ES global state is already locked.
*
*/
void CFE_ES_SetAppState(uint32 AppID, uint32 TargetState)
{
CFE_ES_AppRecord_t *AppState = &CFE_ES_Global.AppTable[AppID];
if (TargetState >= CFE_ES_AppState_MAX)
{
/* Caller error - invalid state */
return;
}
/*
* States should not move backward under normal circumstances.
*
* This relational comparison depends on the app states being defined in logical order
* (they should be)
*/
if (TargetState != CFE_ES_AppState_UNDEFINED && AppState->AppState >= TargetState)
{
/* Do nothing */
return;
}
AppState->AppState = TargetState;
}
/*
** Function: CFE_ES_RestartApp
**
** Purpose: Restart a single cFE App.
**
*/
int32 CFE_ES_RestartApp(uint32 AppID)
{
int32 ReturnCode = CFE_SUCCESS;
if ( AppID < CFE_PLATFORM_ES_MAX_APPLICATIONS )
{
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if ( CFE_ES_Global.AppTable[AppID].Type == CFE_ES_AppType_CORE )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_DeleteApp: Cannot Restart a CORE Application: %s.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
ReturnCode = CFE_ES_ERR_APPID;
}
else if ( CFE_ES_Global.AppTable[AppID].AppState != CFE_ES_AppState_RUNNING )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_RestartApp: Cannot Restart Application %s, It is not running.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
ReturnCode = CFE_ES_ERR_APPID;
}
else
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_RestartApp: Restart Application %s Initiated\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_RESTART;
CFE_ES_SetAppState(AppID, CFE_ES_AppState_WAITING);
CFE_ES_Global.AppTable[AppID].ControlReq.AppTimer = CFE_PLATFORM_ES_APP_KILL_TIMEOUT;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
}
else /* App ID is not valid */
{
ReturnCode = CFE_ES_ERR_APPID;
CFE_ES_WriteToSysLog("CFE_ES_RestartApp: Invalid Application ID received, RC = 0x%08X\n",
(unsigned int) ReturnCode);
} /* end if */
return(ReturnCode);
} /* End of CFE_ES_RestartApp() */
/*
** Function: CFE_ES_ReloadApp
**
** Purpose: Reload a single cFE App.
**
*/
int32 CFE_ES_ReloadApp(uint32 AppID, const char *AppFileName)
{
int32 ReturnCode = CFE_SUCCESS;
os_fstat_t FileStatus;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if ( CFE_ES_Global.AppTable[AppID].Type == CFE_ES_AppType_CORE )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_DeleteApp: Cannot Reload a CORE Application: %s.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
ReturnCode = CFE_ES_ERR_APPID;
}
else if ( CFE_ES_Global.AppTable[AppID].AppState != CFE_ES_AppState_RUNNING )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ReloadApp: Cannot Reload Application %s, It is not running.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
ReturnCode = CFE_ES_ERR_APPID;
}
else
{
/*
** Check to see if the file exists
*/
if (OS_stat(AppFileName, &FileStatus) == OS_SUCCESS)
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_ReloadApp: Reload Application %s Initiated. New filename = %s\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name, AppFileName);
strncpy((char *)CFE_ES_Global.AppTable[AppID].StartParams.FileName, AppFileName, OS_MAX_PATH_LEN);
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_RELOAD;
CFE_ES_SetAppState(AppID, CFE_ES_AppState_WAITING);
CFE_ES_Global.AppTable[AppID].ControlReq.AppTimer = CFE_PLATFORM_ES_APP_KILL_TIMEOUT;
}
else
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ReloadApp: Cannot Reload Application %s, File %s does not exist.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name,
AppFileName);
ReturnCode = CFE_ES_FILE_IO_ERR;
}
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(ReturnCode);
} /* End of CFE_ES_ReloadApp() */
/*
** Function: CFE_ES_DeleteApp
**
** Purpose: Delete a cFE App.
**
*/
int32 CFE_ES_DeleteApp(uint32 AppID)
{
int32 ReturnCode = CFE_SUCCESS;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if ( CFE_ES_Global.AppTable[AppID].Type == CFE_ES_AppType_CORE )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_DeleteApp: Cannot Delete a CORE Application: %s.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
ReturnCode = CFE_ES_ERR_APPID;
}
else if ( CFE_ES_Global.AppTable[AppID].AppState != CFE_ES_AppState_RUNNING )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_DeleteApp: Cannot Delete Application %s, It is not running.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
ReturnCode = CFE_ES_ERR_APPID;
}
else
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_DeleteApp: Delete Application %s Initiated\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_DELETE;
CFE_ES_SetAppState(AppID, CFE_ES_AppState_WAITING);
CFE_ES_Global.AppTable[AppID].ControlReq.AppTimer = CFE_PLATFORM_ES_APP_KILL_TIMEOUT;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(ReturnCode);
} /* End of CFE_ES_DeleteApp() */
/*
** Function: CFE_ES_ExitApp
**
** Purpose: Exit a cFE App.
**
*/
void CFE_ES_ExitApp(uint32 ExitStatus)
{
int32 ReturnCode;
uint32 AppID;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
* This should only be called with an ExitStatus of either APP_EXIT or APP_ERROR.
* Anything else is invalid and indicates a bug in the caller. In particular,
* if called with APP_RUN then this creates an invalid state (see bug #58).
*/
if (ExitStatus != CFE_ES_RunStatus_APP_EXIT &&
ExitStatus != CFE_ES_RunStatus_APP_ERROR &&
ExitStatus != CFE_ES_RunStatus_CORE_APP_INIT_ERROR &&
ExitStatus != CFE_ES_RunStatus_CORE_APP_RUNTIME_ERROR)
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ExitApp: Called with invalid status (%u).\n",
(unsigned int)ExitStatus);
/* revert to the ERROR status */
ExitStatus = CFE_ES_RunStatus_APP_ERROR;
}
ReturnCode = CFE_ES_GetAppIDInternal(&AppID);
if ( ReturnCode == CFE_SUCCESS )
{
/* Set the status in the global table */
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = ExitStatus;
/*
** Check to see if the App is an external cFE App.
*/
if ( CFE_ES_Global.AppTable[AppID].Type == CFE_ES_AppType_CORE )
{
/*
** A core app should only call this function with one of two ExitStatus codes.
*/
if ( ExitStatus == CFE_ES_RunStatus_CORE_APP_INIT_ERROR )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ExitApp: CORE Application %s Had an Init Error.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
/*
** Unlock the ES Shared data before calling ResetCFE
*/
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
** Do a Processor Reset the cFE
*/
ReturnCode = CFE_ES_ResetCFE(CFE_PSP_RST_TYPE_PROCESSOR);
/*
** The CFE_ES_ResetCFE function does not normally return,
** but it may return during unit testing. If it does,
** log the return code (even if it claims CFE_SUCCESS).
*/
CFE_ES_WriteToSysLog("CFE_ES_ExitApp: CORE Application Init Error Processor Reset, RC = 0x%08X\n",
(unsigned int) ReturnCode);
return;
}
else if ( ExitStatus == CFE_ES_RunStatus_CORE_APP_RUNTIME_ERROR )
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ExitApp: CORE Application %s Had a Runtime Error.\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
/*
** Unlock the ES Shared data before killing the main task
*/
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
** Exit this task
*/
OS_TaskExit();
/*
** Code will not return, except under unit test
*/
return;
}
else
{
CFE_ES_SysLogWrite_Unsync ("CFE_ES_ExitApp, Cannot Exit CORE Application %s\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name );
}
}
else /* It is an external App */
{
CFE_ES_SysLogWrite_Unsync ("Application %s called CFE_ES_ExitApp\n",
CFE_ES_Global.AppTable[AppID].StartParams.Name);
CFE_ES_SetAppState(AppID, CFE_ES_AppState_STOPPED);
/*
** Unlock the ES Shared data before suspending the app
*/
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
** Suspend the Application until ES kills it.
** It might be better to have a way of suspending the app in the OS
*/
while(1)
{
OS_TaskDelay(500);
}
} /* end if */
} /* end if ReturnCode == CFE_SUCCESS */
CFE_ES_UnlockSharedData(__func__,__LINE__);
} /* End of CFE_ES_ExitApp() */
/*
** Function: CFE_ES_RunLoop
**
** Purpose: Check the Run Status of an Application.
**
*/
bool CFE_ES_RunLoop(uint32 *RunStatus)
{
bool ReturnCode;
int32 Status;
uint32 AppID;
uint32 TaskID;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Get App ID
*/
Status = CFE_ES_GetAppIDInternal(&AppID);
if ( Status == CFE_SUCCESS )
{
/*
** Get the Task ID for the main task
*/
OS_ConvertToArrayIndex(CFE_ES_Global.AppTable[AppID].TaskInfo.MainTaskId, &TaskID);
/*
** Increment the execution counter for the main task
*/
CFE_ES_Global.TaskTable[TaskID].ExecutionCounter++;
/*
** Validate RunStatus
*/
if ( *RunStatus == CFE_ES_RunStatus_APP_RUN || *RunStatus == CFE_ES_RunStatus_APP_EXIT || *RunStatus == CFE_ES_RunStatus_APP_ERROR )
{
/*
** Look up the system control request
*/
if ( *RunStatus == CFE_ES_RunStatus_APP_RUN )
{
/*
** App state must be RUNNING (no-op if already set to running)
*/
CFE_ES_SetAppState(AppID, CFE_ES_AppState_RUNNING);
if ( CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest != CFE_ES_RunStatus_APP_RUN )
{
/*
** We have an external request to stop
*/
ReturnCode = false;
}
else
{
/*
** Everything is OK
*/
ReturnCode = true;
}
}
else
{
/*
** Application wants to exit, so let it
*/
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = *RunStatus;
ReturnCode = false;
}
}
else
{
/*
** Not a supported RunStatus code, the app will abort
*/
CFE_ES_SysLogWrite_Unsync("CFE_ES_RunLoop Error: Invalid RunStatus:%d!\n",(int)(*RunStatus));
CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest = CFE_ES_RunStatus_APP_ERROR;
ReturnCode = false;
} /* End if *RunStatus == CFE_ES_RunStatus_APP_RUN .. */
}
else
{
/*
** Cannot do anything without the AppID
*/
CFE_ES_SysLogWrite_Unsync("CFE_ES_RunLoop Error: Cannot get AppID for the caller: RC = %08X\n",(unsigned int)Status);
ReturnCode = false;
} /* end if Status == CFE_SUCCESS */
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(ReturnCode);
} /* End of CFE_ES_RunLoop() */
/*
** Function: CFE_ES_WaitForSystemState
**
** Purpose: Called by applications that need to ensure that all other apps are running
** before completing their initialization process.
**
** This will also mark the calling task itself as "ready"
**
*/
int32 CFE_ES_WaitForSystemState(uint32 MinSystemState, uint32 TimeOutMilliseconds)
{
int32 Status;
uint32 AppID;
uint32 RequiredAppState;
uint32 WaitTime;
uint32 WaitRemaining;
/*
* An application calling CFE_ES_WaitForStartupSync() is assumed to have
* completed its own initialization up to the point it is waiting for.
*
* Determine the implicit app state based on the system state it is indicating
*/
CFE_ES_LockSharedData(__func__,__LINE__);
Status = CFE_ES_GetAppIDInternal(&AppID);
if ( Status == CFE_SUCCESS )
{
RequiredAppState = CFE_ES_AppState_EARLY_INIT;
/*
* If a core app waits for anything above "CORE_READY" then it is assumed to be RUNNING
*
* External apps have additional finer-grained sync:
* - SYSTEM_STATE_APPS_INIT requires that all apps are at least up to LATE_INIT
* - SYSTEM_STATE_OPERATIONAL requires that all apps are RUNNING
* - SYSTEM_STATE_SHUTDOWN requires that all apps are STOPPED (in concept anyway)
*/
if (CFE_ES_Global.AppTable[AppID].Type == CFE_ES_AppType_CORE)
{
if (MinSystemState >= CFE_ES_SystemState_CORE_READY)
{
RequiredAppState = CFE_ES_AppState_RUNNING;
}
}
else if (MinSystemState >= CFE_ES_SystemState_SHUTDOWN)
{
RequiredAppState = CFE_ES_AppState_STOPPED;
}
else if (MinSystemState >= CFE_ES_SystemState_OPERATIONAL)
{
RequiredAppState = CFE_ES_AppState_RUNNING;
}
else if (MinSystemState >= CFE_ES_SystemState_APPS_INIT)
{
RequiredAppState = CFE_ES_AppState_LATE_INIT;
}
/*
* NOTE -- a call to "CFE_ES_WaitForSystemState()" implies that the calling app MUST also
* be in the requisite state. This is hooked into here to avoid needing to update all existing
* apps to add an explicit state change call, but it makes sense because if this was not done an app could
* be waiting for itself (which will always time out).
*/
CFE_ES_SetAppState(AppID, RequiredAppState);
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* Do the actual delay loop.
*
* This is only dependent on the main (startup) task updating the global variable
* to be at least the state requested.
*/
WaitRemaining = TimeOutMilliseconds;
while (CFE_ES_Global.SystemState < MinSystemState)
{
/* TBD: Very Crude timing here, but not sure if it matters,
* as this is only done during startup, not real work */
if (WaitRemaining > CFE_PLATFORM_ES_STARTUP_SYNC_POLL_MSEC)
{
WaitTime = CFE_PLATFORM_ES_STARTUP_SYNC_POLL_MSEC;
}
else if (WaitRemaining > 0)
{
WaitTime = WaitRemaining;
}
else
{
Status = CFE_ES_OPERATION_TIMED_OUT;
break;
}
OS_TaskDelay(WaitTime);
WaitRemaining -= WaitTime;
}
return Status;
} /* End of CFE_ES_WaitForSystemState() */
/*
** Function: CFE_ES_WaitForStartupSync
**
** Purpose: Called by applications that need to ensure that all other apps are running
** before completing their initialization process.
**
** This API is required for compatibility with existing applications. It
** is equivalent to calling CFE_ES_WaitForSystemState(CFE_ES_SystemState_OPERATIONAL)
** for apps that don't require any finer-grained synchronization.
**
*/
void CFE_ES_WaitForStartupSync(uint32 TimeOutMilliseconds)
{
CFE_ES_WaitForSystemState(CFE_ES_SystemState_OPERATIONAL, TimeOutMilliseconds);
}
/*
** Function: CFE_ES_RegisterApp
**
** Purpose: Register a new Application with the cFE. This
** function must be called in a cFE app before
** any other cFE function.
**
*/
int32 CFE_ES_RegisterApp(void)
{
int32 Result = CFE_SUCCESS;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Register the task
*/
Result = OS_TaskRegister();
if (Result == OS_SUCCESS)
{
Result = CFE_SUCCESS;
}
else
{
/*
** Cannot create a syslog entry here because it requires the task to
** be registered
*/
Result = CFE_ES_ERR_APP_REGISTER;
}
/*
** Set the default exception environment
*/
CFE_PSP_SetDefaultExceptionEnvironment();
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(Result);
} /* End of CFE_ES_RegisterApp() */
/*
** Function: CFE_ES_GetAppIDByName
**
** Purpose: Given a valid App Name, return the
** Application ID.
**
*/
int32 CFE_ES_GetAppIDByName(uint32 *AppIdPtr, const char *AppName)
{
int32 Result = CFE_ES_ERR_APPNAME;
uint32 i;
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Search the ES Application table for an app with a matching name.
*/
for ( i = 0; i < CFE_PLATFORM_ES_MAX_APPLICATIONS; i++ )
{
if ( CFE_ES_Global.AppTable[i].AppState != CFE_ES_AppState_UNDEFINED )
{
if ( strncmp(AppName, (char *)CFE_ES_Global.AppTable[i].StartParams.Name, OS_MAX_API_NAME) == 0 )
{
*AppIdPtr = i;
Result = CFE_SUCCESS;
break;
}
}
} /* end for */
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(Result);
} /* End of CFE_ES_GetAppIDByName() */
/*
** Function: CFE_ES_GetAppID
**
** Purpose: Return the Caller's cFE Application ID.
**
*/
int32 CFE_ES_GetAppID(uint32 *AppIdPtr)
{
int32 Result;
CFE_ES_LockSharedData(__func__,__LINE__);
Result = CFE_ES_GetAppIDInternal(AppIdPtr);
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(Result);
} /* End of CFE_ES_GetAppID() */
/*
** Function: CFE_ES_GetAppName
**
** Purpose: Given a valid AppID, return the
** Application name string.
**
*/
int32 CFE_ES_GetAppName(char *AppName, uint32 AppId, uint32 BufferLength)
{
int32 Result;
CFE_ES_LockSharedData(__func__,__LINE__);
if ( AppId < CFE_PLATFORM_ES_MAX_APPLICATIONS )
{
if ( CFE_ES_Global.AppTable[AppId].AppState != CFE_ES_AppState_UNDEFINED )
{
strncpy(AppName, (char *)CFE_ES_Global.AppTable[AppId].StartParams.Name, BufferLength);
AppName[BufferLength - 1] = '\0';
Result = CFE_SUCCESS;
}
else
{
Result = CFE_ES_ERR_APPID;
}
}
else
{
Result = CFE_ES_ERR_APPID;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* Appeasement for poorly-behaved callers:
*
* There is a fair amount of existing code that calls this function but
* does not correctly check the return code. Although these callers are
* incorrect, this at least ensures that if the output buffer will be
* appropriately null terminated (empty string) in the failure case.
*/
if (Result != CFE_SUCCESS)
{
AppName[0] = 0;
}
return(Result);
} /* End of CFE_ES_GetAppName() */
/*
** Function: CFE_ES_GetAppInfo
**
** Purpose: Get Application Information given a specified App ID
**
*/
int32 CFE_ES_GetAppInfo(CFE_ES_AppInfo_t *AppInfo, uint32 AppId)
{
int32 ReturnCode = CFE_SUCCESS;
if ( AppInfo != 0 )
{
if ( AppId < CFE_PLATFORM_ES_MAX_APPLICATIONS )
{
if ( CFE_ES_Global.AppTable[AppId].AppState != CFE_ES_AppState_UNDEFINED )
{
CFE_ES_GetAppInfoInternal(AppId, AppInfo);
ReturnCode = CFE_SUCCESS;
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Not Active: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Exceeds CFE_ES_APPLICATION_MAX: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: Invalid Parameter ( Null Pointer )\n");
ReturnCode = CFE_ES_ERR_BUFFER;
}
return(ReturnCode);
} /* End of CFE_ES_GetAppInfo() */
/*
** Function: CFE_ES_GetTaskInfo
**
** Purpose: Get Task Information given a specified Task ID
**
*/
int32 CFE_ES_GetTaskInfo(CFE_ES_TaskInfo_t *TaskInfo, uint32 OSTaskId)
{
int32 ReturnCode = CFE_SUCCESS;
uint32 TaskId;
CFE_ES_LockSharedData(__func__,__LINE__);
if (OS_ConvertToArrayIndex(OSTaskId, &TaskId) != OS_SUCCESS || TaskId >= OS_MAX_TASKS)
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_GetTaskInfo: Task ID Not Valid: %u\n",(unsigned int)OSTaskId);
ReturnCode = CFE_ES_ERR_TASKID;
}
else if ( CFE_ES_Global.TaskTable[TaskId].RecordUsed == true )
{
/*
** Get the Application ID and Task Name
*/
TaskInfo->AppId = CFE_ES_Global.TaskTable[TaskId].AppId;
strncpy((char *)TaskInfo->TaskName,
(char *)CFE_ES_Global.TaskTable[TaskId].TaskName,OS_MAX_API_NAME);
TaskInfo->TaskName[OS_MAX_API_NAME - 1] = '\0';
/*
** Get the Application Name
*/
if ( CFE_ES_Global.AppTable[TaskInfo->AppId].AppState != CFE_ES_AppState_UNDEFINED )
{
strncpy((char *)TaskInfo->AppName,
(char *)CFE_ES_Global.AppTable[TaskInfo->AppId].StartParams.Name,
OS_MAX_API_NAME);
TaskInfo->AppName[OS_MAX_API_NAME - 1] = '\0';
/*
** Store away the Task ID ( for the QueryAllTasks Cmd )
*/
TaskInfo->TaskId = OSTaskId;
/*
** Get the Execution counter for the task
*/
TaskInfo->ExecutionCounter = CFE_ES_Global.TaskTable[TaskId].ExecutionCounter;
ReturnCode = CFE_SUCCESS;
}
else
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_GetTaskInfo: Task ID:%u Parent App ID:%d not Active.\n",
(unsigned int)OSTaskId,(int)TaskInfo->AppId);
ReturnCode = CFE_ES_ERR_TASKID;
}
}
else
{
CFE_ES_SysLogWrite_Unsync("CFE_ES_GetTaskInfo: Task ID Not Active: %u\n",(unsigned int)OSTaskId);
ReturnCode = CFE_ES_ERR_TASKID;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(ReturnCode);
} /* End of CFE_ES_GetAppName() */
/*
** Function: CFE_ES_CreateChildTask
**
** Purpose: Create a Child task for the Application.
**
*/
int32 CFE_ES_CreateChildTask(uint32 *TaskIdPtr,
const char *TaskName,
CFE_ES_ChildTaskMainFuncPtr_t FunctionPtr,
uint32 *StackPtr,
uint32 StackSize,
uint32 Priority,
uint32 Flags)
{
int32 Result;
uint32 AppId= 0xFFFFFFFF;
int32 ReturnCode;
uint32 TaskId;
uint32 ParentTaskId;
/*