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

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

* empty log message *

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