OSDN Git Service

9c87211278edb3291321b606c6ca0afe237669b8
[android-x86/system-bt.git] / bta / ag / bta_ag_sdp.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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 file contains the audio gateway functions performing SDP
22  *  operations.
23  *
24  ******************************************************************************/
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include "bta_api.h"
31 #include "bta_sys.h"
32 #include "bta_ag_api.h"
33 #include "bta_ag_int.h"
34 #include "sdp_api.h"
35 #include "btm_api.h"
36 #include "bt_common.h"
37 #include "utl.h"
38 #include "bt_utils.h"
39
40 /* Number of protocol elements in protocol element list. */
41 #define BTA_AG_NUM_PROTO_ELEMS      2
42
43 /* Number of elements in service class id list. */
44 #define BTA_AG_NUM_SVC_ELEMS        2
45
46 /* size of database for service discovery */
47 #ifndef BTA_AG_DISC_BUF_SIZE
48 #define BTA_AG_DISC_BUF_SIZE        BT_DEFAULT_BUFFER_SIZE
49 #endif
50
51 /* declare sdp callback functions */
52 void bta_ag_sdp_cback_1(UINT16 status);
53 void bta_ag_sdp_cback_2(UINT16 status);
54 void bta_ag_sdp_cback_3(UINT16 status);
55
56 /* SDP callback function table */
57 typedef tSDP_DISC_CMPL_CB *tBTA_AG_SDP_CBACK;
58 const tBTA_AG_SDP_CBACK bta_ag_sdp_cback_tbl[] =
59 {
60     bta_ag_sdp_cback_1,
61     bta_ag_sdp_cback_2,
62     bta_ag_sdp_cback_3
63 };
64
65 /*******************************************************************************
66 **
67 ** Function         bta_ag_sdp_cback
68 **
69 ** Description      SDP callback function.
70 **
71 **
72 ** Returns          void
73 **
74 *******************************************************************************/
75 static void bta_ag_sdp_cback(UINT16 status, UINT8 idx)
76 {
77     UINT16              event;
78     tBTA_AG_SCB         *p_scb;
79
80     APPL_TRACE_DEBUG("bta_ag_sdp_cback status:0x%x", status);
81
82     if ((p_scb = bta_ag_scb_by_idx(idx)) != NULL)
83     {
84         /* set event according to int/acp */
85         if (p_scb->role == BTA_AG_ACP)
86         {
87             event = BTA_AG_DISC_ACP_RES_EVT;
88         }
89         else
90         {
91             event = BTA_AG_DISC_INT_RES_EVT;
92         }
93
94         tBTA_AG_DISC_RESULT *p_buf =
95             (tBTA_AG_DISC_RESULT *)osi_malloc(sizeof(tBTA_AG_DISC_RESULT));
96         p_buf->hdr.event = event;
97         p_buf->hdr.layer_specific = idx;
98         p_buf->status = status;
99         bta_sys_sendmsg(p_buf);
100     }
101 }
102
103 /*******************************************************************************
104 **
105 ** Function         bta_ag_sdp_cback_1 to 3
106 **
107 ** Description      SDP callback functions.  Since there is no way to
108 **                  distinguish scb from the callback we need separate
109 **                  callbacks for each scb.
110 **
111 **
112 ** Returns          void
113 **
114 *******************************************************************************/
115 void bta_ag_sdp_cback_1(UINT16 status) {bta_ag_sdp_cback(status, 1);}
116 void bta_ag_sdp_cback_2(UINT16 status) {bta_ag_sdp_cback(status, 2);}
117 void bta_ag_sdp_cback_3(UINT16 status) {bta_ag_sdp_cback(status, 3);}
118
119 /******************************************************************************
120 **
121 ** Function         bta_ag_add_record
122 **
123 ** Description      This function is called by a server application to add
124 **                  HSP or HFP information to an SDP record.  Prior to
125 **                  calling this function the application must call
126 **                  SDP_CreateRecord() to create an SDP record.
127 **
128 ** Returns          TRUE if function execution succeeded,
129 **                  FALSE if function execution failed.
130 **
131 ******************************************************************************/
132 BOOLEAN bta_ag_add_record(UINT16 service_uuid, char *p_service_name, UINT8 scn,
133                           tBTA_AG_FEAT features, UINT32 sdp_handle)
134 {
135     tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_AG_NUM_PROTO_ELEMS];
136     UINT16              svc_class_id_list[BTA_AG_NUM_SVC_ELEMS];
137     UINT16              browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
138     UINT16              version;
139     UINT16              profile_uuid;
140     UINT8               network;
141     BOOLEAN             result = TRUE;
142     BOOLEAN             codec_supported = FALSE;
143     UINT8               buf[2];
144
145     APPL_TRACE_DEBUG("bta_ag_add_record uuid: %x", service_uuid);
146
147     memset( proto_elem_list, 0 , BTA_AG_NUM_PROTO_ELEMS*sizeof(tSDP_PROTOCOL_ELEM));
148
149     /* add the protocol element sequence */
150     proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
151     proto_elem_list[0].num_params = 0;
152     proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
153     proto_elem_list[1].num_params = 1;
154     proto_elem_list[1].params[0] = scn;
155     result &= SDP_AddProtocolList(sdp_handle, BTA_AG_NUM_PROTO_ELEMS, proto_elem_list);
156
157     /* add service class id list */
158     svc_class_id_list[0] = service_uuid;
159     svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
160     result &= SDP_AddServiceClassIdList(sdp_handle, BTA_AG_NUM_SVC_ELEMS, svc_class_id_list);
161
162     /* add profile descriptor list */
163     if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE)
164     {
165         profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
166         version = HFP_VERSION_1_6;
167     }
168     else
169     {
170         profile_uuid = UUID_SERVCLASS_HEADSET;
171         version = HSP_VERSION_1_2;
172     }
173     result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
174
175     /* add service name */
176     if (p_service_name != NULL && p_service_name[0] != 0)
177     {
178         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
179                     (UINT32)(strlen(p_service_name)+1), (UINT8 *) p_service_name);
180     }
181
182     /* add features and network */
183     if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE)
184     {
185         network = (features & BTA_AG_FEAT_REJECT) ? 1 : 0;
186         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_DATA_STORES_OR_NETWORK,
187                     UINT_DESC_TYPE, 1, &network);
188
189         if (features & BTA_AG_FEAT_CODEC)
190             codec_supported = TRUE;
191
192         features &= BTA_AG_SDP_FEAT_SPEC;
193
194         /* Codec bit position is different in SDP and in BRSF */
195         if (codec_supported)
196             features |= 0x0020;
197
198         UINT16_TO_BE_FIELD(buf, features);
199         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, 2, buf);
200     }
201
202     /* add browse group list */
203     result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
204
205     return result;
206 }
207
208 /*******************************************************************************
209 **
210 ** Function         bta_ag_create_records
211 **
212 ** Description      Create SDP records for registered services.
213 **
214 **
215 ** Returns          void
216 **
217 *******************************************************************************/
218 void bta_ag_create_records(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
219 {
220     int                 i;
221     tBTA_SERVICE_MASK   services;
222
223     services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
224     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1)
225     {
226         /* if service is set in mask */
227         if (services & 1)
228         {
229             /* add sdp record if not already registered */
230             if (bta_ag_cb.profile[i].sdp_handle == 0)
231             {
232                 bta_ag_cb.profile[i].sdp_handle = SDP_CreateRecord();
233                 bta_ag_cb.profile[i].scn = BTM_AllocateSCN();
234                 bta_ag_add_record(bta_ag_uuid[i], p_data->api_register.p_name[i],
235                     bta_ag_cb.profile[i].scn, p_data->api_register.features,
236                     bta_ag_cb.profile[i].sdp_handle);
237                 bta_sys_add_uuid(bta_ag_uuid[i]);
238             }
239         }
240     }
241
242     p_scb->hsp_version = HSP_VERSION_1_2;
243
244 }
245
246 /*******************************************************************************
247 **
248 ** Function         bta_ag_del_records
249 **
250 ** Description      Delete SDP records for any registered services.
251 **
252 **
253 ** Returns          void
254 **
255 *******************************************************************************/
256 void bta_ag_del_records(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
257 {
258     tBTA_AG_SCB         *p = &bta_ag_cb.scb[0];
259     tBTA_SERVICE_MASK   services;
260     tBTA_SERVICE_MASK   others = 0;
261     int                 i;
262     UNUSED(p_data);
263
264     /* get services of all other registered servers */
265     for (i = 0; i < BTA_AG_NUM_IDX; i++, p++)
266     {
267         if (p_scb == p)
268         {
269             continue;
270         }
271
272         if (p->in_use && p->dealloc == FALSE)
273         {
274             others |= p->reg_services;
275         }
276     }
277
278     others >>= BTA_HSP_SERVICE_ID;
279     services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
280     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1, others >>= 1)
281     {
282         /* if service registered for this scb and not registered for any other scb */
283         if (((services & 1) == 1) && ((others & 1) == 0))
284         {
285             APPL_TRACE_DEBUG("bta_ag_del_records %d", i);
286             if (bta_ag_cb.profile[i].sdp_handle != 0)
287             {
288                 SDP_DeleteRecord(bta_ag_cb.profile[i].sdp_handle);
289                 bta_ag_cb.profile[i].sdp_handle = 0;
290             }
291             BTM_FreeSCN(bta_ag_cb.profile[i].scn);
292             BTM_SecClrService(bta_ag_sec_id[i]);
293             bta_sys_remove_uuid(bta_ag_uuid[i]);
294         }
295     }
296 }
297
298 /*******************************************************************************
299 **
300 ** Function         bta_ag_sdp_find_attr
301 **
302 ** Description      Process SDP discovery results to find requested attributes
303 **                  for requested service.
304 **
305 **
306 ** Returns          TRUE if results found, FALSE otherwise.
307 **
308 *******************************************************************************/
309 BOOLEAN bta_ag_sdp_find_attr(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
310 {
311     tSDP_DISC_REC       *p_rec = NULL;
312     tSDP_DISC_ATTR      *p_attr;
313     tSDP_PROTOCOL_ELEM  pe;
314     UINT16              uuid;
315     BOOLEAN             result = FALSE;
316
317     if (service & BTA_HFP_SERVICE_MASK)
318     {
319         uuid = UUID_SERVCLASS_HF_HANDSFREE;
320         p_scb->peer_version = HFP_VERSION_1_1;   /* Default version */
321     }
322     else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
323     {
324         uuid = UUID_SERVCLASS_HEADSET_HS;
325         p_scb->peer_version = 0x0100;   /* Default version */
326     }
327     else
328     {
329         return result;
330     }
331
332     /* loop through all records we found */
333     while (TRUE)
334     {
335         /* get next record; if none found, we're done */
336         if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL)
337         {
338             if (uuid == UUID_SERVCLASS_HEADSET_HS)
339             {
340                 /* Search again in case the peer device is HSP v1.0 */
341                 uuid = UUID_SERVCLASS_HEADSET;
342                 if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL)
343                 {
344                     break;
345                 }
346             }
347             else
348                 break;
349         }
350
351         /* get scn from proto desc list if initiator */
352         if (p_scb->role == BTA_AG_INT)
353         {
354             if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
355             {
356                 p_scb->peer_scn = (UINT8) pe.params[0];
357             }
358             else
359             {
360                 continue;
361             }
362         }
363
364         /* get profile version (if failure, version parameter is not updated) */
365         SDP_FindProfileVersionInRec(p_rec, uuid, &p_scb->peer_version);
366
367         /* get features if HFP */
368         if (service & BTA_HFP_SERVICE_MASK)
369         {
370             if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL)
371             {
372                 /* Found attribute. Get value. */
373                 /* There might be race condition between SDP and BRSF.  */
374                 /* Do not update if we already received BRSF.           */
375                 if (p_scb->peer_features == 0)
376                     p_scb->peer_features = p_attr->attr_value.v.u16;
377             }
378
379             /* Remote supports 1.7, store it in the file */
380             if (p_scb->peer_version == HFP_VERSION_1_7)
381             {
382                 bool ret = FALSE;
383                 /* Check if the device is already part of the list, if not store it */
384                 ret = is_device_present(IOT_HFP_1_7_BLACKLIST, p_scb->peer_addr);
385
386                 if (ret == FALSE)
387                 {
388                    add_iot_device(IOT_DEV_CONF_FILE, IOT_HFP_1_7_BLACKLIST,
389                                   p_scb->peer_addr, METHOD_BD);
390                 }
391             }
392         }
393         else    /* HSP */
394         {
395             if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL)) != NULL)
396             {
397                 /* Remote volume control of HSP */
398                 if (p_attr->attr_value.v.u8)
399                     p_scb->peer_features |= BTA_AG_PEER_FEAT_VOL;
400                 else
401                     p_scb->peer_features &= ~BTA_AG_PEER_FEAT_VOL;
402             }
403
404         }
405
406         /* found what we needed */
407         result = TRUE;
408         break;
409     }
410     return result;
411 }
412
413 /*******************************************************************************
414 **
415 ** Function         bta_ag_do_disc
416 **
417 ** Description      Do service discovery.
418 **
419 **
420 ** Returns          void
421 **
422 *******************************************************************************/
423 void bta_ag_do_disc(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
424 {
425     tSDP_UUID       uuid_list[2];
426     UINT16          num_uuid = 1;
427     UINT16          attr_list[4];
428     UINT8           num_attr;
429     BOOLEAN         db_inited = FALSE;
430
431     /* HFP initiator; get proto list and features */
432     if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
433     {
434         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
435         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
436         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
437         attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
438         num_attr = 4;
439         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
440     }
441     /* HFP acceptor; get features */
442     else if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_ACP)
443     {
444         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
445         attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
446         attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
447         num_attr = 3;
448         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
449     }
450     /* HSP initiator; get proto list */
451     else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT)
452     {
453         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
454         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
455         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
456         attr_list[3] = ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL;
457         num_attr = 4;
458
459         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET;        /* Legacy from HSP v1.0 */
460         if (p_scb->hsp_version >= HSP_VERSION_1_2)
461         {
462             uuid_list[1].uu.uuid16 = UUID_SERVCLASS_HEADSET_HS;
463             num_uuid = 2;
464         }
465     }
466     /* HSP acceptor; no discovery */
467     else
468     {
469         return;
470     }
471
472     /* allocate buffer for sdp database */
473     p_scb->p_disc_db = (tSDP_DISCOVERY_DB *)osi_malloc(BTA_AG_DISC_BUF_SIZE);
474     /* set up service discovery database; attr happens to be attr_list len */
475     uuid_list[0].len = LEN_UUID_16;
476     uuid_list[1].len = LEN_UUID_16;
477     db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE,
478                                     num_uuid, uuid_list, num_attr, attr_list);
479
480     if(db_inited)
481     {
482         /*Service discovery not initiated */
483         /*Fix for below Klockwork issue
484          *Array 'bta_ag_sdp_cback_tbl' size is 3
485          *Possible attempt to access element -1,3..USHRT_MAX-1 of array 'bta_ag_sdp_cback_tbl' */
486         db_inited = SDP_ServiceSearchAttributeRequest(p_scb->peer_addr, p_scb->p_disc_db,
487                                       bta_ag_sdp_cback_tbl[(UINT16)(bta_ag_scb_to_idx(p_scb) - 1)]);
488     }
489
490     if(!db_inited)
491     {
492         /*free discover db */
493         bta_ag_free_db(p_scb, NULL);
494         /* sent failed event */
495         bta_ag_sm_execute(p_scb, BTA_AG_DISC_FAIL_EVT, NULL);
496     }
497
498 }
499
500 /*******************************************************************************
501 **
502 ** Function         bta_ag_free_db
503 **
504 ** Description      Free discovery database.
505 **
506 **
507 ** Returns          void
508 **
509 *******************************************************************************/
510 void bta_ag_free_db(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
511 {
512     UNUSED(p_data);
513     osi_free_and_reset((void **)&p_scb->p_disc_db);
514 }