@@ -78,8 +78,6 @@ const RtkMode_t ntpServerMode = RTK_MODE_NTP;
7878
7979static NetworkUDP *ntpServer; // This will be instantiated when we know the NTP port
8080static uint8_t ntpServerState;
81- static volatile uint8_t
82- ntpSockIndex; // The W5500 socket index for NTP - so we can enable and read the correct interrupt
8381static uint32_t lastLoggedNTPRequest;
8482
8583// ----------------------------------------
@@ -474,7 +472,7 @@ struct NTPpacket
474472
475473// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
476474// NTP process one request
477- // recTv contains the timeval the NTP packet was received - from the W5500 interrupt
475+ // recTv contains the timeval the NTP packet was received
478476// syncTv contains the timeval when the RTC was last sync'd
479477// ntpDiag will contain useful diagnostics
480478bool ntpProcessOneRequest (bool process, const timeval *recTv, const timeval *syncTv, char *ntpDiag = nullptr ,
@@ -486,19 +484,24 @@ bool ntpProcessOneRequest(bool process, const timeval *recTv, const timeval *syn
486484 if (ntpDiag != nullptr )
487485 *ntpDiag = 0 ; // Clear any existing diagnostics
488486
489- int packetDataSize = ntpServer-> parsePacket ();
487+ gettimeofday ((timeval *)ðernetNtpTv, nullptr ); // Record the possible time of the NTP request
490488
491- IPAddress remoteIP = ntpServer->remoteIP ();
492- uint16_t remotePort = ntpServer->remotePort ();
489+ int packetDataSize = 0 ;
493490
494- if (ntpDiag != nullptr ) // Add the packet size and remote IP/Port to the diagnostics
495- {
496- snprintf (ntpDiag, ntpDiagSize, " NTP request from: Remote IP: %d.%d.%d.%d Remote Port: %d\r\n " , remoteIP[0 ],
497- remoteIP[1 ], remoteIP[2 ], remoteIP[3 ], remotePort);
498- }
491+ if (ntpServer->available () > 0 )
492+ packetDataSize = ntpServer->parsePacket ();
499493
500- if (packetDataSize && (packetDataSize >= NTPpacket::NTPpacketSize) )
494+ if (packetDataSize >= NTPpacket::NTPpacketSize)
501495 {
496+ IPAddress remoteIP = ntpServer->remoteIP ();
497+ uint16_t remotePort = ntpServer->remotePort ();
498+
499+ if (ntpDiag != nullptr ) // Add the packet size and remote IP/Port to the diagnostics
500+ {
501+ snprintf (ntpDiag, ntpDiagSize, " NTP request from: Remote IP: %d.%d.%d.%d Remote Port: %d\r\n " , remoteIP[0 ],
502+ remoteIP[1 ], remoteIP[2 ], remoteIP[3 ], remotePort);
503+ }
504+
502505 // Read the NTP packet
503506 NTPpacket packet;
504507
@@ -774,7 +777,6 @@ void ntpServerStop()
774777 // Release the NTP server memory
775778 if (ntpServer)
776779 {
777- w5500DisableSocketInterrupt (ntpSockIndex); // Disable the receive interrupt
778780 ntpServer->stop ();
779781 delete ntpServer;
780782 ntpServer = nullptr ;
@@ -850,12 +852,7 @@ void ntpServerUpdate()
850852 ntpServerStop ();
851853 else
852854 {
853- // Start the NTP server
854- // TODO
855- // ntpServer->begin(settings.ethernetNtpPort);
856- // ntpSockIndex = ntpServer->getSockIndex(); // Get the socket index
857- // w5500ClearSocketInterrupts(); // Clear all interrupts
858- // w5500EnableSocketInterrupt(ntpSockIndex); // Enable the RECV interrupt for the desired socket index
855+ ntpServer->begin (settings.ethernetNtpPort ); // Start the NTP server
859856 online.ethernetNTPServer = true ;
860857 if (!inMainMenu)
861858 reportHeapNow (settings.debugNtp );
@@ -872,9 +869,6 @@ void ntpServerUpdate()
872869
873870 else
874871 {
875- if (w5500CheckSocketInterrupt (ntpSockIndex))
876- w5500ClearSocketInterrupt (ntpSockIndex); // Clear the socket interrupt here
877-
878872 // Check for new NTP requests - if the time has been sync'd
879873 bool processed = ntpProcessOneRequest (systemState == STATE_NTPSERVER_SYNC, (const timeval *)ðernetNtpTv,
880874 (const timeval *)&gnssSyncTv, ntpDiag, sizeof (ntpDiag));
0 commit comments