-
Notifications
You must be signed in to change notification settings - Fork 83
/
rtcmclient.cpp
271 lines (221 loc) · 7.22 KB
/
rtcmclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright 2016 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtcmclient.h"
#include "rtcm3_simple.h"
#include <QDebug>
namespace {
void rtcm_rx(uint8_t *data, int len, int type) {
if (RtcmClient::currentMsgHandler) {
QByteArray rtcm_data((const char*)data, len);
RtcmClient::currentMsgHandler->emitRtcmReceived(rtcm_data, type);
}
}
void rtcm_rx_1006(rtcm_ref_sta_pos_t *pos) {
if (RtcmClient::currentMsgHandler) {
RtcmClient::currentMsgHandler->emitRefPosReceived(
pos->lat, pos->lon, pos->height, pos->ant_height);
}
}
}
// Static member initialization
RtcmClient *RtcmClient::currentMsgHandler = 0;
bool RtcmClient::gpsOnly = false;
rtcm3_state RtcmClient::rtcmState;
RtcmClient::RtcmClient(QObject *parent) : QObject(parent)
{
mTcpSocket = new QTcpSocket(this);
mSerialPort = new QSerialPort(this);
qRegisterMetaType<rtcm_obs_header_t>("rtcm_obs_header_t");
qRegisterMetaType<rtcm_obs_t>("rtcm_obs_gps_t");
currentMsgHandler = this;
rtcm3_init_state(&rtcmState);
rtcm3_set_rx_callback(rtcm_rx, &rtcmState);
rtcm3_set_rx_callback_1005_1006(rtcm_rx_1006, &rtcmState);
connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(tcpInputDataAvailable()));
connect(mTcpSocket, SIGNAL(connected()), this, SLOT(tcpInputConnected()));
connect(mTcpSocket, SIGNAL(disconnected()),
this, SLOT(tcpInputDisconnected()));
connect(mTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(tcpInputError(QAbstractSocket::SocketError)));
connect(mSerialPort, SIGNAL(readyRead()), this, SLOT(serialDataAvailable()));
connect(mSerialPort, SIGNAL(error(QSerialPort::SerialPortError)),
this, SLOT(serialPortError(QSerialPort::SerialPortError)));
}
bool RtcmClient::connectNtrip(QString server, QString stream, QString user, QString pass, int port)
{
mNtripUser = user;
mNtripPassword = pass;
mNtripStream = stream;
mNtripServer = server;
mTcpSocket->abort();
mTcpSocket->connectToHost(server, port);
return true;
}
bool RtcmClient::connectTcp(QString server, int port)
{
mNtripUser = "";
mNtripPassword = "";
mNtripStream = "";
mNtripServer = "";
mTcpSocket->abort();
mTcpSocket->connectToHost(server, port);
return true;
}
bool RtcmClient::connectSerial(QString port, int baudrate)
{
if(mSerialPort->isOpen()) {
mSerialPort->close();
}
mSerialPort->setPortName(port);
mSerialPort->open(QIODevice::ReadWrite);
if(!mSerialPort->isOpen()) {
return false;
}
mSerialPort->setBaudRate(baudrate);
mSerialPort->setDataBits(QSerialPort::Data8);
mSerialPort->setParity(QSerialPort::NoParity);
mSerialPort->setStopBits(QSerialPort::OneStop);
mSerialPort->setFlowControl(QSerialPort::NoFlowControl);
return true;
}
bool RtcmClient::isTcpConnected()
{
// If no stream is selected we have simply connected to a TCP server.
return mTcpSocket->isOpen();
}
bool RtcmClient::isSerialConnected()
{
return mSerialPort->isOpen();
}
void RtcmClient::disconnectTcpNtrip()
{
mTcpSocket->close();
}
void RtcmClient::disconnectSerial()
{
mSerialPort->close();
}
void RtcmClient::setGpsOnly(bool isGpsOnly)
{
gpsOnly = isGpsOnly;
}
void RtcmClient::emitRtcmReceived(QByteArray data, int type, bool sync)
{
emit rtcmReceived(data, type, sync);
}
void RtcmClient::emitRefPosReceived(double lat, double lon, double height, double antenna_height)
{
emit refPosReceived(lat, lon, height, antenna_height);
}
QByteArray RtcmClient::encodeBasePos(double lat, double lon, double height, double antenna_height)
{
rtcm_ref_sta_pos_t pos;
int len;
pos.staid = 0;
pos.lat = lat;
pos.lon = lon;
pos.height = height;
pos.ant_height = antenna_height;
quint8 buffer[40];
rtcm3_encode_1006(pos, buffer, &len);
QByteArray rtcm_data((const char*)buffer, len);
return rtcm_data;
}
void RtcmClient::tcpInputConnected()
{
qDebug() << "RTCM TCP connected";
// If no stream is selected we have simply connected to a TCP server.
if (mNtripStream.size() > 0) {
QString msg;
msg += "GET /" + mNtripStream + " HTTP/1.1\r\n";
msg += "User-Agent: NTRIP " + mNtripServer + "\r\n";
if (mNtripUser.size() > 0 || mNtripPassword.size() > 0) {
QString authStr = mNtripUser + ":" + mNtripPassword;
QByteArray auth;
auth.append(authStr);
msg += "Authorization: Basic " + auth.toBase64() + "\r\n";
}
msg += "Accept: */*\r\nConnection: close\r\n";
msg += "\r\n";
mTcpSocket->write(msg.toLocal8Bit());
}
}
void RtcmClient::tcpInputDisconnected()
{
qDebug() << "RTCM TCP disconnected";
}
void RtcmClient::tcpInputDataAvailable()
{
QByteArray data = mTcpSocket->readAll();
for (int i = 0;i < data.size();i++) {
int ret = rtcm3_input_data(data.at(i), &rtcmState);
if (ret == -1 || ret == -2) {
//qWarning() << "RTCM decode error:" << ret;
}
}
}
void RtcmClient::tcpInputError(QAbstractSocket::SocketError socketError)
{
(void)socketError;
QString errorStr = mTcpSocket->errorString();
qWarning() << "RTCM TCP Error:" << errorStr;
mTcpSocket->close();
}
void RtcmClient::serialDataAvailable()
{
while (mSerialPort->bytesAvailable() > 0) {
QByteArray data = mSerialPort->readAll();
for (int i = 0;i < data.size();i++) {
int ret = rtcm3_input_data(data.at(i), &rtcmState);
if (ret == -1 || ret == -2) {
//qWarning() << "RTCM decode error:" << ret;
}
}
}
}
void RtcmClient::serialPortError(QSerialPort::SerialPortError error)
{
QString message;
switch (error) {
case QSerialPort::NoError:
break;
case QSerialPort::DeviceNotFoundError:
message = tr("Device not found");
break;
case QSerialPort::OpenError:
message = tr("Can't open device");
break;
case QSerialPort::NotOpenError:
message = tr("Not open error");
break;
case QSerialPort::ResourceError:
message = tr("Port disconnected");
break;
case QSerialPort::PermissionError:
message = tr("Permission error");
break;
case QSerialPort::UnknownError:
message = tr("Unknown error");
break;
default:
message = "Error number: " + QString::number(error);
break;
}
if(!message.isEmpty()) {
qDebug() << "Serial error:" << message;
if(mSerialPort->isOpen()) {
mSerialPort->close();
}
}
}