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 "bt_target.h"
27
28#include <string.h>
29#include "a2d_api.h"
30#include "a2d_int.h"
31#include "a2d_sbc.h"
32#include "bt_utils.h"
33
34/******************************************************************************
35**
36** Function         A2D_BldSbcInfo
37**
38** Description      This function is called by an application to build
39**                  the SBC Media Codec Capabilities byte sequence
40**                  beginning from the LOSC octet.
41**                  Input Parameters:
42**                      media_type:  Indicates Audio, or Multimedia.
43**
44**                      p_ie:  The SBC Codec Information Element information.
45**
46**                  Output Parameters:
47**                      p_result:  the resulting codec info byte sequence.
48**
49** Returns          A2D_SUCCESS if function execution succeeded.
50**                  Error status code, otherwise.
51******************************************************************************/
52tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
53{
54    tA2D_STATUS status;
55
56    if( p_ie == NULL || p_result == NULL ||
57        (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
58        (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
59        (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
60        (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
61        (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
62        (p_ie->max_bitpool < p_ie->min_bitpool) ||
63        (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
64        (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
65        (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
66        (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) )
67    {
68        /* if any unused bit is set */
69        status = A2D_INVALID_PARAMS;
70    }
71    else
72    {
73        status = A2D_SUCCESS;
74        *p_result++ = A2D_SBC_INFO_LEN;
75        *p_result++ = media_type;
76        *p_result++ = A2D_MEDIA_CT_SBC;
77
78        /* Media Codec Specific Information Element */
79        *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
80
81        *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
82
83        *p_result++ = p_ie->min_bitpool;
84        *p_result   = p_ie->max_bitpool;
85    }
86    return status;
87}
88
89/******************************************************************************
90**
91** Function         A2D_ParsSbcInfo
92**
93** Description      This function is called by an application to parse
94**                  the SBC Media Codec Capabilities byte sequence
95**                  beginning from the LOSC octet.
96**                  Input Parameters:
97**                      p_info:  the byte sequence to parse.
98**
99**                      for_caps:  TRUE, if the byte sequence is for get capabilities response.
100**
101**                  Output Parameters:
102**                      p_ie:  The SBC Codec Information Element information.
103**
104** Returns          A2D_SUCCESS if function execution succeeded.
105**                  Error status code, otherwise.
106******************************************************************************/
107tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, const UINT8 *p_info,
108                            BOOLEAN for_caps)
109{
110    tA2D_STATUS status = A2D_SUCCESS;
111    UINT8 losc;
112
113    if (p_ie == NULL || p_info == NULL)
114        return A2D_INVALID_PARAMS;
115
116    losc = *p_info;
117    p_info += 2;
118
119    /* If the function is called for the wrong Media Type or Media Codec Type */
120    if (losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC)
121        return A2D_WRONG_CODEC;
122
123    p_info++;
124    p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
125    p_ie->ch_mode   = *p_info & A2D_SBC_IE_CH_MD_MSK;
126    p_info++;
127    p_ie->block_len     = *p_info & A2D_SBC_IE_BLOCKS_MSK;
128    p_ie->num_subbands  = *p_info & A2D_SBC_IE_SUBBAND_MSK;
129    p_ie->alloc_mthd    = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
130    p_info++;
131    p_ie->min_bitpool = *p_info++;
132    p_ie->max_bitpool = *p_info;
133    if (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL )
134        status = A2D_BAD_MIN_BITPOOL;
135
136    if (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
137         p_ie->max_bitpool < p_ie->min_bitpool)
138        status = A2D_BAD_MAX_BITPOOL;
139
140    if (for_caps != FALSE)
141        return status;
142
143    if (A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT)
144        status = A2D_BAD_SAMP_FREQ;
145    if (A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT)
146        status = A2D_BAD_CH_MODE;
147    if (A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT)
148        status = A2D_BAD_BLOCK_LEN;
149    if (A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT)
150        status = A2D_BAD_SUBBANDS;
151    if (A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT)
152        status = A2D_BAD_ALLOC_MTHD;
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******************************************************************************/
179void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
180{
181    if(p_dst)
182    {
183        *p_dst  = 0;
184        if(frag)
185            *p_dst  |= A2D_SBC_HDR_F_MSK;
186        if(start)
187            *p_dst  |= A2D_SBC_HDR_S_MSK;
188        if(last)
189            *p_dst  |= A2D_SBC_HDR_L_MSK;
190        *p_dst  |= (A2D_SBC_HDR_NUM_MSK & num);
191    }
192}
193
194/******************************************************************************
195**
196** Function         A2D_ParsSbcMplHdr
197**
198** Description      This function is called by an application to parse
199**                  the SBC Media Payload header.
200**                  Input Parameters:
201**                      p_src:  the byte sequence to parse..
202**
203**                  Output Parameters:
204**                      frag:  1, if fragmented. 0, otherwise.
205**
206**                      start:  1, if the starting packet of a fragmented frame.
207**
208**                      last:  1, if the last packet of a fragmented frame.
209**
210**                      num:  If frag is 1, this is the number of remaining fragments
211**                            (including this fragment) of this frame.
212**                            If frag is 0, this is the number of frames in this packet.
213**
214** Returns          void.
215******************************************************************************/
216void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
217{
218    if(p_src && p_frag && p_start && p_last && p_num)
219    {
220        *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE: FALSE;
221        *p_start= (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE: FALSE;
222        *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE: FALSE;
223        *p_num  = (*p_src & A2D_SBC_HDR_NUM_MSK);
224    }
225}
226
227