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 | }
|
---|
35 |
|
---|
36 | // Destructor
|
---|
37 | ////////////////////////////////////////////////////////////////////////////
|
---|
38 | bncSocket::~bncSocket() {
|
---|
39 | delete _socket;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //
|
---|
43 | ////////////////////////////////////////////////////////////////////////////
|
---|
44 | void bncSocket::connectToHost(const QString &hostName, quint16 port,
|
---|
45 | QIODevice::OpenMode mode) {
|
---|
46 | if (_socket) {
|
---|
47 | _socket->connectToHost(hostName, port, mode);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | //
|
---|
52 | ////////////////////////////////////////////////////////////////////////////
|
---|
53 | bool bncSocket::waitForConnected(int msecs) {
|
---|
54 | if (_socket) {
|
---|
55 | return _socket->waitForConnected(msecs);
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | //
|
---|
63 | ////////////////////////////////////////////////////////////////////////////
|
---|
64 | QAbstractSocket::SocketState bncSocket::state() const {
|
---|
65 | if (_socket) {
|
---|
66 | return _socket->state();
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | return QAbstractSocket::UnconnectedState;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | //
|
---|
74 | ////////////////////////////////////////////////////////////////////////////
|
---|
75 | void bncSocket::close() {
|
---|
76 | if (_socket) {
|
---|
77 | _socket->close();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | //
|
---|
82 | ////////////////////////////////////////////////////////////////////////////
|
---|
83 | qint64 bncSocket::bytesAvailable() const {
|
---|
84 | if (_socket) {
|
---|
85 | return _socket->bytesAvailable();
|
---|
86 | }
|
---|
87 | else {
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | //
|
---|
93 | ////////////////////////////////////////////////////////////////////////////
|
---|
94 | bool bncSocket::canReadLine() const {
|
---|
95 | if (_socket) {
|
---|
96 | return _socket->canReadLine();
|
---|
97 | }
|
---|
98 | else {
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | //
|
---|
104 | ////////////////////////////////////////////////////////////////////////////
|
---|
105 | QByteArray bncSocket::readLine(qint64 maxlen) {
|
---|
106 | if (_socket) {
|
---|
107 | return _socket->readLine(maxlen);
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | return "";
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | //
|
---|
115 | ////////////////////////////////////////////////////////////////////////////
|
---|
116 | bool bncSocket::waitForReadyRead(int msecs) {
|
---|
117 | if (_socket) {
|
---|
118 | return _socket->waitForReadyRead(msecs);
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | //
|
---|
126 | ////////////////////////////////////////////////////////////////////////////
|
---|
127 | qint64 bncSocket::read(char* data, qint64 maxlen) {
|
---|
128 | if (_socket) {
|
---|
129 | return _socket->read(data, maxlen);
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | return -1;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | //
|
---|
137 | ////////////////////////////////////////////////////////////////////////////
|
---|
138 | qint64 bncSocket::write(const char* data, qint64 len) {
|
---|
139 | if (_socket) {
|
---|
140 | return _socket->write(data, len);
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | return -1;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | //
|
---|
148 | ////////////////////////////////////////////////////////////////////////////
|
---|
149 | bool bncSocket::waitForBytesWritten(int msecs) {
|
---|
150 | if (_socket) {
|
---|
151 | return _socket->waitForBytesWritten(msecs);
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // Connect to Caster, send the Request
|
---|
159 | ////////////////////////////////////////////////////////////////////////////
|
---|
160 | t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude,
|
---|
161 | const QByteArray& longitude, const QByteArray& nmea,
|
---|
162 | const QByteArray& ntripVersion,
|
---|
163 | int timeOut, QString& msg) {
|
---|
164 |
|
---|
165 | if (ntripVersion == "AUTO") {
|
---|
166 | emit newMessage("NTRIP Version AUTO not yet implemented", "true");
|
---|
167 | return failure;
|
---|
168 | }
|
---|
169 | else if (ntripVersion == "2") {
|
---|
170 |
|
---|
171 | }
|
---|
172 | else if (ntripVersion != "1") {
|
---|
173 | emit newMessage("Unknown NTRIP Version " + ntripVersion, "true");
|
---|
174 | return failure;
|
---|
175 | }
|
---|
176 |
|
---|
177 | delete _socket;
|
---|
178 | _socket = new QTcpSocket();
|
---|
179 |
|
---|
180 | // Connect the Socket
|
---|
181 | // ------------------
|
---|
182 | QSettings settings;
|
---|
183 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
184 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
185 |
|
---|
186 | if ( proxyHost.isEmpty() ) {
|
---|
187 | _socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
188 | }
|
---|
189 | else {
|
---|
190 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
191 | }
|
---|
192 | if (!_socket->waitForConnected(timeOut)) {
|
---|
193 | msg += "Connect timeout\n";
|
---|
194 | delete _socket;
|
---|
195 | _socket = 0;
|
---|
196 | return failure;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Send Request
|
---|
200 | // ------------
|
---|
201 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
202 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
203 | QByteArray userAndPwd;
|
---|
204 |
|
---|
205 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
206 | {
|
---|
207 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
208 | passW.toAscii()).toBase64() + "\r\n";
|
---|
209 | }
|
---|
210 |
|
---|
211 | QUrl hlp;
|
---|
212 | hlp.setScheme("http");
|
---|
213 | hlp.setHost(mountPoint.host());
|
---|
214 | hlp.setPort(mountPoint.port());
|
---|
215 | hlp.setPath(mountPoint.path());
|
---|
216 |
|
---|
217 | QByteArray reqStr;
|
---|
218 | if ( proxyHost.isEmpty() ) {
|
---|
219 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
220 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
|
---|
221 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
222 | + userAndPwd + "\r\n";
|
---|
223 | } else {
|
---|
224 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
|
---|
225 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
226 | + "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
227 | + userAndPwd + "\r\n";
|
---|
228 | }
|
---|
229 |
|
---|
230 | // NMEA string to handle VRS stream
|
---|
231 | // --------------------------------
|
---|
232 | double lat, lon;
|
---|
233 |
|
---|
234 | lat = strtod(latitude,NULL);
|
---|
235 | lon = strtod(longitude,NULL);
|
---|
236 |
|
---|
237 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
238 | const char* flagN="N";
|
---|
239 | const char* flagE="E";
|
---|
240 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
241 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
242 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
243 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
244 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
245 | int lat_deg = (int)lat;
|
---|
246 | double lat_min=(lat-lat_deg)*60.;
|
---|
247 | int lon_deg = (int)lon;
|
---|
248 | double lon_min=(lon-lon_deg)*60.;
|
---|
249 | int hh = 0 , mm = 0;
|
---|
250 | double ss = 0.0;
|
---|
251 | hh=ttime.hour();
|
---|
252 | mm=ttime.minute();
|
---|
253 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
254 | QString gga;
|
---|
255 | gga += "GPGGA,";
|
---|
256 | 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'));
|
---|
257 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
258 | gga += flagN;
|
---|
259 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
260 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
261 | int xori;
|
---|
262 | char XOR = 0;
|
---|
263 | char *Buff =gga.toAscii().data();
|
---|
264 | int iLen = strlen(Buff);
|
---|
265 | for (xori = 0; xori < iLen; xori++) {
|
---|
266 | XOR ^= (char)Buff[xori];
|
---|
267 | }
|
---|
268 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
269 | reqStr += "$";
|
---|
270 | reqStr += gga;
|
---|
271 | reqStr += "\r\n";
|
---|
272 | }
|
---|
273 |
|
---|
274 | msg += reqStr;
|
---|
275 |
|
---|
276 | _socket->write(reqStr, reqStr.length());
|
---|
277 |
|
---|
278 | if (!_socket->waitForBytesWritten(timeOut)) {
|
---|
279 | msg += "Write timeout\n";
|
---|
280 | delete _socket;
|
---|
281 | _socket = 0;
|
---|
282 | return failure;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return success;
|
---|
286 | }
|
---|
287 |
|
---|