-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNTRIPClient.cpp
138 lines (126 loc) · 3.69 KB
/
NTRIPClient.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
#include"NTRIPClient.h"
bool NTRIPClient::reqSrcTbl(char* host,int &port)
{
if(!connect(host,port)){
Serial.print("Cannot connect to ");
Serial.println(host);
return false;
}
String p;
p = String("GET /");
p = p + String(" HTTP/1.0\r\n");
p = p + String("Host: caster.centipede.fr:2101\r\n");
p = p + String("User-Agent: NTRIP Client/buchedv1.01\r\n");
p = p + String("Accept: text/html\r\n");
p = p + String("User-Agent: Mozilla/5.0\r\n");
p = p + String("Connection: keep-alive\r\n");
p = p + String("\r\n");
print(p);
unsigned long timeout = millis();
while (available() == 0) {
if (millis() - timeout > 5000) {
Serial.println("Client Timeout !");
stop();
return false;
}
delay(10);
}
char buffer[20];
readLine(buffer,sizeof(buffer));
if(strstr(buffer,"SOURCETABLE 200 OK")!=NULL)
{
Serial.print((char*)buffer);
return false;
}
return true;
}
bool NTRIPClient::reqRaw(char* host,int &port,char* mntpnt,char* user,char* psw)
{
if(!connect(host,port))return false;
String p="GET /";
String auth="";
Serial.println("Request NTRIP");
p = p + mntpnt + String(" HTTP/1.0\r\n"
"User-Agent: NTRIPClient for Arduino v1.0\r\n"
);
if (strlen(user)==0) {
p = p + String(
"Accept: */*\r\n"
"Connection: close\r\n"
);
}
else {
auth = base64::encode(String(user) + String(":") + psw);
#ifdef Debug
Serial.println(String(user) + String(":") + psw);
#endif
p = p + String("Authorization: Basic ");
p = p + auth;
p = p + String("\r\n");
}
p = p + String("\r\n");
print(p);
#ifdef Debug
Serial.println(p);
#endif
unsigned long timeout = millis();
while (available() == 0) {
if (millis() - timeout > 20000) {
Serial.println("Client Timeout !");
return false;
}
delay(10);
}
char buffer[50];
readLine(buffer,sizeof(buffer));
if(strncmp((char*)buffer,"ICY 200 OK",10))
{
Serial.print((char*)buffer);
return false;
}
return true;
}
bool NTRIPClient::reqRaw(char* host,int &port,char* mntpnt)
{
return reqRaw(host,port,mntpnt,"","");
}
int NTRIPClient::readLine(char* _buffer,int size)
{
int len = 0;
while(available()) {
_buffer[len] = read();
len++;
if(_buffer[len-1] == '\n' || len >= size) break;
}
_buffer[len]='\0';
return len;
}
void NTRIPClient::sendGGA(const char* ggaMessage, const char* host, int port, const char* user, const char* passwd, const char* mntpnt) {
if (!connected()) {
Serial.println("NTRIPClient not connected, reconnecting...");
if (!connect(host, port)) {
Serial.println("Reconnection failed");
return;
}
}
String p = String("GET /") + String(mntpnt) + String(" HTTP/1.0\r\n");
p += String("Host: ") + String(host) + String(":") + String(port) + String("\r\n");
p += String("User-Agent: NTRIP Client/buchedv1.01\r\n");
p += String("Accept: */*\r\n");
if (strlen(user) > 0) {
String auth = base64::encode(String(user) + ":" + String(passwd));
p += String("Authorization: Basic ") + auth + String("\r\n");
}
p += String("Connection: close\r\n");
p += String("\r\n");
print(p);
print(ggaMessage);
print("\r\n");
//Serial.println("NTRIPClient sent GGA: " + String(ggaMessage));
}
void NTRIPClient::setLastGGA(String gga) {
lastGGA = gga;
}
String NTRIPClient::getLastGGA() {
return lastGGA;
}