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

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

* empty log message *

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