-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_tbl_internal.c
1419 lines (1225 loc) · 53.5 KB
/
cfe_tbl_internal.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_internal.c
**
** Purpose: cFE Table Services (TBL) utility function source file
**
** Author: D. Kobe/the Hammers Company, Inc.
**
** Notes:
**
*/
/*
** Required header files...
*/
#include "cfe_tbl_module_all.h"
#include <stdio.h>
#include <string.h>
/*----------------------------------------------------------------
*
* Function: CFE_TBL_EarlyInit
*
* Implemented per public API
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_EarlyInit(void)
{
uint16 i;
uint32 j;
int32 Status;
/* Clear task global */
memset(&CFE_TBL_Global, 0, sizeof(CFE_TBL_Global));
/* Initialize the Table Registry */
for (i = 0; i < CFE_PLATFORM_TBL_MAX_NUM_TABLES; i++)
{
CFE_TBL_InitRegistryRecord(&CFE_TBL_Global.Registry[i]);
}
/* Initialize the Table Access Descriptors nonzero values */
for (i = 0; i < CFE_PLATFORM_TBL_MAX_NUM_HANDLES; i++)
{
CFE_TBL_Global.Handles[i].AppId = CFE_TBL_NOT_OWNED;
CFE_TBL_Global.Handles[i].PrevLink = CFE_TBL_END_OF_LIST;
CFE_TBL_Global.Handles[i].NextLink = CFE_TBL_END_OF_LIST;
}
/* Initialize the Table Validation Results Records nonzero values */
for (i = 0; i < CFE_PLATFORM_TBL_MAX_NUM_VALIDATIONS; i++)
{
CFE_TBL_Global.ValidationResults[i].State = CFE_TBL_VALIDATION_FREE;
}
/* Initialize the Dump-Only Table Dump Control Blocks nonzero values */
for (i = 0; i < CFE_PLATFORM_TBL_MAX_SIMULTANEOUS_LOADS; i++)
{
CFE_TBL_Global.DumpControlBlocks[i].State = CFE_TBL_DUMP_FREE;
/* Prevent Shared Buffers from being used until successfully allocated */
CFE_TBL_Global.LoadBuffs[i].Taken = true;
}
CFE_TBL_Global.HkTlmTblRegIndex = CFE_TBL_NOT_FOUND;
CFE_TBL_Global.LastTblUpdated = CFE_TBL_NOT_FOUND;
/*
** Create table registry access mutex
*/
Status = OS_MutSemCreate(&CFE_TBL_Global.RegistryMutex, CFE_TBL_MUT_REG_NAME, CFE_TBL_MUT_REG_VALUE);
if (Status != OS_SUCCESS)
{
return Status;
} /* end if */
/*
** Create working buffer access mutex
*/
Status = OS_MutSemCreate(&CFE_TBL_Global.WorkBufMutex, CFE_TBL_MUT_WORK_NAME, CFE_TBL_MUT_WORK_VALUE);
if (Status != OS_SUCCESS)
{
return Status;
} /* end if */
/* Initialize memory partition and allocate shared table buffers. */
Status = CFE_ES_PoolCreate(&CFE_TBL_Global.Buf.PoolHdl, CFE_TBL_Global.Buf.Partition.Data,
sizeof(CFE_TBL_Global.Buf.Partition));
if (Status < 0)
{
return Status;
}
else
{
/* Initialize each of the shared load buffers */
j = 0;
do
{
/* Allocate memory for shared load buffers */
Status = CFE_ES_GetPoolBuf(&CFE_TBL_Global.LoadBuffs[j].BufferPtr, CFE_TBL_Global.Buf.PoolHdl,
CFE_PLATFORM_TBL_MAX_SNGL_TABLE_SIZE);
if (Status < CFE_PLATFORM_TBL_MAX_SNGL_TABLE_SIZE)
{
return Status;
}
else
{
/* The buffer is successfully created, so allow it to be used */
CFE_TBL_Global.LoadBuffs[j].Taken = false;
}
j++;
} while (j < CFE_PLATFORM_TBL_MAX_SIMULTANEOUS_LOADS);
}
/* Try to obtain a previous image of the Critical Table Registry from the Critical Data Store */
Status = CFE_ES_RegisterCDSEx(&CFE_TBL_Global.CritRegHandle,
(sizeof(CFE_TBL_CritRegRec_t) * CFE_PLATFORM_TBL_MAX_CRITICAL_TABLES),
"CFE_TBL.CritReg", true);
/* Assume for the moment that nothing is already in the CDS and zero out the Critical Table Registry */
for (i = 0; i < CFE_PLATFORM_TBL_MAX_CRITICAL_TABLES; i++)
{
CFE_TBL_Global.CritReg[i].CDSHandle = CFE_ES_CDS_BAD_HANDLE;
}
if (Status == CFE_ES_CDS_ALREADY_EXISTS)
{
/* Try to recover the Critical Table Registry from the CDS */
Status = CFE_ES_RestoreFromCDS(CFE_TBL_Global.CritReg, CFE_TBL_Global.CritRegHandle);
if (Status != CFE_SUCCESS)
{
/* Note if we were unable to recover error free Critical Table Registry from the CDS */
CFE_ES_WriteToSysLog("CFE_TBL:EarlyInit-Failed to recover Critical Table Registry (Err=0x%08X)\n",
(unsigned int)Status);
}
/* Whether we recovered the Critical Table Registry or not, we are successful with initialization */
Status = CFE_SUCCESS;
}
else if (Status != CFE_SUCCESS)
{
/* Not being able to support Critical Tables is not the end of the world */
/* Note the problem and move on */
CFE_ES_WriteToSysLog("CFE_TBL:EarlyInit-Failed to create Critical Table Registry (Err=0x%08X)\n",
(unsigned int)Status);
/* Failure to support critical tables is not a good enough reason to exit the cFE on start up */
Status = CFE_SUCCESS;
}
else
{
/* Save the initial version of the Critical Table Registry in the CDS */
Status = CFE_ES_CopyToCDS(CFE_TBL_Global.CritRegHandle, CFE_TBL_Global.CritReg);
if (Status != CFE_SUCCESS)
{
/* Not being able to support Critical Tables is not the end of the world */
/* Note the problem and move on */
CFE_ES_WriteToSysLog("CFE_TBL:EarlyInit-Failed to save Critical Table Registry (Err=0x%08X)\n",
(unsigned int)Status);
/* Failure to support critical tables is not a good enough reason to exit the cFE on start up */
Status = CFE_SUCCESS;
}
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_InitRegistryRecord
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TBL_InitRegistryRecord(CFE_TBL_RegistryRec_t *RegRecPtr)
{
memset(RegRecPtr, 0, sizeof(*RegRecPtr));
RegRecPtr->OwnerAppId = CFE_TBL_NOT_OWNED;
RegRecPtr->NotificationMsgId = CFE_SB_INVALID_MSG_ID;
RegRecPtr->HeadOfAccessList = CFE_TBL_END_OF_LIST;
RegRecPtr->LoadInProgress = CFE_TBL_NO_LOAD_IN_PROGRESS;
RegRecPtr->ValidateActiveIndex = CFE_TBL_NO_VALIDATION_PENDING;
RegRecPtr->ValidateInactiveIndex = CFE_TBL_NO_VALIDATION_PENDING;
RegRecPtr->CDSHandle = CFE_ES_CDS_BAD_HANDLE;
RegRecPtr->DumpControlIndex = CFE_TBL_NO_DUMP_PENDING;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_ValidateHandle
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_ValidateHandle(CFE_TBL_Handle_t TblHandle)
{
/* Is the handle out of range? */
if (TblHandle >= CFE_PLATFORM_TBL_MAX_NUM_HANDLES)
{
return CFE_TBL_ERR_INVALID_HANDLE;
}
else
{
/* Check to see if the Handle is no longer valid for this Table */
if (CFE_TBL_Global.Handles[TblHandle].UsedFlag == false)
{
return CFE_TBL_ERR_INVALID_HANDLE;
}
}
return CFE_SUCCESS;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_ValidateAccess
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_ValidateAccess(CFE_TBL_Handle_t TblHandle, CFE_ES_AppId_t *AppIdPtr)
{
int32 Status;
/* Check to make sure App ID is legit */
Status = CFE_ES_GetAppID(AppIdPtr);
if (Status != CFE_SUCCESS)
{
return Status;
}
/* Check table handle validity */
Status = CFE_TBL_ValidateHandle(TblHandle);
if (Status != CFE_SUCCESS)
{
return Status;
}
Status = CFE_TBL_CheckAccessRights(TblHandle, *AppIdPtr);
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_CheckAccessRights
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_CheckAccessRights(CFE_TBL_Handle_t TblHandle, CFE_ES_AppId_t ThisAppId)
{
int32 Status = CFE_SUCCESS;
if (!CFE_RESOURCEID_TEST_EQUAL(ThisAppId, CFE_TBL_Global.Handles[TblHandle].AppId))
{
/* The Table Service Task always has access rights so that tables */
/* can be manipulated via ground command */
if (!CFE_RESOURCEID_TEST_EQUAL(ThisAppId, CFE_TBL_Global.TableTaskAppId))
{
Status = CFE_TBL_ERR_NO_ACCESS;
}
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_RemoveAccessLink
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_RemoveAccessLink(CFE_TBL_Handle_t TblHandle)
{
int32 Status = CFE_SUCCESS;
CFE_TBL_AccessDescriptor_t *AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
CFE_TBL_RegistryRec_t * RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
/* Lock Access to the table while we modify the linked list */
CFE_TBL_LockRegistry();
/* If we are removing the head of the linked list, then point */
/* the head pointer to the link after this one */
if (AccessDescPtr->PrevLink == CFE_TBL_END_OF_LIST)
{
RegRecPtr->HeadOfAccessList = AccessDescPtr->NextLink;
/* Update the next link, if there is one, to be the new head of the list */
if (AccessDescPtr->NextLink != CFE_TBL_END_OF_LIST)
{
CFE_TBL_Global.Handles[AccessDescPtr->NextLink].PrevLink = CFE_TBL_END_OF_LIST;
}
}
else /* Access Descriptor is not the head of the list */
{
/* Set the next link on the previous link to the next link of the link being removed */
CFE_TBL_Global.Handles[AccessDescPtr->PrevLink].NextLink = AccessDescPtr->NextLink;
/* If this link is not the end of the list, then complete two way linkage */
/* by setting the next link's previous link to the previous link of the link being removed */
if (AccessDescPtr->NextLink != CFE_TBL_END_OF_LIST)
{
CFE_TBL_Global.Handles[AccessDescPtr->NextLink].PrevLink = AccessDescPtr->PrevLink;
}
}
/* Return the Access Descriptor to the pool */
AccessDescPtr->UsedFlag = false;
/* If this was the last Access Descriptor for this table, we can free the memory buffers as well */
if (RegRecPtr->HeadOfAccessList == CFE_TBL_END_OF_LIST)
{
/* Only free memory that we have allocated. If the image is User Defined, then don't bother */
if (RegRecPtr->UserDefAddr == false)
{
/* Free memory allocated to buffers */
Status = CFE_ES_PutPoolBuf(CFE_TBL_Global.Buf.PoolHdl, RegRecPtr->Buffers[0].BufferPtr);
RegRecPtr->Buffers[0].BufferPtr = NULL;
if (Status < 0)
{
CFE_ES_WriteToSysLog(
"CFE_TBL:RemoveAccessLink-PutPoolBuf[0] Fail Stat=0x%08X, Hndl=0x%08lX, Buf=0x%08lX\n",
(unsigned int)Status, CFE_RESOURCEID_TO_ULONG(CFE_TBL_Global.Buf.PoolHdl),
(unsigned long)RegRecPtr->Buffers[0].BufferPtr);
}
/* If a double buffered table, then free the second buffer as well */
if (RegRecPtr->DoubleBuffered)
{
Status = CFE_ES_PutPoolBuf(CFE_TBL_Global.Buf.PoolHdl, RegRecPtr->Buffers[1].BufferPtr);
RegRecPtr->Buffers[1].BufferPtr = NULL;
if (Status < 0)
{
CFE_ES_WriteToSysLog(
"CFE_TBL:RemoveAccessLink-PutPoolBuf[1] Fail Stat=0x%08X, Hndl=0x%08lX, Buf=0x%08lX\n",
(unsigned int)Status, CFE_RESOURCEID_TO_ULONG(CFE_TBL_Global.Buf.PoolHdl),
(unsigned long)RegRecPtr->Buffers[1].BufferPtr);
}
}
else
{
/* If a shared buffer has been allocated to the table, then release it as well */
if (RegRecPtr->LoadInProgress != CFE_TBL_NO_LOAD_IN_PROGRESS)
{
/* Free the working buffer */
CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].Taken = false;
RegRecPtr->LoadInProgress = CFE_TBL_NO_LOAD_IN_PROGRESS;
}
}
}
}
/* Unlock the registry to allow others to modify it */
CFE_TBL_UnlockRegistry();
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_GetAddressInternal
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_GetAddressInternal(void **TblPtr, CFE_TBL_Handle_t TblHandle, CFE_ES_AppId_t ThisAppId)
{
int32 Status;
CFE_TBL_AccessDescriptor_t *AccessDescPtr;
CFE_TBL_RegistryRec_t * RegRecPtr;
/* Check table handle validity */
Status = CFE_TBL_ValidateHandle(TblHandle);
if (Status == CFE_SUCCESS)
{
/* Get a pointer to the Access Descriptor */
AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
/* Verify that we are allowed access to the table */
Status = CFE_TBL_CheckAccessRights(TblHandle, ThisAppId);
if (Status == CFE_SUCCESS)
{
/* Get a pointer to the Table Registry entry */
RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
/* If table is unowned, then owner must have unregistered it when we weren't looking */
if (CFE_RESOURCEID_TEST_EQUAL(RegRecPtr->OwnerAppId, CFE_TBL_NOT_OWNED))
{
Status = CFE_TBL_ERR_UNREGISTERED;
CFE_ES_WriteToSysLog("CFE_TBL:GetAddressInternal-App(%lu) attempt to access unowned Tbl Handle=%d\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), (int)TblHandle);
}
else /* Table Registry Entry is valid */
{
/* Lock the table and return the current pointer */
AccessDescPtr->LockFlag = true;
/* Save the buffer we are using in the access descriptor */
/* This is used to ensure that if the buffer becomes inactive while */
/* we are using it, no one will modify it until we are done */
AccessDescPtr->BufferIndex = RegRecPtr->ActiveBufferIndex;
*TblPtr = RegRecPtr->Buffers[AccessDescPtr->BufferIndex].BufferPtr;
/* Return any pending warning or info status indicators */
Status = CFE_TBL_GetNextNotification(TblHandle);
/* Clear Table Updated Notify Bit so that caller only gets it once */
AccessDescPtr->Updated = false;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_TBL:GetAddressInternal-App(%lu) does not have access to Tbl Handle=%d\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), (int)TblHandle);
}
}
else
{
CFE_ES_WriteToSysLog("CFE_TBL:GetAddressInternal-App(%lu) using invalid Tbl Handle=%d\n",
CFE_RESOURCEID_TO_ULONG(ThisAppId), (int)TblHandle);
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_GetNextNotification
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_GetNextNotification(CFE_TBL_Handle_t TblHandle)
{
int32 Status = CFE_SUCCESS;
CFE_TBL_AccessDescriptor_t *AccessDescPtr = &CFE_TBL_Global.Handles[TblHandle];
CFE_TBL_RegistryRec_t * RegRecPtr = &CFE_TBL_Global.Registry[AccessDescPtr->RegIndex];
if (!RegRecPtr->TableLoadedOnce)
{
/* If the table has never been loaded, return an error code for the address */
Status = CFE_TBL_ERR_NEVER_LOADED;
}
else if (AccessDescPtr->Updated)
{
/* If the table has been updated recently, return the update status */
Status = CFE_TBL_INFO_UPDATED;
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_FindTableInRegistry
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int16 CFE_TBL_FindTableInRegistry(const char *TblName)
{
int16 RegIndx = CFE_TBL_NOT_FOUND;
int16 i = -1;
do
{
/* Point to next record in the Table Registry */
i++;
/* Check to see if the record is currently being used */
if (!CFE_RESOURCEID_TEST_EQUAL(CFE_TBL_Global.Registry[i].OwnerAppId, CFE_TBL_NOT_OWNED))
{
/* Perform a case sensitive name comparison */
if (strcmp(TblName, CFE_TBL_Global.Registry[i].Name) == 0)
{
/* If the names match, then return the index */
RegIndx = i;
}
}
} while ((RegIndx == CFE_TBL_NOT_FOUND) && (i < (CFE_PLATFORM_TBL_MAX_NUM_TABLES - 1)));
return RegIndx;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_FindFreeRegistryEntry
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int16 CFE_TBL_FindFreeRegistryEntry(void)
{
int16 RegIndx = CFE_TBL_NOT_FOUND;
int16 i = 0;
while ((RegIndx == CFE_TBL_NOT_FOUND) && (i < CFE_PLATFORM_TBL_MAX_NUM_TABLES))
{
/* A Table Registry is only "Free" when there isn't an owner AND */
/* all other applications are not sharing or locking the table */
if (CFE_RESOURCEID_TEST_EQUAL(CFE_TBL_Global.Registry[i].OwnerAppId, CFE_TBL_NOT_OWNED) &&
(CFE_TBL_Global.Registry[i].HeadOfAccessList == CFE_TBL_END_OF_LIST))
{
RegIndx = i;
}
else
{
i++;
}
}
return RegIndx;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_FindFreeHandle
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_TBL_Handle_t CFE_TBL_FindFreeHandle(void)
{
CFE_TBL_Handle_t HandleIndx = CFE_TBL_END_OF_LIST;
int16 i = 0;
while ((HandleIndx == CFE_TBL_END_OF_LIST) && (i < CFE_PLATFORM_TBL_MAX_NUM_HANDLES))
{
if (CFE_TBL_Global.Handles[i].UsedFlag == false)
{
HandleIndx = i;
}
else
{
i++;
}
}
return HandleIndx;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_FormTableName
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TBL_FormTableName(char *FullTblName, const char *TblName, CFE_ES_AppId_t ThisAppId)
{
char AppName[OS_MAX_API_NAME];
CFE_ES_GetAppName(AppName, ThisAppId, sizeof(AppName));
/* Ensure that AppName is null terminated */
AppName[OS_MAX_API_NAME - 1] = '\0';
/* Complete formation of processor specific table name */
sprintf(FullTblName, "%s.%s", AppName, TblName);
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_LockRegistry
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_LockRegistry(void)
{
int32 Status;
Status = OS_MutSemTake(CFE_TBL_Global.RegistryMutex);
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS;
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_UnlockRegistry
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_UnlockRegistry(void)
{
int32 Status;
Status = OS_MutSemGive(CFE_TBL_Global.RegistryMutex);
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS;
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_GetWorkingBuffer
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_GetWorkingBuffer(CFE_TBL_LoadBuff_t **WorkingBufferPtr, CFE_TBL_RegistryRec_t *RegRecPtr,
bool CalledByApp)
{
int32 Status = CFE_SUCCESS;
int32 i;
int32 InactiveBufferIndex;
CFE_TBL_Handle_t AccessIterator;
/* Initialize return pointer to NULL */
*WorkingBufferPtr = NULL;
/* If a load is already in progress, return the previously allocated working buffer */
if (RegRecPtr->LoadInProgress != CFE_TBL_NO_LOAD_IN_PROGRESS)
{
if (RegRecPtr->DoubleBuffered)
{
*WorkingBufferPtr = &RegRecPtr->Buffers[(1U - RegRecPtr->ActiveBufferIndex)];
}
else
{
*WorkingBufferPtr = &CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress];
}
}
else
{
/* If the table is uninitialized and the function is called by an application (rather than */
/* by the Table Services application), then use the current active buffer as the working buffer. */
/* This allows many tasks with many tables to perform the initialization without conflict */
/* over the accessibility of the shared working buffers. */
if ((RegRecPtr->TableLoadedOnce == false) && (CalledByApp == true))
{
if (RegRecPtr->DoubleBuffered)
{
*WorkingBufferPtr = &RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex];
}
else
{
*WorkingBufferPtr = &RegRecPtr->Buffers[0];
}
}
else
{
/* If the table is a double buffered table, then check to make sure the */
/* inactive buffer has been freed by any Applications that may have been using it */
if (RegRecPtr->DoubleBuffered)
{
/* Determine the index of the Inactive Buffer Pointer */
InactiveBufferIndex = 1 - RegRecPtr->ActiveBufferIndex;
/* Scan the access descriptor table to determine if anyone is still using the inactive buffer */
AccessIterator = RegRecPtr->HeadOfAccessList;
while ((AccessIterator != CFE_TBL_END_OF_LIST) && (Status == CFE_SUCCESS))
{
if ((CFE_TBL_Global.Handles[AccessIterator].BufferIndex == InactiveBufferIndex) &&
(CFE_TBL_Global.Handles[AccessIterator].LockFlag))
{
Status = CFE_TBL_ERR_NO_BUFFER_AVAIL;
CFE_ES_WriteToSysLog(
"CFE_TBL:GetWorkingBuffer-Inactive Dbl Buff Locked for '%s' by AppId=%lu\n",
RegRecPtr->Name, CFE_RESOURCEID_TO_ULONG(CFE_TBL_Global.Handles[AccessIterator].AppId));
}
/* Move to next access descriptor in linked list */
AccessIterator = CFE_TBL_Global.Handles[AccessIterator].NextLink;
}
/* If buffer is free, then return the pointer to it */
if (Status == CFE_SUCCESS)
{
*WorkingBufferPtr = &RegRecPtr->Buffers[InactiveBufferIndex];
RegRecPtr->LoadInProgress = InactiveBufferIndex;
}
}
else /* Single Buffered Table */
{
/* Take Mutex to make sure we are not trying to grab a working buffer that some */
/* other application is also trying to grab. */
Status = OS_MutSemTake(CFE_TBL_Global.WorkBufMutex);
/* Make note of any errors but continue and hope for the best */
if (Status != OS_SUCCESS)
{
CFE_ES_WriteToSysLog("CFE_TBL:GetWorkBuf-Internal error taking WorkBuf Mutex (Status=0x%08X)\n",
(unsigned int)Status);
}
/* Determine if there are any common buffers available */
i = 0;
while ((i < CFE_PLATFORM_TBL_MAX_SIMULTANEOUS_LOADS) && (CFE_TBL_Global.LoadBuffs[i].Taken == true))
{
i++;
}
/* If a free buffer was found, then return the address to the associated shared buffer */
if (i < CFE_PLATFORM_TBL_MAX_SIMULTANEOUS_LOADS)
{
CFE_TBL_Global.LoadBuffs[i].Taken = true;
*WorkingBufferPtr = &CFE_TBL_Global.LoadBuffs[i];
RegRecPtr->LoadInProgress = i;
/* Translate OS_SUCCESS into CFE_SUCCESS */
Status = CFE_SUCCESS;
}
else
{
Status = CFE_TBL_ERR_NO_BUFFER_AVAIL;
CFE_ES_WriteToSysLog("CFE_TBL:GetWorkingBuffer-All shared buffers are locked\n");
}
/* Allow others to obtain a shared working buffer */
OS_MutSemGive(CFE_TBL_Global.WorkBufMutex);
}
if ((*WorkingBufferPtr) != NULL &&
(*WorkingBufferPtr)->BufferPtr != RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].BufferPtr)
{
/* In case the file contains a partial table load, get the active buffer contents first */
memcpy((*WorkingBufferPtr)->BufferPtr, RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].BufferPtr,
RegRecPtr->Size);
}
}
}
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_LoadFromFile
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_LoadFromFile(const char *AppName, CFE_TBL_LoadBuff_t *WorkingBufferPtr, CFE_TBL_RegistryRec_t *RegRecPtr,
const char *Filename)
{
int32 Status = CFE_SUCCESS;
CFE_FS_Header_t StdFileHeader;
CFE_TBL_File_Hdr_t TblFileHeader;
osal_id_t FileDescriptor;
size_t FilenameLen = strlen(Filename);
uint32 NumBytes;
uint8 ExtraByte;
if (FilenameLen > (OS_MAX_PATH_LEN - 1))
{
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_FILENAME_LONG_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: Filename is too long ('%s' (%lu) > %lu)",
AppName, Filename, (long unsigned int)FilenameLen,
(long unsigned int)OS_MAX_PATH_LEN - 1);
return CFE_TBL_ERR_FILENAME_TOO_LONG;
}
/* Try to open the specified table file */
Status = OS_OpenCreate(&FileDescriptor, Filename, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if (Status < 0)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_FILE_ACCESS_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s: Unable to open file (FileDescriptor=%d)", AppName, (int)Status);
return CFE_TBL_ERR_ACCESS;
}
Status = CFE_TBL_ReadHeaders(FileDescriptor, &StdFileHeader, &TblFileHeader, Filename);
if (Status != CFE_SUCCESS)
{
/* CFE_TBL_ReadHeaders() generates its own events */
OS_close(FileDescriptor);
return Status;
}
/* Verify that the specified file has compatible data for specified table */
if (strcmp(RegRecPtr->Name, TblFileHeader.TableName) != 0)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_LOAD_TBLNAME_MISMATCH_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: Table name mismatch (exp=%s, tblfilhdr=%s)",
AppName, RegRecPtr->Name, TblFileHeader.TableName);
OS_close(FileDescriptor);
return CFE_TBL_ERR_FILE_FOR_WRONG_TABLE;
}
if ((TblFileHeader.Offset + TblFileHeader.NumBytes) > RegRecPtr->Size)
{
CFE_EVS_SendEventWithAppID(
CFE_TBL_LOAD_EXCEEDS_SIZE_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s: File reports size larger than expected (file=%lu, exp=%lu)", AppName,
(long unsigned int)(TblFileHeader.Offset + TblFileHeader.NumBytes), (long unsigned int)RegRecPtr->Size);
OS_close(FileDescriptor);
return CFE_TBL_ERR_FILE_TOO_LARGE;
}
/* Any Table load that starts beyond the first byte is a "partial load" */
/* But a file that starts with the first byte and ends before filling */
/* the whole table is just considered "short". */
if (TblFileHeader.Offset > 0)
{
Status = CFE_TBL_WARN_PARTIAL_LOAD;
}
else if (TblFileHeader.NumBytes < RegRecPtr->Size)
{
Status = CFE_TBL_WARN_SHORT_FILE;
}
NumBytes =
OS_read(FileDescriptor, ((uint8 *)WorkingBufferPtr->BufferPtr) + TblFileHeader.Offset, TblFileHeader.NumBytes);
if (NumBytes != TblFileHeader.NumBytes)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_FILE_INCOMPLETE_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_TBL_Global.TableTaskAppId, "%s: File load incomplete (exp=%lu, read=%lu)",
AppName, (long unsigned int)TblFileHeader.NumBytes, (long unsigned int)NumBytes);
OS_close(FileDescriptor);
return CFE_TBL_ERR_LOAD_INCOMPLETE;
}
/* Check to see if the file is too large (ie - more data than header claims) */
NumBytes = OS_read(FileDescriptor, &ExtraByte, 1);
/* If successfully read another byte, then file must have too much data */
if (NumBytes == 1)
{
CFE_EVS_SendEventWithAppID(CFE_TBL_FILE_TOO_BIG_ERR_EID, CFE_EVS_EventType_ERROR, CFE_TBL_Global.TableTaskAppId,
"%s: File load too long (file length > %lu)", AppName,
(long unsigned int)TblFileHeader.NumBytes);
OS_close(FileDescriptor);
return CFE_TBL_ERR_FILE_TOO_LARGE;
}
memset(WorkingBufferPtr->DataSource, 0, sizeof(WorkingBufferPtr->DataSource));
strncpy(WorkingBufferPtr->DataSource, Filename, sizeof(WorkingBufferPtr->DataSource) - 1);
/* Save file creation time for later storage into Registry */
WorkingBufferPtr->FileCreateTimeSecs = StdFileHeader.TimeSeconds;
WorkingBufferPtr->FileCreateTimeSubSecs = StdFileHeader.TimeSubSeconds;
/* Compute the CRC on the specified table buffer */
WorkingBufferPtr->Crc =
CFE_ES_CalculateCRC(WorkingBufferPtr->BufferPtr, RegRecPtr->Size, 0, CFE_MISSION_ES_DEFAULT_CRC);
OS_close(FileDescriptor);
return Status;
}
/*----------------------------------------------------------------
*
* Function: CFE_TBL_UpdateInternal
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32 CFE_TBL_UpdateInternal(CFE_TBL_Handle_t TblHandle, CFE_TBL_RegistryRec_t *RegRecPtr,
CFE_TBL_AccessDescriptor_t *AccessDescPtr)
{
int32 Status = CFE_SUCCESS;
CFE_TBL_Handle_t AccessIterator;
bool LockStatus = false;
if ((!RegRecPtr->LoadPending) || (RegRecPtr->LoadInProgress == CFE_TBL_NO_LOAD_IN_PROGRESS))
{
/* Question: Should calling CFE_TBL_Update on a table with no load pending */
/* be considered an error? Currently assuming it is not an error. */
Status = CFE_TBL_INFO_NO_UPDATE_PENDING;
}
else
{
if (RegRecPtr->DoubleBuffered)
{
/* To update a double buffered table only requires a pointer swap */
RegRecPtr->ActiveBufferIndex = (uint8)RegRecPtr->LoadInProgress;
/* Source description in buffer should already have been updated by either */
/* the LoadFromFile function or the Load function (when a memory load). */
/* However, we need to copy it into active registry area */
strncpy(RegRecPtr->LastFileLoaded, RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].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);
}
}
else
{
/* Check to see if the Table is locked by anyone */
AccessIterator = RegRecPtr->HeadOfAccessList;
while (AccessIterator != CFE_TBL_END_OF_LIST)
{
LockStatus = (LockStatus || CFE_TBL_Global.Handles[AccessIterator].LockFlag);
AccessIterator = CFE_TBL_Global.Handles[AccessIterator].NextLink;
}
if (LockStatus)
{
Status = CFE_TBL_INFO_TABLE_LOCKED;
CFE_ES_WriteToSysLog("CFE_TBL:UpdateInternal-Unable to update locked table Handle=%d\n", TblHandle);
}
else
{
/* To update a single buffered table requires a memcpy from working buffer */
if (RegRecPtr->Buffers[0].BufferPtr != CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].BufferPtr)
{
memcpy(RegRecPtr->Buffers[0].BufferPtr,
CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].BufferPtr, RegRecPtr->Size);
}
/* Save source description with active buffer */
strncpy(RegRecPtr->Buffers[0].DataSource,
CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].DataSource,
sizeof(RegRecPtr->Buffers[0].DataSource) - 1);
RegRecPtr->Buffers[0].DataSource[sizeof(RegRecPtr->Buffers[0].DataSource) - 1] = 0;
strncpy(RegRecPtr->LastFileLoaded, CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].DataSource,
sizeof(RegRecPtr->LastFileLoaded) - 1);
RegRecPtr->LastFileLoaded[sizeof(RegRecPtr->LastFileLoaded) - 1] = 0;
/* Save the file creation time from the loaded file into the Table Registry */
RegRecPtr->Buffers[0].FileCreateTimeSecs =
CFE_TBL_Global.LoadBuffs[RegRecPtr->LoadInProgress].FileCreateTimeSecs;
RegRecPtr->Buffers[0].FileCreateTimeSubSecs =