libdrmconf  0.11.3
A library to program DMR radios.
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 
13 class CRC32;
14 
72 class DFUFile: public QObject
73 {
74  Q_OBJECT
75 
76 public:
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:
125  Image();
127  Image(const QString &name, uint8_t altSettings=0);
129  Image(const Image &other);
131  virtual ~Image();
133  Image &operator=(const Image &other);
134 
136  uint8_t alternateSettings() const;
138  void setAlternateSettings(uint8_t s);
139 
141  bool isNamed() const;
143  const QString &name() const;
145  void setName(const QString &name);
147  uint32_t size() const;
149  uint32_t memSize() const;
151  int numElements() const;
153  const Element &element(int i) const;
155  Element &element(int i);
158  void addElement(uint32_t addr, uint32_t size, int index=-1);
160  void addElement(const Element &element);
162  void remElement(int i);
164  bool isAligned(unsigned blocksize) const;
165 
167  bool read(QFile &file, CRC32 &crc, QString &errorMessage);
169  bool write(QFile &file, CRC32 &crc, QString &errorMessage) const;
170 
172  void dump(QTextStream &stream) const;
173 
175  virtual bool isAllocated(uint32_t offset) const;
176 
178  virtual unsigned char *data(uint32_t offset);
180  virtual const unsigned char *data(uint32_t offset) const;
181 
183  void sort();
184 
185  protected:
189  QString _name;
191  QVector<Element> _elements;
194  };
195 
196 public:
198  DFUFile(QObject *parent=nullptr);
199 
201  uint32_t size() const;
203  uint32_t memSize() const;
204 
206  int numImages() const;
208  const Image &image(int i) const;
210  Image &image(int i);
212  void addImage(const QString &name, uint8_t altSettings=1);
214  void addImage(const Image &img);
216  void remImage(int i);
217 
219  bool isAligned(unsigned blocksize) const;
220 
223  bool read(const QString &filename, const ErrorStack &err=ErrorStack());
226  bool read(QFile &file, const ErrorStack &err=ErrorStack());
227 
230  bool write(const QString &filename, const ErrorStack &err=ErrorStack());
233  bool write(QFile &file, const ErrorStack &err=ErrorStack());
234 
236  void dump(QTextStream &stream) const;
237 
239  virtual bool isAllocated(uint32_t offset, uint32_t img=0) const;
240 
242  virtual unsigned char *data(uint32_t offset, uint32_t img=0);
244  virtual const unsigned char *data(uint32_t offset, uint32_t img=0) const;
245 
246 protected:
248  QVector<Image> _images;
249 };
250 
251 #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:662
virtual bool isAllocated(uint32_t offset) const
Returns true if the specified address is allocated.
Definition: dfufile.cc:657
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:603
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:562
AddressMap _addressmap
Maps an address range to element index.
Definition: dfufile.hh:193
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
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:631
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:191
bool isNamed() const
Returns true if the image is named.
Definition: dfufile.cc:501
void dump(QTextStream &stream) const
Prints a textual representation of the image into the given stream.
Definition: dfufile.cc:644
uint8_t alternateSettings() const
Returns the alternate settings byte.
Definition: dfufile.cc:491
QString _name
Optional image name.
Definition: dfufile.hh:189
uint8_t _alternate_settings
Alternate settings byte.
Definition: dfufile.hh:187
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
A collection of images, each consisting of one or more memory sections.
Definition: dfufile.hh:73
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:248
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:41