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

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