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