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

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

* empty log message *

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