1 /** @file
2  * @brief Advance Audio Distribution Profile - SBC Codec header.
3  */
4 /*
5  * SPDX-License-Identifier: Apache-2.0
6  * Copyright (c) 2015-2016 Intel Corporation
7  * Copyright (c) 2021 NXP
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_A2DP_CODEC_H_
22 #define ZEPHYR_INCLUDE_BLUETOOTH_A2DP_CODEC_H_
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /* Sampling Frequency */
29 #define A2DP_SBC_SAMP_FREQ_16000 BIT(7)
30 #define A2DP_SBC_SAMP_FREQ_32000 BIT(6)
31 #define A2DP_SBC_SAMP_FREQ_44100 BIT(5)
32 #define A2DP_SBC_SAMP_FREQ_48000 BIT(4)
33 
34 /* Channel Mode */
35 #define A2DP_SBC_CH_MODE_MONO  BIT(3)
36 #define A2DP_SBC_CH_MODE_DUAL  BIT(2)
37 #define A2DP_SBC_CH_MODE_STREO BIT(1)
38 #define A2DP_SBC_CH_MODE_JOINT BIT(0)
39 
40 /* Block Length */
41 #define A2DP_SBC_BLK_LEN_4  BIT(7)
42 #define A2DP_SBC_BLK_LEN_8  BIT(6)
43 #define A2DP_SBC_BLK_LEN_12 BIT(5)
44 #define A2DP_SBC_BLK_LEN_16 BIT(4)
45 
46 /* Subbands */
47 #define A2DP_SBC_SUBBAND_4 BIT(3)
48 #define A2DP_SBC_SUBBAND_8 BIT(2)
49 
50 /* Allocation Method */
51 #define A2DP_SBC_ALLOC_MTHD_SNR      BIT(1)
52 #define A2DP_SBC_ALLOC_MTHD_LOUDNESS BIT(0)
53 
54 #define BT_A2DP_SBC_SAMP_FREQ(cap)    ((cap->config[0] >> 4) & 0x0f)
55 #define BT_A2DP_SBC_CHAN_MODE(cap)    ((cap->config[0]) & 0x0f)
56 #define BT_A2DP_SBC_BLK_LEN(cap)      ((cap->config[1] >> 4) & 0x0f)
57 #define BT_A2DP_SBC_SUB_BAND(cap)     ((cap->config[1] >> 2) & 0x03)
58 #define BT_A2DP_SBC_ALLOC_MTHD(cap)   ((cap->config[1]) & 0x03)
59 
60 /** @brief SBC Codec */
61 struct bt_a2dp_codec_sbc_params {
62 	/** First two octets of configuration */
63 	uint8_t config[2];
64 	/** Minimum Bitpool Value */
65 	uint8_t min_bitpool;
66 	/** Maximum Bitpool Value */
67 	uint8_t max_bitpool;
68 } __packed;
69 
70 /** If the F bit is set to 0, this field indicates the number of frames contained
71  *  in this packet. If the F bit is set to 1, this field indicates the number
72  *  of remaining fragments, including the current fragment.
73  *  Therefore, the last counter value shall be one.
74  */
75 #define BT_A2DP_SBC_MEDIA_HDR_NUM_FRAMES_GET(hdr) FIELD_GET(GENMASK(3, 0), (hdr))
76 /** Set to 1 for the last packet of a fragmented SBC frame, otherwise set to 0. */
77 #define BT_A2DP_SBC_MEDIA_HDR_L_GET(hdr)          FIELD_GET(BIT(5), (hdr))
78 /** Set to 1 for the starting packet of a fragmented SBC frame, otherwise set to 0. */
79 #define BT_A2DP_SBC_MEDIA_HDR_S_GET(hdr)          FIELD_GET(BIT(6), (hdr))
80 /** Set to 1 if the SBC frame is fragmented, otherwise set to 0. */
81 #define BT_A2DP_SBC_MEDIA_HDR_F_GET(hdr)          FIELD_GET(BIT(7), (hdr))
82 
83 /** If the F bit is set to 0, this field indicates the number of frames contained
84  *  in this packet. If the F bit is set to 1, this field indicates the number
85  *  of remaining fragments, including the current fragment.
86  *  Therefore, the last counter value shall be one.
87  */
88 #define BT_A2DP_SBC_MEDIA_HDR_NUM_FRAMES_SET(hdr, val)\
89 	hdr = ((hdr) & ~GENMASK(3, 0)) | FIELD_PREP(GENMASK(3, 0), (val))
90 /** Set to 1 for the last packet of a fragmented SBC frame, otherwise set to 0. */
91 #define BT_A2DP_SBC_MEDIA_HDR_L_SET(hdr, val)\
92 	hdr = ((hdr) & ~BIT(5)) | FIELD_PREP(BIT(5), (val))
93 /** Set to 1 for the starting packet of a fragmented SBC frame, otherwise set to 0. */
94 #define BT_A2DP_SBC_MEDIA_HDR_S_SET(hdr, val)\
95 	hdr = ((hdr) & ~BIT(6)) | FIELD_PREP(BIT(6), (val))
96 /** Set to 1 if the SBC frame is fragmented, otherwise set to 0. */
97 #define BT_A2DP_SBC_MEDIA_HDR_F_SET(hdr, val)\
98 	hdr = ((hdr) & ~BIT(7)) | FIELD_PREP(BIT(7), (val))
99 
100 #define BT_A2DP_SBC_MEDIA_HDR_ENCODE(num_frames, l, s, f)\
101 	FIELD_PREP(GENMASK(3, 0), num_frames) | FIELD_PREP(BIT(5), l) |\
102 	FIELD_PREP(BIT(6), s) | FIELD_PREP(BIT(7), f)
103 
104 /** @brief get channel num of a2dp sbc config.
105  *
106  *  @param sbc_codec The a2dp sbc parameter.
107  *
108  *  @return the channel num.
109  */
110 uint8_t bt_a2dp_sbc_get_channel_num(struct bt_a2dp_codec_sbc_params *sbc_codec);
111 
112 /** @brief get sample rate of a2dp sbc config.
113  *
114  *  @param sbc_codec The a2dp sbc parameter.
115  *
116  *  @return the sample rate.
117  */
118 uint32_t bt_a2dp_sbc_get_sampling_frequency(struct bt_a2dp_codec_sbc_params *sbc_codec);
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_A2DP_CODEC_H_ */
125