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

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