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

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

* empty log message *

File size: 17.8 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 "bncsettings.h"
62#include "latencychecker.h"
63
64#include "RTCM/RTCM2Decoder.h"
65#include "RTCM3/RTCM3Decoder.h"
66#include "RTIGS/RTIGSDecoder.h"
67#include "GPSS/gpssDecoder.h"
68#include "serial/qextserialport.h"
69
70using namespace std;
71
72// Constructor 1
73////////////////////////////////////////////////////////////////////////////
74bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
75 const QByteArray& format) {
76
77 _rawInpFile = new QFile(rawInpFileName);
78 _rawInpFile->open(QIODevice::ReadOnly);
79 _format = format;
80 _staID = rawInpFileName.mid(
81 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
82
83 initialize();
84}
85
86// Constructor 2
87////////////////////////////////////////////////////////////////////////////
88bncGetThread::bncGetThread(const QUrl& mountPoint,
89 const QByteArray& format,
90 const QByteArray& latitude,
91 const QByteArray& longitude,
92 const QByteArray& nmea,
93 const QByteArray& ntripVersion, int iMount) {
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;
101 _ntripVersion = ntripVersion;
102 _iMount = iMount; // index in mountpoints array
103
104 initialize();
105}
106
107// Initialization (common part of the constructor)
108////////////////////////////////////////////////////////////////////////////
109void bncGetThread::initialize() {
110
111 setTerminationEnabled(true);
112
113 bncApp* app = (bncApp*) qApp;
114
115 connect(this, SIGNAL(newMessage(QByteArray,bool)),
116 app, SLOT(slotMessage(const QByteArray,bool)));
117
118 _isToBeDeleted = false;
119 _decoder = 0;
120 _query = 0;
121 _nextSleep = 0;
122 _rawOutFile = 0;
123 _staID_orig = _staID;
124
125 bncSettings settings;
126
127 _miscMount = settings.value("miscMount").toString();
128
129 // Check name conflict
130 // -------------------
131 QListIterator<QString> it(settings.value("mountPoints").toStringList());
132 int num = 0;
133 int ind = -1;
134 while (it.hasNext()) {
135 ++ind;
136 QStringList hlp = it.next().split(" ");
137 if (hlp.size() <= 1) continue;
138 QUrl url(hlp[0]);
139 if (_mountPoint.path() == url.path()) {
140 if (_iMount > ind || _iMount < 0) {
141 ++num;
142 }
143 }
144 }
145
146 if (num > 0) {
147 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
148 }
149
150 // RINEX writer
151 // ------------
152 _samplingRate = settings.value("rnxSampl").toInt();
153 if ( settings.value("rnxPath").toString().isEmpty() ) {
154 _rnx = 0;
155 if (_rawInpFile) {
156 cerr << "no RINEX path specified" << endl;
157 ::exit(1);
158 }
159 }
160 else {
161 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
162 _longitude, _nmea, _ntripVersion);
163 }
164
165 // Serial Port
166 // -----------
167 _serialNMEA = NO_NMEA;
168 _serialOutFile = 0;
169 _serialPort = 0;
170
171 if (settings.value("serialMountPoint").toString() == _staID) {
172 _serialPort = new QextSerialPort(settings.value("serialPortName").toString() );
173 _serialPort->setTimeout(0,100);
174
175 // Baud Rate
176 // ---------
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 }
211
212 // Parity
213 // ------
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 }
227
228 // Data Bits
229 // ---------
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 }
250
251 // Flow Control
252 // ------------
253 hlp = settings.value("serialFlowControl").toString();
254 if (hlp == "XONXOFF") {
255 _serialPort->setFlowControl(FLOW_XONXOFF);
256 }
257 else if (hlp == "HARDWARE") {
258 _serialPort->setFlowControl(FLOW_HARDWARE);
259 }
260 else {
261 _serialPort->setFlowControl(FLOW_OFF);
262 }
263
264 // Open Serial Port
265 // ----------------
266 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
267 if (!_serialPort->isOpen()) {
268 delete _serialPort;
269 _serialPort = 0;
270 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
271 }
272 connect(_serialPort, SIGNAL(readyRead()),
273 this, SLOT(slotSerialReadyRead()));
274
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 }
289 }
290 }
291
292 // Manual NMEA
293 // -----------
294 else {
295 _serialNMEA = MANUAL_NMEA;
296 }
297 }
298
299 // Raw Output
300 // ----------
301 // QByteArray rawOutFileName = "./" + _staID + ".raw";
302 // _rawOutFile = new QFile(rawOutFileName);
303 // _rawOutFile->open(QIODevice::WriteOnly);
304
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
336 _latencyChecker = new latencyChecker(_staID);
337
338 msleep(100); //sleep 0.1 sec
339}
340
341// Destructor
342////////////////////////////////////////////////////////////////////////////
343bncGetThread::~bncGetThread() {
344 if (_query) {
345 _query->stop();
346 _query->deleteLater();
347 }
348 delete _decoder;
349 delete _rnx;
350 delete _rawInpFile;
351 delete _rawOutFile;
352 delete _serialOutFile;
353 delete _serialPort;
354 delete _latencyChecker;
355 emit getThreadFinished(_staID);
356}
357
358//
359////////////////////////////////////////////////////////////////////////////
360void bncGetThread::terminate() {
361 _isToBeDeleted = true;
362 if (!isRunning()) {
363 delete this;
364 }
365}
366
367// Run
368////////////////////////////////////////////////////////////////////////////
369void bncGetThread::run() {
370
371 while (true) {
372 try {
373 if (_isToBeDeleted) {
374 QThread::exit(0);
375 this->deleteLater();
376 return;
377 }
378
379 if (tryReconnect() != success) {
380 _latencyChecker->checkReconnect();
381 continue;
382 }
383
384 // Delete old observations
385 // -----------------------
386 QListIterator<p_obs> itOld(_decoder->_obsList);
387 while (itOld.hasNext()) {
388 delete itOld.next();
389 }
390 _decoder->_obsList.clear();
391
392 // Read Data
393 // ---------
394 QByteArray data;
395 if (_query) {
396 _query->waitForReadyRead(data);
397 }
398 else if (_rawInpFile) {
399 const qint64 maxBytes = 1024;
400 data = _rawInpFile->read(maxBytes);
401 if (data.isEmpty()) {
402 cout << "no more data" << endl;
403 ::exit(0);
404 }
405 }
406 qint64 nBytes = data.size();
407
408 // Timeout, reconnect
409 // ------------------
410 if (nBytes == 0) {
411 _latencyChecker->checkReconnect();
412 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
413 continue;
414 }
415 else {
416 emit newBytes(_staID, nBytes);
417 }
418
419 // Output Data
420 // -----------
421 if (_rawOutFile) {
422 _rawOutFile->write(data);
423 _rawOutFile->flush();
424 }
425 if (_serialPort) {
426 slotSerialReadyRead();
427 _serialPort->write(data);
428 }
429
430 // Decode Data
431 // -----------
432 vector<string> errmsg;
433 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
434
435 // Perform various scans and checks
436 // --------------------------------
437 _latencyChecker->checkOutage(irc == success);
438 _latencyChecker->checkObsLatency(_decoder->_obsList);
439 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
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)", true) );
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 // Emit new observation signal
486 // ---------------------------
487 bool firstObs = (obs == _decoder->_obsList.first());
488 obs->_status = t_obs::posted;
489 emit newObs(_staID, firstObs, obs);
490 }
491 _decoder->_obsList.clear();
492 }
493 catch (...) {
494 emit(newMessage(_staID + "bncGetThread exception", true));
495 _isToBeDeleted = true;
496 }
497 }
498}
499
500// Try Re-Connect
501////////////////////////////////////////////////////////////////////////////
502t_irc bncGetThread::tryReconnect() {
503
504 // Easy Return
505 // -----------
506 if (_query && _query->status() == bncNetQuery::running) {
507 _nextSleep = 0;
508 if (_rnx) {
509 _rnx->setReconnectFlag(false);
510 }
511 return success;
512 }
513
514 // Start a new query
515 // -----------------
516 if (!_rawInpFile) {
517
518 sleep(_nextSleep);
519 if (_nextSleep == 0) {
520 _nextSleep = 1;
521 }
522 else {
523 _nextSleep = 2 * _nextSleep;
524 if (_nextSleep > 256) {
525 _nextSleep = 256;
526 }
527 }
528
529 delete _query;
530 if (_ntripVersion == "R") {
531 _query = new bncNetQueryUdp();
532 }
533 if (_ntripVersion == "R") {
534 _query = new bncNetQueryRtp();
535 }
536 else if (_ntripVersion == "N") {
537 _query = new bncNetQueryV0();
538 }
539 else if (_ntripVersion == "2") {
540 _query = new bncNetQueryV2();
541 }
542 else {
543 _query = new bncNetQueryV1();
544 }
545 if (_nmea == "yes" && _serialNMEA != AUTO_NMEA) {
546 QByteArray gga = ggaString(_latitude, _longitude, "100.0");
547 _query->startRequest(_mountPoint, gga);
548 }
549 else {
550 _query->startRequest(_mountPoint, "");
551 }
552 if (_query->status() != bncNetQuery::running) {
553 return failure;
554 }
555 }
556
557 if (_rnx) {
558 _rnx->setReconnectFlag(true);
559 }
560
561 return success;
562}
563
564// RTCM scan output
565//////////////////////////////////////////////////////////////////////////////
566void bncGetThread::scanRTCM() {
567
568 bncSettings settings;
569 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
570
571 if ( _miscMount == _staID || _miscMount == "ALL" ) {
572
573 // RTCM message types
574 // ------------------
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 }
579
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 }
586
587 // RTCM Antenna Coordinates
588 // ------------------------
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 }
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 }
608 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
609 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
610 antT));
611 }
612 }
613 }
614
615 _decoder->_typeList.clear();
616 _decoder->_antType.clear();
617 _decoder->_antList.clear();
618}
619
620// Handle Data from Serial Port
621////////////////////////////////////////////////////////////////////////////
622void bncGetThread::slotSerialReadyRead() {
623 if (_serialPort) {
624 int nb = _serialPort->bytesAvailable();
625 if (nb > 0) {
626 QByteArray data = _serialPort->read(nb);
627
628 if (_serialNMEA == AUTO_NMEA) {
629 _query->sendNMEA(data);
630 }
631
632 if (_serialOutFile) {
633 _serialOutFile->write(data);
634 _serialOutFile->flush();
635 }
636 }
637 }
638}
Note: See TracBrowser for help on using the repository browser.