6 #include "pico/stdlib.h"
7 #include "pico/cyw43_arch.h"
10 #include "lwip/pbuf.h"
13 volatile struct tm *
utc = NULL;
18 if (status == 0 && result)
38 cyw43_arch_lwip_begin();
39 struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT,
NTP_MSG_LEN, PBUF_RAM);
40 uint8_t *req = (uint8_t *) p->payload;
45 cyw43_arch_lwip_end();
51 printf(
"NTP request failed\n");
56 static void ntp_dns_found(
const char *hostname,
const ip_addr_t *ipaddr,
void *arg)
62 printf(
"NTP address %s\n", ipaddr_ntoa(ipaddr));
67 printf(
"NTP DNS request failed\n");
72 static void ntp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port)
75 uint8_t mode = pbuf_get_at(p, 0) & 0x7;
76 uint8_t stratum = pbuf_get_at(p, 1);
80 uint8_t seconds_buf[4] = {0};
81 pbuf_copy_partial(p, seconds_buf,
sizeof(seconds_buf), 40);
82 uint32_t seconds_since_1900 = seconds_buf[0] << 24 | seconds_buf[1] << 16 | seconds_buf[2] << 8 | seconds_buf[3];
83 uint32_t seconds_since_1970 = seconds_since_1900 -
NTP_DELTA;
84 time_t epoch = seconds_since_1970;
89 printf(
"Invalid NTP response\n");
100 printf(
"Failed to allocate state\n");
103 state->
ntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
106 printf(
"Failed to create PCB\n");
131 cyw43_arch_lwip_begin();
133 cyw43_arch_lwip_end();
138 else if (err != ERR_INPROGRESS)
140 printf(
"DNS request failed\n");
static void ntp_result(NTP_T *state, int status, time_t *result)
static int64_t ntp_failed_handler(alarm_id_t id, void *user_data)
static void ntp_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg)
void ntp_deinit(void)
NTP deinit.
static void ntp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
volatile struct tm * current_utc
UTC time struct.
static NTP_T * ntp_init(void)
volatile struct tm * utc
UTC time struct.
static void ntp_request(NTP_T *state)
void ntp_update_time(void)
NTP update time.
ip_addr_t ntp_server_address
absolute_time_t ntp_test_time
alarm_id_t ntp_resend_alarm