Debian Codegraph @

examples/crf-talker.c

raw
1 /*
2 * Copyright (c) 2018, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* CRF Talker example.
29 *
30 * This example implements a very simple CRF talker application which reads
31 * system clock to get current time, generates CRF timestamps, creates AVTP CRF
32 * packets and transmit them via the network.
33 *
34 * TSN stream parameters (e.g. destination mac address and maximum transit
35 * time) are passed via command-line arguments. Run 'crf-talker --help' for
36 * more information.
37 *
38 * This example relies on system clock to generate CRF timestamps and to keep
39 * transmission rate. So make sure the system clock is synchronized with the
40 * PTP Hardware Clock (PHC) from your NIC and that the PHC is synchronized with
41 * the PTP time from the network. For further information on how to synchronize
42 * those clocks see ptp4l(8) and phc2sys(8) man pages.
43 *
44 * Here is an example to setup ptp4l and phc2sys on PTP master host. Replace
45 * $IFNAME by your PTP capable NIC name. The gPTP.cfg file mentioned below can
46 * be found in /usr/share/doc/linuxptp/ (depending on your distro).
47 * $ ptp4l -f gPTP.cfg -i $IFNAME
48 * $ phc2sys -f gPTP.cfg -c $IFNAME -s CLOCK_REALTIME -w
49 */
50
51 #include <alloca.h>
52 #include <argp.h>
53 #include <arpa/inet.h>
54 #include <linux/if.h>
55 #include <linux/if_ether.h>
56 #include <linux/if_packet.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sys/ioctl.h>
61 #include <time.h>
62 #include <unistd.h>
63 #include <math.h>
64
65 #include "avtp.h"
66 #include "avtp_crf.h"
67 #include "examples/common.h"
68
69 #define STREAM_ID 0xAABBCCDDEEFF0002
70
71 /* Values based on Spec 1722 Table 28 recommendation. */
72 #define SAMPLE_RATE 48000
73 #define TIMESTAMP_INTERVAL 160
74 #define TIMESTAMPS_PER_SEC 300
75 #define TIMESTAMPS_PER_PKT 6
76
77 #define NSEC_PER_SEC 1000000000ULL
78 #define NSEC_PER_MSEC 1000000ULL
79
80 #define DATA_LEN (sizeof(uint64_t) * TIMESTAMPS_PER_PKT)
81 #define PDU_SIZE (sizeof(struct avtp_crf_pdu) + DATA_LEN)
82 #define PDUS_PER_SEC (TIMESTAMPS_PER_SEC / TIMESTAMPS_PER_PKT)
83 #define CRF_PERIOD (NSEC_PER_SEC / TIMESTAMPS_PER_SEC)
84 #define NOMINAL_PERIOD (1.0 / SAMPLE_RATE)
85 #define TX_INTERVAL (NSEC_PER_SEC / PDUS_PER_SEC)
86
87 static char ifname[IFNAMSIZ];
88 static uint8_t macaddr[ETH_ALEN];
89 static int mtt;
90
91 static struct argp_option options[] = {
92 {"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" },
93 {"ifname", 'i', "IFNAME", 0, "Network Interface" },
94 {"max-transit-time", 'm', "MSEC", 0, "Maximum Transit Time in ms" },
95 { 0 }
96 };
97
98 static error_t parser(int key, char *arg, struct argp_state *state)
99 {
100 int res;
101
102 switch (key) {
103 case 'd':
104 res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
105 &macaddr[0], &macaddr[1], &macaddr[2],
106 &macaddr[3], &macaddr[4], &macaddr[5]);
107 if (res != 6) {
108 fprintf(stderr, "Invalid address\n");
109 exit(EXIT_FAILURE);
110 }
111
112 break;
113 case 'i':
114 strncpy(ifname, arg, sizeof(ifname) - 1);
115 break;
116 case 'm':
117 mtt = atoi(arg) * NSEC_PER_MSEC;
118 break;
119 }
120
121 return 0;
122 }
123
124 static struct argp argp = { options, parser };
125
126 static uint64_t calculate_crf_timestamp(struct timespec tspec, uint64_t rounded_mtt)
127 {
128 uint64_t ts, crf_time;
129 const uint64_t tc = 0;
130
131 ts = (tspec.tv_sec * NSEC_PER_SEC) + tspec.tv_nsec;
132
133 /* Equation 14 defined in spec 1722:
134 * Tcrf = Ts + (ceil(TTmax/p) * p) + Tc
135 * TCRF : CRF timestamp placed in the CRF AVTPDU
136 * Ts : the original timestamp, sampled at the source
137 * TTmax: the Max Transit Time value chosen for the network
138 * P : the nominal period of the clock source
139 * TC : the amount of time that samples spend accumulating in the
140 * Talker’s transmit buffer
141 */
142
143 /* Value for sample accumulating time (TC) is system specific. Since
144 * this is a CRF talker example, for simplicity, the value for Tc
145 * is set to 0.
146 */
147 crf_time = ts + rounded_mtt + tc;
148
149 return crf_time;
150 }
151
152 static int init_pdu(struct avtp_crf_pdu *pdu)
153 {
154 int res;
155
156 res = avtp_crf_pdu_init(pdu);
157 if (res < 0)
158 return -1;
159
160 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_FS, 0);
161 if (res < 0)
162 return -1;
163
164 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_TYPE,
165 AVTP_CRF_TYPE_AUDIO_SAMPLE);
166 if (res < 0)
167 return -1;
168
169 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_STREAM_ID, STREAM_ID);
170 if (res < 0)
171 return -1;
172
173 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_PULL,
174 AVTP_CRF_PULL_MULT_BY_1);
175 if (res < 0)
176 return -1;
177
178 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_BASE_FREQ,
179 SAMPLE_RATE);
180 if (res < 0)
181 return -1;
182
183 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_TIMESTAMP_INTERVAL,
184 TIMESTAMP_INTERVAL);
185 if (res < 0)
186 return -1;
187
188 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_CRF_DATA_LEN, DATA_LEN);
189 if (res < 0)
190 return -1;
191
192 return 0;
193 }
194
195 int main(int argc, char *argv[])
196 {
197 int sk_fd, res, idx;
198 uint8_t seq_num = 0;
199 uint64_t crf_time, rounded_mtt;
200 struct timespec clksrc_ts = {0};
201 struct sockaddr_ll sk_addr = {0};
202 struct avtp_crf_pdu *pdu = alloca(PDU_SIZE);
203
204 argp_parse(&argp, argc, argv, 0, NULL, NULL);
205
206 sk_fd = create_talker_socket(-1);
207 if (sk_fd < 0) {
208 return 1;
209 }
210
211 res = setup_socket_address(sk_fd, ifname, macaddr, ETH_P_TSN, &sk_addr);
212 if (res < 0)
213 goto err;
214
215 res = init_pdu(pdu);
216 if (res < 0)
217 goto err;
218
219 res = clock_gettime(CLOCK_REALTIME, &clksrc_ts);
220 if (res < 0) {
221 perror("Failed to get time");
222 goto err;
223 }
224
225 rounded_mtt = ceil(mtt / NOMINAL_PERIOD) * NOMINAL_PERIOD;
226
227 while (1) {
228 ssize_t n;
229
230 crf_time = calculate_crf_timestamp(clksrc_ts, rounded_mtt);
231 for (idx = 0; idx < TIMESTAMPS_PER_PKT; idx++)
232 pdu->crf_data[idx] = htobe64(crf_time + (CRF_PERIOD * idx));
233
234 res = avtp_crf_pdu_set(pdu, AVTP_CRF_FIELD_SEQ_NUM, seq_num++);
235 if (res < 0)
236 goto err;
237
238 n = sendto(sk_fd, pdu, PDU_SIZE, 0,
239 (struct sockaddr *) &sk_addr, sizeof(sk_addr));
240 if (n < 0) {
241 perror("Failed to send data");
242 goto err;
243 }
244
245 if (n != PDU_SIZE) {
246 fprintf(stderr, "wrote %zd bytes, expected %zd\n",
247 n, PDU_SIZE);
248 }
249
250 clksrc_ts.tv_nsec += TX_INTERVAL;
251 if (clksrc_ts.tv_nsec >= NSEC_PER_SEC) {
252 clksrc_ts.tv_sec++;
253 clksrc_ts.tv_nsec -= NSEC_PER_SEC;
254 }
255 clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &clksrc_ts, NULL);
256 }
257
258 close(sk_fd);
259 return 0;
260
261 err:
262 close(sk_fd);
263 return 1;
264 }