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

Last change on this file since 1747 was 1744, checked in by weber, 16 years ago

* empty log message *

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