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

Last change on this file since 1770 was 1770, checked in by zdenek, 15 years ago

* empty log message *

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