Skip to content

Commit

Permalink
Add macro for NIST response bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Damiano <[email protected]>
  • Loading branch information
SRGDamia1 committed Sep 16, 2024
1 parent b1f45a1 commit 7561c6c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/modems/DigiXBeeWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@ uint32_t DigiXBeeWifi::getNISTTime(void) {
// Wait up to 5 seconds for a response
if (connectionMade) {
uint32_t start = millis();
while (gsmClient && gsmClient.available() < 4 &&
while (gsmClient && gsmClient.available() < NIST_RESPONSE_BYTES &&
millis() - start < 5000L) {
// wait
}

if (gsmClient.available() >= 4) {
MS_DBG(F("NIST responded after"), millis() - start, F("ms"));
byte response[4] = {0};
gsmClient.read(response, 4);
byte response[NIST_RESPONSE_BYTES] = {0};
gsmClient.read(response, NIST_RESPONSE_BYTES);
gsmClient.stop();
return parseNISTBytes(response);
} else {
Expand Down
90 changes: 47 additions & 43 deletions src/modems/LoggerModemMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,16 @@
* @brief The port hosting the NIST "time" protocol (37)
*/
#define TIME_PROTOCOL_PORT 37
/**
* @brief The size of the NIST response from the "time" protocol, in bytes.
*/
#define NIST_RESPONSE_BYTES 4

// Try up to 4 NIST IP addresses attempting to get a timestamp from NIST
#if !defined NIST_SERVER_RETRYS || defined(DOXYGEN)
/**
* @brief The number of retry attempts when connecting to the NIST server.
*/
#define NIST_SERVER_RETRYS 4
#define NIST_SERVER_RETRYS 12
#endif // NIST_SERVER_RETRYS

/**
Expand All @@ -486,47 +489,48 @@
* subclass.
*
*/
#define MS_MODEM_GET_NIST_TIME(specificModem) \
uint32_t specificModem::getNISTTime(void) { \
/** Check for and bail if not connected to the internet. */ \
if (!isInternetAvailable()) { \
MS_DBG(F("No internet connection, cannot connect to NIST.")); \
return 0; \
} \
\
/** Try up to 12 times to get a timestamp from NIST. */ \
for (uint8_t i = 0; i < 12; i++) { \
while (millis() < _lastNISTrequest + 4000) { /* wait */ \
} \
\
/** Make TCP connection. */ \
MS_DBG(F("\nConnecting to NIST daytime Server")); \
bool connectionMade = gsmClient.connect("time.nist.gov", \
TIME_PROTOCOL_PORT, 15); \
\
/** Wait up to 5 seconds for a response. */ \
if (connectionMade) { \
uint32_t start = millis(); \
while (gsmClient && \
gsmClient.available() < NIST_SERVER_RETRYS && \
millis() - start < 5000L) {} \
\
if (gsmClient.available() >= 4) { \
MS_DBG(F("NIST responded after"), millis() - start, \
F("ms")); \
byte response[4] = {0}; \
gsmClient.read(response, 4); \
if (gsmClient.connected()) gsmClient.stop(); \
return parseNISTBytes(response); \
} else { \
MS_DBG(F("NIST Time server did not respond!")); \
if (gsmClient.connected()) gsmClient.stop(); \
} \
} else { \
MS_DBG(F("Unable to open TCP to NIST!")); \
} \
} \
return 0; \
#define MS_MODEM_GET_NIST_TIME(specificModem) \
uint32_t specificModem::getNISTTime(void) { \
/** Check for and bail if not connected to the internet. */ \
if (!isInternetAvailable()) { \
MS_DBG(F("No internet connection, cannot connect to NIST.")); \
return 0; \
} \
\
/** Try up to 12 times to get a timestamp from NIST. */ \
for (uint8_t i = 0; i < 12; i++) { \
while (millis() < _lastNISTrequest + 4000) { /* wait */ \
} \
\
/** Make TCP connection. */ \
MS_DBG(F("\nConnecting to NIST daytime Server")); \
bool connectionMade = gsmClient.connect("time.nist.gov", \
TIME_PROTOCOL_PORT, 15); \
\
/** Wait up to 5 seconds for a response. */ \
if (connectionMade) { \
uint32_t start = millis(); \
while (gsmClient && \
gsmClient.available() < NIST_SERVER_RETRYS && \
millis() - start < 5000L) {} \
\
if (gsmClient.available() >= NIST_RESPONSE_BYTES) { \
MS_DBG(F("NIST responded after"), millis() - start, \
F("ms")); \
byte response[NIST_RESPONSE_BYTES] = {0}; \
gsmClient.read(response, NIST_RESPONSE_BYTES); \
if (gsmClient.connected()) gsmClient.stop(); \
uint32_t nistParsed = parseNISTBytes(response); \
if (nistParsed != 0) { return parseNISTBytes(response); } \
} else { \
MS_DBG(F("NIST Time server did not respond!")); \
if (gsmClient.connected()) gsmClient.stop(); \
} \
} else { \
MS_DBG(F("Unable to open TCP to NIST!")); \
} \
} \
return 0; \
}

#if defined(TINY_GSM_MODEM_XBEE) || defined(TINY_GSM_MODEM_ESP8266)
Expand Down

0 comments on commit 7561c6c

Please sign in to comment.