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

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

* empty log message *

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