The friendly Operating System for the Internet of Things
ntp_packet.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
22 #ifndef NTP_PACKET_H
23 #define NTP_PACKET_H
24 
25 #include <stdint.h>
26 #include "byteorder.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
39 #define NTP_PACKET_LI_POS (6U)
40 #define NTP_PACKET_LI_MASK (0xc0)
41 #define NTP_PACKET_VN_POS (3U)
42 #define NTP_PACKET_VN_MASK (0x38)
43 #define NTP_PACKET_MODE_MASK (0x07)
46 #define NTP_VERSION (4U)
47 #define NTP_PORT (123U)
52 typedef enum {
60 } ntp_mode_t;
61 
67 typedef struct __attribute__((packed)) {
71 
77 typedef struct __attribute__((packed)) {
78  uint8_t li_vn_mode;
79  uint8_t stratum;
80  uint8_t poll;
81  uint8_t precision;
89 } ntp_packet_t;
90 
97 static inline void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
98 {
99  packet->li_vn_mode &= ~NTP_PACKET_LI_MASK;
100  packet->li_vn_mode |= li << NTP_PACKET_LI_POS;
101 }
102 
108 static inline void ntp_packet_set_vn(ntp_packet_t *packet)
109 {
110  packet->li_vn_mode &= ~NTP_PACKET_VN_MASK;
112 }
113 
120 static inline void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
121 {
122  packet->li_vn_mode &= ~NTP_PACKET_MODE_MASK;
123  packet->li_vn_mode |= mode & NTP_PACKET_MODE_MASK;
124 }
125 
133 static inline uint8_t ntp_packet_get_li(ntp_packet_t *packet)
134 {
135  return (packet->li_vn_mode & NTP_PACKET_LI_MASK) >> NTP_PACKET_LI_POS;
136 }
137 
145 static inline uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
146 {
147  return (packet->li_vn_mode & NTP_PACKET_VN_MASK) >> NTP_PACKET_VN_POS;
148 }
149 
158 {
159  return (packet->li_vn_mode & NTP_PACKET_MODE_MASK);
160 }
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif /* NTP_PACKET_H */
167 
ntp_timestamp_t transmit
transmit timestamp
Definition: ntp_packet.h:88
network_uint32_t root_dispersion
root dispersion in NTP short format
Definition: ntp_packet.h:83
#define NTP_PACKET_LI_MASK
leap indicator mask
Definition: ntp_packet.h:40
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:86
NTP timestamp.
Definition: ntp_packet.h:67
static void ntp_packet_set_li(ntp_packet_t *packet, uint8_t li)
Set leap indicator in a NTP packet.
Definition: ntp_packet.h:97
symmetric active
Definition: ntp_packet.h:54
ntp_timestamp_t receive
receive timestamp
Definition: ntp_packet.h:87
#define NTP_PACKET_VN_POS
version position
Definition: ntp_packet.h:41
static void ntp_packet_set_vn(ntp_packet_t *packet)
Set version in a NTP packet.
Definition: ntp_packet.h:108
static uint8_t ntp_packet_get_li(ntp_packet_t *packet)
Get leap indicator from a NTP packet.
Definition: ntp_packet.h:133
ntp_timestamp_t reference
reference timestamp
Definition: ntp_packet.h:85
ntp_mode_t
NTP modes.
Definition: ntp_packet.h:52
#define NTP_VERSION
NTP version.
Definition: ntp_packet.h:46
uint8_t li_vn_mode
leap indicator, version and mode
Definition: ntp_packet.h:78
network_uint32_t fraction
fraction of seconds in 232 picoseconds
Definition: ntp_packet.h:69
network_uint32_t seconds
seconds since 1 January 1900 00:00 UTC
Definition: ntp_packet.h:68
network_uint32_t root_delay
root delay in NTP short format
Definition: ntp_packet.h:82
network_uint32_t reference_id
reference ID
Definition: ntp_packet.h:84
ntp_timestamp_t origin
origin timesptamp
Definition: ntp_packet.h:86
#define NTP_PACKET_LI_POS
Bit positions and masks for ntp_packet_t::li_vn_mode.
Definition: ntp_packet.h:39
symmetric passive
Definition: ntp_packet.h:55
Functions to work with different byte orders.
#define NTP_PACKET_VN_MASK
version mask
Definition: ntp_packet.h:42
uint8_t precision
precision in log2 seconds
Definition: ntp_packet.h:81
reserved for private use
Definition: ntp_packet.h:59
uint8_t stratum
stratum
Definition: ntp_packet.h:79
static void ntp_packet_set_mode(ntp_packet_t *packet, ntp_mode_t mode)
Set mode in a NTP packet.
Definition: ntp_packet.h:120
broadcast
Definition: ntp_packet.h:58
#define NTP_PACKET_MODE_MASK
mode mask
Definition: ntp_packet.h:43
static ntp_mode_t ntp_packet_get_mode(ntp_packet_t *packet)
Get mode from a NTP packet.
Definition: ntp_packet.h:157
NTP packet.
Definition: ntp_packet.h:77
static uint8_t ntp_packet_get_vn(ntp_packet_t *packet)
Get version from a NTP packet.
Definition: ntp_packet.h:145
uint8_t poll
poll in log2 seconds
Definition: ntp_packet.h:80