libdrmconf  0.12.0
A library to program DMR radios.
codeplug.hh
1 #ifndef CODEPLUG_HH
2 #define CODEPLUG_HH
3 
4 #include <QObject>
5 #include <QHash>
6 #include "dfufile.hh"
7 
8 //#include "userdatabase.hh"
9 //#include "config.hh"
10 
11 class Config;
12 class ConfigItem;
13 
14 
18 class Codeplug: public DFUFile
19 {
20  Q_OBJECT
21 
22 public:
25  class Flags {
26  public:
38 
40  Flags();
41  };
42 
50  class Element
51  {
52  protected:
54  struct Offset {
56  struct BitOffset {
58  const unsigned int byte;
60  const unsigned int bit;
61  };
62  };
63 
64  public:
66  struct Limit {
68  template <class T> struct Range {
70  const T min;
72  const T max;
74  inline T limit(const T &value) const {
75  return std::min(max, std::max(min, value));
76  }
78  inline bool in(const T &value) const {
79  return (value <= max) && (value >= min);
80  }
81  };
82  };
83 
84  protected:
88  Element(uint8_t *ptr, size_t size);
89 
90  public:
92  Element(const Element &other);
94  virtual ~Element();
95 
97  Element &operator=(const Element &other);
98 
100  virtual bool isValid() const;
103  virtual void clear();
104 
106  bool fill(uint8_t value, unsigned offset=0, int size=-1);
107 
109  bool getBit(const Offset::BitOffset &offset) const;
111  bool getBit(unsigned offset, unsigned bit) const;
113  void setBit(const Offset::BitOffset &offset, bool value=true);
115  void setBit(unsigned offset, unsigned bit, bool value=true);
117  void clearBit(unsigned offset, unsigned bit);
118 
120  uint8_t getUInt2(unsigned offset, unsigned bit) const;
122  void setUInt2(unsigned offset, unsigned bit, uint8_t value);
123 
125  uint8_t getUInt3(unsigned offset, unsigned bit) const;
127  void setUInt3(unsigned offset, unsigned bit, uint8_t value);
128 
130  uint8_t getUInt4(unsigned offset, unsigned bit) const;
132  void setUInt4(unsigned offset, unsigned bit, uint8_t value);
133 
135  uint8_t getUInt5(unsigned offset, unsigned bit) const;
137  void setUInt5(unsigned offset, unsigned bit, uint8_t value);
138 
140  uint8_t getUInt6(unsigned offset, unsigned bit) const;
142  void setUInt6(unsigned offset, unsigned bit, uint8_t value);
143 
145  uint8_t getUInt8(unsigned offset) const;
147  void setUInt8(unsigned offset, uint8_t value);
149  int8_t getInt8(unsigned offset) const;
151  void setInt8(unsigned offset, int8_t value);
152 
154  uint16_t getUInt16_be(unsigned offset) const;
156  uint16_t getUInt16_le(unsigned offset) const;
158  void setUInt16_be(unsigned offset, uint16_t value);
160  void setUInt16_le(unsigned offset, uint16_t value);
161 
163  uint32_t getUInt24_be(unsigned offset) const;
165  uint32_t getUInt24_le(unsigned offset) const;
167  void setUInt24_be(unsigned offset, uint32_t value);
169  void setUInt24_le(unsigned offset, uint32_t value);
170 
172  uint32_t getUInt32_be(unsigned offset) const;
174  uint32_t getUInt32_le(unsigned offset) const;
176  void setUInt32_be(unsigned offset, uint32_t value);
178  void setUInt32_le(unsigned offset, uint32_t value);
179 
181  uint64_t getUInt64_be(unsigned offset) const;
183  uint64_t getUInt64_le(unsigned offset) const;
185  void setUInt64_be(unsigned offset, uint64_t value);
187  void setUInt64_le(unsigned offset, uint64_t value);
188 
190  uint8_t getBCD2(unsigned offset) const;
192  void setBCD2(unsigned offset, uint8_t value);
193 
195  uint16_t getBCD4_be(unsigned offset) const;
197  void setBCD4_be(unsigned offset, uint16_t value);
199  uint16_t getBCD4_le(unsigned offset) const;
201  void setBCD4_le(unsigned offset, uint16_t value);
202 
204  uint32_t getBCD8_be(unsigned offset) const;
206  void setBCD8_be(unsigned offset, uint32_t value);
208  uint32_t getBCD8_le(unsigned offset) const;
210  void setBCD8_le(unsigned offset, uint32_t value);
211 
213  QString readASCII(unsigned offset, unsigned maxlen, uint8_t eos=0x00) const;
216  void writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos=0x00);
217 
219  QString readUnicode(unsigned offset, unsigned maxlen, uint16_t eos=0x0000) const;
222  void writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos=0x0000);
223 
224  protected:
226  uint8_t *_data;
228  size_t _size;
229  };
230 
237  class Context
238  {
239  public:
241  explicit Context(Config *config);
242 
244  Config *config() const;
245 
248  ConfigItem *obj(const QMetaObject *elementType, unsigned idx);
251  int index(ConfigItem *obj);
253  bool add(ConfigItem *obj, unsigned idx);
254 
256  bool addTable(const QMetaObject *obj);
258  bool hasTable(const QMetaObject *obj) const;
259 
261  template <class T>
262  T* get(unsigned idx) {
263  return this->obj(&(T::staticMetaObject), idx)->template as<T>();
264  }
265 
267  template <class T>
268  bool has(unsigned idx) {
269  return nullptr != this->obj(&(T::staticMetaObject), idx)->template as<T>();
270  }
271 
273  template <class T>
274  unsigned int count() {
275  return getTable(&T::staticMetaObject).indices.size();
276  }
277 
278  protected:
280  class Table {
281  public:
283  QHash<unsigned, ConfigItem *> objects;
285  QHash<ConfigItem *, unsigned> indices;
286  };
287 
288  protected:
290  Table &getTable(const QMetaObject *obj);
291 
292  protected:
296  QHash<QString, Table> _tables;
297  };
298 
299 protected:
301  explicit Codeplug(QObject *parent=nullptr);
302 
303 public:
305  virtual ~Codeplug();
306 
311  virtual bool index(Config *config, Context &ctx, const ErrorStack &err=ErrorStack()) const = 0;
312 
315  virtual bool decode(Config *config, const ErrorStack &err=ErrorStack()) = 0;
318  virtual bool postprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
319 
322  virtual Config *preprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
325  virtual bool encode(Config *config, const Flags &flags=Flags(), const ErrorStack &err=ErrorStack()) = 0;
326 };
327 
328 #endif // CODEPLUG_HH
Internal used table type to associate objects and indices.
Definition: codeplug.hh:280
QHash< unsigned, ConfigItem * > objects
The index->object map.
Definition: codeplug.hh:283
QHash< ConfigItem *, unsigned > indices
The object->index map.
Definition: codeplug.hh:285
Base class for all codeplug contexts.
Definition: codeplug.hh:238
QHash< QString, Table > _tables
Table of tables.
Definition: codeplug.hh:296
Context(Config *config)
Empty constructor.
Definition: codeplug.cc:603
int index(ConfigItem *obj)
Returns the index for the given object.
Definition: codeplug.cc:659
Config * config() const
Returns the reference to the config object.
Definition: codeplug.cc:622
Config * _config
A weak reference to the config object.
Definition: codeplug.hh:294
ConfigItem * obj(const QMetaObject *elementType, unsigned idx)
Resolves the given index for the specifies element type.
Definition: codeplug.cc:652
bool add(ConfigItem *obj, unsigned idx)
Associates the given object with the given index.
Definition: codeplug.cc:668
unsigned int count()
Returns the number of elements for the specified type.
Definition: codeplug.hh:274
bool has(unsigned idx)
Returns true, if the given index is defined for the specified type.
Definition: codeplug.hh:268
T * get(unsigned idx)
Returns the object associated by the given index and type.
Definition: codeplug.hh:262
bool addTable(const QMetaObject *obj)
Adds a table for the given type.
Definition: codeplug.cc:644
bool hasTable(const QMetaObject *obj) const
Returns true if a table is defined for the given type.
Definition: codeplug.cc:627
Table & getTable(const QMetaObject *obj)
Returns a reference to the table for the given type.
Definition: codeplug.cc:637
Represents the abstract base class of all codeplug elements.
Definition: codeplug.hh:51
void setUInt6(unsigned offset, unsigned bit, uint8_t value)
Stores a 6bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:215
Element(uint8_t *ptr, size_t size)
Hidden constructor.
Definition: codeplug.cc:22
uint16_t getUInt16_be(unsigned offset) const
Reads a 16bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:264
int8_t getInt8(unsigned offset) const
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition: codeplug.cc:246
void setBCD8_le(unsigned offset, uint32_t value)
Stores a 8-digit (4-byte/32bit) BDC value in little-endian at the given byte-offset.
Definition: codeplug.cc:542
void setBit(const Offset::BitOffset &offset, bool value=true)
Sets a specific bit at the given byte-offset.
Definition: codeplug.cc:86
uint16_t getBCD4_le(unsigned offset) const
Reads a 4-digit (2-byte/16bit) BDC value in little-endian at the given byte-offset.
Definition: codeplug.cc:477
void setUInt16_be(unsigned offset, uint16_t value)
Stores a 16bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:284
uint8_t * _data
Holds the pointer to the element.
Definition: codeplug.hh:226
uint64_t getUInt64_le(unsigned offset) const
Reads a 64bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:401
uint32_t getUInt24_be(unsigned offset) const
Reads a 24bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:305
uint32_t getUInt24_le(unsigned offset) const
Reads a 24bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:315
void setUInt8(unsigned offset, uint8_t value)
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:236
void setBCD2(unsigned offset, uint8_t value)
Stores a 2-digit (1-byte/8bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:442
void setBCD4_le(unsigned offset, uint16_t value)
Stores a 4-digit (1-byte/16bit) BDC value in little-endian at the given byte-offset.
Definition: codeplug.cc:487
uint32_t getBCD8_be(unsigned offset) const
Reads a 8-digit (4-byte/32bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:501
void setUInt32_be(unsigned offset, uint32_t value)
Stores a 32bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:370
uint64_t getUInt64_be(unsigned offset) const
Reads a 64bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:391
void writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos=0x00)
Stores up to maxlen ASCII chars at the given byte-offset using eos as the string termination char.
Definition: codeplug.cc:569
void clearBit(unsigned offset, unsigned bit)
Clears a specific bit at the given byte-offset.
Definition: codeplug.cc:105
void setUInt3(unsigned offset, unsigned bit, uint8_t value)
Stores a 3bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:149
void setUInt64_be(unsigned offset, uint64_t value)
Stores a 64bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:411
void setUInt16_le(unsigned offset, uint16_t value)
Stores a 16bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:294
bool getBit(const Offset::BitOffset &offset) const
Reads a specific bit at the given byte-offset.
Definition: codeplug.cc:69
void setUInt4(unsigned offset, unsigned bit, uint8_t value)
Stores a 4bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:171
QString readASCII(unsigned offset, unsigned maxlen, uint8_t eos=0x00) const
Reads up to maxlen ASCII chars at the given byte-offset using eos as the string termination char.
Definition: codeplug.cc:560
void setUInt2(unsigned offset, unsigned bit, uint8_t value)
Stores a 2bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:127
void setUInt24_le(unsigned offset, uint32_t value)
Stores a 24bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:337
uint8_t getUInt8(unsigned offset) const
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:227
uint8_t getUInt4(unsigned offset, unsigned bit) const
Reads a 4bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:161
void setInt8(unsigned offset, int8_t value)
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition: codeplug.cc:254
void setUInt32_le(unsigned offset, uint32_t value)
Stores a 32bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:380
void setUInt64_le(unsigned offset, uint64_t value)
Stores a 64bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:421
virtual ~Element()
Destructor.
Definition: codeplug.cc:34
uint16_t getUInt16_le(unsigned offset) const
Reads a 16bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:274
QString readUnicode(unsigned offset, unsigned maxlen, uint16_t eos=0x0000) const
Reads up to maxlen unicode chars at the given byte-offset using eos as the string termination char.
Definition: codeplug.cc:580
uint16_t getBCD4_be(unsigned offset) const
Reads a 4-digit (2-byte/16bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:454
virtual bool isValid() const
Returns true if the pointer is not null.
Definition: codeplug.cc:46
uint32_t getUInt32_be(unsigned offset) const
Reads a 32bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:350
bool fill(uint8_t value, unsigned offset=0, int size=-1)
Fills the memsets the entire element to the given value.
Definition: codeplug.cc:56
Element & operator=(const Element &other)
Copy assignment.
Definition: codeplug.cc:39
void setBCD8_be(unsigned offset, uint32_t value)
Stores a 8-digit (4-byte/32bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:513
uint32_t getBCD8_le(unsigned offset) const
Reads a 8-digit (4-byte/32bit) BDC value in little-endian at the given byte-offset.
Definition: codeplug.cc:530
uint8_t getBCD2(unsigned offset) const
Reads a 2-digit (1-byte/8bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:432
void setBCD4_be(unsigned offset, uint16_t value)
Stores a 4-digit (2-byte/16bit) BDC value in big-endian at the given byte-offset.
Definition: codeplug.cc:464
void setUInt24_be(unsigned offset, uint32_t value)
Stores a 24bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:325
void setUInt5(unsigned offset, unsigned bit, uint8_t value)
Stores a 5bit iunsinged nteger at the given byte- and bit-offset.
Definition: codeplug.cc:193
uint8_t getUInt5(unsigned offset, unsigned bit) const
Reads a 5bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:183
uint8_t getUInt3(unsigned offset, unsigned bit) const
Reads a 3bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:139
uint8_t getUInt2(unsigned offset, unsigned bit) const
Reads a 2bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:117
uint8_t getUInt6(unsigned offset, unsigned bit) const
Reads a 6bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:205
uint32_t getUInt32_le(unsigned offset) const
Reads a 32bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:360
size_t _size
Holds the size of the element.
Definition: codeplug.hh:228
virtual void clear()
Abstract method to reset the element within the codeplug.
Definition: codeplug.cc:51
void writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos=0x0000)
Stores up to maxlen unicode chars at the given byte-offset using eos as the string termination char.
Definition: codeplug.cc:589
Certain flags passed to CodePlug::encode to control the transfer and encoding of the codeplug.
Definition: codeplug.hh:25
bool updateCodePlug
If true, the codeplug will first be downloaded from the device, updated from the abstract config and ...
Definition: codeplug.hh:31
bool autoEnableRoaming
If true enables automatic roaming when there is a roaming zone defined that is used by any channel.
Definition: codeplug.hh:37
bool autoEnableGPS
If true enables GPS when there is a GPS or APRS system defined that is used by any channel.
Definition: codeplug.hh:34
Flags()
Default constructor, enables code-plug update and disables automatic GPS/APRS and roaming.
Definition: codeplug.cc:12
This class defines the interface all device-specific code-plugs must implement.
Definition: codeplug.hh:19
Codeplug(QObject *parent=nullptr)
Hidden default constructor.
Definition: codeplug.cc:682
virtual bool index(Config *config, Context &ctx, const ErrorStack &err=ErrorStack()) const =0
Indexes all elements of the codeplug.
virtual bool encode(Config *config, const Flags &flags=Flags(), const ErrorStack &err=ErrorStack())=0
Encodes a given abstract configuration (config) to the device specific binary code-plug.
virtual bool postprocess(Config *config, const ErrorStack &err=ErrorStack()) const
Retruns a post-processed configuration of the decoded config.
Definition: codeplug.cc:698
virtual ~Codeplug()
Destructor.
Definition: codeplug.cc:688
virtual Config * preprocess(Config *config, const ErrorStack &err=ErrorStack()) const
Retruns a prepared configuration for this particular radio.
Definition: codeplug.cc:693
virtual bool decode(Config *config, const ErrorStack &err=ErrorStack())=0
Decodes a binary codeplug to the given abstract configuration config.
Base class for all configuration objects (channels, zones, contacts, etc).
Definition: configobject.hh:40
The config class, representing the codeplug configuration.
Definition: config.hh:70
A collection of images, each consisting of one or more memory sections.
Definition: dfufile.hh:73
uint32_t size() const
Returns the total size of the DFU file.
Definition: dfufile.cc:52
Implements a stack of error messages to provide a pretty formatted error traceback.
Definition: errorstack.hh:41
Holds a range of values [min, max].
Definition: codeplug.hh:68
T limit(const T &value) const
Limits the value to the range.
Definition: codeplug.hh:74
bool in(const T &value) const
Checks if value is in range.
Definition: codeplug.hh:78
const T max
Upper bound.
Definition: codeplug.hh:72
const T min
Lower bound.
Definition: codeplug.hh:70
Base class for Limits.
Definition: codeplug.hh:66
Some type to specify a bit offset.
Definition: codeplug.hh:56
const unsigned int bit
The bit within the byte.
Definition: codeplug.hh:60
const unsigned int byte
The byte offset.
Definition: codeplug.hh:58
Base class for Offsets.
Definition: codeplug.hh:54