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

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

* empty log message *

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