libdrmconf  0.11.3
A library to program DMR radios.
codeplug.hh
1 #ifndef CODEPLUG_HH
2 #define CODEPLUG_HH
3 
4 #include <QObject>
5 #include "dfufile.hh"
6 #include "userdatabase.hh"
7 #include <QHash>
8 #include "config.hh"
9 
10 //class Config;
11 class ConfigItem;
12 
13 
17 class Codeplug: public DFUFile
18 {
19  Q_OBJECT
20 
21 public:
24  class Flags {
25  public:
37 
39  Flags();
40  };
41 
46  class Element
47  {
48  protected:
52  Element(uint8_t *ptr, size_t size);
53 
54  public:
56  Element(const Element &other);
58  virtual ~Element();
59 
61  virtual bool isValid() const;
64  virtual void clear();
65 
67  bool fill(uint8_t value, unsigned offset=0, int size=-1);
68 
70  bool getBit(unsigned offset, unsigned bit) const;
72  void setBit(unsigned offset, unsigned bit, bool value=true);
74  void clearBit(unsigned offset, unsigned bit);
75 
77  uint8_t getUInt2(unsigned offset, unsigned bit) const;
79  void setUInt2(unsigned offset, unsigned bit, uint8_t value);
80 
82  uint8_t getUInt3(unsigned offset, unsigned bit) const;
84  void setUInt3(unsigned offset, unsigned bit, uint8_t value);
85 
87  uint8_t getUInt4(unsigned offset, unsigned bit) const;
89  void setUInt4(unsigned offset, unsigned bit, uint8_t value);
90 
92  uint8_t getUInt5(unsigned offset, unsigned bit) const;
94  void setUInt5(unsigned offset, unsigned bit, uint8_t value);
95 
97  uint8_t getUInt6(unsigned offset, unsigned bit) const;
99  void setUInt6(unsigned offset, unsigned bit, uint8_t value);
100 
102  uint8_t getUInt8(unsigned offset) const;
104  void setUInt8(unsigned offset, uint8_t value);
106  int8_t getInt8(unsigned offset) const;
108  void setInt8(unsigned offset, int8_t value);
109 
111  uint16_t getUInt16_be(unsigned offset) const;
113  uint16_t getUInt16_le(unsigned offset) const;
115  void setUInt16_be(unsigned offset, uint16_t value);
117  void setUInt16_le(unsigned offset, uint16_t value);
118 
120  uint32_t getUInt24_be(unsigned offset) const;
122  uint32_t getUInt24_le(unsigned offset) const;
124  void setUInt24_be(unsigned offset, uint32_t value);
126  void setUInt24_le(unsigned offset, uint32_t value);
127 
129  uint32_t getUInt32_be(unsigned offset) const;
131  uint32_t getUInt32_le(unsigned offset) const;
133  void setUInt32_be(unsigned offset, uint32_t value);
135  void setUInt32_le(unsigned offset, uint32_t value);
136 
138  uint64_t getUInt64_be(unsigned offset) const;
140  uint64_t getUInt64_le(unsigned offset) const;
142  void setUInt64_be(unsigned offset, uint64_t value);
144  void setUInt64_le(unsigned offset, uint64_t value);
145 
147  uint8_t getBCD2(unsigned offset) const;
149  void setBCD2(unsigned offset, uint8_t value);
150 
152  uint16_t getBCD4_be(unsigned offset) const;
154  void setBCD4_be(unsigned offset, uint16_t value);
156  uint16_t getBCD4_le(unsigned offset) const;
158  void setBCD4_le(unsigned offset, uint16_t value);
159 
161  uint32_t getBCD8_be(unsigned offset) const;
163  void setBCD8_be(unsigned offset, uint32_t value);
165  uint32_t getBCD8_le(unsigned offset) const;
167  void setBCD8_le(unsigned offset, uint32_t value);
168 
170  QString readASCII(unsigned offset, unsigned maxlen, uint8_t eos=0x00) const;
173  void writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos=0x00);
174 
176  QString readUnicode(unsigned offset, unsigned maxlen, uint16_t eos=0x0000) const;
179  void writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos=0x0000);
180 
181  protected:
183  uint8_t *_data;
185  size_t _size;
186  };
187 
194  class Context
195  {
196  public:
198  explicit Context(Config *config);
199 
201  Config *config() const;
202 
205  ConfigItem *obj(const QMetaObject *elementType, unsigned idx);
208  int index(ConfigItem *obj);
210  bool add(ConfigItem *obj, unsigned idx);
211 
213  bool addTable(const QMetaObject *obj);
215  bool hasTable(const QMetaObject *obj) const;
216 
218  template <class T>
219  T* get(unsigned idx) {
220  return this->obj(&(T::staticMetaObject), idx)->template as<T>();
221  }
222 
224  template <class T>
225  bool has(unsigned idx) {
226  return nullptr != this->obj(&(T::staticMetaObject), idx)->template as<T>();
227  }
228 
230  template <class T>
231  unsigned int count() {
232  return getTable(&T::staticMetaObject).indices.size();
233  }
234 
235  protected:
237  class Table {
238  public:
240  QHash<unsigned, ConfigItem *> objects;
242  QHash<ConfigItem *, unsigned> indices;
243  };
244 
245  protected:
247  Table &getTable(const QMetaObject *obj);
248 
249  protected:
253  QHash<QString, Table> _tables;
254  };
255 
256 protected:
258  explicit Codeplug(QObject *parent=nullptr);
259 
260 public:
262  virtual ~Codeplug();
263 
268  virtual bool index(Config *config, Context &ctx, const ErrorStack &err=ErrorStack()) const = 0;
269 
272  virtual bool decode(Config *config, const ErrorStack &err=ErrorStack()) = 0;
275  virtual bool encode(Config *config, const Flags &flags=Flags(), const ErrorStack &err=ErrorStack()) = 0;
276 };
277 
278 #endif // CODEPLUG_HH
Internal used table type to associate objects and indices.
Definition: codeplug.hh:237
QHash< unsigned, ConfigItem * > objects
The index->object map.
Definition: codeplug.hh:240
QHash< ConfigItem *, unsigned > indices
The object->index map.
Definition: codeplug.hh:242
Base class for all codeplug contexts.
Definition: codeplug.hh:195
QHash< QString, Table > _tables
Table of tables.
Definition: codeplug.hh:253
Context(Config *config)
Empty constructor.
Definition: codeplug.cc:586
int index(ConfigItem *obj)
Returns the index for the given object.
Definition: codeplug.cc:641
Config * config() const
Returns the reference to the config object.
Definition: codeplug.cc:604
Config * _config
A weak reference to the config object.
Definition: codeplug.hh:251
ConfigItem * obj(const QMetaObject *elementType, unsigned idx)
Resolves the given index for the specifies element type.
Definition: codeplug.cc:634
bool add(ConfigItem *obj, unsigned idx)
Associates the given object with the given index.
Definition: codeplug.cc:650
unsigned int count()
Returns the number of elements for the specified type.
Definition: codeplug.hh:231
bool has(unsigned idx)
Returns true, if the given index is defined for the specified type.
Definition: codeplug.hh:225
T * get(unsigned idx)
Returns the object associated by the given index and type.
Definition: codeplug.hh:219
bool addTable(const QMetaObject *obj)
Adds a table for the given type.
Definition: codeplug.cc:626
bool hasTable(const QMetaObject *obj) const
Returns true if a table is defined for the given type.
Definition: codeplug.cc:609
Table & getTable(const QMetaObject *obj)
Returns a reference to the table for the given type.
Definition: codeplug.cc:619
Represents the abstract base class of all codeplug elements.
Definition: codeplug.hh:47
void setUInt6(unsigned offset, unsigned bit, uint8_t value)
Stores a 6bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:198
Element(uint8_t *ptr, size_t size)
Hidden constructor.
Definition: codeplug.cc:21
uint16_t getUInt16_be(unsigned offset) const
Reads a 16bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:247
int8_t getInt8(unsigned offset) const
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition: codeplug.cc:229
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:525
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:460
void setUInt16_be(unsigned offset, uint16_t value)
Stores a 16bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:267
uint8_t * _data
Holds the pointer to the element.
Definition: codeplug.hh:183
uint64_t getUInt64_le(unsigned offset) const
Reads a 64bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:384
uint32_t getUInt24_be(unsigned offset) const
Reads a 24bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:288
uint32_t getUInt24_le(unsigned offset) const
Reads a 24bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:298
void setUInt8(unsigned offset, uint8_t value)
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:219
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:425
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:470
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:484
void setUInt32_be(unsigned offset, uint32_t value)
Stores a 32bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:353
uint64_t getUInt64_be(unsigned offset) const
Reads a 64bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:374
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:552
void clearBit(unsigned offset, unsigned bit)
Clears a specific bit at the given byte-offset.
Definition: codeplug.cc:88
void setUInt3(unsigned offset, unsigned bit, uint8_t value)
Stores a 3bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:132
void setUInt64_be(unsigned offset, uint64_t value)
Stores a 64bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:394
void setUInt16_le(unsigned offset, uint16_t value)
Stores a 16bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:277
void setUInt4(unsigned offset, unsigned bit, uint8_t value)
Stores a 4bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:154
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:543
void setUInt2(unsigned offset, unsigned bit, uint8_t value)
Stores a 2bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:110
void setUInt24_le(unsigned offset, uint32_t value)
Stores a 24bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:320
uint8_t getUInt8(unsigned offset) const
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:210
uint8_t getUInt4(unsigned offset, unsigned bit) const
Reads a 4bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:144
void setInt8(unsigned offset, int8_t value)
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition: codeplug.cc:237
void setUInt32_le(unsigned offset, uint32_t value)
Stores a 32bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:363
void setUInt64_le(unsigned offset, uint64_t value)
Stores a 64bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:404
virtual ~Element()
Destructor.
Definition: codeplug.cc:33
uint16_t getUInt16_le(unsigned offset) const
Reads a 16bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:257
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:563
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:437
virtual bool isValid() const
Returns true if the pointer is not null.
Definition: codeplug.cc:38
uint32_t getUInt32_be(unsigned offset) const
Reads a 32bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:333
bool fill(uint8_t value, unsigned offset=0, int size=-1)
Fills the memsets the entire element to the given value.
Definition: codeplug.cc:48
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:496
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:513
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:415
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:447
void setBit(unsigned offset, unsigned bit, bool value=true)
Sets a specific bit at the given byte-offset.
Definition: codeplug.cc:73
void setUInt24_be(unsigned offset, uint32_t value)
Stores a 24bit big-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:308
void setUInt5(unsigned offset, unsigned bit, uint8_t value)
Stores a 5bit iunsinged nteger at the given byte- and bit-offset.
Definition: codeplug.cc:176
uint8_t getUInt5(unsigned offset, unsigned bit) const
Reads a 5bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:166
uint8_t getUInt3(unsigned offset, unsigned bit) const
Reads a 3bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:122
uint8_t getUInt2(unsigned offset, unsigned bit) const
Reads a 2bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:100
uint8_t getUInt6(unsigned offset, unsigned bit) const
Reads a 6bit unsigned integer at the given byte- and bit-offset.
Definition: codeplug.cc:188
uint32_t getUInt32_le(unsigned offset) const
Reads a 32bit little-endian unsigned integer at the given byte-offset.
Definition: codeplug.cc:343
size_t _size
Holds the size of the element.
Definition: codeplug.hh:185
virtual void clear()
Abstract method to reset the element within the codeplug.
Definition: codeplug.cc:43
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:572
bool getBit(unsigned offset, unsigned bit) const
Reads a specific bit at the given byte-offset.
Definition: codeplug.cc:61
Certain flags passed to CodePlug::encode to control the transfer and encoding of the codeplug.
Definition: codeplug.hh:24
bool updateCodePlug
If true, the codeplug will first be downloaded from the device, updated from the abstract config and ...
Definition: codeplug.hh:30
bool autoEnableRoaming
If true enables automatic roaming when there is a roaming zone defined that is used by any channel.
Definition: codeplug.hh:36
bool autoEnableGPS
If true enables GPS when there is a GPS or APRS system defined that is used by any channel.
Definition: codeplug.hh:33
Flags()
Default constructor, enables code-plug update and disables automatic GPS/APRS and roaming.
Definition: codeplug.cc:11
This class defines the interface all device-specific code-plugs must implement.
Definition: codeplug.hh:18
Codeplug(QObject *parent=nullptr)
Hidden default constructor.
Definition: codeplug.cc:664
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 ~Codeplug()
Destructor.
Definition: codeplug.cc:670
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:69
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