Pico Led Controller 1.0.3
A project to control LEDs using Raspberry Pi Pico W
ntp.h File Reference
#include <string.h>
#include <time.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/dns.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"

Go to the source code of this file.

Data Structures

struct  NTP_T_
 NTP struct. More...
 

Macros

#define NTP_SERVER   "pool.ntp.org"
 
#define NTP_MSG_LEN   48
 
#define NTP_PORT   123
 
#define NTP_DELTA   2208988800
 
#define NTP_TEST_TIME   (30 * 1000)
 
#define NTP_RESEND_TIME   (10 * 1000)
 

Typedefs

typedef struct NTP_T_ NTP_T
 NTP struct. More...
 

Functions

static void ntp_result (NTP_T *state, int status, time_t *result)
 NTP result. More...
 
static int64_t ntp_failed_handler (alarm_id_t id, void *user_data)
 NTP failed handler. More...
 
static void ntp_request (NTP_T *state)
 NTP request. More...
 
static void ntp_dns_found (const char *hostname, const ip_addr_t *ipaddr, void *arg)
 NTP DNS found. More...
 
static void ntp_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
 NTP recv. More...
 
static NTP_Tntp_init (void)
 NTP init. More...
 
void ntp_deinit (void)
 NTP deinit. More...
 
void ntp_update_time (void)
 NTP update time. More...
 

Variables

volatile struct tm * utc
 UTC time struct. More...
 
volatile struct tm * current_utc
 UTC time struct. More...
 

Macro Definition Documentation

◆ NTP_DELTA

#define NTP_DELTA   2208988800

Definition at line 17 of file ntp.h.

◆ NTP_MSG_LEN

#define NTP_MSG_LEN   48

Definition at line 15 of file ntp.h.

◆ NTP_PORT

#define NTP_PORT   123

Definition at line 16 of file ntp.h.

◆ NTP_RESEND_TIME

#define NTP_RESEND_TIME   (10 * 1000)

Definition at line 19 of file ntp.h.

◆ NTP_SERVER

#define NTP_SERVER   "pool.ntp.org"

Definition at line 14 of file ntp.h.

◆ NTP_TEST_TIME

#define NTP_TEST_TIME   (30 * 1000)

Definition at line 18 of file ntp.h.

Typedef Documentation

◆ NTP_T

typedef struct NTP_T_ NTP_T

NTP struct.

Struct for the NTP state.

Function Documentation

◆ ntp_deinit()

void ntp_deinit ( void  )

NTP deinit.

Function de-initializes the NTP.

Definition at line 114 of file ntp.c.

115 {
116  if (utc)
117  {
118  free((void*)utc);
119  utc = NULL;
120  }
121 }
volatile struct tm * utc
UTC time struct.
Definition: ntp.c:13

References utc.

Referenced by main().

◆ ntp_dns_found()

static void ntp_dns_found ( const char *  hostname,
const ip_addr_t *  ipaddr,
void *  arg 
)
static

NTP DNS found.

Function handles the NTP DNS found.

Parameters
hostnamehostname
ipaddrIP address
argargument

◆ ntp_failed_handler()

static int64_t ntp_failed_handler ( alarm_id_t  id,
void *  user_data 
)
static

NTP failed handler.

NTP DNS found.

Function handles the NTP failed handler.

Parameters
idalarm ID
user_datauser data
Returns
: time

Function handles the NTP DNS found.

Parameters
hostnamehostname
ipaddrIP address
argargument

◆ ntp_init()

static NTP_T* ntp_init ( void  )
static

NTP init.

Function initializes the NTP.

Returns
: NTP state

◆ ntp_recv()

static void ntp_recv ( void *  arg,
struct udp_pcb *  pcb,
struct pbuf *  p,
const ip_addr_t *  addr,
u16_t  port 
)
static

NTP recv.

Function receives the NTP response.

Parameters
argargument
pcbUDP PCB
ppbuf
addrIP address
portport

◆ ntp_request()

static void ntp_request ( NTP_T state)
static

NTP request.

Function sends the NTP request.

Parameters
stateNTP state

◆ ntp_result()

static void ntp_result ( NTP_T state,
int  status,
time_t *  result 
)
static

NTP result.

Function handles the NTP result.

Parameters
stateNTP state
statusstatus
resultresult

◆ ntp_update_time()

void ntp_update_time ( void  )

NTP update time.

Function updates the NTP time.

Definition at line 123 of file ntp.c.

124 {
125  NTP_T *state = ntp_init();
126  if (!state)
127  return;
128  if (absolute_time_diff_us(get_absolute_time(), state->ntp_test_time) < 0 && !state->dns_request_sent)
129  {
130  state->ntp_resend_alarm = add_alarm_in_ms(NTP_RESEND_TIME, ntp_failed_handler, state, true);
131  cyw43_arch_lwip_begin();
132  int err = dns_gethostbyname(NTP_SERVER, &state->ntp_server_address, ntp_dns_found, state);
133  cyw43_arch_lwip_end();
134 
135  state->dns_request_sent = true;
136  if (err == ERR_OK)
137  ntp_request(state);
138  else if (err != ERR_INPROGRESS)
139  {
140  printf("DNS request failed\n");
141  ntp_result(state, -1, NULL);
142  }
143  }
144  free(state);
145 }
static void ntp_result(NTP_T *state, int status, time_t *result)
Definition: ntp.c:16
static int64_t ntp_failed_handler(alarm_id_t id, void *user_data)
Definition: ntp.c:48
static void ntp_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg)
Definition: ntp.c:56
static NTP_T * ntp_init(void)
Definition: ntp.c:95
static void ntp_request(NTP_T *state)
Definition: ntp.c:36
#define NTP_SERVER
Definition: ntp.h:14
#define NTP_RESEND_TIME
Definition: ntp.h:19
NTP struct.
Definition: ntp.h:35
ip_addr_t ntp_server_address
Definition: ntp.h:36
absolute_time_t ntp_test_time
Definition: ntp.h:39
bool dns_request_sent
Definition: ntp.h:37
alarm_id_t ntp_resend_alarm
Definition: ntp.h:40

References NTP_T_::dns_request_sent, ntp_dns_found(), ntp_failed_handler(), ntp_init(), ntp_request(), NTP_T_::ntp_resend_alarm, NTP_RESEND_TIME, ntp_result(), NTP_SERVER, NTP_T_::ntp_server_address, and NTP_T_::ntp_test_time.

Variable Documentation

◆ current_utc

volatile struct tm* current_utc
extern

UTC time struct.

Struct for the last checked UTC time.

Definition at line 14 of file ntp.c.

Referenced by ntp_result().

◆ utc

volatile struct tm* utc
extern

UTC time struct.

Struct for the UTC time of program start.

Definition at line 13 of file ntp.c.

Referenced by ntp_deinit(), ntp_result(), and ssi_handler().