From 0897b0969d8513c1bd5203caf0ac5497314f2bb9 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 10 Jun 2024 16:12:38 -0300 Subject: [PATCH] update example --- mongoose.h | 81 ++++++++++++++++------------- src/sntp.h | 2 + src/util.h | 1 - tutorials/udp/sntp-time-sync/main.c | 13 +++-- 4 files changed, 57 insertions(+), 40 deletions(-) diff --git a/mongoose.h b/mongoose.h index f538b32f..5a68c8d9 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1052,7 +1052,6 @@ uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len); uint64_t mg_millis(void); // Return milliseconds since boot -uint64_t mg_now(void); // Return milliseconds since Epoch bool mg_path_is_sane(const struct mg_str path); #define mg_htons(x) mg_ntohs(x) @@ -2372,6 +2371,8 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url, void mg_sntp_request(struct mg_connection *c); int64_t mg_sntp_parse(const unsigned char *buf, size_t len); +uint64_t mg_now(void); // Return milliseconds since Epoch + @@ -3050,6 +3051,49 @@ struct mg_tcpip_driver_tm4c_data { #endif +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500 + +#undef MG_ENABLE_TCPIP_DRIVER_INIT +#define MG_ENABLE_TCPIP_DRIVER_INIT 0 + +#endif + + +#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7 + +struct mg_tcpip_driver_xmc7_data { + int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5 + uint8_t phy_addr; +}; + +#ifndef MG_TCPIP_PHY_ADDR +#define MG_TCPIP_PHY_ADDR 0 +#endif + +#ifndef MG_DRIVER_MDC_CR +#define MG_DRIVER_MDC_CR 3 +#endif + +#define MG_TCPIP_DRIVER_INIT(mgr) \ + do { \ + static struct mg_tcpip_driver_xmc7_data driver_data_; \ + static struct mg_tcpip_if mif_; \ + driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ + driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \ + mif_.ip = MG_TCPIP_IP; \ + mif_.mask = MG_TCPIP_MASK; \ + mif_.gw = MG_TCPIP_GW; \ + mif_.driver = &mg_tcpip_driver_xmc7; \ + mif_.driver_data = &driver_data_; \ + MG_SET_MAC_ADDRESS(mif_.mac); \ + mg_tcpip_init(mgr, &mif_); \ + MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \ + } while (0) + +#endif + + + #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC struct mg_tcpip_driver_xmc_data { @@ -3096,41 +3140,6 @@ struct mg_tcpip_driver_xmc_data { #endif - -#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7 - -struct mg_tcpip_driver_xmc7_data { - int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5 - uint8_t phy_addr; -}; - -#ifndef MG_TCPIP_PHY_ADDR -#define MG_TCPIP_PHY_ADDR 0 -#endif - -#ifndef MG_DRIVER_MDC_CR -#define MG_DRIVER_MDC_CR 3 -#endif - -#define MG_TCPIP_DRIVER_INIT(mgr) \ - do { \ - static struct mg_tcpip_driver_xmc7_data driver_data_; \ - static struct mg_tcpip_if mif_; \ - driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \ - driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \ - mif_.ip = MG_TCPIP_IP; \ - mif_.mask = MG_TCPIP_MASK; \ - mif_.gw = MG_TCPIP_GW; \ - mif_.driver = &mg_tcpip_driver_xmc7; \ - mif_.driver_data = &driver_data_; \ - MG_SET_MAC_ADDRESS(mif_.mac); \ - mg_tcpip_init(mgr, &mif_); \ - MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \ - } while (0) - -#endif - - #ifdef __cplusplus } #endif diff --git a/src/sntp.h b/src/sntp.h index fd20c5f8..99cbbe03 100644 --- a/src/sntp.h +++ b/src/sntp.h @@ -6,3 +6,5 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data); void mg_sntp_request(struct mg_connection *c); int64_t mg_sntp_parse(const unsigned char *buf, size_t len); + +uint64_t mg_now(void); // Return milliseconds since Epoch diff --git a/src/util.h b/src/util.h index dde1d58f..58f3b5ab 100644 --- a/src/util.h +++ b/src/util.h @@ -18,7 +18,6 @@ uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len); uint64_t mg_millis(void); // Return milliseconds since boot -uint64_t mg_now(void); // Return milliseconds since Epoch bool mg_path_is_sane(const struct mg_str path); #define mg_htons(x) mg_ntohs(x) diff --git a/tutorials/udp/sntp-time-sync/main.c b/tutorials/udp/sntp-time-sync/main.c index 27801ac0..b38650b3 100644 --- a/tutorials/udp/sntp-time-sync/main.c +++ b/tutorials/udp/sntp-time-sync/main.c @@ -17,6 +17,7 @@ static struct mg_connection *s_sntp_conn = NULL; // On embedded systems, rename to time() time_t my_time(time_t *tp) { + // you can just return mg_now() / 1000; time_t t = s_boot_timestamp + mg_millis() / 1000; if (tp != NULL) *tp = t; return t; @@ -25,9 +26,15 @@ time_t my_time(time_t *tp) { // SNTP client callback static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { - int64_t t = *(int64_t *) ev_data; - MG_INFO(("Got SNTP time: %lld ms from epoch", t)); - s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000); + // Time received, the internal protocol handler updates what mg_now() returns + uint64_t curtime = mg_now(); + MG_INFO(("SNTP-updated current time is: %llu ms from epoch", curtime)); + // otherwise, you can process the server returned data yourself + { + uint64_t t = *(uint64_t *) ev_data; + s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000); + MG_INFO(("Got SNTP time: %llu ms from epoch, ", t)); + } } else if (ev == MG_EV_CLOSE) { s_sntp_conn = NULL; }