From d49e03b0b7530509ec88e518253b195e957c076b Mon Sep 17 00:00:00 2001 From: Lee Leahy Date: Wed, 5 Jul 2023 08:24:21 -1000 Subject: [PATCH 1/2] Add COMPILE_NETWORK and simplify COMPILE_WIFI and COMPILE_ETHERNET --- Firmware/RTK_Surveyor/NtripClient.ino | 37 +++++++++----------------- Firmware/RTK_Surveyor/NtripServer.ino | 35 ++++++++++-------------- Firmware/RTK_Surveyor/RTK_Surveyor.ino | 6 +++++ 3 files changed, 33 insertions(+), 45 deletions(-) diff --git a/Firmware/RTK_Surveyor/NtripClient.ino b/Firmware/RTK_Surveyor/NtripClient.ino index 87e41849d..c11613e4f 100644 --- a/Firmware/RTK_Surveyor/NtripClient.ino +++ b/Firmware/RTK_Surveyor/NtripClient.ino @@ -1,3 +1,14 @@ +#if !COMPILE_NETWORK + +void ntripClientStart() +{ + systemPrintln("NTRIP Client not available: Ethernet and WiFi not compiled"); +} +void ntripClientStop(bool clientAllocated) {online.ntripClient = false;} +void ntripClientUpdate() {} + +#else // COMPILE_NETWORK + /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= NTRIP Client States: NTRIP_CLIENT_OFF: WiFi off or using NTRIP server @@ -62,9 +73,7 @@ static const int NTRIPCLIENT_MS_BETWEEN_GGA = 5000; // 5s between transmission o //---------------------------------------- // The WiFi / Ethernet connection to the NTRIP caster to obtain RTCM data. -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) static NTRIPClient *ntripClient; -#endif // COMPILE_WIFI || COMPILE_ETHERNET // Throttle the time between connection attempts // ms - Max of 4,294,967,295 or 4.3M seconds or 71,000 minutes or 1193 hours or 49 days between attempts @@ -83,7 +92,6 @@ unsigned long lastGGAPush = 0; bool ntripClientConnect() { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) if (!ntripClient) return false; @@ -157,9 +165,6 @@ bool ntripClientConnect() ntripClient->write((const uint8_t *)serverRequest, strlen(serverRequest)); ntripClientTimer = millis(); return true; -#else //! COMPILE_WIFI || COMPILE_ETHERNET - return false; -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Determine if another connection is possible or if the limit has been reached @@ -195,17 +200,12 @@ bool ntripClientConnectLimitReached() // Determine if NTRIP client data is available int ntripClientReceiveDataAvailable() { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) return ntripClient->available(); -#else //! COMPILE_WIFI || COMPILE_ETHERNET - return 0; -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Read the response from the NTRIP client void ntripClientResponse(char *response, size_t maxLength) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) char *responseEnd; // Make sure that we can zero terminate the response @@ -216,7 +216,6 @@ void ntripClientResponse(char *response, size_t maxLength) { *response++ = ntripClient->read(); } -#endif // COMPILE_WIFI || COMPILE_ETHERNET // Zero terminate the response *response = '\0'; @@ -260,7 +259,6 @@ void ntripClientSetState(NTRIPClientState newState) void ntripClientStart() { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Stop NTRIP client and WiFi ntripClientStop(true); // Do not allocate new wifiClient @@ -280,15 +278,11 @@ void ntripClientStart() } ntripClientConnectionAttempts = 0; -#else //! COMPILE_WIFI || COMPILE_ETHERNET - systemPrintln("NTRIP Client not available: Ethernet and WiFi not compiled"); -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Stop the NTRIP client void ntripClientStop(bool wifiClientAllocated) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) if (ntripClient) { // Break the NTRIP client connection if necessary @@ -324,7 +318,6 @@ void ntripClientStop(bool wifiClientAllocated) ntripClientSetState((ntripClient && (wifiClientAllocated == false)) ? NTRIP_CLIENT_ON : NTRIP_CLIENT_OFF); online.ntripClient = false; wifiIncomingRTCM = false; -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Determine if NTRIP Client is needed @@ -471,7 +464,6 @@ void ntripClientUpdate() break; case NTRIP_CLIENT_CONNECTING: -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Check for no response from the caster service if (ntripClientReceiveDataAvailable() < strlen("ICY 200 OK")) // Wait until at least a few bytes have arrived { @@ -587,11 +579,9 @@ void ntripClientUpdate() } } } -#endif // COMPILE_WIFI || COMPILE_ETHERNET break; case NTRIP_CLIENT_CONNECTED: -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Check for a broken connection if (!ntripClient->connected()) { @@ -636,14 +626,12 @@ void ntripClientUpdate() systemPrintf("NTRIP Client received %d RTCM bytes, pushed to ZED\r\n", rtcmCount); } } -#endif // COMPILE_WIFI || COMPILE_ETHERNET break; } } void pushGPGGA(NMEA_GGA_data_t *nmeaData) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Provide the caster with our current position as needed if (ntripClient->connected() && settings.ntripClient_TransmitGGA == true) { @@ -658,5 +646,6 @@ void pushGPGGA(NMEA_GGA_data_t *nmeaData) ntripClient->print((const char *)nmeaData->nmea); } } -#endif // COMPILE_WIFI || COMPILE_ETHERNET } + +#endif // COMPILE_NETWORK diff --git a/Firmware/RTK_Surveyor/NtripServer.ino b/Firmware/RTK_Surveyor/NtripServer.ino index 76332d9e8..486aac4ae 100644 --- a/Firmware/RTK_Surveyor/NtripServer.ino +++ b/Firmware/RTK_Surveyor/NtripServer.ino @@ -1,3 +1,15 @@ +#if !COMPILE_NETWORK + +void ntripServerProcessRTCM(uint8_t incoming) {} +void ntripServerStart() +{ + systemPrintln("NTRIP Server not available: Ethernet and WiFi not compiled"); +} +void ntripServerStop(bool clientAllocated) {online.ntripServer = false;} +void ntripServerUpdate() {} + +#else // COMPILE_NETWORK + /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= NTRIP Server States: NTRIP_SERVER_OFF: WiFi off or using NTRIP Client @@ -60,9 +72,7 @@ static const int MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS = 30; //---------------------------------------- // WiFi connection used to push RTCM to NTRIP caster over WiFi -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) static NTRIPClient *ntripServer; -#endif // COMPILE_WIFI || COMPILE_ETHERNET // Count of bytes sent by the NTRIP server to the NTRIP caster uint32_t ntripServerBytesSent = 0; @@ -82,7 +92,6 @@ static uint32_t ntripServerStateLastDisplayed = 0; // Initiate a connection to the NTRIP caster bool ntripServerConnectCaster() { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) const int SERVER_BUFFER_SIZE = 512; char serverBuffer[SERVER_BUFFER_SIZE]; @@ -118,9 +127,6 @@ bool ntripServerConnectCaster() // Send the authorization credentials to the NTRIP caster ntripServer->write((const uint8_t *)serverBuffer, strlen(serverBuffer)); return true; -#else // COMPILE_WIFI || COMPILE_ETHERNET - return false; -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Determine if the connection limit has been reached @@ -163,7 +169,6 @@ bool ntripServerConnectLimitReached() // Read the authorization response from the NTRIP caster void ntripServerResponse(char *response, size_t maxLength) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) char *responseEnd; // Make sure that we can zero terminate the response @@ -172,7 +177,6 @@ void ntripServerResponse(char *response, size_t maxLength) // Read bytes from the caster and store them while ((response < responseEnd) && ntripServer->available()) *response++ = ntripServer->read(); -#endif // COMPILE_WIFI || COMPILE_ETHERNET // Zero terminate the response *response = '\0'; @@ -223,7 +227,6 @@ void ntripServerSetState(NTRIPServerState newState) // This function gets called as each RTCM byte comes in void ntripServerProcessRTCM(uint8_t incoming) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) if (ntripServerState == NTRIP_SERVER_CASTING) { // Generate and print timestamp if needed @@ -272,13 +275,11 @@ void ntripServerProcessRTCM(uint8_t incoming) ntripServerSetState(NTRIP_SERVER_CONNECTING); rtcmParsingState = RTCM_TRANSPORT_STATE_WAIT_FOR_PREAMBLE_D3; } -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Start the NTRIP server void ntripServerStart() { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Stop NTRIP server and WiFi ntripServerStop(true); // Don't allocate new wifiClient @@ -297,15 +298,11 @@ void ntripServerStart() } ntripServerConnectionAttempts = 0; -#else // COMPILE_WIFI || COMPILE_ETHERNET - systemPrintln("NTRIP Server not available: Ethernet and WiFi not compiled"); -#endif // COMPILE_WIFI || COMPILE_ETHERNET } // Stop the NTRIP server void ntripServerStop(bool wifiClientAllocated) { -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) if (ntripServer) { // Break the NTRIP server connection if necessary @@ -332,8 +329,6 @@ void ntripServerStop(bool wifiClientAllocated) // Determine the next NTRIP server state ntripServerSetState((ntripServer && (wifiClientAllocated == false)) ? NTRIP_SERVER_ON : NTRIP_SERVER_OFF); -#endif // COMPILE_WIFI || COMPILE_ETHERNET - online.ntripServer = false; } @@ -486,7 +481,6 @@ void ntripServerUpdate() // Wait for authorization response case NTRIP_SERVER_AUTHORIZATION: // Check if caster service responded -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) if (ntripServer->available() < strlen("ICY 200 OK")) // Wait until at least a few bytes have arrived { // Check for response timeout @@ -570,11 +564,9 @@ void ntripServerUpdate() } } } -#endif // COMPILE_WIFI || COMPILE_ETHERNET break; // NTRIP server authorized to send RTCM correction data to NTRIP caster case NTRIP_SERVER_CASTING: -#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) // Check for a broken connection if (!ntripServer->connected()) { @@ -593,7 +585,8 @@ void ntripServerUpdate() // All is well cyclePositionLEDs(); } -#endif // COMPILE_WIFI || COMPILE_ETHERNET break; } } + +#endif // COMPILE_NETWORK diff --git a/Firmware/RTK_Surveyor/RTK_Surveyor.ino b/Firmware/RTK_Surveyor/RTK_Surveyor.ino index bc071e25d..a182e7b2b 100644 --- a/Firmware/RTK_Surveyor/RTK_Surveyor.ino +++ b/Firmware/RTK_Surveyor/RTK_Surveyor.ino @@ -29,6 +29,12 @@ // #define REF_STN_GNSS_DEBUG //Uncomment this line to output GNSS library debug messages on serialGNSS. Ref Stn only. // Needs ENABLE_DEVELOPER +#if defined(COMPILE_WIFI) || defined(COMPILE_ETHERNET) +#define COMPILE_NETWORK true +#else // COMPILE_WIFI || COMPILE_ETHERNET +#define COMPILE_NETWORK false +#endif // COMPILE_WIFI || COMPILE_ETHERNET + // Always define ENABLE_DEVELOPER to enable its use in conditional statements #ifndef ENABLE_DEVELOPER #define ENABLE_DEVELOPER \ From 4b2158883a59c5c244bd62f383891c6f4deb54f4 Mon Sep 17 00:00:00 2001 From: Lee Leahy Date: Wed, 5 Jul 2023 09:49:00 -1000 Subject: [PATCH 2/2] Switch from NTRIPClient to NetworkClient and remove COMPILE_* ifdefs --- Firmware/RTK_Surveyor/NTRIPClient.h | 260 ------------------------- Firmware/RTK_Surveyor/NetworkClient.h | 187 ++++++++++++++++++ Firmware/RTK_Surveyor/NtripClient.ino | 8 +- Firmware/RTK_Surveyor/NtripServer.ino | 8 +- Firmware/RTK_Surveyor/RTK_Surveyor.ino | 2 +- 5 files changed, 196 insertions(+), 269 deletions(-) delete mode 100644 Firmware/RTK_Surveyor/NTRIPClient.h create mode 100644 Firmware/RTK_Surveyor/NetworkClient.h diff --git a/Firmware/RTK_Surveyor/NTRIPClient.h b/Firmware/RTK_Surveyor/NTRIPClient.h deleted file mode 100644 index fb907d65e..000000000 --- a/Firmware/RTK_Surveyor/NTRIPClient.h +++ /dev/null @@ -1,260 +0,0 @@ -#if !defined(COMPILE_WIFI) && !defined(COMPILE_ETHERNET) - -// Remove all of NTRIPClient.h - -#else // COMPILE_WIFI || COMPILE_ETHERNET - -// Define a hybrid class which can support both WiFiClient and EthernetClient -// Build the Class according to WiFi and Ethernet compile guards - -#if defined(COMPILE_WIFI) && defined(COMPILE_ETHERNET) - -class NTRIPClient : public WiFiClient, public EthernetClient - -#elif defined(COMPILE_ETHERNET) - -class NTRIPClient : public EthernetClient - -#elif defined(COMPILE_WIFI) - -class NTRIPClient : public WiFiClient - -#endif // COMPILE_WIFI || COMPILE_ETHERNET - -{ - public: - NTRIPClient(bool useWiFiNotEthernet) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (HAS_ETHERNET && !useWiFiNotEthernet) - { - _ntripClientEthernet = new EthernetClient; - _useWiFiNotEthernet = false; - } - else - { - _ntripClientWiFi = new WiFiClient; - _useWiFiNotEthernet = true; - } -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - { - _ntripClientEthernet = new EthernetClient; - } -#elif defined(COMPILE_WIFI) - { - _ntripClientWiFi = new WiFiClient; - } -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - ~NTRIPClient() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - { - _ntripClientEthernet->stop(); - delete _ntripClientEthernet; - _ntripClientEthernet = nullptr; - } - else - { - // EthernetClient does not have a destructor. It is virtual. - delete _ntripClientWiFi; - _ntripClientWiFi = nullptr; - } -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - { - _ntripClientEthernet->stop(); - delete _ntripClientEthernet; - _ntripClientEthernet = nullptr; - } -#elif defined(COMPILE_WIFI) - // EthernetClient does not have a destructor. It is virtual. - delete _ntripClientWiFi; - _ntripClientWiFi = nullptr; -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - operator bool() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet; - else - return _ntripClientWiFi; -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet; - else - return false; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi; -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - int connect(const char *host, uint16_t port) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->connect(host, port); - else - return _ntripClientWiFi->connect(host, port); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->connect(host, port); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->connect(host, port); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - size_t write(uint8_t b) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->write(b); - else - return _ntripClientWiFi->write(b); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->write(b); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->write(b); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - size_t write(const uint8_t *buf, size_t size) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->write(buf, size); - else - return _ntripClientWiFi->write(buf, size); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->write(buf, size); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->write(buf, size); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - int available() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->available(); - else - return _ntripClientWiFi->available(); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->available(); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->available(); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - int read(uint8_t *buf, size_t size) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->read(); - else - return _ntripClientWiFi->read(); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->read(); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->read(); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - int read() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->read(); - else - return _ntripClientWiFi->read(); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->read(); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->read(); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - void stop() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - _ntripClientEthernet->stop(); - else - _ntripClientWiFi->stop(); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - _ntripClientEthernet->stop(); - else - return; -#elif defined(COMPILE_WIFI) - _ntripClientWiFi->stop(); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - uint8_t connected() - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->connected(); - else - return _ntripClientWiFi->connected(); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->connected(); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->connected(); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - size_t print(const char *printMe) - { -#if defined(COMPILE_ETHERNET) && defined(COMPILE_WIFI) - if (!_useWiFiNotEthernet) - return _ntripClientEthernet->print(printMe); - else - return _ntripClientWiFi->print(printMe); -#elif defined(COMPILE_ETHERNET) - if (HAS_ETHERNET) - return _ntripClientEthernet->print(printMe); - else - return 0; -#elif defined(COMPILE_WIFI) - return _ntripClientWiFi->print(printMe); -#endif // COMPILE_ETHERNET || COMPILE_WIFI - }; - - protected: - bool _useWiFiNotEthernet; -#if defined(COMPILE_WIFI) - WiFiClient *_ntripClientWiFi; -#endif // COMPILE_WIFI -#if defined(COMPILE_ETHERNET) - EthernetClient *_ntripClientEthernet; -#endif // COMPILE_ETHERNET -}; - -#endif // COMPILE_WIFI || COMPILE_ETHERNET diff --git a/Firmware/RTK_Surveyor/NetworkClient.h b/Firmware/RTK_Surveyor/NetworkClient.h new file mode 100644 index 000000000..cda9dd842 --- /dev/null +++ b/Firmware/RTK_Surveyor/NetworkClient.h @@ -0,0 +1,187 @@ +#ifndef __NETWORK_CLIENT_H__ +#define __NETWORK_CLIENT_H__ + +class NetworkClient : public Client +{ + public: + + //------------------------------ + // Create the network client + //------------------------------ + NetworkClient(bool useWiFiNotEthernet) + { +#if defined(COMPILE_ETHERNET) + if (HAS_ETHERNET && (!useWiFiNotEthernet)) + _client = new EthernetClient; + else +#endif // COMPILE_ETHERNET +#if defined(COMPILE_WIFI) + _client = new WiFiClient; +#else // COMPILE_WIFI + _client = nullptr; +#endif // COMPILE_WIFI + }; + + //------------------------------ + // Delete the network client + //------------------------------ + ~NetworkClient() + { + if (_client) + { + _client->stop(); + delete _client; + _client = nullptr; + } + }; + + //------------------------------ + // Determine if receive data is available + //------------------------------ + + int available() + { + if (_client) + return _client->available(); + return 0; + } + + //------------------------------ + // Determine if the network client was allocated + //------------------------------ + + operator bool() + { + return _client; + } + + //------------------------------ + // Connect to the server + //------------------------------ + + int connect(IPAddress ip, uint16_t port) + { + if (_client) + return _client->connect(ip, port); + return 0; + } + + int connect(const char *host, uint16_t port) + { + if (_client) + return _client->connect(host, port); + return 0; + } + + //------------------------------ + // Determine if the client is connected to the server + //------------------------------ + + uint8_t connected() + { + if (_client) + return _client->connected(); + return 0; + } + + //------------------------------ + // Finish transmitting all the data to the server + //------------------------------ + + void flush() + { + if (_client) + _client->flush(); + } + + //------------------------------ + // Look at the next received byte in the data stream + //------------------------------ + + int peek() + { + if (_client) + return _client->peek(); + return -1; + } + + //------------------------------ + // Display the network client status + //------------------------------ + size_t print(const char *printMe) + { + if (_client) + return _client->print(printMe); + return 0; + }; + + //------------------------------ + // Receive a data byte from the server + //------------------------------ + + int read() + { + if (_client) + return _client->read(); + return 0; + } + + //------------------------------ + // Receive a buffer of data from the server + //------------------------------ + + int read(uint8_t *buf, size_t size) + { + if (_client) + return _client->read(buf, size); + return 0; + } + + //------------------------------ + // Stop the network client + //------------------------------ + + void stop() + { + if (_client) + _client->stop(); + } + + //------------------------------ + // Send a data byte to the server + //------------------------------ + + size_t write(uint8_t b) + { + if (_client) + return _client->write(b); + return 0; + } + + //------------------------------ + // Send a buffer of data to the server + //------------------------------ + + size_t write(const uint8_t *buf, size_t size) + { + if (_client) + return _client->write(buf, size); + return 0; + } + + protected: + + bool _useWiFiNotEthernet; + Client * _client; // Ethernet or WiFi client + + //------------------------------ + // Return the IP address + //------------------------------ + + uint8_t* rawIPAddress(IPAddress& addr) + { + return Client::rawIPAddress(addr); + } +}; + +#endif // __NETWORK_CLIENT_H__ diff --git a/Firmware/RTK_Surveyor/NtripClient.ino b/Firmware/RTK_Surveyor/NtripClient.ino index c11613e4f..99b50c2d7 100644 --- a/Firmware/RTK_Surveyor/NtripClient.ino +++ b/Firmware/RTK_Surveyor/NtripClient.ino @@ -73,7 +73,7 @@ static const int NTRIPCLIENT_MS_BETWEEN_GGA = 5000; // 5s between transmission o //---------------------------------------- // The WiFi / Ethernet connection to the NTRIP caster to obtain RTCM data. -static NTRIPClient *ntripClient; +static NetworkClient *ntripClient; // Throttle the time between connection attempts // ms - Max of 4,294,967,295 or 4.3M seconds or 71,000 minutes or 1193 hours or 49 days between attempts @@ -270,7 +270,7 @@ void ntripClientStart() systemPrintln("NTRIP Client start"); // Allocate the ntripClient structure - ntripClient = new NTRIPClient(false); //(settings.ntripClientUseWiFiNotEthernet); //For future expansion + ntripClient = new NetworkClient(false); // Startup WiFi and the NTRIP client if (ntripClient) @@ -293,9 +293,9 @@ void ntripClientStop(bool wifiClientAllocated) delete ntripClient; ntripClient = nullptr; - // Allocate the NTRIP client structure if not done + // Allocate the ntripClient structure if not done if (wifiClientAllocated == false) - ntripClient = new NTRIPClient(false); // settings.ntripClientUseWiFiNotEthernet); //For future expansion + ntripClient = new NetworkClient(false); } // Increase timeouts if we started WiFi diff --git a/Firmware/RTK_Surveyor/NtripServer.ino b/Firmware/RTK_Surveyor/NtripServer.ino index 486aac4ae..cecd29c65 100644 --- a/Firmware/RTK_Surveyor/NtripServer.ino +++ b/Firmware/RTK_Surveyor/NtripServer.ino @@ -72,7 +72,7 @@ static const int MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS = 30; //---------------------------------------- // WiFi connection used to push RTCM to NTRIP caster over WiFi -static NTRIPClient *ntripServer; +static NetworkClient *ntripServer; // Count of bytes sent by the NTRIP server to the NTRIP caster uint32_t ntripServerBytesSent = 0; @@ -290,7 +290,7 @@ void ntripServerStart() reportHeapNow(); // Allocate the ntripServer structure - ntripServer = new NTRIPClient(false); //(settings.ntripServerUseWiFiNotEthernet); //For future expansion + ntripServer = new NetworkClient(false); //(settings.ntripServerUseWiFiNotEthernet); //For future expansion // Restart WiFi and the NTRIP server if possible if (ntripServer) @@ -313,9 +313,9 @@ void ntripServerStop(bool wifiClientAllocated) delete ntripServer; ntripServer = nullptr; - // Allocate the NTRIP server structure if not done + // Allocate the ntripServer structure if not done if (wifiClientAllocated == false) - ntripServer = new NTRIPClient(false); //(settings.ntripServerUseWiFiNotEthernet); //For future expansion + ntripServer = new NetworkClient(false); //(settings.ntripServerUseWiFiNotEthernet); //For future expansion } // Increase timeouts if we started WiFi diff --git a/Firmware/RTK_Surveyor/RTK_Surveyor.ino b/Firmware/RTK_Surveyor/RTK_Surveyor.ino index a182e7b2b..0d3939e40 100644 --- a/Firmware/RTK_Surveyor/RTK_Surveyor.ino +++ b/Firmware/RTK_Surveyor/RTK_Surveyor.ino @@ -563,7 +563,7 @@ unsigned long lastEthernetCheck = 0; // Prevents cable checking from continually volatile bool ethernetTcpConnected = false; //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include "NTRIPClient.h" //Define a hybrid class which can support both WiFiClient and EthernetClient +#include "NetworkClient.h" //Supports both WiFiClient and EthernetClient // Global variables //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-