File: dm1701_codeplug.cc

package info (click to toggle)
qdmr 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,236 kB
  • sloc: cpp: 93,672; xml: 10,526; python: 1,108; makefile: 78; sh: 10
file content (929 lines) | stat: -rw-r--r-- 26,218 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#include "dm1701_codeplug.hh"
#include "logger.hh"
#include "config.hh"
#include <QTimeZone>


#define NUM_CHANNELS                3000
#define ADDR_CHANNELS           0x110000
#define CHANNEL_SIZE            0x000040

#define NUM_CONTACTS               10000
#define ADDR_CONTACTS           0x140000
#define CONTACT_SIZE            0x000024

#define NUM_ZONES                    250
#define ADDR_ZONES              0x0149e0
#define ZONE_SIZE               0x000040
#define ADDR_ZONEEXTS           0x031000
#define ZONEEXT_SIZE            0x0000e0

#define NUM_GROUPLISTS               250
#define ADDR_GROUPLISTS         0x00ec20
#define GROUPLIST_SIZE          0x000060

#define NUM_SCANLISTS                250
#define ADDR_SCANLISTS          0x018860
#define SCANLIST_SIZE           0x000068

#define ADDR_TIMESTAMP          0x002000
#define ADDR_SETTINGS           0x002040
#define SETTINGS_SIZE           0x0000b0
#define ADDR_BOOTSETTINGS       0x02f000
#define ADDR_MENUSETTINGS       0x0020f0
#define ADDR_BUTTONSETTINGS     0x002100
#define BUTTONSETTINGS_SIZE     0x000014
#define ADDR_PRIVACY_KEYS       0x0059c0

#define NUM_GPSSYSTEMS                16
#define ADDR_GPSSYSTEMS         0x03ec40
#define GPSSYSTEM_SIZE          0x000010

#define ADDR_EMERGENCY_SETTINGS 0x005a50
#define NUM_EMERGENCY_SYSTEMS         32
#define ADDR_EMERGENCY_SYSTEMS  0x005a60
#define EMERGENCY_SYSTEM_SIZE   0x000028

#define ADDR_VFO_CHANNEL_A      0x02ef00
#define ADDR_VFO_CHANNEL_B      0x02ef40



/* ********************************************************************************************* *
 * Implementation of DM1701Codeplug::ChannelElement
 * ********************************************************************************************* */
DM1701Codeplug::ChannelElement::ChannelElement(uint8_t *ptr, size_t size)
  : TyTCodeplug::ChannelElement::ChannelElement(ptr, size)
{
  // pass...
}

DM1701Codeplug::ChannelElement::ChannelElement(uint8_t *ptr)
  : TyTCodeplug::ChannelElement(ptr, CHANNEL_SIZE)
{
  // pass...
}

void
DM1701Codeplug::ChannelElement::clear() {
  TyTCodeplug::ChannelElement::clear();

  enableTightSquelch(false);
  enableReverseBurst(true);
  setPower(Channel::Power::High);
  setBit(0x0003, 6, true);
  setUInt8(0x0005, 0xc3);
  setUInt8(0x000f, 0xff);
}

bool
DM1701Codeplug::ChannelElement::tightSquelchEnabled() const {
  return !getBit(0x0000, 5);
}
void
DM1701Codeplug::ChannelElement::enableTightSquelch(bool enable) {
  setBit(0x0000, 5, !enable);
}

bool
DM1701Codeplug::ChannelElement::reverseBurst() const {
  return getBit(0x0004, 2);
}
void
DM1701Codeplug::ChannelElement::enableReverseBurst(bool enable) {
  setBit(0x0004, 2, enable);
}

Channel::Power
DM1701Codeplug::ChannelElement::power() const {
  if (getBit(0x0004, 5))
    return Channel::Power::High;
  return Channel::Power::Low;
}
void
DM1701Codeplug::ChannelElement::setPower(Channel::Power pwr) {
  switch (pwr) {
  case Channel::Power::Min:
  case Channel::Power::Low:
  case Channel::Power::Mid:
    setBit(0x0004, 5, false);
    break;
  case Channel::Power::High:
  case Channel::Power::Max:
    setBit(0x0004, 5, true);
  }
}

Channel *
DM1701Codeplug::ChannelElement::toChannelObj(const ErrorStack &err) const {
  Channel *ch = TyTCodeplug::ChannelElement::toChannelObj(err);
  if (nullptr == ch)
    return ch;

  ch->setPower(power());

  // Apply extension
  if (ch->tytChannelExtension()) {
    ch->tytChannelExtension()->enableTightSquelch(tightSquelchEnabled());
    ch->tytChannelExtension()->enableReverseBurst(reverseBurst());
  }

  return ch;
}

void
DM1701Codeplug::ChannelElement::fromChannelObj(const Channel *c, Context &ctx) {
  TyTCodeplug::ChannelElement::fromChannelObj(c, ctx);

  setPower(c->power());

  // apply extensions (extension will be created in TyTCodeplug::ChannelElement::fromChannelObj)
  if (TyTChannelExtension *ex = c->tytChannelExtension()) {
    enableTightSquelch(ex->tightSquelch());
    enableReverseBurst(ex->reverseBurst());
  }
}


/* ********************************************************************************************* *
 * Implementation of DM1701Codeplug::VFOChannelElement
 * ********************************************************************************************* */
DM1701Codeplug::VFOChannelElement::VFOChannelElement(uint8_t *ptr, size_t size)
  : ChannelElement(ptr, size)
{
  // pass...
}

DM1701Codeplug::VFOChannelElement::VFOChannelElement(uint8_t *ptr)
  : ChannelElement(ptr, CHANNEL_SIZE)
{
  // pass...
}

DM1701Codeplug::VFOChannelElement::~VFOChannelElement() {
  // pass...
}

QString
DM1701Codeplug::VFOChannelElement::name() const {
  return "";
}
void
DM1701Codeplug::VFOChannelElement::setName(const QString &txt) {
  Q_UNUSED(txt)
  // pass...
}

unsigned
DM1701Codeplug::VFOChannelElement::stepSize() const {
  return (getUInt8(32)+1)*2500;
}
void
DM1701Codeplug::VFOChannelElement::setStepSize(unsigned ss_Hz) {
  ss_Hz = std::min(50000U, std::max(ss_Hz, 2500U));
  setUInt8(32, ss_Hz/2500-1);
  setUInt8(33, 0xff);
}


/* ********************************************************************************************* *
 * Implementation of DM1701Codeplug::GeneralSettingsElement
 * ********************************************************************************************* */
DM1701Codeplug::GeneralSettingsElement::GeneralSettingsElement(uint8_t *ptr, size_t size)
  : TyTCodeplug::GeneralSettingsElement(ptr, size)
{
  // pass...
}

DM1701Codeplug::GeneralSettingsElement::GeneralSettingsElement(uint8_t *ptr)
  : TyTCodeplug::GeneralSettingsElement(ptr, SETTINGS_SIZE)
{
  // pass...
}

void
DM1701Codeplug::GeneralSettingsElement::clear() {
  TyTCodeplug::GeneralSettingsElement::clear();

  enableChannelModeA(true);
  enableChannelModeB(true);
  enableChannelMode(true);
  enableGroupCallMatch(true);
  enablePrivateCallMatch(true);
  setTimeZone(QTimeZone::systemTimeZone());
  setChannelHangTime(3000);

  memset(_data+0x91, 0xff, 0x1f);
}

bool
DM1701Codeplug::GeneralSettingsElement::channelModeA() const {
  return getBit(0x43,3);
}
void
DM1701Codeplug::GeneralSettingsElement::enableChannelModeA(bool enable) {
  setBit(0x43,3, enable);
}

bool
DM1701Codeplug::GeneralSettingsElement::channelModeB() const {
  return getBit(0x43,7);
}
void
DM1701Codeplug::GeneralSettingsElement::enableChannelModeB(bool enable) {
  setBit(0x43,7, enable);
}

bool
DM1701Codeplug::GeneralSettingsElement::channelMode() const {
  return 0xff == getUInt8(0x57);
}
void
DM1701Codeplug::GeneralSettingsElement::enableChannelMode(bool enable) {
  setUInt8(0x57, enable ? 0xff : 0x00);
}

bool
DM1701Codeplug::GeneralSettingsElement::groupCallMatch() const {
  return getBit(0x6b, 0);
}
void
DM1701Codeplug::GeneralSettingsElement::enableGroupCallMatch(bool enable) {
  setBit(0x6b, 0, enable);
}
bool
DM1701Codeplug::GeneralSettingsElement::privateCallMatch() const {
  return getBit(0x6b, 1);
}
void
DM1701Codeplug::GeneralSettingsElement::enablePrivateCallMatch(bool enable) {
  setBit(0x6b, 1, enable);
}

QTimeZone
DM1701Codeplug::GeneralSettingsElement::timeZone() const {
  return QTimeZone((int(getUInt5(0x6b, 3))-12)*3600);
}
void
DM1701Codeplug::GeneralSettingsElement::setTimeZone(const QTimeZone &zone) {
  int idx = (zone.standardTimeOffset(QDateTime::currentDateTime())/3600)+12;
  setUInt5(0x6b, 3, uint8_t(idx));
}

unsigned
DM1701Codeplug::GeneralSettingsElement::channelHangTime() const {
  return unsigned(getUInt8(0x90))*100;
}
void
DM1701Codeplug::GeneralSettingsElement::setChannelHangTime(unsigned dur) {
  setUInt8(0x90, dur/100);
}

bool
DM1701Codeplug::GeneralSettingsElement::fromConfig(const Config *config) {
  if (! TyTCodeplug::GeneralSettingsElement::fromConfig(config))
    return false;

  setTimeZone(QTimeZone::systemTimeZone());

  // apply extension
  if (TyTSettingsExtension *ex  = config->settings()->tytExtension()) {
    enableChannelMode(ex->channelMode());
    enableChannelModeA(ex->channelModeA());
    enableChannelModeB(ex->channelModeB());
    enableGroupCallMatch(ex->groupCallMatch());
    enablePrivateCallMatch(ex->privateCallMatch());
    setChannelHangTime(ex->channelHangTime());
  }

  return true;
}

bool
DM1701Codeplug::GeneralSettingsElement::updateConfig(Config *config) {
  if (! TyTCodeplug::GeneralSettingsElement::updateConfig(config))
    return false;

  // Update extension if set.
  if (TyTSettingsExtension *ex = config->settings()->tytExtension()) {
    ex->enableChannelMode(channelMode());
    ex->enableChannelModeA(channelModeA());
    ex->enableChannelModeB(channelModeB());
    ex->enableGroupCallMatch(groupCallMatch());
    ex->enablePrivateCallMatch(privateCallMatch());
    ex->setChannelHangTime(channelHangTime());
  }

  return true;
}

/* ********************************************************************************************* *
 * Implementation of DM1701Codeplug::ButtonSettingsElement
 * ********************************************************************************************* */
DM1701Codeplug::ButtonSettingsElement::ButtonSettingsElement(uint8_t *ptr, size_t size)
  : TyTCodeplug::ButtonSettingsElement(ptr, size)
{
  // pass...
}

DM1701Codeplug::ButtonSettingsElement::ButtonSettingsElement(uint8_t *ptr)
  : TyTCodeplug::ButtonSettingsElement(ptr, BUTTONSETTINGS_SIZE)
{
  // pass...
}

void
DM1701Codeplug::ButtonSettingsElement::clear() {
  setSideButton3Short(ButtonAction::Disabled);
  setSideButton3Long(ButtonAction::Disabled);
  setProgButton1Short(ButtonAction::Disabled);
  setProgButton1Long(ButtonAction::Disabled);
  setProgButton2Short(ButtonAction::Disabled);
  setProgButton2Long(ButtonAction::Disabled);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::sideButton3Short() const {
  return ButtonAction(getUInt8(0x06));
}
void
DM1701Codeplug::ButtonSettingsElement::setSideButton3Short(ButtonAction action) {
  setUInt8(0x06, action);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::sideButton3Long() const {
  return ButtonAction(getUInt8(0x07));
}
void
DM1701Codeplug::ButtonSettingsElement::setSideButton3Long(ButtonAction action) {
  setUInt8(0x07, action);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::progButton1Short() const {
  return ButtonAction(getUInt8(0x08));
}
void
DM1701Codeplug::ButtonSettingsElement::setProgButton1Short(ButtonAction action) {
  setUInt8(0x08, action);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::progButton1Long() const {
  return ButtonAction(getUInt8(0x09));
}
void
DM1701Codeplug::ButtonSettingsElement::setProgButton1Long(ButtonAction action) {
  setUInt8(0x09, action);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::progButton2Short() const {
  return ButtonAction(getUInt8(0x0a));
}
void
DM1701Codeplug::ButtonSettingsElement::setProgButton2Short(ButtonAction action) {
  setUInt8(0x0a, action);
}

TyTCodeplug::ButtonSettingsElement::ButtonAction
DM1701Codeplug::ButtonSettingsElement::progButton2Long() const {
  return ButtonAction(getUInt8(0x0b));
}
void
DM1701Codeplug::ButtonSettingsElement::setProgButton2Long(ButtonAction action) {
  setUInt8(0x0b, action);
}

bool
DM1701Codeplug::ButtonSettingsElement::fromConfig(const Config *config) {
  if (! TyTCodeplug::ButtonSettingsElement::fromConfig(config))
    return false;

  if (config->tytExtension()) {
    TyTButtonSettings *ex = config->tytExtension()->buttonSettings();
    setSideButton3Short(ex->sideButton3Short());
    setSideButton3Long(ex->sideButton3Long());
    setProgButton1Short(ex->progButton1Short());
    setProgButton1Long(ex->progButton1Long());
    setProgButton2Short(ex->progButton2Short());
    setProgButton2Long(ex->progButton2Long());
  }

  return true;
}

bool
DM1701Codeplug::ButtonSettingsElement::updateConfig(Config *config) {
  if (! TyTCodeplug::ButtonSettingsElement::updateConfig(config))
    return false;

  if (config->tytExtension()) {
    TyTButtonSettings *ex = config->tytExtension()->buttonSettings();
    ex->setSideButton3Short(sideButton3Short());
    ex->setSideButton3Long(sideButton3Long());
    ex->setProgButton1Short(progButton1Short());
    ex->setProgButton1Long(progButton1Long());
    ex->setProgButton2Short(progButton2Short());
    ex->setProgButton2Long(progButton2Long());
  }

  return true;
}


/* ******************************************************************************************** *
 * Implementation of UV390Codeplug::ZoneElement
 * ******************************************************************************************** */
DM1701Codeplug::ZoneExtElement::ZoneExtElement(uint8_t *ptr, size_t size)
  : Codeplug::Element(ptr, size)
{
  // pass...
}

DM1701Codeplug::ZoneExtElement::ZoneExtElement(uint8_t *ptr)
  : Codeplug::Element(ptr, ZONEEXT_SIZE)
{
  // pass...
}

DM1701Codeplug::ZoneExtElement::~ZoneExtElement() {
  // pass...
}

void
DM1701Codeplug::ZoneExtElement::clear() {
  memset(_data, 0x00, 0xe0);
}

uint16_t
DM1701Codeplug::ZoneExtElement::memberIndexA(unsigned n) const {
  return getUInt16_le(0x00 + 2*n);
}

void
DM1701Codeplug::ZoneExtElement::setMemberIndexA(unsigned n, uint16_t idx) {
  setUInt16_le(0x00 + 2*n, idx);
}

uint16_t
DM1701Codeplug::ZoneExtElement::memberIndexB(unsigned n) const {
  return getUInt16_le(0x60 + 2*n);
}

void
DM1701Codeplug::ZoneExtElement::setMemberIndexB(unsigned n, uint16_t idx) {
  setUInt16_le(0x60 + 2*n, idx);
}

bool
DM1701Codeplug::ZoneExtElement::fromZoneObj(const Zone *zone, Context &ctx) {
  // Store remaining channels from list A
  for (int i=16; i<64; i++) {
    if (i < zone->A()->count())
      setMemberIndexA(i-16, ctx.index(zone->A()->get(i)));
    else
      setMemberIndexA(i-16, 0);
  }
  // Store channel from list B
  for (int i=0; i<64; i++) {
    if (i < zone->B()->count())
      setMemberIndexB(i, ctx.index(zone->B()->get(i)));
    else
      setMemberIndexB(i, 0);
  }

  return true;
}

bool
DM1701Codeplug::ZoneExtElement::linkZoneObj(Zone *zone, Context &ctx) {
  for (int i=0; (i<48) && memberIndexA(i); i++) {
    if (! ctx.has<Channel>(memberIndexA(i))) {
      logError() << "Cannot link zone extension: Channel index " << memberIndexA(i) << " not defined.";
      return false;
    }
    zone->A()->add(ctx.get<Channel>(memberIndexA(i)));
  }

  for (int i=0; (i<64) && memberIndexB(i); i++) {
    if (! ctx.has<Channel>(memberIndexB(i))) {
      logWarn() << "Cannot link zone extension: Channel index " << memberIndexB(i) << " not defined.";
      return false;
    }
    zone->B()->add(ctx.get<Channel>(memberIndexB(i)));
  }

  return true;
}


/* ********************************************************************************************* *
 * Implementation of DM1701Codeplug
 * ********************************************************************************************* */
DM1701Codeplug::DM1701Codeplug(QObject *parent)
  : TyTCodeplug(parent)
{
  addImage("Baofeng DM-1701 Codeplug");
  image(0).addElement(0x002000, 0x3e000);
  image(0).addElement(0x110000, 0x90000);

  // Clear entire codeplug
  clear();
}

DM1701Codeplug::~DM1701Codeplug() {
  // pass...
}

void
DM1701Codeplug::clearTimestamp() {
  TimestampElement(data(ADDR_TIMESTAMP)).clear();
}

bool
DM1701Codeplug::encodeTimestamp() {
  TimestampElement ts(data(ADDR_TIMESTAMP));
  ts.setTimestamp(QDateTime::currentDateTime());
  return true;
}

void
DM1701Codeplug::clearGeneralSettings() {
  GeneralSettingsElement(data(ADDR_SETTINGS)).clear();
}

bool
DM1701Codeplug::encodeGeneralSettings(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(ctx); Q_UNUSED(err)
  return GeneralSettingsElement(data(ADDR_SETTINGS)).fromConfig(config);
}

bool
DM1701Codeplug::decodeGeneralSettings(Config *config, const ErrorStack &err) {
  Q_UNUSED(err)
  return GeneralSettingsElement(data(ADDR_SETTINGS)).updateConfig(config);
}

void
DM1701Codeplug::clearChannels() {
  // Clear channels
  for (int i=0; i<NUM_CHANNELS; i++)
    ChannelElement(data(ADDR_CHANNELS+i*CHANNEL_SIZE)).clear();
}

bool
DM1701Codeplug::encodeChannels(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(err)
  // Define Channels
  for (int i=0; i<NUM_CHANNELS; i++) {
    ChannelElement chan(data(ADDR_CHANNELS+i*CHANNEL_SIZE));
    if (i < config->channelList()->count()) {
      chan.fromChannelObj(config->channelList()->channel(i), ctx);
    } else {
      chan.clear();
    }
  }
  return true;
}

bool
DM1701Codeplug::createChannels(Config *config, Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_CHANNELS; i++) {
    ChannelElement chan(data(ADDR_CHANNELS+i*CHANNEL_SIZE));
    if (! chan.isValid())
      continue;
    if (Channel *obj = chan.toChannelObj()) {
      config->channelList()->add(obj); ctx.add(obj, i+1);
    } else {
      errMsg(err) << "Invalid channel at index %" << i << ".";
      return false;
    }
  }
  return true;
}

bool
DM1701Codeplug::linkChannels(Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_CHANNELS; i++) {
    ChannelElement chan(data(ADDR_CHANNELS+i*CHANNEL_SIZE));
    if (! chan.isValid())
      continue;
    if (! chan.linkChannelObj(ctx.get<Channel>(i+1), ctx)) {
      errMsg(err) << "Cannot link channel at index " << i << ".";
      return false;
    }
  }
  return true;
}

void
DM1701Codeplug::clearContacts() {
  // Clear contacts
  for (int i=0; i<NUM_CONTACTS; i++)
    ContactElement(data(ADDR_CONTACTS+i*CONTACT_SIZE)).clear();
}

bool
DM1701Codeplug::encodeContacts(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(ctx); Q_UNUSED(err)
  // Encode contacts
  for (int i=0; i<NUM_CONTACTS; i++) {
    ContactElement cont(data(ADDR_CONTACTS+i*CONTACT_SIZE));
    if (i < config->contacts()->digitalCount())
      cont.fromContactObj(config->contacts()->digitalContact(i));
    else
      cont.clear();
  }
  return true;
}

bool
DM1701Codeplug::createContacts(Config *config, Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_CONTACTS; i++) {
    ContactElement cont(data(ADDR_CONTACTS+i*CONTACT_SIZE));
    if (! cont.isValid())
      continue;
    if (DMRContact *obj = cont.toContactObj()) {
      config->contacts()->add(obj); ctx.add(obj, i+1);
    } else {
      errMsg(err) << "Invalid contact at index " << i << ".";
      return false;
    }
  }
  return true;
}

void
DM1701Codeplug::clearZones() {
  // Clear zones & zone extensions
  for (int i=0; i<NUM_ZONES; i++) {
    ZoneElement(data(ADDR_ZONES+i*ZONE_SIZE)).clear();
    ZoneExtElement(data(ADDR_ZONEEXTS+i*ZONEEXT_SIZE)).clear();
  }
}

bool
DM1701Codeplug::encodeZones(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(err)
  for (int i=0; i<NUM_ZONES; i++) {
    ZoneElement zone(data(ADDR_ZONES + i*ZONE_SIZE));
    ZoneExtElement ext(data(ADDR_ZONEEXTS + i*ZONEEXT_SIZE));
    zone.clear();
    ext.clear();
    if (i < config->zones()->count()) {
      zone.fromZoneObj(config->zones()->zone(i), ctx);
      if (config->zones()->zone(i)->B()->count() || (16 < config->zones()->zone(i)->A()->count()))
        ext.fromZoneObj(config->zones()->zone(i), ctx);
    }
  }
  return true;
}

bool
DM1701Codeplug::createZones(Config *config, Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_ZONES; i++) {
    ZoneElement zone(data(ADDR_ZONES+i*ZONE_SIZE));
    if (! zone.isValid())
      continue;
    if (Zone *obj = zone.toZoneObj()) {
      config->zones()->add(obj); ctx.add(obj, i+1);
    } else {
      errMsg(err) << "Invalid zone at index " << i << ".";
      return false;
    }
  }

  return true;
}

bool
DM1701Codeplug::linkZones(Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_ZONES; i++) {
    ZoneElement zone(data(ADDR_ZONES+i*ZONE_SIZE));
    if (! zone.isValid())
      continue;
    if (! zone.linkZone(ctx.get<Zone>(i+1), ctx)) {
      errMsg(err) << "Cannot link zone at index " << i << ".";
      return false;
    }
    ZoneExtElement zoneext(data(ADDR_ZONEEXTS + i*ZONEEXT_SIZE));
    if (! zoneext.linkZoneObj(ctx.get<Zone>(i+1), ctx)) {
      errMsg(err) << "Cannot link zone extension at index " << i << ".";
      return false;
    }
  }

  return true;
}

void
DM1701Codeplug::clearGroupLists() {
  for (int i=0; i<NUM_GROUPLISTS; i++)
    GroupListElement(data(ADDR_GROUPLISTS+i*GROUPLIST_SIZE)).clear();
}

bool
DM1701Codeplug::encodeGroupLists(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(err)
  for (int i=0; i<NUM_GROUPLISTS; i++) {
    GroupListElement glist(data(ADDR_GROUPLISTS+i*GROUPLIST_SIZE));
    if (i < config->rxGroupLists()->count())
      glist.fromGroupListObj(config->rxGroupLists()->list(i), ctx);
    else
      glist.clear();
  }
  return true;
}

bool
DM1701Codeplug::createGroupLists(Config *config, Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_GROUPLISTS; i++) {
    GroupListElement glist(data(ADDR_GROUPLISTS+i*GROUPLIST_SIZE));
    if (! glist.isValid())
      continue;
    if (RXGroupList *obj = glist.toGroupListObj(ctx)) {
      config->rxGroupLists()->add(obj); ctx.add(obj, i+1);
    } else {
      errMsg(err) << "Invalid group list at index " << i << ".";
      return false;
    }
  }
  return true;
}

bool
DM1701Codeplug::linkGroupLists(Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_GROUPLISTS; i++) {
    GroupListElement glist(data(ADDR_GROUPLISTS+i*GROUPLIST_SIZE));
    if (! glist.isValid())
      continue;
    if (! glist.linkGroupListObj(ctx.get<RXGroupList>(i+1), ctx)) {
      errMsg(err) << "Cannot link group list at index " << i << ".";
      return false;
    }
  }
  return true;
}

void
DM1701Codeplug::clearScanLists() {
  // Clear scan lists
  for (int i=0; i<NUM_SCANLISTS; i++)
    ScanListElement(data(ADDR_SCANLISTS + i*SCANLIST_SIZE)).clear();
}

bool
DM1701Codeplug::encodeScanLists(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(err)
  // Define Scanlists
  for (int i=0; i<NUM_SCANLISTS; i++) {
    ScanListElement scan(data(ADDR_SCANLISTS + i*SCANLIST_SIZE));
    if (i < config->scanlists()->count())
      scan.fromScanListObj(config->scanlists()->scanlist(i), ctx);
    else
      scan.clear();
  }
  return true;
}

bool
DM1701Codeplug::createScanLists(Config *config, Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_SCANLISTS; i++) {
    ScanListElement scan(data(ADDR_SCANLISTS + i*SCANLIST_SIZE));
    if (! scan.isValid())
      continue;
    if (ScanList *obj = scan.toScanListObj(ctx)) {
      config->scanlists()->add(obj); ctx.add(obj, i+1);
    } else {
      errMsg(err) << "Invalid scanlist at index " << i << ".";
      return false;
    }
  }
  return true;
}

bool
DM1701Codeplug::linkScanLists(Context &ctx, const ErrorStack &err) {
  for (int i=0; i<NUM_SCANLISTS; i++) {
    ScanListElement scan(data(ADDR_SCANLISTS + i*SCANLIST_SIZE));
    if (! scan.isValid())
      continue;
    if (! scan.linkScanListObj(ctx.get<ScanList>(i+1), ctx)) {
      errMsg(err) << "Cannot link scan list at index" << i << ".";
      return false;
    }
  }

  return true;
}

void
DM1701Codeplug::clearMenuSettings() {
  MenuSettingsElement(data(ADDR_MENUSETTINGS)).clear();
}

void
DM1701Codeplug::clearButtonSettings() {
  ButtonSettingsElement(data(ADDR_BUTTONSETTINGS)).clear();
}

bool
DM1701Codeplug::encodeButtonSettings(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(ctx); Q_UNUSED(err)
  // Encode settings
  return ButtonSettingsElement(data(ADDR_BUTTONSETTINGS)).fromConfig(config);
}

bool
DM1701Codeplug::decodeButtonSetttings(Config *config, const ErrorStack &err) {
  Q_UNUSED(err)
  return ButtonSettingsElement(data(ADDR_BUTTONSETTINGS)).updateConfig(config);
}


void
DM1701Codeplug::clearPrivacyKeys() {
  EncryptionElement(data(ADDR_PRIVACY_KEYS)).clear();
}

bool
DM1701Codeplug::encodePrivacyKeys(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(flags); Q_UNUSED(err);
  // First, reset keys
  clearPrivacyKeys();
  // Get keys
  EncryptionElement keys(data(ADDR_PRIVACY_KEYS));
  return keys.fromCommercialExt(config->commercialExtension(), ctx);
}

bool
DM1701Codeplug::decodePrivacyKeys(Config *config, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(config)
  // Get keys
  EncryptionElement keys(data(ADDR_PRIVACY_KEYS));
  // Decode element
  if (! keys.updateCommercialExt(ctx)) {
    errMsg(err) << "Cannot create encryption extension.";
    return false;
  }
  return true;
}


void
DM1701Codeplug::clearTextMessages() {
  MessageBankElement(data(Offset::messages())).clear();
}

bool
DM1701Codeplug::encodeTextMessages(Context &ctx, const Flags &flags, const ErrorStack &err) {
  return MessageBankElement(data(Offset::messages())).encode(ctx, flags, err);
}

bool
DM1701Codeplug::decodeTextMessages(Context &ctx, const ErrorStack &err) {
  return MessageBankElement(data(Offset::messages())).decode(ctx, err);
}


void
DM1701Codeplug::clearEmergencySystems() {
  EmergencySettingsElement(data(ADDR_EMERGENCY_SETTINGS)).clear();
  for (int i=0; i<NUM_EMERGENCY_SYSTEMS; i++)
    EmergencySystemElement(data(ADDR_EMERGENCY_SYSTEMS + i*EMERGENCY_SYSTEM_SIZE)).clear();
}

void
DM1701Codeplug::clearVFOSettings() {
  VFOChannelElement(data(ADDR_VFO_CHANNEL_A)).clear();
  VFOChannelElement(data(ADDR_VFO_CHANNEL_B)).clear();
}


void
DM1701Codeplug::clearPositioningSystems() {
  // pass, nothing to do.
}

bool
DM1701Codeplug::encodePositioningSystems(Config *config, const Flags &flags, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(config); Q_UNUSED(flags); Q_UNUSED(ctx); Q_UNUSED(err)
  // Pass, nothing to do
  return true;
}

bool
DM1701Codeplug::createPositioningSystems(Config *config, Context &ctx, const ErrorStack &err) {
  Q_UNUSED(config); Q_UNUSED(ctx); Q_UNUSED(err)
  // Pass, nothing to do
  return true;
}

bool
DM1701Codeplug::linkPositioningSystems(Context &ctx, const ErrorStack &err) {
  Q_UNUSED(ctx); Q_UNUSED(err);
  // Nothing to do
  return true;
}