summaryrefslogtreecommitdiffstats
path: root/stack/sdp/sdp_api.c
blob: 4899aa88b7892de2f2d72d977908e201d0fe282b (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
/******************************************************************************
 *
 *  Copyright (C) 1999-2012 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.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  this file contains SDP interface functions
 *
 ******************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "bt_target.h"
#include "gki.h"
#include "l2cdefs.h"
#include "hcidefs.h"
#include "hcimsgs.h"

#include "sdp_api.h"
#include "sdpint.h"
#include "btu.h"

#include <cutils/log.h>
#define info(fmt, ...)  LOGI ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
#define debug(fmt, ...) LOGD ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
#define error(fmt, ...) LOGE ("## ERROR : %s: " fmt "##",__FUNCTION__,  ## __VA_ARGS__)
#define asrt(s) if(!(s)) LOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)


/**********************************************************************
**   C L I E N T    F U N C T I O N    P R O T O T Y P E S            *
***********************************************************************/

/*******************************************************************************
**
** Function         SDP_InitDiscoveryDb
**
** Description      This function is called to initialize a discovery database.
**
** Parameters:      p_db        - (input) address of an area of memory where the
**                                        discovery database is managed.
**                  len         - (input) size (in bytes) of the memory
**                                  NOTE: This must be larger than sizeof(tSDP_DISCOVERY_DB)
**                  num_uuid    - (input) number of UUID filters applied
**                  p_uuid_list - (input) list of UUID filters
**                  num_attr    - (input) number of attribute filters applied
**                  p_attr_list - (input) list of attribute filters
**
**
** Returns          BOOLEAN
**                          TRUE if successful
**                          FALSE if one or more parameters are bad
**
*******************************************************************************/
BOOLEAN SDP_InitDiscoveryDb (tSDP_DISCOVERY_DB *p_db, UINT32 len, UINT16 num_uuid,
                             tSDP_UUID *p_uuid_list, UINT16 num_attr, UINT16 *p_attr_list)
{
#if SDP_CLIENT_ENABLED == TRUE
    UINT16  xx;

    /* verify the parameters */
    if (p_db == NULL || (sizeof (tSDP_DISCOVERY_DB) > len) ||
        num_attr > SDP_MAX_ATTR_FILTERS || num_uuid > SDP_MAX_UUID_FILTERS)
    {
        SDP_TRACE_ERROR4("SDP_InitDiscoveryDb Illegal param: p_db 0x%x, len %d, num_uuid %d, num_attr %d",
                         (UINT32)p_db, len, num_uuid, num_attr);

        return(FALSE);
    }

    memset (p_db, 0, (size_t)len);

    p_db->mem_size = len - sizeof (tSDP_DISCOVERY_DB);
    p_db->mem_free = p_db->mem_size;
    p_db->p_first_rec = NULL;
    p_db->p_free_mem = (UINT8 *)(p_db + 1);

    for (xx = 0; xx < num_uuid; xx++)
        p_db->uuid_filters[xx] = *p_uuid_list++;

    p_db->num_uuid_filters = num_uuid;

    for (xx = 0; xx < num_attr; xx++)
        p_db->attr_filters[xx] = *p_attr_list++;

    /* sort attributes */
    sdpu_sort_attr_list( num_attr, p_db );

    p_db->num_attr_filters = num_attr;
#endif
    return(TRUE);
}



/*******************************************************************************
**
** Function         SDP_CancelServiceSearch
**
** Description      This function cancels an active query to an SDP server.
**
** Returns          TRUE if discovery cancelled, FALSE if a matching activity is not found.
**
*******************************************************************************/
BOOLEAN SDP_CancelServiceSearch (tSDP_DISCOVERY_DB *p_db)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB     *p_ccb = sdpu_find_ccb_by_db (p_db);
    if (!p_ccb)
        return(FALSE);

    sdp_disconnect (p_ccb, SDP_CANCEL);
    p_ccb->disc_state = SDP_DISC_WAIT_CANCEL;
#endif
    return(TRUE);
}



/*******************************************************************************
**
** Function         SDP_ServiceSearchRequest
**
** Description      This function queries an SDP server for information.
**
** Returns          TRUE if discovery started, FALSE if failed.
**
*******************************************************************************/
BOOLEAN SDP_ServiceSearchRequest (UINT8 *p_bd_addr, tSDP_DISCOVERY_DB *p_db,
                                  tSDP_DISC_CMPL_CB *p_cb)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB     *p_ccb;

    /* Specific BD address */
    p_ccb = sdp_conn_originate (p_bd_addr);

    if (!p_ccb)
        return(FALSE);

    p_ccb->disc_state = SDP_DISC_WAIT_CONN;
    p_ccb->p_db       = p_db;
    p_ccb->p_cb       = p_cb;

    return(TRUE);
#else
    return(FALSE);
#endif
}


/*******************************************************************************
**
** Function         SDP_ServiceSearchAttributeRequest
**
** Description      This function queries an SDP server for information.
**
**                  The difference between this API function and the function
**                  SDP_ServiceSearchRequest is that this one does a
**                  combined ServiceSearchAttributeRequest SDP function.
**                  (This is for Unplug Testing)
**
** Returns          TRUE if discovery started, FALSE if failed.
**
*******************************************************************************/
BOOLEAN SDP_ServiceSearchAttributeRequest (UINT8 *p_bd_addr, tSDP_DISCOVERY_DB *p_db,
                                           tSDP_DISC_CMPL_CB *p_cb)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB     *p_ccb;

    /* Specific BD address */
    p_ccb = sdp_conn_originate (p_bd_addr);

    if (!p_ccb)
        return(FALSE);

    p_ccb->disc_state = SDP_DISC_WAIT_CONN;
    p_ccb->p_db       = p_db;
    p_ccb->p_cb       = p_cb;

    p_ccb->is_attr_search = TRUE;

    return(TRUE);
#else
    return(FALSE);
#endif
}
/*******************************************************************************
**
** Function         SDP_ServiceSearchAttributeRequest2
**
** Description      This function queries an SDP server for information.
**
**                  The difference between this API function and the function
**                  SDP_ServiceSearchRequest is that this one does a
**                  combined ServiceSearchAttributeRequest SDP function.
**                  (This is for Unplug Testing)
**
** Returns          TRUE if discovery started, FALSE if failed.
**
*******************************************************************************/
BOOLEAN SDP_ServiceSearchAttributeRequest2 (UINT8 *p_bd_addr, tSDP_DISCOVERY_DB *p_db,
                                            tSDP_DISC_CMPL_CB2 *p_cb2, void * user_data)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB     *p_ccb;

    /* Specific BD address */
    p_ccb = sdp_conn_originate (p_bd_addr);

    if (!p_ccb)
        return(FALSE);

    p_ccb->disc_state = SDP_DISC_WAIT_CONN;
    p_ccb->p_db       = p_db;
    p_ccb->p_cb2       = p_cb2;

    p_ccb->is_attr_search = TRUE;
    p_ccb->user_data = user_data;

    return(TRUE);
#else
    return(FALSE);
#endif
}

#if SDP_CLIENT_ENABLED == TRUE
void SDP_SetIdleTimeout (BD_ADDR addr, UINT16 timeout)
{
}
#endif

/*******************************************************************************
**
** Function         SDP_FindAttributeInDb
**
** Description      This function queries an SDP database for a specific attribute.
**                  If the p_start_rec pointer is NULL, it looks from the beginning
**                  of the database, else it continues from the next record after
**                  p_start_rec.
**
** Returns          Pointer to matching record, or NULL
**
*******************************************************************************/
tSDP_DISC_REC *SDP_FindAttributeInDb (tSDP_DISCOVERY_DB *p_db, UINT16 attr_id,
                                      tSDP_DISC_REC *p_start_rec)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_REC   *p_rec;
    tSDP_DISC_ATTR  *p_attr;

    /* Must have a valid database */
    if (p_db == NULL)
        return(NULL);

    if (!p_start_rec)
        p_rec = p_db->p_first_rec;
    else
        p_rec = p_start_rec->p_next_rec;

    while (p_rec)
    {
        p_attr = p_rec->p_first_attr;
        while (p_attr)
        {
            if (p_attr->attr_id == attr_id)
                return(p_rec);

            p_attr = p_attr->p_next_attr;
        }

        p_rec = p_rec->p_next_rec;
    }
#endif
    /* If here, no matching attribute found */
    return(NULL);
}


/*******************************************************************************
**
** Function         SDP_FindAttributeInRec
**
** Description      This function searches an SDP discovery record for a specific
**                  attribute.
**
** Returns          Pointer to matching attribute entry, or NULL
**
*******************************************************************************/
tSDP_DISC_ATTR *SDP_FindAttributeInRec (tSDP_DISC_REC *p_rec, UINT16 attr_id)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr;

    p_attr = p_rec->p_first_attr;
    while (p_attr)
    {
        if (p_attr->attr_id == attr_id)
            return(p_attr);

        p_attr = p_attr->p_next_attr;
    }
#endif
    /* If here, no matching attribute found */
    return(NULL);
}

/*******************************************************************************
**
** Function         SDP_FindServiceUUIDInRec
**
** Description      This function is called to read the service UUID within a record
**                  if there is any.
**
** Parameters:      p_rec      - pointer to a SDP record.
**                  p_uuid     - output parameter to save the UUID found.
**
** Returns          TRUE if found, otherwise FALSE.
**
*******************************************************************************/
BOOLEAN SDP_FindServiceUUIDInRec(tSDP_DISC_REC *p_rec, tBT_UUID * p_uuid)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr, *p_sattr, *p_extra_sattr;

    p_attr = p_rec->p_first_attr;

    while (p_attr)
    {
        if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST)
            && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
        {
            for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
            {
                if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                {
                    /* only support 16 bits UUID for now */
                    if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)
                    {
                        p_uuid->len = 2;
                        p_uuid->uu.uuid16 = p_sattr->attr_value.v.u16;
                    }
                    return(TRUE);
                }

                /* Checking for Toyota G Block Car Kit:
                **  This car kit puts an extra data element sequence
                **  where the UUID is suppose to be!!!
                */
                else
                {
                    if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)
                    {
                        /* Look through data element sequence until no more UUIDs */
                        for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr; p_extra_sattr; p_extra_sattr = p_extra_sattr->p_next_attr)
                        {
                            /* Increment past this to see if the next attribut is UUID */
                            if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) == UUID_DESC_TYPE)
                                /* only support 16 bits UUID for now */
                                && (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2))
                            {
                                p_uuid->len = 2;
                                p_uuid->uu.uuid16 = p_extra_sattr->attr_value.v.u16;
                                return(TRUE);
                            }
                        }
                    }
                }
            }
            break;
        }
        else if (p_attr->attr_id == ATTR_ID_SERVICE_ID)
        {
            if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
                /* only support 16 bits UUID for now */
                && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2))
            {
                p_uuid->len = 2;
                p_uuid->uu.uuid16 = p_attr->attr_value.v.u16;
                return(TRUE);
            }
        }
        p_attr = p_attr->p_next_attr;
    }
    return FALSE;
#endif
}

/*******************************************************************************
**
** Function         SDP_FindServiceUUIDInRec_128bit
**
** Description      This function is called to read the 128-bit service UUID within a record
**                  if there is any.
**
** Parameters:      p_rec      - pointer to a SDP record.
**                  p_uuid     - output parameter to save the UUID found.
**
** Returns          TRUE if found, otherwise FALSE.
**
*******************************************************************************/
BOOLEAN SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC *p_rec, tBT_UUID * p_uuid)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr, *p_sattr, *p_extra_sattr;

    p_attr = p_rec->p_first_attr;

    while (p_attr)
    {
        if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST)
            && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
        {
            for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
            {
                if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                {
                    /* only support 128 bits UUID for now */
                    if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16)
                    {
                        p_uuid->len = 16;
                        memcpy(p_uuid->uu.uuid128, p_sattr->attr_value.v.array, MAX_UUID_SIZE);
                    }
                    return(TRUE);
                }
            }
            break;
        }
        else if (p_attr->attr_id == ATTR_ID_SERVICE_ID)
        {
            if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
                /* only support 128 bits UUID for now */
                && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16))
            {
                p_uuid->len = 16;
                memcpy(p_uuid->uu.uuid128, p_sattr->attr_value.v.array, MAX_UUID_SIZE);
                return(TRUE);
            }
        }
        p_attr = p_attr->p_next_attr;
    }
    return FALSE;
#endif
}

/*******************************************************************************
**
** Function         SDP_FindServiceInDb
**
** Description      This function queries an SDP database for a specific service.
**                  If the p_start_rec pointer is NULL, it looks from the beginning
**                  of the database, else it continues from the next record after
**                  p_start_rec.
**
** Returns          Pointer to record containing service class, or NULL
**
*******************************************************************************/
tSDP_DISC_REC *SDP_FindServiceInDb (tSDP_DISCOVERY_DB *p_db, UINT16 service_uuid, tSDP_DISC_REC *p_start_rec)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_REC   *p_rec;
    tSDP_DISC_ATTR  *p_attr, *p_sattr, *p_extra_sattr;

    /* Must have a valid database */
    if (p_db == NULL)
        return(NULL);

    if (!p_start_rec)
        p_rec = p_db->p_first_rec;
    else
        p_rec = p_start_rec->p_next_rec;

    while (p_rec)
    {
        p_attr = p_rec->p_first_attr;
        while (p_attr)
        {
            if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST)
                && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
            {
                for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
                {

                    if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                     && (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2) ) {
                        printf("SDP_FindServiceInDb - p_sattr value = 0x%x serviceuuid = 0x%x\r\n", p_sattr->attr_value.v.u16, service_uuid);
                        if(service_uuid == UUID_SERVCLASS_HDP_PROFILE)
                        {
                            if( (p_sattr->attr_value.v.u16==UUID_SERVCLASS_HDP_SOURCE) || ( p_sattr->attr_value.v.u16==UUID_SERVCLASS_HDP_SOURCE))
                            {
                                printf("SDP_FindServiceInDb found HDP source or sink\n" );
                                return (p_rec);
                            }
                        }

                    }

                    if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                        && (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)
                        /* for a specific uuid, or any one */
                        && ((p_sattr->attr_value.v.u16 == service_uuid) || service_uuid == 0))
                    {
                        return(p_rec);
                    }

                    /* Checking for Toyota G Block Car Kit:
                    **  This car kit puts an extra data element sequence
                    **  where the UUID is suppose to be!!!
                    */
                    else
                    {
                        if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)
                        {
                            /* Look through data element sequence until no more UUIDs */
                            for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr; p_extra_sattr; p_extra_sattr = p_extra_sattr->p_next_attr)
                            {
                                /* Increment past this to see if the next attribut is UUID */
                                if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) == UUID_DESC_TYPE)
                                    && (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)
                                    /* for a specific uuid, or any one */
                                    && ((p_extra_sattr->attr_value.v.u16 == service_uuid) || (service_uuid == 0)))
                                {
                                    return(p_rec);
                                }
                            }
                        }
                    }
                }
                break;
            }
            else if (p_attr->attr_id == ATTR_ID_SERVICE_ID)
            {
                if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
                    && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2))
                {
                    printf("SDP_FindServiceInDb - p_attr value = 0x%x serviceuuid= 0x%x \r\n", p_attr->attr_value.v.u16, service_uuid);
                }

                if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
                    && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)
                    /* find a specific UUID or anyone */
                    && ((p_attr->attr_value.v.u16 == service_uuid) || service_uuid == 0))
                    return(p_rec);
            }

            p_attr = p_attr->p_next_attr;
        }

        p_rec = p_rec->p_next_rec;
    }
#endif
    /* If here, no matching UUID found */
    return(NULL);
}

/*******************************************************************************
**
** Function         SDP_FindServiceInDb_128bit
**
** Description      This function queries an SDP database for a specific service.
**                  If the p_start_rec pointer is NULL, it looks from the beginning
**                  of the database, else it continues from the next record after
**                  p_start_rec.
**
**                  This function is kept separate from SDP_FindServiceInDb since
**                  that API is expected to return only 16-bit UUIDs
**
** Returns          Pointer to record containing service class, or NULL
**
*******************************************************************************/
tSDP_DISC_REC *SDP_FindServiceInDb_128bit(tSDP_DISCOVERY_DB *p_db, tSDP_DISC_REC *p_start_rec)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_REC   *p_rec;
    tSDP_DISC_ATTR  *p_attr, *p_sattr, *p_extra_sattr;

    /* Must have a valid database */
    if (p_db == NULL)
        return(NULL);

    if (!p_start_rec)
        p_rec = p_db->p_first_rec;
    else
        p_rec = p_start_rec->p_next_rec;

    while (p_rec)
    {
        p_attr = p_rec->p_first_attr;
        while (p_attr)
        {
            if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST)
                && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
            {
                for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
                {
                    if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                        && (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16))
                    {
                        return(p_rec);
                    }
                }
                break;
            }
            else if (p_attr->attr_id == ATTR_ID_SERVICE_ID)
            {
                if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
                    && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16))
                    return(p_rec);
            }

            p_attr = p_attr->p_next_attr;
        }

        p_rec = p_rec->p_next_rec;
    }
#endif
    /* If here, no matching UUID found */
    return(NULL);
}


/*******************************************************************************
**
** Function         SDP_FindServiceUUIDInDb
**
** Description      This function queries an SDP database for a specific service.
**                  If the p_start_rec pointer is NULL, it looks from the beginning
**                  of the database, else it continues from the next record after
**                  p_start_rec.
**
** NOTE             the only difference between this function and the previous function
**                  "SDP_FindServiceInDb()" is that this function takes a tBT_UUID input
**
** Returns          Pointer to record containing service class, or NULL
**
*******************************************************************************/
tSDP_DISC_REC *SDP_FindServiceUUIDInDb (tSDP_DISCOVERY_DB *p_db, tBT_UUID *p_uuid, tSDP_DISC_REC *p_start_rec)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_REC   *p_rec;
    tSDP_DISC_ATTR  *p_attr, *p_sattr;

    /* Must have a valid database */
    if (p_db == NULL)
        return(NULL);

    if (!p_start_rec)
        p_rec = p_db->p_first_rec;
    else
        p_rec = p_start_rec->p_next_rec;

    while (p_rec)
    {
        p_attr = p_rec->p_first_attr;
        while (p_attr)
        {
            if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST)
                && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
            {
                for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
                {
                    if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                    {

                        printf("uuid len=%d ", p_uuid->len);
                        if (p_uuid->len == 2)
                        {
                            printf("uuid=0x%x \n", p_uuid->uu.uuid16);
                        }
                        else
                        {
                            printf("\n");
                        }

                        if (sdpu_compare_uuid_with_attr (p_uuid, p_sattr))
                            return(p_rec);
                    }
                }
                break;
            }
            else if (p_attr->attr_id == ATTR_ID_SERVICE_ID)
            {
                if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE )
                {
                    if (sdpu_compare_uuid_with_attr (p_uuid, p_attr))
                        return(p_rec);
                }
            }

            p_attr = p_attr->p_next_attr;
        }

        p_rec = p_rec->p_next_rec;
    }
#endif  /* CLIENT_ENABLED == TRUE */
    /* If here, no matching UUID found */
    return(NULL);
}

#if SDP_CLIENT_ENABLED == TRUE
/*******************************************************************************
**
** Function         sdp_fill_proto_elem
**
** Description      This function retrieves the protocol element.
**
** Returns          TRUE if found, FALSE if not
**                  If found, the passed protocol list element is filled in.
**
*******************************************************************************/
static BOOLEAN sdp_fill_proto_elem( tSDP_DISC_ATTR  *p_attr, UINT16 layer_uuid,
                                    tSDP_PROTOCOL_ELEM *p_elem)
{
    tSDP_DISC_ATTR  *p_sattr;

    /* Walk through the protocol descriptor list */
    for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr; p_attr = p_attr->p_next_attr)
    {
        /* Safety check - each entry should itself be a sequence */
        if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
            return(FALSE);

        /* Now, see if the entry contains the layer we are interested in */
        for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
        {
            /* SDP_TRACE_DEBUG3 ("SDP - p_sattr 0x%x, layer_uuid:0x%x, u16:0x%x####",
                p_sattr, layer_uuid, p_sattr->attr_value.v.u16); */

            if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                && (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)
                && (p_sattr->attr_value.v.u16 == layer_uuid))
            {
                /* Bingo. Now fill in the passed element */
                p_elem->protocol_uuid = layer_uuid;
                p_elem->num_params = 0;

                /* Store the parameters, if any */
                for (p_sattr = p_sattr->p_next_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
                {
                    if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) != UINT_DESC_TYPE)
                        break;

                    if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)
                        p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u16;
                    else
                        p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u8;

                    if (p_elem->num_params >= SDP_MAX_PROTOCOL_PARAMS)
                        break;
                }
                return(TRUE);
            }
        }
    }

    return(FALSE);
}
#endif  /* CLIENT_ENABLED == TRUE */

/*******************************************************************************
**
** Function         SDP_FindProtocolListElemInRec
**
** Description      This function looks at a specific discovery record for a protocol
**                  list element.
**
** Returns          TRUE if found, FALSE if not
**                  If found, the passed protocol list element is filled in.
**
*******************************************************************************/
BOOLEAN SDP_FindProtocolListElemInRec (tSDP_DISC_REC *p_rec, UINT16 layer_uuid, tSDP_PROTOCOL_ELEM *p_elem)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr;

    p_attr = p_rec->p_first_attr;
    while (p_attr)
    {
        /* Find the protocol descriptor list */
        if ((p_attr->attr_id == ATTR_ID_PROTOCOL_DESC_LIST)
            && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
        {
            return sdp_fill_proto_elem(p_attr, layer_uuid, p_elem);
        }
        p_attr = p_attr->p_next_attr;
    }
#endif
    /* If here, no match found */
    return(FALSE);
}


/*******************************************************************************
**
** Function         SDP_FindAddProtoListsElemInRec
**
** Description      This function looks at a specific discovery record for a protocol
**                  list element.
**
** Returns          TRUE if found, FALSE if not
**                  If found, the passed protocol list element is filled in.
**
*******************************************************************************/
BOOLEAN SDP_FindAddProtoListsElemInRec (tSDP_DISC_REC *p_rec, UINT16 layer_uuid, tSDP_PROTOCOL_ELEM *p_elem)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr, *p_sattr;
    BOOLEAN         ret = FALSE;

    p_attr = p_rec->p_first_attr;
    while (p_attr)
    {
        /* Find the additional protocol descriptor list attribute */
        if ((p_attr->attr_id == ATTR_ID_ADDITION_PROTO_DESC_LISTS)
            && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
        {
            for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
            {
                /* Safety check - each entry should itself be a sequence */
                if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)
                {
                    if ( (ret = sdp_fill_proto_elem(p_sattr, layer_uuid, p_elem)) == TRUE)
                        break;
                }
            }
            return ret;
        }
        p_attr = p_attr->p_next_attr;
    }
#endif
    /* If here, no match found */
    return(FALSE);
}


/*******************************************************************************
**
** Function         SDP_FindProfileVersionInRec
**
** Description      This function looks at a specific discovery record for the
**                  Profile list descriptor, and pulls out the version number.
**                  The version number consists of an 8-bit major version and
**                  an 8-bit minor version.
**
** Returns          TRUE if found, FALSE if not
**                  If found, the major and minor version numbers that were passed
**                  in are filled in.
**
*******************************************************************************/
BOOLEAN SDP_FindProfileVersionInRec (tSDP_DISC_REC *p_rec, UINT16 profile_uuid, UINT16 *p_version)
{
#if SDP_CLIENT_ENABLED == TRUE
    tSDP_DISC_ATTR  *p_attr, *p_sattr;

    p_attr = p_rec->p_first_attr;
    while (p_attr)
    {
        /* Find the profile descriptor list */
        if ((p_attr->attr_id == ATTR_ID_BT_PROFILE_DESC_LIST)
            && (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE))
        {
            /* Walk through the protocol descriptor list */
            for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr; p_attr = p_attr->p_next_attr)
            {
                /* Safety check - each entry should itself be a sequence */
                if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
                    return(FALSE);

                /* Now, see if the entry contains the profile UUID we are interested in */
                for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr)
                {
                    if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE)
                        && (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)  /* <- This is bytes, not size code! */
                        && (p_sattr->attr_value.v.u16 == profile_uuid))
                    {
                        /* Now fill in the major and minor numbers */
                        /* if the attribute matches the description for version (type UINT, size 2 bytes) */
                        p_sattr = p_sattr->p_next_attr;

                        if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UINT_DESC_TYPE) &&
                            (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2))
                        {
                            /* The high order 8 bits is the major number, low order is the minor number (big endian) */
                            *p_version = p_sattr->attr_value.v.u16;

                            return(TRUE);
                        }
                        else
                            return(FALSE);  /* The type and/or size was not valid for the profile list version */
                    }
                }
            }

            return(FALSE);
        }
        p_attr = p_attr->p_next_attr;
    }
#endif  /* CLIENT_ENABLED == TRUE */

    /* If here, no match found */
    return(FALSE);
}

/*******************************************************************************
**                   Device Identification (DI) Client Functions
*******************************************************************************/

/*******************************************************************************
**
** Function         SDP_DiDiscover
**
** Description      This function queries a remote device for DI information.
**
** Returns          SDP_SUCCESS if query started successfully, else error
**
*******************************************************************************/
UINT16 SDP_DiDiscover( BD_ADDR remote_device, tSDP_DISCOVERY_DB *p_db,
                       UINT32 len, tSDP_DISC_CMPL_CB *p_cb )
{
#if SDP_CLIENT_ENABLED == TRUE
    UINT16  result   = SDP_DI_DISC_FAILED;
    UINT16  num_uuids = 1;
    UINT16  di_uuid   = UUID_SERVCLASS_PNP_INFORMATION;

    /* build uuid for db init */
    tSDP_UUID init_uuid;
    init_uuid.len = 2;
    init_uuid.uu.uuid16 = di_uuid;

    if ( SDP_InitDiscoveryDb(p_db, len, num_uuids, &init_uuid, 0, NULL) )
        if ( SDP_ServiceSearchRequest(remote_device, p_db, p_cb) )
            result = SDP_SUCCESS;

    return result;
#else
    return SDP_DI_DISC_FAILED;
#endif
}

/*******************************************************************************
**
** Function         SDP_GetNumDiRecords
**
** Description      Searches specified database for DI records
**
** Returns          number of DI records found
**
*******************************************************************************/
UINT8 SDP_GetNumDiRecords( tSDP_DISCOVERY_DB *p_db )
{
#if SDP_CLIENT_ENABLED == TRUE
    UINT8   num_records = 0;
    tSDP_DISC_REC *p_curr_record = NULL;

    do
    {
        p_curr_record = SDP_FindServiceInDb( p_db, UUID_SERVCLASS_PNP_INFORMATION,
                                             p_curr_record );
        if ( p_curr_record )
            num_records++;
    }while ( p_curr_record );

    return num_records;
#else
    return 0;
#endif
}

/*******************************************************************************
**
** Function         SDP_GetDiRecord
**
** Description      This function retrieves a remote device's DI record from
**                  the specified database.
**
** Returns          SDP_SUCCESS if record retrieved, else error
**
*******************************************************************************/
UINT16 SDP_GetDiRecord( UINT8 get_record_index, tSDP_DI_GET_RECORD *p_device_info,
                        tSDP_DISCOVERY_DB *p_db )
{
#if SDP_CLIENT_ENABLED == TRUE
    UINT16  result = SDP_NO_DI_RECORD_FOUND;
    UINT8  curr_record_index = 1;

    tSDP_DISC_REC *p_curr_record = NULL;

    /* find the requested SDP record in the discovery database */
    do
    {
        p_curr_record = SDP_FindServiceInDb( p_db, UUID_SERVCLASS_PNP_INFORMATION,
                                             p_curr_record );
        if ( p_curr_record )
        {
            if ( curr_record_index++ == get_record_index )
            {
                result = SDP_SUCCESS;
                break;
            }
        }
    }while ( p_curr_record );

    if ( result == SDP_SUCCESS )
    {
        /* copy the information from the SDP record to the DI record */
        tSDP_DISC_ATTR *p_curr_attr = NULL;

        /* ClientExecutableURL is optional */
        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_CLIENT_EXE_URL );
        if ( p_curr_attr )
            BCM_STRNCPY_S( p_device_info->rec.client_executable_url, sizeof(p_device_info->rec.client_executable_url),
                           (char *)p_curr_attr->attr_value.v.array, SDP_MAX_ATTR_LEN );
        else
            p_device_info->rec.client_executable_url[0] = '\0';

        /* Service Description is optional */
        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_SERVICE_DESCRIPTION );
        if ( p_curr_attr )
            BCM_STRNCPY_S( p_device_info->rec.service_description, sizeof(p_device_info->rec.service_description),
                           (char *)p_curr_attr->attr_value.v.array, SDP_MAX_ATTR_LEN );
        else
            p_device_info->rec.service_description[0] = '\0';

        /* DocumentationURL is optional */
        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_DOCUMENTATION_URL );
        if ( p_curr_attr )
            BCM_STRNCPY_S( p_device_info->rec.documentation_url, sizeof(p_device_info->rec.documentation_url),
                           (char *)p_curr_attr->attr_value.v.array, SDP_MAX_ATTR_LEN );
        else
            p_device_info->rec.documentation_url[0] = '\0';

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_SPECIFICATION_ID );
        if ( p_curr_attr )
            p_device_info->spec_id = p_curr_attr->attr_value.v.u16;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_VENDOR_ID );
        if ( p_curr_attr )
            p_device_info->rec.vendor = p_curr_attr->attr_value.v.u16;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_VENDOR_ID_SOURCE );
        if ( p_curr_attr )
            p_device_info->rec.vendor_id_source = p_curr_attr->attr_value.v.u16;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_PRODUCT_ID );
        if ( p_curr_attr )
            p_device_info->rec.product = p_curr_attr->attr_value.v.u16;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_PRODUCT_VERSION );
        if ( p_curr_attr )
            p_device_info->rec.version = p_curr_attr->attr_value.v.u16;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;

        p_curr_attr = SDP_FindAttributeInRec( p_curr_record, ATTR_ID_PRIMARY_RECORD );
        if ( p_curr_attr )
            p_device_info->rec.primary_record = (BOOLEAN)p_curr_attr->attr_value.v.u8;
        else
            result = SDP_ERR_ATTR_NOT_PRESENT;
    }

    return result;
#else   /* SDP_CLIENT_ENABLED is FALSE */
    return SDP_NO_DI_RECORD_FOUND;
#endif
}

/*******************************************************************************
**                   Device Identification (DI) Server Functions
*******************************************************************************/

/*******************************************************************************
**
** Function         SDP_SetLocalDiRecord
**
** Description      This function adds a DI record to the local SDP database.
**
**
**
** Returns          Returns SDP_SUCCESS if record added successfully, else error
**
*******************************************************************************/
UINT16 SDP_SetLocalDiRecord( tSDP_DI_RECORD *p_device_info, UINT32 *p_handle )
{
#if SDP_SERVER_ENABLED == TRUE
    UINT16  result = SDP_SUCCESS;
    UINT32  handle;
    UINT16  di_uuid = UUID_SERVCLASS_PNP_INFORMATION;
    UINT16  di_specid = BLUETOOTH_DI_SPECIFICATION;
    UINT8   temp_u16[2];
    UINT8   *p_temp;
    UINT8   u8;

    *p_handle = 0;
    if ( p_device_info == NULL )
        return SDP_ILLEGAL_PARAMETER;

    /* if record is to be primary record, get handle to replace old primary */
    if ( p_device_info->primary_record == TRUE && sdp_cb.server_db.di_primary_handle )
        handle = sdp_cb.server_db.di_primary_handle;
    else
    {
        if ( (handle = SDP_CreateRecord()) == 0 )
            return SDP_NO_RESOURCES;
    }

    *p_handle = handle;

    /* build the SDP entry */
    /* Add the UUID to the Service Class ID List */
    if ((SDP_AddServiceClassIdList(handle, 1, &di_uuid)) == FALSE)
        result = SDP_DI_REG_FAILED;

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        p_temp = temp_u16;
        UINT16_TO_BE_STREAM(p_temp, di_specid);
        if ( !(SDP_AddAttribute(handle, ATTR_ID_SPECIFICATION_ID,
                                UINT_DESC_TYPE, sizeof(di_specid),
                                temp_u16)) )
            result = SDP_DI_REG_FAILED;
    }

    /* optional - if string is null, do not add attribute */
    if ( result == SDP_SUCCESS )
    {
        if ( p_device_info->client_executable_url[0] != '\0' )
        {
            if ( !((strlen(p_device_info->client_executable_url)+1 <= SDP_MAX_ATTR_LEN) &&
                   SDP_AddAttribute(handle, ATTR_ID_CLIENT_EXE_URL, URL_DESC_TYPE,
                                    (UINT32)(strlen(p_device_info->client_executable_url)+1),
                                    (UINT8 *)p_device_info->client_executable_url)) )
                result = SDP_DI_REG_FAILED;
        }
    }

    /* optional - if string is null, do not add attribute */
    if ( result == SDP_SUCCESS )
    {
        if ( p_device_info->service_description[0] != '\0' )
        {
            if ( !((strlen(p_device_info->service_description)+1 <= SDP_MAX_ATTR_LEN) &&
                   SDP_AddAttribute(handle, ATTR_ID_SERVICE_DESCRIPTION,
                                    TEXT_STR_DESC_TYPE,
                                    (UINT32)(strlen(p_device_info->service_description)+1),
                                    (UINT8 *)p_device_info->service_description)) )
                result = SDP_DI_REG_FAILED;
        }
    }

    /* optional - if string is null, do not add attribute */
    if ( result == SDP_SUCCESS )
    {
        if ( p_device_info->documentation_url[0] != '\0' )
        {
            if ( !((strlen(p_device_info->documentation_url)+1 <= SDP_MAX_ATTR_LEN) &&
                   SDP_AddAttribute(handle, ATTR_ID_DOCUMENTATION_URL, URL_DESC_TYPE,
                                    (UINT32)(strlen(p_device_info->documentation_url)+1),
                                    (UINT8 *)p_device_info->documentation_url)) )
                result = SDP_DI_REG_FAILED;
        }
    }

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        p_temp = temp_u16;
        UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor);
        if ( !(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID, UINT_DESC_TYPE,
                                sizeof(p_device_info->vendor), temp_u16)) )
            result = SDP_DI_REG_FAILED;
    }

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        p_temp = temp_u16;
        UINT16_TO_BE_STREAM (p_temp, p_device_info->product);
        if ( !(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_ID,
                                UINT_DESC_TYPE, sizeof(p_device_info->product), temp_u16)) )
            result = SDP_DI_REG_FAILED;
    }

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        p_temp = temp_u16;
        UINT16_TO_BE_STREAM (p_temp, p_device_info->version);
        if ( !(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_VERSION, UINT_DESC_TYPE,
                                sizeof(p_device_info->version), temp_u16)) )
            result = SDP_DI_REG_FAILED;
    }

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        u8 = (UINT8)p_device_info->primary_record;
        if ( !(SDP_AddAttribute(handle, ATTR_ID_PRIMARY_RECORD,
                                BOOLEAN_DESC_TYPE, 1, &u8)) )
            result = SDP_DI_REG_FAILED;
    }

    /* mandatory */
    if ( result == SDP_SUCCESS)
    {
        p_temp = temp_u16;
        UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor_id_source);
        if ( !(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID_SOURCE, UINT_DESC_TYPE,
                                sizeof(p_device_info->vendor_id_source), temp_u16)) )
            result = SDP_DI_REG_FAILED;
    }

    if ( result != SDP_SUCCESS )
        SDP_DeleteRecord( handle );
    else if (p_device_info->primary_record == TRUE)
        sdp_cb.server_db.di_primary_handle = handle;

    return result;
#else   /* SDP_SERVER_ENABLED is FALSE */
    return SDP_DI_REG_FAILED;
#endif  /* if SDP_SERVER_ENABLED */
}

/*******************************************************************************
**
** Function         SDP_GetLocalDiRecord
**
** Description      This function adds a DI record to the local SDP database.
**
**                  Fills in the device information of the record
**                  p_handle - if p_handle == 0, the primary record is returned
**
** Returns          Returns SDP_SUCCESS if record exists, else error
**
*******************************************************************************/
UINT16 SDP_GetLocalDiRecord(tSDP_DI_GET_RECORD *p_device_info, UINT32 *p_handle )
{
    UINT16 result = SDP_NO_DI_RECORD_FOUND;

#if SDP_SERVER_ENABLED == TRUE
    tSDP_RECORD     *p_rec;
    tSDP_ATTRIBUTE  *p_attr;
    UINT8           *p_temp;
    INT32            templen;

    if (*p_handle == 0)
        *p_handle = sdp_cb.server_db.di_primary_handle;

    if ((p_rec = sdp_db_find_record(*p_handle)) != NULL)
    {
        memset(p_device_info, 0, sizeof(tSDP_DI_RECORD));

        result = SDP_SUCCESS;

        /* Retrieve the Specification ID */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_SPECIFICATION_ID,
                                              ATTR_ID_SPECIFICATION_ID)) != NULL)
        {
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_UINT16 (p_device_info->spec_id, p_temp);
        }

        /* Retrieve the Vendor ID */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_VENDOR_ID,
                                              ATTR_ID_VENDOR_ID)) != NULL)
        {
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_UINT16 (p_device_info->rec.vendor, p_temp);
        }

        /* Retrieve the Product ID */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_PRODUCT_ID,
                                              ATTR_ID_PRODUCT_ID)) != NULL)
        {
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_UINT16 (p_device_info->rec.product, p_temp);
        }

        /* Retrieve the Version ID */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_PRODUCT_VERSION,
                                              ATTR_ID_PRODUCT_VERSION)) != NULL)
        {
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_UINT16 (p_device_info->rec.version, p_temp);
        }

        /* Retrieve the Vendor ID Source ID */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_VENDOR_ID_SOURCE,
                                              ATTR_ID_VENDOR_ID_SOURCE)) != NULL)
        {
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_UINT16 (p_device_info->rec.vendor_id_source, p_temp);
        }

        /* Retrieve the Primary Record */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_PRIMARY_RECORD,
                                              ATTR_ID_PRIMARY_RECORD)) != NULL)
        {
            p_device_info->rec.primary_record = *p_attr->value_ptr;
        }

        /* Retrieve the Client Executable URL */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_CLIENT_EXE_URL,
                                              ATTR_ID_CLIENT_EXE_URL)) != NULL)
        {
            templen = (INT32)((p_attr->len < SDP_MAX_ATTR_LEN) ? p_attr->len : SDP_MAX_ATTR_LEN);
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_ARRAY (p_temp, p_device_info->rec.client_executable_url, templen);
        }

        /* Retrieve the Service Description */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_SERVICE_DESCRIPTION,
                                              ATTR_ID_SERVICE_DESCRIPTION)) != NULL)
        {
            templen = (INT32)((p_attr->len < SDP_MAX_ATTR_LEN) ? p_attr->len : SDP_MAX_ATTR_LEN);
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_ARRAY (p_temp, p_device_info->rec.service_description, templen);
        }

        /* Retrieve the Documentation URL */
        if ((p_attr = sdp_db_find_attr_in_rec(p_rec, ATTR_ID_DOCUMENTATION_URL,
                                              ATTR_ID_DOCUMENTATION_URL)) != NULL)
        {
            templen = (INT32)((p_attr->len < SDP_MAX_ATTR_LEN) ? p_attr->len : SDP_MAX_ATTR_LEN);
            p_temp = p_attr->value_ptr;
            BE_STREAM_TO_ARRAY (p_temp, p_device_info->rec.documentation_url, templen);
        }
    }
    else
        *p_handle = 0;
#endif

    return result;
}


/*******************************************************************************
**
** Function         SDP_SetTraceLevel
**
** Description      This function sets the trace level for SDP. If called with
**                  a value of 0xFF, it simply reads the current trace level.
**
** Returns          the new (current) trace level
**
*******************************************************************************/
UINT8 SDP_SetTraceLevel (UINT8 new_level)
{
    if (new_level != 0xFF)
        sdp_cb.trace_level = new_level;

    return(sdp_cb.trace_level);
}

#if SDP_FOR_JV_INCLUDED == TRUE
/*******************************************************************************
**
** Function         SDP_ConnOpen
**
** Description      This function creates a connection to the SDP server on the
**                  given device.
**
** Returns          0, if failed to initiate connection. Otherwise, the handle.
**
*******************************************************************************/
UINT32 SDP_ConnOpen (UINT8 *p_bd_addr, tSDP_DISC_RES_CB *p_rcb,
                     tSDP_DISC_CMPL_CB *p_cb)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB    *p_ccb;
    UINT32      idx = 0;

    if (!p_cb || !p_rcb)
        return(idx);

    /* Specific BD address */
    p_ccb = sdp_conn_originate (p_bd_addr);

    if (!p_ccb)
        return(idx);

    p_ccb->disc_state = SDP_DISC_WAIT_CONN;
    p_ccb->p_db       = (tSDP_DISCOVERY_DB *)p_rcb;
    p_ccb->p_cb       = p_cb;

    p_ccb->is_attr_search = SDP_IS_PASS_THRU;

    idx = (UINT32)(p_ccb - sdp_cb.ccb);
    return(UINT32)(idx + 1);
#else
    return(0);
#endif
}

/*******************************************************************************
**
** Function         SDP_WriteData
**
** Description      This function sends data to the connected SDP server.
**
** Returns          TRUE if data is sent, FALSE if failed.
**
*******************************************************************************/
BOOLEAN SDP_WriteData (UINT32 handle, BT_HDR  *p_msg)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB    *p_ccb = NULL;

    if (p_msg && (handle > 0) && (handle <= SDP_MAX_CONNECTIONS) )
    {
        p_ccb = &sdp_cb.ccb[handle - 1];
        if ( (p_ccb->con_state == SDP_STATE_CONNECTED) &&
             (p_ccb->con_flags & SDP_FLAGS_IS_ORIG) )
        {
            /* Start inactivity timer */
            btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
            L2CA_DataWrite (p_ccb->connection_id, p_msg);
            return TRUE;
        }
    }
#endif
    return FALSE;
}

/*******************************************************************************
**
** Function         SDP_ConnClose
**
** Description      This function is called to close a SDP connection.
**
** Parameters:      handle      - Handle of the connection returned by SDP_ConnOpen
**
** Returns          TRUE if connection is closed, FALSE if failed to find the handle.
**
*******************************************************************************/
BOOLEAN SDP_ConnClose (UINT32 handle)
{
#if SDP_CLIENT_ENABLED == TRUE
    tCONN_CB    *p_ccb = NULL;

    if (handle > 0 && handle <= SDP_MAX_CONNECTIONS)
    {
        p_ccb = &sdp_cb.ccb[handle - 1];
        sdp_disconnect (p_ccb, SDP_SUCCESS);
        return TRUE;
    }
#endif
    return FALSE;
}
#endif