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

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