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

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

* empty log message *

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