1 /*
2  * Copyright (c) 2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public functions for the Precision Time Protocol time specification.
10  *
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_NET_PTP_TIME_H_
14 #define ZEPHYR_INCLUDE_NET_PTP_TIME_H_
15 
16 /**
17  * @brief Precision Time Protocol time specification
18  * @defgroup ptp_time PTP time
19  * @ingroup networking
20  * @{
21  */
22 
23 #include <zephyr/net/net_core.h>
24 #include <zephyr/toolchain.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @brief Precision Time Protocol Timestamp format.
32  *
33  * This structure represents a timestamp according
34  * to the Precision Time Protocol standard.
35  *
36  * Seconds are encoded as a 48 bits unsigned integer.
37  * Nanoseconds are encoded as a 32 bits unsigned integer.
38  */
39 struct net_ptp_time {
40 	/** Seconds encoded on 48 bits. */
41 	union {
42 		struct {
43 #ifdef CONFIG_LITTLE_ENDIAN
44 			uint32_t low;
45 			uint16_t high;
46 			uint16_t unused;
47 #else
48 			uint16_t unused;
49 			uint16_t high;
50 			uint32_t low;
51 #endif
52 		} _sec;
53 		uint64_t second;
54 	};
55 
56 	/** Nanoseconds. */
57 	uint32_t nanosecond;
58 };
59 
60 #ifdef __cplusplus
61 }
62 #endif
63 
64 /**
65  * @brief Precision Time Protocol Extended Timestamp format.
66  *
67  * This structure represents an extended timestamp according
68  * to the Precision Time Protocol standard.
69  *
70  * Seconds are encoded as 48 bits unsigned integer.
71  * Fractional nanoseconds are encoded as 48 bits, their unit
72  * is 2*(-16) ns.
73  */
74 struct net_ptp_extended_time {
75 	/** Seconds encoded on 48 bits. */
76 	union {
77 		struct {
78 #ifdef CONFIG_LITTLE_ENDIAN
79 			uint32_t low;
80 			uint16_t high;
81 			uint16_t unused;
82 #else
83 			uint16_t unused;
84 			uint16_t high;
85 			uint32_t low;
86 #endif
87 		} _sec;
88 		uint64_t second;
89 	};
90 
91 	/** Fractional nanoseconds on 48 bits. */
92 	union {
93 		struct {
94 #ifdef CONFIG_LITTLE_ENDIAN
95 			uint32_t low;
96 			uint16_t high;
97 			uint16_t unused;
98 #else
99 			uint16_t unused;
100 			uint16_t high;
101 			uint32_t low;
102 #endif
103 		} _fns;
104 		uint64_t fract_nsecond;
105 	};
106 } __packed;
107 
108 /**
109  * @}
110  */
111 
112 #endif /* ZEPHYR_INCLUDE_NET_PTP_TIME_H_ */
113