Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sometime time server does not provide valid time , this need to be managed inside NTPClient::forceUpdate() #133

Open
jef-fr opened this issue Mar 26, 2021 · 0 comments
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@jef-fr
Copy link

jef-fr commented Mar 26, 2021

RFC4330 says:
The server reply should be discarded if any of the LI, Stratum, or Transmit Timestamp fields is 0 or the Mode field is not 4 (unicast) or 5 (broadcast).

I have detected that sometime the time server reply with Transmit Timestamp fields equal to 0, this error case is not managed inside forceUpdate and lead to totaly wrong time. In the error case I have seen LI,Stratum,Mode field where correct.

I suggest to check this inside forceUpdate
Proposed changes (obviously many other method, could also just return false if Transmit Timestamp fields equal to 0.

bool NTPClient::forceUpdate() {
  #ifdef DEBUG_NTPClient
    Serial.println("Update from NTP Server");
  #endif

unsigned long secsSince1900=0L;
int nbtry=0;

  do {
	  this->sendNTPPacket();

	  // Wait till data is there or timeout...
	  byte timeout = 0;
	  int cb = 0;
	  do {
		delay ( 10 );
		cb = this->_udp->parsePacket();
		if (timeout > 100) 
		{
			return false; // timeout after 1000 ms
		}
		timeout++;
	 } while (cb == 0);
	  this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);

	  unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
	  unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
	  // combine the four bytes (two words) into a long integer
	  // this is NTP time (seconds since Jan 1 1900):
	   secsSince1900 = highWord << 16 | lowWord;
	  if (secsSince1900 == 0L) 
	  {
		  if (++nbtry >10)
		  {
			return false;
		  }
	  }
	  else
	  {
 	    this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
		this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
	  }
   } while ( secsSince1900 == 0L);
  return true;
}
@jef-fr jef-fr changed the title sometime time server does not prvide valid time , this need to be managed inside NTPClient::forceUpdate() sometime time server does not provide valid time , this need to be managed inside NTPClient::forceUpdate() Mar 26, 2021
@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Mar 26, 2021
@per1234 per1234 added the topic: code Related to content of the project itself label Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants