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

Last change on this file since 1912 was 1817, checked in by mervart, 15 years ago

* empty log message *

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