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

Last change on this file since 937 was 937, 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 line;
396 QByteArray allLines;
397
398 struct converttimeinfo cti;
399 converttime(&cti, ep->GPSweek, ep->TOC);
400 if (_rinexVers == 3) {
401 line.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 else if (_rinexVers == 2) {
407 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
408 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
409 cti.minute, (double) cti.second, ep->clock_bias,
410 ep->clock_drift, ep->clock_driftrate);
411 }
412 allLines += line;
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 // Output into file
452 // ----------------
453 if (printFile && _ephStreamGPS) {
454 *_ephStreamGPS << allLines;
455 _ephStreamGPS->flush();
456 }
457
458 // Output into the socket
459 // ----------------------
460 if (_sockets) {
461 QMutableListIterator<QTcpSocket*> is(*_sockets);
462 while (is.hasNext()) {
463 QTcpSocket* sock = is.next();
464 if (sock->state() == QAbstractSocket::ConnectedState) {
465 if (sock->write(allLines) == -1) {
466 delete sock;
467 is.remove();
468 }
469 }
470 else if (sock->state() != QAbstractSocket::ConnectingState) {
471 delete sock;
472 is.remove();
473 }
474 }
475 }
476}
477
478// Print One Glonass Ephemeris
479////////////////////////////////////////////////////////////////////////////
480void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
481
482 QString line;
483 QByteArray allLines;
484
485 int ww = ep->GPSWeek;
486 int tow = ep->GPSTOW;
487 struct converttimeinfo cti;
488
489 updatetime(&ww, &tow, ep->tb*1000, 0);
490 converttime(&cti, ww, tow);
491
492 int tk = ep->tk-3*60*60;
493 if (tk < 0) {
494 tk += 86400;
495 }
496
497 if (_rinexVers == 3) {
498 line.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
499 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
500 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
501 }
502 else if (_rinexVers == 2) {
503 line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
504 ep->almanac_number, cti.year%100, cti.month, cti.day,
505 cti.hour, cti.minute, (double) cti.second, -ep->tau,
506 ep->gamma, (double) tk);
507 }
508 allLines += line;
509
510 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
511 ep->x_velocity, ep->x_acceleration,
512 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
513 allLines += line;
514
515 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
516 ep->y_velocity, ep->y_acceleration,
517 (double) ep->frequency_number);
518 allLines += line;
519
520 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
521 ep->z_velocity, ep->z_acceleration, (double) ep->E);
522 allLines += line;
523
524 // Output into file
525 // ----------------
526 if (printFile && _ephStreamGlonass) {
527 *_ephStreamGlonass << allLines;
528 _ephStreamGlonass->flush();
529 }
530
531 // Output into the socket
532 // ----------------------
533 if (_sockets) {
534 QMutableListIterator<QTcpSocket*> is(*_sockets);
535 while (is.hasNext()) {
536 QTcpSocket* sock = is.next();
537 if (sock->state() == QAbstractSocket::ConnectedState) {
538 if (sock->write(allLines) == -1) {
539 delete sock;
540 is.remove();
541 }
542 }
543 else if (sock->state() != QAbstractSocket::ConnectingState) {
544 delete sock;
545 is.remove();
546 }
547 }
548 }
549}
550
551// Set Port Number
552////////////////////////////////////////////////////////////////////////////
553void bncApp::setPort(int port) {
554 _port = port;
555 if (_port != 0) {
556 _server = new QTcpServer;
557 _server->listen(QHostAddress::Any, _port);
558 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
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 _serverCorr = new QTcpServer;
569 _serverCorr->listen(QHostAddress::Any, _portCorr);
570 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
571 _socketsCorr = new QList<QTcpSocket*>;
572 }
573}
574
575// New Connection
576////////////////////////////////////////////////////////////////////////////
577void bncApp::slotNewConnection() {
578 _sockets->push_back( _server->nextPendingConnection() );
579}
580
581// New Connection
582////////////////////////////////////////////////////////////////////////////
583void bncApp::slotNewConnectionCorr() {
584 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
585}
586
587//
588////////////////////////////////////////////////////////////////////////////
589void bncApp::slotQuit() {
590 cout << "bncApp::slotQuit" << endl;
591 delete _caster;
592 quit();
593}
594
595//
596////////////////////////////////////////////////////////////////////////////
597void bncApp::slotNewCorrLine(QString line) {
598 if (_socketsCorr) {
599 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
600 while (is.hasNext()) {
601 QTcpSocket* sock = is.next();
602 if (sock->state() == QAbstractSocket::ConnectedState) {
603 if (sock->write(line.toAscii()) == -1) {
604 delete sock;
605 is.remove();
606 }
607 }
608 else if (sock->state() != QAbstractSocket::ConnectingState) {
609 delete sock;
610 is.remove();
611 }
612 }
613 }
614}
Note: See TracBrowser for help on using the repository browser.