source: ntrip/trunk/BNC/bncgetthread.cpp@ 1768

Last change on this file since 1768 was 1768, checked in by mervart, 15 years ago

* empty log message *

File size: 18.9 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncGetThread
30 *
31 * Purpose: Thread that retrieves data from NTRIP caster
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Dec-2005
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdlib.h>
42#include <iomanip>
43#include <sstream>
44
45#include <QFile>
46#include <QTextStream>
47#include <QtNetwork>
48#include <QTime>
49
50#include "bncgetthread.h"
51#include "bnctabledlg.h"
52#include "bncapp.h"
53#include "bncutils.h"
54#include "bncrinex.h"
55#include "bnczerodecoder.h"
56#include "bncnetqueryv0.h"
57#include "bncnetqueryv1.h"
58#include "bncnetqueryv2.h"
59#include "bncnetqueryrtp.h"
60#include "bncnetqueryudp.h"
61#include "bncnetquerys.h"
62#include "bncsettings.h"
63#include "latencychecker.h"
64
65#include "RTCM/RTCM2Decoder.h"
66#include "RTCM3/RTCM3Decoder.h"
67#include "RTIGS/RTIGSDecoder.h"
68#include "GPSS/gpssDecoder.h"
69#include "serial/qextserialport.h"
70
71using namespace std;
72
73// Constructor 1
74////////////////////////////////////////////////////////////////////////////
75bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
76 const QByteArray& format) {
77
78 _rawInpFile = new QFile(rawInpFileName);
79 _rawInpFile->open(QIODevice::ReadOnly);
80 _format = format;
81 _staID = rawInpFileName.mid(
82 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
83
84 initialize();
85}
86
87// Constructor 2
88////////////////////////////////////////////////////////////////////////////
89bncGetThread::bncGetThread(const QUrl& mountPoint,
90 const QByteArray& format,
91 const QByteArray& latitude,
92 const QByteArray& longitude,
93 const QByteArray& nmea,
94 const QByteArray& ntripVersion, int iMount) {
95 _rawInpFile = 0;
96 _mountPoint = mountPoint;
97 _staID = mountPoint.path().mid(1).toAscii();
98 _format = format;
99 _latitude = latitude;
100 _longitude = longitude;
101 _nmea = nmea;
102 _ntripVersion = ntripVersion;
103 _iMount = iMount; // index in mountpoints array
104
105 initialize();
106}
107
108// Initialization (common part of the constructor)
109////////////////////////////////////////////////////////////////////////////
110void bncGetThread::initialize() {
111
112 setTerminationEnabled(true);
113
114 bncApp* app = (bncApp*) qApp;
115
116 connect(this, SIGNAL(newMessage(QByteArray,bool)),
117 app, SLOT(slotMessage(const QByteArray,bool)));
118
119 _isToBeDeleted = false;
120 _decoder = 0;
121 _query = 0;
122 _nextSleep = 0;
123 _rawOutFile = 0;
124 _staID_orig = _staID;
125
126 bncSettings settings;
127
128 _miscMount = settings.value("miscMount").toString();
129
130 // Check name conflict
131 // -------------------
132 QListIterator<QString> it(settings.value("mountPoints").toStringList());
133 int num = 0;
134 int ind = -1;
135 while (it.hasNext()) {
136 ++ind;
137 QStringList hlp = it.next().split(" ");
138 if (hlp.size() <= 1) continue;
139 QUrl url(hlp[0]);
140 if (_mountPoint.path() == url.path()) {
141 if (_iMount > ind || _iMount < 0) {
142 ++num;
143 }
144 }
145 }
146
147 if (num > 0) {
148 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
149 }
150
151 // RINEX writer
152 // ------------
153 _samplingRate = settings.value("rnxSampl").toInt();
154 if ( settings.value("rnxPath").toString().isEmpty() ) {
155 _rnx = 0;
156 if (_rawInpFile) {
157 cerr << "no RINEX path specified" << endl;
158 ::exit(1);
159 }
160 }
161 else {
162 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
163 _longitude, _nmea, _ntripVersion);
164 }
165
166 // Serial Port
167 // -----------
168 _serialNMEA = NO_NMEA;
169 _serialOutFile = 0;
170 _serialPort = 0;
171
172 if (settings.value("serialMountPoint").toString() == _staID) {
173 _serialPort = new QextSerialPort(settings.value("serialPortName").toString() );
174 _serialPort->setTimeout(0,100);
175
176 // Baud Rate
177 // ---------
178 QString hlp = settings.value("serialBaudRate").toString();
179 if (hlp == "110") {
180 _serialPort->setBaudRate(BAUD110);
181 }
182 else if (hlp == "300") {
183 _serialPort->setBaudRate(BAUD300);
184 }
185 else if (hlp == "600") {
186 _serialPort->setBaudRate(BAUD600);
187 }
188 else if (hlp == "1200") {
189 _serialPort->setBaudRate(BAUD1200);
190 }
191 else if (hlp == "2400") {
192 _serialPort->setBaudRate(BAUD2400);
193 }
194 else if (hlp == "4800") {
195 _serialPort->setBaudRate(BAUD4800);
196 }
197 else if (hlp == "9600") {
198 _serialPort->setBaudRate(BAUD9600);
199 }
200 else if (hlp == "19200") {
201 _serialPort->setBaudRate(BAUD19200);
202 }
203 else if (hlp == "38400") {
204 _serialPort->setBaudRate(BAUD38400);
205 }
206 else if (hlp == "57600") {
207 _serialPort->setBaudRate(BAUD57600);
208 }
209 else if (hlp == "115200") {
210 _serialPort->setBaudRate(BAUD115200);
211 }
212
213 // Parity
214 // ------
215 hlp = settings.value("serialParity").toString();
216 if (hlp == "NONE") {
217 _serialPort->setParity(PAR_NONE);
218 }
219 else if (hlp == "ODD") {
220 _serialPort->setParity(PAR_ODD);
221 }
222 else if (hlp == "EVEN") {
223 _serialPort->setParity(PAR_EVEN);
224 }
225 else if (hlp == "SPACE") {
226 _serialPort->setParity(PAR_SPACE);
227 }
228
229 // Data Bits
230 // ---------
231 hlp = settings.value("serialDataBits").toString();
232 if (hlp == "5") {
233 _serialPort->setDataBits(DATA_5);
234 }
235 else if (hlp == "6") {
236 _serialPort->setDataBits(DATA_6);
237 }
238 else if (hlp == "7") {
239 _serialPort->setDataBits(DATA_7);
240 }
241 else if (hlp == "8") {
242 _serialPort->setDataBits(DATA_8);
243 }
244 hlp = settings.value("serialStopBits").toString();
245 if (hlp == "1") {
246 _serialPort->setStopBits(STOP_1);
247 }
248 else if (hlp == "2") {
249 _serialPort->setStopBits(STOP_2);
250 }
251
252 // Flow Control
253 // ------------
254 hlp = settings.value("serialFlowControl").toString();
255 if (hlp == "XONXOFF") {
256 _serialPort->setFlowControl(FLOW_XONXOFF);
257 }
258 else if (hlp == "HARDWARE") {
259 _serialPort->setFlowControl(FLOW_HARDWARE);
260 }
261 else {
262 _serialPort->setFlowControl(FLOW_OFF);
263 }
264
265 // Open Serial Port
266 // ----------------
267 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
268 if (!_serialPort->isOpen()) {
269 delete _serialPort;
270 _serialPort = 0;
271 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
272 }
273 connect(_serialPort, SIGNAL(readyRead()),
274 this, SLOT(slotSerialReadyRead()));
275
276 // Automatic NMEA
277 // --------------
278 if (settings.value("serialAutoNMEA").toString() == "Auto") {
279 _serialNMEA = AUTO_NMEA;
280
281 QString fName = settings.value("serialFileNMEA").toString();
282 if (!fName.isEmpty()) {
283 _serialOutFile = new QFile(fName);
284 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
285 _serialOutFile->open(QIODevice::WriteOnly | QIODevice::Append);
286 }
287 else {
288 _serialOutFile->open(QIODevice::WriteOnly);
289 }
290 }
291 }
292
293 // Manual NMEA
294 // -----------
295 else {
296 _serialNMEA = MANUAL_NMEA;
297 }
298 }
299
300 // Raw Output
301 // ----------
302 // QByteArray rawOutFileName = "./" + _staID + ".raw";
303 // _rawOutFile = new QFile(rawOutFileName);
304 // _rawOutFile->open(QIODevice::WriteOnly);
305
306
307 // Instantiate the decoder
308 // -----------------------
309 if (_format.indexOf("RTCM_2") != -1 || _format.indexOf("RTCM2") != -1 ||
310 _format.indexOf("RTCM 2") != -1 ) {
311 emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
312 _decoder = new RTCM2Decoder(_staID.data());
313 }
314 else if (_format.indexOf("RTCM_3") != -1 || _format.indexOf("RTCM3") != -1 ||
315 _format.indexOf("RTCM 3") != -1 ) {
316 emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
317 _decoder = new RTCM3Decoder(_staID);
318 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
319 this, SIGNAL(newMessage(QByteArray,bool)));
320 }
321 else if (_format.indexOf("RTIGS") != -1) {
322 emit(newMessage(_staID + ": Get data in RTIGS format", true));
323 _decoder = new RTIGSDecoder();
324 }
325 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
326 emit(newMessage(_staID + ": Get Data in GPSS format", true));
327 _decoder = new gpssDecoder();
328 }
329 else if (_format.indexOf("ZERO") != -1) {
330 emit(newMessage(_staID + ": Get data in original format", true));
331 _decoder = new bncZeroDecoder(_staID);
332 }
333 else {
334 emit(newMessage(_staID + ": Unknown data format " + _format, true));
335 _isToBeDeleted = true;
336 delete this;
337 }
338
339 _latencyChecker = new latencyChecker(_staID);
340
341 msleep(100); //sleep 0.1 sec
342}
343
344// Destructor
345////////////////////////////////////////////////////////////////////////////
346bncGetThread::~bncGetThread() {
347 if (_query) {
348 _query->stop();
349 _query->deleteLater();
350 }
351 delete _decoder;
352 delete _rnx;
353 delete _rawInpFile;
354 delete _rawOutFile;
355 delete _serialOutFile;
356 delete _serialPort;
357 delete _latencyChecker;
358 emit getThreadFinished(_staID);
359}
360
361//
362////////////////////////////////////////////////////////////////////////////
363void bncGetThread::terminate() {
364 _isToBeDeleted = true;
365 if (!isRunning()) {
366 delete this;
367 }
368}
369
370// Run
371////////////////////////////////////////////////////////////////////////////
372void bncGetThread::run() {
373
374 while (true) {
375 try {
376 if (_isToBeDeleted) {
377 QThread::exit(0);
378 this->deleteLater();
379 return;
380 }
381
382 if (tryReconnect() != success) {
383 _latencyChecker->checkReconnect();
384 continue;
385 }
386
387 // Delete old observations
388 // -----------------------
389 QListIterator<p_obs> itOld(_decoder->_obsList);
390 while (itOld.hasNext()) {
391 delete itOld.next();
392 }
393 _decoder->_obsList.clear();
394
395 // Read Data
396 // ---------
397 QByteArray data;
398 if (_query) {
399 _query->waitForReadyRead(data);
400 }
401 else if (_rawInpFile) {
402 const qint64 maxBytes = 1024;
403 data = _rawInpFile->read(maxBytes);
404 if (data.isEmpty()) {
405 cout << "no more data" << endl;
406 ::exit(0);
407 }
408 }
409 qint64 nBytes = data.size();
410
411 // Timeout, reconnect
412 // ------------------
413 if (nBytes == 0) {
414 _latencyChecker->checkReconnect();
415 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
416 continue;
417 }
418 else {
419 emit newBytes(_staID, nBytes);
420 }
421
422 // Output Data
423 // -----------
424 if (_rawOutFile) {
425 _rawOutFile->write(data);
426 _rawOutFile->flush();
427 }
428 if (_serialPort) {
429 slotSerialReadyRead();
430 _serialPort->write(data);
431 }
432
433 // Decode Data
434 // -----------
435 vector<string> errmsg;
436 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
437
438 // Perform various scans and checks
439 // --------------------------------
440 _latencyChecker->checkOutage(irc == success);
441 _latencyChecker->checkObsLatency(_decoder->_obsList);
442 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
443
444 scanRTCM();
445
446 // Loop over all observations (observations output)
447 // ------------------------------------------------
448 QListIterator<p_obs> it(_decoder->_obsList);
449 while (it.hasNext()) {
450 p_obs obs = it.next();
451
452 // Check observation epoch
453 // -----------------------
454 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
455 int week;
456 double sec;
457 currentGPSWeeks(week, sec);
458 const double secPerWeek = 7.0 * 24.0 * 3600.0;
459
460 if (week < obs->_o.GPSWeek) {
461 week += 1;
462 sec -= secPerWeek;
463 }
464 if (week > obs->_o.GPSWeek) {
465 week -= 1;
466 sec += secPerWeek;
467 }
468 double dt = fabs(sec - obs->_o.GPSWeeks);
469 const double maxDt = 600.0;
470 if (week != obs->_o.GPSWeek || dt > maxDt) {
471 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
472 delete obs;
473 continue;
474 }
475 }
476
477 // RINEX Output
478 // ------------
479 if (_rnx) {
480 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
481 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
482 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
483 _rnx->deepCopy(obs);
484 }
485 _rnx->dumpEpoch(newTime);
486 }
487
488 // Emit new observation signal
489 // ---------------------------
490 bool firstObs = (obs == _decoder->_obsList.first());
491 obs->_status = t_obs::posted;
492 emit newObs(_staID, firstObs, obs);
493 }
494 _decoder->_obsList.clear();
495 }
496 catch (...) {
497 emit(newMessage(_staID + "bncGetThread exception", true));
498 _isToBeDeleted = true;
499 }
500 }
501}
502
503// Try Re-Connect
504////////////////////////////////////////////////////////////////////////////
505t_irc bncGetThread::tryReconnect() {
506
507 // Easy Return
508 // -----------
509 if (_query && _query->status() == bncNetQuery::running) {
510 _nextSleep = 0;
511 if (_rnx) {
512 _rnx->setReconnectFlag(false);
513 }
514 return success;
515 }
516
517 // Start a new query
518 // -----------------
519 if (!_rawInpFile) {
520
521 sleep(_nextSleep);
522 if (_nextSleep == 0) {
523 _nextSleep = 1;
524 }
525 else {
526 _nextSleep = 2 * _nextSleep;
527 if (_nextSleep > 256) {
528 _nextSleep = 256;
529 }
530 }
531
532 delete _query;
533 if (_ntripVersion == "U") {
534 _query = new bncNetQueryUdp();
535 }
536 else if (_ntripVersion == "R") {
537 _query = new bncNetQueryRtp();
538 }
539 else if (_ntripVersion == "S") {
540 _query = new bncNetQueryS();
541 }
542 else if (_ntripVersion == "N") {
543 _query = new bncNetQueryV0();
544 }
545 else if (_ntripVersion == "2") {
546 _query = new bncNetQueryV2();
547 }
548 else {
549 _query = new bncNetQueryV1();
550 }
551 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
552 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
553 _query->startRequest(_mountPoint, gga);
554 }
555 else {
556 _query->startRequest(_mountPoint, "");
557 }
558 if (_query->status() != bncNetQuery::running) {
559 return failure;
560 }
561 }
562
563 if (_rnx) {
564 _rnx->setReconnectFlag(true);
565 }
566
567 return success;
568}
569
570// RTCM scan output
571//////////////////////////////////////////////////////////////////////////////
572void bncGetThread::scanRTCM() {
573
574 bncSettings settings;
575 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
576
577 if ( _miscMount == _staID || _miscMount == "ALL" ) {
578
579 // RTCM message types
580 // ------------------
581 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
582 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
583 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
584 }
585
586 // RTCMv3 antenna descriptor
587 // -------------------------
588 for (int ii=0;ii<_decoder->_antType.size();ii++) {
589 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
590 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
591 }
592
593 // RTCM Antenna Coordinates
594 // ------------------------
595 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
596 QByteArray antT;
597 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
598 antT = "ARP";
599 }
600 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
601 antT = "APC";
602 }
603 QByteArray ant1, ant2, ant3;
604 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
605 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
606 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
607 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
608 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
609 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
610 if (_decoder->_antList[ii].height_f) {
611 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
612 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
613 }
614 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
615 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
616 antT));
617 }
618 }
619 }
620
621 _decoder->_typeList.clear();
622 _decoder->_antType.clear();
623 _decoder->_antList.clear();
624}
625
626// Handle Data from Serial Port
627////////////////////////////////////////////////////////////////////////////
628void bncGetThread::slotSerialReadyRead() {
629 if (_serialPort) {
630 int nb = _serialPort->bytesAvailable();
631 if (nb > 0) {
632 QByteArray data = _serialPort->read(nb);
633
634 if (_serialNMEA == AUTO_NMEA) {
635 int i1 = data.indexOf("$GPGGA");
636 if (i1 != -1) {
637 int i2 = data.indexOf("*", i1);
638 if (i2 != -1 && data.size() > i2 + 1) {
639 QByteArray gga = data.mid(i1,i2-i1+3);
640 _query->sendNMEA(gga);
641 }
642 }
643 }
644
645 if (_serialOutFile) {
646 _serialOutFile->write(data);
647 _serialOutFile->flush();
648 }
649 }
650 }
651}
652
653//
654//////////////////////////////////////////////////////////////////////////////
655void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
656 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
657
658 if ( decoder ) {
659 QMutexLocker locker(&_mutex);
660
661 string storedPRN;
662 vector<int> IODs;
663
664 if ( decoder->storeEph(gpseph, storedPRN, IODs) ) {
665#ifdef DEBUG_RTCM2_2021
666 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
667
668 for (unsigned ii = 0; ii < IODs.size(); ii++) {
669 msg += QString(" %1").arg(IODs[ii],4);
670 }
671
672 emit(newMessage(msg.toAscii()));
673#endif
674 }
675 }
676}
677
Note: See TracBrowser for help on using the repository browser.