clangtidy corrections part 2

This commit is contained in:
2023-10-12 00:44:27 +02:00
parent 23d854a826
commit a43e2db92b
41 changed files with 2274 additions and 2485 deletions
+192 -134
View File
@@ -4,84 +4,90 @@
* @brief Contains the implementation of the class NTRIPClient.
* @version 0.1
* @date 2022-09-18
*
*
* @copyright Copyright (c) 2022
*
*
*/
#include "ntripClient.h"
NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS* gps, const char* host, uint16_t port, const char* mountPoint, const char* user, const char* password) {
this->gps = gps;
strcpy(this->host, host);
this->port = port;
strcpy(this->mountPoint, mountPoint);
strcpy(this->user, user);
strcpy(this->password, password);
this->ntripClient = new WiFiClient;
this->state = NTRIPClientStates::closeConnection;
this->loopDelay = 20;
NTRIPClient::NTRIPClient(SFE_UBLOX_GNSS *gps, const char *host, uint16_t port, const char *mountPoint, const char *user, const char *password)
: gps{gps}, port{port}, host{host}, mountPoint{mountPoint}, user{user}, password{password}, ntripClient{new WiFiClient}, state{NTRIPClientStates::closeConnection}
{
Component::loopDelay = NTRIPClient::loopDelay;
}
NTRIPClient::~NTRIPClient() {
NTRIPClient::~NTRIPClient()
{
delete this->ntripClient;
}
void NTRIPClient::run() {
switch (this->state) {
case NTRIPClientStates::openConnection:
if (!this->activated) {
this->state = NTRIPClientStates::closeConnection;
break;
}
std::cout << "Connecting to the NTRIP caster..." << std::endl;
if (this->beginClient()) {
std::cout << "Connected to the NTRIP caster!" << std::endl;
this->state = NTRIPClientStates::pushData;
} else {
std::cout << "Failed!" << std::endl;
this->state = NTRIPClientStates::wait;
this->activated = false;
}
break;
case NTRIPClientStates::pushData:
if (!processConnection() || !this->activated)
this->state = NTRIPClientStates::closeConnection;
break;
case NTRIPClientStates::closeConnection:
std::cout << "Closing the connection to the NTRIP caster..." << std::endl;
this->closeConnection();
state = NTRIPClientStates::wait;
break;
case NTRIPClientStates::wait:
if (this->activated)
this->state = NTRIPClientStates::openConnection;
else
this->checkAutoReconnect();
break;
case NTRIPClientStates::notAvailable:
break;
default:
std::cout << "Wrong state in NTRIPClient.cpp..." << std::endl;
void NTRIPClient::run()
{
switch (this->state)
{
case NTRIPClientStates::openConnection:
if (!this->activated)
{
this->state = NTRIPClientStates::closeConnection;
break;
}
std::cout << "Connecting to the NTRIP caster..." << std::endl;
if (this->beginClient())
{
std::cout << "Connected to the NTRIP caster!" << std::endl;
this->state = NTRIPClientStates::pushData;
}
else
{
std::cout << "Failed!" << std::endl;
this->state = NTRIPClientStates::wait;
this->activated = false;
}
break;
case NTRIPClientStates::pushData:
if (!processConnection() || !this->activated)
{
this->state = NTRIPClientStates::closeConnection;
}
break;
case NTRIPClientStates::closeConnection:
std::cout << "Closing the connection to the NTRIP caster..." << std::endl;
this->closeConnection();
state = NTRIPClientStates::wait;
break;
case NTRIPClientStates::wait:
if (this->activated)
{
this->state = NTRIPClientStates::openConnection;
}
else
{
this->checkAutoReconnect();
}
break;
case NTRIPClientStates::notAvailable:
break;
default:
std::cout << "Wrong state in NTRIPClient.cpp..." << std::endl;
this->state = NTRIPClientStates::closeConnection;
break;
}
}
void NTRIPClient::runAsChild() {
void NTRIPClient::runAsChild()
{
this->pushGPGGA();
}
void NTRIPClient::gpsConfiguration() {
void NTRIPClient::gpsConfiguration()
{
this->gps->setSPIOutput(COM_TYPE_UBX | COM_TYPE_NMEA);
this->gps->setPortInput(COM_PORT_SPI, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3);
// Set the differential mode - ambiguities are fixed whenever possible
@@ -90,21 +96,28 @@ void NTRIPClient::gpsConfiguration() {
this->gps->enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_SPI, 10);
}
bool NTRIPClient::setActivated(bool state) {
// std::cout << "NTRIPClient::setActivated: b - " << b << std::endl;
bool NTRIPClient::setActivated(bool state)
{
if (state && this->state != NTRIPClientStates::notAvailable)
{
this->activated = true;
}
else if (state)
{
return false;
else {
}
else
{
this->activated = false;
this->autoReconnect = false;
}
return true;
}
void NTRIPClient::setAutoReconnect(bool state) {
if (state) {
void NTRIPClient::setAutoReconnect(bool state)
{
if (state)
{
this->reconnectAttemps = 0;
this->autoReconnect = true;
return;
@@ -113,61 +126,67 @@ void NTRIPClient::setAutoReconnect(bool state) {
this->autoReconnect = false;
}
bool NTRIPClient::beginClient() {
bool NTRIPClient::beginClient()
{
static constexpr uint16_t httpError = 401;
static constexpr uint16_t httpCheck = 200;
std::cout << "Opening socket to " << this->host << std::endl;
char serverRequest[this->bufferSize];
char credentials[this->bufferSize];
if (!this->ntripClient->connect(this->host, this->port)) {
if (!static_cast<bool>(this->ntripClient->connect(static_cast<const char *>(this->host), this->port)))
{
std::cout << "Connection to caster failed" << std::endl;
return false;
} else {
std::cout << "Connected to " << this->host << " : " << this->port << std::endl;
std::cout << "Requesting NTRIP Data from mount point " << this->mountPoint << std::endl;
// Generate the server request (GET)
snprintf(serverRequest,
this->bufferSize,
"GET /%s HTTP/1.0\r\nUser-Agent: NTRIP SparkFun u-blox Client v1.0\r\n",
this->mountPoint);
// Credentials
uint8_t userCredentialsLength = strlen(this->user) + strlen(this->password) + 2;
char* userCredentials = new char[userCredentialsLength];
snprintf(userCredentials, userCredentialsLength, "%s:%s", this->user, this->password);
std::cout << "Sending credentials: " << userCredentials << std::endl;
//Encode
base64 b;
String strEncodedCredentials = b.encode(userCredentials);
delete userCredentials;
char encodedCredentials[strEncodedCredentials.length() + 1];
strEncodedCredentials.toCharArray(encodedCredentials, sizeof(encodedCredentials));
snprintf(credentials, sizeof(credentials), "Authorization: Basic %s\r\n", encodedCredentials);
}
std::cout << "Connected to " << this->host << " : " << this->port << std::endl;
std::cout << "Requesting NTRIP Data from mount point " << this->mountPoint << std::endl;
// Generate the server request (GET)
snprintf(static_cast<char *>(serverRequest),
this->bufferSize,
static_cast<const char *>("GET /%s HTTP/1.0\r\nUser-Agent: NTRIP SparkFun u-blox Client v1.0\r\n"),
this->mountPoint);
// Credentials
const uint8_t userCredentialsLength = strlen(this->user) + strlen(this->password) + 2;
auto *userCredentials = new char[userCredentialsLength];
snprintf(static_cast<char *>(userCredentials), userCredentialsLength, static_cast<const char *>("%s:%s"), this->user, this->password);
std::cout << "Sending credentials: " << userCredentials << std::endl;
// Encode
const base64 base;
const String strEncodedCredentials = base64::encode(userCredentials);
delete userCredentials;
char encodedCredentials[strEncodedCredentials.length() + 1];
strEncodedCredentials.toCharArray(static_cast<char *>(encodedCredentials), sizeof(encodedCredentials));
snprintf(credentials, sizeof(credentials), static_cast<const char *>("Authorization: Basic %s\r\n"), static_cast<const char *>(encodedCredentials));
// Add the encoded credentials to the server request
strncat(serverRequest, credentials, this->bufferSize);
strncat(serverRequest, "\r\n", this->bufferSize);
strncat(static_cast<char *>(serverRequest), static_cast<const char *>(credentials), this->bufferSize);
strncat(static_cast<char *>(serverRequest), static_cast<const char *>("\r\n"), this->bufferSize);
std::cout << "serverRequest size: "
<< strlen(serverRequest)
<< " of "
<< this->bufferSize
<< " bytes available"
<< std::endl;
std::cout << static_cast<const char *>("serverRequest size: ")
<< strlen(serverRequest)
<< static_cast<const char *>(" of ")
<< this->bufferSize
<< static_cast<const char *>(" bytes available")
<< std::endl;
// Send the server request
std::cout << "Sending server request: " << serverRequest << std::endl;
this->ntripClient->write(serverRequest, strlen(serverRequest));
//Wait up to 5 seconds for response
uint32_t lastMillis = millis();
while (!ntripClient->available()) {
if (millis() - lastMillis > this->timeOut) {
// Wait up to 5 seconds for response
const uint32_t lastMillis = millis();
while (static_cast<bool>(!ntripClient->available()))
{
if (millis() - lastMillis > this->timeOut)
{
std::cout << "Caster timed out!" << std::endl;
this->ntripClient->stop();
return false;
@@ -175,35 +194,48 @@ bool NTRIPClient::beginClient() {
delay(10);
}
//Check reply
// Check reply
uint16_t httpStatusCode = 0;
char response[this->bufferSize];
uint16_t responseIndex = 0;
while (this->ntripClient->available()) {
while (static_cast<bool>(this->ntripClient->available()))
{
if (responseIndex == sizeof(response))
{
break;
}
response[responseIndex++] = ntripClient->read();
if (httpStatusCode == 0) {
if (strstr(response, "200") != nullptr)
httpStatusCode = 200;
if (strstr(response, "401") != nullptr)
httpStatusCode = 401;
if (httpStatusCode == 0)
{
if (strstr(response, static_cast<const char *>("200")) != nullptr)
{
httpStatusCode = httpCheck;
}
if (strstr(response, static_cast<const char *>("401")) != nullptr)
{
httpStatusCode = httpError;
}
}
}
response[responseIndex] = '\0';
// std::cout << "Caster response: " << response << std::endl;
if (httpStatusCode != 200) {
std::cout << "Failed to connect to " << this->host << " - HTTP Code: " << (int) httpStatusCode
if (httpStatusCode != httpCheck)
{
std::cout << "Failed to connect to " << this->host << " - HTTP Code: " << (int)httpStatusCode
<< " Length of Response: " << responseIndex << std::endl;
if (httpStatusCode == 0)
{
std::cout << "Response: " << response << std::endl;
else if (httpStatusCode == 401)
}
else if (httpStatusCode == httpError)
{
std::cout << "Statuscode 401 - Unauthorized" << std::endl;
}
return false;
}
@@ -212,35 +244,47 @@ bool NTRIPClient::beginClient() {
return true;
}
void NTRIPClient::closeConnection() {
if (this->ntripClient->connected())
void NTRIPClient::closeConnection()
{
if (static_cast<bool>(this->ntripClient->connected()))
{
this->ntripClient->stop();
}
this->activated = false;
std::cout << "NtripClient disconnected from: " << this->host << std::endl;
}
bool NTRIPClient::processConnection() {
if (this->ntripClient->connected()) {
bool NTRIPClient::processConnection()
{
if (static_cast<bool>(this->ntripClient->connected()))
{
uint8_t rtcmData[this->bufferSize * 8];
uint16_t rtcmCount = 0;
while (this->ntripClient->available()) {
while (static_cast<bool>(this->ntripClient->available()))
{
rtcmData[rtcmCount++] = ntripClient->read();
if (rtcmCount == sizeof(rtcmData))
{
break;
}
}
if (rtcmCount > 0) {
if (rtcmCount > 0)
{
this->lastReceivedRtcmTime = millis();
this->gps->pushRawData(rtcmData, rtcmCount);
this->gps->pushRawData(static_cast<uint8_t *>(rtcmData), rtcmCount);
// std::cout << "Pushed " << rtcmCount << " RTCM bytes to ZED." << std::endl;
}
} else {
}
else
{
std::cout << "Connection to " << this->host << " dropped!" << std::endl;
return false;
}
if (millis() - this->lastReceivedRtcmTime > this->timeOut) {
if (millis() - this->lastReceivedRtcmTime > this->timeOut)
{
std::cout << "RTCM timeout!" << std::endl;
return false;
}
@@ -248,15 +292,21 @@ bool NTRIPClient::processConnection() {
return true;
}
void NTRIPClient::checkAutoReconnect() {
void NTRIPClient::checkAutoReconnect()
{
if (!this->autoReconnect)
{
return;
}
if (millis() - this->lastReconnectTime < this->reconnectDelayTime)
{
return;
}
this->lastReconnectTime = millis();
if (this->reconnectAttemps >= this->maxReconnectAttemps) {
if (this->reconnectAttemps >= this->maxReconnectAttemps)
{
this->autoReconnect = false;
return;
}
@@ -265,26 +315,34 @@ void NTRIPClient::checkAutoReconnect() {
this->reconnectAttemps++;
}
void NTRIPClient::pushGPGGA() {
void NTRIPClient::pushGPGGA()
{
if (!this->transmitLocation && !this->activated)
{
return;
}
if (millis() - this->lastGPGGAPushTime < this->pushGPGGATime)
{
return;
}
this->lastGPGGAPushTime = millis();
if (!this->ntripClient->connected())
{
std::cout << "Failed to pushing GGA to server: " << std::endl;
NMEA_GGA_data_t *data = new NMEA_GGA_data_t;
uint8_t res = this->gps->getLatestNMEAGPGGA(data);
}
auto *data = new NMEA_GGA_data_t;
const uint8_t res = this->gps->getLatestNMEAGPGGA(data);
if (res == 2)
this->ntripClient->print((const char *)data);
{
this->ntripClient->print(reinterpret_cast<const char *>(data));
}
delete data;
}
bool NTRIPClient::isConnected() {
if (this->state == NTRIPClientStates::pushData)
return true;
return false;
bool NTRIPClient::isConnected()
{
return this->state == NTRIPClientStates::pushData;
}