-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathcfe_es_apps.c
1456 lines (1302 loc) · 47.7 KB
/
cfe_es_apps.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_apps.c
**
** Purpose:
** This file contains functions for starting cFE applications from a filesystem.
**
** References:
** Flight Software Branch C Coding Standard Version 1.0a
** cFE Flight Software Application Developers Guide
**
** Notes:
**
*/
/*
** Includes
*/
#include "private/cfe_private.h"
#include "cfe_es.h"
#include "cfe_psp.h"
#include "cfe_es_global.h"
#include "cfe_es_task.h"
#include "cfe_es_apps.h"
#include "cfe_es_log.h"
#include <stdio.h>
#include <string.h> /* memset() */
#include <fcntl.h>
/*
** Defines
*/
#define ES_START_BUFF_SIZE 128
/*
**
** Global Variables
**
*/
/*
****************************************************************************
** Functions
***************************************************************************
*/
/*
** Name:
** CFE_ES_StartApplications
**
** Purpose:
** This routine loads/starts cFE applications.
**
*/
void CFE_ES_StartApplications(uint32 ResetType, const char *StartFilePath )
{
char ES_AppLoadBuffer[ES_START_BUFF_SIZE]; /* A buffer of for a line in a file */
const char *TokenList[CFE_ES_STARTSCRIPT_MAX_TOKENS_PER_LINE];
uint32 NumTokens;
uint32 BuffLen = 0; /* Length of the current buffer */
int32 AppFile = 0;
char c;
int32 ReadStatus;
bool LineTooLong = false;
bool FileOpened = false;
/*
** Get the ES startup script filename.
** If this is a Processor Reset, try to open the file in the volatile disk first.
*/
if ( ResetType == CFE_PSP_RST_TYPE_PROCESSOR )
{
/*
** Open the file in the volatile disk.
*/
AppFile = OS_open( CFE_PLATFORM_ES_VOLATILE_STARTUP_FILE, O_RDONLY, 0);
if ( AppFile >= 0 )
{
CFE_ES_WriteToSysLog ("ES Startup: Opened ES App Startup file: %s\n",
CFE_PLATFORM_ES_VOLATILE_STARTUP_FILE);
FileOpened = true;
}
else
{
CFE_ES_WriteToSysLog ("ES Startup: Cannot Open Volatile Startup file, Trying Nonvolatile.\n");
FileOpened = false;
}
} /* end if */
/*
** This if block covers two cases: A Power on reset, and a Processor reset when
** the startup file on the volatile file system could not be opened.
*/
if ( FileOpened == false )
{
/*
** Try to Open the file passed in to the cFE start.
*/
AppFile = OS_open( (const char *)StartFilePath, O_RDONLY, 0);
if ( AppFile >= 0 )
{
CFE_ES_WriteToSysLog ("ES Startup: Opened ES App Startup file: %s\n",StartFilePath);
FileOpened = true;
}
else
{
CFE_ES_WriteToSysLog ("ES Startup: Error, Can't Open ES App Startup file: %s EC = 0x%08X\n",
StartFilePath, (unsigned int)AppFile );
FileOpened = false;
}
}
/*
** If the file is opened in either the Nonvolatile or the Volatile disk, process it.
*/
if ( FileOpened == true)
{
memset(ES_AppLoadBuffer,0x0,ES_START_BUFF_SIZE);
BuffLen = 0;
NumTokens = 0;
TokenList[0] = ES_AppLoadBuffer;
/*
** Parse the lines from the file. If it has an error
** or reaches EOF, then abort the loop.
*/
while(1)
{
ReadStatus = OS_read(AppFile, &c, 1);
if ( ReadStatus == OS_ERROR )
{
CFE_ES_WriteToSysLog ("ES Startup: Error Reading Startup file. EC = 0x%08X\n",(unsigned int)ReadStatus);
break;
}
else if ( ReadStatus == 0 )
{
/*
** EOF Reached
*/
break;
}
else if(c != '!')
{
if ( c <= ' ')
{
/*
** Skip all white space in the file
*/
;
}
else if ( c == ',' )
{
/*
** replace the field delimiter with a null
** This is used to separate the tokens
*/
if ( BuffLen < ES_START_BUFF_SIZE )
{
ES_AppLoadBuffer[BuffLen] = 0;
}
else
{
LineTooLong = true;
}
BuffLen++;
if ( NumTokens < (CFE_ES_STARTSCRIPT_MAX_TOKENS_PER_LINE-1))
{
/*
* NOTE: pointer never deferenced unless "LineTooLong" is false.
*/
++NumTokens;
TokenList[NumTokens] = &ES_AppLoadBuffer[BuffLen];
}
}
else if ( c != ';' )
{
/*
** Regular data gets copied in
*/
if ( BuffLen < ES_START_BUFF_SIZE )
{
ES_AppLoadBuffer[BuffLen] = c;
}
else
{
LineTooLong = true;
}
BuffLen++;
}
else
{
if ( LineTooLong == true )
{
/*
** The was too big for the buffer
*/
CFE_ES_WriteToSysLog ("ES Startup: ES Startup File Line is too long: %u bytes.\n",(unsigned int)BuffLen);
LineTooLong = false;
}
else
{
/*
** Send the line to the file parser
** Ensure termination of the last token and send it along
*/
ES_AppLoadBuffer[BuffLen] = 0;
CFE_ES_ParseFileEntry(TokenList, 1 + NumTokens);
}
BuffLen = 0;
NumTokens = 0;
}
}
else
{
/*
** break when EOF character '!' is reached
*/
break;
}
}
/*
** close the file
*/
OS_close(AppFile);
}
}
/*
**---------------------------------------------------------------------------------------
** Name: CFE_ES_ParseFileEntry
**
** Purpose: This function parses the startup file line for an individual
** cFE application.
**---------------------------------------------------------------------------------------
*/
int32 CFE_ES_ParseFileEntry(const char **TokenList, uint32 NumTokens)
{
const char *FileName;
const char *AppName;
const char *EntryPoint;
const char *EntryType;
unsigned int Priority;
unsigned int StackSize;
unsigned int ExceptionAction;
uint32 ApplicationId;
int32 CreateStatus = CFE_ES_ERR_APP_CREATE;
/*
** Check to see if the correct number of items were parsed
*/
if ( NumTokens < 8 )
{
CFE_ES_WriteToSysLog("ES Startup: Invalid ES Startup file entry: %u\n",(unsigned int)NumTokens);
return (CreateStatus);
}
EntryType = TokenList[0];
FileName = TokenList[1];
EntryPoint = TokenList[2];
AppName = TokenList[3];
/*
* NOTE: In previous CFE versions the sscanf() function was used to convert
* these string values into integers. This approach of using the pre-tokenized strings
* and strtoul() is safer but the side effect is that it will also be more "permissive" in
* what is accepted vs. rejected by this function.
*
* For instance if the startup script contains "123xyz", this will be converted to the value
* 123 instead of triggering a validation failure as it would have in CFE <= 6.5.0.
*
* This permissive parsing should not be relied upon, as it may become more strict again in
* future CFE revisions.
*/
Priority = strtoul(TokenList[4], NULL, 0);
StackSize = strtoul(TokenList[5], NULL, 0);
ExceptionAction = strtoul(TokenList[7], NULL, 0);
if(strcmp(EntryType,"CFE_APP")==0)
{
CFE_ES_WriteToSysLog("ES Startup: Loading file: %s, APP: %s\n",
FileName, AppName);
/*
** Validate Some parameters
** Exception action should be 0 ( Restart App ) or
** 1 ( Processor reset ). If it's non-zero, assume it means
** reset CPU.
*/
if ( ExceptionAction > CFE_ES_ExceptionAction_RESTART_APP )
ExceptionAction = CFE_ES_ExceptionAction_PROC_RESTART;
/*
** Now create the application
*/
CreateStatus = CFE_ES_AppCreate(&ApplicationId, FileName,
EntryPoint, AppName, (uint32) Priority,
(uint32) StackSize, (uint32) ExceptionAction );
}
else if(strcmp(EntryType,"CFE_LIB")==0)
{
CFE_ES_WriteToSysLog("ES Startup: Loading shared library: %s\n",FileName);
/*
** Now load the library
*/
CreateStatus = CFE_ES_LoadLibrary(&ApplicationId, FileName,
EntryPoint, AppName);
}
else
{
CFE_ES_WriteToSysLog("ES Startup: Unexpected EntryType %s in startup file.\n",EntryType);
}
return (CreateStatus);
}
/*
**---------------------------------------------------------------------------------------
** Name: ES_AppCreate
**
** Purpose: This function loads and creates a cFE Application.
** This function can be called from the ES startup code when it
** loads the cFE Applications from the disk using the startup script, or it
** can be called when the ES Start Application command is executed.
**
**---------------------------------------------------------------------------------------
*/
int32 CFE_ES_AppCreate(uint32 *ApplicationIdPtr,
const char *FileName,
const void *EntryPointData,
const char *AppName,
uint32 Priority,
uint32 StackSize,
uint32 ExceptionAction)
{
cpuaddr StartAddr;
int32 ReturnCode;
uint32 i;
bool AppSlotFound;
uint32 TaskId;
uint32 ModuleId;
/*
* The FileName must not be NULL
*/
if (FileName == NULL)
{
return CFE_ES_ERR_APP_CREATE;
}
/*
** Allocate an ES_AppTable entry
*/
CFE_ES_LockSharedData(__func__,__LINE__);
AppSlotFound = false;
for ( i = 0; i < CFE_PLATFORM_ES_MAX_APPLICATIONS; i++ )
{
if ( CFE_ES_Global.AppTable[i].AppState == CFE_ES_AppState_UNDEFINED )
{
AppSlotFound = true;
memset ( &(CFE_ES_Global.AppTable[i]), 0, sizeof(CFE_ES_AppRecord_t));
/* set state EARLY_INIT for OS_TaskCreate below (indicates record is in use) */
CFE_ES_Global.AppTable[i].AppState = CFE_ES_AppState_EARLY_INIT;
break;
}
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
** If a slot was found, create the application
*/
if ( AppSlotFound == true)
{
/*
** Load the module
*/
ReturnCode = OS_ModuleLoad ( &ModuleId, AppName, FileName );
/*
** If the Load was OK, then lookup the address of the entry point
*/
if ( ReturnCode == OS_SUCCESS )
{
ReturnCode = OS_SymbolLookup( &StartAddr, (const char*)EntryPointData );
if ( ReturnCode != OS_SUCCESS )
{
CFE_ES_WriteToSysLog("ES Startup: Could not find symbol:%s. EC = 0x%08X\n",
(const char*)EntryPointData, (unsigned int)ReturnCode);
CFE_ES_LockSharedData(__func__,__LINE__);
CFE_ES_Global.AppTable[i].AppState = CFE_ES_AppState_UNDEFINED; /* Release slot */
CFE_ES_UnlockSharedData(__func__,__LINE__);
/* Unload the module from memory, so that it does not consume resources */
ReturnCode = OS_ModuleUnload(ModuleId);
if ( ReturnCode != OS_SUCCESS ) /* There's not much we can do except notify */
{
CFE_ES_WriteToSysLog("ES Startup: Failed to unload APP: %s. EC = 0x%08X\n",
AppName, (unsigned int)ReturnCode);
}
return(CFE_ES_ERR_APP_CREATE);
}
}
else /* load not successful */
{
CFE_ES_WriteToSysLog("ES Startup: Could not load cFE application file:%s. EC = 0x%08X\n",
FileName, (unsigned int)ReturnCode);
CFE_ES_LockSharedData(__func__,__LINE__);
CFE_ES_Global.AppTable[i].AppState = CFE_ES_AppState_UNDEFINED; /* Release slot */
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(CFE_ES_ERR_APP_CREATE);
}
/*
** If the EntryPoint symbol was found, then start creating the App
*/
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Allocate and populate the ES_AppTable entry
*/
CFE_ES_Global.AppTable[i].Type = CFE_ES_AppType_EXTERNAL;
/*
** Fill out the parameters in the AppStartParams sub-structure
*/
strncpy((char *)CFE_ES_Global.AppTable[i].StartParams.Name, AppName, OS_MAX_API_NAME);
CFE_ES_Global.AppTable[i].StartParams.Name[OS_MAX_API_NAME - 1] = '\0';
strncpy((char *)CFE_ES_Global.AppTable[i].StartParams.EntryPoint, (const char *)EntryPointData, OS_MAX_API_NAME);
CFE_ES_Global.AppTable[i].StartParams.EntryPoint[OS_MAX_API_NAME - 1] = '\0';
strncpy((char *)CFE_ES_Global.AppTable[i].StartParams.FileName, FileName, OS_MAX_PATH_LEN);
CFE_ES_Global.AppTable[i].StartParams.FileName[OS_MAX_PATH_LEN - 1] = '\0';
CFE_ES_Global.AppTable[i].StartParams.StackSize = StackSize;
CFE_ES_Global.AppTable[i].StartParams.StartAddress = StartAddr;
CFE_ES_Global.AppTable[i].StartParams.ModuleId = ModuleId;
CFE_ES_Global.AppTable[i].StartParams.ExceptionAction = ExceptionAction;
CFE_ES_Global.AppTable[i].StartParams.Priority = Priority;
/*
** Fill out the Task Info
*/
strncpy((char *)CFE_ES_Global.AppTable[i].TaskInfo.MainTaskName, AppName, OS_MAX_API_NAME);
CFE_ES_Global.AppTable[i].TaskInfo.MainTaskName[OS_MAX_API_NAME - 1] = '\0';
/*
** Fill out the Task State info
*/
CFE_ES_Global.AppTable[i].ControlReq.AppControlRequest = CFE_ES_RunStatus_APP_RUN;
CFE_ES_Global.AppTable[i].ControlReq.AppTimerMsec = 0;
/*
** Create the primary task for the newly loaded task
*/
ReturnCode = OS_TaskCreate(&CFE_ES_Global.AppTable[i].TaskInfo.MainTaskId, /* task id */
AppName, /* task name */
(osal_task_entry)StartAddr, /* task function pointer */
NULL, /* stack pointer */
StackSize, /* stack size */
Priority, /* task priority */
OS_FP_ENABLED); /* task options */
if(ReturnCode != OS_SUCCESS)
{
CFE_ES_SysLogWrite_Unsync("ES Startup: AppCreate Error: TaskCreate %s Failed. EC = 0x%08X!\n",
AppName,(unsigned int)ReturnCode);
CFE_ES_Global.AppTable[i].AppState = CFE_ES_AppState_UNDEFINED;
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(CFE_ES_ERR_APP_CREATE);
}
else
{
/*
** Record the ES_TaskTable entry
*/
OS_ConvertToArrayIndex(CFE_ES_Global.AppTable[i].TaskInfo.MainTaskId, &TaskId);
if ( CFE_ES_Global.TaskTable[TaskId].RecordUsed == true )
{
CFE_ES_SysLogWrite_Unsync("ES Startup: Error: ES_TaskTable slot in use at task creation!\n");
}
else
{
CFE_ES_Global.TaskTable[TaskId].RecordUsed = true;
}
CFE_ES_Global.TaskTable[TaskId].AppId = i;
CFE_ES_Global.TaskTable[TaskId].TaskId = CFE_ES_Global.AppTable[i].TaskInfo.MainTaskId;
strncpy((char *)CFE_ES_Global.TaskTable[TaskId].TaskName,
(char *)CFE_ES_Global.AppTable[i].TaskInfo.MainTaskName,OS_MAX_API_NAME );
CFE_ES_Global.TaskTable[TaskId].TaskName[OS_MAX_API_NAME - 1]='\0';
CFE_ES_SysLogWrite_Unsync("ES Startup: %s loaded and created\n", AppName);
*ApplicationIdPtr = i;
/*
** Increment the registered App and Registered External Task variables.
*/
CFE_ES_Global.RegisteredTasks++;
CFE_ES_Global.RegisteredExternalApps++;
CFE_ES_UnlockSharedData(__func__,__LINE__);
return(CFE_SUCCESS);
} /* End If OS_TaskCreate */
}
else /* appSlot not found */
{
CFE_ES_WriteToSysLog("ES Startup: No free application slots available\n");
return(CFE_ES_ERR_APP_CREATE);
}
} /* End Function */
/*
**---------------------------------------------------------------------------------------
** Name: CFE_ES_LoadLibrary
**
** Purpose: This function loads and initializes a cFE Shared Library.
**
**---------------------------------------------------------------------------------------
*/
int32 CFE_ES_LoadLibrary(uint32 *LibraryIdPtr,
const char *FileName,
const void *EntryPointData,
const char *LibName)
{
CFE_ES_LibraryEntryFuncPtr_t FunctionPointer;
CFE_ES_LibRecord_t * LibSlotPtr;
size_t StringLength;
int32 Status;
uint32 CheckSlot;
uint32 ModuleId;
bool IsModuleLoaded;
/*
* First, should verify that the supplied "LibName" fits within the internal limit
* (currently sized to OS_MAX_API_NAME, but not assuming that will always be)
*/
StringLength = strlen(LibName);
if (StringLength >= sizeof(CFE_ES_Global.LibTable[0].LibName))
{
return CFE_ES_BAD_ARGUMENT;
}
/*
** Allocate an ES_LibTable entry
*/
IsModuleLoaded = false;
LibSlotPtr = NULL;
FunctionPointer = NULL;
ModuleId = 0;
Status = CFE_ES_ERR_LOAD_LIB; /* error that will be returned if no slots found */
CFE_ES_LockSharedData(__func__,__LINE__);
for ( CheckSlot = 0; CheckSlot < CFE_PLATFORM_ES_MAX_LIBRARIES; CheckSlot++ )
{
if (CFE_ES_Global.LibTable[CheckSlot].RecordUsed)
{
if (strcmp(CFE_ES_Global.LibTable[CheckSlot].LibName, LibName) == 0)
{
/*
* Indicate to caller that the library is already loaded.
* (This is when there was a matching LibName in the table)
*
* Do nothing more; not logging this event as it may or may
* not be an error.
*/
*LibraryIdPtr = CheckSlot;
Status = CFE_ES_LIB_ALREADY_LOADED;
break;
}
}
else if (LibSlotPtr == NULL)
{
/* Remember list position as possible place for new entry. */
LibSlotPtr = &CFE_ES_Global.LibTable[CheckSlot];
*LibraryIdPtr = CheckSlot;
Status = CFE_SUCCESS;
}
else
{
/* No action */
}
}
if (Status == CFE_SUCCESS)
{
/* reserve the slot while still under lock */
strcpy(LibSlotPtr->LibName, LibName);
LibSlotPtr->RecordUsed = true;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* If any off-nominal condition exists, skip the rest of this logic.
* Additionally write any extra information about what happened to syslog
* Note - not logging "already loaded" conditions, as this is not necessarily an error.
*/
if (Status != CFE_SUCCESS)
{
if (Status == CFE_ES_ERR_LOAD_LIB)
{
CFE_ES_WriteToSysLog("ES Startup: No free library slots available\n");
}
return Status;
}
/*
* -------------------
* IMPORTANT:
*
* there is now a reserved entry in the global library table,
* which must be freed if something goes wrong hereafter.
*
* Avoid any inline "return" statements - all paths must proceed to
* the end of this function where the cleanup will be done.
*
* Record sufficient breadcrumbs along the way, such that proper
* cleanup can be done in case it is necessary.
* -------------------
*/
/*
* STAGE 2:
* Do the OS_ModuleLoad() if is called for (i.e. ModuleLoadFile is NOT null)
*/
if (Status == CFE_SUCCESS && FileName != NULL)
{
Status = OS_ModuleLoad( &ModuleId, LibName, FileName );
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS; /* just in case CFE_SUCCESS is different than OS_SUCCESS */
IsModuleLoaded = true;
}
else
{
/* load not successful. Note OS errors are better displayed as decimal integers. */
CFE_ES_WriteToSysLog("ES Startup: Could not load cFE Shared Library: %d\n", (int)Status);
Status = CFE_ES_ERR_LOAD_LIB; /* convert OS error to CFE error code */
}
}
/*
* STAGE 3:
* Figure out the Entry point / Initialization function.
*
* This depends on whether it is a dynamically loaded or a statically linked library,
* or it could be omitted altogether for libraries which do not require an init function.
*
* For dynamically loaded objects where FileName is non-NULL, the
* "EntryPointData" is a normal C string (const char *) with the name of the function.
*
* If the name of the function is the string "NULL" -- then treat this as no function
* needed and skip the lookup entirely (this is to support startup scripts where some
* string must be in the entry point field).
*/
if (Status == CFE_SUCCESS && EntryPointData != NULL)
{
if (strcmp(EntryPointData, "NULL") != 0)
{
/*
* If the entry point is explicitly set as NULL,
* this means the library has no init function - skip the lookup.
* Otherwise lookup the address of the entry point
*/
cpuaddr StartAddr;
Status = OS_SymbolLookup( &StartAddr, EntryPointData );
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS; /* just in case CFE_SUCCESS is different than OS_SUCCESS */
FunctionPointer = (CFE_ES_LibraryEntryFuncPtr_t)StartAddr;
}
else
{
/* could not find symbol. Note OS errors are better displayed as decimal integers */
CFE_ES_WriteToSysLog("ES Startup: Could not find Library Init symbol:%s. EC = %d\n",
(const char *)EntryPointData, (int)Status);
Status = CFE_ES_ERR_LOAD_LIB; /* convert OS error to CFE error code */
}
}
}
/*
* STAGE 4:
* Call the Initialization function, if one was identified during the previous stage
*/
if (Status == CFE_SUCCESS && FunctionPointer != NULL)
{
/*
** Call the library initialization routine
*/
Status = (*FunctionPointer)(*LibraryIdPtr);
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("ES Startup: Load Shared Library Init Error = 0x%08x\n", (unsigned int)Status);
}
}
/*
* LAST STAGE:
* Do final clean-up
*
* If fully successful, then increment the "RegisteredLibs" counter.
* Otherwise in case of an error, do clean up based on the breadcrumbs
*/
if(Status == CFE_SUCCESS)
{
/* Increment the counter, which needs to be done under lock */
CFE_ES_LockSharedData(__func__,__LINE__);
CFE_ES_Global.RegisteredLibs++;
CFE_ES_UnlockSharedData(__func__,__LINE__);
}
else
{
/*
* If the above code had loaded a module, then unload it
*/
if (IsModuleLoaded)
{
OS_ModuleUnload( ModuleId );
}
/* Release Slot - No need to lock as it is resetting just a single bool value */
LibSlotPtr->RecordUsed = false;
}
return(Status);
} /* End Function */
/*
**---------------------------------------------------------------------------------------
** Name: CFE_ES_RunAppTableScan
**
** Purpose: This function scans the ES Application table and acts on the changes
** in application states. This is where the external cFE Applications are
** restarted, reloaded, or deleted.
**---------------------------------------------------------------------------------------
*/
bool CFE_ES_RunAppTableScan(uint32 ElapsedTime, void *Arg)
{
uint32 i;
CFE_ES_AppRecord_t *AppPtr;
CFE_ES_AppTableScanState_t *State = (CFE_ES_AppTableScanState_t *)Arg;
if (State->PendingAppStateChanges == 0)
{
/*
* If the command count changes, then a scan becomes due immediately.
*/
if (State->LastScanCommandCount == CFE_ES_TaskData.CommandCounter &&
State->BackgroundScanTimer > ElapsedTime)
{
/* no action at this time, background scan is not due yet */
State->BackgroundScanTimer -= ElapsedTime;
return false;
}
}
/*
* Every time a scan is initiated (for any reason)
* reset the background scan timer to the full value,
* and take a snapshot of the the command counter.
*/
State->BackgroundScanTimer = CFE_PLATFORM_ES_APP_SCAN_RATE;
State->LastScanCommandCount = CFE_ES_TaskData.CommandCounter;
State->PendingAppStateChanges = 0;
/*
* Scan needs to be done with the table locked,
* as these state changes need to be done atomically
* with respect to other tasks that also access/update
* the state.
*/
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Scan the ES Application table. Skip entries that are:
** - Not in use, or
** - cFE Core apps, or
** - Currently running
*/
for ( i = 0; i < CFE_PLATFORM_ES_MAX_APPLICATIONS; i++ )
{
AppPtr = &CFE_ES_Global.AppTable[i];
if (AppPtr->Type == CFE_ES_AppType_EXTERNAL)
{
if (AppPtr->AppState > CFE_ES_AppState_RUNNING)
{
/*
* Increment the "pending" counter which reflects
* the number of apps that are in some phase of clean up.
*/
++State->PendingAppStateChanges;
/*
* Decrement the wait timer, if active.
* When the timeout value becomes zero, take the action to delete/restart/reload the app
*/
if ( AppPtr->ControlReq.AppTimerMsec > ElapsedTime )
{
AppPtr->ControlReq.AppTimerMsec -= ElapsedTime;
}
else
{
AppPtr->ControlReq.AppTimerMsec = 0;
/*
* Temporarily unlock the table, and invoke the
* control request function for this app.
*/
CFE_ES_UnlockSharedData(__func__,__LINE__);
CFE_ES_ProcessControlRequest(i);
CFE_ES_LockSharedData(__func__,__LINE__);
} /* end if */
}
else if (AppPtr->AppState == CFE_ES_AppState_RUNNING &&
AppPtr->ControlReq.AppControlRequest > CFE_ES_RunStatus_APP_RUN)
{
/* this happens after a command arrives to restart/reload/delete an app */
/* switch to WAITING state, and set the timer for transition */
AppPtr->AppState = CFE_ES_AppState_WAITING;
AppPtr->ControlReq.AppTimerMsec = CFE_PLATFORM_ES_APP_KILL_TIMEOUT * CFE_PLATFORM_ES_APP_SCAN_RATE;
}
} /* end if */
} /* end for loop */
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* This state machine is considered active if there are any
* pending app state changes. Returning "true" will cause this job
* to be called from the background task at a faster interval.
*/
return (State->PendingAppStateChanges != 0);
} /* End Function */
/*
**---------------------------------------------------------------------------------------
** Name: CFE_ES_ProcessControlRequest
**
** Purpose: This function will perform the requested control action for an application.
**---------------------------------------------------------------------------------------
*/
void CFE_ES_ProcessControlRequest(uint32 AppID)
{
int32 Status;
CFE_ES_AppStartParams_t AppStartParams;
uint32 NewAppId;
/*
** First get a copy of the Apps Start Parameters
*/
memcpy(&AppStartParams, &(CFE_ES_Global.AppTable[AppID].StartParams), sizeof(CFE_ES_AppStartParams_t));
/*
** Now, find out what kind of Application control is being requested
*/
switch ( CFE_ES_Global.AppTable[AppID].ControlReq.AppControlRequest )
{
case CFE_ES_RunStatus_APP_EXIT:
/*
** Kill the app, and dont restart it
*/
Status = CFE_ES_CleanUpApp(AppID);
if ( Status == CFE_SUCCESS )
{
CFE_EVS_SendEvent(CFE_ES_EXIT_APP_INF_EID, CFE_EVS_EventType_INFORMATION,
"Exit Application %s Completed.",AppStartParams.Name);
}
else
{
CFE_EVS_SendEvent(CFE_ES_EXIT_APP_ERR_EID, CFE_EVS_EventType_ERROR,
"Exit Application %s Failed: CleanUpApp Error 0x%08X.",AppStartParams.Name, (unsigned int)Status);
}
break;
case CFE_ES_RunStatus_APP_ERROR:
/*
** Kill the app, and dont restart it
*/
Status = CFE_ES_CleanUpApp(AppID);
if ( Status == CFE_SUCCESS )
{
CFE_EVS_SendEvent(CFE_ES_ERREXIT_APP_INF_EID, CFE_EVS_EventType_INFORMATION,
"Exit Application %s on Error Completed.",AppStartParams.Name);
}
else
{
CFE_EVS_SendEvent(CFE_ES_ERREXIT_APP_ERR_EID, CFE_EVS_EventType_ERROR,
"Exit Application %s on Error Failed: CleanUpApp Error 0x%08X.",AppStartParams.Name, (unsigned int)Status);
}
break;
case CFE_ES_RunStatus_SYS_DELETE:
/*
** Kill the app, and dont restart it
*/
Status = CFE_ES_CleanUpApp(AppID);
if ( Status == CFE_SUCCESS )
{
CFE_EVS_SendEvent(CFE_ES_STOP_INF_EID, CFE_EVS_EventType_INFORMATION,
"Stop Application %s Completed.",AppStartParams.Name);
}
else
{
CFE_EVS_SendEvent(CFE_ES_STOP_ERR3_EID, CFE_EVS_EventType_ERROR,
"Stop Application %s Failed: CleanUpApp Error 0x%08X.",AppStartParams.Name, (unsigned int)Status);
}
break;
case CFE_ES_RunStatus_SYS_RESTART:
/*
** Kill the app
*/
Status = CFE_ES_CleanUpApp(AppID);
if ( Status == CFE_SUCCESS )
{
/*
** And start it back up again
*/
Status = CFE_ES_AppCreate(&NewAppId, (char *)AppStartParams.FileName,
(char *)AppStartParams.EntryPoint,
(char *)AppStartParams.Name,
AppStartParams.Priority,
AppStartParams.StackSize,
AppStartParams.ExceptionAction);
if ( Status == CFE_SUCCESS )
{
CFE_EVS_SendEvent(CFE_ES_RESTART_APP_INF_EID, CFE_EVS_EventType_INFORMATION,
"Restart Application %s Completed.", AppStartParams.Name);
}
else
{
CFE_EVS_SendEvent(CFE_ES_RESTART_APP_ERR3_EID, CFE_EVS_EventType_ERROR,
"Restart Application %s Failed: AppCreate Error 0x%08X.", AppStartParams.Name, (unsigned int)Status);
}
}
else
{
CFE_EVS_SendEvent(CFE_ES_RESTART_APP_ERR4_EID, CFE_EVS_EventType_ERROR,
"Restart Application %s Failed: CleanUpApp Error 0x%08X.", AppStartParams.Name, (unsigned int)Status);
}
break;
case CFE_ES_RunStatus_SYS_RELOAD:
/*
** Kill the app
*/