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

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

* empty log message *

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