1 /******************************************************************************
2  *
3  *  Copyright 2008-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  *  This is the implementation for the audio/video registration module.
22  *
23  ******************************************************************************/
24 
25 #include <bluetooth/log.h>
26 #include <com_android_bluetooth_flags.h>
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <cstring>
31 #include <memory>
32 
33 #include "avdt_api.h"
34 #include "avrc_defs.h"
35 #include "avrcp_sdp_records.h"
36 #include "bta/ar/bta_ar_int.h"
37 #include "bta/include/bta_ar_api.h"
38 #include "bta/sys/bta_sys.h"
39 #include "profile/avrcp/avrcp_sdp_service.h"
40 #include "sdpdefs.h"
41 #include "stack/include/avct_api.h"
42 #include "stack/include/avrc_api.h"
43 #include "stack/include/bt_types.h"
44 #include "stack/include/bt_uuid16.h"
45 #include "stack/include/sdp_api.h"
46 #include "types/raw_address.h"
47 
48 using namespace bluetooth::legacy::stack::sdp;
49 using namespace bluetooth::avrcp;
50 using namespace bluetooth;
51 
52 /* AV control block */
53 tBTA_AR_CB bta_ar_cb;
54 
55 /*******************************************************************************
56  *
57  * Function         bta_ar_id
58  *
59  * Description      This function maps sys_id to ar id mask.
60  *
61  * Returns          void
62  *
63  ******************************************************************************/
bta_ar_id(tBTA_SYS_ID sys_id)64 static uint8_t bta_ar_id(tBTA_SYS_ID sys_id) {
65   uint8_t mask = 0;
66   if (sys_id == BTA_ID_AV) {
67     mask = BTA_AR_AV_MASK;
68   } else if (sys_id == BTA_ID_AVK) {
69     mask = BTA_AR_AVK_MASK;
70   }
71   return mask;
72 }
bta_ar_avrc_add_cat(uint16_t categories)73 static void bta_ar_avrc_add_cat(uint16_t categories) {
74   uint8_t temp[sizeof(uint16_t)], *p;
75   /* Change supported categories on the second one */
76   if (bta_ar_cb.sdp_tg_handle != 0) {
77     p = temp;
78     UINT16_TO_BE_STREAM(p, categories);
79     if (!get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
80                 bta_ar_cb.sdp_tg_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, sizeof(temp),
81                 (uint8_t*)temp)) {
82       log::warn("Unable to add SDP attribute for supported categories handle:{}",
83                 bta_ar_cb.sdp_tg_handle);
84     }
85   }
86 }
87 
88 /*******************************************************************************
89  *
90  * Function         bta_ar_init
91  *
92  * Description      This function is called to register to AVDTP.
93  *
94  * Returns          void
95  *
96  ******************************************************************************/
bta_ar_init(void)97 void bta_ar_init(void) {
98   /* initialize control block */
99   memset(&bta_ar_cb, 0, sizeof(tBTA_AR_CB));
100 }
101 
102 /*******************************************************************************
103  *
104  * Function         bta_ar_reg_avdt
105  *
106  * Description      This function is called to register to AVDTP.
107  *
108  * Returns          void
109  *
110  ******************************************************************************/
bta_ar_avdt_cback(uint8_t handle,const RawAddress & bd_addr,uint8_t event,tAVDT_CTRL * p_data,uint8_t scb_index)111 static void bta_ar_avdt_cback(uint8_t handle, const RawAddress& bd_addr, uint8_t event,
112                               tAVDT_CTRL* p_data, uint8_t scb_index) {
113   /* route the AVDT registration callback to av or avk */
114   if (bta_ar_cb.p_av_conn_cback) {
115     (*bta_ar_cb.p_av_conn_cback)(handle, bd_addr, event, p_data, scb_index);
116   }
117 }
118 
119 /*******************************************************************************
120  *
121  * Function         bta_ar_reg_avdt
122  *
123  * Description      AR module registration to AVDT.
124  *
125  * Returns          void
126  *
127  ******************************************************************************/
bta_ar_reg_avdt(AvdtpRcb * p_reg,tAVDT_CTRL_CBACK * p_cback)128 void bta_ar_reg_avdt(AvdtpRcb* p_reg, tAVDT_CTRL_CBACK* p_cback) {
129   bta_ar_cb.p_av_conn_cback = p_cback;
130   if (bta_ar_cb.avdt_registered == 0) {
131     AVDT_Register(p_reg, bta_ar_avdt_cback);
132   } else {
133     log::warn("doesn't register again (registered:{})", bta_ar_cb.avdt_registered);
134   }
135   bta_ar_cb.avdt_registered |= BTA_AR_AV_MASK;
136 }
137 
138 /*******************************************************************************
139  *
140  * Function         bta_ar_dereg_avdt
141  *
142  * Description      This function is called to de-register from AVDTP.
143  *
144  * Returns          void
145  *
146  ******************************************************************************/
bta_ar_dereg_avdt()147 void bta_ar_dereg_avdt() {
148   bta_ar_cb.p_av_conn_cback = NULL;
149   bta_ar_cb.avdt_registered &= ~BTA_AR_AV_MASK;
150 
151   if (bta_ar_cb.avdt_registered == 0) {
152     AVDT_Deregister();
153   }
154 }
155 
156 /*******************************************************************************
157  *
158  * Function         bta_ar_reg_avct
159  *
160  * Description      This function is called to register to AVCTP.
161  *
162  * Returns          void
163  *
164  ******************************************************************************/
bta_ar_reg_avct()165 void bta_ar_reg_avct() {
166   if (bta_ar_cb.avct_registered == 0) {
167     AVCT_Register();
168   }
169   bta_ar_cb.avct_registered |= BTA_AR_AV_MASK;
170 }
171 
172 /*******************************************************************************
173  *
174  * Function         bta_ar_dereg_avct
175  *
176  * Description      This function is called to deregister from AVCTP.
177  *
178  * Returns          void
179  *
180  ******************************************************************************/
bta_ar_dereg_avct()181 void bta_ar_dereg_avct() {
182   bta_ar_cb.avct_registered &= ~BTA_AR_AV_MASK;
183 
184   if (bta_ar_cb.avct_registered == 0) {
185     AVCT_Deregister();
186   }
187 }
188 
189 /******************************************************************************
190  *
191  * Function         bta_ar_reg_avrc
192  *
193  * Description      This function is called to register an SDP record for AVRCP.
194  *
195  * Returns          void
196  *
197  *****************************************************************************/
bta_ar_reg_avrc(uint16_t service_uuid,const char * service_name,const char * provider_name,uint16_t categories,bool browse_supported,uint16_t profile_version)198 void bta_ar_reg_avrc(uint16_t service_uuid, const char* service_name, const char* provider_name,
199                      uint16_t categories, bool browse_supported, uint16_t profile_version) {
200   if (!categories) {
201     return;
202   }
203 
204   if (com::android::bluetooth::flags::avrcp_sdp_records()) {
205     const std::shared_ptr<AvrcpSdpService>& avrcp_sdp_service = AvrcpSdpService::Get();
206     AvrcpSdpRecord add_record_request = {service_uuid,
207                                          service_name,
208                                          provider_name,
209                                          categories,
210                                          browse_supported,
211                                          profile_version,
212                                          0};
213     if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
214       avrcp_sdp_service->AddRecord(add_record_request, bta_ar_cb.sdp_tg_request_id);
215       log::debug("Assigned target request id {}", bta_ar_cb.sdp_tg_request_id);
216     } else if (service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL ||
217                service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL) {
218       avrcp_sdp_service->AddRecord(add_record_request, bta_ar_cb.sdp_ct_request_id);
219       log::debug("Assigned control request id {}", bta_ar_cb.sdp_ct_request_id);
220     }
221     return;
222   }
223   uint8_t mask = BTA_AR_AV_MASK;
224   uint8_t temp[8], *p;
225 
226   if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
227     if (bta_ar_cb.sdp_tg_handle == 0) {
228       bta_ar_cb.tg_registered = mask;
229       bta_ar_cb.sdp_tg_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
230       AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_tg_handle,
231                      browse_supported, profile_version, 0);
232       bta_sys_add_uuid(service_uuid);
233     }
234     /* only one TG is allowed (first-come, first-served).
235      * If sdp_tg_handle is non-0, ignore this request */
236   } else if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) ||
237              (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL)) {
238     bta_ar_cb.ct_categories[mask - 1] = categories;
239     categories = bta_ar_cb.ct_categories[0] | bta_ar_cb.ct_categories[1];
240     if (bta_ar_cb.sdp_ct_handle == 0) {
241       bta_ar_cb.sdp_ct_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
242       AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_ct_handle,
243                      browse_supported, profile_version, 0);
244       bta_sys_add_uuid(service_uuid);
245     } else {
246       /* multiple CTs are allowed.
247        * Change supported categories on the second one */
248       p = temp;
249       UINT16_TO_BE_STREAM(p, categories);
250       if (!get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
251                   bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, (uint32_t)2,
252                   (uint8_t*)temp)) {
253         log::warn("Unable to add supported features handle:{}", bta_ar_cb.sdp_ct_handle);
254       }
255     }
256   }
257 }
258 
259 /******************************************************************************
260  *
261  * Function         bta_ar_dereg_avrc
262  *
263  * Description      This function is called to de-register/delete an SDP record
264  *                  for AVRCP.
265  *
266  * Returns          void
267  *
268  *****************************************************************************/
bta_ar_dereg_avrc(uint16_t service_uuid)269 void bta_ar_dereg_avrc(uint16_t service_uuid) {
270   log::verbose("Deregister AVRC 0x{:x}", service_uuid);
271   if (com::android::bluetooth::flags::avrcp_sdp_records()) {
272     const std::shared_ptr<AvrcpSdpService>& avrcp_sdp_service = AvrcpSdpService::Get();
273     if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
274         bta_ar_cb.sdp_tg_request_id != UNASSIGNED_REQUEST_ID) {
275       avrcp_sdp_service->RemoveRecord(UUID_SERVCLASS_AV_REM_CTRL_TARGET,
276                                       bta_ar_cb.sdp_tg_request_id);
277       bta_ar_cb.sdp_tg_request_id = UNASSIGNED_REQUEST_ID;
278     } else if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL ||
279                 service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL) &&
280                bta_ar_cb.sdp_ct_request_id != UNASSIGNED_REQUEST_ID) {
281       avrcp_sdp_service->RemoveRecord(UUID_SERVCLASS_AV_REMOTE_CONTROL,
282                                       bta_ar_cb.sdp_ct_request_id);
283       bta_ar_cb.sdp_ct_request_id = UNASSIGNED_REQUEST_ID;
284     }
285     return;
286   }
287   uint8_t mask = BTA_AR_AV_MASK;
288   uint16_t categories = 0;
289   uint8_t temp[8], *p;
290 
291   if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
292     if (bta_ar_cb.sdp_tg_handle && mask == bta_ar_cb.tg_registered) {
293       bta_ar_cb.tg_registered = 0;
294       if (!get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(bta_ar_cb.sdp_tg_handle)) {
295         log::warn("Unable to delete SDP record handle:{}", bta_ar_cb.sdp_tg_handle);
296       }
297       bta_ar_cb.sdp_tg_handle = 0;
298       bta_sys_remove_uuid(service_uuid);
299     }
300   } else if (service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) {
301     if (bta_ar_cb.sdp_ct_handle) {
302       bta_ar_cb.ct_categories[mask - 1] = 0;
303       categories = bta_ar_cb.ct_categories[0] | bta_ar_cb.ct_categories[1];
304       if (!categories) {
305         /* no CT is still registered - cleanup */
306         if (get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(bta_ar_cb.sdp_ct_handle)) {
307           log::warn("Unable to delete SDP record handle:{}", bta_ar_cb.sdp_ct_handle);
308         }
309         bta_ar_cb.sdp_ct_handle = 0;
310         bta_sys_remove_uuid(service_uuid);
311       } else {
312         /* change supported categories to the remaining one */
313         p = temp;
314         UINT16_TO_BE_STREAM(p, categories);
315         if (!get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
316                     bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
317                     (uint32_t)2, (uint8_t*)temp)) {
318           log::warn("Unable to add SDP supported features handle:{}", bta_ar_cb.sdp_ct_handle);
319         }
320       }
321     }
322   }
323 }
324 
325 /******************************************************************************
326  *
327  * Function         bta_ar_reg_avrc_for_src_sink_coexist
328  *
329  * Description      This function is called to register an SDP record for AVRCP.
330  *                  Add sys_id to distinguish src or sink role and add also save
331  *tg_categories
332  *
333  * Returns          void
334  *
335  *****************************************************************************/
336 // TODO: b/341353017 - Remove it as part of flag cleanup
bta_ar_reg_avrc_for_src_sink_coexist(uint16_t service_uuid,const char * service_name,const char * provider_name,uint16_t categories,tBTA_SYS_ID sys_id,bool browse_supported,uint16_t profile_version)337 void bta_ar_reg_avrc_for_src_sink_coexist(uint16_t service_uuid, const char* service_name,
338                                           const char* provider_name, uint16_t categories,
339                                           tBTA_SYS_ID sys_id, bool browse_supported,
340                                           uint16_t profile_version) {
341   uint8_t mask = bta_ar_id(sys_id);
342   uint8_t temp[8], *p;
343   uint16_t class_list[2];
344   uint16_t count = 1;
345   if (!mask || !categories) {
346     return;
347   }
348   if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
349     bta_ar_cb.tg_categories[mask - 1] = categories;
350     categories = bta_ar_cb.tg_categories[0] | bta_ar_cb.tg_categories[1];
351     if (bta_ar_cb.sdp_tg_handle == 0) {
352       bta_ar_cb.tg_registered = mask;
353       bta_ar_cb.sdp_tg_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
354       AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_tg_handle,
355                      browse_supported, profile_version, 0);
356       bta_sys_add_uuid(service_uuid);
357     }
358     /* Change supported categories on the second one */
359     bta_ar_avrc_add_cat(categories);
360     /* only one TG is allowed (first-come, first-served).
361      * If sdp_tg_handle is non-0, ignore this request */
362   } else if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) ||
363              (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL)) {
364     bta_ar_cb.ct_categories[mask - 1] = categories;
365     categories = bta_ar_cb.ct_categories[0] | bta_ar_cb.ct_categories[1];
366     if (bta_ar_cb.sdp_ct_handle == 0) {
367       bta_ar_cb.sdp_ct_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
368       AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_ct_handle,
369                      browse_supported, profile_version, 0);
370       bta_sys_add_uuid(service_uuid);
371       bta_ar_cb.ct_ver = categories;
372     } else {
373       /* If first reg 1,3 version, reg 1.6 must update class id */
374       if (bta_ar_cb.ct_ver < profile_version) {
375         log::verbose("ver=0x{:x}", profile_version);
376         if (bta_ar_cb.ct_ver <= AVRC_REV_1_3 && profile_version > AVRC_REV_1_3) {
377           bta_ar_cb.ct_ver = profile_version;
378           /* add service class id list */
379           class_list[0] = service_uuid;
380           if (service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) {
381             class_list[1] = UUID_SERVCLASS_AV_REM_CTRL_CONTROL;
382             count = 2;
383           }
384           if (!get_legacy_stack_sdp_api()->handle.SDP_AddServiceClassIdList(bta_ar_cb.sdp_ct_handle,
385                                                                             count, class_list)) {
386             log::warn("Unable to add SDP service class id list handle:{}", bta_ar_cb.sdp_ct_handle);
387           }
388         } else {
389           bta_ar_cb.ct_ver = profile_version;
390         }
391         if (!get_legacy_stack_sdp_api()->handle.SDP_AddProfileDescriptorList(
392                     bta_ar_cb.sdp_ct_handle, service_uuid, profile_version)) {
393           log::warn("Unable to add SDP profile descriptor version handle:{}",
394                     bta_ar_cb.sdp_ct_handle);
395         }
396       }
397       /* multiple CT are allowed.
398        * Change supported categories on the second one */
399       p = temp;
400       UINT16_TO_BE_STREAM(p, categories);
401       if (!get_legacy_stack_sdp_api()->handle.SDP_AddAttribute(
402                   bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, (uint32_t)2,
403                   (uint8_t*)temp)) {
404         log::warn("Unable to add SDP attribute supported features handle:{}",
405                   bta_ar_cb.sdp_ct_handle);
406       }
407     }
408   }
409 }
410