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