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

Last change on this file since 3345 was 3345, checked in by mervart, 13 years ago
File size: 20.7 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 "bncnetqueryudp0.h"
62#include "bncnetquerys.h"
63#include "bncsettings.h"
64#include "latencychecker.h"
65#include "bncpppclient.h"
66#include "upload/bncrtnetdecoder.h"
67#include "RTCM/RTCM2Decoder.h"
68#include "RTCM3/RTCM3Decoder.h"
69#include "GPSS/gpssDecoder.h"
70#include "serial/qextserialport.h"
71
72using namespace std;
73
74// Constructor 1
75////////////////////////////////////////////////////////////////////////////
76bncGetThread::bncGetThread(bncRawFile* rawFile) {
77
78 _rawFile = rawFile;
79 _format = rawFile->format();
80 _staID = rawFile->staID();
81 _rawOutput = false;
82 _ntripVersion = "N";
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) {
95 _rawFile = 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
104 bncSettings settings;
105 if (!settings.value("rawOutFile").toString().isEmpty()) {
106 _rawOutput = true;
107 }
108
109 initialize();
110}
111
112// Initialization (common part of the constructor)
113////////////////////////////////////////////////////////////////////////////
114void bncGetThread::initialize() {
115
116 setTerminationEnabled(true);
117
118 bncApp* app = (bncApp*) qApp;
119
120 connect(this, SIGNAL(newMessage(QByteArray,bool)),
121 app, SLOT(slotMessage(const QByteArray,bool)));
122
123 _isToBeDeleted = false;
124 _decoder = 0;
125 _query = 0;
126 _nextSleep = 0;
127 _PPPclient = 0;
128
129 bncSettings settings;
130
131 _miscMount = settings.value("miscMount").toString();
132
133 // RINEX writer
134 // ------------
135 _samplingRate = settings.value("rnxSampl").toInt();
136 if ( settings.value("rnxPath").toString().isEmpty() ) {
137 _rnx = 0;
138 }
139 else {
140 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
141 _longitude, _nmea, _ntripVersion);
142 }
143
144 // Serial Port
145 // -----------
146 _serialNMEA = NO_NMEA;
147 _serialOutFile = 0;
148 _serialPort = 0;
149
150 if (settings.value("serialMountPoint").toString() == _staID) {
151 _serialPort = new QextSerialPort(settings.value("serialPortName").toString() );
152 _serialPort->setTimeout(0,100);
153
154 // Baud Rate
155 // ---------
156 QString hlp = settings.value("serialBaudRate").toString();
157 if (hlp == "110") {
158 _serialPort->setBaudRate(BAUD110);
159 }
160 else if (hlp == "300") {
161 _serialPort->setBaudRate(BAUD300);
162 }
163 else if (hlp == "600") {
164 _serialPort->setBaudRate(BAUD600);
165 }
166 else if (hlp == "1200") {
167 _serialPort->setBaudRate(BAUD1200);
168 }
169 else if (hlp == "2400") {
170 _serialPort->setBaudRate(BAUD2400);
171 }
172 else if (hlp == "4800") {
173 _serialPort->setBaudRate(BAUD4800);
174 }
175 else if (hlp == "9600") {
176 _serialPort->setBaudRate(BAUD9600);
177 }
178 else if (hlp == "19200") {
179 _serialPort->setBaudRate(BAUD19200);
180 }
181 else if (hlp == "38400") {
182 _serialPort->setBaudRate(BAUD38400);
183 }
184 else if (hlp == "57600") {
185 _serialPort->setBaudRate(BAUD57600);
186 }
187 else if (hlp == "115200") {
188 _serialPort->setBaudRate(BAUD115200);
189 }
190
191 // Parity
192 // ------
193 hlp = settings.value("serialParity").toString();
194 if (hlp == "NONE") {
195 _serialPort->setParity(PAR_NONE);
196 }
197 else if (hlp == "ODD") {
198 _serialPort->setParity(PAR_ODD);
199 }
200 else if (hlp == "EVEN") {
201 _serialPort->setParity(PAR_EVEN);
202 }
203 else if (hlp == "SPACE") {
204 _serialPort->setParity(PAR_SPACE);
205 }
206
207 // Data Bits
208 // ---------
209 hlp = settings.value("serialDataBits").toString();
210 if (hlp == "5") {
211 _serialPort->setDataBits(DATA_5);
212 }
213 else if (hlp == "6") {
214 _serialPort->setDataBits(DATA_6);
215 }
216 else if (hlp == "7") {
217 _serialPort->setDataBits(DATA_7);
218 }
219 else if (hlp == "8") {
220 _serialPort->setDataBits(DATA_8);
221 }
222 hlp = settings.value("serialStopBits").toString();
223 if (hlp == "1") {
224 _serialPort->setStopBits(STOP_1);
225 }
226 else if (hlp == "2") {
227 _serialPort->setStopBits(STOP_2);
228 }
229
230 // Flow Control
231 // ------------
232 hlp = settings.value("serialFlowControl").toString();
233 if (hlp == "XONXOFF") {
234 _serialPort->setFlowControl(FLOW_XONXOFF);
235 }
236 else if (hlp == "HARDWARE") {
237 _serialPort->setFlowControl(FLOW_HARDWARE);
238 }
239 else {
240 _serialPort->setFlowControl(FLOW_OFF);
241 }
242
243 // Open Serial Port
244 // ----------------
245 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
246 if (!_serialPort->isOpen()) {
247 delete _serialPort;
248 _serialPort = 0;
249 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
250 }
251 connect(_serialPort, SIGNAL(readyRead()),
252 this, SLOT(slotSerialReadyRead()));
253
254 // Automatic NMEA
255 // --------------
256 if (settings.value("serialAutoNMEA").toString() == "Auto") {
257 _serialNMEA = AUTO_NMEA;
258
259 QString fName = settings.value("serialFileNMEA").toString();
260 if (!fName.isEmpty()) {
261 _serialOutFile = new QFile(fName);
262 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
263 _serialOutFile->open(QIODevice::WriteOnly | QIODevice::Append);
264 }
265 else {
266 _serialOutFile->open(QIODevice::WriteOnly);
267 }
268 }
269 }
270
271 // Manual NMEA
272 // -----------
273 else {
274 _serialNMEA = MANUAL_NMEA;
275 }
276 }
277
278 // Initialize PPP Client?
279 // ----------------------
280#ifndef MLS_SOFTWARE
281 if (settings.value("pppMount").toString() == _staID) {
282 _PPPclient = new bncPPPclient(_staID);
283 app->_bncPPPclient = _PPPclient;
284 qRegisterMetaType<bncTime>("bncTime");
285 connect(_PPPclient, SIGNAL(newPosition(bncTime, double, double, double)),
286 this, SIGNAL(newPosition(bncTime, double, double, double)));
287 connect(_PPPclient, SIGNAL(newNMEAstr(QByteArray)),
288 this, SIGNAL(newNMEAstr(QByteArray)));
289 }
290#endif
291
292 // Instantiate the decoder
293 // -----------------------
294 if (_format.indexOf("RTCM_2") != -1 || _format.indexOf("RTCM2") != -1 ||
295 _format.indexOf("RTCM 2") != -1 ) {
296 emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
297 _decoder = new RTCM2Decoder(_staID.data());
298 }
299 else if (_format.indexOf("RTCM_3") != -1 || _format.indexOf("RTCM3") != -1 ||
300 _format.indexOf("RTCM 3") != -1 ) {
301 emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
302 _decoder = new RTCM3Decoder(_staID, _rawFile);
303 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
304 this, SIGNAL(newMessage(QByteArray,bool)));
305 }
306 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
307 emit(newMessage(_staID + ": Get Data in GPSS format", true));
308 _decoder = new gpssDecoder();
309 }
310 else if (_format.indexOf("ZERO") != -1) {
311 emit(newMessage(_staID + ": Get data in original format", true));
312 _decoder = new bncZeroDecoder(_staID);
313 }
314 else if (_format.indexOf("RTNET") != -1) {
315 emit(newMessage(_staID + ": Get data in RTNet format", true));
316 _decoder = new bncRtnetDecoder();
317 }
318 else {
319 emit(newMessage(_staID + ": Unknown data format " + _format, true));
320 _isToBeDeleted = true;
321 }
322
323 _latencyChecker = new latencyChecker(_staID);
324
325 msleep(100); //sleep 0.1 sec
326}
327
328// Destructor
329////////////////////////////////////////////////////////////////////////////
330bncGetThread::~bncGetThread() {
331 if (isRunning()) {
332 wait();
333 }
334 if (_query) {
335 _query->stop();
336 _query->deleteLater();
337 }
338 delete _PPPclient;
339 delete _decoder;
340 delete _rnx;
341 delete _rawFile;
342 delete _serialOutFile;
343 delete _serialPort;
344 delete _latencyChecker;
345 emit getThreadFinished(_staID);
346}
347
348//
349////////////////////////////////////////////////////////////////////////////
350void bncGetThread::terminate() {
351 _isToBeDeleted = true;
352 if (!isRunning()) {
353 delete this;
354 }
355}
356
357// Run
358////////////////////////////////////////////////////////////////////////////
359void bncGetThread::run() {
360
361 while (true) {
362 try {
363 if (_isToBeDeleted) {
364 QThread::exit(0);
365 this->deleteLater();
366 return;
367 }
368
369 if (tryReconnect() != success) {
370 _latencyChecker->checkReconnect();
371 continue;
372 }
373
374 // Delete old observations
375 // -----------------------
376 _decoder->_obsList.clear();
377
378 // Read Data
379 // ---------
380 QByteArray data;
381 if (_query) {
382 _query->waitForReadyRead(data);
383 }
384 else if (_rawFile) {
385 data = _rawFile->readChunk();
386
387 if (data.isEmpty()) {
388 cout << "no more data" << endl;
389 QThread::exit(0);
390 this->deleteLater();
391 return;
392 }
393 }
394 qint64 nBytes = data.size();
395
396 // Timeout, reconnect
397 // ------------------
398 if (nBytes == 0) {
399 _latencyChecker->checkReconnect();
400 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
401 msleep(10000); //sleep 10 sec, G. Weber
402 continue;
403 }
404 else {
405 emit newBytes(_staID, nBytes);
406 }
407
408 // Output Data
409 // -----------
410 if (_rawOutput) {
411 bncApp* app = (bncApp*) qApp;
412 app->writeRawData(data, _staID, _format);
413 }
414
415 if (_serialPort) {
416 slotSerialReadyRead();
417 _serialPort->write(data);
418 }
419
420 // Decode Data
421 // -----------
422 vector<string> errmsg;
423 _decoder->_obsList.clear();
424 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
425
426 // Perform various scans and checks
427 // --------------------------------
428 _latencyChecker->checkOutage(irc == success);
429 _latencyChecker->checkObsLatency(_decoder->_obsList);
430 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
431
432 emit newLatency(_staID, _latencyChecker->currentLatency());
433
434 scanRTCM();
435
436 // Loop over all observations (observations output)
437 // ------------------------------------------------
438 QListIterator<t_obs> it(_decoder->_obsList);
439 bool firstObs = true;
440 while (it.hasNext()) {
441 const t_obs& obs = it.next();
442
443 QString prn = QString("%1%2").arg(obs.satSys)
444 .arg(obs.satNum, 2, 10, QChar('0'));
445 long iSec = long(floor(obs.GPSWeeks+0.5));
446 long obsTime = obs.GPSWeek * 7*24*3600 + iSec;
447
448 // Check observation epoch
449 // -----------------------
450 if (!_rawFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
451 int week;
452 double sec;
453 currentGPSWeeks(week, sec);
454 long currTime = week * 7*24*3600 + long(sec);
455 const double maxDt = 600.0;
456 if (fabs(currTime - obsTime) > maxDt) {
457 emit( newMessage(_staID + ": Wrong observation epoch(s)", false) );
458 continue;
459 }
460 }
461
462 // Check observations coming twice (e.g. KOUR0 Problem)
463 // ----------------------------------------------------
464 QMap<QString, long>::const_iterator it = _prnLastEpo.find(prn);
465 if (it != _prnLastEpo.end()) {
466 long oldTime = it.value();
467 if (obsTime < oldTime) {
468 emit( newMessage(_staID +
469 ": old observation " + prn.toAscii(), false));
470 continue;
471 }
472 else if (obsTime == oldTime) {
473 emit( newMessage(_staID +
474 ": observation coming more than once " + prn.toAscii(), false));
475 continue;
476 }
477 }
478 _prnLastEpo[prn] = obsTime;
479
480 // RINEX Output
481 // ------------
482 if (_rnx) {
483 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
484 _rnx->deepCopy(obs);
485 }
486 _rnx->dumpEpoch(obsTime);
487 }
488
489 // PPP Client
490 // ----------
491#ifndef MLS_SOFTWARE
492 if (_PPPclient) {
493 _PPPclient->putNewObs(obs);
494 }
495#endif
496
497 // Emit new observation signal
498 // ---------------------------
499 if (!_isToBeDeleted) {
500 emit newObs(_staID, firstObs, obs);
501 }
502 firstObs = false;
503 }
504 _decoder->_obsList.clear();
505 }
506 catch (Exception& exc) {
507 emit(newMessage(_staID + " " + exc.what(), true));
508 _isToBeDeleted = true;
509 }
510 catch (...) {
511 emit(newMessage(_staID + " bncGetThread exception", true));
512 _isToBeDeleted = true;
513 }
514 }
515}
516
517// Try Re-Connect
518////////////////////////////////////////////////////////////////////////////
519t_irc bncGetThread::tryReconnect() {
520
521 // Easy Return
522 // -----------
523 if (_query && _query->status() == bncNetQuery::running) {
524 _nextSleep = 0;
525 if (_rnx) {
526 _rnx->setReconnectFlag(false);
527 }
528 return success;
529 }
530
531 // Start a new query
532 // -----------------
533 if (!_rawFile) {
534
535 sleep(_nextSleep);
536 if (_nextSleep == 0) {
537 _nextSleep = 1;
538 }
539 else {
540 _nextSleep = 2 * _nextSleep;
541 if (_nextSleep > 256) {
542 _nextSleep = 256;
543 }
544#ifdef MLS_SOFTWARE
545 if (_nextSleep > 4) {
546 _nextSleep = 4;
547 }
548#endif
549 }
550
551 delete _query;
552 if (_ntripVersion == "U") {
553 _query = new bncNetQueryUdp();
554 }
555 else if (_ntripVersion == "R") {
556 _query = new bncNetQueryRtp();
557 }
558 else if (_ntripVersion == "S") {
559 _query = new bncNetQueryS();
560 }
561 else if (_ntripVersion == "N") {
562 _query = new bncNetQueryV0();
563 }
564 else if (_ntripVersion == "UN") {
565 _query = new bncNetQueryUdp0();
566 }
567 else if (_ntripVersion == "2") {
568 _query = new bncNetQueryV2(false);
569 }
570 else if (_ntripVersion == "2s") {
571 _query = new bncNetQueryV2(true);
572 }
573 else {
574 _query = new bncNetQueryV1();
575 }
576 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
577 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
578 _query->startRequest(_mountPoint, gga);
579 }
580 else {
581 _query->startRequest(_mountPoint, "");
582 }
583 if (_query->status() != bncNetQuery::running) {
584 return failure;
585 }
586 }
587
588 if (_rnx) {
589 _rnx->setReconnectFlag(true);
590 }
591
592 return success;
593}
594
595// RTCM scan output
596//////////////////////////////////////////////////////////////////////////////
597void bncGetThread::scanRTCM() {
598
599 bncSettings settings;
600 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
601
602 if ( _miscMount == _staID || _miscMount == "ALL" ) {
603
604 // RTCM message types
605 // ------------------
606 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
607 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
608 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
609 }
610
611 // RTCMv3 antenna descriptor
612 // -------------------------
613 for (int ii=0;ii<_decoder->_antType.size();ii++) {
614 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
615 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
616 }
617
618 // RTCM Antenna Coordinates
619 // ------------------------
620 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
621 QByteArray antT;
622 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
623 antT = "ARP";
624 }
625 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
626 antT = "APC";
627 }
628 QByteArray ant1, ant2, ant3;
629 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
630 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
631 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
632 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
633 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
634 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
635 if (_decoder->_antList[ii].height_f) {
636 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
637 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
638 }
639 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
640 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
641 antT));
642 }
643 }
644 }
645
646#ifdef MLS_SOFTWARE
647 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
648 QByteArray antT;
649 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
650 antT = "ARP";
651 }
652 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
653 antT = "APC";
654 }
655 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
656 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
657 antT));
658 }
659
660 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
661 emit(newRTCMMessage(_staID, _decoder->_typeList[ii]));
662 }
663#endif
664
665
666
667
668 _decoder->_typeList.clear();
669 _decoder->_antType.clear();
670 _decoder->_antList.clear();
671}
672
673// Handle Data from Serial Port
674////////////////////////////////////////////////////////////////////////////
675void bncGetThread::slotSerialReadyRead() {
676 if (_serialPort) {
677 int nb = _serialPort->bytesAvailable();
678 if (nb > 0) {
679 QByteArray data = _serialPort->read(nb);
680
681 if (_serialNMEA == AUTO_NMEA) {
682 int i1 = data.indexOf("$GPGGA");
683 if (i1 != -1) {
684 int i2 = data.indexOf("*", i1);
685 if (i2 != -1 && data.size() > i2 + 1) {
686 QByteArray gga = data.mid(i1,i2-i1+3);
687 _query->sendNMEA(gga);
688 }
689 }
690 }
691
692 if (_serialOutFile) {
693 _serialOutFile->write(data);
694 _serialOutFile->flush();
695 }
696 }
697 }
698}
699
700//
701//////////////////////////////////////////////////////////////////////////////
702void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
703 RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
704 RTCM3Decoder* decoder3 = dynamic_cast<RTCM3Decoder*>(_decoder);
705
706 if ( decoder2 ) {
707 QMutexLocker locker(&_mutex);
708
709 string storedPRN;
710 vector<int> IODs;
711
712 if ( decoder2->storeEph(gpseph, storedPRN, IODs) ) {
713#ifdef DEBUG_RTCM2_2021
714 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
715
716 for (unsigned ii = 0; ii < IODs.size(); ii++) {
717 msg += QString(" %1").arg(IODs[ii],4);
718 }
719
720 emit(newMessage(msg.toAscii()));
721#endif
722 }
723 }
724
725 if ( decoder3 ) {
726 QMutexLocker locker(&_mutex);
727
728 if ( decoder3->storeEph(gpseph) ) {
729#ifdef DEBUG_RTCM3
730 QString msg = _staID + QString(": RTCM3Decoder, stored eph for satellite %1").arg(gpseph.satellite);
731 emit(newMessage(msg.toAscii(),true));
732#endif
733 }
734 }
735}
736
Note: See TracBrowser for help on using the repository browser.