-
Notifications
You must be signed in to change notification settings - Fork 203
/
cfe_es_apps.c
1779 lines (1603 loc) · 59.6 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
/************************************************************************
* 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_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 "cfe_es_module_all.h"
#include "cfe_evs_core_internal.h"
#include "cfe_sb_core_internal.h"
#include "cfe_tbl_core_internal.h"
#include "cfe_time_core_internal.h"
#include <stdio.h>
#include <string.h> /* memset() */
#include <fcntl.h>
/*
** Defines
*/
#define ES_START_BUFF_SIZE 128
/*
**
** Global Variables
**
*/
/*
****************************************************************************
** Functions
***************************************************************************
*/
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
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 */
char ScriptFileName[OS_MAX_PATH_LEN];
const char *TokenList[CFE_ES_STARTSCRIPT_MAX_TOKENS_PER_LINE];
uint32 NumTokens;
uint32 NumLines;
size_t BuffLen; /* Length of the current buffer */
osal_id_t AppFile = OS_OBJECT_ID_UNDEFINED;
int32 Status;
int32 OsStatus;
char c = 0;
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)
{
/*
** First Attempt to parse as file in the volatile disk (temp area).
*/
Status = CFE_FS_ParseInputFileName(ScriptFileName, CFE_PLATFORM_ES_VOLATILE_STARTUP_FILE,
sizeof(ScriptFileName), CFE_FS_FileCategory_TEMP);
if (Status == CFE_SUCCESS)
{
OsStatus = OS_OpenCreate(&AppFile, ScriptFileName, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if (OsStatus == OS_SUCCESS)
{
FileOpened = true;
}
else
{
CFE_ES_WriteToSysLog("%s: Cannot Open Volatile Startup file: %s, Trying Nonvolatile.\n", __func__,
ScriptFileName);
}
}
else
{
/* not expected -- likely a misconfiguration in CFE_PLATFORM_ES_VOLATILE_STARTUP_FILE setting */
CFE_ES_WriteToSysLog("%s: CFE_FS_ParseInputFileName() RC=%08x parsing volatile script file name.\n",
__func__, (unsigned int)Status);
}
}
/*
** 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 = CFE_FS_ParseInputFileName(ScriptFileName, StartFilePath, sizeof(ScriptFileName),
CFE_FS_FileCategory_SCRIPT);
if (Status == CFE_SUCCESS)
{
OsStatus = OS_OpenCreate(&AppFile, ScriptFileName, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if (OsStatus == OS_SUCCESS)
{
FileOpened = true;
}
else
{
CFE_ES_WriteToSysLog("%s: Error, Can't Open ES App Startup file: %s, EC = %ld\n", __func__,
ScriptFileName, (long)OsStatus);
}
}
else
{
/* not expected -- likely a misconfiguration in the user-supplied StartFilePath */
CFE_ES_WriteToSysLog("%s: CFE_FS_ParseInputFileName() RC=%08x parsing StartFilePath.\n", __func__,
(unsigned int)Status);
}
}
/*
** If the file is opened in either the Nonvolatile or the Volatile disk, process it.
*/
if (FileOpened == true)
{
CFE_ES_WriteToSysLog("%s: Opened ES App Startup file: %s\n", __func__, ScriptFileName);
memset(ES_AppLoadBuffer, 0x0, ES_START_BUFF_SIZE);
BuffLen = 0;
NumTokens = 0;
NumLines = 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)
{
OsStatus = OS_read(AppFile, &c, 1);
if (OsStatus < OS_SUCCESS)
{
CFE_ES_WriteToSysLog("%s: Error Reading Startup file. EC = %ld\n", __func__, (long)OsStatus);
break;
}
else if (OsStatus == 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++;
++NumTokens;
if (NumTokens < CFE_ES_STARTSCRIPT_MAX_TOKENS_PER_LINE && !LineTooLong)
{
/*
* NOTE: pointer never dereferenced unless "LineTooLong" is false.
*/
TokenList[NumTokens] = &ES_AppLoadBuffer[BuffLen];
}
else
{
LineTooLong = true;
}
}
else if (c != ';')
{
/*
** Regular data gets copied in
*/
if (BuffLen < ES_START_BUFF_SIZE)
{
ES_AppLoadBuffer[BuffLen] = c;
}
else
{
LineTooLong = true;
}
BuffLen++;
}
else
{
++NumLines;
if (LineTooLong == true)
{
/*
** The line was not formed correctly
*/
CFE_ES_WriteToSysLog("%s: **WARNING** File Line %u is malformed: %u bytes, %u tokens.\n",
__func__, (unsigned int)NumLines, (unsigned int)BuffLen,
(unsigned int)NumTokens);
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);
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_ParseFileEntry(const char **TokenList, uint32 NumTokens)
{
const char * ModuleName;
const char * EntryType;
unsigned long ParsedValue;
union
{
CFE_ES_AppId_t AppId;
CFE_ES_LibId_t LibId;
} IdBuf;
int32 Status;
CFE_ES_AppStartParams_t ParamBuf;
memset(&ParamBuf, 0, sizeof(ParamBuf));
/*
** Check to see if the correct number of items were parsed
*/
if (NumTokens < 8)
{
CFE_ES_WriteToSysLog("%s: Invalid ES Startup file entry: %u\n", __func__, (unsigned int)NumTokens);
return CFE_ES_BAD_ARGUMENT;
}
/* Get pointers to specific tokens that are simple strings used as-is */
EntryType = TokenList[0];
ModuleName = TokenList[3];
/*
* Other tokens will need to be scrubbed/converted.
* Both Libraries and Apps use File Name (1) and Symbol Name (2) fields so copy those now
*/
memset(&ParamBuf, 0, sizeof(ParamBuf));
Status = CFE_FS_ParseInputFileName(ParamBuf.BasicInfo.FileName, TokenList[1], sizeof(ParamBuf.BasicInfo.FileName),
CFE_FS_FileCategory_DYNAMIC_MODULE);
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("%s: Invalid ES Startup script file name: %s\n", __func__, TokenList[1]);
return Status;
}
strncpy(ParamBuf.BasicInfo.InitSymbolName, TokenList[2], sizeof(ParamBuf.BasicInfo.InitSymbolName) - 1);
if (strcmp(EntryType, "CFE_APP") == 0)
{
CFE_ES_WriteToSysLog("%s: Loading file: %s, APP: %s\n", __func__, ParamBuf.BasicInfo.FileName, ModuleName);
/*
* Priority and Exception action have limited ranges, which is checked here
* Task priority cannot be bigger than OS_MAX_TASK_PRIORITY
*/
ParsedValue = strtoul(TokenList[4], NULL, 0);
if (ParsedValue > OS_MAX_TASK_PRIORITY)
{
ParamBuf.MainTaskInfo.Priority = OS_MAX_TASK_PRIORITY;
}
else
{
/* convert parsed value to correct type */
ParamBuf.MainTaskInfo.Priority = (CFE_ES_TaskPriority_Atom_t)ParsedValue;
}
/* No specific upper/lower limit for stack size - will pass value through */
ParamBuf.MainTaskInfo.StackSize = strtoul(TokenList[5], NULL, 0);
/*
** Validate Some parameters
** Exception action should be 0 ( Restart App ) or
** 1 ( Processor reset ). If it's non-zero, assume it means
** reset CPU.
*/
ParsedValue = strtoul(TokenList[7], NULL, 0);
if (ParsedValue > CFE_ES_ExceptionAction_RESTART_APP)
{
ParamBuf.ExceptionAction = CFE_ES_ExceptionAction_PROC_RESTART;
}
else
{
/* convert parsed value to correct type */
ParamBuf.ExceptionAction = (CFE_ES_ExceptionAction_Enum_t)ParsedValue;
}
/*
** Now create the application
*/
Status = CFE_ES_AppCreate(&IdBuf.AppId, ModuleName, &ParamBuf);
}
else if (strcmp(EntryType, "CFE_LIB") == 0)
{
CFE_ES_WriteToSysLog("%s: Loading shared library: %s\n", __func__, ParamBuf.BasicInfo.FileName);
/*
** Now load the library
*/
Status = CFE_ES_LoadLibrary(&IdBuf.LibId, ModuleName, &ParamBuf.BasicInfo);
}
else
{
CFE_ES_WriteToSysLog("%s: Unexpected EntryType %s in startup file.\n", __func__, EntryType);
Status = CFE_ES_ERR_APP_CREATE;
}
return Status;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_LoadModule(CFE_ResourceId_t ParentResourceId, const char *ModuleName,
const CFE_ES_ModuleLoadParams_t *LoadParams, CFE_ES_ModuleLoadStatus_t *LoadStatus)
{
osal_id_t ModuleId = OS_OBJECT_ID_UNDEFINED;
cpuaddr InitSymbolAddress;
int32 ReturnCode;
int32 OsStatus;
uint32 LoadFlags;
LoadFlags = 0;
InitSymbolAddress = 0;
ReturnCode = CFE_SUCCESS;
if (LoadParams->FileName[0] != 0)
{
switch (CFE_ResourceId_GetBase(ParentResourceId))
{
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.
*/
OsStatus = OS_ModuleLoad(&ModuleId, ModuleName, LoadParams->FileName, LoadFlags);
if (OsStatus != OS_SUCCESS)
{
CFE_ES_WriteToSysLog("%s: Could not load file:%s. EC = %ld\n", __func__, LoadParams->FileName,
(long)OsStatus);
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->InitSymbolName[0] != 0 &&
strcmp(LoadParams->InitSymbolName, "NULL") != 0)
{
OsStatus = OS_ModuleSymbolLookup(ModuleId, &InitSymbolAddress, LoadParams->InitSymbolName);
if (OsStatus != OS_SUCCESS)
{
CFE_ES_WriteToSysLog("%s: Could not find symbol:%s. EC = %ld\n", __func__, LoadParams->InitSymbolName,
(long)OsStatus);
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->InitSymbolAddress = InitSymbolAddress;
}
else if (OS_ObjectIdDefined(ModuleId))
{
/* If the module had been successfully loaded, then unload it,
* so that it does not consume resources */
OsStatus = OS_ModuleUnload(ModuleId);
if (OsStatus != OS_SUCCESS) /* There's not much we can do except notify */
{
CFE_ES_WriteToSysLog("%s: Failed to unload: %s. EC = %ld\n", __func__, ModuleName, (long)OsStatus);
}
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_GetTaskFunction(CFE_ES_TaskEntryFuncPtr_t *FuncPtr)
{
CFE_ES_TaskRecord_t * TaskRecPtr;
CFE_ES_TaskEntryFuncPtr_t EntryFunc;
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;
EntryFunc = NULL;
while (true)
{
OS_TaskDelay(CFE_PLATFORM_ES_STARTUP_SYNC_POLL_MSEC);
CFE_ES_LockSharedData(__func__, __LINE__);
TaskRecPtr = CFE_ES_GetTaskRecordByContext();
if (TaskRecPtr != NULL)
{
EntryFunc = TaskRecPtr->EntryFunc;
if (CFE_RESOURCEID_TEST_DEFINED(TaskRecPtr->AppId) && EntryFunc != 0)
{
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;
}
/* output function address to caller */
if (FuncPtr != NULL)
{
*FuncPtr = EntryFunc;
}
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_ES_TaskEntryPoint(void)
{
CFE_ES_TaskEntryFuncPtr_t RealEntryFunc;
if (CFE_ES_GetTaskFunction(&RealEntryFunc) == CFE_SUCCESS && RealEntryFunc != NULL)
{
/*
* Set the default exception environment, which should
* be done serialized (i.e. only one task at a time should
* call into CFE_PSP_SetDefaultExceptionEnvironment).
*/
CFE_ES_LockSharedData(__func__, __LINE__);
CFE_PSP_SetDefaultExceptionEnvironment();
CFE_ES_UnlockSharedData(__func__, __LINE__);
/*
* Call the actual task entry function
*/
(*RealEntryFunc)();
}
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_StartAppTask(CFE_ES_TaskId_t *TaskIdPtr, const char *TaskName, CFE_ES_TaskEntryFuncPtr_t EntryFunc,
const CFE_ES_TaskStartParams_t *Params, CFE_ES_AppId_t ParentAppId)
{
CFE_ES_TaskRecord_t *TaskRecPtr;
osal_id_t OsalTaskId = OS_OBJECT_ID_UNDEFINED;
CFE_ES_TaskId_t LocalTaskId;
int32 OsStatus;
int32 ReturnCode;
/*
* Create the primary task for the newly loaded task
*/
OsStatus = OS_TaskCreate(&OsalTaskId, /* task id */
TaskName, /* task name matches app name for main task */
CFE_ES_TaskEntryPoint, /* task function pointer */
Params->StackPtr, /* stack pointer (allocate if NULL) */
Params->StackSize, /* stack size */
Params->Priority, /* task priority */
OS_FP_ENABLED); /* task options */
CFE_ES_LockSharedData(__func__, __LINE__);
if (OsStatus == 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("%s: Error: ES_TaskTable slot for ID %lx in use at task creation!\n", __func__,
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 = ParentAppId;
TaskRecPtr->EntryFunc = EntryFunc;
TaskRecPtr->StartParams = *Params;
strncpy(TaskRecPtr->TaskName, TaskName, 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("%s: AppCreate Error: TaskCreate %s Failed. EC = %ld!\n", __func__, TaskName,
(long)OsStatus);
ReturnCode = CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
*TaskIdPtr = CFE_ES_TASKID_UNDEFINED;
}
CFE_ES_UnlockSharedData(__func__, __LINE__);
return ReturnCode;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_AppCreate(CFE_ES_AppId_t *ApplicationIdPtr, const char *AppName, const CFE_ES_AppStartParams_t *Params)
{
CFE_Status_t Status;
CFE_ES_AppRecord_t *AppRecPtr;
CFE_ResourceId_t PendingResourceId = CFE_RESOURCEID_UNDEFINED;
/*
* The AppName must not be NULL
*/
if (AppName == NULL || Params == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
/* Confirm name will fit inside the record */
if (memchr(AppName, 0, sizeof(AppRecPtr->AppName)) == NULL)
{
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("%s: Duplicate app name '%s'\n", __func__, 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("%s: No free application slots available\n", __func__);
Status = CFE_ES_NO_RESOURCE_IDS_AVAILABLE;
}
else
{
/* Fully clear the entry, just in case of stale data */
memset(AppRecPtr, 0, sizeof(*AppRecPtr));
/* Store the app name from passed-in value */
strncpy(AppRecPtr->AppName, AppName, sizeof(AppRecPtr->AppName) - 1);
AppRecPtr->Type = CFE_ES_AppType_EXTERNAL;
/*
* Fill out the parameters in the StartParams sub-structure
*
* This contains all relevant info, including file name, entry point,
* main task info, etc. which is required to start the app now
* or in a future restart/reload request.
*/
AppRecPtr->StartParams = *Params;
/*
* 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, AppName, &AppRecPtr->StartParams.BasicInfo, &AppRecPtr->LoadStatus);
/*
* If the Load was OK, then complete the initialization
*/
if (Status == CFE_SUCCESS)
{
Status =
CFE_ES_StartAppTask(&AppRecPtr->MainTaskId, /* Task ID (output) stored in App Record as main task */
AppName, /* Main Task name matches app name */
(CFE_ES_TaskEntryFuncPtr_t)
AppRecPtr->LoadStatus.InitSymbolAddress, /* Init Symbol is main task entry point */
&AppRecPtr->StartParams.MainTaskInfo, /* Main task parameters */
CFE_ES_APPID_C(PendingResourceId)); /* Parent App ID */
}
/*
* 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
*/
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;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_ES_LoadLibrary(CFE_ES_LibId_t *LibraryIdPtr, const char *LibName, const CFE_ES_ModuleLoadParams_t *Params)
{
CFE_ES_LibraryEntryFuncPtr_t FunctionPointer;
CFE_ES_LibRecord_t * LibSlotPtr;
int32 Status;
CFE_ResourceId_t PendingResourceId;
/*
* The LibName must not be NULL
*/
if (LibName == NULL || Params == NULL)
{
return CFE_ES_BAD_ARGUMENT;
}
/* Confirm name will fit inside the record */
if (memchr(LibName, 0, sizeof(LibSlotPtr->LibName)) == NULL)
{
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("%s: Duplicate Lib name '%s'\n", __func__, 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("%s: No free library slots available\n", __func__);
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->LibName, LibName, sizeof(LibSlotPtr->LibName) - 1);
LibSlotPtr->LibName[sizeof(LibSlotPtr->LibName) - 1] = '\0';
LibSlotPtr->LoadParams = *Params;
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, LibName, &LibSlotPtr->LoadParams, &LibSlotPtr->LoadStatus);
if (Status == CFE_SUCCESS)
{
FunctionPointer = (CFE_ES_LibraryEntryFuncPtr_t)LibSlotPtr->LoadStatus.InitSymbolAddress;
if (FunctionPointer != NULL)
{
Status = (*FunctionPointer)(CFE_ES_LIBID_C(PendingResourceId));
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("%s: Load Shared Library Init Error = 0x%08x\n", __func__, (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;
}
/*----------------------------------------------------------------
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
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_Global.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 command counter.
*/
NumAppTimeouts = 0;