|
@@ -0,0 +1,258 @@
|
|
|
+#include "ntrip_client.h"
|
|
|
+
|
|
|
+#include <memory>
|
|
|
+
|
|
|
+ntrip_client::ntrip_client(std::string strip,std::string strport,std::string strMountPoint,
|
|
|
+ std::string strUserName,std::string strPassWord)
|
|
|
+{
|
|
|
+ mstrip = strip;
|
|
|
+ mstrport = strport;
|
|
|
+ mstrMountPoint = strMountPoint;
|
|
|
+ mstrUserName = strUserName;
|
|
|
+ mstrPassWord = strPassWord;
|
|
|
+
|
|
|
+ socket_ = new QTcpSocket(this);
|
|
|
+
|
|
|
+ connect(socket_, SIGNAL(connected()), this, SLOT(connectedSlot()));
|
|
|
+ connect(socket_, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
|
|
|
+ connect(socket_, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
|
|
|
+ connect(socket_, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
|
|
+ SLOT(errorSlot(QAbstractSocket::SocketError)));
|
|
|
+
|
|
|
+ mpTimerNtrip = new QTimer(this);
|
|
|
+ connect(mpTimerNtrip,SIGNAL(timeout()),this,SLOT(onTimerNtrip()));
|
|
|
+ mpTimerNtrip->setInterval(1000);
|
|
|
+ mpTimerNtrip->start();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::setserverinfo(std::string strip, std::string strport, std::string strMountPoint, std::string strUserName, std::string strPassWord)
|
|
|
+{
|
|
|
+ mstrip = strip;
|
|
|
+ mstrport = strport;
|
|
|
+ mstrMountPoint = strMountPoint;
|
|
|
+ mstrUserName = strUserName;
|
|
|
+ mstrPassWord = strPassWord;
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::run()
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ntrip_client::connectedSlot()
|
|
|
+{
|
|
|
+ if(socket_->state() == QAbstractSocket::ConnectedState)
|
|
|
+ {
|
|
|
+ qDebug("main is connected.\n");
|
|
|
+ mFindCMState = 3;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ qDebug("main is not connected.\n");
|
|
|
+ }
|
|
|
+ isConnected_ = true;
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::disconnectedSlot()
|
|
|
+{
|
|
|
+
|
|
|
+ if(mFindCMState != -1)mFindCMState = 0;
|
|
|
+ isConnected_ = false;
|
|
|
+ socket_->close();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::readyReadSlot()
|
|
|
+{
|
|
|
+ char * str;
|
|
|
+
|
|
|
+ QByteArray message = socket_->readAll();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ switch (mFindCMState) {
|
|
|
+
|
|
|
+ case 4:
|
|
|
+ str = (char *)message.data();
|
|
|
+ if(strstr(str,"ICY 200 OK") > 0)
|
|
|
+ {
|
|
|
+ mFindCMState = 5;
|
|
|
+ qDebug("Login succcess");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(strstr(str,"HTTP 401 Unauthorized") > 0)
|
|
|
+ {
|
|
|
+ mFindCMState = -1;
|
|
|
+ qDebug("Authorize Fail. message is %s",message.data());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ emit updatentripmsg(message);
|
|
|
+#ifdef DEBUG_NTRIP_MSG
|
|
|
+ ShowNtripMsg(message);
|
|
|
+#else
|
|
|
+ qDebug("ntrip msg len is %d",message.length());
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::errorSlot(QAbstractSocket::SocketError)
|
|
|
+{
|
|
|
+
|
|
|
+ disconnectedSlot();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ntrip_client::onTimerNtrip()
|
|
|
+{
|
|
|
+ if(mbConnect == false)
|
|
|
+ {
|
|
|
+ if(mFindCMState != 0)
|
|
|
+ {
|
|
|
+ disconnectedSlot();
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ switch (mFindCMState) {
|
|
|
+ case -1:
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 0:
|
|
|
+ socket_->connectToHost(mstrip.data(),atoi(mstrport.data()));
|
|
|
+ mFindCMState = 1;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ SendUserPass();
|
|
|
+ mFindCMState = 4;
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ SendGPGGA();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::SendUserPass()
|
|
|
+{
|
|
|
+ char strSend[1000];
|
|
|
+
|
|
|
+ char strLine[100];
|
|
|
+ char strX[1000];
|
|
|
+ snprintf(strX,1000,"%s:%s",mstrUserName.data(),mstrPassWord.data());
|
|
|
+ QByteArray ba; ba.append(strX);qDebug(ba.toBase64().data());
|
|
|
+ snprintf(strSend,1000,"GET /%s HTTP/1.0\r\n",mstrMountPoint.data());
|
|
|
+ snprintf(strLine,100,"User-Agent: NTRIP GNSSInternetRadio/1.4.10\r\n");strncat(strSend,strLine,1000);
|
|
|
+ snprintf(strLine,100,"Accept: */*\r\n");strncat(strSend,strLine,1000);
|
|
|
+ snprintf(strLine,100,"Connection: close\r\n");strncat(strSend,strLine,1000);
|
|
|
+ snprintf(strLine,100,"Authorization: Basic %s\r\n",ba.toBase64().data());strncat(strSend,strLine,1000);
|
|
|
+ snprintf(strLine,100,"\r\n");strncat(strSend,strLine,1000);
|
|
|
+ socket_->write(strSend,strlen(strSend));
|
|
|
+ socket_->flush();
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::SendGPGGA()
|
|
|
+{
|
|
|
+ char strSend[1000];
|
|
|
+ double flon = mfLon;
|
|
|
+ double flat = mfLat;
|
|
|
+ double flon_deg = flon - (double)((int)flon);
|
|
|
+ double flat_deg = flat - (double)((int)flat);
|
|
|
+ flon_deg = 60.0*flon_deg;
|
|
|
+ flat_deg = 60.0*flat_deg;
|
|
|
+ flon = (double)((int)flon);
|
|
|
+ flat = (double)((int)flat);
|
|
|
+ flon = flon*100.0 + flon_deg;
|
|
|
+ flat = flat*100.0 + flat_deg;
|
|
|
+
|
|
|
+ char strgga[1000];
|
|
|
+
|
|
|
+ QDateTime dt = QDateTime::currentDateTime();
|
|
|
+ snprintf(strgga,1000,"$GPGGA,%02d%02d%02d.%03d,%11.7f,N,%11.7f,E,1,13,1.5,46.4085,M,-9.452,M,99,AAAA",
|
|
|
+ dt.time().hour(),dt.time().minute(),dt.time().second(),dt.time().msec(),flat,flon);
|
|
|
+ char check = calccheck(strgga);
|
|
|
+ snprintf(strSend,1000,"%s*%02X\r\n",strgga,check);
|
|
|
+ qDebug(strSend);
|
|
|
+ socket_->write(strSend,strlen(strSend));
|
|
|
+ socket_->flush();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::ShowNtripMsg(QByteArray ba)
|
|
|
+{
|
|
|
+ if(ba.size()<1)return;
|
|
|
+ char * strout = 0;
|
|
|
+ std::shared_ptr<char> str_ptr = std::shared_ptr<char>(new char[ba.size()*3 + 100]);
|
|
|
+ strout = str_ptr.get();
|
|
|
+ snprintf(strout,3000,"Ntrip msg len is %d info is: ",ba.length());
|
|
|
+ char strtem[10];
|
|
|
+ int nlen = ba.length();
|
|
|
+ int i;
|
|
|
+ for(i=0;i<nlen;i++)
|
|
|
+ {
|
|
|
+ snprintf(strtem,10,"%02X ",(unsigned char )ba.at(i));
|
|
|
+ strncat(strout,strtem,10);
|
|
|
+ }
|
|
|
+ qDebug(strout);
|
|
|
+}
|
|
|
+
|
|
|
+int ntrip_client::GetState()
|
|
|
+{
|
|
|
+ return mFindCMState;
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::StartConnect()
|
|
|
+{
|
|
|
+ mbConnect = true;
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::StopConnect()
|
|
|
+{
|
|
|
+ mbConnect = false;
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::SetGGA(char *strgga)
|
|
|
+{
|
|
|
+ mMutexstrgga.lock();
|
|
|
+ strncpy(mstrgga,strgga,1000);
|
|
|
+ mMutexstrgga.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void ntrip_client::SetLatLon(double flon, double flat)
|
|
|
+{
|
|
|
+ mfLon = flon;
|
|
|
+ mfLat = flat;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+char ntrip_client::calccheck(const char *strsen)
|
|
|
+{
|
|
|
+ int nlen = strnlen(strsen,1000);
|
|
|
+ int i;
|
|
|
+ char check;
|
|
|
+ check = strsen[1]^strsen[2];
|
|
|
+ for(i=3;i<nlen;i++)
|
|
|
+ {
|
|
|
+ check = check^strsen[i];
|
|
|
+ }
|
|
|
+ return check;
|
|
|
+}
|