-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_tbl_api.c
1625 lines (1391 loc) · 67.3 KB
/
cfe_tbl_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
** File: cfe_tbl_api.c
**
** Purpose: cFE Table Services (TBL) library API source file
**
** Author: D. Kobe/the Hammers Company, Inc.
**
** Notes:
**
*/
/*
** Required header files...
*/
#include "cfe_tbl_module_all.h"
#include <string.h>
/*
** Local Macros
*/
/*----------------------------------------------------------------
*
* Function: CFE_TBL_Register
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_Register(CFE_TBL_Handle_t *TblHandlePtr, const char *Name, size_t Size, uint16 TblOptionFlags,
CFE_TBL_CallbackFuncPtr_t TblValidationFuncPtr)
{
CFE_TBL_AccessDescriptor_t *AccessDescPtr = NULL;
CFE_TBL_RegistryRec_t * RegRecPtr = NULL;
CFE_TBL_LoadBuff_t * WorkingBufferPtr;
CFE_TBL_CritRegRec_t * CritRegRecPtr = NULL;
int32 Status;
size_t NameLen;
int16 RegIndx;
CFE_ES_AppId_t ThisAppId;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
char TblName[CFE_TBL_MAX_FULL_NAME_LEN] = {""};
CFE_TBL_Handle_t AccessIndex;
if (TblHandlePtr == NULL || Name == NULL)
{
return CFE_TBL_BAD_ARGUMENT;
}
/* Check to make sure calling application is legit */
Status = CFE_ES_GetAppID(&ThisAppId);
if (Status == CFE_SUCCESS)
{
/* Assume we can't make a table and return a bad handle for now */
*TblHandlePtr = CFE_TBL_BAD_TABLE_HANDLE;
/* Make sure specified table name is not too long or too short */
NameLen = strlen(Name);
if ((NameLen > CFE_MISSION_TBL_MAX_NAME_LENGTH) || (NameLen == 0))
{
Status = CFE_TBL_ERR_INVALID_NAME;
/* Perform a buffer overrun safe copy of name for debug log message */
strncpy(TblName, Name, sizeof(TblName) - 1);
TblName[sizeof(TblName) - 1] = '\0';
CFE_ES_WriteToSysLog("CFE_TBL:Register-Table Name (%s) is bad length (%d)", TblName, (int)NameLen);
}
else
{
/* Modify specified name to be processor specific name */
/* of the form "AppName.TableName" */
CFE_TBL_FormTableName(TblName, Name, ThisAppId);
/* Make sure the specified size is acceptable */
/* Single buffered tables are allowed to be up to CFE_PLATFORM_TBL_MAX_SNGL_TABLE_SIZE */
/* Double buffered tables are allowed to be up to CFE_PLATFORM_TBL_MAX_DBL_TABLE_SIZE */
if (Size == 0)
{
Status = CFE_TBL_ERR_INVALID_SIZE;
CFE_ES_WriteToSysLog("CFE_TBL:Register-Table %s has size of zero\n", Name);
}
else if ((Size > CFE_PLATFORM_TBL_MAX_SNGL_TABLE_SIZE) &&
((TblOptionFlags & CFE_TBL_OPT_BUFFER_MSK) == CFE_TBL_OPT_SNGL_BUFFER))
{
Status = CFE_TBL_ERR_INVALID_SIZE;
CFE_ES_WriteToSysLog("CFE_TBL:Register-Single Buffered Table '%s' has size %d > %d\n", Name, (int)Size,
CFE_PLATFORM_TBL_MAX_SNGL_TABLE_SIZE);
}
else if ((Size > CFE_PLATFORM_TBL_MAX_DBL_TABLE_SIZE) &&
((TblOptionFlags & CFE_TBL_OPT_BUFFER_MSK) == CFE_TBL_OPT_DBL_BUFFER))
{
Status = CFE_TBL_ERR_INVALID_SIZE;
CFE_ES_WriteToSysLog("CFE_TBL:Register-Dbl Buffered Table '%s' has size %d > %d\n", Name, (int)Size,
CFE_PLATFORM_TBL_MAX_DBL_TABLE_SIZE);
}
/* Verify Table Option settings are legal */
/* User defined table addresses are only legal for single buffered, dump-only, non-critical tables */
if ((TblOptionFlags & CFE_TBL_OPT_USR_DEF_MSK) == (CFE_TBL_OPT_USR_DEF_ADDR & CFE_TBL_OPT_USR_DEF_MSK))
{
if (((TblOptionFlags & CFE_TBL_OPT_BUFFER_MSK) == CFE_TBL_OPT_DBL_BUFFER) ||
((TblOptionFlags & CFE_TBL_OPT_LD_DMP_MSK) == CFE_TBL_OPT_LOAD_DUMP) ||
((TblOptionFlags & CFE_TBL_OPT_CRITICAL_MSK) == CFE_TBL_OPT_CRITICAL))
{
Status = CFE_TBL_ERR_INVALID_OPTIONS;
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-User Def tbl '%s' cannot be dbl buff, load/dump or critical\n", Name);
}
}
else if ((TblOptionFlags & CFE_TBL_OPT_LD_DMP_MSK) == CFE_TBL_OPT_DUMP_ONLY)
{
/* Dump Only tables cannot be double buffered, nor critical */
if (((TblOptionFlags & CFE_TBL_OPT_BUFFER_MSK) == CFE_TBL_OPT_DBL_BUFFER) ||
((TblOptionFlags & CFE_TBL_OPT_CRITICAL_MSK) == CFE_TBL_OPT_CRITICAL))
{
Status = CFE_TBL_ERR_INVALID_OPTIONS;
CFE_ES_WriteToSysLog("CFE_TBL:Register-Dump Only tbl '%s' cannot be double buffered or critical\n",
Name);
}
}
}
}
else /* Application ID was invalid */
{
CFE_ES_WriteToSysLog("CFE_TBL:Register-Bad AppId(%lu)\n", CFE_RESOURCEID_TO_ULONG(ThisAppId));
}
/* If input parameters appear acceptable, register the table */
if (Status == CFE_SUCCESS)
{
/* Lock Registry for update. This prevents two applications from */
/* trying to register/share tables at the same location at the same time */
CFE_TBL_LockRegistry();
/* Check for duplicate table name */
RegIndx = CFE_TBL_FindTableInRegistry(TblName);
/* Check to see if table is already in the registry */
if (RegIndx != CFE_TBL_NOT_FOUND)
{
/* Get pointer to Registry Record Entry to speed up processing */
RegRecPtr = &CFE_TBL_Global.Registry[RegIndx];
/* If this app previously owned the table, then allow them to re-register */
if (CFE_RESOURCEID_TEST_EQUAL(RegRecPtr->OwnerAppId, ThisAppId))
{
/* If the new table is the same size as the old, then no need to reallocate memory */
if (Size != RegRecPtr->Size)
{
/* If the new size is different, the old table must deleted */
/* but this function can't do that because it is probably shared */
/* and is probably still being accessed. Someone else will need */
/* to clean up this mess. */
Status = CFE_TBL_ERR_DUPLICATE_DIFF_SIZE;
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Attempt to register existing table ('%s') with different size(%d!=%d)\n",
TblName, (int)Size, (int)RegRecPtr->Size);
}
else
{
/* Warn calling application that this is a duplicate registration */
Status = CFE_TBL_WARN_DUPLICATE;
/* Find the existing access descriptor for the table */
/* and return the same handle that was returned previously */
AccessIndex = RegRecPtr->HeadOfAccessList;
while ((AccessIndex != CFE_TBL_END_OF_LIST) && (*TblHandlePtr == CFE_TBL_BAD_TABLE_HANDLE))
{
if ((CFE_TBL_Global.Handles[AccessIndex].UsedFlag == true) &&
CFE_RESOURCEID_TEST_EQUAL(CFE_TBL_Global.Handles[AccessIndex].AppId, ThisAppId) &&
(CFE_TBL_Global.Handles[AccessIndex].RegIndex == RegIndx))
{
*TblHandlePtr = AccessIndex;
}
else
{
AccessIndex = CFE_TBL_Global.Handles[AccessIndex].NextLink;
}
}
}
}
else /* Duplicate named table owned by another Application */
{
Status = CFE_TBL_ERR_DUPLICATE_NOT_OWNED;
CFE_ES_WriteToSysLog("CFE_TBL:Register-App(%lu) Registering Duplicate Table '%s' owned by App(%lu)\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), TblName,
CFE_RESOURCEID_TO_ULONG(RegRecPtr->OwnerAppId));
}
}
else /* Table not already in registry */
{
/* Locate empty slot in table registry */
RegIndx = CFE_TBL_FindFreeRegistryEntry();
}
/* Check to make sure we found a free entry in registry */
if (RegIndx == CFE_TBL_NOT_FOUND)
{
Status = CFE_TBL_ERR_REGISTRY_FULL;
CFE_ES_WriteToSysLog("CFE_TBL:Register-Registry full\n");
}
/* If this is a duplicate registration, no other work is required */
if (Status != CFE_TBL_WARN_DUPLICATE)
{
/* Search Access Descriptor Array for free Descriptor */
*TblHandlePtr = CFE_TBL_FindFreeHandle();
/* Check to make sure there was a handle available */
if (*TblHandlePtr == CFE_TBL_END_OF_LIST)
{
Status = CFE_TBL_ERR_HANDLES_FULL;
CFE_ES_WriteToSysLog("CFE_TBL:Register-No more free handles\n");
}
/* If no errors, then initialize the table registry entry */
/* and return the registry index to the caller as the handle */
if ((Status & CFE_SEVERITY_BITMASK) != CFE_SEVERITY_ERROR)
{
/* Get pointer to Registry Record Entry to speed up processing */
RegRecPtr = &CFE_TBL_Global.Registry[RegIndx];
/* Initialize Registry Record to default settings */
CFE_TBL_InitRegistryRecord(RegRecPtr);
if ((TblOptionFlags & CFE_TBL_OPT_USR_DEF_MSK) != (CFE_TBL_OPT_USR_DEF_ADDR & CFE_TBL_OPT_USR_DEF_MSK))
{
RegRecPtr->UserDefAddr = false;
/* Allocate the memory buffer(s) for the table and inactive table, if necessary */
Status = CFE_ES_GetPoolBuf(&RegRecPtr->Buffers[0].BufferPtr, CFE_TBL_Global.Buf.PoolHdl, Size);
if (Status < 0)
{
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-1st Buf Alloc GetPool fail Stat=0x%08X MemPoolHndl=0x%08lX\n",
(unsigned int)Status, CFE_RESOURCEID_TO_ULONG(CFE_TBL_Global.Buf.PoolHdl));
}
else
{
/* Zero the memory buffer */
Status = CFE_SUCCESS;
memset(RegRecPtr->Buffers[0].BufferPtr, 0x0, Size);
}
}
else
{
/* Set buffer pointer to NULL for user defined address tables */
RegRecPtr->Buffers[0].BufferPtr = NULL;
RegRecPtr->UserDefAddr = true;
}
if (((TblOptionFlags & CFE_TBL_OPT_DBL_BUFFER) == CFE_TBL_OPT_DBL_BUFFER) &&
((Status & CFE_SEVERITY_BITMASK) != CFE_SEVERITY_ERROR))
{
/* Allocate memory for the dedicated secondary buffer */
Status = CFE_ES_GetPoolBuf(&RegRecPtr->Buffers[1].BufferPtr, CFE_TBL_Global.Buf.PoolHdl, Size);
if (Status < 0)
{
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-2nd Buf Alloc GetPool fail Stat=0x%08X MemPoolHndl=0x%08lX\n",
(unsigned int)Status, CFE_RESOURCEID_TO_ULONG(CFE_TBL_Global.Buf.PoolHdl));
}
else
{
/* Zero the dedicated secondary buffer */
Status = CFE_SUCCESS;
memset(RegRecPtr->Buffers[1].BufferPtr, 0x0, Size);
}
RegRecPtr->ActiveBufferIndex = 0;
RegRecPtr->DoubleBuffered = true;
}
else /* Single Buffered Table */
{
RegRecPtr->DoubleBuffered = false;
RegRecPtr->ActiveBufferIndex = 0;
}
if ((Status & CFE_SEVERITY_BITMASK) != CFE_SEVERITY_ERROR)
{
/* Save the size of the table */
RegRecPtr->Size = Size;
/* Save the Callback function pointer */
RegRecPtr->ValidationFuncPtr = TblValidationFuncPtr;
/* Save Table Name in Registry */
strncpy(RegRecPtr->Name, TblName, sizeof(RegRecPtr->Name) - 1);
RegRecPtr->Name[sizeof(RegRecPtr->Name) - 1] = '\0';
/* Set the "Dump Only" flag to value based upon selected option */
if ((TblOptionFlags & CFE_TBL_OPT_LD_DMP_MSK) == CFE_TBL_OPT_DUMP_ONLY)
{
RegRecPtr->DumpOnly = true;
}
else
{
RegRecPtr->DumpOnly = false;
}
/* Initialize the Table Access Descriptor */
AccessDescPtr = &CFE_TBL_Global.Handles[*TblHandlePtr];
AccessDescPtr->AppId = ThisAppId;
AccessDescPtr->LockFlag = false;
AccessDescPtr->Updated = false;
if ((RegRecPtr->DumpOnly) && (!RegRecPtr->UserDefAddr))
{
/* Dump Only Tables are assumed to be loaded at all times */
/* unless the address is specified by the application. In */
/* that case, it isn't loaded until the address is specified */
RegRecPtr->TableLoadedOnce = true;
}
AccessDescPtr->RegIndex = RegIndx;
AccessDescPtr->PrevLink = CFE_TBL_END_OF_LIST; /* We are the head of the list */
AccessDescPtr->NextLink = CFE_TBL_END_OF_LIST; /* We are the end of the list */
AccessDescPtr->UsedFlag = true;
/* Make sure the Table Registry entry points to First Access Descriptor */
RegRecPtr->HeadOfAccessList = *TblHandlePtr;
/* If the table is a critical table, allocate space for it in the Critical Data Store */
/* OR locate its previous incarnation there and extract its previous contents */
if ((TblOptionFlags & CFE_TBL_OPT_CRITICAL_MSK) == CFE_TBL_OPT_CRITICAL)
{
/* Register a CDS under the table name and determine if the table already exists there */
Status = CFE_ES_RegisterCDSEx(&RegRecPtr->CDSHandle, Size, TblName, true);
if (Status == CFE_ES_CDS_ALREADY_EXISTS)
{
Status = CFE_TBL_GetWorkingBuffer(&WorkingBufferPtr, RegRecPtr, true);
if (Status != CFE_SUCCESS)
{
/* Unable to get a working buffer - this error is not really */
/* possible at this point during table registration. But we */
/* do need to handle the error case because if the function */
/* call did fail, WorkingBufferPtr would be a NULL pointer. */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Failed to get work buffer for '%s.%s' (ErrCode=0x%08X)\n",
AppName, Name, (unsigned int)Status);
}
else
{
/* CDS exists for this table - try to restore the data */
Status = CFE_ES_RestoreFromCDS(WorkingBufferPtr->BufferPtr, RegRecPtr->CDSHandle);
if (Status != CFE_SUCCESS)
{
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Failed to recover '%s.%s' from CDS (ErrCode=0x%08X)\n",
AppName, Name, (unsigned int)Status);
}
}
if (Status != CFE_SUCCESS)
{
/* Treat a restore from existing CDS error the same as */
/* after a power-on reset (CDS was created but is empty) */
Status = CFE_SUCCESS;
}
else
{
/* Try to locate the associated information in the Critical Table Registry */
CFE_TBL_FindCriticalTblInfo(&CritRegRecPtr, RegRecPtr->CDSHandle);
if ((CritRegRecPtr != NULL) && (CritRegRecPtr->TableLoadedOnce))
{
strncpy(WorkingBufferPtr->DataSource, CritRegRecPtr->LastFileLoaded,
sizeof(WorkingBufferPtr->DataSource) - 1);
WorkingBufferPtr->DataSource[sizeof(WorkingBufferPtr->DataSource) - 1] = '\0';
WorkingBufferPtr->FileCreateTimeSecs = CritRegRecPtr->FileCreateTimeSecs;
WorkingBufferPtr->FileCreateTimeSubSecs = CritRegRecPtr->FileCreateTimeSubSecs;
strncpy(RegRecPtr->LastFileLoaded, CritRegRecPtr->LastFileLoaded,
sizeof(RegRecPtr->LastFileLoaded) - 1);
RegRecPtr->LastFileLoaded[sizeof(RegRecPtr->LastFileLoaded) - 1] = '\0';
RegRecPtr->TimeOfLastUpdate.Seconds = CritRegRecPtr->TimeOfLastUpdate.Seconds;
RegRecPtr->TimeOfLastUpdate.Subseconds = CritRegRecPtr->TimeOfLastUpdate.Subseconds;
RegRecPtr->TableLoadedOnce = CritRegRecPtr->TableLoadedOnce;
/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc = CFE_ES_CalculateCRC(
WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
/* Make sure everyone who sees the table knows that it has been updated */
CFE_TBL_NotifyTblUsersOfUpdate(RegRecPtr);
/* Make sure the caller realizes the contents have been initialized */
Status = CFE_TBL_INFO_RECOVERED_TBL;
}
else
{
/* If an error occurred while trying to get the previous contents registry info, */
/* Log the error in the System Log and pretend like we created a new CDS */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Failed to recover '%s.%s' info from CDS TblReg\n", AppName,
Name);
Status = CFE_SUCCESS;
}
}
/* Mark the table as critical for future reference */
RegRecPtr->CriticalTable = true;
}
if (Status == CFE_SUCCESS)
{
/* Find and initialize a free entry in the Critical Table Registry */
CFE_TBL_FindCriticalTblInfo(&CritRegRecPtr, CFE_ES_CDS_BAD_HANDLE);
if (CritRegRecPtr != NULL)
{
CritRegRecPtr->CDSHandle = RegRecPtr->CDSHandle;
strncpy(CritRegRecPtr->Name, TblName, sizeof(CritRegRecPtr->Name) - 1);
CritRegRecPtr->Name[sizeof(CritRegRecPtr->Name) - 1] = '\0';
CritRegRecPtr->FileCreateTimeSecs = 0;
CritRegRecPtr->FileCreateTimeSubSecs = 0;
CritRegRecPtr->LastFileLoaded[0] = '\0';
CritRegRecPtr->TimeOfLastUpdate.Seconds = 0;
CritRegRecPtr->TimeOfLastUpdate.Subseconds = 0;
CritRegRecPtr->TableLoadedOnce = false;
CFE_ES_CopyToCDS(CFE_TBL_Global.CritRegHandle, CFE_TBL_Global.CritReg);
}
else
{
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Failed to find a free Crit Tbl Reg Rec for '%s'\n",
RegRecPtr->Name);
}
/* Mark the table as critical for future reference */
RegRecPtr->CriticalTable = true;
}
else if (Status != CFE_TBL_INFO_RECOVERED_TBL)
{
CFE_ES_WriteToSysLog(
"CFE_TBL:Register-Failed to register '%s.%s' as a CDS (ErrCode=0x%08X)\n", AppName,
Name, (unsigned int)Status);
/* Notify caller that although they asked for it to be critical, it isn't */
Status = CFE_TBL_WARN_NOT_CRITICAL;
}
}
/* The last step of the registration process is claiming ownership. */
/* By making it the last step, other APIs do not have to lock registry */
/* to share the table or get its address because registry entries that */
/* are unowned are not checked to see if they match names, etc. */
RegRecPtr->OwnerAppId = ThisAppId;
}
}
}
/* Unlock Registry for update */
CFE_TBL_UnlockRegistry();
}
/* On Error conditions, notify ground of screw up */
if (Status < 0)
{
/* Make sure the returned handle is invalid when an error occurs */
*TblHandlePtr = CFE_TBL_BAD_TABLE_HANDLE;
/* Translate AppID of caller into App Name */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_EVS_SendEventWithAppID(CFE_TBL_REGISTER_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s Failed to Register '%s', Status=0x%08X", AppName, TblName, (unsigned int)Status);
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_Share
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_Share(CFE_TBL_Handle_t *TblHandlePtr, const char *TblName)
{
int32 Status;
CFE_ES_AppId_t ThisAppId;
int16 RegIndx;
CFE_TBL_AccessDescriptor_t *AccessDescPtr = NULL;
CFE_TBL_RegistryRec_t * RegRecPtr = NULL;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
if (TblHandlePtr == NULL || TblName == NULL)
{
return CFE_TBL_BAD_ARGUMENT;
}
/* Get a valid Application ID for calling App */
Status = CFE_ES_GetAppID(&ThisAppId);
if (Status == CFE_SUCCESS)
{
/* Lock Registry for update. This prevents two applications from */
/* trying to register/share tables at the same location at the same time */
CFE_TBL_LockRegistry();
RegIndx = CFE_TBL_FindTableInRegistry(TblName);
/* If we found the table, then get a new Access Descriptor and initialize it */
if (RegIndx != CFE_TBL_NOT_FOUND)
{
/* Get pointer to Registry Record Entry to speed up processing */
RegRecPtr = &CFE_TBL_Global.Registry[RegIndx];
/* Search Access Descriptor Array for free Descriptor */
*TblHandlePtr = CFE_TBL_FindFreeHandle();
/* Check to make sure there was a handle available */
if (*TblHandlePtr == CFE_TBL_END_OF_LIST)
{
Status = CFE_TBL_ERR_HANDLES_FULL;
CFE_ES_WriteToSysLog("CFE_TBL:Share-No more free handles\n");
}
else
{
/* Initialize the Table Access Descriptor */
AccessDescPtr = &CFE_TBL_Global.Handles[*TblHandlePtr];
AccessDescPtr->AppId = ThisAppId;
AccessDescPtr->LockFlag = false;
AccessDescPtr->Updated = false;
/* Check current state of table in order to set Notification flags properly */
if (RegRecPtr->TableLoadedOnce)
{
AccessDescPtr->Updated = true;
}
AccessDescPtr->RegIndex = RegIndx;
AccessDescPtr->UsedFlag = true;
AccessDescPtr->PrevLink = CFE_TBL_END_OF_LIST; /* We are the new head of the list */
AccessDescPtr->NextLink = RegRecPtr->HeadOfAccessList;
/* Make sure the old head of the list now sees this as the head */
CFE_TBL_Global.Handles[RegRecPtr->HeadOfAccessList].PrevLink = *TblHandlePtr;
/* Make sure the Registry Record see this as the head of the list */
RegRecPtr->HeadOfAccessList = *TblHandlePtr;
}
}
else /* Table could not be found in registry */
{
Status = CFE_TBL_ERR_INVALID_NAME;
CFE_ES_WriteToSysLog("CFE_TBL:Share-Table '%s' not found in Registry\n", TblName);
}
CFE_TBL_UnlockRegistry();
}
else /* Application ID was invalid */
{
CFE_ES_WriteToSysLog("CFE_TBL:Share-Bad AppId(%lu)\n", CFE_RESOURCEID_TO_ULONG(ThisAppId));
}
/* On Error conditions, notify ground of screw up */
if (Status < 0)
{
/* Translate AppID of caller into App Name */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_EVS_SendEventWithAppID(CFE_TBL_SHARE_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s Failed to Share '%s', Status=0x%08X", AppName, TblName, (unsigned int)Status);
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_Unregister
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_Unregister(CFE_TBL_Handle_t TblHandle)
{
int32 Status;
CFE_ES_AppId_t ThisAppId;
CFE_TBL_RegistryRec_t * RegRecPtr = NULL;
CFE_TBL_AccessDescriptor_t *AccessDescPtr = NULL;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
/* Verify that this application has the right to perform operation */
Status = CFE_TBL_ValidateAccess(TblHandle, &ThisAppId);
if (Status == CFE_SUCCESS)
{
/* Get a pointer to the relevant Access Descriptor */
AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
/* Get a pointer to the relevant entry in the registry */
RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
/* Verify that the application unregistering the table owns the table */
if (CFE_RESOURCEID_TEST_EQUAL(RegRecPtr->OwnerAppId, ThisAppId))
{
/* Mark table as free, although, technically, it isn't free until the */
/* linked list of Access Descriptors has no links in it. */
/* NOTE: Allocated memory is freed when all Access Links have been */
/* removed. This allows Applications to continue to use the */
/* data until they acknowledge that the table has been removed. */
RegRecPtr->OwnerAppId = CFE_TBL_NOT_OWNED;
/* Remove Table Name */
RegRecPtr->Name[0] = '\0';
}
/* Remove the Access Descriptor Link from linked list */
/* NOTE: If this removes the last access link, then */
/* memory buffers are set free as well. */
CFE_TBL_RemoveAccessLink(TblHandle);
}
else
{
CFE_ES_WriteToSysLog("CFE_TBL:Unregister-App(%lu) does not have access to Tbl Handle=%d\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), (int)TblHandle);
}
/* On Error conditions, notify ground of screw up */
if (Status < 0)
{
/* Translate AppID of caller into App Name */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
CFE_EVS_SendEventWithAppID(CFE_TBL_UNREGISTER_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s Failed to Unregister '?', Status=0x%08X", AppName, (unsigned int)Status);
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_Load
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_Load(CFE_TBL_Handle_t TblHandle, CFE_TBL_SrcEnum_t SrcType, const void *SrcDataPtr)
{
int32 Status;
CFE_ES_AppId_t ThisAppId;
CFE_TBL_LoadBuff_t * WorkingBufferPtr;
CFE_TBL_AccessDescriptor_t *AccessDescPtr;
CFE_TBL_RegistryRec_t * RegRecPtr;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
bool FirstTime = false;
if (SrcDataPtr == NULL)
{
return CFE_TBL_BAD_ARGUMENT;
}
/* Verify access rights and get a valid Application ID for calling App */
Status = CFE_TBL_ValidateAccess(TblHandle, &ThisAppId);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_HANDLE_ACCESS_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: No access to Tbl Handle=%d", AppName,
(int)TblHandle);
return Status;
}
AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
/* Translate AppID of caller into App Name */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
/* Initialize return pointer to NULL */
WorkingBufferPtr = NULL;
/* Check to see if this is a dump only table */
if (RegRecPtr->DumpOnly)
{
if ((!RegRecPtr->UserDefAddr) || (RegRecPtr->TableLoadedOnce))
{
CFE_EVS_SendEventWithAppID(CFE_TBL_LOADING_A_DUMP_ONLY_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: Attempted to load Dump Only Tbl '%s'",
AppName, RegRecPtr->Name);
return CFE_TBL_ERR_DUMP_ONLY;
}
/* The Application is allowed to call Load once when the address */
/* of the dump only table is being defined by the application. */
RegRecPtr->Buffers[0].BufferPtr = (void *)SrcDataPtr;
RegRecPtr->TableLoadedOnce = true;
snprintf(RegRecPtr->Buffers[0].DataSource, sizeof(RegRecPtr->Buffers[0].DataSource), "Addr 0x%08lX",
(unsigned long)SrcDataPtr);
RegRecPtr->Buffers[0].FileCreateTimeSecs = 0;
RegRecPtr->Buffers[0].FileCreateTimeSubSecs = 0;
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_SUCCESS_INF_EID, CFE_EVS_EventType_DEBUG, CFE_TBL_Global.TableTaskAppId,
"Successfully loaded '%s' from '%s'", RegRecPtr->Name,
RegRecPtr->Buffers[0].DataSource);
return CFE_SUCCESS;
}
/* Loads by an Application are not allowed if a table load is already in progress */
if (RegRecPtr->LoadInProgress != CFE_TBL_NO_LOAD_IN_PROGRESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_IN_PROGRESS_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: Load already in progress for '%s'", AppName,
RegRecPtr->Name);
return CFE_TBL_ERR_LOAD_IN_PROGRESS;
}
/* Obtain a working buffer (either the table's dedicated buffer or one of the shared buffers) */
Status = CFE_TBL_GetWorkingBuffer(&WorkingBufferPtr, RegRecPtr, true);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_NO_WORK_BUFFERS_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: Failed to get Working Buffer (Stat=%u)", AppName,
(unsigned int)Status);
return Status;
}
/* Perform appropriate update to working buffer */
/* Determine whether the load is to occur from a file or from a block of memory */
switch (SrcType)
{
case CFE_TBL_SRC_FILE:
/* Load the data from the file into the specified buffer */
Status = CFE_TBL_LoadFromFile(AppName, WorkingBufferPtr, RegRecPtr, (const char *)SrcDataPtr);
if ((Status == CFE_TBL_WARN_PARTIAL_LOAD) && (!RegRecPtr->TableLoadedOnce))
{
/* Uninitialized tables cannot be loaded with partial table loads */
/* Partial loads can only occur on previously loaded tables. */
CFE_EVS_SendEventWithAppID(CFE_TBL_PARTIAL_LOAD_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId,
"%s: Attempted to load from partial Tbl '%s' from '%s' (Stat=%u)", AppName,
RegRecPtr->Name, (const char *)SrcDataPtr, (unsigned int)Status);
Status = CFE_TBL_ERR_PARTIAL_LOAD;
}
break;
case CFE_TBL_SRC_ADDRESS:
/* When the source is a block of memory, it is assumed to be a complete load */
memcpy(WorkingBufferPtr->BufferPtr, (uint8 *)SrcDataPtr, RegRecPtr->Size);
snprintf(WorkingBufferPtr->DataSource, sizeof(WorkingBufferPtr->DataSource), "Addr 0x%08lX",
(unsigned long)SrcDataPtr);
WorkingBufferPtr->FileCreateTimeSecs = 0;
WorkingBufferPtr->FileCreateTimeSubSecs = 0;
/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc =
CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
break;
default:
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_TYPE_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId,
"%s: Attempted to load from illegal source type=%d", AppName, (int)SrcType);
Status = CFE_TBL_ERR_ILLEGAL_SRC_TYPE;
}
/* If the data was successfully loaded, then validate its contents */
if ((Status >= CFE_SUCCESS) && (RegRecPtr->ValidationFuncPtr != NULL))
{
Status = (RegRecPtr->ValidationFuncPtr)(WorkingBufferPtr->BufferPtr);
if (Status > CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_VAL_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s: Validation func return code invalid (Stat=%u) for '%s'", AppName,
(unsigned int)Status, RegRecPtr->Name);
Status = -1;
}
if (Status < 0)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_VALIDATION_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId,
"%s: Validation func reports table invalid (Stat=%u) for '%s'", AppName,
(unsigned int)Status, RegRecPtr->Name);
/* Zero out the buffer to remove any bad data */
memset(WorkingBufferPtr->BufferPtr, 0, RegRecPtr->Size);
}
}
/* Perform the table update to complete the load */
if (Status < CFE_SUCCESS)
{
/* The load has had a problem, free the working buffer for another attempt */
if ((!RegRecPtr->DoubleBuffered) && (RegRecPtr->TableLoadedOnce == true))
{
/* For single buffered tables, freeing entails resetting flag */
CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].Taken = false;
}
/* For double buffered tables, freeing buffer is simple */
RegRecPtr->LoadInProgress = CFE_TBL_NO_LOAD_IN_PROGRESS;
return Status;
}
FirstTime = !RegRecPtr->TableLoadedOnce;
/* If this is not the first load, then the data must be moved from the inactive buffer */
/* to the active buffer to complete the load. First loads are done directly to the active. */
if (!FirstTime)
{
/* Force the table update */
RegRecPtr->LoadPending = true;
Status = CFE_TBL_UpdateInternal(TblHandle, RegRecPtr, AccessDescPtr);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_UPDATE_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s: Failed to update '%s' (Stat=%u)", AppName, RegRecPtr->Name,
(unsigned int)Status);
}
}
else
{
/* On initial loads, make sure registry is given file/address of data source */
strncpy(RegRecPtr->LastFileLoaded, WorkingBufferPtr->DataSource, sizeof(RegRecPtr->LastFileLoaded) - 1);
RegRecPtr->LastFileLoaded[sizeof(RegRecPtr->LastFileLoaded) - 1] = '\0';
CFE_TBL_NotifyTblUsersOfUpdate(RegRecPtr);
/* If the table is a critical table, update the appropriate CDS with the new data */
if (RegRecPtr->CriticalTable == true)
{
CFE_TBL_UpdateCriticalTblCDS(RegRecPtr);
}
Status = CFE_SUCCESS;
}
if (Status == CFE_SUCCESS)
{
/* The first time a table is loaded, the event message is DEBUG */
/* to help eliminate a flood of events during a startup */
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_SUCCESS_INF_EID,
FirstTime ? CFE_EVS_EventType_DEBUG : CFE_EVS_EventType_INFORMATION,
CFE_TBL_Global.TableTaskAppId, "Successfully loaded '%s' from '%s'", RegRecPtr->Name,
RegRecPtr->LastFileLoaded);
/* Save the index of the table for housekeeping telemetry */
CFE_TBL_Global.LastTblUpdated = AccessDescPtr->RegIndex;
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_Update
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_Update(CFE_TBL_Handle_t TblHandle)
{
int32 Status;
CFE_ES_AppId_t ThisAppId;
CFE_TBL_RegistryRec_t * RegRecPtr = NULL;
CFE_TBL_AccessDescriptor_t *AccessDescPtr = NULL;
char AppName[OS_MAX_API_NAME] = {"UNKNOWN"};
/* Verify access rights and get a valid Application ID for calling App */
Status = CFE_TBL_ValidateAccess(TblHandle, &ThisAppId);
if (Status == CFE_SUCCESS)
{
/* Get pointers to pertinent records in registry and handles */
AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
Status = CFE_TBL_UpdateInternal(TblHandle, RegRecPtr, AccessDescPtr);
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("CFE_TBL:Update-App(%lu) fail to update Tbl '%s' (Stat=0x%08X)\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), RegRecPtr->Name, (unsigned int)Status);
}
}
else
{
CFE_ES_WriteToSysLog("CFE_TBL:Update-App(%lu) does not have access to Tbl Handle=%d\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), (int)TblHandle);
}
if (Status != CFE_TBL_ERR_BAD_APP_ID)
{
/* Translate AppID of caller into App Name */
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
}
/* On Error conditions, notify ground of screw up */
if (Status < 0)
{
if (RegRecPtr != NULL)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_UPDATE_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s Failed to Update '%s', Status=0x%08X", AppName, RegRecPtr->Name,
(unsigned int)Status);
}
else
{
CFE_EVS_SendEventWithAppID(CFE_TBL_UPDATE_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s Failed to Update '?', Status=0x%08X", AppName, (unsigned int)Status);
}
}
else
{
/* If there was a warning (ie - Table is currently locked), then do not issue a message */
if (Status == CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_UPDATE_SUCCESS_INF_EID, CFE_EVS_EventType_INFORMATION,
CFE_TBL_Global.TableTaskAppId, "%s Successfully Updated '%s'", AppName,
RegRecPtr->Name);
/* Save the index of the table for housekeeping telemetry */
CFE_TBL_Global.LastTblUpdated = AccessDescPtr->RegIndex;
}
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_GetAddress
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_GetAddress(void **TblPtr, CFE_TBL_Handle_t TblHandle)
{
int32 Status;
CFE_ES_AppId_t ThisAppId;
if (TblPtr == NULL)