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

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

* empty log message *

File size: 17.8 KB
RevLine 
[280]1// Part of BNC, a utility for retrieving decoding and
[464]2// converting GNSS data streams from NTRIP broadcasters.
[280]3//
[464]4// Copyright (C) 2007
[280]5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
[464]7// Czech Technical University Prague, Department of Geodesy
[280]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.
[35]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[35]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
[277]41#include <stdlib.h>
[1044]42#include <iomanip>
[1218]43#include <sstream>
[277]44
[35]45#include <QFile>
46#include <QTextStream>
47#include <QtNetwork>
[356]48#include <QTime>
[35]49
50#include "bncgetthread.h"
[192]51#include "bnctabledlg.h"
[243]52#include "bncapp.h"
[352]53#include "bncutils.h"
[408]54#include "bncrinex.h"
[423]55#include "bnczerodecoder.h"
[1621]56#include "bncnetqueryv0.h"
[1387]57#include "bncnetqueryv1.h"
58#include "bncnetqueryv2.h"
[1410]59#include "bncnetqueryrtp.h"
[1721]60#include "bncnetqueryudp.h"
[1535]61#include "bncsettings.h"
[1558]62#include "latencychecker.h"
[65]63
[243]64#include "RTCM/RTCM2Decoder.h"
[297]65#include "RTCM3/RTCM3Decoder.h"
[293]66#include "RTIGS/RTIGSDecoder.h"
[1310]67#include "GPSS/gpssDecoder.h"
[1318]68#include "serial/qextserialport.h"
[35]69
70using namespace std;
71
[1143]72// Constructor 1
[35]73////////////////////////////////////////////////////////////////////////////
[1138]74bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
75 const QByteArray& format) {
76
77 _rawInpFile = new QFile(rawInpFileName);
78 _rawInpFile->open(QIODevice::ReadOnly);
[1555]79 _format = format;
80 _staID = rawInpFileName.mid(
81 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
[1147]82
[1555]83 initialize();
[1138]84}
85
[1143]86// Constructor 2
87////////////////////////////////////////////////////////////////////////////
[278]88bncGetThread::bncGetThread(const QUrl& mountPoint,
[366]89 const QByteArray& format,
90 const QByteArray& latitude,
91 const QByteArray& longitude,
[1353]92 const QByteArray& nmea,
93 const QByteArray& ntripVersion, int iMount) {
[1555]94 _rawInpFile = 0;
95 _mountPoint = mountPoint;
96 _staID = mountPoint.path().mid(1).toAscii();
97 _format = format;
98 _latitude = latitude;
99 _longitude = longitude;
100 _nmea = nmea;
[1353]101 _ntripVersion = ntripVersion;
[1555]102 _iMount = iMount; // index in mountpoints array
[1139]103
104 initialize();
105}
106
[1555]107// Initialization (common part of the constructor)
[1143]108////////////////////////////////////////////////////////////////////////////
[1139]109void bncGetThread::initialize() {
110
[1555]111 setTerminationEnabled(true);
112
[1225]113 bncApp* app = (bncApp*) qApp;
114
[1555]115 connect(this, SIGNAL(newMessage(QByteArray,bool)),
116 app, SLOT(slotMessage(const QByteArray,bool)));
117
[1525]118 _isToBeDeleted = false;
[1555]119 _decoder = 0;
120 _query = 0;
121 _nextSleep = 0;
122 _rawOutFile = 0;
123 _staID_orig = _staID;
[255]124
[1574]125 bncSettings settings;
[1595]126
[1574]127 _miscMount = settings.value("miscMount").toString();
128
[255]129 // Check name conflict
130 // -------------------
131 QListIterator<QString> it(settings.value("mountPoints").toStringList());
132 int num = 0;
[278]133 int ind = -1;
[255]134 while (it.hasNext()) {
[278]135 ++ind;
[255]136 QStringList hlp = it.next().split(" ");
137 if (hlp.size() <= 1) continue;
138 QUrl url(hlp[0]);
139 if (_mountPoint.path() == url.path()) {
[1162]140 if (_iMount > ind || _iMount < 0) {
[278]141 ++num;
[255]142 }
143 }
144 }
[278]145
146 if (num > 0) {
147 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
[255]148 }
[408]149
150 // RINEX writer
151 // ------------
152 _samplingRate = settings.value("rnxSampl").toInt();
153 if ( settings.value("rnxPath").toString().isEmpty() ) {
154 _rnx = 0;
[1555]155 if (_rawInpFile) {
156 cerr << "no RINEX path specified" << endl;
157 ::exit(1);
158 }
[408]159 }
160 else {
[1353]161 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
[1639]162 _longitude, _nmea, _ntripVersion);
[408]163 }
164
[1555]165 // Serial Port
166 // -----------
[1710]167 _serialNMEA = NO_NMEA;
168 _serialOutFile = 0;
169 _serialPort = 0;
170
[1327]171 if (settings.value("serialMountPoint").toString() == _staID) {
[1710]172 _serialPort = new QextSerialPort(settings.value("serialPortName").toString() );
173 _serialPort->setTimeout(0,100);
174
175 // Baud Rate
176 // ---------
[1330]177 QString hlp = settings.value("serialBaudRate").toString();
178 if (hlp == "110") {
179 _serialPort->setBaudRate(BAUD110);
180 }
181 else if (hlp == "300") {
182 _serialPort->setBaudRate(BAUD300);
183 }
184 else if (hlp == "600") {
185 _serialPort->setBaudRate(BAUD600);
186 }
187 else if (hlp == "1200") {
188 _serialPort->setBaudRate(BAUD1200);
189 }
190 else if (hlp == "2400") {
191 _serialPort->setBaudRate(BAUD2400);
192 }
193 else if (hlp == "4800") {
194 _serialPort->setBaudRate(BAUD4800);
195 }
196 else if (hlp == "9600") {
197 _serialPort->setBaudRate(BAUD9600);
198 }
199 else if (hlp == "19200") {
200 _serialPort->setBaudRate(BAUD19200);
201 }
202 else if (hlp == "38400") {
203 _serialPort->setBaudRate(BAUD38400);
204 }
205 else if (hlp == "57600") {
206 _serialPort->setBaudRate(BAUD57600);
207 }
208 else if (hlp == "115200") {
209 _serialPort->setBaudRate(BAUD115200);
210 }
[1710]211
212 // Parity
213 // ------
[1330]214 hlp = settings.value("serialParity").toString();
215 if (hlp == "NONE") {
216 _serialPort->setParity(PAR_NONE);
217 }
218 else if (hlp == "ODD") {
219 _serialPort->setParity(PAR_ODD);
220 }
221 else if (hlp == "EVEN") {
222 _serialPort->setParity(PAR_EVEN);
223 }
224 else if (hlp == "SPACE") {
225 _serialPort->setParity(PAR_SPACE);
226 }
[1710]227
228 // Data Bits
229 // ---------
[1330]230 hlp = settings.value("serialDataBits").toString();
231 if (hlp == "5") {
232 _serialPort->setDataBits(DATA_5);
233 }
234 else if (hlp == "6") {
235 _serialPort->setDataBits(DATA_6);
236 }
237 else if (hlp == "7") {
238 _serialPort->setDataBits(DATA_7);
239 }
240 else if (hlp == "8") {
241 _serialPort->setDataBits(DATA_8);
242 }
243 hlp = settings.value("serialStopBits").toString();
244 if (hlp == "1") {
245 _serialPort->setStopBits(STOP_1);
246 }
247 else if (hlp == "2") {
248 _serialPort->setStopBits(STOP_2);
249 }
[1710]250
251 // Flow Control
252 // ------------
253 hlp = settings.value("serialFlowControl").toString();
[1711]254 if (hlp == "XONXOFF") {
[1710]255 _serialPort->setFlowControl(FLOW_XONXOFF);
256 }
257 else if (hlp == "HARDWARE") {
258 _serialPort->setFlowControl(FLOW_HARDWARE);
259 }
[1711]260 else {
261 _serialPort->setFlowControl(FLOW_OFF);
262 }
[1710]263
264 // Open Serial Port
265 // ----------------
[1326]266 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
[1331]267 if (!_serialPort->isOpen()) {
268 delete _serialPort;
269 _serialPort = 0;
[1451]270 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
[1331]271 }
[1606]272 connect(_serialPort, SIGNAL(readyRead()),
273 this, SLOT(slotSerialReadyRead()));
274
[1710]275 // Automatic NMEA
276 // --------------
277 if (settings.value("serialAutoNMEA").toString() == "Auto") {
278 _serialNMEA = AUTO_NMEA;
279
280 QString fName = settings.value("serialFileNMEA").toString();
281 if (!fName.isEmpty()) {
282 _serialOutFile = new QFile(fName);
283 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
284 _serialOutFile->open(QIODevice::WriteOnly | QIODevice::Append);
285 }
286 else {
287 _serialOutFile->open(QIODevice::WriteOnly);
288 }
[1634]289 }
[1606]290 }
[1710]291
292 // Manual NMEA
293 // -----------
294 else {
295 _serialNMEA = MANUAL_NMEA;
[1636]296 }
[1318]297 }
298
[1137]299 // Raw Output
300 // ----------
[1140]301 // QByteArray rawOutFileName = "./" + _staID + ".raw";
302 // _rawOutFile = new QFile(rawOutFileName);
303 // _rawOutFile->open(QIODevice::WriteOnly);
[1137]304
[1555]305
306 // Instantiate the decoder
307 // -----------------------
308 if (_format.indexOf("RTCM_2") != -1) {
309 emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
310 _decoder = new RTCM2Decoder(_staID.data());
311 }
312 else if (_format.indexOf("RTCM_3") != -1) {
313 emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
314 _decoder = new RTCM3Decoder(_staID);
315 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
316 this, SIGNAL(newMessage(QByteArray,bool)));
317 }
318 else if (_format.indexOf("RTIGS") != -1) {
319 emit(newMessage(_staID + ": Get data in RTIGS format", true));
320 _decoder = new RTIGSDecoder();
321 }
322 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
323 emit(newMessage(_staID + ": Get Data in GPSS format", true));
324 _decoder = new gpssDecoder();
325 }
326 else if (_format.indexOf("ZERO") != -1) {
327 emit(newMessage(_staID + ": Get data in original format", true));
328 _decoder = new bncZeroDecoder(_staID);
329 }
330 else {
331 emit(newMessage(_staID + ": Unknown data format " + _format, true));
332 _isToBeDeleted = true;
333 delete this;
334 }
335
[1558]336 _latencyChecker = new latencyChecker(_staID);
[1557]337
[319]338 msleep(100); //sleep 0.1 sec
[35]339}
340
341// Destructor
342////////////////////////////////////////////////////////////////////////////
343bncGetThread::~bncGetThread() {
[1527]344 if (_query) {
345 _query->stop();
[1708]346 _query->deleteLater();
[1527]347 }
[617]348 delete _decoder;
[1044]349 delete _rnx;
[1138]350 delete _rawInpFile;
351 delete _rawOutFile;
[1607]352 delete _serialOutFile;
[1328]353 delete _serialPort;
[1557]354 delete _latencyChecker;
[1556]355 emit getThreadFinished(_staID);
[35]356}
357
[1390]358//
359////////////////////////////////////////////////////////////////////////////
360void bncGetThread::terminate() {
[1525]361 _isToBeDeleted = true;
[1559]362 if (!isRunning()) {
363 delete this;
364 }
[1390]365}
366
[136]367// Run
368////////////////////////////////////////////////////////////////////////////
369void bncGetThread::run() {
370
[35]371 while (true) {
[629]372 try {
[1555]373 if (_isToBeDeleted) {
374 QThread::exit(0);
375 this->deleteLater();
376 return;
[629]377 }
[621]378
[1555]379 if (tryReconnect() != success) {
[1572]380 _latencyChecker->checkReconnect();
[1555]381 continue;
[621]382 }
[1555]383
384 // Delete old observations
385 // -----------------------
386 QListIterator<p_obs> itOld(_decoder->_obsList);
387 while (itOld.hasNext()) {
388 delete itOld.next();
389 }
[621]390 _decoder->_obsList.clear();
391
[1555]392 // Read Data
393 // ---------
[1377]394 QByteArray data;
395 if (_query) {
396 _query->waitForReadyRead(data);
[1138]397 }
398 else if (_rawInpFile) {
[1146]399 const qint64 maxBytes = 1024;
[1555]400 data = _rawInpFile->read(maxBytes);
401 if (data.isEmpty()) {
402 cout << "no more data" << endl;
403 ::exit(0);
404 }
[1138]405 }
[1555]406 qint64 nBytes = data.size();
[1138]407
[1555]408 // Timeout, reconnect
409 // ------------------
410 if (nBytes == 0) {
[1576]411 _latencyChecker->checkReconnect();
[1555]412 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
413 continue;
414 }
415 else {
[567]416 emit newBytes(_staID, nBytes);
[1555]417 }
[567]418
[1555]419 // Output Data
420 // -----------
421 if (_rawOutFile) {
422 _rawOutFile->write(data);
423 _rawOutFile->flush();
424 }
425 if (_serialPort) {
[1633]426 slotSerialReadyRead();
[1555]427 _serialPort->write(data);
428 }
429
430 // Decode Data
431 // -----------
432 vector<string> errmsg;
[1562]433 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
[1137]434
[1555]435 // Perform various scans and checks
436 // --------------------------------
[1562]437 _latencyChecker->checkOutage(irc == success);
438 _latencyChecker->checkObsLatency(_decoder->_obsList);
[1567]439 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
440
[1555]441 scanRTCM();
[1138]442
[1555]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;
[1218]460 }
[1555]461 if (week > obs->_o.GPSWeek) {
462 week -= 1;
463 sec += secPerWeek;
[650]464 }
[1555]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)", true) );
469 delete obs;
470 continue;
[940]471 }
[686]472 }
[1555]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);
[1271]481 }
[1555]482 _rnx->dumpEpoch(newTime);
[1271]483 }
[1555]484
485 // Emit new observation signal
486 // ---------------------------
487 bool firstObs = (obs == _decoder->_obsList.first());
488 obs->_status = t_obs::posted;
489 emit newObs(_staID, firstObs, obs);
[249]490 }
[1555]491 _decoder->_obsList.clear();
[35]492 }
[1555]493 catch (...) {
[1559]494 emit(newMessage(_staID + "bncGetThread exception", true));
495 _isToBeDeleted = true;
[1555]496 }
[35]497 }
498}
499
[136]500// Try Re-Connect
501////////////////////////////////////////////////////////////////////////////
[1555]502t_irc bncGetThread::tryReconnect() {
503
504 // Easy Return
505 // -----------
506 if (_query && _query->status() == bncNetQuery::running) {
507 _nextSleep = 0;
[1709]508 if (_rnx) {
509 _rnx->setReconnectFlag(false);
510 }
[1555]511 return success;
[408]512 }
[1555]513
514 // Start a new query
515 // -----------------
516 if (!_rawInpFile) {
517
[138]518 sleep(_nextSleep);
[1555]519 if (_nextSleep == 0) {
520 _nextSleep = 1;
[138]521 }
522 else {
[1555]523 _nextSleep = 2 * _nextSleep;
[442]524 if (_nextSleep > 256) {
525 _nextSleep = 256;
[152]526 }
[138]527 }
[1555]528
529 delete _query;
[1722]530 if (_ntripVersion == "U") {
[1721]531 _query = new bncNetQueryUdp();
532 }
[1722]533 else if (_ntripVersion == "R") {
[1555]534 _query = new bncNetQueryRtp();
535 }
[1621]536 else if (_ntripVersion == "N") {
537 _query = new bncNetQueryV0();
538 }
[1555]539 else if (_ntripVersion == "2") {
540 _query = new bncNetQueryV2();
541 }
542 else {
543 _query = new bncNetQueryV1();
544 }
[1710]545 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
546 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
[1555]547 _query->startRequest(_mountPoint, gga);
548 }
549 else {
550 _query->startRequest(_mountPoint, "");
551 }
552 if (_query->status() != bncNetQuery::running) {
553 return failure;
554 }
[138]555 }
[658]556
[1555]557 if (_rnx) {
558 _rnx->setReconnectFlag(true);
[658]559 }
[1555]560
561 return success;
[658]562}
[1044]563
[1555]564// RTCM scan output
[1044]565//////////////////////////////////////////////////////////////////////////////
[1555]566void bncGetThread::scanRTCM() {
[1044]567
[1555]568 bncSettings settings;
[1574]569 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
570
571 if ( _miscMount == _staID || _miscMount == "ALL" ) {
572
[1575]573 // RTCM message types
574 // ------------------
[1574]575 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
576 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
577 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
578 }
[1044]579
[1574]580 // RTCMv3 antenna descriptor
581 // -------------------------
582 for (int ii=0;ii<_decoder->_antType.size();ii++) {
583 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
584 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
585 }
[1555]586
[1575]587 // RTCM Antenna Coordinates
588 // ------------------------
[1574]589 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
590 QByteArray antT;
591 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
592 antT = "ARP";
593 }
594 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
595 antT = "APC";
596 }
[1575]597 QByteArray ant1, ant2, ant3;
598 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
599 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
600 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
601 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
602 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
603 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
604 if (_decoder->_antList[ii].height_f) {
605 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
606 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
607 }
[1574]608 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
609 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
610 antT));
[1218]611 }
612 }
[1044]613 }
[1575]614
[1555]615 _decoder->_typeList.clear();
616 _decoder->_antType.clear();
617 _decoder->_antList.clear();
[1044]618}
[1555]619
[1606]620// Handle Data from Serial Port
621////////////////////////////////////////////////////////////////////////////
622void bncGetThread::slotSerialReadyRead() {
[1607]623 if (_serialPort) {
[1633]624 int nb = _serialPort->bytesAvailable();
625 if (nb > 0) {
626 QByteArray data = _serialPort->read(nb);
[1711]627
628 if (_serialNMEA == AUTO_NMEA) {
629 _query->sendNMEA(data);
630 }
631
[1633]632 if (_serialOutFile) {
633 _serialOutFile->write(data);
634 _serialOutFile->flush();
635 }
[1607]636 }
637 }
[1606]638}
Note: See TracBrowser for help on using the repository browser.