1 /*
2  * Copyright (c) 2024 BayLibre SAS
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file ds.h
9  * @brief Datasets types.
10  *
11  * References are to version 2019 of IEEE 1588, ("PTP")
12  */
13 
14 #ifndef ZEPHYR_INCLUDE_PTP_DS_H_
15 #define ZEPHYR_INCLUDE_PTP_DS_H_
16 
17 #include <zephyr/net/ptp_time.h>
18 
19 #include "ddt.h"
20 #include "state_machine.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * @brief PTP Default Dataset.
28  * @note 8.2.1 - defaultDS data set member specification
29  */
30 struct ptp_default_ds {
31 	/* static */
32 	/** Clock ID */
33 	ptp_clk_id	       clk_id;
34 	/** Indicates number of ptp ports on the PTP Instance. */
35 	uint16_t	       n_ports;
36 	/* dynamic */
37 	/** Quality of a clock. */
38 	struct ptp_clk_quality clk_quality;
39 	/** Parameter used in the execution BTCA. */
40 	uint8_t		       priority1;
41 	/** Parameter used in the execution BTCA. */
42 	uint8_t		       priority2;
43 	/** ID number of the instance in a domain. */
44 	uint8_t		       domain;
45 	/** sdoId attribute. */
46 	uint16_t	       sdo_id: 12;
47 	/** Flag indicating timeReceiver mode. */
48 	bool		       time_receiver_only;
49 	/* optional */
50 	/** Current value of the PTP Instance Time. */
51 	struct net_ptp_time    current_time;
52 	/** Enable flag. */
53 	bool		       enable;
54 	/** Flag indication if external port configuration option is enabled. */
55 	bool		       external_port_conf_en;
56 	/** Maximum value of steps removed of an Announce messages to be considered in BTCA. */
57 	uint8_t		       max_steps_rm;
58 	/** PTP Instance type. */
59 	uint8_t		       type;
60 };
61 
62 /**
63  * @brief PTP Current Dataset.
64  * @note 8.2.2 - currentDS data set member specification
65  */
66 struct ptp_current_ds {
67 	/** Number of PTP Communication Paths traversed between PTP Instance and the GM. */
68 	uint16_t	 steps_rm;
69 	/** Current value of time difference between a Transmitter and Receiver.
70 	 *
71 	 * @note it is computed as <time on the Receiver> - <time on the Transmitter>
72 	 */
73 	ptp_timeinterval offset_from_tt;
74 	/** Mean propagation time. */
75 	ptp_timeinterval mean_delay;
76 	/* optional */
77 	/** Flag inticating if port is synchronized. */
78 	bool		 sync_uncertain;
79 };
80 
81 /**
82  * @brief PTP Parent Dataset.
83  * @note 8.2.3 - parentDS data set member specification
84  */
85 struct ptp_parent_ds {
86 	/** PTP Port's ID */
87 	struct ptp_port_id     port_id;
88 	/** Flag indication if the Instance has a Port in Receiver state or has estimates
89 	 * of obsreved_parent_offset_scaled_log_variance or obsreved_parent_clk_phase_change_rate.
90 	 */
91 	bool		       stats;
92 	/** Estimate of the variance of the phase offset. */
93 	uint16_t	       obsreved_parent_offset_scaled_log_variance;
94 	/** Estimate of the phase change rate. */
95 	int32_t		       obsreved_parent_clk_phase_change_rate;
96 	/** Grandmaster's ID. */
97 	ptp_clk_id	       gm_id;
98 	/** Grandmaster's Clock quality. */
99 	struct ptp_clk_quality gm_clk_quality;
100 	/** Value of Grandmaster's priority1 attribute. */
101 	uint8_t		       gm_priority1;
102 	/** Value of Grandmaster's priority2 attribute. */
103 	uint8_t		       gm_priority2;
104 	/** Address of the PTP Port issuing sync messages used to synchronize this PTP Instance. */
105 	struct ptp_port_addr   protocol_addr;
106 	/** Flag inticating use of sync_uncertain flag in Announce message. */
107 	bool		       sync_uncertain;
108 };
109 
110 /**
111  * @brief PTP Time Properties Dataset.
112  * @note 8.2.4 - timePropertiesDS data set member specification
113  */
114 struct ptp_time_prop_ds {
115 	/** Value of dLS received from Grandmaster */
116 	int16_t current_utc_offset;
117 	/** Flags used for operation of time received from Grandmaster PTP Instance. */
118 	uint8_t flags;
119 	/** Source of time used by Grandmaster PTP Instance. */
120 	uint8_t time_src;
121 };
122 
123 /**
124  * @brief PTP Non-volatile Storage Dataset.
125  * @note 8.2.7 - nonvolatileStorageDS
126  */
127 struct ptp_nvs_ds {
128 	/** Reset non-volatile storage. */
129 	bool reset;
130 	/** Save current values of applicable dynamic or configurable data set members
131 	 * to non-volatile storage.
132 	 */
133 	bool save;
134 };
135 
136 /**
137  * @brief Enumeration for types of delay mechanisms for PTP Clock.
138  */
139 enum ptp_delay_mechanism {
140 	PTP_DM_E2E = 1,
141 	PTP_DM_P2P,
142 	PTP_DM_COMMON_P2P,
143 	PTP_DM_SPECIAL,
144 	PTP_DM_NO_MECHANISM = 0xFE
145 };
146 
147 /**
148  * @brief PTP Port Dataset
149  * @note 8.2.15 - portDS data set member specification
150  */
151 struct ptp_port_ds {
152 	/* static */
153 	/** PTP Port's ID. */
154 	struct ptp_port_id	 id;
155 	/* dynamic */
156 	/** State of a PTP Port. */
157 	enum ptp_port_state	 state;
158 	/** Logarithm to the base 2  minimal Delay_Req interval in nanoseconds. */
159 	int8_t			 log_min_delay_req_interval;
160 	/** Current one-way propagation delay. */
161 	ptp_timeinterval	 mean_link_delay;
162 	/* configurable */
163 	/** Logarithm to the base 2 Announce interval in nanoseconds. */
164 	int8_t			 log_announce_interval;
165 	/** Number of Announce intervals before timeout. */
166 	uint8_t			 announce_receipt_timeout;
167 	/** Logarithm to the base 2 Sync interval in nanoseconds. */
168 	int8_t			 log_sync_interval;
169 	/** Delay mechanism used by the PTP Port. */
170 	enum ptp_delay_mechanism delay_mechanism;
171 	/** Logarithm to the base 2  minimal Pdelay_Req interval in nanoseconds. */
172 	int8_t			 log_min_pdelay_req_interval;
173 	/** Version of supported PTP standard. */
174 	uint8_t			 version;
175 	/** Value of delay asymmetry. */
176 	ptp_timeinterval	 delay_asymmetry;
177 	/* optional */
178 	/** Enable flag. */
179 	bool			 enable;
180 	/** Flag setting PTP Port in timeTransmitter mode. */
181 	bool			 time_transmitter_only;
182 };
183 
184 /**
185  * @brief PTP Description Port Dataset.
186  * @note 8.2.18 - descriptionPortDS
187  */
188 struct ptp_dest_port_ds {
189 	/** PTP profile identifier for the PTP Port. */
190 	union {
191 		struct {
192 			uint8_t byte[6];
193 		};
194 		uint64_t id: 48;
195 	} profile_id;
196 	/** Protocol address of the PTP Port */
197 	struct ptp_port_addr protocol_addr;
198 };
199 
200 /**
201  * @brief Generic Data set type used for dataset comparison algorithm.
202  */
203 struct ptp_dataset {
204 	/** Parameter used in the execution BTCA. */
205 	uint8_t		       priority1;
206 	/** PTP Clock's ID. */
207 	ptp_clk_id	       clk_id;
208 	/** PTP Clock's quality. */
209 	struct ptp_clk_quality clk_quality;
210 	/** Parameter used in the execution BTCA. */
211 	uint8_t		       priority2;
212 	/** Number of PTP Communication Paths traversed between PTP Instance and the GM. */
213 	uint16_t	       steps_rm;
214 	/** timeTransmitter ID. */
215 	struct ptp_port_id     sender;
216 	/** timeReceiver ID. */
217 	struct ptp_port_id     receiver;
218 };
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 /**
225  * @}
226  */
227 
228 #endif /* ZEPHYR_INCLUDE_PTP_DS_H_ */
229