libdrmconf 0.13.3
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#include "transferflags.hh"
8
9class Config;
10class ConfigItem;
12
13
17class Codeplug: public DFUFile
18{
19 Q_OBJECT
20
21public:
24 class Flags: public TransferFlags {
25 public:
27 Flags();
28
29 public:
34 bool updateCodeplug() const;
36 void setUpdateCodeplug(bool enable);
37
40 bool autoEnableGPS() const;
42 void setAutoEnableGPS(bool enable);
43
46 bool autoEnableRoaming() const;
48 void setAutoEnableRoaming(bool enable);
49
50 protected:
51 bool _updateCodeplug;
52 bool _autoEnableGPS;
53 bool _autoEnableRoaming;
54 };
55
56
64 class Element
65 {
66 protected:
68 struct Offset {
70 struct Bit {
72 const unsigned int byte;
74 const unsigned int bit;
75
77 inline Bit operator+ (unsigned int bits) const {
78 unsigned int tmp = 8 * byte + (7-bit) + bits;
79 return {tmp/8, (7 - (tmp % 8))};
80 }
81
83 inline Bit operator- (unsigned int bits) const {
84 unsigned int tmp = 8 * byte + (7-bit) - bits;
85 return {tmp/8, (7 - (tmp % 8))};
86 }
87 };
88 };
89
90 public:
92 struct Limit {
94 template <class T> struct Range {
96 const T min;
98 const T max;
100 inline T limit(const T &value) const {
101 return std::min(max, std::max(min, value));
102 }
103
104 inline bool in(const T &value) const {
105 return (value <= max) && (value >= min);
106 }
107
108 inline T mapTo(const Range<T> &other, const T &value) const {
109 T myD = max-min, oD = other.max-other.min;
110 return ((limit(value)-min)*oD)/myD + other.min;
111 }
112 };
113 };
114
115 protected:
119 Element(uint8_t *ptr, size_t size);
120
121 public:
123 Element(const Element &other);
125 virtual ~Element();
126
128 Element &operator=(const Element &other);
129
131 virtual bool isValid() const;
134 virtual void clear();
135
137 bool fill(uint8_t value, unsigned offset=0, int size=-1);
138
140 bool getBit(const Offset::Bit &offset) const;
142 bool getBit(unsigned offset, unsigned bit) const;
144 void setBit(const Offset::Bit &offset, bool value=true);
146 void setBit(unsigned offset, unsigned bit, bool value=true);
148 void clearBit(unsigned offset, unsigned bit);
150 void clearBit(const Offset::Bit &offset);
151
153 uint8_t getUInt2(const Offset::Bit &offset) const;
155 uint8_t getUInt2(unsigned offset, unsigned bit) const;
157 void setUInt2(const Offset::Bit &offset, uint8_t value);
159 void setUInt2(unsigned offset, unsigned bit, uint8_t value);
160
162 uint8_t getUInt3(const Offset::Bit &offset) const;
164 uint8_t getUInt3(unsigned offset, unsigned bit) const;
166 void setUInt3(const Offset::Bit &offset, uint8_t value);
168 void setUInt3(unsigned offset, unsigned bit, uint8_t value);
169
171 uint8_t getUInt4(const Offset::Bit &offset) const;
173 uint8_t getUInt4(unsigned offset, unsigned bit) const;
175 void setUInt4(const Offset::Bit &offset, uint8_t value);
177 void setUInt4(unsigned offset, unsigned bit, uint8_t value);
178
180 uint8_t getUInt5(const Offset::Bit &offset) const;
182 uint8_t getUInt5(unsigned offset, unsigned bit) const;
184 void setUInt5(const Offset::Bit &offset, uint8_t value);
186 void setUInt5(unsigned offset, unsigned bit, uint8_t value);
187
189 uint8_t getUInt6(const Offset::Bit &offset) const;
191 uint8_t getUInt6(unsigned offset, unsigned bit) const;
193 void setUInt6(const Offset::Bit &offset, uint8_t value);
195 void setUInt6(unsigned offset, unsigned bit, uint8_t value);
196
198 uint8_t getUInt8(unsigned offset) const;
200 void setUInt8(unsigned offset, uint8_t value);
202 int8_t getInt8(unsigned offset) const;
204 void setInt8(unsigned offset, int8_t value);
205
207 uint16_t getUInt16_be(unsigned offset) const;
209 uint16_t getUInt16_le(unsigned offset) const;
211 void setUInt16_be(unsigned offset, uint16_t value);
213 void setUInt16_le(unsigned offset, uint16_t value);
214
216 uint32_t getUInt24_be(unsigned offset) const;
218 uint32_t getUInt24_le(unsigned offset) const;
220 void setUInt24_be(unsigned offset, uint32_t value);
222 void setUInt24_le(unsigned offset, uint32_t value);
223
225 uint32_t getUInt32_be(unsigned offset) const;
227 uint32_t getUInt32_le(unsigned offset) const;
229 void setUInt32_be(unsigned offset, uint32_t value);
231 void setUInt32_le(unsigned offset, uint32_t value);
232
234 uint64_t getUInt64_be(unsigned offset) const;
236 uint64_t getUInt64_le(unsigned offset) const;
238 void setUInt64_be(unsigned offset, uint64_t value);
240 void setUInt64_le(unsigned offset, uint64_t value);
241
243 uint8_t getBCD2(unsigned offset) const;
245 void setBCD2(unsigned offset, uint8_t value);
246
248 uint16_t getBCD4_be(unsigned offset) const;
250 void setBCD4_be(unsigned offset, uint16_t value);
252 uint16_t getBCD4_le(unsigned offset) const;
254 void setBCD4_le(unsigned offset, uint16_t value);
255
257 uint32_t getBCD8_be(unsigned offset) const;
259 void setBCD8_be(unsigned offset, uint32_t value);
261 uint32_t getBCD8_le(unsigned offset) const;
263 void setBCD8_le(unsigned offset, uint32_t value);
264
266 QString readASCII(unsigned offset, unsigned maxlen, uint8_t eos=0x00) const;
269 void writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos=0x00);
270
272 QString readUnicode(unsigned offset, unsigned maxlen, uint16_t eos=0x0000) const;
275 void writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos=0x0000);
276
277 protected:
279 uint8_t *_data;
281 size_t _size;
282 };
283
291 {
292 public:
294 explicit Context(Config *config);
295
297 Config *config() const;
298
301
304 ConfigItem *obj(const QMetaObject *elementType, unsigned idx);
307 int index(ConfigItem *obj);
309 bool add(ConfigItem *obj, unsigned idx);
310
312 bool addTable(const QMetaObject *obj);
314 bool hasTable(const QMetaObject *obj) const;
315
317 template <class T>
318 T* get(unsigned idx) {
319 return this->obj(&(T::staticMetaObject), idx)->template as<T>();
320 }
321
323 template <class T>
324 bool has(unsigned idx) {
325 return nullptr != this->obj(&(T::staticMetaObject), idx)->template as<T>();
326 }
327
329 template <class T>
330 unsigned int count() {
331 return getTable(&T::staticMetaObject).indices.size();
332 }
333
334 protected:
336 class Table {
337 public:
339 QHash<unsigned, ConfigItem *> objects;
341 QHash<ConfigItem *, unsigned> indices;
342 };
343
344 protected:
346 Table &getTable(const QMetaObject *obj);
347
348 protected:
354 QHash<QString, Table> _tables;
355 };
356
357protected:
359 explicit Codeplug(QObject *parent=nullptr);
360
361public:
363 virtual ~Codeplug();
364
369 virtual bool index(Config *config, Context &ctx, const ErrorStack &err=ErrorStack()) const = 0;
370
373 virtual bool decode(Config *config, const ErrorStack &err=ErrorStack()) = 0;
376 virtual bool postprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
377
380 virtual Config *preprocess(Config *config, const ErrorStack &err=ErrorStack()) const;
383 virtual bool encode(Config *config, const Flags &flags=Flags(), const ErrorStack &err=ErrorStack()) = 0;
384};
385
386#endif // CODEPLUG_HH
Internal used table type to associate objects and indices.
Definition codeplug.hh:336
QHash< unsigned, ConfigItem * > objects
The index->object map.
Definition codeplug.hh:339
QHash< ConfigItem *, unsigned > indices
The object->index map.
Definition codeplug.hh:341
Base class for all codeplug contexts.
Definition codeplug.hh:291
QHash< QString, Table > _tables
Table of tables.
Definition codeplug.hh:354
Context(Config *config)
Empty constructor.
Definition codeplug.cc:691
SatelliteDatabase * satellites() const
Returns a reference to the satellite database.
Definition codeplug.cc:718
int index(ConfigItem *obj)
Returns the index for the given object.
Definition codeplug.cc:755
Config * config() const
Returns the reference to the config object.
Definition codeplug.cc:713
Config * _config
A weak reference to the config object.
Definition codeplug.hh:350
ConfigItem * obj(const QMetaObject *elementType, unsigned idx)
Resolves the given index for the specifies element type.
Definition codeplug.cc:748
T * get(unsigned idx)
Returns the object associated by the given index and type.
Definition codeplug.hh:318
bool add(ConfigItem *obj, unsigned idx)
Associates the given object with the given index.
Definition codeplug.cc:764
unsigned int count()
Returns the number of elements for the specified type.
Definition codeplug.hh:330
SatelliteDatabase * _satellites
A weak reference to the satellite database.
Definition codeplug.hh:352
bool has(unsigned idx)
Returns true, if the given index is defined for the specified type.
Definition codeplug.hh:324
bool addTable(const QMetaObject *obj)
Adds a table for the given type.
Definition codeplug.cc:740
bool hasTable(const QMetaObject *obj) const
Returns true if a table is defined for the given type.
Definition codeplug.cc:723
Table & getTable(const QMetaObject *obj)
Returns a reference to the table for the given type.
Definition codeplug.cc:733
Represents the abstract base class of all codeplug elements.
Definition codeplug.hh:65
Element(uint8_t *ptr, size_t size)
Hidden constructor.
Definition codeplug.cc:56
uint16_t getUInt16_be(unsigned offset) const
Reads a 16bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:349
int8_t getInt8(unsigned offset) const
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition codeplug.cc:331
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:630
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:565
void setUInt16_be(unsigned offset, uint16_t value)
Stores a 16bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:371
uint8_t * _data
Holds the pointer to the element.
Definition codeplug.hh:279
uint64_t getUInt64_le(unsigned offset) const
Reads a 64bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:489
uint32_t getUInt24_be(unsigned offset) const
Reads a 24bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:393
uint32_t getUInt24_le(unsigned offset) const
Reads a 24bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:403
void setUInt8(unsigned offset, uint8_t value)
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:321
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:530
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:575
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:589
void setUInt3(const Offset::Bit &offset, uint8_t value)
Stores a 3bit unsigned integer at the given bit-offset.
Definition codeplug.cc:191
void setUInt32_be(unsigned offset, uint32_t value)
Stores a 32bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:458
void setUInt4(const Offset::Bit &offset, uint8_t value)
Stores a 4bit unsigned integer at the given bit-offset.
Definition codeplug.cc:222
uint64_t getUInt64_be(unsigned offset) const
Reads a 64bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:479
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:657
void clearBit(unsigned offset, unsigned bit)
Clears a specific bit at the given byte-offset.
Definition codeplug.cc:143
uint8_t getUInt6(const Offset::Bit &offset) const
Reads a 6bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:281
void setUInt64_be(unsigned offset, uint64_t value)
Stores a 64bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:499
void setUInt16_le(unsigned offset, uint16_t value)
Stores a 16bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:382
uint8_t getUInt5(const Offset::Bit &offset) const
Reads a 5bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:250
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:648
uint8_t getUInt3(const Offset::Bit &offset) const
Reads a 3bit unsigned integer at the given bit-offset.
Definition codeplug.cc:187
void setUInt24_le(unsigned offset, uint32_t value)
Stores a 24bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:425
uint8_t getUInt8(unsigned offset) const
Reads a 8bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:312
void setInt8(unsigned offset, int8_t value)
Reads a 8bit signed integer at the given byte- and bit-offset.
Definition codeplug.cc:339
void setUInt32_le(unsigned offset, uint32_t value)
Stores a 32bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:468
void setUInt64_le(unsigned offset, uint64_t value)
Stores a 64bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:509
void setUInt6(const Offset::Bit &offset, uint8_t value)
Stores a 6bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:285
virtual ~Element()
Destructor.
Definition codeplug.cc:68
uint16_t getUInt16_le(unsigned offset) const
Reads a 16bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:360
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:668
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:542
virtual bool isValid() const
Returns true if the pointer is not null.
Definition codeplug.cc:80
void setUInt5(const Offset::Bit &offset, uint8_t value)
Stores a 5bit unsigned integer at the given byte- and bit-offset.
Definition codeplug.cc:254
uint32_t getUInt32_be(unsigned offset) const
Reads a 32bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:438
bool fill(uint8_t value, unsigned offset=0, int size=-1)
Fills the memsets the entire element to the given value.
Definition codeplug.cc:90
Element & operator=(const Element &other)
Copy assignment.
Definition codeplug.cc:73
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:601
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:618
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:520
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:552
void setUInt24_be(unsigned offset, uint32_t value)
Stores a 24bit big-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:413
void setBit(const Offset::Bit &offset, bool value=true)
Sets a specific bit at the given byte-offset.
Definition codeplug.cc:120
bool getBit(const Offset::Bit &offset) const
Reads a specific bit at the given byte-offset.
Definition codeplug.cc:103
uint8_t getUInt4(const Offset::Bit &offset) const
Reads a 4bit unsigned integer at the given bit-offset.
Definition codeplug.cc:218
uint8_t getUInt2(const Offset::Bit &offset) const
Reads a 2bit unsigned integer at the given bit-offset.
Definition codeplug.cc:156
uint32_t getUInt32_le(unsigned offset) const
Reads a 32bit little-endian unsigned integer at the given byte-offset.
Definition codeplug.cc:448
size_t _size
Holds the size of the element.
Definition codeplug.hh:281
virtual void clear()
Abstract method to reset the element within the codeplug.
Definition codeplug.cc:85
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:677
void setUInt2(const Offset::Bit &offset, uint8_t value)
Stores a 2bit unsigned integer at the given bit-offset.
Definition codeplug.cc:160
Certain flags passed to CodePlug::encode to control the transfer and encoding of the codeplug.
Definition codeplug.hh:24
void setUpdateCodeplug(bool enable)
Sets if the codeplug gets updated.
Definition codeplug.cc:25
void setAutoEnableGPS(bool enable)
Sets if the GPS gets enabled automatically.
Definition codeplug.cc:36
bool autoEnableRoaming() const
If true enables automatic roaming when there is a roaming zone defined that is used by any channel.
Definition codeplug.cc:42
bool autoEnableGPS() const
If true enables GPS when there is a GPS or APRS system defined that is used by any channel.
Definition codeplug.cc:31
Flags()
Default constructor, enables code-plug update and disables automatic GPS/APRS and roaming.
Definition codeplug.cc:12
void setAutoEnableRoaming(bool enable)
Sets if roaming gets enabled automatically.
Definition codeplug.cc:47
bool updateCodeplug() const
If true, the codeplug will first be downloaded from the device, updated from the abstract config and ...
Definition codeplug.cc:20
Codeplug(QObject *parent=nullptr)
Hidden default constructor.
Definition codeplug.cc:778
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
Returns a post-processed configuration of the decoded config.
Definition codeplug.cc:794
virtual ~Codeplug()
Destructor.
Definition codeplug.cc:784
virtual Config * preprocess(Config *config, const ErrorStack &err=ErrorStack()) const
Returns a prepared configuration for this particular radio.
Definition codeplug.cc:789
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:35
The config class, representing the codeplug configuration.
Definition config.hh:70
DFUFile(QObject *parent=nullptr)
Constructs an empty DFU file object.
Definition dfufile.cc:45
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:43
A table holding all known satellites.
Definition satellitedatabase.hh:108
TransferFlags()
Default constructor.
Definition transferflags.cc:3
Holds a range of values [min, max].
Definition codeplug.hh:94
T limit(const T &value) const
Limits the value to the range.
Definition codeplug.hh:100
T mapTo(const Range< T > &other, const T &value) const
Maps a value from this range to the given range.
Definition codeplug.hh:108
bool in(const T &value) const
Checks if value is in range.
Definition codeplug.hh:104
const T max
Upper bound.
Definition codeplug.hh:98
const T min
Lower bound.
Definition codeplug.hh:96
Base class for Limits.
Definition codeplug.hh:92
Some type to specify a bit offset.
Definition codeplug.hh:70
const unsigned int byte
The byte offset.
Definition codeplug.hh:72
Bit operator+(unsigned int bits) const
Implements a simple increment.
Definition codeplug.hh:77
Bit operator-(unsigned int bits) const
Implements a simple increment.
Definition codeplug.hh:83
const unsigned int bit
The bit within the byte.
Definition codeplug.hh:74
Base class for Offsets.
Definition codeplug.hh:68