Home | History | Annotate | Download | only in avrc
      1 /******************************************************************************
      2  *
      3  *  Copyright 2003-2016 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  *  AVRCP SDP related functions
     22  *
     23  ******************************************************************************/
     24 #include <string.h>
     25 
     26 #include "avrc_api.h"
     27 #include "avrc_int.h"
     28 #include "bt_common.h"
     29 
     30 using bluetooth::Uuid;
     31 
     32 /*****************************************************************************
     33  *  Global data
     34  ****************************************************************************/
     35 tAVRC_CB avrc_cb;
     36 static uint16_t a2dp_attr_list_sdp[] = {
     37     ATTR_ID_SERVICE_CLASS_ID_LIST, /* update A2DP_NUM_ATTR, if changed */
     38     ATTR_ID_BT_PROFILE_DESC_LIST,  ATTR_ID_SUPPORTED_FEATURES,
     39     ATTR_ID_SERVICE_NAME,          ATTR_ID_PROTOCOL_DESC_LIST,
     40     ATTR_ID_PROVIDER_NAME};
     41 
     42 /******************************************************************************
     43  *
     44  * Function         avrc_sdp_cback
     45  *
     46  * Description      This is the SDP callback function used by A2DP_FindService.
     47  *                  This function will be executed by SDP when the service
     48  *                  search is completed.  If the search is successful, it
     49  *                  finds the first record in the database that matches the
     50  *                  UUID of the search.  Then retrieves various parameters
     51  *                  from the record.  When it is finished it calls the
     52  *                  application callback function.
     53  *
     54  * Returns          Nothing.
     55  *
     56  *****************************************************************************/
     57 static void avrc_sdp_cback(uint16_t status) {
     58   AVRC_TRACE_API("%s status: %d", __func__, status);
     59 
     60   /* reset service_uuid, so can start another find service */
     61   avrc_cb.service_uuid = 0;
     62 
     63   /* return info from sdp record in app callback function */
     64   avrc_cb.find_cback.Run(status);
     65 
     66   return;
     67 }
     68 
     69 /******************************************************************************
     70  *
     71  * Function         AVRC_FindService
     72  *
     73  * Description      This function is called by the application to perform
     74  *                  service discovery and retrieve AVRCP SDP record information
     75  *                  from a peer device.  Information is returned for the first
     76  *                  service record found on the server that matches the service
     77  *                  UUID. The callback function will be executed when service
     78  *                  discovery is complete.  There can only be one outstanding
     79  *                  call to AVRC_FindService() at a time; the application must
     80  *                  wait for the callback before it makes another call to the
     81  *                  function.  The application is responsible for allocating
     82  *                  memory for the discovery database.  It is recommended that
     83  *                  the size of the discovery database be at least 300 bytes.
     84  *                  The application can deallocate the memory after the
     85  *                  callback function has executed.
     86  *
     87  *                  Input Parameters:
     88  *                      service_uuid: Indicates
     89  *                                       TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
     90  *                                     r CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
     91  *
     92  *                      bd_addr:  BD address of the peer device.
     93  *
     94  *                      p_db:  SDP discovery database parameters.
     95  *
     96  *                      p_cback:  Pointer to the callback function.
     97  *
     98  *                  Output Parameters:
     99  *                      None.
    100  *
    101  * Returns          AVRC_SUCCESS if successful.
    102  *                  AVRC_BAD_PARAMS if discovery database parameters are
    103  *                  invalid.
    104  *                  AVRC_NO_RESOURCES if there are not enough resources to
    105  *                                    perform the service search.
    106  *
    107  *****************************************************************************/
    108 uint16_t AVRC_FindService(uint16_t service_uuid, const RawAddress& bd_addr,
    109                           tAVRC_SDP_DB_PARAMS* p_db,
    110                           const tAVRC_FIND_CBACK& find_cback) {
    111   bool result = true;
    112 
    113   AVRC_TRACE_API("%s uuid: %x", __func__, service_uuid);
    114   if ((service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
    115        service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL) ||
    116       p_db == NULL || p_db->p_db == NULL || find_cback.is_null())
    117     return AVRC_BAD_PARAM;
    118 
    119   /* check if it is busy */
    120   if (avrc_cb.service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET ||
    121       avrc_cb.service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL)
    122     return AVRC_NO_RESOURCES;
    123 
    124 
    125   if (p_db->p_attrs == NULL || p_db->num_attr == 0) {
    126     p_db->p_attrs = a2dp_attr_list_sdp;
    127     p_db->num_attr = AVRC_NUM_ATTR;
    128   }
    129 
    130   Uuid uuid_list = Uuid::From16Bit(service_uuid);
    131   result = SDP_InitDiscoveryDb(p_db->p_db, p_db->db_len, 1, &uuid_list,
    132                                p_db->num_attr, p_db->p_attrs);
    133 
    134   if (result) {
    135     /* store service_uuid and discovery db pointer */
    136     avrc_cb.p_db = p_db->p_db;
    137     avrc_cb.service_uuid = service_uuid;
    138     avrc_cb.find_cback = find_cback;
    139 
    140     /* perform service search */
    141     result =
    142         SDP_ServiceSearchAttributeRequest(bd_addr, p_db->p_db, avrc_sdp_cback);
    143   }
    144 
    145   return (result ? AVRC_SUCCESS : AVRC_FAIL);
    146 }
    147 
    148 /******************************************************************************
    149  *
    150  * Function         AVRC_AddRecord
    151  *
    152  * Description      This function is called to build an AVRCP SDP record.
    153  *                  Prior to calling this function the application must
    154  *                  call SDP_CreateRecord() to create an SDP record.
    155  *
    156  *                  Input Parameters:
    157  *                      service_uuid:  Indicates
    158  *                                        TG(UUID_SERVCLASS_AV_REM_CTRL_TARGET)
    159  *                                     or CT(UUID_SERVCLASS_AV_REMOTE_CONTROL)
    160  *
    161  *                      p_service_name:  Pointer to a null-terminated character
    162  *                      string containing the service name.
    163  *                      If service name is not used set this to NULL.
    164  *
    165  *                      p_provider_name:  Pointer to a null-terminated character
    166  *                      string containing the provider name.
    167  *                      If provider name is not used set this to NULL.
    168  *
    169  *                      categories:  Supported categories.
    170  *
    171  *                      sdp_handle:  SDP handle returned by SDP_CreateRecord().
    172  *
    173  *                      browse_supported:  browse support info.
    174  *
    175  *                      profile_version:  profile version of avrcp record.
    176  *
    177  *                  Output Parameters:
    178  *                      None.
    179  *
    180  * Returns          AVRC_SUCCESS if successful.
    181  *                  AVRC_NO_RESOURCES if not enough resources to build the SDP
    182  *                                    record.
    183  *
    184  *****************************************************************************/
    185 uint16_t AVRC_AddRecord(uint16_t service_uuid, const char* p_service_name,
    186                         const char* p_provider_name, uint16_t categories,
    187                         uint32_t sdp_handle, bool browse_supported,
    188                         uint16_t profile_version) {
    189   uint16_t browse_list[1];
    190   bool result = true;
    191   uint8_t temp[8];
    192   uint8_t* p;
    193   uint16_t count = 1;
    194   uint8_t index = 0;
    195   uint16_t class_list[2];
    196 
    197   AVRC_TRACE_API("%s uuid: %x", __func__, service_uuid);
    198 
    199   if (service_uuid != UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
    200       service_uuid != UUID_SERVCLASS_AV_REMOTE_CONTROL)
    201     return AVRC_BAD_PARAM;
    202 
    203   /* add service class id list */
    204   class_list[0] = service_uuid;
    205   if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) &&
    206       (profile_version > AVRC_REV_1_3)) {
    207     class_list[1] = UUID_SERVCLASS_AV_REM_CTRL_CONTROL;
    208     count = 2;
    209   }
    210   result &= SDP_AddServiceClassIdList(sdp_handle, count, class_list);
    211 
    212   uint16_t protocol_reported_version;
    213   /* AVRCP versions 1.3 to 1.5 report (version - 1) in the protocol
    214      descriptor list. Oh, and 1.6 and 1.6.1 report version 1.4.
    215      /because-we-smart */
    216   if (profile_version < AVRC_REV_1_6) {
    217     protocol_reported_version = profile_version - 1;
    218   } else {
    219     protocol_reported_version = AVCT_REV_1_4;
    220   }
    221 
    222   /* add protocol descriptor list */
    223   tSDP_PROTOCOL_ELEM avrc_proto_desc_list[AVRC_NUM_PROTO_ELEMS];
    224   avrc_proto_desc_list[0].num_params = 1;
    225   avrc_proto_desc_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
    226   avrc_proto_desc_list[0].params[0] = AVCT_PSM;
    227   avrc_proto_desc_list[0].params[1] = 0;
    228   for (index = 1; index < AVRC_NUM_PROTO_ELEMS; index++) {
    229     avrc_proto_desc_list[index].num_params = 1;
    230     avrc_proto_desc_list[index].protocol_uuid = UUID_PROTOCOL_AVCTP;
    231     avrc_proto_desc_list[index].params[0] = protocol_reported_version;
    232     avrc_proto_desc_list[index].params[1] = 0;
    233   }
    234   result &= SDP_AddProtocolList(sdp_handle, AVRC_NUM_PROTO_ELEMS,
    235                                 &avrc_proto_desc_list[0]);
    236 
    237   /* additional protocal descriptor, required only for version > 1.3 */
    238   if ((profile_version > AVRC_REV_1_3) && (browse_supported)) {
    239     tSDP_PROTO_LIST_ELEM avrc_add_proto_desc_list;
    240     avrc_add_proto_desc_list.num_elems = 2;
    241     avrc_add_proto_desc_list.list_elem[0].num_params = 1;
    242     avrc_add_proto_desc_list.list_elem[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
    243     avrc_add_proto_desc_list.list_elem[0].params[0] = AVCT_BR_PSM;
    244     avrc_add_proto_desc_list.list_elem[0].params[1] = 0;
    245     avrc_add_proto_desc_list.list_elem[1].num_params = 1;
    246     avrc_add_proto_desc_list.list_elem[1].protocol_uuid = UUID_PROTOCOL_AVCTP;
    247     avrc_add_proto_desc_list.list_elem[1].params[0] = protocol_reported_version;
    248     avrc_add_proto_desc_list.list_elem[1].params[1] = 0;
    249 
    250     result &=
    251         SDP_AddAdditionProtoLists(sdp_handle, 1, &avrc_add_proto_desc_list);
    252   }
    253   /* add profile descriptor list   */
    254   result &= SDP_AddProfileDescriptorList(
    255       sdp_handle, UUID_SERVCLASS_AV_REMOTE_CONTROL, profile_version);
    256 
    257   /* add supported categories */
    258   p = temp;
    259   UINT16_TO_BE_STREAM(p, categories);
    260   result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES,
    261                              UINT_DESC_TYPE, (uint32_t)2, (uint8_t*)temp);
    262 
    263   /* add provider name */
    264   if (p_provider_name != NULL) {
    265     result &= SDP_AddAttribute(
    266         sdp_handle, ATTR_ID_PROVIDER_NAME, TEXT_STR_DESC_TYPE,
    267         (uint32_t)(strlen(p_provider_name) + 1), (uint8_t*)p_provider_name);
    268   }
    269 
    270   /* add service name */
    271   if (p_service_name != NULL) {
    272     result &= SDP_AddAttribute(
    273         sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
    274         (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
    275   }
    276 
    277   /* add browse group list */
    278   browse_list[0] = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
    279   result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1,
    280                                 browse_list);
    281 
    282   return (result ? AVRC_SUCCESS : AVRC_FAIL);
    283 }
    284 
    285 /******************************************************************************
    286  *
    287  * Function         AVRC_SetTraceLevel
    288  *
    289  * Description      Sets the trace level for AVRC. If 0xff is passed, the
    290  *                  current trace level is returned.
    291  *
    292  *                  Input Parameters:
    293  *                      new_level:  The level to set the AVRC tracing to:
    294  *                      0xff-returns the current setting.
    295  *                      0-turns off tracing.
    296  *                      >= 1-Errors.
    297  *                      >= 2-Warnings.
    298  *                      >= 3-APIs.
    299  *                      >= 4-Events.
    300  *                      >= 5-Debug.
    301  *
    302  * Returns          The new trace level or current trace level if
    303  *                  the input parameter is 0xff.
    304  *
    305  *****************************************************************************/
    306 uint8_t AVRC_SetTraceLevel(uint8_t new_level) {
    307   if (new_level != 0xFF) avrc_cb.trace_level = new_level;
    308 
    309   return (avrc_cb.trace_level);
    310 }
    311 
    312 /*******************************************************************************
    313  *
    314  * Function         AVRC_Init
    315  *
    316  * Description      This function is called at stack startup to allocate the
    317  *                  control block (if using dynamic memory), and initializes the
    318  *                  control block and tracing level.
    319  *
    320  * Returns          void
    321  *
    322  ******************************************************************************/
    323 void AVRC_Init(void) {
    324   memset(&avrc_cb, 0, sizeof(tAVRC_CB));
    325 
    326 #if defined(AVRC_INITIAL_TRACE_LEVEL)
    327   avrc_cb.trace_level = AVRC_INITIAL_TRACE_LEVEL;
    328 #else
    329   avrc_cb.trace_level = BT_TRACE_LEVEL_NONE;
    330 #endif
    331 }
    332