source: ntrip/trunk/BNC/bncgetthread.cpp@ 442

Last change on this file since 442 was 442, checked in by weber, 17 years ago

* empty log message *

File size: 13.0 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters,
3// written by Leos Mervart.
4//
5// Copyright (C) 2006
6// German Federal Agency for Cartography and Geodesy (BKG)
7// http://www.bkg.bund.de
8// Czech Technical University Prague, Department of Advanced Geodesy
9// http://www.fsv.cvut.cz
10//
11// Email: euref-ip@bkg.bund.de
12//
13// This program is free software; you can redistribute it and/or
14// modify it under the terms of the GNU General Public License
15// as published by the Free Software Foundation, version 2.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26/* -------------------------------------------------------------------------
27 * BKG NTRIP Client
28 * -------------------------------------------------------------------------
29 *
30 * Class: bncGetThread
31 *
32 * Purpose: Thread that retrieves data from NTRIP caster
33 *
34 * Author: L. Mervart
35 *
36 * Created: 24-Dec-2005
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
42#include <stdlib.h>
43
44#include <QFile>
45#include <QTextStream>
46#include <QtNetwork>
47#include <QTime>
48
49#include "bncgetthread.h"
50#include "bnctabledlg.h"
51#include "bncapp.h"
52#include "bncutils.h"
53#include "bncrinex.h"
54#include "bnczerodecoder.h"
55
56#include "RTCM/RTCM2Decoder.h"
57#include "RTCM3/RTCM3Decoder.h"
58#include "RTIGS/RTIGSDecoder.h"
59
60using namespace std;
61
62// Constructor
63////////////////////////////////////////////////////////////////////////////
64bncGetThread::bncGetThread(const QUrl& mountPoint,
65 const QByteArray& format,
66 const QByteArray& latitude,
67 const QByteArray& longitude,
68 const QByteArray& nmea, int iMount) {
69 _decoder = 0;
70 _mountPoint = mountPoint;
71 _staID = mountPoint.path().mid(1).toAscii();
72 _staID_orig = _staID;
73 _format = format;
74 _latitude = latitude;
75 _longitude = longitude;
76 _nmea = nmea;
77 _socket = 0;
78 _timeOut = 20*1000; // 20 seconds
79 _nextSleep = 1; // 1 second
80 _iMount = iMount; // index in mountpoints array
81
82 // Check name conflict
83 // -------------------
84 QSettings settings;
85 QListIterator<QString> it(settings.value("mountPoints").toStringList());
86 int num = 0;
87 int ind = -1;
88 while (it.hasNext()) {
89 ++ind;
90 QStringList hlp = it.next().split(" ");
91 if (hlp.size() <= 1) continue;
92 QUrl url(hlp[0]);
93 if (_mountPoint.path() == url.path()) {
94 if (_iMount > ind) {
95 ++num;
96 }
97 }
98 }
99
100 if (num > 0) {
101 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
102 }
103
104 // RINEX writer
105 // ------------
106 _samplingRate = settings.value("rnxSampl").toInt();
107 if ( settings.value("rnxPath").toString().isEmpty() ) {
108 _rnx = 0;
109 }
110 else {
111 _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
112 }
113
114 msleep(100); //sleep 0.1 sec
115}
116
117// Destructor
118////////////////////////////////////////////////////////////////////////////
119bncGetThread::~bncGetThread() {
120 delete _socket;
121 delete _decoder;
122}
123
124// Connect to Caster, send the Request (static)
125////////////////////////////////////////////////////////////////////////////
126QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
127 QByteArray& latitude, QByteArray& longitude,
128 QByteArray& nmea, int timeOut,
129 QString& msg) {
130
131 // Connect the Socket
132 // ------------------
133 QSettings settings;
134 QString proxyHost = settings.value("proxyHost").toString();
135 int proxyPort = settings.value("proxyPort").toInt();
136
137 QTcpSocket* socket = new QTcpSocket();
138 if ( proxyHost.isEmpty() ) {
139 socket->connectToHost(mountPoint.host(), mountPoint.port());
140 }
141 else {
142 socket->connectToHost(proxyHost, proxyPort);
143 }
144 if (!socket->waitForConnected(timeOut)) {
145 msg += "Connect timeout\n";
146 delete socket;
147 return 0;
148 }
149
150 // Send Request
151 // ------------
152 QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
153 mountPoint.password().toAscii();
154
155 QUrl hlp;
156 hlp.setScheme("http");
157 hlp.setHost(mountPoint.host());
158 hlp.setPort(mountPoint.port());
159 hlp.setPath(mountPoint.path());
160
161 QByteArray reqStr;
162 if ( proxyHost.isEmpty() ) {
163 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
164 reqStr = "GET " + hlp.path().toAscii() +
165 " HTTP/1.0\r\n"
166 "User-Agent: NTRIP BNC 1.2b\r\n"
167 "Authorization: Basic " +
168 userAndPwd.toBase64() + "\r\n";
169 } else {
170 reqStr = "GET " + hlp.toEncoded() +
171 " HTTP/1.0\r\n"
172 "User-Agent: NTRIP BNC 1.2b\r\n"
173 "Authorization: Basic " +
174 userAndPwd.toBase64() + "\r\n";
175 }
176 if (hlp.path().indexOf(".skl") > 0) { reqStr += "Host: " + hlp.host().toAscii() + "\r\n"; }
177 reqStr += "\r\n";
178
179//////////////////////////////////////////////////////////////////
180// Additional NMEA String in request to handle VRS data streams //
181// will be ignored from standard casters //
182//////////////////////////////////////////////////////////////////
183
184 double lat, lon;
185
186 lat = strtod(latitude,NULL);
187 lon = strtod(longitude,NULL);
188
189 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
190 const char* flagN="N";
191 const char* flagE="E";
192 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
193 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
194 if (lon < -180.) {lon=(lon+360.); flagE="E";}
195 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
196 QTime ttime(QTime::currentTime());
197 int lat_deg = (int)lat;
198 double lat_min=(lat-lat_deg)*60.;
199 int lon_deg = (int)lon;
200 double lon_min=(lon-lon_deg)*60.;
201 int hh = 0 , mm = 0;
202 double ss = 0.0;
203 hh=ttime.hour();
204 mm=ttime.minute();
205 ss=(double)ttime.second()+0.001*ttime.msec();
206 QString gga;
207 gga += "GPGGA,";
208 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
209 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
210 gga += flagN;
211 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
212 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
213 int xori;
214 char XOR = 0;
215 char *Buff =gga.toAscii().data();
216 int iLen = strlen(Buff);
217 for (xori = 0; xori < iLen; xori++) {
218 XOR ^= (char)Buff[xori];
219 }
220 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
221 reqStr += "$";
222 reqStr += gga;
223 reqStr += "\r\n";
224 }
225////////////////////////////////////////////////////////////////
226
227 msg += reqStr;
228
229 socket->write(reqStr, reqStr.length());
230
231 if (!socket->waitForBytesWritten(timeOut)) {
232 msg += "Write timeout\n";
233 delete socket;
234 return 0;
235 }
236
237 return socket;
238}
239
240// Init Run
241////////////////////////////////////////////////////////////////////////////
242t_irc bncGetThread::initRun() {
243
244 // Send the Request
245 // ----------------
246 QString msg;
247
248 _socket = bncGetThread::request(_mountPoint, _latitude, _longitude, _nmea, _timeOut, msg);
249
250 //// emit(newMessage(msg.toAscii()));
251
252 if (!_socket) {
253 return failure;
254 }
255
256 // Read Caster Response
257 // --------------------
258 _socket->waitForReadyRead(_timeOut);
259 if (_socket->canReadLine()) {
260 QString line = _socket->readLine();
261 if (line.indexOf("Unauthorized") != -1) {
262 QStringList table;
263 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
264 QString net;
265 QStringListIterator it(table);
266 while (it.hasNext()) {
267 QString line = it.next();
268 if (line.indexOf("STR") == 0) {
269 QStringList tags = line.split(";");
270 if (tags.at(1) == _staID_orig) {
271 net = tags.at(7);
272 break;
273 }
274 }
275 }
276
277 QString reg;
278 it.toFront();
279 while (it.hasNext()) {
280 QString line = it.next();
281 if (line.indexOf("NET") == 0) {
282 QStringList tags = line.split(";");
283 if (tags.at(1) == net) {
284 reg = tags.at(7);
285 break;
286 }
287 }
288 }
289 emit(newMessage((_staID + ": Caster Response: " + line +
290 " Adjust User-ID and Password Register, see"
291 "\n " + reg).toAscii()));
292 return fatal;
293 }
294 if (line.indexOf("ICY 200 OK") != 0) {
295 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
296 return failure;
297 }
298 }
299 else {
300 emit(newMessage(_staID + ": Response Timeout"));
301 return failure;
302 }
303
304 // Instantiate the filter
305 // ----------------------
306 if (!_decoder) {
307 if (_format.indexOf("RTCM_2") != -1) {
308 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
309 _decoder = new RTCM2Decoder();
310 }
311 else if (_format.indexOf("RTCM_3") != -1) {
312 emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
313 _decoder = new RTCM3Decoder();
314 }
315 else if (_format.indexOf("RTIGS") != -1) {
316 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
317 _decoder = new RTIGSDecoder();
318 }
319 else if (_format.indexOf("SP3") != -1 || _format.indexOf("ZERO") != -1) {
320 emit(newMessage("Get Data: " + _staID + " in original format"));
321 _decoder = new bncZeroDecoder(_staID);
322 }
323 else {
324 emit(newMessage(_staID + ": Unknown data format " + _format));
325 return fatal;
326 }
327 }
328 return success;
329}
330
331// Run
332////////////////////////////////////////////////////////////////////////////
333void bncGetThread::run() {
334
335 t_irc irc = initRun();
336
337 if (irc == fatal) {
338 QThread::exit(1);
339 return;
340 }
341 else if (irc != success) {
342 emit(newMessage(_staID + ": initRun failed, reconnecting"));
343 tryReconnect();
344 }
345
346 // Read Incoming Data
347 // ------------------
348 while (true) {
349 try {
350 if (_socket->state() != QAbstractSocket::ConnectedState) {
351 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
352 tryReconnect();
353 }
354
355
356 _socket->waitForReadyRead(_timeOut);
357 qint64 nBytes = _socket->bytesAvailable();
358 if (nBytes > 0) {
359 char* data = new char[nBytes];
360 _socket->read(data, nBytes);
361
362 _decoder->Decode(data, nBytes);
363 delete [] data;
364
365 for (list<Observation*>::iterator it = _decoder->_obsList.begin();
366 it != _decoder->_obsList.end(); it++) {
367
368 // Check observation epoch
369 // -----------------------
370 int week;
371 double sec;
372 currentGPSWeeks(week, sec);
373
374 const double secPerWeek = 7.0 * 24.0 * 3600.0;
375 const double maxDt = 600.0;
376
377 if (week < (*it)->GPSWeek) {
378 week += 1;
379 sec -= secPerWeek;
380 }
381 if (week > (*it)->GPSWeek) {
382 week -= 1;
383 sec += secPerWeek;
384 }
385 double dt = fabs(sec - (*it)->GPSWeeks);
386 if (week != (*it)->GPSWeek || dt > maxDt) {
387 emit( newMessage("Wrong observation epoch") );
388 delete (*it);
389 continue;
390 }
391
392 // RINEX Output
393 // ------------
394 if (_rnx) {
395 long iSec = long(floor((*it)->GPSWeeks+0.5));
396 long newTime = (*it)->GPSWeek * 7*24*3600 + iSec;
397 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
398 _rnx->deepCopy(*it);
399 }
400 _rnx->dumpEpoch(newTime);
401 }
402
403 emit newObs(_staID, *it);
404 bool firstObs = (it == _decoder->_obsList.begin());
405 _global_caster->newObs(_staID, firstObs, *it);
406 }
407 _decoder->_obsList.clear();
408 }
409 else {
410 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
411 tryReconnect();
412 }
413 }
414 catch (const char* msg) {
415 emit(newMessage(_staID + msg));
416 tryReconnect();
417 }
418 }
419}
420
421// Exit
422////////////////////////////////////////////////////////////////////////////
423void bncGetThread::exit(int exitCode) {
424 if (exitCode!= 0) {
425 emit error(_staID);
426 }
427 QThread::exit(exitCode);
428 terminate();
429}
430
431// Try Re-Connect
432////////////////////////////////////////////////////////////////////////////
433void bncGetThread::tryReconnect() {
434 if (_rnx) {
435 _rnx->setReconnectFlag(true);
436 }
437 while (1) {
438 delete _socket; _socket = 0;
439 sleep(_nextSleep);
440 if ( initRun() == success ) {
441 break;
442 }
443 else {
444 _nextSleep *= 2;
445 if (_nextSleep > 256) {
446 _nextSleep = 256;
447 }
448 _nextSleep += rand() % 6;
449 }
450 }
451 _nextSleep = 1;
452}
Note: See TracBrowser for help on using the repository browser.