-
Notifications
You must be signed in to change notification settings - Fork 203
/
cfe_es_api.c
2212 lines (1932 loc) · 70.9 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
/************************************************************************
* NASA Docket No. GSC-18,719-1, and identified as “core Flight System: Bootes”
*
* Copyright (c) 2020 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 "cfe_es_module_all.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_GetResetType(uint32 *ResetSubtypePtr)
{
if (ResetSubtypePtr != NULL)
{
*ResetSubtypePtr = CFE_ES_Global.ResetDataPtr->ResetVars.ResetSubtype;
}
return CFE_ES_Global.ResetDataPtr->ResetVars.ResetType;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_ResetCFE(uint32 ResetType)
{
int32 ReturnCode;
if (ResetType == CFE_PSP_RST_TYPE_PROCESSOR)
{
/*
** Increment the processor reset count
*/
CFE_ES_Global.ResetDataPtr->ResetVars.ProcessorResetCount++;
/*
** Before doing a Processor reset, check to see
** if the maximum number has been exceeded
*/
if (CFE_ES_Global.ResetDataPtr->ResetVars.ProcessorResetCount >
CFE_ES_Global.ResetDataPtr->ResetVars.MaxProcessorResetCount)
{
CFE_ES_WriteToSysLog("%s: POWER ON RESET due to max proc resets (Commanded).\n", __func__);
/*
** 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).");
/*
** Call the BSP reset routine
*/
CFE_PSP_Restart(CFE_PSP_RST_TYPE_POWERON);
}
else
{
CFE_ES_WriteToSysLog("%s: PROCESSOR RESET called from CFE_ES_ResetCFE (Commanded).\n", __func__);
/*
** Update the reset variables
*/
CFE_ES_Global.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).");
/*
** Call the BSP reset routine
*/
CFE_PSP_Restart(CFE_PSP_RST_TYPE_PROCESSOR);
}
/*
** 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("%s: POWERON RESET called from CFE_ES_ResetCFE (Commanded).\n", __func__);
/*
** 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).");
/*
** 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("%s: Invalid Reset Type: %d.\n", __func__, (int)ResetType);
ReturnCode = CFE_ES_BAD_ARGUMENT;
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_RestartApp(CFE_ES_AppId_t AppID)
{
int32 ReturnCode = CFE_SUCCESS;
os_fstat_t FileStatus;
CFE_ES_AppRecord_t *AppRecPtr;
AppRecPtr = CFE_ES_LocateAppRecordByID(AppID);
if (AppRecPtr != NULL)
{
CFE_ES_LockSharedData(__func__, __LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if (AppRecPtr->Type == CFE_ES_AppType_CORE)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Restart a CORE Application: %s.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else if (AppRecPtr->AppState != CFE_ES_AppState_RUNNING)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Restart Application %s, It is not running.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else
{
/*
** Check to see if the file exists
*/
if (OS_stat(AppRecPtr->StartParams.BasicInfo.FileName, &FileStatus) == OS_SUCCESS)
{
CFE_ES_SysLogWrite_Unsync("%s: Restart Application %s Initiated\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
AppRecPtr->ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_RESTART;
}
else
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Restart Application %s, File %s does not exist.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr),
AppRecPtr->StartParams.BasicInfo.FileName);
ReturnCode = CFE_ES_FILE_IO_ERR;
}
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
}
else /* App ID is not valid */
{
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
CFE_ES_WriteToSysLog("%s: Invalid Application ID received, AppID = %lu\n", __func__,
CFE_RESOURCEID_TO_ULONG(AppID));
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_ReloadApp(CFE_ES_AppId_t AppID, const char *AppFileName)
{
int32 ReturnCode = CFE_SUCCESS;
os_fstat_t FileStatus;
CFE_ES_AppRecord_t *AppRecPtr = CFE_ES_LocateAppRecordByID(AppID);
if (AppRecPtr != NULL)
{
CFE_ES_LockSharedData(__func__, __LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if (AppRecPtr->Type == CFE_ES_AppType_CORE)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Reload a CORE Application: %s.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else if (AppRecPtr->AppState != CFE_ES_AppState_RUNNING)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Reload Application %s, It is not running.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else
{
/*
** Check to see if the file exists
*/
if (OS_stat(AppFileName, &FileStatus) == OS_SUCCESS)
{
CFE_ES_SysLogWrite_Unsync("%s: Reload Application %s Initiated. New filename = %s\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr), AppFileName);
strncpy(AppRecPtr->StartParams.BasicInfo.FileName, AppFileName,
sizeof(AppRecPtr->StartParams.BasicInfo.FileName) - 1);
AppRecPtr->StartParams.BasicInfo.FileName[sizeof(AppRecPtr->StartParams.BasicInfo.FileName) - 1] = 0;
AppRecPtr->ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_RELOAD;
}
else
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Reload Application %s, File %s does not exist.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr), AppFileName);
ReturnCode = CFE_ES_FILE_IO_ERR;
}
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
}
else /* App ID is not valid */
{
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
CFE_ES_WriteToSysLog("%s: Invalid Application ID received, AppID = %lu\n", __func__,
CFE_RESOURCEID_TO_ULONG(AppID));
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_DeleteApp(CFE_ES_AppId_t AppID)
{
int32 ReturnCode = CFE_SUCCESS;
CFE_ES_AppRecord_t *AppRecPtr = CFE_ES_LocateAppRecordByID(AppID);
if (AppRecPtr != NULL)
{
CFE_ES_LockSharedData(__func__, __LINE__);
/*
** Check to see if the App is an external cFE App.
*/
if (AppRecPtr->Type == CFE_ES_AppType_CORE)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Delete a CORE Application: %s.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else if (AppRecPtr->AppState != CFE_ES_AppState_RUNNING)
{
CFE_ES_SysLogWrite_Unsync("%s: Cannot Delete Application %s, It is not running.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else
{
CFE_ES_SysLogWrite_Unsync("%s: Delete Application %s Initiated\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
AppRecPtr->ControlReq.AppControlRequest = CFE_ES_RunStatus_SYS_DELETE;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
}
else /* App ID is not valid */
{
ReturnCode = CFE_ES_ERR_RESOURCEID_NOT_VALID;
CFE_ES_WriteToSysLog("%s: Invalid Application ID received, AppID = %lu\n", __func__,
CFE_RESOURCEID_TO_ULONG(AppID));
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_ES_ExitApp(uint32 ExitStatus)
{
int32 ReturnCode;
CFE_ES_AppRecord_t *AppRecPtr;
CFE_ES_LockSharedData(__func__, __LINE__);
/*
* This should only be called with a valid ExitStatus, anything else is invalid
* and indicates a bug in the caller.
*/
if (ExitStatus == CFE_ES_RunStatus_UNDEFINED || ExitStatus >= CFE_ES_RunStatus_MAX)
{
CFE_ES_SysLogWrite_Unsync("%s: Called with invalid status (%u).\n", __func__, (unsigned int)ExitStatus);
/* revert to the ERROR status */
ExitStatus = CFE_ES_RunStatus_APP_ERROR;
}
AppRecPtr = CFE_ES_GetAppRecordByContext();
if (AppRecPtr != NULL)
{
/*
* Set the status in the global table.
*
* The passed-in status should only be stored if there was no already-pending
* request from a ground command or other source, such as an exception, etc.
*
* If a control request is already pending, it is assumed that this exit is
* part of an orderly shutdown caused by that request, and therefore it
* should not be overwritten here.
*/
if (AppRecPtr->ControlReq.AppControlRequest == CFE_ES_RunStatus_APP_RUN)
{
AppRecPtr->ControlReq.AppControlRequest = ExitStatus;
}
/*
** Check to see if the App is an external cFE App.
*/
if (AppRecPtr->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("%s: CORE Application %s Had an Init Error.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
/*
** 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("%s: CORE Application Init Error Processor Reset, RC = 0x%08X\n", __func__,
(unsigned int)ReturnCode);
return;
}
else if (ExitStatus == CFE_ES_RunStatus_CORE_APP_RUNTIME_ERROR)
{
CFE_ES_SysLogWrite_Unsync("%s: CORE Application %s Had a Runtime Error.\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
/*
** 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("%s: Cannot Exit CORE Application %s\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
}
}
else /* It is an external App */
{
CFE_ES_SysLogWrite_Unsync("%s: Application %s called CFE_ES_ExitApp\n", __func__,
CFE_ES_AppRecordGetName(AppRecPtr));
AppRecPtr->AppState = 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 ReturnCode == CFE_SUCCESS */
CFE_ES_UnlockSharedData(__func__, __LINE__);
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
bool CFE_ES_RunLoop(uint32 *RunStatus)
{
bool ReturnCode;
CFE_ES_AppRecord_t *AppRecPtr;
/*
* call CFE_ES_IncrementTaskCounter() so this is
* recorded as task activity for outgoing telemetry.
*
* This will update the counter for whatever task context
* is calling this API, which is expected to be the main
* task of the app. This can be done outside of any lock
* because each task has its own counter which is only updated
* by itself.
*/
CFE_ES_IncrementTaskCounter();
/*
* This API should generally only be called with the status as CFE_ES_RunStatus_APP_RUN.
* Anything else gets an immediate "false" return which should cause the caller to
* break out of its main loop. There is no need to take the lock or do any other
* accounting in that case.
*
* Note that the RunStatus really doesn't add much value here, so this also allows
* this function to be called with NULL, with the possibility of phasing this out
* entirely.
*/
if (RunStatus != NULL && *RunStatus != CFE_ES_RunStatus_APP_RUN)
{
return false;
}
CFE_ES_LockSharedData(__func__, __LINE__);
/*
** Get App Record
*/
AppRecPtr = CFE_ES_GetAppRecordByContext();
if (AppRecPtr != NULL)
{
/*
** App state must be RUNNING (no-op if already set to running)
*/
if (AppRecPtr->AppState < CFE_ES_AppState_RUNNING)
{
AppRecPtr->AppState = CFE_ES_AppState_RUNNING;
}
/*
* Check if the control request is also set to "RUN"
* Anything else should also return false, so the loop will exit.
*/
if (AppRecPtr->ControlReq.AppControlRequest == CFE_ES_RunStatus_APP_RUN)
{
ReturnCode = true;
}
else
{
/*
* Just in case, also output the status, just in case the app looks at this.
*/
if (RunStatus != NULL)
{
*RunStatus = AppRecPtr->ControlReq.AppControlRequest;
}
ReturnCode = false;
}
}
else
{
/*
* Cannot do anything without the AppID
*/
CFE_ES_SysLogWrite_Unsync("%s: Error getting AppID for the caller\n", __func__);
ReturnCode = false;
} /* end if Status == CFE_SUCCESS */
CFE_ES_UnlockSharedData(__func__, __LINE__);
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_WaitForSystemState(uint32 MinSystemState, uint32 TimeOutMilliseconds)
{
int32 Status = CFE_SUCCESS;
CFE_ES_AppRecord_t *AppRecPtr;
uint32 RequiredAppState;
uint32 WaitTime;
uint32 WaitRemaining;
/*
* Calling app 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__);
/*
** Get App Record
*/
AppRecPtr = CFE_ES_GetAppRecordByContext();
if (AppRecPtr != NULL)
{
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 (AppRecPtr->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).
*/
if (AppRecPtr->AppState < RequiredAppState)
{
AppRecPtr->AppState = 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;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_ES_WaitForStartupSync(uint32 TimeOutMilliseconds)
{
CFE_ES_WaitForSystemState(CFE_ES_SystemState_OPERATIONAL, TimeOutMilliseconds);
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetAppIDByName(CFE_ES_AppId_t *AppIdPtr, const char *AppName)
{
CFE_ES_AppRecord_t *AppRecPtr;
int32 Result;
if (AppName == NULL || AppIdPtr == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
CFE_ES_LockSharedData(__func__, __LINE__);
AppRecPtr = CFE_ES_LocateAppRecordByName(AppName);
if (AppRecPtr == NULL)
{
/*
* ensure the output value is set to a safe value,
* in case the caller does not check the return code.
*/
Result = CFE_ES_ERR_NAME_NOT_FOUND;
*AppIdPtr = CFE_ES_APPID_UNDEFINED;
}
else
{
Result = CFE_SUCCESS;
*AppIdPtr = CFE_ES_AppRecordGetID(AppRecPtr);
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetLibIDByName(CFE_ES_LibId_t *LibIdPtr, const char *LibName)
{
CFE_ES_LibRecord_t *LibRecPtr;
int32 Result;
if (LibName == NULL || LibIdPtr == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
CFE_ES_LockSharedData(__func__, __LINE__);
LibRecPtr = CFE_ES_LocateLibRecordByName(LibName);
if (LibRecPtr == NULL)
{
/*
* ensure the output value is set to a safe value,
* in case the caller does not check the return code.
*/
Result = CFE_ES_ERR_NAME_NOT_FOUND;
*LibIdPtr = CFE_ES_LIBID_UNDEFINED;
}
else
{
Result = CFE_SUCCESS;
*LibIdPtr = CFE_ES_LibRecordGetID(LibRecPtr);
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetTaskIDByName(CFE_ES_TaskId_t *TaskIdPtr, const char *TaskName)
{
osal_id_t OsalId = OS_OBJECT_ID_UNDEFINED;
int32 OsStatus;
CFE_Status_t Result;
if (TaskName == NULL || TaskIdPtr == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
/* For tasks IDs, defer to OSAL for name lookup */
OsStatus = OS_TaskGetIdByName(&OsalId, TaskName);
if (OsStatus == OS_SUCCESS)
{
Result = CFE_SUCCESS;
*TaskIdPtr = CFE_ES_TaskId_FromOSAL(OsalId);
}
else
{
Result = CFE_ES_ERR_NAME_NOT_FOUND;
*TaskIdPtr = CFE_ES_TASKID_UNDEFINED;
}
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetAppID(CFE_ES_AppId_t *AppIdPtr)
{
CFE_ES_AppRecord_t *AppRecPtr;
int32 Result;
if (AppIdPtr == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
CFE_ES_LockSharedData(__func__, __LINE__);
AppRecPtr = CFE_ES_GetAppRecordByContext();
if (AppRecPtr != NULL)
{
*AppIdPtr = CFE_ES_AppRecordGetID(AppRecPtr);
Result = CFE_SUCCESS;
}
else
{
*AppIdPtr = CFE_ES_APPID_UNDEFINED;
Result = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetTaskID(CFE_ES_TaskId_t *TaskIdPtr)
{
int32 Result;
CFE_ES_TaskRecord_t *TaskRecPtr;
if (TaskIdPtr == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
CFE_ES_LockSharedData(__func__, __LINE__);
TaskRecPtr = CFE_ES_GetTaskRecordByContext();
if (TaskRecPtr == NULL)
{
*TaskIdPtr = CFE_ES_TASKID_UNDEFINED;
Result = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
else
{
*TaskIdPtr = CFE_ES_TaskRecordGetID(TaskRecPtr);
Result = CFE_SUCCESS;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetAppName(char *AppName, CFE_ES_AppId_t AppId, size_t BufferLength)
{
int32 Result;
CFE_ES_AppRecord_t *AppRecPtr;
if (BufferLength == 0 || AppName == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
/*
** Get App Record
*/
AppRecPtr = CFE_ES_LocateAppRecordByID(AppId);
CFE_ES_LockSharedData(__func__, __LINE__);
/*
* confirm that the app record is a match,
* which must be done while locked.
*/
if (CFE_ES_AppRecordIsMatch(AppRecPtr, AppId))
{
strncpy(AppName, CFE_ES_AppRecordGetName(AppRecPtr), BufferLength - 1);
AppName[BufferLength - 1] = '\0';
Result = CFE_SUCCESS;
}
else
{
AppName[0] = 0;
Result = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetLibName(char *LibName, CFE_ES_LibId_t LibId, size_t BufferLength)
{
int32 Result;
CFE_ES_LibRecord_t *LibRecPtr;
if (BufferLength == 0 || LibName == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
/*
** Get Lib Record
*/
LibRecPtr = CFE_ES_LocateLibRecordByID(LibId);
CFE_ES_LockSharedData(__func__, __LINE__);
/*
* confirm that the Lib record is a match,
* which must be done while locked.
*/
if (CFE_ES_LibRecordIsMatch(LibRecPtr, LibId))
{
strncpy(LibName, CFE_ES_LibRecordGetName(LibRecPtr), BufferLength - 1);
LibName[BufferLength - 1] = '\0';
Result = CFE_SUCCESS;
}
else
{
LibName[0] = 0;
Result = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return Result;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetTaskName(char *TaskName, CFE_ES_TaskId_t TaskId, size_t BufferLength)
{
int32 OsStatus;
osal_id_t OsalId;
if (BufferLength == 0 || TaskName == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
if (!CFE_RESOURCEID_TEST_DEFINED(TaskId))
{
return CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
/*
* Query OSAL to get the task name
*/
OsalId = CFE_ES_TaskId_ToOSAL(TaskId);
OsStatus = OS_GetResourceName(OsalId, TaskName, BufferLength);
if (OsStatus == OS_ERR_INVALID_ID)
{
/* Supplied ID is not a CFE task */
return CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
if (OsStatus == OS_ERR_NAME_TOO_LONG)
{
/* Name is too long to fit in supplied buffer */
return CFE_ES_BAD_ARGUMENT;
}
if (OsStatus != OS_SUCCESS)
{
/* Some other uncaught error */
CFE_ES_WriteToSysLog("%s(): Unexpected error from OS_GetResourceName(): %ld", __func__, (long)OsStatus);
return CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
}
return CFE_SUCCESS;
}
/*----------------------------------------------------------------
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_Status_t CFE_ES_GetAppInfo(CFE_ES_AppInfo_t *AppInfo, CFE_ES_AppId_t AppId)
{
CFE_ES_AppRecord_t * AppRecPtr;
CFE_ES_TaskRecord_t *TaskRecPtr;
int32 Status;
osal_id_t ModuleId;
uint32 i;
if (AppInfo == NULL)
{
CFE_ES_WriteToSysLog("%s: Invalid Parameter ( Null Pointer )\n", __func__);
return CFE_ES_BAD_ARGUMENT;
}
memset(AppInfo, 0, sizeof(*AppInfo));