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;
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             status = A2D_SUCCESS;
131             if (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL ) {
132                 status = A2D_BAD_MIN_BITPOOL;
133             }
134 
135             if (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
136                     p_ie->max_bitpool < p_ie->min_bitpool) {
137                 status = A2D_BAD_MAX_BITPOOL;
138             }
139 
140             if (for_caps == FALSE) {
141                 if (A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT) {
142                     status = A2D_BAD_SAMP_FREQ;
143                 }
144                 if (A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT) {
145                     status = A2D_BAD_CH_MODE;
146                 }
147                 if (A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT) {
148                     status = A2D_BAD_BLOCK_LEN;
149                 }
150                 if (A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT) {
151                     status = A2D_BAD_SUBBANDS;
152                 }
153                 if (A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT) {
154                     status = A2D_BAD_ALLOC_MTHD;
155                 }
156             }
157         }
158     }
159     return status;
160 }
161 
162 /******************************************************************************
163 **
164 ** Function         A2D_BldSbcMplHdr
165 **
166 ** Description      This function is called by an application to parse
167 **                  the SBC Media Payload header.
168 **                  Input Parameters:
169 **                      frag:  1, if fragmented. 0, otherwise.
170 **
171 **                      start:  1, if the starting packet of a fragmented frame.
172 **
173 **                      last:  1, if the last packet of a fragmented frame.
174 **
175 **                      num:  If frag is 1, this is the number of remaining fragments
176 **                            (including this fragment) of this frame.
177 **                            If frag is 0, this is the number of frames in this packet.
178 **
179 **                  Output Parameters:
180 **                      p_dst:  the resulting media payload header byte sequence.
181 **
182 ** Returns          void.
183 ******************************************************************************/
A2D_BldSbcMplHdr(UINT8 * p_dst,BOOLEAN frag,BOOLEAN start,BOOLEAN last,UINT8 num)184 void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
185 {
186     if (p_dst) {
187         *p_dst  = 0;
188         if (frag) {
189             *p_dst  |= A2D_SBC_HDR_F_MSK;
190         }
191         if (start) {
192             *p_dst  |= A2D_SBC_HDR_S_MSK;
193         }
194         if (last) {
195             *p_dst  |= A2D_SBC_HDR_L_MSK;
196         }
197         *p_dst  |= (A2D_SBC_HDR_NUM_MSK & num);
198     }
199 }
200 
201 /******************************************************************************
202 **
203 ** Function         A2D_ParsSbcMplHdr
204 **
205 ** Description      This function is called by an application to parse
206 **                  the SBC Media Payload header.
207 **                  Input Parameters:
208 **                      p_src:  the byte sequence to parse..
209 **
210 **                  Output Parameters:
211 **                      frag:  1, if fragmented. 0, otherwise.
212 **
213 **                      start:  1, if the starting packet of a fragmented frame.
214 **
215 **                      last:  1, if the last packet of a fragmented frame.
216 **
217 **                      num:  If frag is 1, this is the number of remaining fragments
218 **                            (including this fragment) of this frame.
219 **                            If frag is 0, this is the number of frames in this packet.
220 **
221 ** Returns          void.
222 ******************************************************************************/
A2D_ParsSbcMplHdr(UINT8 * p_src,BOOLEAN * p_frag,BOOLEAN * p_start,BOOLEAN * p_last,UINT8 * p_num)223 void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
224 {
225     if (p_src && p_frag && p_start && p_last && p_num) {
226         *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE : FALSE;
227         *p_start = (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE : FALSE;
228         *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE : FALSE;
229         *p_num  = (*p_src & A2D_SBC_HDR_NUM_MSK);
230     }
231 }
232 
233 #endif /* #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) */
234