1 /*
2  * Copyright (c) 2024 BayLibre SAS
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file ddt.h
9  * @brief Derived data types.
10  *
11  * @note Based on IEEE 1588:2019 section 5.3 - Derived data types
12  */
13 
14 #ifndef ZEPHYR_INCLUDE_PTP_DDT_H_
15 #define ZEPHYR_INCLUDE_PTP_DDT_H_
16 
17 #include <stdbool.h>
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * @brief PTP time interval in nanoseconds.
26  * @note 5.3.2 - time interval expressed in nanoseconds multiplied by 2^16
27  */
28 typedef int64_t ptp_timeinterval;
29 
30 /**
31  * @brief Structure for storing PTP timestamp used in PTP Protocol.
32  * @note 5.3.3 - timestamp with respect to epoch
33  */
34 struct ptp_timestamp {
35 	/** Seconds encoded on 48 bits - high 16 bits. */
36 	uint16_t seconds_high;
37 	/** Seconds encoded on 48 bits - low 32 bits. */
38 	uint32_t seconds_low;
39 	/** Nanoseconds. */
40 	uint32_t nanoseconds;
41 } __packed;
42 
43 /**
44  * @brief PTP Clock Identity.
45  * @note 5.3.4 - identifies unique entities within a PTP network.
46  */
47 typedef struct {
48 	/** ID bytes. */
49 	uint8_t id[8];
50 } ptp_clk_id;
51 
52 /**
53  * @brief PTP Port Identity.
54  * @note 5.3.5 - identifies a PTP Port or a Link port.
55  */
56 struct ptp_port_id {
57 	/** PTP Clock ID. */
58 	ptp_clk_id clk_id;
59 	/** PTP Port number. */
60 	uint16_t   port_number;
61 } __packed;
62 
63 /**
64  * @brief Structure represeniting address of a PTP Port.
65  * @note 5.3.6 - represents the protocol address of a PTP port
66  */
67 struct ptp_port_addr {
68 	/** PTP Port's protocol. */
69 	uint16_t protocol;
70 	/** Address length. */
71 	uint16_t addr_len;
72 	/** Address field. */
73 	uint8_t  address[];
74 } __packed;
75 
76 /**
77  * @brief Structure for PTP Clock quality metrics.
78  * @note 5.3.7 - quality of a clock
79  */
80 struct ptp_clk_quality {
81 	/** PTP Clock's class */
82 	uint8_t  class;
83 	/** Accuracy of the PTP Clock. */
84 	uint8_t  accuracy;
85 	/** Value representing stability of the Local PTP Clock. */
86 	uint16_t offset_scaled_log_variance;
87 } __packed;
88 
89 /**
90  * @brief
91  * @note 5.3.8 - TLV (type, length, value) extension fields
92  */
93 struct ptp_tlv {
94 	/** Type of the TLV value. */
95 	uint16_t type;
96 	/** Length of the TLV value field. */
97 	uint16_t length;
98 	/** TLV's data field. */
99 	uint8_t  value[];
100 } __packed;
101 
102 /**
103  * @brief Generic datatype for storing text in PTP messages.
104  * @note 5.3.9 - holds textual content in PTP messages
105  */
106 struct ptp_text {
107 	/** Length of the text field.
108 	 *
109 	 * @note Might be larger than number of symbols due to UTF-8 encoding.
110 	 */
111 	uint8_t length;
112 	/** Text itself.
113 	 *
114 	 * @note Encoded as UTF-8, single symbol can be 1-4 bytes long
115 	 */
116 	uint8_t text[];
117 } __packed;
118 
119 /**
120  * @brief Type holding difference between two numeric value
121  * @note 5.3.11 - relative difference between two numeric values.
122  * It's a dimensionless fraction and multiplied by 2^62.
123  */
124 typedef int64_t ptp_relative_diff;
125 
126 struct ptp_port;
127 
128 struct ptp_clock;
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 /**
135  * @}
136  */
137 
138 #endif /* ZEPHYR_INCLUDE_PTP_PDT_H_ */
139