1 /******************************************************************************
2  *
3  *  Copyright (C) 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 "common/bt_target.h"
26 #include <string.h>
27 #include "bta/bta_ar_api.h"
28 #include "bta_ar_int.h"
29 
30 #if BTA_AR_INCLUDED
31 
32 /* AV control block */
33 #if BTA_DYNAMIC_MEMORY == FALSE
34 tBTA_AR_CB  bta_ar_cb;
35 #else
36 tBTA_AR_CB  *bta_ar_cb_ptr;
37 #endif
38 
39 /*******************************************************************************
40 **
41 ** Function         bta_ar_id
42 **
43 ** Description      This function maps sys_id to ar id mask.
44 **
45 ** Returns          void
46 **
47 *******************************************************************************/
bta_ar_id(tBTA_SYS_ID sys_id)48 static UINT8 bta_ar_id(tBTA_SYS_ID sys_id)
49 {
50     UINT8   mask = 0;
51     if (sys_id == BTA_ID_AV) {
52         mask = BTA_AR_AV_MASK;
53     } else if (sys_id == BTA_ID_AVK) {
54         mask = BTA_AR_AVK_MASK;
55     }
56 
57     return mask;
58 }
59 
60 /*******************************************************************************
61 **
62 ** Function         bta_ar_init
63 **
64 ** Description      This function is called to register to AVDTP.
65 **
66 ** Returns          void
67 **
68 *******************************************************************************/
bta_ar_init(void)69 void bta_ar_init(void)
70 {
71     /* initialize control block */
72     memset(&bta_ar_cb, 0, sizeof(tBTA_AR_CB));
73 }
74 
75 /*******************************************************************************
76 **
77 ** Function         bta_ar_reg_avdt
78 **
79 ** Description      This function is called to register to AVDTP.
80 **
81 ** Returns          void
82 **
83 *******************************************************************************/
bta_ar_avdt_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)84 static void bta_ar_avdt_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
85 {
86     /* route the AVDT registration callback to av or avk */
87     if (bta_ar_cb.p_av_conn_cback) {
88         (*bta_ar_cb.p_av_conn_cback)(handle, bd_addr, event, p_data);
89     }
90     if (bta_ar_cb.p_avk_conn_cback) {
91         (*bta_ar_cb.p_avk_conn_cback)(handle, bd_addr, event, p_data);
92     }
93 }
94 
95 /*******************************************************************************
96 **
97 ** Function         bta_ar_reg_avdt
98 **
99 ** Description      AR module registration to AVDT.
100 **
101 ** Returns          void
102 **
103 *******************************************************************************/
bta_ar_reg_avdt(tAVDT_REG * p_reg,tAVDT_CTRL_CBACK * p_cback,tBTA_SYS_ID sys_id)104 void bta_ar_reg_avdt(tAVDT_REG *p_reg, tAVDT_CTRL_CBACK *p_cback, tBTA_SYS_ID sys_id)
105 {
106     UINT8   mask = 0;
107 
108     if (sys_id == BTA_ID_AV) {
109         bta_ar_cb.p_av_conn_cback = p_cback;
110         mask = BTA_AR_AV_MASK;
111     } else if (sys_id == BTA_ID_AVK) {
112         bta_ar_cb.p_avk_conn_cback = p_cback;
113         mask = BTA_AR_AVK_MASK;
114     }
115 #if (BTA_AR_DEBUG == TRUE)
116     else {
117         APPL_TRACE_ERROR("bta_ar_reg_avdt: the registration is from wrong sys_id:%d", sys_id);
118     }
119 #endif
120 
121     if (mask) {
122         if (bta_ar_cb.avdt_registered == 0) {
123             AVDT_Register(p_reg, bta_ar_avdt_cback);
124         }
125         bta_ar_cb.avdt_registered |= mask;
126     }
127 }
128 
129 /*******************************************************************************
130 **
131 ** Function         bta_ar_dereg_avdt
132 **
133 ** Description      This function is called to de-register from AVDTP.
134 **
135 ** Returns          void
136 **
137 *******************************************************************************/
bta_ar_dereg_avdt(tBTA_SYS_ID sys_id)138 void bta_ar_dereg_avdt(tBTA_SYS_ID sys_id)
139 {
140     UINT8   mask = 0;
141 
142     if (sys_id == BTA_ID_AV) {
143         bta_ar_cb.p_av_conn_cback = NULL;
144         mask = BTA_AR_AV_MASK;
145     } else if (sys_id == BTA_ID_AVK) {
146         bta_ar_cb.p_avk_conn_cback = NULL;
147         mask = BTA_AR_AVK_MASK;
148     }
149     bta_ar_cb.avdt_registered &= ~mask;
150 
151     if (bta_ar_cb.avdt_registered == 0) {
152         AVDT_Deregister();
153     }
154 }
155 
156 /*******************************************************************************
157 **
158 ** Function         bta_ar_avdt_conn
159 **
160 ** Description      This function is called to let ar know that some AVDTP profile
161 **                  is connected for this sys_id.
162 **                  If the other sys modules started a timer for PENDING_EVT,
163 **                  the timer can be stopped now.
164 **
165 ** Returns          void
166 **
167 *******************************************************************************/
bta_ar_avdt_conn(tBTA_SYS_ID sys_id,BD_ADDR bd_addr)168 void bta_ar_avdt_conn(tBTA_SYS_ID sys_id, BD_ADDR bd_addr)
169 {
170     UINT8       event = BTA_AR_AVDT_CONN_EVT;
171     tAVDT_CTRL  data;
172 
173     if (sys_id == BTA_ID_AV) {
174         if (bta_ar_cb.p_avk_conn_cback) {
175             (*bta_ar_cb.p_avk_conn_cback)(0, bd_addr, event, &data);
176         }
177     } else if (sys_id == BTA_ID_AVK) {
178         if (bta_ar_cb.p_av_conn_cback) {
179             (*bta_ar_cb.p_av_conn_cback)(0, bd_addr, event, &data);
180         }
181     }
182 }
183 
184 /*******************************************************************************
185 **
186 ** Function         bta_ar_reg_avct
187 **
188 ** Description      This function is called to register to AVCTP.
189 **
190 ** Returns          void
191 **
192 *******************************************************************************/
bta_ar_reg_avct(UINT16 mtu,UINT16 mtu_br,UINT8 sec_mask,tBTA_SYS_ID sys_id)193 void bta_ar_reg_avct(UINT16 mtu, UINT16 mtu_br, UINT8 sec_mask, tBTA_SYS_ID sys_id)
194 {
195     UINT8   mask = bta_ar_id (sys_id);
196 
197     if (mask) {
198         if (bta_ar_cb.avct_registered == 0) {
199             AVCT_Register(mtu, mtu_br, sec_mask);
200         }
201         bta_ar_cb.avct_registered |= mask;
202     }
203 }
204 
205 /*******************************************************************************
206 **
207 ** Function         bta_ar_dereg_avct
208 **
209 ** Description      This function is called to deregister from AVCTP.
210 **
211 ** Returns          void
212 **
213 *******************************************************************************/
bta_ar_dereg_avct(tBTA_SYS_ID sys_id)214 void bta_ar_dereg_avct(tBTA_SYS_ID sys_id)
215 {
216     UINT8   mask = bta_ar_id (sys_id);
217 
218     bta_ar_cb.avct_registered &= ~mask;
219 
220     if (bta_ar_cb.avct_registered == 0) {
221         AVCT_Deregister();
222     }
223 }
224 
225 /******************************************************************************
226 **
227 ** Function         bta_ar_reg_avrc
228 **
229 ** Description      This function is called to register an SDP record for AVRCP.
230 **
231 ** Returns          void
232 **
233 ******************************************************************************/
bta_ar_reg_avrc(UINT16 service_uuid,char * service_name,char * provider_name,UINT16 categories,tBTA_SYS_ID sys_id,BOOLEAN browsing_en)234 void bta_ar_reg_avrc(UINT16 service_uuid, char *service_name, char *provider_name,
235                      UINT16 categories, tBTA_SYS_ID sys_id, BOOLEAN browsing_en)
236 {
237     UINT8   mask = bta_ar_id (sys_id);
238     UINT8   temp[8], *p;
239 
240     if (!mask || !categories) {
241         return;
242     }
243 
244     if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
245         if (bta_ar_cb.sdp_tg_handle == 0) {
246             bta_ar_cb.tg_registered = mask;
247             bta_ar_cb.sdp_tg_handle = SDP_CreateRecord();
248             AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_tg_handle, browsing_en);
249             bta_sys_add_uuid(service_uuid);
250         }
251         /* only one TG is allowed (first-come, first-served).
252          * If sdp_tg_handle is non-0, ignore this request */
253     } else if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) || (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL)) {
254         bta_ar_cb.ct_categories [mask - 1] = categories;
255         categories = bta_ar_cb.ct_categories[0] | bta_ar_cb.ct_categories[1];
256         if (bta_ar_cb.sdp_ct_handle == 0) {
257             bta_ar_cb.sdp_ct_handle = SDP_CreateRecord();
258             AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_ct_handle, browsing_en);
259             bta_sys_add_uuid(service_uuid);
260         } else {
261             /* multiple CTs are allowed.
262              * Change supported categories on the second one */
263             p = temp;
264             UINT16_TO_BE_STREAM(p, categories);
265             SDP_AddAttribute(bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
266                              (UINT32)2, (UINT8 *)temp);
267         }
268     }
269 }
270 
271 /******************************************************************************
272 **
273 ** Function         bta_ar_dereg_avrc
274 **
275 ** Description      This function is called to de-register/delete an SDP record for AVRCP.
276 **
277 ** Returns          void
278 **
279 ******************************************************************************/
bta_ar_dereg_avrc(UINT16 service_uuid,tBTA_SYS_ID sys_id)280 void bta_ar_dereg_avrc(UINT16 service_uuid, tBTA_SYS_ID sys_id)
281 {
282     UINT8   mask = bta_ar_id (sys_id);
283     UINT16  categories = 0;
284     UINT8   temp[8], *p;
285 
286     if (!mask) {
287         return;
288     }
289 
290     if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET) {
291         if (bta_ar_cb.sdp_tg_handle && mask == bta_ar_cb.tg_registered) {
292             bta_ar_cb.tg_registered = 0;
293             SDP_DeleteRecord(bta_ar_cb.sdp_tg_handle);
294             bta_ar_cb.sdp_tg_handle = 0;
295             bta_sys_remove_uuid(service_uuid);
296         }
297     } else if (service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) {
298         if (bta_ar_cb.sdp_ct_handle) {
299             bta_ar_cb.ct_categories [mask - 1] = 0;
300             categories = bta_ar_cb.ct_categories[0] | bta_ar_cb.ct_categories[1];
301             if (!categories) {
302                 /* no CT is still registered - cleaup */
303                 SDP_DeleteRecord(bta_ar_cb.sdp_ct_handle);
304                 bta_ar_cb.sdp_ct_handle = 0;
305                 bta_sys_remove_uuid(service_uuid);
306             } else {
307                 /* change supported categories to the remaning one */
308                 p = temp;
309                 UINT16_TO_BE_STREAM(p, categories);
310                 SDP_AddAttribute(bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
311                                  (UINT32)2, (UINT8 *)temp);
312             }
313         }
314     }
315 
316 }
317 
318 #endif /* #if BTA_AR_INCLUDED */
319