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

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

* empty log message *

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