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

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

* empty log message *

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