Debian Codegraph @

examples/common.c

raw
1 /*
2 * Copyright (c) 2019, 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 #include <arpa/inet.h>
29 #include <linux/if.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_packet.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/timerfd.h>
37 #include <sys/types.h>
38 #include <time.h>
39 #include <unistd.h>
40
41 #include "examples/common.h"
42
43 #define NSEC_PER_SEC 1000000000ULL
44 #define NSEC_PER_MSEC 1000000ULL
45
46 int calculate_avtp_time(uint32_t *avtp_time, uint32_t max_transit_time)
47 {
48 int res;
49 struct timespec tspec;
50 uint64_t ptime;
51
52 res = clock_gettime(CLOCK_REALTIME, &tspec);
53 if (res < 0) {
54 perror("Failed to get time");
55 return -1;
56 }
57
58 ptime = (tspec.tv_sec * NSEC_PER_SEC) +
59 (max_transit_time * NSEC_PER_MSEC) + tspec.tv_nsec;
60
61 *avtp_time = ptime % (1ULL << 32);
62
63 return 0;
64 }
65
66 int get_presentation_time(uint64_t avtp_time, struct timespec *tspec)
67 {
68 int res;
69 uint64_t ptime, now;
70
71 res = clock_gettime(CLOCK_REALTIME, tspec);
72 if (res < 0) {
73 perror("Failed to get time from PHC");
74 return -1;
75 }
76
77 now = (tspec->tv_sec * NSEC_PER_SEC) + tspec->tv_nsec;
78
79 /* The avtp_timestamp within AAF packet is the lower part (32
80 * less-significant bits) from presentation time calculated by the
81 * talker.
82 */
83 ptime = (now & 0xFFFFFFFF00000000ULL) | avtp_time;
84
85 /* If 'ptime' is less than the 'now', it means the higher part
86 * from 'ptime' needs to be incremented by 1 in order to recover the
87 * presentation time set by the talker.
88 */
89 if (ptime < now)
90 ptime += (1ULL << 32);
91
92 tspec->tv_sec = ptime / NSEC_PER_SEC;
93 tspec->tv_nsec = ptime % NSEC_PER_SEC;
94
95 return 0;
96 }
97
98 int setup_socket_address(int fd, const char *ifname, uint8_t macaddr[],
99 int protocol, struct sockaddr_ll *sk_addr)
100 {
101 int res;
102 struct ifreq req;
103
104 snprintf(req.ifr_name, sizeof(req.ifr_name), "%s", ifname);
105 res = ioctl(fd, SIOCGIFINDEX, &req);
106 if (res < 0) {
107 perror("Failed to get interface index");
108 return -1;
109 }
110
111 sk_addr->sll_family = AF_PACKET;
112 sk_addr->sll_protocol = htons(protocol);
113 sk_addr->sll_halen = ETH_ALEN;
114 sk_addr->sll_ifindex = req.ifr_ifindex;
115 memcpy(sk_addr->sll_addr, macaddr, ETH_ALEN);
116
117 return 0;
118 }
119
120 int create_talker_socket(int priority)
121 {
122 int fd, res;
123
124 fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_TSN));
125 if (fd < 0) {
126 perror("Failed to open socket");
127 return -1;
128 }
129
130 if (priority != -1) {
131 res = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority,
132 sizeof(priority));
133 if (res < 0) {
134 perror("Failed to set priority");
135 goto err;
136 }
137 }
138
139 return fd;
140
141 err:
142 close(fd);
143 return -1;
144 }
145
146 int create_listener_socket(char *ifname, uint8_t macaddr[], int protocol)
147 {
148 int fd, res;
149 struct packet_mreq mreq;
150
151 struct sockaddr_ll sk_addr;
152
153 fd = socket(AF_PACKET, SOCK_DGRAM, htons(protocol));
154 if (fd < 0) {
155 perror("Failed to open socket");
156 return -1;
157 }
158
159 res = setup_socket_address(fd, ifname, macaddr, protocol, &sk_addr);
160 if (res < 0)
161 goto err;
162
163 res = bind(fd, (struct sockaddr *) &sk_addr, sizeof(sk_addr));
164 if (res < 0) {
165 perror("Couldn't bind() to interface");
166 goto err;
167 }
168
169 mreq.mr_ifindex = sk_addr.sll_ifindex;
170 mreq.mr_type = PACKET_MR_MULTICAST;
171 mreq.mr_alen = ETH_ALEN;
172 memcpy(&mreq.mr_address, macaddr, ETH_ALEN);
173
174 res = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
175 &mreq, sizeof(struct packet_mreq));
176 if (res < 0) {
177 perror("Couldn't set PACKET_ADD_MEMBERSHIP");
178 goto err;
179 }
180
181 return fd;
182
183 err:
184 close(fd);
185 return -1;
186 }
187
188 int arm_timer(int fd, struct timespec *tspec)
189 {
190 int res;
191 struct itimerspec timer_spec = { 0 };
192
193 timer_spec.it_value.tv_sec = tspec->tv_sec;
194 timer_spec.it_value.tv_nsec = tspec->tv_nsec;
195
196 res = timerfd_settime(fd, TFD_TIMER_ABSTIME, &timer_spec, NULL);
197 if (res < 0) {
198 perror("Failed to set timer");
199 return -1;
200 }
201
202 return 0;
203 }
204
205 int present_data(uint8_t *data, size_t len)
206 {
207 ssize_t n;
208
209 n = write(STDOUT_FILENO, data, len);
210 if (n < 0 || n != len) {
211 perror("Failed to write()");
212 return -1;
213 }
214
215 return 0;
216 }