F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
FileCommon.cpp
Go to the documentation of this file.
1#include <FpConfig.hpp>
2#include <Os/File.hpp>
3#include <Fw/Types/Assert.hpp>
4
5#ifdef __cplusplus
6extern "C" {
7#endif // __cplusplus
8
9#include <Utils/Hash/libcrc/lib_crc.h> // borrow CRC
10
11#ifdef __cplusplus
12}
13#endif // __cplusplus
14
15namespace Os {
16 File::Status File::niceCRC32(U32 &crc, const char* fileName)
17 {
18 //Constants used in this function
19 const U32 CHUNK_SIZE = 4096;
20 const U32 INITIAL_SEED = 0xFFFFFFFF;
21 const U32 MAX_IT = 0xFFFFFFFF; //Max int for U32
22 //Loop variables for calculating CRC
23 NATIVE_INT_TYPE offset = 0;
24 U32 seed = INITIAL_SEED;
25 Status status;
26 File file;
27 U8 file_buffer[CHUNK_SIZE];
28 bool eof = false;
29 //Loop across the whole file
30 for (U32 i = 0; !eof && i < MAX_IT; i++) {
31 //Open and check status
32 status = file.open(fileName, OPEN_READ);
33 if (status != OP_OK) {
34 crc = 0;
35 return status;
36 }
37 //Find our place
38 status = file.seek(offset, true);
39 if (status != OP_OK) {
40 crc = 0;
41 return status;
42 }
43 NATIVE_INT_TYPE chunk = CHUNK_SIZE;
44 //Read data and check status
45 status = file.read(file_buffer, chunk, false);
46 offset += chunk;
47 //Close file, then update CRC. This reduces time file is required open
48 file.close();
49 if (chunk != 0 && status == OP_OK) {
50 for (U32 ci = 0; static_cast<NATIVE_INT_TYPE>(ci) < chunk; ci++) {
51 seed = update_crc_32(seed, file_buffer[ci]);
52 }
53 } else if (chunk == 0 && status == OP_OK) {
54 eof = true;
55 break;
56 } else {
57 crc = 0;
58 return status;
59 }
60 }
61 //Reach max-loop
62 if (!eof) {
63 crc = 0;
64 return OTHER_ERROR;
65 }
66 //Good CRC
67 crc = seed;
68 return OP_OK;
69 }
70}
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
C++-compatible configuration header for fprime configuration.
static Status niceCRC32(U32 &crc, const char *fileName)
Calculates CRC32 of file, not burdening FS.
void close()
close file
Definition File.cpp:36
@ OTHER_ERROR
A catch-all for other errors. Have to look in implementation-specific code.
Definition File.hpp:31
@ OP_OK
Operation was successful.
Definition File.hpp:24
Status seek(NATIVE_INT_TYPE offset, bool absolute=true)
seek to location. If absolute = true, absolute from beginning of file
Definition File.cpp:24
@ OPEN_READ
Open file for reading.
Definition File.hpp:15
Status open(const char *fileName, Mode mode)
open file. Writing creates file if it doesn't exist
Definition File.cpp:12
Status read(void *buffer, NATIVE_INT_TYPE &size, bool waitForFull=true)
waitForFull = true to wait for all bytes to be read
Definition File.cpp:28
unsigned long update_crc_32(unsigned long crc, char c)
Definition lib_crc.c:269
Definition File.cpp:6