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 |
|
---|
54 | #include "RTCM/RTCM2Decoder.h"
|
---|
55 | #include "RTCM3/RTCM3Decoder.h"
|
---|
56 | #include "RTIGS/RTIGSDecoder.h"
|
---|
57 |
|
---|
58 | using namespace std;
|
---|
59 |
|
---|
60 | // Constructor
|
---|
61 | ////////////////////////////////////////////////////////////////////////////
|
---|
62 | bncGetThread::bncGetThread(const QUrl& mountPoint,
|
---|
63 | const QByteArray& format, int iMount) {
|
---|
64 | _decoder = 0;
|
---|
65 | _mountPoint = mountPoint;
|
---|
66 | _staID = mountPoint.path().mid(1).toAscii();
|
---|
67 | _staID_orig = _staID;
|
---|
68 | _format = format;
|
---|
69 | _socket = 0;
|
---|
70 | _timeOut = 20*1000; // 20 seconds
|
---|
71 | _nextSleep = 1; // 1 second
|
---|
72 | _iMount = iMount; // index in mountpoints array
|
---|
73 |
|
---|
74 | // Check name conflict
|
---|
75 | // -------------------
|
---|
76 | QSettings settings;
|
---|
77 | QListIterator<QString> it(settings.value("mountPoints").toStringList());
|
---|
78 | int num = 0;
|
---|
79 | int ind = -1;
|
---|
80 | while (it.hasNext()) {
|
---|
81 | ++ind;
|
---|
82 | QStringList hlp = it.next().split(" ");
|
---|
83 | if (hlp.size() <= 1) continue;
|
---|
84 | QUrl url(hlp[0]);
|
---|
85 | if (_mountPoint.path() == url.path()) {
|
---|
86 | if (_iMount > ind) {
|
---|
87 | ++num;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (num > 0) {
|
---|
93 | _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
|
---|
94 | }
|
---|
95 | msleep(100); //sleep 0.1 sec
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Destructor
|
---|
99 | ////////////////////////////////////////////////////////////////////////////
|
---|
100 | bncGetThread::~bncGetThread() {
|
---|
101 | delete _socket;
|
---|
102 | delete _decoder;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Connect to Caster, send the Request (static)
|
---|
106 | ////////////////////////////////////////////////////////////////////////////
|
---|
107 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint, int timeOut,
|
---|
108 | QString& msg) {
|
---|
109 |
|
---|
110 | // Connect the Socket
|
---|
111 | // ------------------
|
---|
112 | QSettings settings;
|
---|
113 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
114 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
115 |
|
---|
116 | QTcpSocket* socket = new QTcpSocket();
|
---|
117 | if ( proxyHost.isEmpty() ) {
|
---|
118 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | socket->connectToHost(proxyHost, proxyPort);
|
---|
122 | }
|
---|
123 | if (!socket->waitForConnected(timeOut)) {
|
---|
124 | msg += "Connect timeout\n";
|
---|
125 | delete socket;
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Send Request
|
---|
130 | // ------------
|
---|
131 | QByteArray userAndPwd = mountPoint.userName().toAscii() + ":" +
|
---|
132 | mountPoint.password().toAscii();
|
---|
133 |
|
---|
134 | QUrl hlp;
|
---|
135 | hlp.setScheme("http");
|
---|
136 | hlp.setHost(mountPoint.host());
|
---|
137 | hlp.setPort(mountPoint.port());
|
---|
138 | hlp.setPath(mountPoint.path());
|
---|
139 |
|
---|
140 | QByteArray reqStr;
|
---|
141 | if ( proxyHost.isEmpty() ) {
|
---|
142 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
143 | reqStr = "GET " + hlp.path().toAscii() +
|
---|
144 | " HTTP/1.0\r\n"
|
---|
145 | "User-Agent: NTRIP BNC 1.1b\r\n"
|
---|
146 | "Authorization: Basic " +
|
---|
147 | userAndPwd.toBase64() + "\r\n\r\n";
|
---|
148 | } else {
|
---|
149 | reqStr = "GET " + hlp.toEncoded() +
|
---|
150 | " HTTP/1.0\r\n"
|
---|
151 | "User-Agent: NTRIP BNC 1.1b\r\n"
|
---|
152 | "Authorization: Basic " +
|
---|
153 | userAndPwd.toBase64() + "\r\n\r\n";
|
---|
154 | }
|
---|
155 |
|
---|
156 | //////////////////////////////////////////////////////////////////
|
---|
157 | // Additional NMEA String in request to handle VRS data streams //
|
---|
158 | // will be ignored from standard casters //
|
---|
159 | //////////////////////////////////////////////////////////////////
|
---|
160 |
|
---|
161 | double lat, lon;
|
---|
162 | lat = settings.value("approxLat", 0).toDouble();
|
---|
163 | lon = settings.value("approxLon", 0).toDouble();
|
---|
164 | if ((lat != 0.0) && (lon != 0.0) && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
165 | const char* flagN="N";
|
---|
166 | const char* flagE="E";
|
---|
167 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
168 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
169 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
170 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
171 | QTime ttime(QTime::currentTime());
|
---|
172 | int lat_deg = (int)lat;
|
---|
173 | double lat_min=(lat-lat_deg)*60.;
|
---|
174 | int lon_deg = (int)lon;
|
---|
175 | double lon_min=(lon-lon_deg)*60.;
|
---|
176 | int hh = 0 , mm = 0;
|
---|
177 | double ss = 0.0;
|
---|
178 | hh=ttime.hour();
|
---|
179 | mm=ttime.minute();
|
---|
180 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
181 | QString gga;
|
---|
182 | gga += "GPGGA,";
|
---|
183 | 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'));
|
---|
184 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
185 | gga += flagN;
|
---|
186 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
187 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
188 | int xori;
|
---|
189 | char XOR = 0;
|
---|
190 | char *Buff =gga.toAscii().data();
|
---|
191 | int iLen = strlen(Buff);
|
---|
192 | for (xori = 0; xori < iLen; xori++) {
|
---|
193 | XOR ^= (char)Buff[xori];
|
---|
194 | }
|
---|
195 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
196 | reqStr += "$";
|
---|
197 | reqStr += gga;
|
---|
198 | reqStr += "\r\n";
|
---|
199 | }
|
---|
200 | ////////////////////////////////////////////////////////////////
|
---|
201 |
|
---|
202 | msg += reqStr;
|
---|
203 |
|
---|
204 | socket->write(reqStr, reqStr.length());
|
---|
205 |
|
---|
206 | if (!socket->waitForBytesWritten(timeOut)) {
|
---|
207 | msg += "Write timeout\n";
|
---|
208 | delete socket;
|
---|
209 | return 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | return socket;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // Init Run
|
---|
216 | ////////////////////////////////////////////////////////////////////////////
|
---|
217 | t_irc bncGetThread::initRun() {
|
---|
218 |
|
---|
219 | // Send the Request
|
---|
220 | // ----------------
|
---|
221 | QString msg;
|
---|
222 |
|
---|
223 | _socket = bncGetThread::request(_mountPoint, _timeOut, msg);
|
---|
224 |
|
---|
225 | //// emit(newMessage(msg.toAscii()));
|
---|
226 |
|
---|
227 | if (!_socket) {
|
---|
228 | return failure;
|
---|
229 | }
|
---|
230 |
|
---|
231 | // Read Caster Response
|
---|
232 | // --------------------
|
---|
233 | _socket->waitForReadyRead(_timeOut);
|
---|
234 | if (_socket->canReadLine()) {
|
---|
235 | QString line = _socket->readLine();
|
---|
236 | if (line.indexOf("Unauthorized") != -1) {
|
---|
237 | QStringList table;
|
---|
238 | bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
|
---|
239 | QString net;
|
---|
240 | QStringListIterator it(table);
|
---|
241 | while (it.hasNext()) {
|
---|
242 | QString line = it.next();
|
---|
243 | if (line.indexOf("STR") == 0) {
|
---|
244 | QStringList tags = line.split(";");
|
---|
245 | if (tags.at(1) == _staID_orig) {
|
---|
246 | net = tags.at(7);
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | QString reg;
|
---|
253 | it.toFront();
|
---|
254 | while (it.hasNext()) {
|
---|
255 | QString line = it.next();
|
---|
256 | if (line.indexOf("NET") == 0) {
|
---|
257 | QStringList tags = line.split(";");
|
---|
258 | if (tags.at(1) == net) {
|
---|
259 | reg = tags.at(7);
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | emit(newMessage((_staID + ": Caster Response: " + line +
|
---|
265 | " Adjust User-ID and Password Register, see"
|
---|
266 | "\n " + reg).toAscii()));
|
---|
267 | return fatal;
|
---|
268 | }
|
---|
269 | if (line.indexOf("ICY 200 OK") != 0) {
|
---|
270 | emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
|
---|
271 | return failure;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | else {
|
---|
275 | emit(newMessage(_staID + ": Response Timeout"));
|
---|
276 | return failure;
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Instantiate the filter
|
---|
280 | // ----------------------
|
---|
281 | if (!_decoder) {
|
---|
282 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
283 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
---|
284 | _decoder = new RTCM2Decoder();
|
---|
285 | }
|
---|
286 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
287 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.0 format"));
|
---|
288 | _decoder = new RTCM3Decoder();
|
---|
289 | }
|
---|
290 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
291 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
---|
292 | _decoder = new RTIGSDecoder();
|
---|
293 | }
|
---|
294 | else {
|
---|
295 | emit(newMessage(_staID + ": Unknown data format " + _format));
|
---|
296 | return fatal;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | return success;
|
---|
300 | }
|
---|
301 |
|
---|
302 | // Run
|
---|
303 | ////////////////////////////////////////////////////////////////////////////
|
---|
304 | void bncGetThread::run() {
|
---|
305 |
|
---|
306 | t_irc irc = initRun();
|
---|
307 |
|
---|
308 | if (irc == fatal) {
|
---|
309 | QThread::exit(1);
|
---|
310 | return;
|
---|
311 | }
|
---|
312 | else if (irc != success) {
|
---|
313 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
---|
314 | tryReconnect();
|
---|
315 | }
|
---|
316 |
|
---|
317 | // Read Incoming Data
|
---|
318 | // ------------------
|
---|
319 | while (true) {
|
---|
320 | try {
|
---|
321 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
322 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
---|
323 | tryReconnect();
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | _socket->waitForReadyRead(_timeOut);
|
---|
328 | qint64 nBytes = _socket->bytesAvailable();
|
---|
329 | if (nBytes > 0) {
|
---|
330 | char* data = new char[nBytes];
|
---|
331 | _socket->read(data, nBytes);
|
---|
332 | _decoder->Decode(data, nBytes);
|
---|
333 | delete [] data;
|
---|
334 | for (list<Observation*>::iterator it = _decoder->_obsList.begin();
|
---|
335 | it != _decoder->_obsList.end(); it++) {
|
---|
336 |
|
---|
337 | // Check observation epoch
|
---|
338 | // -----------------------
|
---|
339 | int week;
|
---|
340 | double sec;
|
---|
341 | currentGPSWeeks(week, sec);
|
---|
342 |
|
---|
343 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
344 | const double maxDt = 600.0;
|
---|
345 |
|
---|
346 | if (week < (*it)->GPSWeek) {
|
---|
347 | week += 1;
|
---|
348 | sec -= secPerWeek;
|
---|
349 | }
|
---|
350 | if (week > (*it)->GPSWeek) {
|
---|
351 | week -= 1;
|
---|
352 | sec += secPerWeek;
|
---|
353 | }
|
---|
354 | double dt = fabs(sec - (*it)->GPSWeeks);
|
---|
355 | if (week != (*it)->GPSWeek || dt > maxDt) {
|
---|
356 | emit( newMessage("Wrong observation epoch") );
|
---|
357 | delete (*it);
|
---|
358 | continue;
|
---|
359 | }
|
---|
360 |
|
---|
361 | emit newObs(_staID, *it);
|
---|
362 | bool firstObs = (it == _decoder->_obsList.begin());
|
---|
363 | _global_caster->newObs(_staID, _mountPoint, firstObs, *it, _format);
|
---|
364 | }
|
---|
365 | _decoder->_obsList.clear();
|
---|
366 | }
|
---|
367 | else {
|
---|
368 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
---|
369 | tryReconnect();
|
---|
370 | }
|
---|
371 | }
|
---|
372 | catch (const char* msg) {
|
---|
373 | emit(newMessage(_staID + msg));
|
---|
374 | tryReconnect();
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | // Exit
|
---|
380 | ////////////////////////////////////////////////////////////////////////////
|
---|
381 | void bncGetThread::exit(int exitCode) {
|
---|
382 | if (exitCode!= 0) {
|
---|
383 | emit error(_staID);
|
---|
384 | }
|
---|
385 | QThread::exit(exitCode);
|
---|
386 | terminate();
|
---|
387 | }
|
---|
388 |
|
---|
389 | // Try Re-Connect
|
---|
390 | ////////////////////////////////////////////////////////////////////////////
|
---|
391 | void bncGetThread::tryReconnect() {
|
---|
392 | while (1) {
|
---|
393 | delete _socket; _socket = 0;
|
---|
394 | sleep(_nextSleep);
|
---|
395 | if ( initRun() == success ) {
|
---|
396 | break;
|
---|
397 | }
|
---|
398 | else {
|
---|
399 | _nextSleep *= 2;
|
---|
400 | if (_nextSleep > 128) {
|
---|
401 | _nextSleep = 128;
|
---|
402 | }
|
---|
403 | _nextSleep += rand() % 6;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | _nextSleep = 1;
|
---|
407 | }
|
---|