summaryrefslogtreecommitdiffstats
path: root/stack/a2dp
diff options
context:
space:
mode:
authorAndre Eisenbach <andre@broadcom.com>2012-02-22 13:18:21 -0800
committerMatthew Xie <mattx@google.com>2012-07-14 11:19:11 -0700
commite448862a47c08eb23185aaed574b39264f5005fc (patch)
tree2bc6246e3091315e77224fd798ea2fe8074ef972 /stack/a2dp
parenta2ca4b83ab8bbbfd8d5f6693e927ed4b82094624 (diff)
downloadexternal_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.zip
external_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.tar.gz
external_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.tar.bz2
Initial Bluedroid stack commit
Diffstat (limited to 'stack/a2dp')
-rw-r--r--stack/a2dp/a2d_api.c382
-rw-r--r--stack/a2dp/a2d_int.h70
-rw-r--r--stack/a2dp/a2d_m12.c141
-rw-r--r--stack/a2dp/a2d_m24.c149
-rw-r--r--stack/a2dp/a2d_sbc.c386
5 files changed, 1128 insertions, 0 deletions
diff --git a/stack/a2dp/a2d_api.c b/stack/a2dp/a2d_api.c
new file mode 100644
index 0000000..dbff646
--- /dev/null
+++ b/stack/a2dp/a2d_api.c
@@ -0,0 +1,382 @@
+/*****************************************************************************
+**
+** Name: a2d_api.c
+**
+** Description:Common API for the Advanced Audio Distribution Profile (A2DP)
+**
+** Copyright (c) 2002-2009, Broadcom Corp., All Rights Reserved.
+** WIDCOMM Bluetooth Core. Proprietary and confidential.
+**
+*****************************************************************************/
+#include <string.h>
+#include "bt_target.h"
+#include "sdpdefs.h"
+#include "a2d_api.h"
+#include "a2d_int.h"
+#include "avdt_api.h"
+
+/*****************************************************************************
+** Global data
+*****************************************************************************/
+#if A2D_DYNAMIC_MEMORY == FALSE
+tA2D_CB a2d_cb;
+#endif
+
+
+/******************************************************************************
+**
+** Function a2d_sdp_cback
+**
+** Description This is the SDP callback function used by A2D_FindService.
+** This function will be executed by SDP when the service
+** search is completed. If the search is successful, it
+** finds the first record in the database that matches the
+** UUID of the search. Then retrieves various parameters
+** from the record. When it is finished it calls the
+** application callback function.
+**
+** Returns Nothing.
+**
+******************************************************************************/
+static void a2d_sdp_cback(UINT16 status)
+{
+ tSDP_DISC_REC *p_rec = NULL;
+ tSDP_DISC_ATTR *p_attr;
+ BOOLEAN found = FALSE;
+ tA2D_Service a2d_svc;
+ tSDP_PROTOCOL_ELEM elem;
+
+ A2D_TRACE_API1("a2d_sdp_cback status: %d", status);
+
+ if (status == SDP_SUCCESS)
+ {
+ /* loop through all records we found */
+ do
+ {
+ /* get next record; if none found, we're done */
+ if ((p_rec = SDP_FindServiceInDb(a2d_cb.find.p_db,
+ a2d_cb.find.service_uuid, p_rec)) == NULL)
+ {
+ break;
+ }
+ memset(&a2d_svc, 0, sizeof(tA2D_Service));
+
+ /* get service name */
+ if ((p_attr = SDP_FindAttributeInRec(p_rec,
+ ATTR_ID_SERVICE_NAME)) != NULL)
+ {
+ a2d_svc.p_service_name = (char *) p_attr->attr_value.v.array;
+ a2d_svc.service_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
+ }
+
+ /* get provider name */
+ if ((p_attr = SDP_FindAttributeInRec(p_rec,
+ ATTR_ID_PROVIDER_NAME)) != NULL)
+ {
+ a2d_svc.p_provider_name = (char *) p_attr->attr_value.v.array;
+ a2d_svc.provider_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
+ }
+
+ /* get supported features */
+ if ((p_attr = SDP_FindAttributeInRec(p_rec,
+ ATTR_ID_SUPPORTED_FEATURES)) != NULL)
+ {
+ a2d_svc.features = p_attr->attr_value.v.u16;
+ }
+
+ /* get AVDTP version */
+ if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_AVDTP, &elem))
+ {
+ a2d_svc.avdt_version = elem.params[0];
+ A2D_TRACE_DEBUG1("avdt_version: 0x%x", a2d_svc.avdt_version);
+ }
+
+ /* we've got everything, we're done */
+ found = TRUE;
+ break;
+
+ } while (TRUE);
+ }
+
+ a2d_cb.find.service_uuid = 0;
+ /* return info from sdp record in app callback function */
+ if (a2d_cb.find.p_cback != NULL)
+ {
+ (*a2d_cb.find.p_cback)(found, &a2d_svc);
+ }
+
+ return;
+}
+
+/*******************************************************************************
+**
+** Function a2d_set_avdt_sdp_ver
+**
+** Description This function allows the script wrapper to change the
+** avdt version of a2dp.
+**
+** Returns None
+**
+*******************************************************************************/
+void a2d_set_avdt_sdp_ver (UINT16 avdt_sdp_ver)
+{
+ a2d_cb.avdt_sdp_ver = avdt_sdp_ver;
+}
+
+/******************************************************************************
+**
+** Function A2D_AddRecord
+**
+** Description This function is called by a server application to add
+** SRC or SNK information to an SDP record. Prior to
+** calling this function the application must call
+** SDP_CreateRecord() to create an SDP record.
+**
+** Input Parameters:
+** service_uuid: Indicates SRC or SNK.
+**
+** p_service_name: Pointer to a null-terminated character
+** string containing the service name.
+**
+** p_provider_name: Pointer to a null-terminated character
+** string containing the provider name.
+**
+** features: Profile supported features.
+**
+** sdp_handle: SDP handle returned by SDP_CreateRecord().
+**
+** Output Parameters:
+** None.
+**
+** Returns A2D_SUCCESS if function execution succeeded,
+** A2D_INVALID_PARAMS if bad parameters are given.
+** A2D_FAIL if function execution failed.
+**
+******************************************************************************/
+tA2D_STATUS A2D_AddRecord(UINT16 service_uuid, char *p_service_name, char *p_provider_name,
+ UINT16 features, UINT32 sdp_handle)
+{
+ UINT16 browse_list[1];
+ BOOLEAN result = TRUE;
+ UINT8 temp[8];
+ UINT8 *p;
+ tSDP_PROTOCOL_ELEM proto_list [A2D_NUM_PROTO_ELEMS];
+
+ A2D_TRACE_API1("A2D_AddRecord uuid: %x", service_uuid);
+
+ if( (sdp_handle == 0) ||
+ (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) )
+ return A2D_INVALID_PARAMS;
+
+ /* add service class id list */
+ result &= SDP_AddServiceClassIdList(sdp_handle, 1, &service_uuid);
+
+ memset((void*) proto_list, 0 , A2D_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
+
+ /* add protocol descriptor list */
+ proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
+ proto_list[0].num_params = 1;
+ proto_list[0].params[0] = AVDT_PSM;
+ proto_list[1].protocol_uuid = UUID_PROTOCOL_AVDTP;
+ proto_list[1].num_params = 1;
+ proto_list[1].params[0] = a2d_cb.avdt_sdp_ver;
+
+ result &= SDP_AddProtocolList(sdp_handle, A2D_NUM_PROTO_ELEMS, proto_list);
+
+ /* add profile descriptor list */
+ result &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_ADV_AUDIO_DISTRIBUTION, A2D_VERSION);
+
+ /* add supported feature */
+ if (features != 0)
+ {
+ p = temp;
+ UINT16_TO_BE_STREAM(p, features);
+ result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
+ (UINT32)2, (UINT8*)temp);
+ }
+
+ /* add provider name */
+ if (p_provider_name != NULL)
+ {
+ result &= SDP_AddAttribute(sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
+ (UINT32)(strlen(p_provider_name)+1), (UINT8 *) p_provider_name);
+ }
+
+ /* add service name */
+ if (p_service_name != NULL)
+ {
+ result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
+ (UINT32)(strlen(p_service_name)+1), (UINT8 *) p_service_name);
+ }
+
+ /* add browse group list */
+ browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
+ result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
+
+
+ return (result ? A2D_SUCCESS : A2D_FAIL);
+}
+
+/******************************************************************************
+**
+** Function A2D_FindService
+**
+** Description This function is called by a client application to
+** perform service discovery and retrieve SRC or SNK SDP
+** record information from a server. Information is
+** returned for the first service record found on the
+** server that matches the service UUID. The callback
+** function will be executed when service discovery is
+** complete. There can only be one outstanding call to
+** A2D_FindService() at a time; the application must wait
+** for the callback before it makes another call to
+** the function.
+**
+** Input Parameters:
+** service_uuid: Indicates SRC or SNK.
+**
+** bd_addr: BD address of the peer device.
+**
+** p_db: Pointer to the information to initialize
+** the discovery database.
+**
+** p_cback: Pointer to the A2D_FindService()
+** callback function.
+**
+** Output Parameters:
+** None.
+**
+** Returns A2D_SUCCESS if function execution succeeded,
+** A2D_INVALID_PARAMS if bad parameters are given.
+** A2D_BUSY if discovery is already in progress.
+** A2D_FAIL if function execution failed.
+**
+******************************************************************************/
+tA2D_STATUS A2D_FindService(UINT16 service_uuid, BD_ADDR bd_addr,
+ tA2D_SDP_DB_PARAMS *p_db, tA2D_FIND_CBACK *p_cback)
+{
+ tSDP_UUID uuid_list;
+ BOOLEAN result = TRUE;
+ UINT16 a2d_attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2D_NUM_ATTR, if changed */
+ ATTR_ID_BT_PROFILE_DESC_LIST,
+ ATTR_ID_SUPPORTED_FEATURES,
+ ATTR_ID_SERVICE_NAME,
+ ATTR_ID_PROTOCOL_DESC_LIST,
+ ATTR_ID_PROVIDER_NAME};
+
+ A2D_TRACE_API1("A2D_FindService uuid: %x", service_uuid);
+ if( (service_uuid != UUID_SERVCLASS_AUDIO_SOURCE && service_uuid != UUID_SERVCLASS_AUDIO_SINK) ||
+ p_db == NULL || p_db->p_db == NULL || p_cback == NULL)
+ return A2D_INVALID_PARAMS;
+
+ if( a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SOURCE ||
+ a2d_cb.find.service_uuid == UUID_SERVCLASS_AUDIO_SINK)
+ return A2D_BUSY;
+
+ /* set up discovery database */
+ uuid_list.len = LEN_UUID_16;
+ uuid_list.uu.uuid16 = service_uuid;
+
+ if(p_db->p_attrs == NULL || p_db->num_attr == 0)
+ {
+ p_db->p_attrs = a2d_attr_list;
+ p_db->num_attr = A2D_NUM_ATTR;
+ }
+
+ result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list, p_db->num_attr,
+ p_db->p_attrs);
+
+ if (result == TRUE)
+ {
+ /* store service_uuid and discovery db pointer */
+ a2d_cb.find.p_db = p_db->p_db;
+ a2d_cb.find.service_uuid = service_uuid;
+ a2d_cb.find.p_cback = p_cback;
+
+ /* perform service search */
+ result = SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, a2d_sdp_cback);
+ if(FALSE == result)
+ {
+ a2d_cb.find.service_uuid = 0;
+ }
+ }
+
+ return (result ? A2D_SUCCESS : A2D_FAIL);
+}
+
+/******************************************************************************
+**
+** Function A2D_SetTraceLevel
+**
+** Description Sets the trace level for A2D. If 0xff is passed, the
+** current trace level is returned.
+**
+** Input Parameters:
+** new_level: The level to set the A2D tracing to:
+** 0xff-returns the current setting.
+** 0-turns off tracing.
+** >= 1-Errors.
+** >= 2-Warnings.
+** >= 3-APIs.
+** >= 4-Events.
+** >= 5-Debug.
+**
+** Returns The new trace level or current trace level if
+** the input parameter is 0xff.
+**
+******************************************************************************/
+UINT8 A2D_SetTraceLevel (UINT8 new_level)
+{
+ if (new_level != 0xFF)
+ a2d_cb.trace_level = new_level;
+
+ return (a2d_cb.trace_level);
+}
+
+/******************************************************************************
+** Function A2D_BitsSet
+**
+** Description Check the given num for the number of bits set
+** Returns A2D_SET_ONE_BIT, if one and only one bit is set
+** A2D_SET_ZERO_BIT, if all bits clear
+** A2D_SET_MULTL_BIT, if multiple bits are set
+******************************************************************************/
+UINT8 A2D_BitsSet(UINT8 num)
+{
+ UINT8 count;
+ BOOLEAN res;
+ if(num == 0)
+ res = A2D_SET_ZERO_BIT;
+ else
+ {
+ count = (num & (num - 1));
+ res = ((count==0)?A2D_SET_ONE_BIT:A2D_SET_MULTL_BIT);
+ }
+ return res;
+}
+
+/*******************************************************************************
+**
+** Function A2D_Init
+**
+** Description This function is called to initialize the control block
+** for this layer. It must be called before accessing any
+** other API functions for this layer. It is typically called
+** once during the start up of the stack.
+**
+** Returns void
+**
+*******************************************************************************/
+void A2D_Init(void)
+{
+ memset(&a2d_cb, 0, sizeof(tA2D_CB));
+
+ a2d_cb.avdt_sdp_ver = AVDT_VERSION;
+
+#if defined(A2D_INITIAL_TRACE_LEVEL)
+ a2d_cb.trace_level = A2D_INITIAL_TRACE_LEVEL;
+#else
+ a2d_cb.trace_level = BT_TRACE_LEVEL_NONE;
+#endif
+}
+
diff --git a/stack/a2dp/a2d_int.h b/stack/a2dp/a2d_int.h
new file mode 100644
index 0000000..07cf34c
--- /dev/null
+++ b/stack/a2dp/a2d_int.h
@@ -0,0 +1,70 @@
+/*****************************************************************************
+**
+** Name: a2d_int.h
+**
+** Description:A2DP internal header file
+**
+** Copyright (c) 2002-2009, Broadcom Corp., All Rights Reserved.
+** WIDCOMM Bluetooth Core. Proprietary and confidential.
+**
+*****************************************************************************/
+#ifndef A2D_INT_H
+#define A2D_INT_H
+
+#include "a2d_api.h"
+
+/*****************************************************************************
+** Constants
+*****************************************************************************/
+#define A2D_VERSION 0x0102
+
+/* Number of attributes in A2D SDP record. */
+#define A2D_NUM_ATTR 6
+
+/* Number of protocol elements in protocol element list. */
+#define A2D_NUM_PROTO_ELEMS 2
+
+/*****************************************************************************
+** Type definitions
+*****************************************************************************/
+
+/* Control block used by A2D_FindService(). */
+typedef struct
+{
+ tA2D_FIND_CBACK *p_cback; /* pointer to application callback */
+ tSDP_DISCOVERY_DB *p_db; /* pointer to discovery database */
+ UINT16 service_uuid; /* service UUID of search */
+} tA2D_FIND_CB;
+
+typedef struct
+{
+ tA2D_FIND_CB find; /* find service control block */
+ UINT8 trace_level;
+ BOOLEAN use_desc;
+ UINT16 avdt_sdp_ver; /* AVDTP version */
+} tA2D_CB;
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/******************************************************************************
+** Main Control Block
+*******************************************************************************/
+#if A2D_DYNAMIC_MEMORY == FALSE
+A2D_API extern tA2D_CB a2d_cb;
+#else
+A2D_API extern tA2D_CB *a2d_cb_ptr;
+#define a2d_cb (*a2d_cb_ptr)
+#endif
+
+/* Used only for conformance testing */
+A2D_API extern void a2d_set_avdt_sdp_ver (UINT16 avdt_sdp_ver);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* A2D_INT_H */
diff --git a/stack/a2dp/a2d_m12.c b/stack/a2dp/a2d_m12.c
new file mode 100644
index 0000000..3c9fb98
--- /dev/null
+++ b/stack/a2dp/a2d_m12.c
@@ -0,0 +1,141 @@
+/*****************************************************************************
+**
+** Name: a2d_m12.c
+**
+** Description:utility functions to help build and parse MPEG-1, 2 Audio
+** Codec Information Element and Media Payload.
+** Copyright (c) 2002-2004, WIDCOMM Inc., All Rights Reserved.
+** WIDCOMM Bluetooth Core. Proprietary and confidential.
+**
+*****************************************************************************/
+#include "bt_target.h"
+
+#if (A2D_M12_INCLUDED == TRUE)
+#include "a2d_api.h"
+#include "a2d_int.h"
+#include "a2d_m12.h"
+
+
+/******************************************************************************
+**
+** Function A2D_BldM12Info
+**
+** Description This function is called by an application to build
+** the MPEG-1, 2 Audio Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** media_type: Indicates Audio, or Multimedia.
+**
+** p_ie: The MPEG-1, 2 Audio Codec Information Element information.
+**
+** Output Parameters:
+** p_result: the resulting codec info byte sequence.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_BldM12Info(UINT8 media_type, tA2D_M12_CIE *p_ie, UINT8 *p_result)
+{
+ tA2D_STATUS status;
+ UINT8 *p_vbr;
+
+ if( p_ie == NULL || p_result == NULL ||
+ (p_ie->layer & ~A2D_M12_IE_LAYER_MSK) ||
+ (p_ie->ch_mode & ~A2D_M12_IE_CH_MD_MSK) ||
+ (p_ie->samp_freq & ~A2D_M12_IE_SAMP_FREQ_MSK) ||
+ (p_ie->bitrate & ~A2D_M12_IE_BITRATE_MSK))
+ {
+ /* if any unused bit is set */
+ status = A2D_INVALID_PARAMS;
+ }
+ else
+ {
+ status = A2D_SUCCESS;
+ *p_result++ = A2D_M12_INFO_LEN;
+ *p_result++ = media_type;
+ *p_result++ = A2D_MEDIA_CT_M12;
+
+ /* Media Codec Specific Information Element */
+ *p_result = p_ie->layer | p_ie->ch_mode;
+ if(p_ie->crc)
+ *p_result |= A2D_M12_IE_CRC_MSK;
+ p_result++;
+
+ *p_result = p_ie->samp_freq;
+ if(p_ie->mpf)
+ *p_result |= A2D_M12_IE_MPF_MSK;
+ p_result++;
+
+ p_vbr = p_result;
+ UINT16_TO_BE_STREAM(p_result, p_ie->bitrate);
+ if(p_ie->vbr)
+ *p_vbr |= A2D_M12_IE_VBR_MSK;
+ }
+ return status;
+}
+
+/******************************************************************************
+**
+** Function A2D_ParsM12Info
+**
+** Description This function is called by an application to parse
+** the MPEG-1, 2 Audio Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** Input Parameters:
+** p_info: the byte sequence to parse.
+**
+** for_caps: TRUE, if the byte sequence is for get capabilities response.
+**
+** Output Parameters:
+** p_ie: The MPEG-1, 2 Audio Codec Information Element information.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_ParsM12Info(tA2D_M12_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
+{
+ tA2D_STATUS status;
+ UINT8 losc;
+ UINT8 mt;
+
+ if( p_ie == NULL || p_info == NULL)
+ status = A2D_INVALID_PARAMS;
+ else
+ {
+ losc = *p_info++;
+ mt = *p_info++;
+ /* If the function is called for the wrong Media Type or Media Codec Type */
+ if(losc != A2D_M12_INFO_LEN || *p_info != A2D_MEDIA_CT_M12)
+ status = A2D_WRONG_CODEC;
+ else
+ {
+ p_info++;
+ p_ie->layer = *p_info & A2D_M12_IE_LAYER_MSK;
+ p_ie->crc = (*p_info & A2D_M12_IE_CRC_MSK) >> 4;
+ p_ie->ch_mode = *p_info & A2D_M12_IE_CH_MD_MSK;
+ p_info++;
+ p_ie->mpf = (*p_info & A2D_M12_IE_MPF_MSK) >> 6;
+ p_ie->samp_freq = *p_info & A2D_M12_IE_SAMP_FREQ_MSK;
+ p_info++;
+ p_ie->vbr = (*p_info & A2D_M12_IE_VBR_MSK) >> 7;
+ BE_STREAM_TO_UINT16(p_ie->bitrate, p_info);
+ p_ie->bitrate &= A2D_M12_IE_BITRATE_MSK;
+
+ status = A2D_SUCCESS;
+ if(for_caps == FALSE)
+ {
+ if(A2D_BitsSet(p_ie->layer) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_LAYER;
+ if(A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_CH_MODE;
+ if(A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_SAMP_FREQ;
+ if((A2D_BitsSet((UINT8)(p_ie->bitrate&0xFF)) +
+ A2D_BitsSet((UINT8)((p_ie->bitrate&0xFF00)>>8)))!= A2D_SET_ONE_BIT)
+ status = A2D_BAD_BIT_RATE;
+ }
+ }
+ }
+ return status;
+}
+
+#endif /* A2D_M12_INCLUDED == TRUE */
diff --git a/stack/a2dp/a2d_m24.c b/stack/a2dp/a2d_m24.c
new file mode 100644
index 0000000..d8df766
--- /dev/null
+++ b/stack/a2dp/a2d_m24.c
@@ -0,0 +1,149 @@
+/*****************************************************************************
+**
+** Name: a2d_m24.c
+**
+** Description:utility functions to help build and parse MPEG-2, 4 AAC
+** Codec Information Element and Media Payload.
+** Copyright (c) 2002-2004, WIDCOMM Inc., All Rights Reserved.
+** WIDCOMM Bluetooth Core. Proprietary and confidential.
+**
+*****************************************************************************/
+
+#include "bt_target.h"
+
+#if (A2D_M24_INCLUDED == TRUE)
+#include "a2d_api.h"
+#include "a2d_int.h"
+#include "a2d_m24.h"
+
+
+/******************************************************************************
+**
+** Function A2D_BldM24Info
+**
+** Description This function is called by an application to build
+** the MPEG-2, 4 AAC Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** Input Parameters:
+** media_type: Indicates Audio, or Multimedia.
+**
+** p_ie: MPEG-2, 4 AAC Codec Information Element information.
+**
+** Output Parameters:
+** p_result: the resulting codec info byte sequence.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_BldM24Info(UINT8 media_type, tA2D_M24_CIE *p_ie, UINT8 *p_result)
+{
+ tA2D_STATUS status;
+ UINT16 bitrate45;
+
+ if( p_ie == NULL || p_result == NULL ||
+ (p_ie->obj_type & ~A2D_M24_IE_OBJ_MSK) ||
+ (p_ie->samp_freq & ~A2D_M24_IE_SAMP_FREQ_MSK) ||
+ (p_ie->chnl & ~A2D_M24_IE_CHNL_MSK) ||
+ (p_ie->bitrate & ~A2D_M24_IE_BITRATE_MSK))
+ {
+ /* if any unused bit is set */
+ status = A2D_INVALID_PARAMS;
+ }
+ else
+ {
+ status = A2D_SUCCESS;
+ *p_result++ = A2D_M24_INFO_LEN;
+ *p_result++ = media_type;
+ *p_result++ = A2D_MEDIA_CT_M24;
+
+ /* Media Codec Specific Information Element */
+ *p_result++ = p_ie->obj_type;
+
+ UINT16_TO_BE_STREAM(p_result, p_ie->samp_freq);
+ p_result--;
+ *p_result++ |= p_ie->chnl;
+
+ *p_result = (p_ie->bitrate & A2D_M24_IE_BITRATE3_MSK) >> 16;
+ if(p_ie->vbr)
+ *p_result |= A2D_M24_IE_VBR_MSK;
+ p_result++;
+ bitrate45 = (UINT16)(p_ie->bitrate & A2D_M24_IE_BITRATE45_MSK);
+ UINT16_TO_BE_STREAM(p_result, bitrate45);
+ }
+ return status;
+}
+
+/******************************************************************************
+**
+** Function A2D_ParsM24Info
+**
+** Description This function is called by an application to parse
+** the MPEG-2, 4 AAC Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** Input Parameters:
+** p_info: the byte sequence to parse.
+**
+** for_caps: TRUE, if the byte sequence is for get capabilities response.
+**
+** Output Parameters:
+** p_ie: MPEG-2, 4 AAC Codec Information Element information.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_ParsM24Info(tA2D_M24_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
+{
+ tA2D_STATUS status;
+ UINT8 vbr;
+ UINT16 u16;
+ UINT8 losc;
+ UINT8 mt;
+
+ if( p_ie == NULL || p_info == NULL)
+ status = A2D_INVALID_PARAMS;
+ else
+ {
+ losc = *p_info++;
+ mt = *p_info++;
+ /* If the function is called for the wrong Media Type or Media Codec Type */
+ if(losc != A2D_M24_INFO_LEN || *p_info != A2D_MEDIA_CT_M24)
+ status = A2D_WRONG_CODEC;
+ else
+ {
+ p_info++;
+ p_ie->obj_type = *p_info++;
+ BE_STREAM_TO_UINT16(p_ie->samp_freq, p_info);
+ p_ie->chnl = p_ie->samp_freq & A2D_M24_IE_CHNL_MSK;
+ p_ie->samp_freq &= A2D_M24_IE_SAMP_FREQ_MSK;
+ vbr = *p_info++;
+ BE_STREAM_TO_UINT16(u16, p_info); /* u16 contains the octect4, 5 of bterate */
+
+ p_ie->vbr = (vbr & A2D_M24_IE_VBR_MSK) >> 7;
+ vbr &= ~A2D_M24_IE_VBR_MSK; /* vbr has become the octect3 of bitrate */
+ p_ie->bitrate = vbr;
+ p_ie->bitrate <<= 16;
+ p_ie->bitrate += u16;
+
+ status = A2D_SUCCESS;
+ if(for_caps == FALSE)
+ {
+ if(A2D_BitsSet(p_ie->obj_type) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_OBJ_TYPE;
+
+ if((A2D_BitsSet((UINT8)(p_ie->samp_freq&0xFF)) +
+ A2D_BitsSet((UINT8)((p_ie->samp_freq&0xFF00)>>8)))!= A2D_SET_ONE_BIT)
+ status = A2D_BAD_SAMP_FREQ;
+
+ if(A2D_BitsSet(p_ie->chnl) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_CHANNEL;
+
+ /* BitRate must be greater than zero when specifying AAC configuration */
+ if (p_ie->bitrate == 0)
+ status = A2D_BAD_BIT_RATE;
+ }
+ }
+ }
+ return status;
+}
+#endif /* (A2D_M24_INCLUDED == TRUE) */
+
diff --git a/stack/a2dp/a2d_sbc.c b/stack/a2dp/a2d_sbc.c
new file mode 100644
index 0000000..cec335a
--- /dev/null
+++ b/stack/a2dp/a2d_sbc.c
@@ -0,0 +1,386 @@
+/*****************************************************************************
+**
+** Name: a2d_sbc.c
+**
+** Description:utility functions to help build and parse SBC
+** Codec Information Element and Media Payload.
+** Copyright (c) 2002-2004, WIDCOMM Inc., All Rights Reserved.
+** WIDCOMM Bluetooth Core. Proprietary and confidential.
+**
+*****************************************************************************/
+#include "bt_target.h"
+
+#if (A2D_SBC_INCLUDED == TRUE)
+#include <string.h>
+#include "a2d_api.h"
+#include "a2d_int.h"
+#include "a2d_sbc.h"
+
+/*************************************************************************************************
+ * SBC descramble code
+ * Purpose: to tie the SBC code with BTE/mobile stack code,
+ * especially for the case when the SBC is ported into a third-party Multimedia chip
+ *
+ * Algorithm:
+ * init process: all counters reset to 0,
+ * calculate base_index: (6 + s16NumOfChannels*s16NumOfSubBands/2)
+ * scramble side: the init process happens every time SBC_Encoder_Init() is called.
+ * descramble side: it would be nice to know if he "init" process has happened.
+ * alter the SBC SYNC word 0x9C (1001 1100) to 0x8C (1000 1100).
+ *
+ * scramble process:
+ * The CRC byte:
+ * Every SBC frame has a frame header.
+ * The 1st byte is the sync word and the following 2 bytes are about the stream format.
+ * They are supposed to be "constant" within a "song"
+ * The 4th byte is the CRC byte. The CRC byte is bound to be random.
+ * Derive 2 items from the CRC byte; one is the "use" bit, the other is the "index".
+ *
+ * SBC keeps 2 sets of "use" & "index"; derived the current and the previous frame.
+ *
+ * The "use" bit is any bit in SBC_PRTC_USE_MASK is set.
+ * If set, SBC uses the "index" from the current frame.
+ * If not set, SBC uses the "index" from the previous frame or 0.
+ *
+ * index = (CRC & 0x3) + ((CRC & 0x30) >> 2) // 8 is the max index
+ *
+ * if(index > 0)
+ * {
+ * p = &u8frame[base_index];
+ * if((index&1)&&(u16PacketLength > (base_index+index*2)))
+ * {
+ * // odd index: swap 2 bytes
+ * tmp = p[index];
+ * p[index] = p[index*2];
+ * p[index*2] = tmp;
+ * }
+ * else
+ * {
+ * // even index: shift by 3
+ * tmp = (p[index] >> 3) + (p[index] << 5);
+ * p[index] = tmp;
+ * }
+ * }
+ * //else index is 0. The frame stays unaltered
+ *
+ */
+#define A2D_SBC_SYNC_WORD 0x9C
+#define A2D_SBC_CRC_IDX 3
+#define A2D_SBC_USE_MASK 0x64
+#define A2D_SBC_SYNC_MASK 0x10
+#define A2D_SBC_CIDX 0
+#define A2D_SBC_LIDX 1
+#define A2D_SBC_CH_M_BITS 0xC /* channel mode bits: 0: mono; 1 ch */
+#define A2D_SBC_SUBBAND_BIT 0x1 /* num of subband bit: 0:4; 1: 8 */
+
+#define A2D_SBC_GET_IDX(sc) (((sc) & 0x3) + (((sc) & 0x30) >> 2))
+
+typedef struct
+{
+ UINT8 use;
+ UINT8 idx;
+} tA2D_SBC_FR_CB;
+
+typedef struct
+{
+ tA2D_SBC_FR_CB fr[2];
+ UINT8 index;
+ UINT8 base;
+} tA2D_SBC_DS_CB;
+
+static tA2D_SBC_DS_CB a2d_sbc_ds_cb;
+/*int a2d_count = 0;*/
+/******************************************************************************
+**
+** Function A2D_SbcChkFrInit
+**
+** Description check if need to init the descramble control block.
+**
+** Returns nothing.
+******************************************************************************/
+void A2D_SbcChkFrInit(UINT8 *p_pkt)
+{
+ UINT8 fmt;
+ UINT8 num_chnl = 1;
+ UINT8 num_subband = 4;
+
+ if((p_pkt[0] & A2D_SBC_SYNC_MASK) == 0)
+ {
+ a2d_cb.use_desc = TRUE;
+ fmt = p_pkt[1];
+ p_pkt[0] |= A2D_SBC_SYNC_MASK;
+ memset(&a2d_sbc_ds_cb, 0, sizeof(tA2D_SBC_DS_CB));
+ if(fmt & A2D_SBC_CH_M_BITS)
+ num_chnl = 2;
+ if(fmt & A2D_SBC_SUBBAND_BIT)
+ num_subband = 8;
+ a2d_sbc_ds_cb.base = 6 + num_chnl*num_subband/2;
+ /*printf("base: %d\n", a2d_sbc_ds_cb.base);
+ a2d_count = 0;*/
+ }
+}
+
+/******************************************************************************
+**
+** Function A2D_SbcDescramble
+**
+** Description descramble the packet.
+**
+** Returns nothing.
+******************************************************************************/
+void A2D_SbcDescramble(UINT8 *p_pkt, UINT16 len)
+{
+ tA2D_SBC_FR_CB *p_cur, *p_last;
+ UINT32 idx, tmp, tmp2;
+
+ if(a2d_cb.use_desc)
+ {
+ /* c2l */
+ p_last = &a2d_sbc_ds_cb.fr[A2D_SBC_LIDX];
+ p_cur = &a2d_sbc_ds_cb.fr[A2D_SBC_CIDX];
+ p_last->idx = p_cur->idx;
+ p_last->use = p_cur->use;
+ /* getc */
+ p_cur->use = p_pkt[A2D_SBC_CRC_IDX] & A2D_SBC_USE_MASK;
+ p_cur->idx = A2D_SBC_GET_IDX(p_pkt[A2D_SBC_CRC_IDX]);
+ a2d_sbc_ds_cb.index = (p_cur->use)?A2D_SBC_CIDX:A2D_SBC_LIDX;
+ /*
+ printf("%05d: ar[%02d]: x%02x, msk: x%02x, use: %s, idx: %02d, ",
+ a2d_count++,
+ A2D_SBC_CRC_IDX, p_pkt[A2D_SBC_CRC_IDX], A2D_SBC_USE_MASK,
+ (p_cur->use)?"cur":"lst", p_cur->idx);
+ */
+ /* descramble */
+ idx = a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx;
+ if(idx > 0)
+ {
+ p_pkt = &p_pkt[a2d_sbc_ds_cb.base];
+ if((idx&1) && (len > (a2d_sbc_ds_cb.base+(idx<<1))))
+ {
+ tmp2 = (idx<<1);
+ tmp = p_pkt[idx];
+ p_pkt[idx] = p_pkt[tmp2];
+ p_pkt[tmp2] = tmp;
+ /*
+ printf("tmp2: %02d, len: %d, idx: %d\n",
+ tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx);
+ */
+ }
+ else
+ {
+ tmp2 = p_pkt[idx];
+ tmp = (tmp2>>3)+(tmp2<<5);
+ p_pkt[idx] = (UINT8)tmp;
+ /*
+ printf("tmp: x%02x, len: %d, idx: %d(cmp:%d)\n",
+ (UINT8)tmp2, len, a2d_sbc_ds_cb.fr[a2d_sbc_ds_cb.index].idx,
+ (a2d_sbc_ds_cb.base+(idx<<1)));
+ */
+ }
+ }
+ /*
+ else
+ {
+ printf("!!!!\n");
+ }
+ */
+ }
+}
+
+/******************************************************************************
+**
+** Function A2D_BldSbcInfo
+**
+** Description This function is called by an application to build
+** the SBC Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** Input Parameters:
+** media_type: Indicates Audio, or Multimedia.
+**
+** p_ie: The SBC Codec Information Element information.
+**
+** Output Parameters:
+** p_result: the resulting codec info byte sequence.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_BldSbcInfo(UINT8 media_type, tA2D_SBC_CIE *p_ie, UINT8 *p_result)
+{
+ tA2D_STATUS status;
+
+ if( p_ie == NULL || p_result == NULL ||
+ (p_ie->samp_freq & ~A2D_SBC_IE_SAMP_FREQ_MSK) ||
+ (p_ie->ch_mode & ~A2D_SBC_IE_CH_MD_MSK) ||
+ (p_ie->block_len & ~A2D_SBC_IE_BLOCKS_MSK) ||
+ (p_ie->num_subbands & ~A2D_SBC_IE_SUBBAND_MSK) ||
+ (p_ie->alloc_mthd & ~A2D_SBC_IE_ALLOC_MD_MSK) ||
+ (p_ie->max_bitpool < p_ie->min_bitpool) ||
+ (p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
+ (p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL) ||
+ (p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL) ||
+ (p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL) )
+ {
+ /* if any unused bit is set */
+ status = A2D_INVALID_PARAMS;
+ }
+ else
+ {
+ status = A2D_SUCCESS;
+ *p_result++ = A2D_SBC_INFO_LEN;
+ *p_result++ = media_type;
+ *p_result++ = A2D_MEDIA_CT_SBC;
+
+ /* Media Codec Specific Information Element */
+ *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
+
+ *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_mthd;
+
+ *p_result++ = p_ie->min_bitpool;
+ *p_result = p_ie->max_bitpool;
+ }
+ return status;
+}
+
+/******************************************************************************
+**
+** Function A2D_ParsSbcInfo
+**
+** Description This function is called by an application to parse
+** the SBC Media Codec Capabilities byte sequence
+** beginning from the LOSC octet.
+** Input Parameters:
+** p_info: the byte sequence to parse.
+**
+** for_caps: TRUE, if the byte sequence is for get capabilities response.
+**
+** Output Parameters:
+** p_ie: The SBC Codec Information Element information.
+**
+** Returns A2D_SUCCESS if function execution succeeded.
+** Error status code, otherwise.
+******************************************************************************/
+tA2D_STATUS A2D_ParsSbcInfo(tA2D_SBC_CIE *p_ie, UINT8 *p_info, BOOLEAN for_caps)
+{
+ tA2D_STATUS status;
+ UINT8 losc;
+ UINT8 mt;
+
+ if( p_ie == NULL || p_info == NULL)
+ status = A2D_INVALID_PARAMS;
+ else
+ {
+ losc = *p_info++;
+ mt = *p_info++;
+ /* If the function is called for the wrong Media Type or Media Codec Type */
+ if(losc != A2D_SBC_INFO_LEN || *p_info != A2D_MEDIA_CT_SBC)
+ status = A2D_WRONG_CODEC;
+ else
+ {
+ p_info++;
+ p_ie->samp_freq = *p_info & A2D_SBC_IE_SAMP_FREQ_MSK;
+ p_ie->ch_mode = *p_info & A2D_SBC_IE_CH_MD_MSK;
+ p_info++;
+ p_ie->block_len = *p_info & A2D_SBC_IE_BLOCKS_MSK;
+ p_ie->num_subbands = *p_info & A2D_SBC_IE_SUBBAND_MSK;
+ p_ie->alloc_mthd = *p_info & A2D_SBC_IE_ALLOC_MD_MSK;
+ p_info++;
+ p_ie->min_bitpool = *p_info++;
+ p_ie->max_bitpool = *p_info;
+ status = A2D_SUCCESS;
+ if(p_ie->min_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->min_bitpool > A2D_SBC_IE_MAX_BITPOOL )
+ status = A2D_BAD_MIN_BITPOOL;
+
+ if(p_ie->max_bitpool < A2D_SBC_IE_MIN_BITPOOL || p_ie->max_bitpool > A2D_SBC_IE_MAX_BITPOOL ||
+ p_ie->max_bitpool < p_ie->min_bitpool)
+ status = A2D_BAD_MAX_BITPOOL;
+
+ if(for_caps == FALSE)
+ {
+ if(A2D_BitsSet(p_ie->samp_freq) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_SAMP_FREQ;
+ if(A2D_BitsSet(p_ie->ch_mode) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_CH_MODE;
+ if(A2D_BitsSet(p_ie->block_len) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_BLOCK_LEN;
+ if(A2D_BitsSet(p_ie->num_subbands) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_SUBBANDS;
+ if(A2D_BitsSet(p_ie->alloc_mthd) != A2D_SET_ONE_BIT)
+ status = A2D_BAD_ALLOC_MTHD;
+ }
+ }
+ }
+ return status;
+}
+
+/******************************************************************************
+**
+** Function A2D_BldSbcMplHdr
+**
+** Description This function is called by an application to parse
+** the SBC Media Payload header.
+** Input Parameters:
+** frag: 1, if fragmented. 0, otherwise.
+**
+** start: 1, if the starting packet of a fragmented frame.
+**
+** last: 1, if the last packet of a fragmented frame.
+**
+** num: If frag is 1, this is the number of remaining fragments
+** (including this fragment) of this frame.
+** If frag is 0, this is the number of frames in this packet.
+**
+** Output Parameters:
+** p_dst: the resulting media payload header byte sequence.
+**
+** Returns void.
+******************************************************************************/
+void A2D_BldSbcMplHdr(UINT8 *p_dst, BOOLEAN frag, BOOLEAN start, BOOLEAN last, UINT8 num)
+{
+ if(p_dst)
+ {
+ *p_dst = 0;
+ if(frag)
+ *p_dst |= A2D_SBC_HDR_F_MSK;
+ if(start)
+ *p_dst |= A2D_SBC_HDR_S_MSK;
+ if(last)
+ *p_dst |= A2D_SBC_HDR_L_MSK;
+ *p_dst |= (A2D_SBC_HDR_NUM_MSK & num);
+ }
+}
+
+/******************************************************************************
+**
+** Function A2D_ParsSbcMplHdr
+**
+** Description This function is called by an application to parse
+** the SBC Media Payload header.
+** Input Parameters:
+** p_src: the byte sequence to parse..
+**
+** Output Parameters:
+** frag: 1, if fragmented. 0, otherwise.
+**
+** start: 1, if the starting packet of a fragmented frame.
+**
+** last: 1, if the last packet of a fragmented frame.
+**
+** num: If frag is 1, this is the number of remaining fragments
+** (including this fragment) of this frame.
+** If frag is 0, this is the number of frames in this packet.
+**
+** Returns void.
+******************************************************************************/
+void A2D_ParsSbcMplHdr(UINT8 *p_src, BOOLEAN *p_frag, BOOLEAN *p_start, BOOLEAN *p_last, UINT8 *p_num)
+{
+ if(p_src && p_frag && p_start && p_last && p_num)
+ {
+ *p_frag = (*p_src & A2D_SBC_HDR_F_MSK) ? TRUE: FALSE;
+ *p_start= (*p_src & A2D_SBC_HDR_S_MSK) ? TRUE: FALSE;
+ *p_last = (*p_src & A2D_SBC_HDR_L_MSK) ? TRUE: FALSE;
+ *p_num = (*p_src & A2D_SBC_HDR_NUM_MSK);
+ }
+}
+
+#endif /* A2D_SBC_INCLUDED == TRUE */