Debian Codegraph @

src/avtp_stream.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 <stddef.h>
30
31 #include "avtp.h"
32 #include "avtp_stream.h"
33 #include "util.h"
34
35 #define SHIFT_SV (31 - 8)
36 #define SHIFT_MR (31 - 12)
37 #define SHIFT_TV (31 - 15)
38 #define SHIFT_SEQ_NUM (31 - 23)
39 #define SHIFT_STREAM_DATA_LEN (31 - 15)
40
41 #define MASK_SV (BITMASK(1) << SHIFT_SV)
42 #define MASK_MR (BITMASK(1) << SHIFT_MR)
43 #define MASK_TV (BITMASK(1) << SHIFT_TV)
44 #define MASK_SEQ_NUM (BITMASK(8) << SHIFT_SEQ_NUM)
45 #define MASK_TU (BITMASK(1))
46 #define MASK_STREAM_DATA_LEN (BITMASK(16) << SHIFT_STREAM_DATA_LEN)
47
48 static int get_field_value(const struct avtp_stream_pdu *pdu,
49 enum avtp_stream_field field, uint64_t *val)
50 {
51 uint32_t bitmap, mask;
52 uint8_t shift;
53
54 switch (field) {
55 case AVTP_STREAM_FIELD_SV:
56 mask = MASK_SV;
57 shift = SHIFT_SV;
58 bitmap = ntohl(pdu->subtype_data);
59 break;
60 case AVTP_STREAM_FIELD_MR:
61 mask = MASK_MR;
62 shift = SHIFT_MR;
63 bitmap = ntohl(pdu->subtype_data);
64 break;
65 case AVTP_STREAM_FIELD_TV:
66 mask = MASK_TV;
67 shift = SHIFT_TV;
68 bitmap = ntohl(pdu->subtype_data);
69 break;
70 case AVTP_STREAM_FIELD_SEQ_NUM:
71 mask = MASK_SEQ_NUM;
72 shift = SHIFT_SEQ_NUM;
73 bitmap = ntohl(pdu->subtype_data);
74 break;
75 case AVTP_STREAM_FIELD_TU:
76 mask = MASK_TU;
77 shift = 0;
78 bitmap = ntohl(pdu->subtype_data);
79 break;
80 case AVTP_STREAM_FIELD_STREAM_DATA_LEN:
81 mask = MASK_STREAM_DATA_LEN;
82 shift = SHIFT_STREAM_DATA_LEN;
83 bitmap = ntohl(pdu->packet_info);
84 break;
85 default:
86 return -EINVAL;
87 }
88
89 *val = BITMAP_GET_VALUE(bitmap, mask, shift);
90
91 return 0;
92 }
93
94 int avtp_stream_pdu_get(const struct avtp_stream_pdu *pdu,
95 enum avtp_stream_field field, uint64_t *val)
96 {
97 int res;
98
99 if (!pdu || !val)
100 return -EINVAL;
101
102 switch (field) {
103 case AVTP_STREAM_FIELD_SV:
104 case AVTP_STREAM_FIELD_MR:
105 case AVTP_STREAM_FIELD_TV:
106 case AVTP_STREAM_FIELD_SEQ_NUM:
107 case AVTP_STREAM_FIELD_TU:
108 case AVTP_STREAM_FIELD_STREAM_DATA_LEN:
109 res = get_field_value(pdu, field, val);
110 break;
111 case AVTP_STREAM_FIELD_TIMESTAMP:
112 *val = ntohl(pdu->avtp_time);
113 res = 0;
114 break;
115 case AVTP_STREAM_FIELD_STREAM_ID:
116 *val = be64toh(pdu->stream_id);
117 res = 0;
118 break;
119 default:
120 return -EINVAL;
121 }
122
123 return res;
124 }
125
126 static int set_field_value(struct avtp_stream_pdu *pdu,
127 enum avtp_stream_field field, uint64_t val)
128 {
129 uint32_t bitmap, mask;
130 uint8_t shift;
131 void *ptr;
132
133 switch (field) {
134 case AVTP_STREAM_FIELD_SV:
135 mask = MASK_SV;
136 shift = SHIFT_SV;
137 ptr = &pdu->subtype_data;
138 break;
139 case AVTP_STREAM_FIELD_MR:
140 mask = MASK_MR;
141 shift = SHIFT_MR;
142 ptr = &pdu->subtype_data;
143 break;
144 case AVTP_STREAM_FIELD_TV:
145 mask = MASK_TV;
146 shift = SHIFT_TV;
147 ptr = &pdu->subtype_data;
148 break;
149 case AVTP_STREAM_FIELD_SEQ_NUM:
150 mask = MASK_SEQ_NUM;
151 shift = SHIFT_SEQ_NUM;
152 ptr = &pdu->subtype_data;
153 break;
154 case AVTP_STREAM_FIELD_TU:
155 mask = MASK_TU;
156 shift = 0;
157 ptr = &pdu->subtype_data;
158 break;
159 case AVTP_STREAM_FIELD_STREAM_DATA_LEN:
160 mask = MASK_STREAM_DATA_LEN;
161 shift = SHIFT_STREAM_DATA_LEN;
162 ptr = &pdu->packet_info;
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 bitmap = get_unaligned_be32(ptr);
169
170 BITMAP_SET_VALUE(bitmap, val, mask, shift);
171
172 put_unaligned_be32(bitmap, ptr);
173
174 return 0;
175 }
176
177 int avtp_stream_pdu_set(struct avtp_stream_pdu *pdu,
178 enum avtp_stream_field field, uint64_t value)
179 {
180 int res;
181
182 if (!pdu)
183 return -EINVAL;
184
185 switch (field) {
186 case AVTP_STREAM_FIELD_SV:
187 case AVTP_STREAM_FIELD_MR:
188 case AVTP_STREAM_FIELD_TV:
189 case AVTP_STREAM_FIELD_SEQ_NUM:
190 case AVTP_STREAM_FIELD_TU:
191 case AVTP_STREAM_FIELD_STREAM_DATA_LEN:
192 res = set_field_value(pdu, field, value);
193 break;
194 case AVTP_STREAM_FIELD_TIMESTAMP:
195 pdu->avtp_time = htonl(value);
196 res = 0;
197 break;
198 case AVTP_STREAM_FIELD_STREAM_ID:
199 pdu->stream_id = htobe64(value);
200 res = 0;
201 break;
202 default:
203 return -EINVAL;
204 }
205
206 return res;
207 }