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

Last change on this file since 2515 was 2488, checked in by mervart, 15 years ago
File size: 20.3 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 double currSec = week * secPerWeek + sec;
466 double obsSec = obs->_o.GPSWeek * secPerWeek + obs->_o.GPSWeeks;
467
468 const double maxDt = 600.0;
469
470 if (fabs(currSec - obsSec) > maxDt) {
471 emit( newMessage(_staID + ": Wrong observation epoch(s)", false) );
472 delete obs;
473 continue;
474 }
475 }
476
477 // RINEX Output
478 // ------------
479 if (_rnx) {
480 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
481 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
482 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
483 _rnx->deepCopy(obs);
484 }
485 _rnx->dumpEpoch(newTime);
486 }
487
488 // PPP Client
489 // ----------
490#ifndef MLS_SOFTWARE
491 if (_PPPclient) {
492 _PPPclient->putNewObs(obs);
493 }
494#endif
495
496 // Emit new observation signal
497 // ---------------------------
498 bool firstObs = (obs == _decoder->_obsList.first());
499 obs->_status = t_obs::posted;
500 emit newObs(_staID, firstObs, obs);
501 }
502 _decoder->_obsList.clear();
503 }
504 catch (...) {
505 emit(newMessage(_staID + "bncGetThread exception", true));
506 _isToBeDeleted = true;
507 }
508 }
509}
510
511// Try Re-Connect
512////////////////////////////////////////////////////////////////////////////
513t_irc bncGetThread::tryReconnect() {
514
515 // Easy Return
516 // -----------
517 if (_query && _query->status() == bncNetQuery::running) {
518 _nextSleep = 0;
519 if (_rnx) {
520 _rnx->setReconnectFlag(false);
521 }
522 return success;
523 }
524
525 // Start a new query
526 // -----------------
527 if (!_rawInpFile) {
528
529 sleep(_nextSleep);
530 if (_nextSleep == 0) {
531 _nextSleep = 1;
532 }
533 else {
534 _nextSleep = 2 * _nextSleep;
535 if (_nextSleep > 256) {
536 _nextSleep = 256;
537 }
538#ifdef MLS_SOFTWARE
539 if (_nextSleep > 4) {
540 _nextSleep = 4;
541 }
542#endif
543 }
544
545 delete _query;
546 if (_ntripVersion == "U") {
547 _query = new bncNetQueryUdp();
548 }
549 else if (_ntripVersion == "R") {
550 _query = new bncNetQueryRtp();
551 }
552 else if (_ntripVersion == "S") {
553 _query = new bncNetQueryS();
554 }
555 else if (_ntripVersion == "N") {
556 _query = new bncNetQueryV0();
557 }
558 else if (_ntripVersion == "UN") {
559 _query = new bncNetQueryUdp0();
560 }
561 else if (_ntripVersion == "2") {
562 _query = new bncNetQueryV2();
563 }
564 else {
565 _query = new bncNetQueryV1();
566 }
567 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
568 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
569 _query->startRequest(_mountPoint, gga);
570 }
571 else {
572 _query->startRequest(_mountPoint, "");
573 }
574 if (_query->status() != bncNetQuery::running) {
575 return failure;
576 }
577 }
578
579 if (_rnx) {
580 _rnx->setReconnectFlag(true);
581 }
582
583 return success;
584}
585
586// RTCM scan output
587//////////////////////////////////////////////////////////////////////////////
588void bncGetThread::scanRTCM() {
589
590 bncSettings settings;
591 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
592
593 if ( _miscMount == _staID || _miscMount == "ALL" ) {
594
595 // RTCM message types
596 // ------------------
597 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
598 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
599 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
600 }
601
602 // RTCMv3 antenna descriptor
603 // -------------------------
604 for (int ii=0;ii<_decoder->_antType.size();ii++) {
605 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
606 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
607 }
608
609 // RTCM Antenna Coordinates
610 // ------------------------
611 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
612 QByteArray antT;
613 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
614 antT = "ARP";
615 }
616 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
617 antT = "APC";
618 }
619 QByteArray ant1, ant2, ant3;
620 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
621 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
622 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
623 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
624 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
625 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
626 if (_decoder->_antList[ii].height_f) {
627 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
628 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
629 }
630 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
631 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
632 antT));
633 }
634 }
635 }
636
637#ifdef MLS_SOFTWARE
638 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
639 QByteArray antT;
640 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
641 antT = "ARP";
642 }
643 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
644 antT = "APC";
645 }
646 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
647 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
648 antT));
649 }
650
651 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
652 emit(newRTCMMessage(_staID, _decoder->_typeList[ii]));
653 }
654#endif
655
656
657
658
659 _decoder->_typeList.clear();
660 _decoder->_antType.clear();
661 _decoder->_antList.clear();
662}
663
664// Handle Data from Serial Port
665////////////////////////////////////////////////////////////////////////////
666void bncGetThread::slotSerialReadyRead() {
667 if (_serialPort) {
668 int nb = _serialPort->bytesAvailable();
669 if (nb > 0) {
670 QByteArray data = _serialPort->read(nb);
671
672 if (_serialNMEA == AUTO_NMEA) {
673 int i1 = data.indexOf("$GPGGA");
674 if (i1 != -1) {
675 int i2 = data.indexOf("*", i1);
676 if (i2 != -1 && data.size() > i2 + 1) {
677 QByteArray gga = data.mid(i1,i2-i1+3);
678 _query->sendNMEA(gga);
679 }
680 }
681 }
682
683 if (_serialOutFile) {
684 _serialOutFile->write(data);
685 _serialOutFile->flush();
686 }
687 }
688 }
689}
690
691//
692//////////////////////////////////////////////////////////////////////////////
693void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
694 RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
695 RTCM3Decoder* decoder3 = dynamic_cast<RTCM3Decoder*>(_decoder);
696
697 if ( decoder2 ) {
698 QMutexLocker locker(&_mutex);
699
700 string storedPRN;
701 vector<int> IODs;
702
703 if ( decoder2->storeEph(gpseph, storedPRN, IODs) ) {
704#ifdef DEBUG_RTCM2_2021
705 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
706
707 for (unsigned ii = 0; ii < IODs.size(); ii++) {
708 msg += QString(" %1").arg(IODs[ii],4);
709 }
710
711 emit(newMessage(msg.toAscii()));
712#endif
713 }
714 }
715
716 if ( decoder3 ) {
717 QMutexLocker locker(&_mutex);
718
719 if ( decoder3->storeEph(gpseph) ) {
720#ifdef DEBUG_RTCM3
721 QString msg = _staID + QString(": RTCM3Decoder, stored eph for satellite %1").arg(gpseph.satellite);
722 emit(newMessage(msg.toAscii(),true));
723#endif
724 }
725 }
726}
727
Note: See TracBrowser for help on using the repository browser.