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

Last change on this file since 1973 was 1973, checked in by weber, 14 years ago

* empty log message *

File size: 19.5 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 }
315
316 _latencyChecker = new latencyChecker(_staID);
317
318 msleep(100); //sleep 0.1 sec
319}
320
321// Destructor
322////////////////////////////////////////////////////////////////////////////
323bncGetThread::~bncGetThread() {
324
325 if (isRunning()) {
326 wait();
327 }
328
329 if (_query) {
330 _query->stop();
331 _query->deleteLater();
332 }
333 delete _decoder;
334 delete _rnx;
335 delete _rawInpFile;
336 delete _rawOutFile;
337 delete _serialOutFile;
338 delete _serialPort;
339 delete _latencyChecker;
340 emit getThreadFinished(_staID);
341}
342
343//
344////////////////////////////////////////////////////////////////////////////
345void bncGetThread::terminate() {
346 _isToBeDeleted = true;
347 if (!isRunning()) {
348 delete this;
349 }
350}
351
352// Run
353////////////////////////////////////////////////////////////////////////////
354void bncGetThread::run() {
355
356 while (true) {
357 try {
358 if (_isToBeDeleted) {
359 QThread::exit(0);
360 this->deleteLater();
361 return;
362 }
363
364 if (tryReconnect() != success) {
365 _latencyChecker->checkReconnect();
366 continue;
367 }
368
369 // Delete old observations
370 // -----------------------
371 QListIterator<p_obs> itOld(_decoder->_obsList);
372 while (itOld.hasNext()) {
373 delete itOld.next();
374 }
375 _decoder->_obsList.clear();
376
377 // Read Data
378 // ---------
379 QByteArray data;
380 if (_query) {
381 _query->waitForReadyRead(data);
382 }
383 else if (_rawInpFile) {
384 const qint64 maxBytes = 1024;
385 data = _rawInpFile->read(maxBytes);
386 if (data.isEmpty()) {
387 cout << "no more data" << endl;
388 ::exit(0);
389 }
390 }
391 qint64 nBytes = data.size();
392
393 // Timeout, reconnect
394 // ------------------
395 if (nBytes == 0) {
396 _latencyChecker->checkReconnect();
397 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
398 continue;
399 }
400 else {
401 emit newBytes(_staID, nBytes);
402 }
403
404 // Output Data
405 // -----------
406 if (_rawOutFile) {
407 _rawOutFile->write(data);
408 _rawOutFile->flush();
409 }
410 if (_serialPort) {
411 slotSerialReadyRead();
412 _serialPort->write(data);
413 }
414
415 // Decode Data
416 // -----------
417 vector<string> errmsg;
418 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
419
420 // Perform various scans and checks
421 // --------------------------------
422 _latencyChecker->checkOutage(irc == success);
423 _latencyChecker->checkObsLatency(_decoder->_obsList);
424 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
425
426 emit newLatency(_staID, _latencyChecker->currentLatency());
427
428 scanRTCM();
429
430 // Loop over all observations (observations output)
431 // ------------------------------------------------
432 QListIterator<p_obs> it(_decoder->_obsList);
433 while (it.hasNext()) {
434 p_obs obs = it.next();
435
436 // Check observation epoch
437 // -----------------------
438 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
439 int week;
440 double sec;
441 currentGPSWeeks(week, sec);
442 const double secPerWeek = 7.0 * 24.0 * 3600.0;
443
444 if (week < obs->_o.GPSWeek) {
445 week += 1;
446 sec -= secPerWeek;
447 }
448 if (week > obs->_o.GPSWeek) {
449 week -= 1;
450 sec += secPerWeek;
451 }
452 double dt = fabs(sec - obs->_o.GPSWeeks);
453 const double maxDt = 600.0;
454 if (week != obs->_o.GPSWeek || dt > maxDt) {
455 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
456 delete obs;
457 continue;
458 }
459 }
460
461 // RINEX Output
462 // ------------
463 if (_rnx) {
464 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
465 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
466 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
467 _rnx->deepCopy(obs);
468 }
469 _rnx->dumpEpoch(newTime);
470 }
471
472 // Emit new observation signal
473 // ---------------------------
474 bool firstObs = (obs == _decoder->_obsList.first());
475 obs->_status = t_obs::posted;
476 emit newObs(_staID, firstObs, obs);
477 }
478 _decoder->_obsList.clear();
479 }
480 catch (...) {
481 emit(newMessage(_staID + "bncGetThread exception", true));
482 _isToBeDeleted = true;
483 }
484 }
485}
486
487// Try Re-Connect
488////////////////////////////////////////////////////////////////////////////
489t_irc bncGetThread::tryReconnect() {
490
491 // Easy Return
492 // -----------
493 if (_query && _query->status() == bncNetQuery::running) {
494 _nextSleep = 0;
495 if (_rnx) {
496 _rnx->setReconnectFlag(false);
497 }
498 return success;
499 }
500
501 // Start a new query
502 // -----------------
503 if (!_rawInpFile) {
504
505 sleep(_nextSleep);
506 if (_nextSleep == 0) {
507 _nextSleep = 1;
508 }
509 else {
510 _nextSleep = 2 * _nextSleep;
511 if (_nextSleep > 256) {
512 _nextSleep = 256;
513 }
514#ifdef MLS_SOFTWARE
515 if (_nextSleep > 4) {
516 _nextSleep = 4;
517 }
518#endif
519 }
520
521 delete _query;
522 if (_ntripVersion == "U") {
523 _query = new bncNetQueryUdp();
524 }
525 else if (_ntripVersion == "R") {
526 _query = new bncNetQueryRtp();
527 }
528 else if (_ntripVersion == "S") {
529 _query = new bncNetQueryS();
530 }
531 else if (_ntripVersion == "N") {
532 _query = new bncNetQueryV0();
533 }
534 else if (_ntripVersion == "UN") {
535 _query = new bncNetQueryUdp0();
536 }
537 else if (_ntripVersion == "2") {
538 _query = new bncNetQueryV2();
539 }
540 else {
541 _query = new bncNetQueryV1();
542 }
543 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
544 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
545 _query->startRequest(_mountPoint, gga);
546 }
547 else {
548 _query->startRequest(_mountPoint, "");
549 }
550 if (_query->status() != bncNetQuery::running) {
551 return failure;
552 }
553 }
554
555 if (_rnx) {
556 _rnx->setReconnectFlag(true);
557 }
558
559 return success;
560}
561
562// RTCM scan output
563//////////////////////////////////////////////////////////////////////////////
564void bncGetThread::scanRTCM() {
565
566 bncSettings settings;
567 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
568
569 if ( _miscMount == _staID || _miscMount == "ALL" ) {
570
571 // RTCM message types
572 // ------------------
573 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
574 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
575 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
576 }
577
578 // RTCMv3 antenna descriptor
579 // -------------------------
580 for (int ii=0;ii<_decoder->_antType.size();ii++) {
581 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
582 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
583 }
584
585 // RTCM Antenna Coordinates
586 // ------------------------
587 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
588 QByteArray antT;
589 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
590 antT = "ARP";
591 }
592 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
593 antT = "APC";
594 }
595 QByteArray ant1, ant2, ant3;
596 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
597 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
598 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
599 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
600 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
601 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
602 if (_decoder->_antList[ii].height_f) {
603 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
604 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
605 }
606 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
607 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
608 antT));
609 }
610 }
611 }
612
613#ifdef MLS_SOFTWARE
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 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
623 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
624 antT));
625 }
626#endif
627
628
629
630
631 _decoder->_typeList.clear();
632 _decoder->_antType.clear();
633 _decoder->_antList.clear();
634}
635
636// Handle Data from Serial Port
637////////////////////////////////////////////////////////////////////////////
638void bncGetThread::slotSerialReadyRead() {
639 if (_serialPort) {
640 int nb = _serialPort->bytesAvailable();
641 if (nb > 0) {
642 QByteArray data = _serialPort->read(nb);
643
644 if (_serialNMEA == AUTO_NMEA) {
645 int i1 = data.indexOf("$GPGGA");
646 if (i1 != -1) {
647 int i2 = data.indexOf("*", i1);
648 if (i2 != -1 && data.size() > i2 + 1) {
649 QByteArray gga = data.mid(i1,i2-i1+3);
650 _query->sendNMEA(gga);
651 }
652 }
653 }
654
655 if (_serialOutFile) {
656 _serialOutFile->write(data);
657 _serialOutFile->flush();
658 }
659 }
660 }
661}
662
663//
664//////////////////////////////////////////////////////////////////////////////
665void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
666 RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
667 RTCM3Decoder* decoder3 = dynamic_cast<RTCM3Decoder*>(_decoder);
668
669 if ( decoder2 ) {
670 QMutexLocker locker(&_mutex);
671
672 string storedPRN;
673 vector<int> IODs;
674
675 if ( decoder2->storeEph(gpseph, storedPRN, IODs) ) {
676#ifdef DEBUG_RTCM2_2021
677 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
678
679 for (unsigned ii = 0; ii < IODs.size(); ii++) {
680 msg += QString(" %1").arg(IODs[ii],4);
681 }
682
683 emit(newMessage(msg.toAscii()));
684#endif
685 }
686 }
687
688 if ( decoder3 ) {
689 QMutexLocker locker(&_mutex);
690
691 if ( decoder3->storeEph(gpseph) ) {
692#ifdef DEBUG_RTCM3
693 QString msg = _staID + QString(": RTCM3Decoder, stored eph for satellite %1").arg(gpseph.satellite);
694 emit(newMessage(msg.toAscii(),true));
695#endif
696 }
697 }
698}
699
Note: See TracBrowser for help on using the repository browser.