Debian Codegraph @

src/avtp.c

raw
1 /*
2 * Copyright (c) 2017, 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 "util.h"
33
34 #define SHIFT_SUBTYPE (31 - 7)
35 #define SHIFT_VERSION (31 - 11)
36
37 #define MASK_SUBTYPE (BITMASK(8) << SHIFT_SUBTYPE)
38 #define MASK_VERSION (BITMASK(3) << SHIFT_VERSION)
39
40 int avtp_pdu_get(const struct avtp_common_pdu *pdu, enum avtp_field field,
41 uint32_t *val)
42 {
43 uint32_t bitmap, mask;
44 uint8_t shift;
45
46 if (!pdu || !val)
47 return -EINVAL;
48
49 switch (field) {
50 case AVTP_FIELD_SUBTYPE:
51 mask = MASK_SUBTYPE;
52 shift = SHIFT_SUBTYPE;
53 break;
54 case AVTP_FIELD_VERSION:
55 mask = MASK_VERSION;
56 shift = SHIFT_VERSION;
57 break;
58 default:
59 return -EINVAL;
60 }
61
62 bitmap = ntohl(pdu->subtype_data);
63
64 *val = BITMAP_GET_VALUE(bitmap, mask, shift);
65
66 return 0;
67 }
68
69 int avtp_pdu_set(struct avtp_common_pdu *pdu, enum avtp_field field,
70 uint32_t value)
71 {
72 uint32_t bitmap, mask;
73 uint8_t shift;
74
75 if (!pdu)
76 return -EINVAL;
77
78 switch (field) {
79 case AVTP_FIELD_SUBTYPE:
80 mask = MASK_SUBTYPE;
81 shift = SHIFT_SUBTYPE;
82 break;
83 case AVTP_FIELD_VERSION:
84 mask = MASK_VERSION;
85 shift = SHIFT_VERSION;
86 break;
87 default:
88 return -EINVAL;
89 }
90
91 bitmap = ntohl(pdu->subtype_data);
92
93 BITMAP_SET_VALUE(bitmap, value, mask, shift);
94
95 pdu->subtype_data = htonl(bitmap);
96
97 return 0;
98 }