libdrmconf 0.12.1
A library to program DMR radios.
Loading...
Searching...
No Matches
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
11class Config;
12class ConfigItem;
14
15
19class Codeplug: public DFUFile
20{
21 Q_OBJECT
22
23public:
26 class Flags {
27 public:
39
41 Flags();
42 };
43
51 class Element
52 {
53 protected:
55 struct Offset {
57 struct Bit {
59 const unsigned int byte;
61 const unsigned int bit;
62
64 inline Bit operator+ (unsigned int bits) const {
65 unsigned int tmp = 8 * byte + (7-bit) + bits;
66 return {tmp/8, (7 - bit % 8)};
67 }
68
70 inline Bit operator- (unsigned int bits) const {
71 unsigned int tmp = 8 * byte + (7-bit) - bits;
72 return {tmp/8, (7 - bit % 8)};
73 }
74 };
75 };
76
77 public:
79 struct Limit {
81 template <class T> struct Range {
83 const T min;
85 const T max;
87 inline T limit(const T &value) const {
88 return std::min(max, std::max(min, value));
89 }
91 inline bool in(const T &value) const {
92 return (value <= max) && (value >= min);
93 }
94 };
95 };
96
97 protected:
101 Element(uint8_t *ptr, size_t size);
102
103 public:
105 Element(const Element &other);
107 virtual ~Element();
108
110 Element &operator=(const Element &other);
111
113 virtual bool isValid() const;
116 virtual void clear();
117
119 bool fill(uint8_t value, unsigned offset=0, int size=-1);
120
122 bool getBit(const Offset::Bit &offset) const;
124 bool getBit(unsigned offset, unsigned bit) const;
126 void setBit(const Offset::Bit &offset, bool value=true);
128 void setBit(unsigned offset, unsigned bit, bool value=true);
130 void clearBit(unsigned offset, unsigned bit);
132 void clearBit(const Offset::Bit &offset);
133
135 uint8_t getUInt2(const Offset::Bit &offset) const;
137 uint8_t getUInt2(unsigned offset, unsigned bit) const;
139 void setUInt2(const Offset::Bit &offset, uint8_t value);
141 void setUInt2(unsigned offset, unsigned bit, uint8_t value);
142
144 uint8_t getUInt3(const Offset::Bit &offset) const;
146 uint8_t getUInt3(unsigned offset, unsigned bit) const;
148 void setUInt3(const Offset::Bit &offset, uint8_t value);
150 void setUInt3(unsigned offset, unsigned bit, uint8_t value);
151
153 uint8_t getUInt4(const Offset::Bit &offset) const;
155 uint8_t getUInt4(unsigned offset, unsigned bit) const;
157 void setUInt4(const Offset::Bit &offset, uint8_t value);
159 void setUInt4(unsigned offset, unsigned bit, uint8_t value);
160
162 uint8_t getUInt5(const Offset::Bit &offset) const;
164 uint8_t getUInt5(unsigned offset, unsigned bit) const;
166 void setUInt5(const Offset::Bit &offset, uint8_t value);
168 void setUInt5(unsigned offset, unsigned bit, uint8_t value);
169
171 uint8_t getUInt6(const Offset::Bit &offset) const;
173 uint8_t getUInt6(unsigned offset, unsigned bit) const;
175 void setUInt6(const Offset::Bit &offset, uint8_t value);
177 void setUInt6(unsigned offset, unsigned bit, uint8_t value);
178
180 uint8_t getUInt8(unsigned offset) const;
182 void setUInt8(unsigned offset, uint8_t value);
184 int8_t getInt8(unsigned offset) const;
186 void setInt8(unsigned offset, int8_t value);
187
189 uint16_t getUInt16_be(unsigned offset) const;
191 uint16_t getUInt16_le(unsigned offset) const;
193 void setUInt16_be(unsigned offset, uint16_t value);
195 void setUInt16_le(unsigned offset, uint16_t value);
196
198 uint32_t getUInt24_be(unsigned offset) const;
200 uint32_t getUInt24_le(unsigned offset) const;
202 void setUInt24_be(unsigned offset, uint32_t value);
204 void setUInt24_le(unsigned offset, uint32_t value);
205
207 uint32_t getUInt32_be(unsigned offset) const;
209 uint32_t getUInt32_le(unsigned offset) const;
211 void setUInt32_be(unsigned offset, uint32_t value);
213 void setUInt32_le(unsigned offset, uint32_t value);
214
216 uint64_t getUInt64_be(unsigned offset) const;
218 uint64_t getUInt64_le(unsigned offset) const;
220 void setUInt64_be(unsigned offset, uint64_t value);
222 void setUInt64_le(unsigned offset, uint64_t value);
223
225 uint8_t getBCD2(unsigned offset) const;
227 void setBCD2(unsigned offset, uint8_t value);
228
230 uint16_t getBCD4_be(unsigned offset) const;
232 void setBCD4_be(unsigned offset, uint16_t value);
234 uint16_t getBCD4_le(unsigned offset) const;
236 void setBCD4_le(unsigned offset, uint16_t value);
237
239 uint32_t getBCD8_be(unsigned offset) const;
241 void setBCD8_be(unsigned offset, uint32_t value);
243 uint32_t getBCD8_le(unsigned offset) const;
245 void setBCD8_le(unsigned offset, uint32_t value);
246
248 QString readASCII(unsigned offset, unsigned maxlen, uint8_t eos=0x00) const;
251 void writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos=0x00);
252
254 QString readUnicode(unsigned offset, unsigned maxlen, uint16_t eos=0x0000) const;
257 void writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos=0x0000);
258
259 protected:
261 uint8_t *_data;
263 size_t _size;
264 };
265
273 {
274 public:
276 explicit Context(Config *config);
277
279 Config *config() const;
280
283
286 ConfigItem *obj(const QMetaObject *elementType, unsigned idx);
289 int index(ConfigItem *obj);
291 bool add(ConfigItem *obj, unsigned idx);
292
294 bool addTable(const QMetaObject *obj);
296 bool hasTable(const QMetaObject *obj) const;
297
299 template <class T>
300 T* get(unsigned idx) {
301 return this->obj(&(T::staticMetaObject), idx)->template as<T>();
302 }
303
305 template <class T>
306 bool has(unsigned idx) {
307 return nullptr != this->obj(&(T::staticMetaObject), idx)->template as<T>();
308 }
309
311 template <class T>
312 unsigned int count() {
313 return getTable(&T::staticMetaObject).indices.size();
314 }
315
316 protected:
318 class Table {
319 public:
321 QHash<unsigned, ConfigItem *> objects;
323 QHash<ConfigItem *, unsigned> indices;
324 };
325
326 protected:
328 Table &getTable(const QMetaObject *obj);
329
330 protected:
336 QHash<QString, Table> _tables;
337 };
338
339protected:
341 explicit Codeplug(QObject *parent=nullptr);
342
343public:
345 virtual ~Codeplug();
346
351 virtual bool index(Config *config, Context &ctx, const ErrorStack &err=ErrorStack()) const = 0;
352
355 virtual bool decode(Config *config, const ErrorStack &err=ErrorStack()) = 0;
358 virtual bool postprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
359
362 virtual Config *preprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
365 virtual bool encode(Config *config, const Flags &flags=Flags(), const ErrorStack &err=ErrorStack()) = 0;
366};
367
368#endif // CODEPLUG_HH
Internal used table type to associate objects and indices.
Definition codeplug.hh:318
QHash< unsigned, ConfigItem * > objects
The index->object map.
Definition codeplug.hh:321
QHash< ConfigItem *, unsigned > indices
The object->index map.
Definition codeplug.hh:323
Base class for all codeplug contexts.
Definition codeplug.hh:273
QHash< QString, Table > _tables
Table of tables.
Definition codeplug.hh:336
Context(Config *config)
Empty constructor.
Definition codeplug.cc:654
SatelliteDatabase * satellites() const
Returns a reference to the satellite database.
Definition codeplug.cc:678
int index(ConfigItem *obj)
Returns the index for the given object.
Definition codeplug.cc:715
Config * config() const
Returns the reference to the config object.
Definition codeplug.cc:673
Config * _config
A weak reference to the config object.
Definition codeplug.hh:332
ConfigItem * obj(const QMetaObject *elementType, unsigned idx)
Resolves the given index for the specifies element type.
Definition codeplug.cc:708
T * get(unsigned idx)
Returns the object associated by the given index and type.
Definition codeplug.hh:300
bool add(ConfigItem *obj, unsigned idx)
Associates the given object with the given index.
Definition codeplug.cc:724
unsigned int count()
Returns the number of elements for the specified type.
Definition codeplug.hh:312
SatelliteDatabase * _satellites
A weak reference to the satellite database.
Definition codeplug.hh:334
bool has(unsigned idx)
Returns true, if the given index is defined for the specified type.
Definition codeplug.hh:306
bool addTable(const QMetaObject *obj)
Adds a table for the given type.
Definition codeplug.cc:700
bool hasTable(const QMetaObject *obj) const
Returns true if a table is defined for the given type.
Definition codeplug.cc:683
Table & getTable(const QMetaObject *obj)
Returns a reference to the table for the given type.
Definition codeplug.cc:693
Represents the abstract base class of all codeplug elements.
Definition codeplug.hh:52
uint16_t getUInt16_be(unsigned offset) const
Reads a 16bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:315
int8_t getInt8(unsigned offset) const
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition codeplug.cc:297
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:593
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:528
void setUInt16_be(unsigned offset, uint16_t value)
Stores a 16bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:335
uint8_t * _data
Holds the pointer to the element.
Definition codeplug.hh:261
uint64_t getUInt64_le(unsigned offset) const
Reads a 64bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:452
uint32_t getUInt24_be(unsigned offset) const
Reads a 24bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:356
uint32_t getUInt24_le(unsigned offset) const
Reads a 24bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:366
void setUInt8(unsigned offset, uint8_t value)
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:287
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:493
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:538
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:552
void setUInt3(const Offset::Bit &offset, uint8_t value)
Stores a 3bit unsigned integer at the given bit-offset.
Definition codeplug.cc:157
void setUInt32_be(unsigned offset, uint32_t value)
Stores a 32bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:421
void setUInt4(const Offset::Bit &offset, uint8_t value)
Stores a 4bit unsigned integer at the given bit-offset.
Definition codeplug.cc:188
uint64_t getUInt64_be(unsigned offset) const
Reads a 64bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:442
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:620
void clearBit(unsigned offset, unsigned bit)
Clears a specific bit at the given byte-offset.
Definition codeplug.cc:109
uint8_t getUInt6(const Offset::Bit &offset) const
Reads a 6bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:247
void setUInt64_be(unsigned offset, uint64_t value)
Stores a 64bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:462
void setUInt16_le(unsigned offset, uint16_t value)
Stores a 16bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:345
uint8_t getUInt5(const Offset::Bit &offset) const
Reads a 5bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:216
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:611
uint8_t getUInt3(const Offset::Bit &offset) const
Reads a 3bit unsigned integer at the given bit-offset.
Definition codeplug.cc:153
void setUInt24_le(unsigned offset, uint32_t value)
Stores a 24bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:388
uint8_t getUInt8(unsigned offset) const
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:278
void setInt8(unsigned offset, int8_t value)
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition codeplug.cc:305
void setUInt32_le(unsigned offset, uint32_t value)
Stores a 32bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:431
void setUInt64_le(unsigned offset, uint64_t value)
Stores a 64bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:472
void setUInt6(const Offset::Bit &offset, uint8_t value)
Stores a 6bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:251
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:325
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:631
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:505
virtual bool isValid() const
Returns true if the pointer is not null.
Definition codeplug.cc:46
void setUInt5(const Offset::Bit &offset, uint8_t value)
Stores a 5bit iunsinged nteger at the given byte- and bit-offset.
Definition codeplug.cc:220
uint32_t getUInt32_be(unsigned offset) const
Reads a 32bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:401
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:564
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:581
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:483
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:515
void setUInt24_be(unsigned offset, uint32_t value)
Stores a 24bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:376
void setBit(const Offset::Bit &offset, bool value=true)
Sets a specific bit at the given byte-offset.
Definition codeplug.cc:86
bool getBit(const Offset::Bit &offset) const
Reads a specific bit at the given byte-offset.
Definition codeplug.cc:69
uint8_t getUInt4(const Offset::Bit &offset) const
Reads a 4bit unsigned integer at the given bit-offset.
Definition codeplug.cc:184
uint8_t getUInt2(const Offset::Bit &offset) const
Reads a 2bit unsigned integer at the given bit-offset.
Definition codeplug.cc:122
uint32_t getUInt32_le(unsigned offset) const
Reads a 32bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:411
size_t _size
Holds the size of the element.
Definition codeplug.hh:263
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:640
void setUInt2(const Offset::Bit &offset, uint8_t value)
Stores a 2bit unsigned integer at the given bit-offset.
Definition codeplug.cc:126
Certain flags passed to CodePlug::encode to control the transfer and encoding of the codeplug.
Definition codeplug.hh:26
bool updateCodePlug
If true, the codeplug will first be downloaded from the device, updated from the abstract config and ...
Definition codeplug.hh:32
bool autoEnableRoaming
If true enables automatic roaming when there is a roaming zone defined that is used by any channel.
Definition codeplug.hh:38
bool autoEnableGPS
If true enables GPS when there is a GPS or APRS system defined that is used by any channel.
Definition codeplug.hh:35
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:20
Codeplug(QObject *parent=nullptr)
Hidden default constructor.
Definition codeplug.cc:738
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:754
virtual ~Codeplug()
Destructor.
Definition codeplug.cc:744
virtual Config * preprocess(Config *config, const ErrorStack &err=ErrorStack()) const
Retruns a prepared configuration for this particular radio.
Definition codeplug.cc:749
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
A table holding all known satellites.
Definition satellitedatabase.hh:64
Holds a range of values [min, max].
Definition codeplug.hh:81
T limit(const T &value) const
Limits the value to the range.
Definition codeplug.hh:87
bool in(const T &value) const
Checks if value is in range.
Definition codeplug.hh:91
const T max
Upper bound.
Definition codeplug.hh:85
const T min
Lower bound.
Definition codeplug.hh:83
Base class for Limits.
Definition codeplug.hh:79
Some type to specify a bit offset.
Definition codeplug.hh:57
const unsigned int byte
The byte offset.
Definition codeplug.hh:59
Bit operator+(unsigned int bits) const
Implements a simple increment.
Definition codeplug.hh:64
Bit operator-(unsigned int bits) const
Implements a simple increment.
Definition codeplug.hh:70
const unsigned int bit
The bit within the byte.
Definition codeplug.hh:61
Base class for Offsets.
Definition codeplug.hh:55