-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_es_cds_mempool.c
371 lines (324 loc) · 12.6 KB
/
cfe_es_cds_mempool.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
/*
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
** File:
** cfe_es_cds_mempool.c
**
** Purpose:
** Set of services for management of the CDS discrete sized memory pools.
**
** References:
** Flight Software Branch C Coding Standard Version 1.0a
** cFE Flight Software Application Developers Guide
**
*/
/*
** Includes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfe_es_module_all.h"
/*****************************************************************************/
/*
** Type Definitions
*/
/*****************************************************************************/
/*
** File Global Data
*/
const size_t CFE_ES_CDSMemPoolDefSize[CFE_ES_CDS_NUM_BLOCK_SIZES] = {
CFE_PLATFORM_ES_CDS_MAX_BLOCK_SIZE, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_16, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_15,
CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_14, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_13, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_12,
CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_11, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_10, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_09,
CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_08, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_07, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_06,
CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_05, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_04, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_03,
CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_02, CFE_PLATFORM_ES_CDS_MEM_BLOCK_SIZE_01};
/*****************************************************************************/
/*
** Functions
*/
/*
** CFE_ES_CDS_PoolRetrieve will obtain a block descriptor from CDS storage.
**
** This is a bridge between the generic pool implementation and the CDS cache.
*/
int32 CFE_ES_CDS_PoolRetrieve(CFE_ES_GenPoolRecord_t *GenPoolRecPtr, size_t Offset, CFE_ES_GenPoolBD_t **BdPtr)
{
CFE_ES_CDS_Instance_t *CDS = (CFE_ES_CDS_Instance_t *)GenPoolRecPtr;
*BdPtr = &CDS->Cache.Data.Desc;
return CFE_ES_CDS_CacheFetch(&CDS->Cache, Offset, sizeof(CFE_ES_GenPoolBD_t));
}
/*
** CFE_ES_CDS_PoolCommit will write a block descriptor to CDS storage.
**
** This is a bridge between the generic pool implementation and the CDS cache.
*/
int32 CFE_ES_CDS_PoolCommit(CFE_ES_GenPoolRecord_t *GenPoolRecPtr, size_t Offset, const CFE_ES_GenPoolBD_t *BdPtr)
{
CFE_ES_CDS_Instance_t *CDS = (CFE_ES_CDS_Instance_t *)GenPoolRecPtr;
CFE_ES_CDS_CachePreload(&CDS->Cache, BdPtr, Offset, sizeof(CFE_ES_GenPoolBD_t));
return CFE_ES_CDS_CacheFlush(&CDS->Cache);
}
/*
** CFE_ES_CreateCDSPool will initialize a pre-allocated memory pool.
**
** NOTE:
** This function is only ever called during "Early Init" phase,
** where it is not possible to have contention writing into the syslog.
** Therefore the use of CFE_ES_SysLogWrite_Unsync() is acceptable
*/
int32 CFE_ES_CreateCDSPool(size_t CDSPoolSize, size_t StartOffset)
{
CFE_ES_CDS_Instance_t *CDS = &CFE_ES_Global.CDSVars;
int32 Status;
size_t SizeCheck;
size_t ActualSize;
SizeCheck = CFE_ES_GenPoolCalcMinSize(CFE_ES_CDS_NUM_BLOCK_SIZES, CFE_ES_CDSMemPoolDefSize, 1);
ActualSize = CDSPoolSize;
if (ActualSize < SizeCheck)
{
/* Must be able make Pool verification, block descriptor and at least one of the smallest blocks */
CFE_ES_SysLogWrite_Unsync("CFE_ES:CreateCDSPool-Pool size(%lu) too small for one CDS Block, need >=%lu\n",
(unsigned long)ActualSize, (unsigned long)SizeCheck);
return CFE_ES_CDS_INVALID_SIZE;
}
Status = CFE_ES_GenPoolInitialize(&CDS->Pool, StartOffset, /* starting offset */
ActualSize, /* total size */
4, /* alignment */
CFE_ES_CDS_NUM_BLOCK_SIZES, CFE_ES_CDSMemPoolDefSize, CFE_ES_CDS_PoolRetrieve,
CFE_ES_CDS_PoolCommit);
return Status;
}
/*
** Function:
** CFE_ES_RebuildCDSPool
**
** Purpose:
**
** NOTE:
** This function is only ever called during "Early Init" phase,
** where it is not possible to have contention writing into the syslog.
** Therefore the use of CFE_ES_SysLogWrite_Unsync() is acceptable
*/
int32 CFE_ES_RebuildCDSPool(size_t CDSPoolSize, size_t StartOffset)
{
CFE_ES_CDS_Instance_t *CDS = &CFE_ES_Global.CDSVars;
int32 Status;
/*
* Start by creating the pool in a clean state, as it would be in a non-rebuild.
*/
Status = CFE_ES_CreateCDSPool(CDSPoolSize, StartOffset);
if (Status != CFE_SUCCESS)
{
return Status;
}
/* Now walk through the CDS memory and attempt to recover existing CDS blocks */
Status = CFE_ES_GenPoolRebuild(&CDS->Pool);
if (Status != CFE_SUCCESS)
{
CFE_ES_SysLogWrite_Unsync("CFE_ES:RebuildCDS-Err rebuilding CDS (Stat=0x%08x)\n", (unsigned int)Status);
Status = CFE_ES_CDS_ACCESS_ERROR;
}
return Status;
}
/*
** Function:
** CFE_ES_CDSBlockWrite
**
** Purpose:
**
*/
int32 CFE_ES_CDSBlockWrite(CFE_ES_CDSHandle_t Handle, const void *DataToWrite)
{
CFE_ES_CDS_Instance_t *CDS = &CFE_ES_Global.CDSVars;
char LogMessage[CFE_ES_MAX_SYSLOG_MSG_SIZE];
int32 Status;
size_t BlockSize;
size_t UserDataSize;
size_t UserDataOffset;
CFE_ES_CDS_RegRec_t * CDSRegRecPtr;
/* Ensure the the log message is an empty string in case it is never written to */
LogMessage[0] = 0;
CDSRegRecPtr = CFE_ES_LocateCDSBlockRecordByID(Handle);
/*
* A CDS block ID must be accessed by only one thread at a time.
* Checking the validity of the block requires access to the registry.
*/
CFE_ES_LockCDS();
if (CFE_ES_CDSBlockRecordIsMatch(CDSRegRecPtr, Handle))
{
/*
* Getting the buffer size via this function retrieves it from the
* internal descriptor, and validates the descriptor as part of the operation.
* This should always agree with the size in the registry for this block.
*/
Status = CFE_ES_GenPoolGetBlockSize(&CDS->Pool, &BlockSize, CDSRegRecPtr->BlockOffset);
if (Status != CFE_SUCCESS)
{
CFE_ES_SysLog_snprintf(LogMessage, sizeof(LogMessage),
"CFE_ES:CDSBlkWrite-Invalid Handle or Block Descriptor.\n");
}
else if (BlockSize <= sizeof(CFE_ES_CDS_BlockHeader_t) || BlockSize != CDSRegRecPtr->BlockSize)
{
CFE_ES_SysLog_snprintf(LogMessage, sizeof(LogMessage),
"CFE_ES:CDSBlkWrite-Block size %lu invalid, expected %lu\n",
(unsigned long)BlockSize, (unsigned long)CDSRegRecPtr->BlockSize);
Status = CFE_ES_CDS_INVALID_SIZE;
}
else
{
UserDataSize = CDSRegRecPtr->BlockSize;
UserDataSize -= sizeof(CFE_ES_CDS_BlockHeader_t);
UserDataOffset = CDSRegRecPtr->BlockOffset;
UserDataOffset += sizeof(CFE_ES_CDS_BlockHeader_t);
CDS->Cache.Data.BlockHeader.Crc =
CFE_ES_CalculateCRC(DataToWrite, UserDataSize, 0, CFE_MISSION_ES_DEFAULT_CRC);
CDS->Cache.Offset = CDSRegRecPtr->BlockOffset;
CDS->Cache.Size = sizeof(CFE_ES_CDS_BlockHeader_t);
/* Write the new block descriptor for the data coming from the Application */
Status = CFE_ES_CDS_CacheFlush(&CDS->Cache);
if (Status != CFE_SUCCESS)
{
CFE_ES_SysLog_snprintf(
LogMessage, sizeof(LogMessage),
"CFE_ES:CDSBlkWrite-Err writing header data to CDS (Stat=0x%08x) @Offset=0x%08lx\n",
(unsigned int)CDS->Cache.AccessStatus, (unsigned long)CDSRegRecPtr->BlockOffset);
}
else
{
Status = CFE_PSP_WriteToCDS(DataToWrite, UserDataOffset, UserDataSize);
if (Status != CFE_PSP_SUCCESS)
{
CFE_ES_SysLog_snprintf(
LogMessage, sizeof(LogMessage),
"CFE_ES:CDSBlkWrite-Err writing user data to CDS (Stat=0x%08x) @Offset=0x%08lx\n",
(unsigned int)Status, (unsigned long)UserDataOffset);
}
}
}
}
else
{
Status = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
CFE_ES_UnlockCDS();
/* Do the actual syslog if something went wrong */
if (LogMessage[0] != 0)
{
CFE_ES_SYSLOG_APPEND(LogMessage);
}
return Status;
}
/*
** Function:
** CFE_ES_CDSBlockRead
**
** Purpose:
**
*/
int32 CFE_ES_CDSBlockRead(void *DataRead, CFE_ES_CDSHandle_t Handle)
{
CFE_ES_CDS_Instance_t *CDS = &CFE_ES_Global.CDSVars;
char LogMessage[CFE_ES_MAX_SYSLOG_MSG_SIZE];
int32 Status;
uint32 CrcOfCDSData;
size_t BlockSize;
size_t UserDataSize;
size_t UserDataOffset;
CFE_ES_CDS_RegRec_t * CDSRegRecPtr;
/* Validate the handle before doing anything */
LogMessage[0] = 0;
CDSRegRecPtr = CFE_ES_LocateCDSBlockRecordByID(Handle);
/*
* A CDS block ID must be accessed by only one thread at a time.
* Checking the validity of the block requires access to the registry.
*/
CFE_ES_LockCDS();
if (CFE_ES_CDSBlockRecordIsMatch(CDSRegRecPtr, Handle))
{
/*
* Getting the buffer size via this function retrieves it from the
* internal descriptor, and validates the descriptor as part of the operation.
* This should always agree with the size in the registry for this block.
*/
Status = CFE_ES_GenPoolGetBlockSize(&CDS->Pool, &BlockSize, CDSRegRecPtr->BlockOffset);
if (Status == CFE_SUCCESS)
{
if (BlockSize <= sizeof(CFE_ES_CDS_BlockHeader_t) || BlockSize != CDSRegRecPtr->BlockSize)
{
Status = CFE_ES_CDS_INVALID_SIZE;
}
else
{
UserDataSize = CDSRegRecPtr->BlockSize;
UserDataSize -= sizeof(CFE_ES_CDS_BlockHeader_t);
UserDataOffset = CDSRegRecPtr->BlockOffset;
UserDataOffset += sizeof(CFE_ES_CDS_BlockHeader_t);
/* Read the header */
Status =
CFE_ES_CDS_CacheFetch(&CDS->Cache, CDSRegRecPtr->BlockOffset, sizeof(CFE_ES_CDS_BlockHeader_t));
if (Status == CFE_SUCCESS)
{
/* Read the data block */
Status = CFE_PSP_ReadFromCDS(DataRead, UserDataOffset, UserDataSize);
if (Status == CFE_PSP_SUCCESS)
{
/* Compute the CRC for the data read from the CDS and determine if the data is still valid */
CrcOfCDSData = CFE_ES_CalculateCRC(DataRead, UserDataSize, 0, CFE_MISSION_ES_DEFAULT_CRC);
/* If the CRCs do not match, report an error */
if (CrcOfCDSData != CDS->Cache.Data.BlockHeader.Crc)
{
Status = CFE_ES_CDS_BLOCK_CRC_ERR;
}
else
{
Status = CFE_SUCCESS;
}
}
}
}
}
}
else
{
Status = CFE_ES_ERR_RESOURCEID_NOT_VALID;
}
CFE_ES_UnlockCDS();
/* Do the actual syslog if something went wrong */
if (LogMessage[0] != 0)
{
CFE_ES_SYSLOG_APPEND(LogMessage);
}
return Status;
}
/*
** Function:
** CFE_ES_CDSReqdMinSize
**
** Purpose:
**
*/
size_t CFE_ES_CDSReqdMinSize(uint32 MaxNumBlocksToSupport)
{
size_t ReqSize;
ReqSize = CFE_ES_GenPoolCalcMinSize(CFE_ES_CDS_NUM_BLOCK_SIZES, CFE_ES_CDSMemPoolDefSize, MaxNumBlocksToSupport);
return ReqSize;
}