1 /*
2  * Copyright (c) 2024 BayLibre SAS
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file transport.h
9  * @brief Function implementing abstraction over networking protocols.
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_PTP_TRANSPORT_H_
13 #define ZEPHYR_INCLUDE_PTP_TRANSPORT_H_
14 
15 #include <zephyr/net/net_ip.h>
16 
17 #include "port.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define PTP_SOCKET_PORT_EVENT (319)
24 #define PTP_SOCKET_PORT_GENERAL (320)
25 
26 /**
27  * @brief Values used to identify PTP Port socket based on used port.
28  */
29 enum ptp_socket {
30 	PTP_SOCKET_EVENT,
31 	PTP_SOCKET_GENERAL,
32 	PTP_SOCKET_CNT
33 };
34 
35 /**
36  * @brief Types of PTP networking protocols.
37  */
38 enum ptp_net_protocol {
39 	PTP_NET_PROTOCOL_UDP_IPv4 = 1,
40 	PTP_NET_PROTOCOL_UDP_IPv6,
41 	PTP_NET_PROTOCOL_IEEE_802_3,
42 };
43 
44 /**
45  * @brief Function handling opening specified transport network connection.
46  *
47  * @param[in] port Pointer to the PTP Port structure.
48  *
49  * @return 0 on success, negative otherwise.
50  */
51 int ptp_transport_open(struct ptp_port *port);
52 
53 /**
54  * @brief Function for closing specified transport network connection.
55  *
56  * @param[in] port Pointer to the PTP Port structure.
57  *
58  * @return 0 on success, negative otherwise.
59  */
60 int ptp_transport_close(struct ptp_port *port);
61 
62 /**
63  * @brief Function for sending PTP message using a specified transport. The message is sent
64  * to the default multicast address.
65  *
66  * @note Address specified in the message is ignored.
67  *
68  * @param[in] port Pointer to the PTP Port structure.
69  * @param[in] msg  Pointer to the message to be send.
70  * @param[in] idx  Index of the socket to be used to send message.
71  *
72  * @return Number of sent bytes.
73  */
74 int ptp_transport_send(struct ptp_port *port, struct ptp_msg *msg, enum ptp_socket idx);
75 
76 /**
77  * @brief Function for sending PTP message using a specified transport. The message is sent
78  * to the address provided with @ref ptp_msg message structure.
79  *
80  * @param[in] port Pointer to the PTP Port structure.
81  * @param[in] msg  Pointer to the message to be send.
82  * @param[in] idx  Index of the socket to be used to send message.
83  *
84  * @return Number of sent bytes.
85  */
86 int ptp_transport_sendto(struct ptp_port *port, struct ptp_msg *msg, enum ptp_socket idx);
87 
88 /**
89  * @brief Function for receiving a PTP message using a specified transport.
90  *
91  * @param[in] port Pointer to the PTP Port structure.
92  * @param[in] idx  Index of the socket to be used to send message.
93  *
94  * @return
95  */
96 int ptp_transport_recv(struct ptp_port *port, struct ptp_msg *msg, enum ptp_socket idx);
97 
98 /**
99  * @brief Function for getting transport's protocol address.
100  *
101  * @param[in] port Pointer to the PTP Port structure.
102  * @param[in] addr Pointer to the buffer to store PTP Port's IP address.
103  *
104  * @return 0 if can't get IP address, otherwise length of the address.
105  */
106 int ptp_transport_protocol_addr(struct ptp_port *port, uint8_t *addr);
107 
108 /**
109  * @brief Function for getting transport's physical address.
110  *
111  * @param[in] port Pointer to the PTP Port structure.
112  *
113  * @return Pointer to the structure holding hardware link layer address.
114  */
115 struct net_linkaddr *ptp_transport_physical_addr(struct ptp_port *port);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 /**
122  * @}
123  */
124 
125 #endif /* ZEPHYR_INCLUDE_PTP_TRANSPORT_H_ */
126