1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Client
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncSocket
|
---|
6 | *
|
---|
7 | * Purpose: Combines QTcpSocket (NTRIP v1) and QNetworkReply (NTRIP v2)
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 27-Dec-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 | #include <iomanip>
|
---|
19 |
|
---|
20 | #include "bncsocket.h"
|
---|
21 | #include "bncapp.h"
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | #define BNCVERSION "1.7"
|
---|
26 |
|
---|
27 | // Constructor
|
---|
28 | ////////////////////////////////////////////////////////////////////////////
|
---|
29 | bncSocket::bncSocket() {
|
---|
30 | bncApp* app = (bncApp*) qApp;
|
---|
31 | app->connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
32 | app, SLOT(slotMessage(const QByteArray,bool)));
|
---|
33 | _socket = 0;
|
---|
34 | _http = 0;
|
---|
35 | }
|
---|
36 |
|
---|
37 | // Destructor
|
---|
38 | ////////////////////////////////////////////////////////////////////////////
|
---|
39 | bncSocket::~bncSocket() {
|
---|
40 | cout << "~bncSocket" << endl;
|
---|
41 | delete _socket;
|
---|
42 | delete _http;
|
---|
43 | }
|
---|
44 |
|
---|
45 | //
|
---|
46 | ////////////////////////////////////////////////////////////////////////////
|
---|
47 | void bncSocket::connectToHost(const QString &hostName, quint16 port,
|
---|
48 | QIODevice::OpenMode mode) {
|
---|
49 | if (_socket) {
|
---|
50 | _socket->connectToHost(hostName, port, mode);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | //
|
---|
55 | ////////////////////////////////////////////////////////////////////////////
|
---|
56 | bool bncSocket::waitForConnected(int msecs) {
|
---|
57 | if (_socket) {
|
---|
58 | return _socket->waitForConnected(msecs);
|
---|
59 | }
|
---|
60 | else {
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | //
|
---|
66 | ////////////////////////////////////////////////////////////////////////////
|
---|
67 | QAbstractSocket::SocketState bncSocket::state() const {
|
---|
68 | if (_socket) {
|
---|
69 | return _socket->state();
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | return QAbstractSocket::UnconnectedState;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | //
|
---|
77 | ////////////////////////////////////////////////////////////////////////////
|
---|
78 | void bncSocket::close() {
|
---|
79 | if (_socket) {
|
---|
80 | _socket->close();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | //
|
---|
85 | ////////////////////////////////////////////////////////////////////////////
|
---|
86 | qint64 bncSocket::bytesAvailable() const {
|
---|
87 | if (_socket) {
|
---|
88 | return _socket->bytesAvailable();
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | return 0;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | //
|
---|
96 | ////////////////////////////////////////////////////////////////////////////
|
---|
97 | bool bncSocket::canReadLine() const {
|
---|
98 | if (_socket) {
|
---|
99 | return _socket->canReadLine();
|
---|
100 | }
|
---|
101 | else {
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | //
|
---|
107 | ////////////////////////////////////////////////////////////////////////////
|
---|
108 | QByteArray bncSocket::readLine(qint64 maxlen) {
|
---|
109 | if (_socket) {
|
---|
110 | return _socket->readLine(maxlen);
|
---|
111 | }
|
---|
112 | else {
|
---|
113 | return "";
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | //
|
---|
118 | ////////////////////////////////////////////////////////////////////////////
|
---|
119 | bool bncSocket::waitForReadyRead(int msecs) {
|
---|
120 | if (_socket) {
|
---|
121 | return _socket->waitForReadyRead(msecs);
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | //
|
---|
129 | ////////////////////////////////////////////////////////////////////////////
|
---|
130 | qint64 bncSocket::read(char* data, qint64 maxlen) {
|
---|
131 | if (_socket) {
|
---|
132 | return _socket->read(data, maxlen);
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | return -1;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | //
|
---|
140 | ////////////////////////////////////////////////////////////////////////////
|
---|
141 | qint64 bncSocket::write(const char* data, qint64 len) {
|
---|
142 | if (_socket) {
|
---|
143 | return _socket->write(data, len);
|
---|
144 | }
|
---|
145 | else {
|
---|
146 | return -1;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | //
|
---|
151 | ////////////////////////////////////////////////////////////////////////////
|
---|
152 | bool bncSocket::waitForBytesWritten(int msecs) {
|
---|
153 | if (_socket) {
|
---|
154 | return _socket->waitForBytesWritten(msecs);
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Connect to Caster, send the Request
|
---|
162 | ////////////////////////////////////////////////////////////////////////////
|
---|
163 | t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude,
|
---|
164 | const QByteArray& longitude, const QByteArray& nmea,
|
---|
165 | const QByteArray& ntripVersion,
|
---|
166 | int timeOut, QString& msg) {
|
---|
167 |
|
---|
168 | if (ntripVersion == "AUTO") {
|
---|
169 | emit newMessage("NTRIP Version AUTO not yet implemented", "true");
|
---|
170 | return failure;
|
---|
171 | }
|
---|
172 | else if (ntripVersion == "2") {
|
---|
173 | return request2(mountPoint, latitude, longitude, nmea, timeOut, msg);
|
---|
174 | }
|
---|
175 | else if (ntripVersion != "1") {
|
---|
176 | emit newMessage("Unknown NTRIP Version " + ntripVersion, "true");
|
---|
177 | return failure;
|
---|
178 | }
|
---|
179 |
|
---|
180 | delete _socket;
|
---|
181 | _socket = new QTcpSocket();
|
---|
182 |
|
---|
183 | // Connect the Socket
|
---|
184 | // ------------------
|
---|
185 | QSettings settings;
|
---|
186 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
187 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
188 |
|
---|
189 | if ( proxyHost.isEmpty() ) {
|
---|
190 | _socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
191 | }
|
---|
192 | else {
|
---|
193 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
194 | }
|
---|
195 | if (!_socket->waitForConnected(timeOut)) {
|
---|
196 | msg += "Connect timeout\n";
|
---|
197 | delete _socket;
|
---|
198 | _socket = 0;
|
---|
199 | return failure;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Send Request
|
---|
203 | // ------------
|
---|
204 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
205 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
206 | QByteArray userAndPwd;
|
---|
207 |
|
---|
208 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
209 | {
|
---|
210 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
211 | passW.toAscii()).toBase64() + "\r\n";
|
---|
212 | }
|
---|
213 |
|
---|
214 | QUrl hlp;
|
---|
215 | hlp.setScheme("http");
|
---|
216 | hlp.setHost(mountPoint.host());
|
---|
217 | hlp.setPort(mountPoint.port());
|
---|
218 | hlp.setPath(mountPoint.path());
|
---|
219 |
|
---|
220 | QByteArray reqStr;
|
---|
221 | if ( proxyHost.isEmpty() ) {
|
---|
222 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
223 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
|
---|
224 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
225 | + userAndPwd + "\r\n";
|
---|
226 | } else {
|
---|
227 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
|
---|
228 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
229 | + "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
230 | + userAndPwd + "\r\n";
|
---|
231 | }
|
---|
232 |
|
---|
233 | // NMEA string to handle VRS stream
|
---|
234 | // --------------------------------
|
---|
235 | double lat, lon;
|
---|
236 |
|
---|
237 | lat = strtod(latitude,NULL);
|
---|
238 | lon = strtod(longitude,NULL);
|
---|
239 |
|
---|
240 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
241 | const char* flagN="N";
|
---|
242 | const char* flagE="E";
|
---|
243 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
244 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
245 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
246 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
247 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
248 | int lat_deg = (int)lat;
|
---|
249 | double lat_min=(lat-lat_deg)*60.;
|
---|
250 | int lon_deg = (int)lon;
|
---|
251 | double lon_min=(lon-lon_deg)*60.;
|
---|
252 | int hh = 0 , mm = 0;
|
---|
253 | double ss = 0.0;
|
---|
254 | hh=ttime.hour();
|
---|
255 | mm=ttime.minute();
|
---|
256 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
257 | QString gga;
|
---|
258 | gga += "GPGGA,";
|
---|
259 | 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'));
|
---|
260 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
261 | gga += flagN;
|
---|
262 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
263 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
264 | int xori;
|
---|
265 | char XOR = 0;
|
---|
266 | char *Buff =gga.toAscii().data();
|
---|
267 | int iLen = strlen(Buff);
|
---|
268 | for (xori = 0; xori < iLen; xori++) {
|
---|
269 | XOR ^= (char)Buff[xori];
|
---|
270 | }
|
---|
271 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
272 | reqStr += "$";
|
---|
273 | reqStr += gga;
|
---|
274 | reqStr += "\r\n";
|
---|
275 | }
|
---|
276 |
|
---|
277 | msg += reqStr;
|
---|
278 |
|
---|
279 | _socket->write(reqStr, reqStr.length());
|
---|
280 |
|
---|
281 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
282 | msg += "Write timeout\n";
|
---|
283 | delete _socket;
|
---|
284 | _socket = 0;
|
---|
285 | return failure;
|
---|
286 | }
|
---|
287 |
|
---|
288 | return success;
|
---|
289 | }
|
---|
290 |
|
---|
291 | //
|
---|
292 | ////////////////////////////////////////////////////////////////////////////
|
---|
293 | void bncSocket::slotRequestFinished(int id, bool error) {
|
---|
294 | cout << "slotRequestFinished " << id << endl;
|
---|
295 | if (error) {
|
---|
296 | cout << "error: " << _http->error() << " "
|
---|
297 | << _http->errorString().toAscii().data() << endl;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | //
|
---|
302 | ////////////////////////////////////////////////////////////////////////////
|
---|
303 | void bncSocket::slotReadyRead(const QHttpResponseHeader&) {
|
---|
304 | cout << "slotReadyRead" << endl;
|
---|
305 | QByteArray buffer = _http->readAll();
|
---|
306 | cout << buffer.data();
|
---|
307 | }
|
---|
308 |
|
---|
309 | //
|
---|
310 | ////////////////////////////////////////////////////////////////////////////
|
---|
311 | void bncSocket::slotSslErrors(const QList<QSslError>&) {
|
---|
312 | cout << "slotSslError" << endl;
|
---|
313 | }
|
---|
314 |
|
---|
315 | //
|
---|
316 | ////////////////////////////////////////////////////////////////////////////
|
---|
317 | void bncSocket::slotDone(bool error) {
|
---|
318 | cout << "slotDone " << endl;
|
---|
319 | if (error) {
|
---|
320 | cout << "error: " << _http->error() << " "
|
---|
321 | << _http->errorString().toAscii().data() << endl;
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | // Connect to Caster NTRIP Version 2
|
---|
326 | ////////////////////////////////////////////////////////////////////////////
|
---|
327 | t_irc bncSocket::request2(const QUrl& url, const QByteArray& latitude,
|
---|
328 | const QByteArray& longitude, const QByteArray& nmea,
|
---|
329 | int timeOut, QString& msg) {
|
---|
330 |
|
---|
331 | delete _socket;
|
---|
332 | _socket = new QTcpSocket();
|
---|
333 |
|
---|
334 | delete _http;
|
---|
335 | _http = new QHttp();
|
---|
336 |
|
---|
337 | _http->setSocket(_socket);
|
---|
338 |
|
---|
339 | _http->setHost(url.host());
|
---|
340 |
|
---|
341 | // Network Request
|
---|
342 | // ---------------
|
---|
343 | QString path = url.path();
|
---|
344 | if (path.isEmpty()) {
|
---|
345 | path = "/";
|
---|
346 | }
|
---|
347 | QHttpRequestHeader request("GET", path);
|
---|
348 | request.addValue("Host" , url.host().toAscii());
|
---|
349 | request.addValue("Ntrip-Version", "NTRIP/2.0");
|
---|
350 | request.addValue("User-Agent" , "NTRIP BNC/" BNCVERSION);
|
---|
351 | if (!url.userName().isEmpty()) {
|
---|
352 | request.addValue("Authorization", "Basic " +
|
---|
353 | (url.userName() + ":" + url.password()).toAscii().toBase64());
|
---|
354 | }
|
---|
355 | request.addValue("Connection" , "close");
|
---|
356 |
|
---|
357 |
|
---|
358 | connect(_http, SIGNAL(done(bool)), this, SLOT(slotDone(bool)));
|
---|
359 | connect(_http, SIGNAL(requestFinished(int, bool)),
|
---|
360 | this, SLOT(slotRequestFinished(int, bool)));
|
---|
361 | connect(_http, SIGNAL(readyRead(const QHttpResponseHeader&)),
|
---|
362 | this, SLOT(slotReadyRead(const QHttpResponseHeader&)));
|
---|
363 | connect(_http, SIGNAL(sslErrors(const QList<QSslError>&)),
|
---|
364 | this, SLOT(slotSslErrors(const QList<QSslError>&)));
|
---|
365 |
|
---|
366 | _http->request(request);
|
---|
367 |
|
---|
368 | return success;
|
---|
369 | }
|
---|