Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / storage / rlefile.h
blob803b3ad0011b9c172573bd3a26eead52c5d94498
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 // TODO should be rle
23 #ifndef _RLEFILE_H_
24 #define _RLEFILE_H_
26 #include "eeprominterface.h"
28 #define ERR_NONE 0
29 #define ERR_FULL 1
30 #define ERR_TMO 2
32 PACK(struct DirEnt {
33 uint8_t startBlk;
34 uint16_t size:12;
35 uint16_t typ:4;
36 });
38 PACK(struct DirEntArm {
39 uint16_t startBlk;
40 uint16_t size:12;
41 uint16_t typ:4;
42 });
44 PACK(struct EeFs{
45 uint8_t version;
46 uint8_t mySize;
47 uint8_t freeList;
48 uint8_t bs;
49 DirEnt files[36];
50 });
52 PACK(struct EeFsArm {
53 uint8_t version;
54 uint16_t mySize;
55 uint16_t freeList;
56 uint8_t bs;
57 uint8_t spare[2];
58 DirEntArm files[62];
59 });
61 struct t_eeprom_header
63 uint32_t sequence_no ; // sequence # to decide which block is most recent
64 uint16_t data_size ; // # bytes in data area
65 uint8_t flags ;
66 uint8_t hcsum ;
69 PACK(struct EepromHeaderFile
71 uint8_t zoneIndex:7;
72 uint8_t exists:1;
73 });
75 PACK(struct EepromHeader
77 uint32_t mark;
78 uint32_t index;
79 EepromHeaderFile files[63];
80 });
82 class RleFile
84 uint8_t m_fileId; //index of file in directory = filename
85 unsigned int m_pos; //over all filepos
86 unsigned int m_currBlk; //current block.id
87 unsigned int m_ofs; //offset inside of the current block
88 uint8_t m_zeroes; //control byte for run length decoder
89 uint8_t m_bRlc; //control byte for run length decoder
90 unsigned int m_err; //error reasons
91 uint16_t m_size;
93 Board::Type board;
94 unsigned int version;
95 uint8_t *eeprom;
96 unsigned int eeprom_size;
97 EeFs *eeFs;
98 EeFsArm *eeFsArm;
99 unsigned int eeFsVersion;
100 unsigned int eeFsBlockSize;
101 unsigned int eeFsSize;
102 unsigned int eeFsFirstBlock;
103 unsigned int eeFsBlocksOffset;
104 unsigned int eeFsBlocksMax;
105 unsigned int eeFsLinkSize;
107 EepromHeader * eepromFatHeader;
109 void eeprom_read_block (void *pointer_ram, unsigned int pointer_eeprom, size_t size);
110 void eeprom_write_block(const void *pointer_ram, unsigned int pointer_eeprom, size_t size);
112 uint8_t EeFsRead(unsigned int blk, unsigned int ofs);
113 void EeFsWrite(unsigned int blk, unsigned int ofs, uint8_t val);
115 unsigned int EeFsGetLink(unsigned int blk);
116 void EeFsSetLink(unsigned int blk, unsigned int val);
117 uint8_t EeFsGetDat(unsigned int blk, unsigned int ofs);
118 void EeFsSetDat(unsigned int blk, unsigned int ofs, const uint8_t *buf, unsigned int len);
119 unsigned int EeFsGetFree();
120 void EeFsFree(unsigned int blk); // free one or more blocks
121 unsigned int EeFsAlloc(); // alloc one block from freelist
122 bool searchFat();
124 public:
126 RleFile();
128 void EeFsCreate(uint8_t *eeprom, int size, Board::Type board, unsigned int version);
130 bool EeFsOpen(uint8_t *eeprom, int size, Board::Type board);
132 ///open file for reading, no close necessary
133 ///for writing use writeRlc() or create()
134 unsigned int openRd(unsigned int i_fileId);
135 /// create a new file with given fileId,
136 /// !!! if this file already exists, then all blocks are reused
137 /// and all contents will be overwritten.
138 /// after writing closeTrunc has to be called
139 void create(unsigned int i_fileId, unsigned int typ);
140 /// close file and truncate the blockchain if to long.
141 void closeTrunc();
143 ///open file, write to file and close it.
144 ///If file existed before, then contents is overwritten.
145 ///If file was larger before, then unused blocks are freed
146 unsigned int writeRlc1(unsigned int i_fileId, unsigned int typ, const uint8_t *buf, unsigned int i_len);
147 unsigned int writeRlc2(unsigned int i_fileId, unsigned int typ, const uint8_t *buf, unsigned int i_len);
149 unsigned int read(uint8_t *buf, unsigned int i_len);
150 unsigned int write1(uint8_t b);
151 unsigned int write(const uint8_t *buf, unsigned int i_len);
153 ///return size of compressed file without block overhead
154 unsigned int size(unsigned int id);
155 ///read from opened file and decode rlc-coded data
156 unsigned int readRlc12(uint8_t *buf, unsigned int i_len, bool rlc2);
157 inline unsigned int readRlc1(uint8_t *buf, unsigned int i_len)
159 return readRlc12(buf, i_len, false);
161 inline unsigned int readRlc2(uint8_t *buf, unsigned int i_len)
163 return readRlc12(buf, i_len, true);
166 uint8_t byte_checksum(uint8_t *p, unsigned int size);
167 unsigned int ee32_check_header(struct t_eeprom_header *hptr);
168 unsigned int get_current_block_number(unsigned int block_no, uint16_t *p_size);
171 unsigned int importRlc(QByteArray & dst, QByteArray & src, unsigned int rlcVersion=2);
173 #endif // _RLEFILE_H_