File: gpssystem.cc

package info (click to toggle)
qdmr 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,424 kB
  • sloc: cpp: 96,043; xml: 10,749; python: 1,108; makefile: 78; sh: 9
file content (641 lines) | stat: -rw-r--r-- 16,271 bytes parent folder | download
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
#include "gpssystem.hh"
#include "contact.hh"
#include "channel.hh"
#include "logger.hh"
#include "utils.hh"
#include <QRegularExpressionMatch>


/* ********************************************************************************************* *
 * Implementation of PositioningSystem
 * ********************************************************************************************* */
PositioningSystem::PositioningSystem(QObject *parent)
  : ConfigObject(parent), _period(0)
{
  // pass...
}

PositioningSystem::PositioningSystem(const QString &name, unsigned period, QObject *parent)
  : ConfigObject(name, parent), _period(period)
{
  // pass...
}

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

unsigned
PositioningSystem::period() const {
  return _period;
}

void
PositioningSystem::setPeriod(unsigned period) {
  _period = period;
  emit modified(this);
}

bool
PositioningSystem::populate(YAML::Node &node, const ConfigItem::Context &context, const ErrorStack &err) {
  if (! ConfigObject::populate(node, context, err))
    return false;
  return true;
}

bool
PositioningSystem::parse(const YAML::Node &node, Context &ctx, const ErrorStack &err) {
  if (! node)
    return false;

  if ((! node.IsMap()) || (1 != node.size())) {
    errMsg(err) << node.Mark().line << ":" << node.Mark().column
                << ": Cannot parse positioning system: Expected object with one child.";
    return false;
  }

  YAML::Node pos = node.begin()->second;
  if (pos && (!pos["period"])) {
    logWarn() << pos.Mark().line << ":" << pos.Mark().column
              << ": Positioning system has no period.";
  }

  return ConfigObject::parse(pos, ctx, err);
}

bool
PositioningSystem::link(const YAML::Node &node, const Context &ctx, const ErrorStack &err) {
  return ConfigObject::link(node.begin()->second, ctx, err);
}

void
PositioningSystem::onReferenceModified() {
  emit modified(this);
}


/* ********************************************************************************************* *
 * Implementation of GPSSystem
 * ********************************************************************************************* */
GPSSystem::GPSSystem(QObject *parent)
  : PositioningSystem(parent), _contact(), _revertChannel()
{
  // Allow revert channel to take a reference to the SelectedChannel singleton
  _revertChannel.allow(SelectedChannel::get()->metaObject());
  // Register '!selected' tag for revert channel
  Context::setTag(staticMetaObject.className(), "revert", "!selected", SelectedChannel::get());
  // By default, selected channel is revert channel
  resetRevertChannel();

  // Connect signals
  connect(&_contact, SIGNAL(modified()), this, SLOT(onReferenceModified()));
  connect(&_revertChannel, SIGNAL(modified()), this, SLOT(onReferenceModified()));
}

GPSSystem::GPSSystem(const QString &name, DMRContact *contact,
                     DMRChannel *revertChannel, unsigned period,
                     QObject *parent)
  : PositioningSystem(name, period, parent), _contact(), _revertChannel()
{
  // Allow revert channel to take a reference to the SelectedChannel singleton
  _revertChannel.allow(SelectedChannel::get()->metaObject());
  // Register '!selected' tag for revert channel
  Context::setTag(staticMetaObject.className(), "revert", "!selected", SelectedChannel::get());

  // Set references.
  _contact.set(contact);
  setRevertChannel(revertChannel);

  // Connect signals
  connect(&_contact, SIGNAL(modified()), this, SLOT(onReferenceModified()));
  connect(&_revertChannel, SIGNAL(modified()), this, SLOT(onReferenceModified()));
}

ConfigItem *
GPSSystem::clone() const {
  GPSSystem *sys = new GPSSystem();
  if (! sys->copy(*this)) {
    sys->deleteLater();
    return nullptr;
  }
  return sys;
}

bool
GPSSystem::hasContact() const {
  return ! _contact.isNull();
}

DMRContact *
GPSSystem::contactObj() const {
  return _contact.as<DMRContact>();
}

void
GPSSystem::setContactObj(DMRContact *contact) {
  _contact.set(contact);
}

void
GPSSystem::setContact(DMRContactReference *contact) {
  _contact.copy(contact);
}

const DMRContactReference *
GPSSystem::contact() const {
  return &_contact;
}

DMRContactReference *
GPSSystem::contact() {
  return &_contact;
}


bool
GPSSystem::hasRevertChannel() const {
  return _revertChannel.is<DMRChannel>();
}

DMRChannel *
GPSSystem::revertChannel() const {
  return _revertChannel.as<DMRChannel>();
}

void
GPSSystem::setRevertChannel(DMRChannel *channel) {
  if (nullptr == channel)
    resetRevertChannel();
  else
    _revertChannel.set(channel);
}

void
GPSSystem::resetRevertChannel() {
  _revertChannel.set(SelectedChannel::get());
}


const DMRChannelReference*
GPSSystem::revert() const {
  return &_revertChannel;
}

DMRChannelReference*
GPSSystem::revert() {
  return &_revertChannel;
}

YAML::Node
GPSSystem::serialize(const Context &context, const ErrorStack &err) {
  YAML::Node node = PositioningSystem::serialize(context, err);
  if (node.IsNull())
    return node;
  YAML::Node type; type["dmr"] = node;
  return type;
}



/* ********************************************************************************************* *
 * Implementation of APRSSystem
 * ********************************************************************************************* */
APRSSystem::APRSSystem(QObject *parent)
  : PositioningSystem(parent), _channel(), _destination(), _destSSID(0),
    _source(), _srcSSID(0), _path(), _icon(Icon::None), _message(),
    _anytone(nullptr), _openGD77(nullptr)
{
  // Allow revert channel to take a reference to the SelectedChannel singleton
  _channel.allow(SelectedChannel::get()->metaObject());
  // Register '!selected' tag for revert channel
  Context::setTag(staticMetaObject.className(), "revert", "!selected", SelectedChannel::get());
  // By default, selected channel is revert channel
  resetRevertChannel();

  // Connect to channel reference
  connect(&_channel, SIGNAL(modified()), this, SLOT(onReferenceModified()));
}

APRSSystem::APRSSystem(const QString &name, FMChannel *channel, const QString &dest, unsigned destSSID,
                       const QString &src, unsigned srcSSID, const QString &path, Icon icon, const QString &message,
                       unsigned period, QObject *parent)
  : PositioningSystem(name, period, parent), _channel(), _destination(dest), _destSSID(destSSID),
    _source(src), _srcSSID(srcSSID), _path(path), _icon(icon), _message(message),
    _anytone(nullptr), _openGD77(nullptr)
{
  // Allow revert channel to take a reference to the SelectedChannel singleton
  _channel.allow(SelectedChannel::get()->metaObject());
  // Register '!selected' tag for revert channel
  Context::setTag(staticMetaObject.className(), "revert", "!selected", SelectedChannel::get());

  // Set revert channel
  setRevertChannel(channel);

  // Connect to channel reference
  connect(&_channel, SIGNAL(modified()), this, SLOT(onReferenceModified()));
}

bool
APRSSystem::copy(const ConfigItem &other) {
  const APRSSystem *sys = other.as<APRSSystem>();
  if ((nullptr == sys) || (! PositioningSystem::copy(other)))
    return false;
  _destination = sys->destination();
  _destSSID = sys->_destSSID;
  _source = sys->_source;
  _srcSSID = sys->_srcSSID;
  _path = sys->_path;
  return true;
}

ConfigItem *
APRSSystem::clone() const {
  APRSSystem *sys = new APRSSystem();
  if (! sys->copy(*this)) {
    sys->deleteLater();
    return nullptr;
  }
  return sys;
}


bool
APRSSystem::hasRevertChannel() const {
  return _channel.is<FMChannel>();
}

FMChannel *
APRSSystem::revertChannel() const {
  return _channel.as<FMChannel>();
}

void
APRSSystem::setRevertChannel(FMChannel *channel) {
  if (nullptr == channel)
    resetRevertChannel();
  else
    _channel.set(channel);
}

void
APRSSystem::resetRevertChannel() {
  _channel.set(SelectedChannel::get());
}


const FMChannelReference *
APRSSystem::revert() const {
  return &_channel;
}

FMChannelReference *
APRSSystem::revert() {
  return &_channel;
}


const QString &
APRSSystem::destination() const {
  return _destination;
}

unsigned
APRSSystem::destSSID() const {
  return _destSSID;
}

void
APRSSystem::setDestination(const QString &call, unsigned ssid) {
  _destination = call;
  _destSSID = ssid;
}

void
APRSSystem::setDestination(const QString &call) {
  _destination = call;
}

void
APRSSystem::setDestSSID(unsigned int ssid) {
  _destSSID = ssid;
}


const QString &
APRSSystem::source() const {
  return _source;
}

unsigned
APRSSystem::srcSSID() const {
  return _srcSSID;
}

void
APRSSystem::setSource(const QString &call, unsigned ssid) {
  _source = call;
  _srcSSID = ssid;
}

void
APRSSystem::setSource(const QString &call) {
  _source = call;
}

void
APRSSystem::setSrcSSID(unsigned ssid) {
  _srcSSID = ssid;
}


const QString &
APRSSystem::path() const {
  return _path;
}
void
APRSSystem::setPath(const QString &path) {
  _path = path.toUpper();
  _path.replace(" ","");
}

APRSSystem::Icon
APRSSystem::icon() const {
  return _icon;
}
void
APRSSystem::setIcon(Icon icon) {
  _icon = icon;
}

const QString &
APRSSystem::message() const {
  return _message;
}
void
APRSSystem::setMessage(const QString &msg) {
  _message = msg;
  emit modified(this);
}

AnytoneFMAPRSSettingsExtension *
APRSSystem::anytoneExtension() const {
  return _anytone;
}
void
APRSSystem::setAnytoneExtension(AnytoneFMAPRSSettingsExtension *ext) {
  if (_anytone) {
    _anytone->deleteLater();
    _anytone = nullptr;
  }
  if (ext) {
    _anytone = ext;
    ext->setParent(this);
    connect(ext, SIGNAL(modified(ConfigItem *)), this, SIGNAL(modified(ConfigItem *)));
  }
}

OpenGD77APRSSystemExtension *
APRSSystem::openGD77Extension() const {
  return _openGD77;
}
void
APRSSystem::setOpenGD77Extension(OpenGD77APRSSystemExtension *ext) {
  if (_openGD77) {
    _openGD77->deleteLater();
    _openGD77 = nullptr;
  }
  if (ext) {
    _openGD77 = ext;
    ext->setParent(this);
    connect(ext, SIGNAL(modified(ConfigItem *)), this, SIGNAL(modified(ConfigItem *)));
  }
}

YAML::Node
APRSSystem::serialize(const Context &context, const ErrorStack &err) {
  YAML::Node node = PositioningSystem::serialize(context, err);
  if (node.IsNull())
    return node;
  YAML::Node type; type["aprs"] = node;
  return type;
}

bool
APRSSystem::populate(YAML::Node &node, const Context &context, const ErrorStack &err) {
  if (! PositioningSystem::populate(node, context, err))
    return false;

  node["destination"] = QString("%1-%2").arg(_destination).arg(_destSSID).toStdString();
  node["source"] = QString("%1-%2").arg(_source).arg(_srcSSID).toStdString();

  QStringList path;
  QRegularExpression pattern("([A-Za-z0-9]+-[0-9]+)");
  int idx = 0;
  auto match = pattern.match(_path, idx);
  while (match.hasMatch()) {
    path.append(match.captured(1));
    idx += match.capturedLength(1);
    match = pattern.match(_path, idx);
  }

  if (path.count()) {
    YAML::Node list = YAML::Node(YAML::NodeType::Sequence);
    list.SetStyle(YAML::EmitterStyle::Flow);
    foreach (QString call, path) {
      list.push_back(call.toStdString());
    }
    node["path"] = list;
  }

  return true;
}


bool
APRSSystem::parse(const YAML::Node &node, Context &ctx, const ErrorStack &err) {
  if (! node)
    return false;

  if ((! node.IsMap()) || (1 != node.size())) {
    errMsg(err) << node.Mark().line << ":" << node.Mark().column
                << ": Cannot parse APRS system: Expected object with one child.";
    return false;
  }

  YAML::Node sys = node.begin()->second;
  if (sys["source"] && sys["source"].IsScalar()) {
    QString source = QString::fromStdString(sys["source"].as<std::string>());
    QRegularExpression pattern("^([A-Z0-9]+)-(1?[0-9])$");
    auto match = pattern.match(source);
    if (! match.hasMatch()) {
      errMsg(err) << sys.Mark().line << ":" << sys.Mark().column
                  << ": Cannot parse APRS system: '" << source << "' not a valid source call and SSID.";
      return false;
    }
    setSource(match.captured(1), match.captured(2).toUInt());
  } else {
    errMsg(err) << sys.Mark().line << ":" << sys.Mark().column
                << ": Cannot parse APRS system: No source call+SSID specified.";
    return false;
  }

  if (sys["destination"] && sys["destination"].IsScalar()) {
    QString dest = QString::fromStdString(sys["destination"].as<std::string>());
    QRegularExpression pattern("^([A-Z0-9]+)-(1?[0-9])$");
    auto match = pattern.match(dest);
    if (! match.hasMatch()) {
      errMsg(err) << sys.Mark().line << ":" << sys.Mark().column
                  << ": Cannot parse APRS system: '" << dest << "' not a valid destination call and SSID.";
      return false;
    }
    setDestination(match.captured(1), match.captured(2).toUInt());
  } else {
    errMsg(err) << sys.Mark().line << ":" << sys.Mark().column
                << ": Cannot parse APRS system: No destination call+SSID specified.";
    return false;
  }

  if (sys["path"] && sys["path"].IsSequence()) {
    QStringList path;
    for (YAML::const_iterator it=sys["path"].begin(); it!=sys["path"].end(); it++) {
      if (it->IsScalar())
        path.append(QString::fromStdString(it->as<std::string>()));
    }
    setPath(path.join(","));
  }

  return PositioningSystem::parse(node, ctx, err);
}


/* ********************************************************************************************* *
 * Implementation of GPSSystems table
 * ********************************************************************************************* */
PositioningSystems::PositioningSystems(QObject *parent)
  : ConfigObjectList(PositioningSystem::staticMetaObject, parent)
{
  // pass...
}

PositioningSystem *
PositioningSystems::system(int idx) const {
  if (ConfigItem *obj = get(idx))
    return obj->as<PositioningSystem>();
  return nullptr;
}

int
PositioningSystems::add(ConfigObject *obj, int row, bool unique) {
  if (obj && obj->is<PositioningSystem>())
    return ConfigObjectList::add(obj, row, unique);
  return -1;
}

int
PositioningSystems::gpsCount() const {
  int c=0;
  for (int i=0; i<_items.size(); i++)
    if (_items.at(i)->is<GPSSystem>())
      c++;
  return c;
}

int
PositioningSystems::indexOfGPSSys(const GPSSystem *gps) const {
  if (! _items.contains((GPSSystem *)gps))
    return -1;

  int idx=0;
  for (int i=0; i<count(); i++) {
    if (gps == _items.at(i))
      return idx;
    if (_items.at(i)->is<GPSSystem>())
      idx++;
  }

  return -1;
}

GPSSystem *
PositioningSystems::gpsSystem(int idx) const {
  if ((0>idx) || (idx >= _items.size()))
    return nullptr;
  for (int i=0; i<_items.size(); i++) {
    if (_items.at(i)->is<GPSSystem>()) {
      if (0==idx)
        return _items.at(i)->as<GPSSystem>();
      else
        idx--;
    }
  }
  return nullptr;
}


int
PositioningSystems::aprsCount() const {
  int c=0;
  for (int i=0; i<count(); i++) {
    if (_items.at(i)->is<APRSSystem>())
      c++;
  }
  return c;
}

int
PositioningSystems::indexOfAPRSSys(APRSSystem *aprs) const {
  if (! _items.contains(aprs))
    return -1;

  int idx=0;
  for (int i=0; i<count(); i++) {
    if (aprs == _items.at(i))
      return idx;
    if (_items.at(i)->is<APRSSystem>())
      idx++;
  }

  return -1;
}

APRSSystem *
PositioningSystems::aprsSystem(int idx) const {
  if ((0>idx) || (idx >= _items.size()))
    return nullptr;
  for (int i=0; i<_items.size(); i++) {
    if (_items.at(i)->is<APRSSystem>()) {
      if (0==idx)
        return _items.at(i)->as<APRSSystem>();
      else
        idx--;
    }
  }
  return nullptr;
}

ConfigItem *
PositioningSystems::allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err) {
  Q_UNUSED(ctx)
  if (! node)
    return nullptr;

  if ((! node.IsMap()) || (1 != node.size())) {
    errMsg(err) << node.Mark().line << ":" << node.Mark().column
                << ": Cannot create positioning system: Expected object with one child.";
    return nullptr;
  }

  QString type = QString::fromStdString(node.begin()->first.as<std::string>());
  if ("dmr" == type) {
    return new GPSSystem();
  } else if ("aprs"==type) {
    return new APRSSystem();
  }

  errMsg(err) << node.Mark().line << ":" << node.Mark().column
              << ": Cannot create positioning system: Unknown type '" << type << "'.";

  return nullptr;
}