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