Midnight Sun Firmware
Loading...
Searching...
No Matches
network_time_protocol.h
1#pragma once
2
3/************************************************************************************************
4 * @file network_time_protocol.h
5 *
6 * @brief Header file defining the Network time protocol helpers
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <cstdint>
14#include <ctime>
15
16/* Inter-component Headers */
17
18/* Intra-component Headers */
19
29#define NTP_VERSION 4U
30
34#define NTP_LEAP_INDICATOR_OFFSET 6U
35
39#define NTP_VERSION_OFFSET 3U
40
44#define NTP_MODE_OFFSET 0U
45
50#define NTP_POLL_TO_SECONDS(poll) (1U << poll)
51
55static constexpr uint32_t NTP_UNIX_EPOCH_DIFF = 2208988800UL;
56
60struct NTPTime {
61 uint32_t seconds;
62 uint32_t fraction;
63};
64
68struct NTPPacket {
69 uint8_t flags;
70 uint8_t stratum;
71 uint8_t poll;
72 uint8_t precision;
73 uint32_t rootDelay;
74 uint32_t rootDispersion;
75 uint8_t referenceId[4];
80};
81
85enum NTPMode {
94};
95
105
111time_t ntpToUnixTime(NTPTime ntpTime);
112
118uint32_t unixToNTPTime(time_t unixTime);
119
124void convertNTPTimestamp(NTPTime &timestamp);
125
130void dumpNTPPacketData(const NTPPacket packet);
131
uint32_t unixToNTPTime(time_t unixTime)
Converts a Unix time to NTP time.
Definition: network_time_protocol.cc:24
void dumpNTPPacketData(const NTPPacket packet)
Dumps the data of an NTP packet for debugging purposes.
Definition: network_time_protocol.cc:33
void convertNTPTimestamp(NTPTime &timestamp)
Converts an NTP timestamp to its human-readable representation.
Definition: network_time_protocol.cc:28
NTPLeapIndicator
Enumeration for the Leap Indicator values.
Definition: network_time_protocol.h:99
time_t ntpToUnixTime(NTPTime ntpTime)
Converts an NTP time to Unix time.
NTPMode
Enumeration for the NTP modes.
Definition: network_time_protocol.h:85
static constexpr uint32_t NTP_UNIX_EPOCH_DIFF
Difference between the NTP epoch (1900-01-01) and the Unix epoch (1970-01-01) in seconds.
Definition: network_time_protocol.h:55
@ NTP_LI_NONE
Definition: network_time_protocol.h:100
@ NTP_LI_LAST_MINUTE_OF_THE_DAY_61_S
Definition: network_time_protocol.h:101
@ NTP_LI_NOSYNC
Definition: network_time_protocol.h:103
@ NTP_LI_LAST_MINUTE_OF_THE_DAY_59_S
Definition: network_time_protocol.h:102
@ NUM_NTP_MODES
Definition: network_time_protocol.h:93
@ NTP_RESERVED_MODE
Definition: network_time_protocol.h:86
@ NTP_CLIENT_MODE
Definition: network_time_protocol.h:89
@ NTP_CONTROL_MODE
Definition: network_time_protocol.h:92
@ NTP_ACTIVE_MODE
Definition: network_time_protocol.h:87
@ NTP_PASSIVE_MODE
Definition: network_time_protocol.h:88
@ NTP_SERVER_MODE
Definition: network_time_protocol.h:90
@ NTP_BROADCAST_MODE
Definition: network_time_protocol.h:91
Structure representing an NTP packet.
Definition: network_time_protocol.h:68
NTPTime originTime
Definition: network_time_protocol.h:77
uint32_t rootDispersion
Definition: network_time_protocol.h:74
uint8_t poll
Definition: network_time_protocol.h:71
uint32_t rootDelay
Definition: network_time_protocol.h:73
NTPTime receiveTime
Definition: network_time_protocol.h:78
NTPTime transmitTime
Definition: network_time_protocol.h:79
uint8_t flags
Definition: network_time_protocol.h:69
uint8_t referenceId[4]
Definition: network_time_protocol.h:75
uint8_t precision
Definition: network_time_protocol.h:72
uint8_t stratum
Definition: network_time_protocol.h:70
NTPTime referenceTime
Definition: network_time_protocol.h:76
Structure representing NTP time format with seconds and fractional seconds.
Definition: network_time_protocol.h:60
uint32_t seconds
Definition: network_time_protocol.h:61
uint32_t fraction
Definition: network_time_protocol.h:62