source: ntrip/trunk/BNC/RTCM3/RTCM3coDecoder.cpp@ 934

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

* empty log message *

File size: 6.7 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: RTCM3coDecoder
30 *
31 * Purpose: RTCM3 Clock Orbit Decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 05-May-2008
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdio.h>
42#include <math.h>
43
44#include "RTCM3coDecoder.h"
45#include "bncutils.h"
46#include "bncrinex.h"
47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
52RTCM3coDecoder::RTCM3coDecoder(const QString& fileName) {
53
54 // File Output
55 // -----------
56 QSettings settings;
57 QString path = settings.value("corrPath").toString();
58 if (!path.isEmpty()) {
59 expandEnvVar(path);
60 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
61 path += QDir::separator();
62 }
63 _fileNameSkl = path + fileName;
64 }
65 _out = 0;
66
67 // Socket Server
68 // -------------
69 int port = settings.value("corrPort").toInt();
70 if (port != 0) {
71 _server = new QTcpServer;
72 _server->listen(QHostAddress::Any, port);
73 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
74 _sockets = new QList<QTcpSocket*>;
75 }
76 else {
77 delete _sockets;
78 delete _server;
79 }
80}
81
82// Destructor
83////////////////////////////////////////////////////////////////////////////
84RTCM3coDecoder::~RTCM3coDecoder() {
85}
86
87// Reopen Output File
88////////////////////////////////////////////////////////////////////////
89void RTCM3coDecoder::reopen() {
90
91 if (!_fileNameSkl.isEmpty()) {
92
93 QSettings settings;
94
95 QDateTime datTim = QDateTime::currentDateTime().toUTC();
96
97 QString hlpStr = bncRinex::nextEpochStr(datTim,
98 settings.value("corrIntr").toString());
99
100 QString fileName = _fileNameSkl
101 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
102 + datTim.toString(".yyC");
103
104 if (_fileName == fileName) {
105 return;
106 }
107 else {
108 _fileName = fileName;
109 }
110
111 delete _out;
112 _out = new ofstream( _fileName.toAscii().data() );
113 }
114}
115
116// New Connection
117////////////////////////////////////////////////////////////////////////////
118void RTCM3coDecoder::slotNewConnection() {
119 _sockets->push_back( _server->nextPendingConnection() );
120}
121
122//
123////////////////////////////////////////////////////////////////////////////
124t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen) {
125
126 _buffer.append(buffer, bufLen);
127
128 while (true) {
129
130 memset(&_co, 0, sizeof(_co));
131
132 int bytesused = 0;
133 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
134 _buffer.size(), &bytesused);
135
136 // Not enough Data
137 // ---------------
138 if (irc == GCOBR_SHORTBUFFER ||
139 irc == GCOBR_MESSAGEEXCEEDSBUFFER) {
140 return failure;
141 }
142
143 // Message correctly decoded
144 // -------------------------
145 else if ( (irc == GCOBR_OK || irc == GCOBR_MESSAGEFOLLOWS) &&
146 bytesused > 0) {
147 reopen();
148
149 int GPSweek;
150 double GPSweeks;
151 currentGPSWeeks(GPSweek, GPSweeks);
152
153 if (_co.NumberOfGPSSat > 0) {
154 if (GPSweeks > _co.GPSEpochTime + 86400.0) {
155 GPSweek += 1;
156 }
157 else if (GPSweeks < _co.GPSEpochTime - 86400.0) {
158 GPSweek -= 1;
159 }
160 GPSweeks = _co.GPSEpochTime;
161 }
162 else {
163 double GPSdaysec = fmod(GPSweeks, 86400.0);
164 int weekDay = int((GPSweeks - GPSdaysec) / 86400.0);
165 if (GPSdaysec > _co.GLONASSEpochTime + 3600.0) {
166 weekDay += 1;
167 if (weekDay > 6) {
168 weekDay = 0;
169 GPSweek += 1;
170 }
171 }
172 else if (GPSdaysec < _co.GLONASSEpochTime - 3600.0) {
173 weekDay -= 1;
174 if (weekDay < 0) {
175 weekDay = 6;
176 GPSweek -= 1;
177 }
178 }
179 GPSweeks = weekDay * 86400.0 + _co.GLONASSEpochTime;
180 }
181
182 for(int ii = 0; ii < _co.NumberOfGPSSat; ++ii) {
183 QString line;
184 line.sprintf("%d %.1f G%2.2d %3d %8.3f %8.3f %8.3f %8.3f\n",
185 GPSweek, GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
186 _co.Sat[ii].Clock.DeltaA0,
187 _co.Sat[ii].Orbit.DeltaRadial,
188 _co.Sat[ii].Orbit.DeltaAlongTrack,
189 _co.Sat[ii].Orbit.DeltaCrossTrack);
190 printLine(line);
191 }
192 for(int ii = CLOCKORBIT_NUMGPS;
193 ii < CLOCKORBIT_NUMGPS + _co.NumberOfGLONASSSat; ++ii) {
194 QString line;
195 line.sprintf("%d %.1f R%2.2d %3d %8.3f %8.3f %8.3f %8.3f\n",
196 GPSweek, GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
197 _co.Sat[ii].Clock.DeltaA0,
198 _co.Sat[ii].Orbit.DeltaRadial,
199 _co.Sat[ii].Orbit.DeltaAlongTrack,
200 _co.Sat[ii].Orbit.DeltaCrossTrack);
201 printLine(line);
202 }
203 _buffer = _buffer.substr(bytesused);
204 return success;
205 }
206
207 // All other Cases
208 // ---------------
209 else {
210 _buffer = _buffer.substr(1);
211 }
212 }
213}
214
215//
216////////////////////////////////////////////////////////////////////////////
217void RTCM3coDecoder::printLine(const QString& line) {
218
219 if (_out) {
220 *_out << line.toAscii().data();
221 _out->flush();
222 }
223
224 if (_sockets) {
225 QMutableListIterator<QTcpSocket*> is(*_sockets);
226 while (is.hasNext()) {
227 QTcpSocket* sock = is.next();
228 if (sock->state() == QAbstractSocket::ConnectedState) {
229 if (sock->write(line.toAscii()) == -1) {
230 delete sock;
231 is.remove();
232 }
233 }
234 else if (sock->state() != QAbstractSocket::ConnectingState) {
235 delete sock;
236 is.remove();
237 }
238 }
239 }
240}
Note: See TracBrowser for help on using the repository browser.