1 /******************************************************************************
2 *
3 * Copyright (C) 2002-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * Utility functions to help build and parse SBC Codec Information Element
22 * and Media Payload.
23 *
24 ******************************************************************************/
25
26 #include "common/bt_target.h"
27
28 #include <string.h>
29 #include "stack/a2d_api.h"
30 #include "a2d_int.h"
31 #include "stack/a2d_sbc.h"
32 #include "common/bt_defs.h"
33
34 #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
35
36 /******************************************************************************
37 **
38 ** Function A2D_BldSbcInfo
39 **
40 ** Description This function is called by an application to build
41 ** the SBC Media Codec Capabilities byte sequence
42 ** beginning from the LOSC octet.
43 ** Input Parameters:
44 ** media_type: Indicates Audio, or Multimedia.
45 **
46 ** p_ie: The SBC Codec Information Element information.
47 **
48 ** Output Parameters:
49 ** p_result: the resulting codec info byte sequence.
50 **
51 ** Returns A2D_SUCCESS if function execution succeeded.
52 ** Error status code, otherwise.
53 ******************************************************************************/
A2D_BldSbcInfo(UINT8 media_type,tA2D_SBC_CIE * p_ie,UINT8 * p_result)54 tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
55 {
56 tA2D_STATUS status;
57
58 if ( p_ie == NULL || p_result == NULL ||
59 (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
60 (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
61 (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
62 (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
63 (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
64 (p_ie->max_bitpool < p_ie->min_bitpool) ||
65 (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
66 (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
67 (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
68 (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) ) {
69 /* if any unused bit is set */
70 status = A2D_INVALID_PARAMS;
71 } else {
72 status = A2D_SUCCESS;
73 *p_result++ = A2D_SBC_INFO_LEN;
74 *p_result++ = media_type;
75 *p_result++ = A2D_MEDIA_CT_SBC;
76
77 /* Media Codec Specific Information Element */
78 *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
79
80 *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
81
82 *p_result++ = p_ie->min_bitpool;
83 *p_result = p_ie->max_bitpool;
84 }
85 return status;
86 }
87
88 /******************************************************************************
89 **
90 ** Function A2D_ParsSbcInfo
91 **
92 ** Description This function is called by an application to parse
93 ** the SBC Media Codec Capabilities byte sequence
94 ** beginning from the LOSC octet.
95 ** Input Parameters:
96 ** p_info: the byte sequence to parse.
97 **
98 ** for_caps: TRUE, if the byte sequence is for get capabilities response.
99 **
100 ** Output Parameters:
101 ** p_ie: The SBC Codec Information Element information.
102 **
103 ** Returns A2D_SUCCESS if function execution succeeded.
104 ** Error status code, otherwise.
105 ******************************************************************************/
A2D_ParsSbcInfo(tA2D_SBC_CIE * p_ie,UINT8 * p_info,BOOLEAN for_caps)106 tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
107 {
108 tA2D_STATUS status = A2D_SUCCESS;
109 UINT8 losc;
110
111 if ( p_ie == NULL || p_info == NULL) {
112 status = A2D_INVALID_PARAMS;
113 } else {
114 losc = *p_info++;
115 p_info++;
116 /* If the function is called for the wrong Media Type or Media Codec Type */
117 if (losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC) {
118 status = A2D_WRONG_CODEC;
119 } else {
120 p_info++;
121 p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
122 p_ie->ch_mode = *p_info & A2D_SBC_IE_CH_MD_MSK;
123 p_info++;
124 p_ie->block_len = *p_info & A2D_SBC_IE_BLOCKS_MSK;
125 p_ie->num_subbands = *p_info & A2D_SBC_IE_SUBBAND_MSK;
126 p_ie->alloc_mthd = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
127 p_info++;
128 p_ie->min_bitpool = *p_info++;
129 p_ie->max_bitpool = *p_info;
130 if (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL ) {
131 status = A2D_BAD_MIN_BITPOOL;
132 }
133
134 if (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
135 p_ie->max_bitpool < p_ie->min_bitpool) {
136 status = A2D_BAD_MAX_BITPOOL;
137 }
138
139 if (for_caps == FALSE) {
140 if (A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT) {
141 status = A2D_BAD_SAMP_FREQ;
142 } else if (A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT) {
143 status = A2D_BAD_CH_MODE;
144 } else if (A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT) {
145 status = A2D_BAD_BLOCK_LEN;
146 } else if (A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT) {
147 status = A2D_BAD_SUBBANDS;
148 } else if (A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT) {
149 status = A2D_BAD_ALLOC_MTHD;
150 }
151 }
152 }
153 }
154 return status;
155 }
156
157 /******************************************************************************
158 **
159 ** Function A2D_BldSbcMplHdr
160 **
161 ** Description This function is called by an application to parse
162 ** the SBC Media Payload header.
163 ** Input Parameters:
164 ** frag: 1, if fragmented. 0, otherwise.
165 **
166 ** start: 1, if the starting packet of a fragmented frame.
167 **
168 ** last: 1, if the last packet of a fragmented frame.
169 **
170 ** num: If frag is 1, this is the number of remaining fragments
171 ** (including this fragment) of this frame.
172 ** If frag is 0, this is the number of frames in this packet.
173 **
174 ** Output Parameters:
175 ** p_dst: the resulting media payload header byte sequence.
176 **
177 ** Returns void.
178 ******************************************************************************/
A2D_BldSbcMplHdr(UINT8 * p_dst,BOOLEAN frag,BOOLEAN start,BOOLEAN last,UINT8 num)179 void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
180 {
181 if (p_dst) {
182 *p_dst = 0;
183 if (frag) {
184 *p_dst |= A2D_SBC_HDR_F_MSK;
185 }
186 if (start) {
187 *p_dst |= A2D_SBC_HDR_S_MSK;
188 }
189 if (last) {
190 *p_dst |= A2D_SBC_HDR_L_MSK;
191 }
192 *p_dst |= (A2D_SBC_HDR_NUM_MSK & num);
193 }
194 }
195
196 /******************************************************************************
197 **
198 ** Function A2D_ParsSbcMplHdr
199 **
200 ** Description This function is called by an application to parse
201 ** the SBC Media Payload header.
202 ** Input Parameters:
203 ** p_src: the byte sequence to parse..
204 **
205 ** Output Parameters:
206 ** frag: 1, if fragmented. 0, otherwise.
207 **
208 ** start: 1, if the starting packet of a fragmented frame.
209 **
210 ** last: 1, if the last packet of a fragmented frame.
211 **
212 ** num: If frag is 1, this is the number of remaining fragments
213 ** (including this fragment) of this frame.
214 ** If frag is 0, this is the number of frames in this packet.
215 **
216 ** Returns void.
217 ******************************************************************************/
A2D_ParsSbcMplHdr(UINT8 * p_src,BOOLEAN * p_frag,BOOLEAN * p_start,BOOLEAN * p_last,UINT8 * p_num)218 void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
219 {
220 if (p_src && p_frag && p_start && p_last && p_num) {
221 *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE : FALSE;
222 *p_start = (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE : FALSE;
223 *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE : FALSE;
224 *p_num = (*p_src & A2D_SBC_HDR_NUM_MSK);
225 }
226 }
227
228 #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */
229