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

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

* empty log message *

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