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