source: ntrip/trunk/BNC/bncapp.cpp@ 972

Last change on this file since 972 was 972, checked in by mervart, 16 years ago

* empty log message *

File size: 17.9 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.
[82]24
25/* -------------------------------------------------------------------------
[93]26 * BKG NTRIP Client
[82]27 * -------------------------------------------------------------------------
28 *
29 * Class: bncApp
30 *
31 * Purpose: This class implements the main application
32 *
33 * Author: L. Mervart
34 *
35 * Created: 29-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
[149]42#include <QSettings>
[150]43#include <QMessageBox>
[519]44#include <cmath>
[642]45#include <unistd.h>
[82]46
47#include "bncapp.h"
[151]48#include "bncutils.h"
[647]49#include "bncrinex.h"
[82]50
51using namespace std;
52
[523]53struct converttimeinfo {
54 int second; /* seconds of GPS time [0..59] */
55 int minute; /* minutes of GPS time [0..59] */
56 int hour; /* hour of GPS time [0..24] */
57 int day; /* day of GPS time [1..28..30(31)*/
58 int month; /* month of GPS time [1..12]*/
59 int year; /* year of GPS time [1980..] */
60};
61
62extern "C" {
63 void converttime(struct converttimeinfo *c, int week, int tow);
64 void updatetime(int *week, int *tow, int tk, int fixnumleap);
65}
66
[82]67// Constructor
68////////////////////////////////////////////////////////////////////////////
69bncApp::bncApp(int argc, char* argv[], bool GUIenabled) :
70 QApplication(argc, argv, GUIenabled) {
[109]71
[841]72 _bncVersion = "BNC 1.6";
[533]73
[150]74 _logFileFlag = 0;
75 _logFile = 0;
76 _logStream = 0;
[621]77 _caster = 0;
[152]78
[516]79 // Lists of Ephemeris
80 // ------------------
81 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
82 _gpsEph[ii-PRN_GPS_START] = 0;
83 }
84 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
85 _glonassEph[ii-PRN_GLONASS_START] = 0;
86 }
87
[533]88 // Eph file(s)
89 // -----------
[534]90 _rinexVers = 0;
[533]91 _ephFileGPS = 0;
92 _ephStreamGPS = 0;
93 _ephFileGlonass = 0;
94 _ephStreamGlonass = 0;
[559]95
[591]96 _port = 0;
[589]97 _server = 0;
98 _sockets = 0;
99
[937]100 _portCorr = 0;
101 _serverCorr = 0;
102 _socketsCorr = 0;
103
[559]104 _pgmName = _bncVersion.leftJustified(20, ' ', true);
105#ifdef WIN32
106 _userName = QString("${USERNAME}");
107#else
108 _userName = QString("${USER}");
109#endif
110 expandEnvVar(_userName);
111 _userName = _userName.leftJustified(20, ' ', true);
[82]112}
113
114// Destructor
115////////////////////////////////////////////////////////////////////////////
116bncApp::~bncApp() {
[109]117 delete _logStream;
118 delete _logFile;
[533]119 delete _ephStreamGPS;
120 delete _ephFileGPS;
[589]121 delete _server;
122 delete _sockets;
[937]123 delete _serverCorr;
124 delete _socketsCorr;
[534]125 if (_rinexVers == 2) {
[533]126 delete _ephStreamGlonass;
127 delete _ephFileGlonass;
128 }
[516]129 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
130 delete _gpsEph[ii-PRN_GPS_START];
131 }
132 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
133 delete _glonassEph[ii-PRN_GLONASS_START];
134 }
[82]135}
136
137// Write a Program Message
138////////////////////////////////////////////////////////////////////////////
139void bncApp::slotMessage(const QByteArray msg) {
[150]140
[243]141 QMutexLocker locker(&_mutex);
142
[150]143 // First time resolve the log file name
144 // ------------------------------------
145 if (_logFileFlag == 0) {
146 _logFileFlag = 1;
147 QSettings settings;
148 QString logFileName = settings.value("logFile").toString();
149 if ( !logFileName.isEmpty() ) {
[151]150 expandEnvVar(logFileName);
[150]151 _logFile = new QFile(logFileName);
[275]152 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
153 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
154 }
155 else {
156 _logFile->open(QIODevice::WriteOnly);
157 }
[150]158 _logStream = new QTextStream();
159 _logStream->setDevice(_logFile);
160 }
161 }
162
[109]163 if (_logStream) {
[566]164 *_logStream << QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
[109]165 *_logStream << msg.data() << endl;
166 _logStream->flush();
[82]167 }
168}
[511]169
[535]170// New GPS Ephemeris
[511]171////////////////////////////////////////////////////////////////////////////
172void bncApp::slotNewGPSEph(gpsephemeris* gpseph) {
[516]173
174 QMutexLocker locker(&_mutex);
175
[534]176 printEphHeader();
177
[532]178 gpsephemeris** ee = &_gpsEph[gpseph->satellite-1];
[538]179 if ( *ee == 0 ||
180 gpseph->GPSweek > (*ee)->GPSweek ||
[594]181 (gpseph->GPSweek == (*ee)->GPSweek && gpseph->TOC > (*ee)->TOC) ) {
[516]182 delete *ee;
183 *ee = gpseph;
[600]184 printGPSEph(gpseph, true);
[516]185 }
186 else {
[600]187 printGPSEph(gpseph, false);
[516]188 delete gpseph;
189 }
[511]190}
191
[535]192// New Glonass Ephemeris
[511]193////////////////////////////////////////////////////////////////////////////
194void bncApp::slotNewGlonassEph(glonassephemeris* glonasseph) {
[516]195
196 QMutexLocker locker(&_mutex);
197
[534]198 printEphHeader();
199
[532]200 glonassephemeris** ee = &_glonassEph[glonasseph->almanac_number-1];
[531]201
[578]202 int wwOld, towOld, wwNew, towNew;
[577]203 if (*ee != 0) {
[578]204 wwOld = (*ee)->GPSWeek;
205 towOld = (*ee)->GPSTOW;
[901]206 updatetime(&wwOld, &towOld, (*ee)->tb*1000, 0);
[577]207
[578]208 wwNew = glonasseph->GPSWeek;
209 towNew = glonasseph->GPSTOW;
[901]210 updatetime(&wwNew, &towNew, glonasseph->tb*1000, 0);
[577]211 }
212
[578]213 if ( *ee == 0 ||
214 wwNew > wwOld ||
215 (wwNew == wwOld && towNew > towOld) ) {
[531]216 delete *ee;
217 *ee = glonasseph;
[600]218 printGlonassEph(glonasseph, true);
[531]219 }
220 else {
[600]221 printGlonassEph(glonasseph, false);
[531]222 delete glonasseph;
223 }
[511]224}
225
[535]226// Print Header of the output File(s)
[516]227////////////////////////////////////////////////////////////////////////////
228void bncApp::printEphHeader() {
[528]229
[535]230 QSettings settings;
231
[534]232 // Initialization
233 // --------------
234 if (_rinexVers == 0) {
[528]235
[533]236 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
[534]237 _rinexVers = 3;
[533]238 }
239 else {
[534]240 _rinexVers = 2;
[533]241 }
[529]242
[533]243 _ephPath = settings.value("ephPath").toString();
244
245 if ( !_ephPath.isEmpty() ) {
246 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
247 _ephPath += QDir::separator();
248 }
249 expandEnvVar(_ephPath);
250 }
[517]251 }
[533]252
[534]253 // (Re-)Open output File(s)
254 // ------------------------
[533]255 if (!_ephPath.isEmpty()) {
256
[566]257 QDateTime datTim = QDateTime::currentDateTime().toUTC();
[533]258
[583]259 QString ephFileNameGPS = _ephPath + "BRDC" +
[563]260 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'));
[533]261
[647]262 QString hlpStr = bncRinex::nextEpochStr(datTim,
263 settings.value("ephIntr").toString());
[584]264
[575]265 if (_rinexVers == 3) {
266 ephFileNameGPS += hlpStr + datTim.toString(".yyP");
267 }
268 else {
269 ephFileNameGPS += hlpStr + datTim.toString(".yyN");
270 }
[563]271
[533]272 if (_ephFileNameGPS == ephFileNameGPS) {
273 return;
274 }
275 else {
276 _ephFileNameGPS = ephFileNameGPS;
277 }
278
[575]279 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
280 delete _gpsEph[ii-PRN_GPS_START];
281 _gpsEph[ii-PRN_GPS_START] = 0;
282 }
283 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
284 delete _glonassEph[ii-PRN_GLONASS_START];
285 _glonassEph[ii-PRN_GLONASS_START] = 0;
286 }
287
[533]288 delete _ephStreamGPS;
289 delete _ephFileGPS;
290
[535]291 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
[536]292 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
293
[535]294 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
295 QFile::exists(ephFileNameGPS) ) {
296 appendFlagGPS = QIODevice::Append;
297 }
298
[533]299 _ephFileGPS = new QFile(ephFileNameGPS);
[535]300 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
[533]301 _ephStreamGPS = new QTextStream();
302 _ephStreamGPS->setDevice(_ephFileGPS);
303
[534]304 if (_rinexVers == 3) {
[533]305 _ephFileGlonass = _ephFileGPS;
306 _ephStreamGlonass = _ephStreamGPS;
307 }
[534]308 else if (_rinexVers == 2) {
[583]309 QString ephFileNameGlonass = _ephPath + "BRDC" +
[563]310 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[575]311 hlpStr + datTim.toString(".yyG");
[533]312
313 delete _ephStreamGlonass;
314 delete _ephFileGlonass;
315
[535]316 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
317 QFile::exists(ephFileNameGlonass) ) {
318 appendFlagGlonass = QIODevice::Append;
319 }
320
[533]321 _ephFileGlonass = new QFile(ephFileNameGlonass);
[535]322 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
[533]323 _ephStreamGlonass = new QTextStream();
324 _ephStreamGlonass->setDevice(_ephFileGlonass);
325 }
326
[534]327 // Header - RINEX Version 3
328 // ------------------------
329 if (_rinexVers == 3) {
[537]330 if ( ! (appendFlagGPS & QIODevice::Append)) {
331 QString line;
332 line.sprintf(
333 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
334 3.0, "", "");
335 *_ephStreamGPS << line;
336
[566]337 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
[559]338 *_ephStreamGPS << _pgmName.toAscii().data()
339 << _userName.toAscii().data()
340 << hlp.toAscii().data()
341 << "PGM / RUN BY / DATE" << endl;
342
343 line.sprintf("%60sEND OF HEADER\n", "");
[537]344 *_ephStreamGPS << line;
345
346 _ephStreamGPS->flush();
[535]347 }
[533]348 }
349
[536]350 // Headers - RINEX Version 2
351 // -------------------------
[534]352 else if (_rinexVers == 2) {
[536]353 if (! (appendFlagGPS & QIODevice::Append)) {
354 QString line;
355 line.sprintf(
[565]356 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.11, "", "");
[536]357 *_ephStreamGPS << line;
358
[566]359 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]360 *_ephStreamGPS << _pgmName.toAscii().data()
361 << _userName.toAscii().data()
362 << hlp.toAscii().data()
363 << "PGM / RUN BY / DATE" << endl;
364
365 line.sprintf("%60sEND OF HEADER\n", "");
[536]366 *_ephStreamGPS << line;
[537]367
368 _ephStreamGPS->flush();
[536]369 }
370 if (! (appendFlagGlonass & QIODevice::Append)) {
371 QString line;
372 line.sprintf(
[582]373 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.11,"","");
[536]374 *_ephStreamGlonass << line;
375
[566]376 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[559]377 *_ephStreamGlonass << _pgmName.toAscii().data()
378 << _userName.toAscii().data()
379 << hlp.toAscii().data()
380 << "PGM / RUN BY / DATE" << endl;
381
382 line.sprintf("%60sEND OF HEADER\n", "");
[536]383 *_ephStreamGlonass << line;
[537]384
385 _ephStreamGlonass->flush();
[536]386 }
[533]387 }
388 }
[516]389}
390
[535]391// Print One GPS Ephemeris
[516]392////////////////////////////////////////////////////////////////////////////
[600]393void bncApp::printGPSEph(gpsephemeris* ep, bool printFile) {
[519]394
[941]395 QString lineV2;
396 QString lineV3;
[533]397
[590]398 struct converttimeinfo cti;
399 converttime(&cti, ep->GPSweek, ep->TOC);
[941]400
401 lineV3.sprintf("G%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
[589]402 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
403 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
404 ep->clock_driftrate);
[941]405
406 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
[590]407 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
408 cti.minute, (double) cti.second, ep->clock_bias,
409 ep->clock_drift, ep->clock_driftrate);
[520]410
[941]411 QString line;
412 QByteArray allLines;
413
[894]414 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", (double)ep->IODE,
[590]415 ep->Crs, ep->Delta_n, ep->M0);
416 allLines += line;
417
[894]418 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->Cuc,
[590]419 ep->e, ep->Cus, ep->sqrt_A);
420 allLines += line;
[520]421
[894]422 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n",
[590]423 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
424 allLines += line;
425
[894]426 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->i0,
[590]427 ep->Crc, ep->omega, ep->OMEGADOT);
428 allLines += line;
[520]429
[590]430 double dd = 0;
431 unsigned long ii = ep->flags;
432 if(ii & GPSEPHF_L2CACODE)
433 dd += 2.0;
434 if(ii & GPSEPHF_L2PCODE)
435 dd += 1.0;
[894]436 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->IDOT, dd,
[590]437 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
438 allLines += line;
[520]439
[590]440 if(ep->URAindex <= 6) /* URA index */
441 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
442 else
443 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
[894]444 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", dd,
[590]445 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
446 allLines += line;
[519]447
[894]448 line.sprintf(" %18.11e %18.11e\n", ((double)ep->TOW), 0.0);
[590]449 allLines += line;
[519]450
[943]451 printOutput(printFile, _ephStreamGPS, lineV2, lineV3, allLines);
[516]452}
453
[535]454// Print One Glonass Ephemeris
[516]455////////////////////////////////////////////////////////////////////////////
[600]456void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
[523]457
[590]458 int ww = ep->GPSWeek;
459 int tow = ep->GPSTOW;
460 struct converttimeinfo cti;
[523]461
[901]462 updatetime(&ww, &tow, ep->tb*1000, 0);
[590]463 converttime(&cti, ww, tow);
[525]464
[885]465 int tk = ep->tk-3*60*60;
466 if (tk < 0) {
467 tk += 86400;
[590]468 }
[525]469
[941]470 QString lineV2;
471 QString lineV3;
472
473 lineV3.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
[590]474 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
[885]475 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
[941]476
477 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
[590]478 ep->almanac_number, cti.year%100, cti.month, cti.day,
479 cti.hour, cti.minute, (double) cti.second, -ep->tau,
[885]480 ep->gamma, (double) tk);
[590]481
[941]482 QString line;
483 QByteArray allLines;
484
[894]485 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
[590]486 ep->x_velocity, ep->x_acceleration,
487 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
488 allLines += line;
489
[894]490 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
[590]491 ep->y_velocity, ep->y_acceleration,
492 (double) ep->frequency_number);
493 allLines += line;
494
[894]495 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
[590]496 ep->z_velocity, ep->z_acceleration, (double) ep->E);
497 allLines += line;
[525]498
[943]499 printOutput(printFile, _ephStreamGlonass, lineV2, lineV3, allLines);
[941]500}
501
502// Output
503////////////////////////////////////////////////////////////////////////////
[943]504void bncApp::printOutput(bool printFile, QTextStream* stream,
505 const QString& lineV2,
[941]506 const QString& lineV3,
507 const QByteArray& allLines) {
[590]508 // Output into file
509 // ----------------
[943]510 if (printFile && stream) {
[941]511 if (_rinexVers == 2) {
[943]512 *stream << lineV2.toAscii();
[941]513 }
514 else {
[943]515 *stream << lineV3.toAscii();
[941]516 }
[943]517 *stream << allLines;
518 stream->flush();
[517]519 }
[590]520
521 // Output into the socket
522 // ----------------------
523 if (_sockets) {
[642]524 QMutableListIterator<QTcpSocket*> is(*_sockets);
[590]525 while (is.hasNext()) {
526 QTcpSocket* sock = is.next();
527 if (sock->state() == QAbstractSocket::ConnectedState) {
[941]528 if (sock->write(lineV3.toAscii()) == -1 ||
529 sock->write(allLines) == -1) {
[642]530 delete sock;
531 is.remove();
532 }
[590]533 }
[642]534 else if (sock->state() != QAbstractSocket::ConnectingState) {
535 delete sock;
536 is.remove();
537 }
[590]538 }
539 }
[516]540}
[589]541
[591]542// Set Port Number
543////////////////////////////////////////////////////////////////////////////
544void bncApp::setPort(int port) {
545 _port = port;
546 if (_port != 0) {
[942]547 delete _server;
[591]548 _server = new QTcpServer;
549 _server->listen(QHostAddress::Any, _port);
550 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
[942]551 delete _sockets;
[591]552 _sockets = new QList<QTcpSocket*>;
553 }
554}
555
[937]556// Set Port Number
557////////////////////////////////////////////////////////////////////////////
558void bncApp::setPortCorr(int port) {
559 _portCorr = port;
560 if (_portCorr != 0) {
[942]561 delete _serverCorr;
[937]562 _serverCorr = new QTcpServer;
563 _serverCorr->listen(QHostAddress::Any, _portCorr);
564 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
[942]565 delete _socketsCorr;
[937]566 _socketsCorr = new QList<QTcpSocket*>;
567 }
568}
569
[589]570// New Connection
571////////////////////////////////////////////////////////////////////////////
572void bncApp::slotNewConnection() {
573 _sockets->push_back( _server->nextPendingConnection() );
574}
575
[937]576// New Connection
577////////////////////////////////////////////////////////////////////////////
578void bncApp::slotNewConnectionCorr() {
579 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
580}
581
[621]582//
583////////////////////////////////////////////////////////////////////////////
584void bncApp::slotQuit() {
585 cout << "bncApp::slotQuit" << endl;
586 delete _caster;
587 quit();
588}
589
[936]590//
591////////////////////////////////////////////////////////////////////////////
[971]592void bncApp::slotNewCorrLine(QString line, QString staID) {
[937]593 if (_socketsCorr) {
594 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
595 while (is.hasNext()) {
596 QTcpSocket* sock = is.next();
597 if (sock->state() == QAbstractSocket::ConnectedState) {
[972]598 if (sock->write(QString(line + " " + staID + "\n").toAscii()) == -1) {
[937]599 delete sock;
600 is.remove();
601 }
602 }
603 else if (sock->state() != QAbstractSocket::ConnectingState) {
604 delete sock;
605 is.remove();
606 }
607 }
608 }
[936]609}
Note: See TracBrowser for help on using the repository browser.