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

Last change on this file since 2385 was 2385, checked in by mervart, 14 years ago

* empty log message *

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