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

Last change on this file since 937 was 937, 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
[590]395 QString line;
396 QByteArray allLines;
[533]397
[590]398 struct converttimeinfo cti;
399 converttime(&cti, ep->GPSweek, ep->TOC);
400 if (_rinexVers == 3) {
[894]401 line.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);
[590]405 }
406 else if (_rinexVers == 2) {
[894]407 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
[590]408 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
409 cti.minute, (double) cti.second, ep->clock_bias,
410 ep->clock_drift, ep->clock_driftrate);
411 }
412 allLines += line;
[520]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
[590]451 // Output into file
452 // ----------------
[600]453 if (printFile && _ephStreamGPS) {
[592]454 *_ephStreamGPS << allLines;
[533]455 _ephStreamGPS->flush();
[590]456 }
[589]457
[590]458 // Output into the socket
459 // ----------------------
460 if (_sockets) {
[642]461 QMutableListIterator<QTcpSocket*> is(*_sockets);
[590]462 while (is.hasNext()) {
463 QTcpSocket* sock = is.next();
464 if (sock->state() == QAbstractSocket::ConnectedState) {
[934]465 if (sock->write(allLines) == -1) {
[642]466 delete sock;
467 is.remove();
468 }
[589]469 }
[642]470 else if (sock->state() != QAbstractSocket::ConnectingState) {
471 delete sock;
472 is.remove();
473 }
[589]474 }
[517]475 }
[516]476}
477
[535]478// Print One Glonass Ephemeris
[516]479////////////////////////////////////////////////////////////////////////////
[600]480void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
[523]481
[590]482 QString line;
483 QByteArray allLines;
[523]484
[590]485 int ww = ep->GPSWeek;
486 int tow = ep->GPSTOW;
487 struct converttimeinfo cti;
[523]488
[901]489 updatetime(&ww, &tow, ep->tb*1000, 0);
[590]490 converttime(&cti, ww, tow);
[525]491
[885]492 int tk = ep->tk-3*60*60;
493 if (tk < 0) {
494 tk += 86400;
[590]495 }
[525]496
[590]497 if (_rinexVers == 3) {
[894]498 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
[590]499 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
[885]500 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
[590]501 }
502 else if (_rinexVers == 2) {
[894]503 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
[590]504 ep->almanac_number, cti.year%100, cti.month, cti.day,
505 cti.hour, cti.minute, (double) cti.second, -ep->tau,
[885]506 ep->gamma, (double) tk);
[590]507 }
508 allLines += line;
509
[894]510 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
[590]511 ep->x_velocity, ep->x_acceleration,
512 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
513 allLines += line;
514
[894]515 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
[590]516 ep->y_velocity, ep->y_acceleration,
517 (double) ep->frequency_number);
518 allLines += line;
519
[894]520 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
[590]521 ep->z_velocity, ep->z_acceleration, (double) ep->E);
522 allLines += line;
[525]523
[590]524 // Output into file
525 // ----------------
[600]526 if (printFile && _ephStreamGlonass) {
[592]527 *_ephStreamGlonass << allLines;
[533]528 _ephStreamGlonass->flush();
[517]529 }
[590]530
531 // Output into the socket
532 // ----------------------
533 if (_sockets) {
[642]534 QMutableListIterator<QTcpSocket*> is(*_sockets);
[590]535 while (is.hasNext()) {
536 QTcpSocket* sock = is.next();
537 if (sock->state() == QAbstractSocket::ConnectedState) {
[934]538 if (sock->write(allLines) == -1) {
[642]539 delete sock;
540 is.remove();
541 }
[590]542 }
[642]543 else if (sock->state() != QAbstractSocket::ConnectingState) {
544 delete sock;
545 is.remove();
546 }
[590]547 }
548 }
[516]549}
[589]550
[591]551// Set Port Number
552////////////////////////////////////////////////////////////////////////////
553void bncApp::setPort(int port) {
554 _port = port;
555 if (_port != 0) {
556 _server = new QTcpServer;
557 _server->listen(QHostAddress::Any, _port);
558 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
559 _sockets = new QList<QTcpSocket*>;
560 }
561}
562
[937]563// Set Port Number
564////////////////////////////////////////////////////////////////////////////
565void bncApp::setPortCorr(int port) {
566 _portCorr = port;
567 if (_portCorr != 0) {
568 _serverCorr = new QTcpServer;
569 _serverCorr->listen(QHostAddress::Any, _portCorr);
570 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
571 _socketsCorr = new QList<QTcpSocket*>;
572 }
573}
574
[589]575// New Connection
576////////////////////////////////////////////////////////////////////////////
577void bncApp::slotNewConnection() {
578 _sockets->push_back( _server->nextPendingConnection() );
579}
580
[937]581// New Connection
582////////////////////////////////////////////////////////////////////////////
583void bncApp::slotNewConnectionCorr() {
584 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
585}
586
[621]587//
588////////////////////////////////////////////////////////////////////////////
589void bncApp::slotQuit() {
590 cout << "bncApp::slotQuit" << endl;
591 delete _caster;
592 quit();
593}
594
[936]595//
596////////////////////////////////////////////////////////////////////////////
597void bncApp::slotNewCorrLine(QString line) {
[937]598 if (_socketsCorr) {
599 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
600 while (is.hasNext()) {
601 QTcpSocket* sock = is.next();
602 if (sock->state() == QAbstractSocket::ConnectedState) {
603 if (sock->write(line.toAscii()) == -1) {
604 delete sock;
605 is.remove();
606 }
607 }
608 else if (sock->state() != QAbstractSocket::ConnectingState) {
609 delete sock;
610 is.remove();
611 }
612 }
613 }
[936]614}
Note: See TracBrowser for help on using the repository browser.