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

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

* empty log message *

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