libdrmconf 0.14.1
A library to program DMR radios.
Loading...
Searching...
No Matches
dfufile.hh
1#ifndef DFUFILE_HH
2#define DFUFILE_HH
3
4#include <QFile>
5#include <QVector>
6#include <QByteArray>
7#include <QString>
8#include <QTextStream>
9
10#include "addressmap.hh"
11#include "errorstack.hh"
12
13class CRC32;
14
72class DFUFile: public QObject
73{
74 Q_OBJECT
75
76public:
78 class Element {
79 public:
81 Element();
83 Element(uint32_t addr, uint32_t size);
85 Element(const Element &other);
87 Element &operator= (const Element &other);
88
90 uint32_t address() const;
92 void setAddress(uint32_t addr);
94 uint32_t size() const;
96 uint32_t memSize() const;
98 bool isAligned(unsigned blocksize) const;
100 const QByteArray &data() const;
102 QByteArray &data();
103
105 bool read(QFile &file, CRC32 &crc, QString &errorMessage);
107 bool write(QFile &file, CRC32 &crc, QString &errorMessage) const;
108
110 void dump(QTextStream &stream) const;
111
112 protected:
114 uint32_t _address;
116 QByteArray _data;
117 };
118
120 class Image
121 {
122 public:
124 typedef QVector<Element>::iterator iterator;
125
126 public:
129 Image();
131 Image(const QString &name, uint8_t altSettings=0);
133 Image(const Image &other);
135 virtual ~Image();
137 Image &operator=(const Image &other);
138
140 uint8_t alternateSettings() const;
142 void setAlternateSettings(uint8_t s);
143
145 bool isNamed() const;
147 const QString &name() const;
149 void setName(const QString &name);
151 uint32_t size() const;
153 uint32_t memSize() const;
155 int numElements() const;
157 const Element &element(int i) const;
159 Element &element(int i);
162 void addElement(uint32_t addr, uint32_t size, int index=-1);
164 void addElement(const Element &element);
166 void remElement(int i);
168 bool isAligned(unsigned blocksize) const;
169
171 iterator begin();
173 iterator end();
174
176 bool read(QFile &file, CRC32 &crc, QString &errorMessage);
178 bool write(QFile &file, CRC32 &crc, QString &errorMessage) const;
179
181 void dump(QTextStream &stream) const;
182
184 virtual bool isAllocated(uint32_t offset) const;
185
187 virtual unsigned char *data(uint32_t offset);
189 virtual const unsigned char *data(uint32_t offset) const;
190
192 void sort();
193
194 protected:
198 QString _name;
200 QVector<Element> _elements;
203 };
204
205public:
207 DFUFile(QObject *parent=nullptr);
208
210 uint32_t size() const;
212 uint32_t memSize() const;
213
215 int numImages() const;
217 const Image &image(int i) const;
219 Image &image(int i);
221 void addImage(const QString &name, uint8_t altSettings=1);
223 void addImage(const Image &img);
225 void remImage(int i);
226
228 bool isAligned(unsigned blocksize) const;
229
232 bool read(const QString &filename, const ErrorStack &err=ErrorStack());
235 bool read(QFile &file, const ErrorStack &err=ErrorStack());
236
239 bool write(const QString &filename, const ErrorStack &err=ErrorStack());
242 bool write(QFile &file, const ErrorStack &err=ErrorStack());
243
245 void dump(QTextStream &stream) const;
246
248 virtual bool isAllocated(uint32_t offset, uint32_t img=0) const;
249
251 virtual unsigned char *data(uint32_t offset, uint32_t img=0);
253 virtual const unsigned char *data(uint32_t offset, uint32_t img=0) const;
254
255protected:
257 QVector<Image> _images;
258};
259
260#endif // DFUFILE_HH
This class represents a memory map.
Definition addressmap.hh:14
Implements the CRC32 checksum as used in DFU files.
Definition crc32.hh:10
Represents a single element within a Image.
Definition dfufile.hh:78
uint32_t memSize() const
Returns the memory size of the element.
Definition dfufile.cc:315
void setAddress(uint32_t addr)
Sets the address of the element.
Definition dfufile.cc:325
bool read(QFile &file, CRC32 &crc, QString &errorMessage)
Reads an element from the given file and updates the CRC.
Definition dfufile.cc:345
const QByteArray & data() const
Returns a reference to the data.
Definition dfufile.cc:335
QByteArray _data
The data of the element.
Definition dfufile.hh:116
bool write(QFile &file, CRC32 &crc, QString &errorMessage) const
Writes an element to the given file and updates the CRC.
Definition dfufile.cc:373
uint32_t size() const
Returns the size of the element (including headers).
Definition dfufile.cc:310
void dump(QTextStream &stream) const
Dumps a textual representation of the element.
Definition dfufile.cc:398
Element & operator=(const Element &other)
Copying assignment.
Definition dfufile.cc:303
uint32_t _address
The address of the element.
Definition dfufile.hh:114
bool isAligned(unsigned blocksize) const
Checks if the element address and size is aligned with the given block size.
Definition dfufile.cc:330
uint32_t address() const
Returns the address of the element.
Definition dfufile.cc:320
Element()
Empty constructor.
Definition dfufile.cc:284
Represents a single image within a DFUFile.
Definition dfufile.hh:121
virtual unsigned char * data(uint32_t offset)
Returns a pointer to the encoded raw data at the specified offset.
Definition dfufile.cc:675
virtual bool isAllocated(uint32_t offset) const
Returns true if the specified address is allocated.
Definition dfufile.cc:668
Image()
Default constructor.
Definition dfufile.cc:442
void setAlternateSettings(uint8_t s)
Sets the alternate settings byte.
Definition dfufile.cc:496
int numElements() const
Returns the number of elements of this image.
Definition dfufile.cc:516
bool write(QFile &file, CRC32 &crc, QString &errorMessage) const
Writes this image to the given file and updates the CRC.
Definition dfufile.cc:614
uint32_t size() const
Returns the total size of the image (including headers).
Definition dfufile.cc:475
bool read(QFile &file, CRC32 &crc, QString &errorMessage)
Reads an image from the given file and updates the CRC.
Definition dfufile.cc:573
AddressMap _addressmap
Maps an address range to element index.
Definition dfufile.hh:202
const Element & element(int i) const
Returns a reference to the i-th element of the image.
Definition dfufile.cc:521
const QString & name() const
Returns the name of the image.
Definition dfufile.cc:506
void setName(const QString &name)
Sets the name of the image.
Definition dfufile.cc:511
bool isAligned(unsigned blocksize) const
Checks if all element addresses and sizes is aligned with the given block size.
Definition dfufile.cc:554
QVector< Element >::iterator iterator
Iterator type over elements.
Definition dfufile.hh:124
uint32_t memSize() const
Returns the memory size stored in the image.
Definition dfufile.cc:483
void remElement(int i)
Removes the i-th element from this image.
Definition dfufile.cc:548
void sort()
Sorts all elements with respect to their addresses.
Definition dfufile.cc:642
Image & operator=(const Image &other)
Copying assignment.
Definition dfufile.cc:466
virtual ~Image()
Destructor.
Definition dfufile.cc:461
QVector< Element > _elements
The elements of the image.
Definition dfufile.hh:200
bool isNamed() const
Returns true if the image is named.
Definition dfufile.cc:501
iterator begin()
Retruns a pointer to the first element.
Definition dfufile.cc:562
iterator end()
Returns a pointer after the last element.
Definition dfufile.cc:567
void dump(QTextStream &stream) const
Prints a textual representation of the image into the given stream.
Definition dfufile.cc:655
uint8_t alternateSettings() const
Returns the alternate settings byte.
Definition dfufile.cc:491
QString _name
Optional image name.
Definition dfufile.hh:198
uint8_t _alternate_settings
Alternate settings byte.
Definition dfufile.hh:196
void addElement(uint32_t addr, uint32_t size, int index=-1)
Adds an element to the image with the given address and size at the specified index.
Definition dfufile.cc:531
bool read(const QString &filename, const ErrorStack &err=ErrorStack())
Reads the specified DFU file.
Definition dfufile.cc:108
QVector< Image > _images
The list of images.
Definition dfufile.hh:257
virtual bool isAllocated(uint32_t offset, uint32_t img=0) const
Returns true if the specified address (and image) is allocated.
Definition dfufile.cc:252
uint32_t memSize() const
Returns the total memory size stored in the DFU file.
Definition dfufile.cc:61
bool isAligned(unsigned blocksize) const
Checks if all image addresses and sizes is aligned with the given block size.
Definition dfufile.cc:100
virtual unsigned char * data(uint32_t offset, uint32_t img=0)
Returns a pointer to the encoded raw data at the specified offset.
Definition dfufile.cc:259
bool write(const QString &filename, const ErrorStack &err=ErrorStack())
Writes to the specified file.
Definition dfufile.cc:192
void remImage(int i)
Deletes the i-th image from the file.
Definition dfufile.cc:95
DFUFile(QObject *parent=nullptr)
Constructs an empty DFU file object.
Definition dfufile.cc:45
void dump(QTextStream &stream) const
Dumps a text representation of the DFU file structure to the specified text stream.
Definition dfufile.cc:273
void addImage(const QString &name, uint8_t altSettings=1)
Adds a new image to the file.
Definition dfufile.cc:85
uint32_t size() const
Returns the total size of the DFU file.
Definition dfufile.cc:52
int numImages() const
Returns the number of images within the DFU file.
Definition dfufile.cc:70
const Image & image(int i) const
Returns a reference to the i-th image of the file.
Definition dfufile.cc:75
Implements a stack of error messages to provide a pretty formatted error traceback.
Definition errorstack.hh:43