-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_es_apps.c
1836 lines (1650 loc) · 59.1 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 "cfe_es_resource.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; /* Length of the current buffer */
osal_id_t AppFile;
int32 Status;
char c;
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.
*/
Status = OS_OpenCreate(&AppFile, CFE_PLATFORM_ES_VOLATILE_STARTUP_FILE, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if ( Status >= 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.
*/
Status = OS_OpenCreate(&AppFile, StartFilePath, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if ( Status >= 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)Status );
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)
{
Status = OS_read(AppFile, &c, 1);
if ( Status < 0 )
{
CFE_ES_WriteToSysLog ("ES Startup: Error Reading Startup file. EC = 0x%08X\n",(unsigned int)Status);
break;
}
else if ( Status == 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 long PriorityIn;
unsigned long StackSizeIn;
unsigned long ExceptionActionIn;
union
{
CFE_ES_AppId_t AppId;
CFE_ES_LibId_t LibId;
} IdBuf;
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.
*
* Also note that this uses "unsigned long" as that is the defined output type of strtoul().
* It will be converted to the correct type later.
*/
PriorityIn = strtoul(TokenList[4], NULL, 0);
StackSizeIn = strtoul(TokenList[5], NULL, 0);
ExceptionActionIn = 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 ( ExceptionActionIn > CFE_ES_ExceptionAction_RESTART_APP )
{
ExceptionActionIn = CFE_ES_ExceptionAction_PROC_RESTART;
}
/*
* Task priority cannot be bigger than OS_MAX_TASK_PRIORITY
*/
if ( PriorityIn > OS_MAX_TASK_PRIORITY )
{
PriorityIn = OS_MAX_TASK_PRIORITY;
}
/*
** Now create the application
*/
CreateStatus = CFE_ES_AppCreate(&IdBuf.AppId, FileName,
EntryPoint, AppName,
PriorityIn,
StackSizeIn,
ExceptionActionIn);
}
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(&IdBuf.LibId, FileName,
EntryPoint, AppName);
}
else
{
CFE_ES_WriteToSysLog("ES Startup: Unexpected EntryType %s in startup file.\n",EntryType);
}
return (CreateStatus);
}
/*
**-------------------------------------------------------------------------------------
** Name: CFE_ES_LoadModule
**
** Helper function to load + configure (but not start) a new app/lib module
**
** Loads the module file via OSAL and stores all relevant info in the table entry as necessary.
**
**-------------------------------------------------------------------------------------
*/
int32 CFE_ES_LoadModule(CFE_ResourceId_t ResourceId, const CFE_ES_ModuleLoadParams_t* LoadParams, CFE_ES_ModuleLoadStatus_t *LoadStatus)
{
osal_id_t ModuleId;
cpuaddr StartAddr;
int32 ReturnCode;
int32 StatusCode;
uint32 LoadFlags;
LoadFlags = 0;
StartAddr = 0;
ReturnCode = CFE_SUCCESS;
if (LoadParams->FileName[0] != 0)
{
switch(CFE_ResourceId_GetBase(ResourceId))
{
case CFE_ES_APPID_BASE:
/*
* Apps should not typically have symbols exposed to other apps.
*
* Keeping symbols local/private may help ensure this module is unloadable
* in the future, depending on underlying OS/loader implementation.
*/
LoadFlags |= OS_MODULE_FLAG_LOCAL_SYMBOLS;
break;
case CFE_ES_LIBID_BASE:
/*
* Libraries need to have their symbols exposed to other apps.
*
* Note on some OS/loader implementations this may make it so the module
* cannot be unloaded, if there is no way to ensure that symbols
* are not being referenced. CFE does not currently support unloading
* of libraries for this reason, among others.
*/
LoadFlags |= OS_MODULE_FLAG_GLOBAL_SYMBOLS;
break;
default:
break;
}
/*
* Load the module via OSAL.
*/
StatusCode = OS_ModuleLoad ( &ModuleId,
LoadParams->Name,
LoadParams->FileName,
LoadFlags );
if (StatusCode != OS_SUCCESS)
{
CFE_ES_WriteToSysLog("ES Startup: Could not load file:%s. EC = 0x%08X\n",
LoadParams->FileName, (unsigned int)StatusCode);
ModuleId = OS_OBJECT_ID_UNDEFINED;
ReturnCode = CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
}
}
else
{
ModuleId = OS_OBJECT_ID_UNDEFINED;
}
/*
* If the Load was OK, then lookup the address of the entry point
*/
if (ReturnCode == CFE_SUCCESS && LoadParams->EntryPoint[0] != 0)
{
StatusCode = OS_ModuleSymbolLookup(ModuleId, &StartAddr, LoadParams->EntryPoint);
if (StatusCode != OS_SUCCESS)
{
CFE_ES_WriteToSysLog("ES Startup: Could not find symbol:%s. EC = 0x%08X\n",
LoadParams->EntryPoint, (unsigned int)StatusCode);
ReturnCode = CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
}
}
if ( ReturnCode == CFE_SUCCESS )
{
/* store the data in the app record after successful load+lookup */
LoadStatus->ModuleId = ModuleId;
LoadStatus->EntryAddress = StartAddr;
}
else if (OS_ObjectIdDefined(ModuleId))
{
/* If the module had been successfully loaded, then unload it,
* so that it does not consume resources */
StatusCode = OS_ModuleUnload(ModuleId);
if ( StatusCode != OS_SUCCESS ) /* There's not much we can do except notify */
{
CFE_ES_WriteToSysLog("ES Startup: Failed to unload: %s. EC = 0x%08X\n",
LoadParams->Name, (unsigned int)StatusCode);
}
}
return ReturnCode;
}
/*
**-------------------------------------------------------------------------------------
** Name: CFE_ES_GetAppEntryPoint
**
** Helper function to act as the intermediate entry point of an app
** This is to support starting apps before having a fully completed entry in the
** global app table. The app startup will delay until the app creation is completed
** and verified, then the actual entry point will be determined.
**
**-------------------------------------------------------------------------------------
*/
int32 CFE_ES_GetAppEntryPoint(osal_task_entry *FuncPtr)
{
CFE_ES_AppRecord_t *AppRecPtr;
int32 ReturnCode;
int32 Timeout;
/*
* Use the same timeout as was used for the startup script itself.
*/
ReturnCode = CFE_ES_ERR_APP_REGISTER;
Timeout = CFE_PLATFORM_ES_STARTUP_SCRIPT_TIMEOUT_MSEC;
while(true)
{
OS_TaskDelay(CFE_PLATFORM_ES_STARTUP_SYNC_POLL_MSEC);
CFE_ES_LockSharedData(__func__,__LINE__);
AppRecPtr = CFE_ES_GetAppRecordByContext();
if (AppRecPtr != NULL)
{
AppRecPtr->AppState = CFE_ES_AppState_EARLY_INIT;
*FuncPtr = (osal_task_entry)AppRecPtr->ModuleInfo.EntryAddress;
ReturnCode = CFE_SUCCESS;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
if (ReturnCode == CFE_SUCCESS || Timeout <= 0)
{
/* end of loop condition */
break;
}
Timeout -= CFE_PLATFORM_ES_STARTUP_SYNC_POLL_MSEC;
}
return (ReturnCode);
}
/*
**-------------------------------------------------------------------------------------
** Name: CFE_ES_AppEntryPoint
**
** Helper function to act as the intermediate entry point of an app
** This is to support starting apps before having a fully completed entry in the
** global app table. The app startup will delay until the app creation is completed
** and verified, then the actual entry point will be determined.
**
**-------------------------------------------------------------------------------------
*/
void CFE_ES_AppEntryPoint(void)
{
osal_task_entry RealEntryFunc;
if (CFE_ES_GetAppEntryPoint(&RealEntryFunc) == CFE_SUCCESS &&
RealEntryFunc != NULL)
{
(*RealEntryFunc)();
}
}
/*
**-------------------------------------------------------------------------------------
** Name: CFE_ES_StartMainTask
**
** Helper function to start (but not load) a new app/lib module
**
** Note that OSAL does not separate the action of creating and start a task, providing
** only OS_TaskCreate which does both. But there is a potential race condition if
** the real task code starts and calls e.g. CFE_ES_RegisterApp() or any other function
** that depends on having an AppID context, before its fully registered in the global app table.
**
** Therefore this calls a dedicated CFE_ES_AppEntryPoint which then will wait until
** the task is fully registered in the global, before calling the actual app entry point.
**
**-------------------------------------------------------------------------------------
*/
int32 CFE_ES_StartAppTask(const CFE_ES_AppStartParams_t* StartParams, CFE_ES_AppId_t RefAppId, CFE_ES_TaskId_t *TaskIdPtr)
{
CFE_ES_TaskRecord_t *TaskRecPtr;
osal_id_t OsalTaskId;
CFE_ES_TaskId_t LocalTaskId;
int32 StatusCode;
int32 ReturnCode;
/*
** Create the primary task for the newly loaded task
*/
StatusCode = OS_TaskCreate(&OsalTaskId, /* task id */
StartParams->BasicInfo.Name, /* task name */
CFE_ES_AppEntryPoint, /* task function pointer */
OSAL_TASK_STACK_ALLOCATE, /* stack pointer (allocate) */
StartParams->StackSize, /* stack size */
StartParams->Priority, /* task priority */
OS_FP_ENABLED); /* task options */
CFE_ES_LockSharedData(__func__,__LINE__);
if ( StatusCode == OS_SUCCESS )
{
/*
* As this is a newly-created task, this shouldn't fail.
* The entry is not (yet) matching the task ID - it will be
* initialized here.
*/
LocalTaskId = CFE_ES_TaskId_FromOSAL(OsalTaskId);
TaskRecPtr = CFE_ES_LocateTaskRecordByID(LocalTaskId);
if ( CFE_ES_TaskRecordIsUsed(TaskRecPtr) )
{
CFE_ES_SysLogWrite_Unsync("ES Startup: Error: ES_TaskTable slot for ID %lx in use at task creation!\n",
OS_ObjectIdToInteger(OsalTaskId));
}
/*
* Clear any other/stale data that might be in the entry,
* and reset all fields to the correct value.
*/
memset(TaskRecPtr, 0, sizeof(*TaskRecPtr));
TaskRecPtr->AppId = RefAppId;
strncpy(TaskRecPtr->TaskName, StartParams->BasicInfo.Name, sizeof(TaskRecPtr->TaskName)-1);
TaskRecPtr->TaskName[sizeof(TaskRecPtr->TaskName)-1] = 0;
CFE_ES_TaskRecordSetUsed(TaskRecPtr, CFE_RESOURCEID_UNWRAP(LocalTaskId));
/*
** Increment the registered Task count.
*/
CFE_ES_Global.RegisteredTasks++;
ReturnCode = CFE_SUCCESS;
*TaskIdPtr = CFE_ES_TaskRecordGetID(TaskRecPtr);
}
else
{
CFE_ES_SysLogWrite_Unsync("ES Startup: AppCreate Error: TaskCreate %s Failed. EC = 0x%08X!\n",
StartParams->BasicInfo.Name,(unsigned int)StatusCode);
ReturnCode = CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
*TaskIdPtr = CFE_ES_TASKID_UNDEFINED;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
return ReturnCode;
}
/*
**---------------------------------------------------------------------------------------
** 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(CFE_ES_AppId_t *ApplicationIdPtr,
const char *FileName,
const char *EntryPointName,
const char *AppName,
CFE_ES_TaskPriority_Atom_t Priority,
size_t StackSize,
CFE_ES_ExceptionAction_Enum_t ExceptionAction)
{
CFE_Status_t Status;
CFE_ES_TaskId_t MainTaskId;
CFE_ES_AppRecord_t *AppRecPtr;
CFE_ResourceId_t PendingResourceId;
/*
* The FileName must not be NULL
*/
if (FileName == NULL || AppName == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
if (strlen(AppName) >= OS_MAX_API_NAME)
{
return CFE_ES_BAD_ARGUMENT;
}
/*
** Allocate an ES_AppTable entry
*/
CFE_ES_LockSharedData(__func__,__LINE__);
/*
** Find an ES AppTable entry, and set to RESERVED
**
** In this state, the entry is no longer free, but also will not pass the
** validation test. So this function effectively has exclusive access
** without holding the global lock.
**
** IMPORTANT: it must set the ID to something else before leaving
** this function or else the resource will be leaked. After this
** point, execution must proceed to the end of the function to
** guarantee that the entry is either completed or freed.
*/
/*
* Check for an existing entry with the same name.
* Also check for a matching Library name.
* (Apps and libraries should be uniquely named)
*/
AppRecPtr = CFE_ES_LocateAppRecordByName(AppName);
if (AppRecPtr != NULL)
{
CFE_ES_SysLogWrite_Unsync("ES Startup: Duplicate app name '%s'\n", AppName);
Status = CFE_ES_ERR_DUPLICATE_NAME;
}
else
{
/* scan for a free slot */
PendingResourceId = CFE_ResourceId_FindNext(CFE_ES_Global.LastAppId, CFE_PLATFORM_ES_MAX_APPLICATIONS, CFE_ES_CheckAppIdSlotUsed);
AppRecPtr = CFE_ES_LocateAppRecordByID(CFE_ES_APPID_C(PendingResourceId));
if (AppRecPtr == NULL)
{
CFE_ES_SysLogWrite_Unsync("ES Startup: No free application slots available\n");
Status = CFE_ES_NO_RESOURCE_IDS_AVAILABLE;
}
else
{
/* Fully clear the entry, just in case of stale data */
memset(AppRecPtr, 0, sizeof(*AppRecPtr));
/*
* Fill out the parameters in the StartParams sub-structure
*/
AppRecPtr->Type = CFE_ES_AppType_EXTERNAL;
strncpy(AppRecPtr->StartParams.BasicInfo.Name, AppName,
sizeof(AppRecPtr->StartParams.BasicInfo.Name)-1);
AppRecPtr->StartParams.BasicInfo.Name[sizeof(AppRecPtr->StartParams.BasicInfo.Name)-1] = '\0';
strncpy(AppRecPtr->StartParams.BasicInfo.FileName, FileName,
sizeof(AppRecPtr->StartParams.BasicInfo.FileName)-1);
AppRecPtr->StartParams.BasicInfo.FileName[sizeof(AppRecPtr->StartParams.BasicInfo.FileName)-1] = '\0';
if (EntryPointName != NULL && strcmp(EntryPointName, "NULL") != 0)
{
strncpy(AppRecPtr->StartParams.BasicInfo.EntryPoint, EntryPointName,
sizeof(AppRecPtr->StartParams.BasicInfo.EntryPoint)-1);
AppRecPtr->StartParams.BasicInfo.EntryPoint[
sizeof(AppRecPtr->StartParams.BasicInfo.EntryPoint)-1] = '\0';
}
AppRecPtr->StartParams.StackSize = StackSize;
AppRecPtr->StartParams.ExceptionAction = ExceptionAction;
AppRecPtr->StartParams.Priority = Priority;
/*
* Fill out the Task State info
*/
AppRecPtr->ControlReq.AppControlRequest = CFE_ES_RunStatus_APP_RUN;
AppRecPtr->ControlReq.AppTimerMsec = 0;
CFE_ES_AppRecordSetUsed(AppRecPtr, CFE_RESOURCEID_RESERVED);
CFE_ES_Global.LastAppId = PendingResourceId;
Status = CFE_SUCCESS;
}
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* If ID allocation was not successful, return now.
* A message regarding the issue should have already been logged
*/
if (Status != CFE_SUCCESS)
{
return Status;
}
/*
* Load the module based on StartParams configured above.
*/
Status = CFE_ES_LoadModule(PendingResourceId, &AppRecPtr->StartParams.BasicInfo, &AppRecPtr->ModuleInfo);
/*
* If the Load was OK, then complete the initialization
*/
if (Status == CFE_SUCCESS)
{
Status = CFE_ES_StartAppTask(&AppRecPtr->StartParams, CFE_ES_APPID_C(PendingResourceId), &MainTaskId);
}
else
{
MainTaskId = CFE_ES_TASKID_UNDEFINED;
}
/*
* Finalize data in the app table entry, which must be done under lock.
* This transitions the entry from being RESERVED to the real ID.
*/
CFE_ES_LockSharedData(__func__,__LINE__);
if ( Status == CFE_SUCCESS )
{
/*
* important - set the ID to its proper value
* which turns this into a real/valid table entry
*/
AppRecPtr->MainTaskId = MainTaskId;
CFE_ES_AppRecordSetUsed(AppRecPtr, PendingResourceId);
/*
** Increment the registered App counter.
*/
CFE_ES_Global.RegisteredExternalApps++;
}
else
{
/*
* Set the table entry back to free
*/
CFE_ES_AppRecordSetFree(AppRecPtr);
PendingResourceId = CFE_RESOURCEID_UNDEFINED;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
*ApplicationIdPtr = CFE_ES_APPID_C(PendingResourceId);
return Status;
} /* End Function */
/*
**---------------------------------------------------------------------------------------
** Name: CFE_ES_LoadLibrary
**
** Purpose: This function loads and initializes a cFE Shared Library.
**
**---------------------------------------------------------------------------------------
*/
int32 CFE_ES_LoadLibrary(CFE_ES_LibId_t *LibraryIdPtr,
const char *FileName,
const char *EntryPointName,
const char *LibName)
{
CFE_ES_LibraryEntryFuncPtr_t FunctionPointer;
CFE_ES_LibRecord_t * LibSlotPtr;
int32 Status;
CFE_ResourceId_t PendingResourceId;
/*
* The FileName must not be NULL
*/
if (FileName == NULL || LibName == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
if (strlen(LibName) >= OS_MAX_API_NAME)
{
return CFE_ES_BAD_ARGUMENT;
}
/*
** Allocate an ES_LibTable entry
*/
FunctionPointer = NULL;
PendingResourceId = CFE_RESOURCEID_UNDEFINED;
/*
** Find an ES AppTable entry, and set to RESERVED
**
** In this state, the entry is no longer free, but also will not pass the
** validation test. So this function effectively has exclusive access
** without holding the global lock.
**
** IMPORTANT: it must set the ID to something else before leaving
** this function or else the resource will be leaked. After this
** point, execution must proceed to the end of the function to
** guarantee that the entry is either completed or freed.
*/
CFE_ES_LockSharedData(__func__,__LINE__);
/*
* Check for an existing entry with the same name.
* Also check for a matching Library name.
* (Libs and libraries should be uniquely named)
*/
LibSlotPtr = CFE_ES_LocateLibRecordByName(LibName);
if (LibSlotPtr != NULL || CFE_ES_LocateAppRecordByName(LibName) != NULL)
{
CFE_ES_SysLogWrite_Unsync("ES Startup: Duplicate Lib name '%s'\n", LibName);
if (LibSlotPtr != NULL)
{
PendingResourceId = CFE_RESOURCEID_UNWRAP(CFE_ES_LibRecordGetID(LibSlotPtr));
}
Status = CFE_ES_ERR_DUPLICATE_NAME;
}
else
{
/* scan for a free slot */
PendingResourceId = CFE_ResourceId_FindNext(CFE_ES_Global.LastLibId, CFE_PLATFORM_ES_MAX_LIBRARIES, CFE_ES_CheckLibIdSlotUsed);
LibSlotPtr = CFE_ES_LocateLibRecordByID(CFE_ES_LIBID_C(PendingResourceId));
if (LibSlotPtr == NULL)
{
CFE_ES_SysLogWrite_Unsync("ES Startup: No free library slots available\n");
Status = CFE_ES_NO_RESOURCE_IDS_AVAILABLE;
}
else
{
/* Fully clear the entry, just in case of stale data */
memset(LibSlotPtr, 0, sizeof(*LibSlotPtr));
/*
* Fill out the parameters in the AppStartParams sub-structure
*/
strncpy(LibSlotPtr->BasicInfo.Name, LibName,
sizeof(LibSlotPtr->BasicInfo.Name)-1);
LibSlotPtr->BasicInfo.Name[sizeof(LibSlotPtr->BasicInfo.Name)-1] = '\0';
strncpy(LibSlotPtr->BasicInfo.FileName, FileName,
sizeof(LibSlotPtr->BasicInfo.FileName)-1);
LibSlotPtr->BasicInfo.FileName[sizeof(LibSlotPtr->BasicInfo.FileName)-1] = '\0';
if (EntryPointName != NULL && strcmp(EntryPointName, "NULL") != 0)
{
strncpy(LibSlotPtr->BasicInfo.EntryPoint, EntryPointName,
sizeof(LibSlotPtr->BasicInfo.EntryPoint)-1);
LibSlotPtr->BasicInfo.EntryPoint[sizeof(LibSlotPtr->BasicInfo.EntryPoint)-1] = '\0';
}
CFE_ES_LibRecordSetUsed(LibSlotPtr, CFE_RESOURCEID_RESERVED);
CFE_ES_Global.LastLibId = PendingResourceId;
Status = CFE_SUCCESS;
}
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
/*
* If any off-nominal condition exists, skip the rest of this logic.
* (Log message already written)
*/
if (Status != CFE_SUCCESS)
{
*LibraryIdPtr = CFE_ES_LIBID_C(PendingResourceId);
return Status;
}
/*
* Load the module based on StartParams configured above.
*/
Status = CFE_ES_LoadModule(PendingResourceId, &LibSlotPtr->BasicInfo, &LibSlotPtr->ModuleInfo);
if (Status == CFE_SUCCESS)
{
FunctionPointer = (CFE_ES_LibraryEntryFuncPtr_t)LibSlotPtr->ModuleInfo.EntryAddress;
if (FunctionPointer != NULL)
{
Status = (*FunctionPointer)(CFE_ES_LIBID_C(PendingResourceId));
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("ES Startup: Load Shared Library Init Error = 0x%08x\n",
(unsigned int)Status);
}
}
}
/*
* Finalize data in the app table entry, which must be done under lock.
* This transitions the entry from being RESERVED to the real type,
* either MAIN_TASK (success) or returning to INVALID (failure).
*/
CFE_ES_LockSharedData(__func__,__LINE__);
if ( Status == CFE_SUCCESS )
{
/*
* important - set the ID to its proper value
* which turns this into a real/valid table entry
*/
CFE_ES_LibRecordSetUsed(LibSlotPtr, PendingResourceId);
/*
* Increment the registered Lib counter.
*/
CFE_ES_Global.RegisteredLibs++;
}
else
{
CFE_ES_LibRecordSetFree(LibSlotPtr);
PendingResourceId = CFE_RESOURCEID_UNDEFINED;
}
CFE_ES_UnlockSharedData(__func__,__LINE__);
*LibraryIdPtr = CFE_ES_LIBID_C(PendingResourceId);
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)
{
CFE_ES_AppTableScanState_t *State = (CFE_ES_AppTableScanState_t *)Arg;
uint32 i;
CFE_ES_AppRecord_t *AppPtr;
CFE_ES_AppId_t AppTimeoutList[CFE_PLATFORM_ES_MAX_APPLICATIONS];
uint32 NumAppTimeouts;
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 */