1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
27
28
33
34
41
42
47
48
49
50
51
53
54
55
56
57
58
59
60
61
76
77
84
85
86
87
88
89
90
103
104
105
106
107
108
109
110
111
112
113
117
118
119
120
121
122
126
127
132
133
134
139
140
141
142
143
144
145
146
147
148
152
153
154
155
156
157
160
161
166
167
170
171
174
175
178
179
180
181
182
183
184
185
186
187
188
189
196
197
198
199
200
201
202
203
204
205
206
210
211
212
213
227
228
229
230
231
232
233
236
237
238
239
240
241
242
246
247
248
249
250
251
252
253
254
255
267
268
269
270
271
272
276
277
278
279
280
284
285
286
287
288
289
290
291
292
293
294
295
296
300
301
302
303
304
305
306
307
308
309
310
313
314
315
316
317
318
319
320
321
322
323
324
325
331
332
333
334
335
336
337
338
339
340
345
346
347
348
349
350
351
352
353
354
355
359
360
364
365
366
367
371
372
373
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
404
405
419
420
421
422
423
424
425
426
427
428
436
437
438
439
440
441
442
443
444
445
451
452
453
454
455
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
542
543
544
545
546
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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
617
618
619
620
621
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
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
697
698
699
700
701
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
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
819
820
821
822
823
824
830
831
832
833
834
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
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
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
939
944
949
954
959
964
969
972
975
976
977
984
987
988
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
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
1062
1063
1064
1065
1066
1067
1068
1069
1080
1081
1082
1083
1084
1086
1087
1088
1089
1090
1091
1092
1093
1094
1104
1105
1106
1107
1109
1110
1111
1112
1113
1114
1115
1116
1117
1138
1139
1140
1141
1142
1143
1144
1145
1148
1149
1150
1151
1152
1153
1154
1155
1156
1161
1162
1163
1169
1170
1171
1176
1177
1178
1183
1184
1185
1186
1187
1192
1193
1194
1195
1196
1197
1198
1199
1215
1216
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1234
1235
1236
1240
1241
1242
1243
1244
1249
1250
1251
1252
1253
1254
1255
1256
1257
1262
1263
1264
1265
1266
1271
1272
1273
1274
1275
1279
1280
1281
1282
1283
1284
1285
1286
1287
1292
1293
1298
1299
1300
1304
1305
1306
1307
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1326
1327
1328
1329
1330
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1344
1345
1346
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1361
1364
1367
1370
1373
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1391
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1413
1427
1430
1431
1432
1433
1434
1435
/* ... */
#include <string.h>
#include "btc_sdp.h"
#include "btc/btc_manage.h"
#include "btc/btc_task.h"
#include "btc/btc_util.h"
#include "bta/bta_sdp_api.h"
#include "bta/bta_sys.h"
#include "bta/utl.h"
#include "bt_sdp.h"
#include "osi/mutex.h"
#include "osi/allocator.h"
#include "esp_sdp_api.h"12 includes
#if (defined BTC_SDP_COMMON_INCLUDED && BTC_SDP_COMMON_INCLUDED == TRUE)
typedef enum {
SDP_RECORD_FREE = 0,
SDP_RECORD_ALLOCED,
}{...} sdp_state_t;
typedef struct {
uint8_t di : 1;
uint8_t primary_di : 1;
uint8_t reserved : 6;
}{...} sdp_flag_t;
typedef struct {
uint8_t state;
sdp_flag_t flag;
int sdp_handle;
esp_bt_uuid_t uuid;
void* record_data;
}{...} sdp_slot_t;
typedef struct {
bool search_allowed;
sdp_slot_t *sdp_slots[SDP_MAX_RECORDS];
osi_mutex_t sdp_slot_mutex;
}{...} sdp_local_param_t;
#if SDP_DYNAMIC_MEMORY == FALSE
static sdp_local_param_t sdp_local_param;
#else
static sdp_local_param_t *sdp_local_param_ptr;
#define sdp_local_param (*sdp_local_param_ptr)/* ... */
#endif
#if SDP_DYNAMIC_MEMORY == FALSE
#define is_sdp_init() (sdp_local_param.sdp_slot_mutex != NULL)
#else
#define is_sdp_init() (&sdp_local_param != NULL && sdp_local_param.sdp_slot_mutex != NULL)
#endif
static void btc_sdp_cleanup(void)
{
#if SDP_DYNAMIC_MEMORY == TRUE
if (sdp_local_param_ptr) {
#endif
if (sdp_local_param.sdp_slot_mutex) {
osi_mutex_free(&sdp_local_param.sdp_slot_mutex);
sdp_local_param.sdp_slot_mutex = NULL;
}{...}
#if SDP_DYNAMIC_MEMORY == TRUE
osi_free(sdp_local_param_ptr);
sdp_local_param_ptr = NULL;
}/* ... */
{...}#endif
}{...}
static inline void btc_sdp_cb_to_app(esp_sdp_cb_event_t event, esp_sdp_cb_param_t *param)
{
esp_sdp_cb_t btc_sdp_cb = (esp_sdp_cb_t)btc_profile_cb_get(BTC_PID_SDP);
if (btc_sdp_cb) {
btc_sdp_cb(event, param);
}{...}
}{...}
static int get_sdp_record_size(bluetooth_sdp_record* in_record)
{
bluetooth_sdp_record *record = in_record;
int records_size = 0;
switch (record->hdr.type) {
case SDP_TYPE_DIP_SERVER:
records_size = sizeof(bluetooth_sdp_record);
break;
...
default:
records_size += sizeof(bluetooth_sdp_record);
records_size += record->hdr.service_name_length;
if (record->hdr.service_name_length > 0) {
records_size++;
}{...}
break;...
}{...}
return records_size;
}{...}
static void set_sdp_slot_info(int id, int sdp_handle, esp_bt_uuid_t *uuid)
{
sdp_slot_t *slot = NULL;
BTC_TRACE_DEBUG("%s() id=%d to sdp_handle=0x%08x", __func__, id, sdp_handle);
if(id >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
return;
}{...}
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
do {
slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) {
BTC_TRACE_ERROR("%s() id = %d ", __func__, id);
break;
}{...}
if (slot->state != SDP_RECORD_ALLOCED) {
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__, id,
sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
break;
}{...}
slot->sdp_handle = sdp_handle;
slot->record_data = NULL;
if (uuid) {
memcpy(&slot->uuid, uuid, sizeof(esp_bt_uuid_t));
}{...} else {
memset(&slot->uuid, 0, sizeof(esp_bt_uuid_t));
}{...}
}{...} while (0);
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
}{...}
static void get_sdp_slot_info(int id, int *sdp_handle, esp_bt_uuid_t *uuid, sdp_flag_t *flag)
{
sdp_slot_t *slot = NULL;
if(id >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
return;
}{...}
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
do {
slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) {
break;
}{...}
if (slot->state != SDP_RECORD_ALLOCED) {
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__, id,
sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
break;
}{...}
if (sdp_handle) {
*sdp_handle = slot->sdp_handle;
}{...}
if (uuid) {
memcpy(uuid, &slot->uuid, sizeof(esp_bt_uuid_t));
}{...}
if (flag) {
*flag = slot->flag;
}{...}
}{...} while (0);
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
}{...}
static int get_sdp_slot_id_by_handle(int handle)
{
sdp_slot_t *slot = NULL;
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
for (int i = 0; i < SDP_MAX_RECORDS; i++) {
slot = sdp_local_param.sdp_slots[i];
if ((slot != NULL) && (slot->sdp_handle == handle)) {
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
return i;
}{...}
}{...}
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
return -1;
}{...}
static bluetooth_sdp_record *start_create_sdp(int id)
{
sdp_slot_t *slot = NULL;
bluetooth_sdp_record* record_data = NULL;
if(id >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
return NULL;
}{...}
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
do {
slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) {
BTC_TRACE_ERROR("%s() id = %d ", __func__, id);
break;
}{...}
if (slot->state != SDP_RECORD_ALLOCED) {
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__, id,
sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
break;
}{...}
record_data = slot->record_data;
}{...} while (0);
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
return record_data;
}{...}
/* ... */
static void copy_sdp_record_common(bluetooth_sdp_record* in_record, bluetooth_sdp_record* out_record)
{
uint8_t *free_ptr = (uint8_t *)(out_record + 1);
memcpy(out_record, in_record, sizeof(bluetooth_sdp_record));
if (in_record->hdr.service_name == NULL || in_record->hdr.service_name_length == 0) {
out_record->hdr.service_name = NULL;
out_record->hdr.service_name_length = 0;
}{...} else {
out_record->hdr.service_name = (char *)free_ptr;
memcpy(free_ptr, in_record->hdr.service_name, in_record->hdr.service_name_length);
free_ptr += in_record->hdr.service_name_length;
*(free_ptr) = '\0';
free_ptr++;
}{...}
}{...}
static void copy_sdp_record(bluetooth_sdp_record* in_record, bluetooth_sdp_record* out_record)
{
switch (in_record->hdr.type) {
case SDP_TYPE_DIP_SERVER:
memcpy(out_record, in_record, sizeof(bluetooth_sdp_record));
break;
...
default:
copy_sdp_record_common(in_record, out_record);
break;...
}{...}
}{...}
static bool check_if_primary_di_record(bluetooth_sdp_record* record)
{
bool ret = false;
if (record->hdr.type == SDP_TYPE_DIP_SERVER) {
bluetooth_sdp_dip_record *di_record = (bluetooth_sdp_dip_record *)record;
ret = di_record->primary_record;
}{...}
return ret;
}{...}
static bool check_if_di_record(bluetooth_sdp_record* record)
{
return record->hdr.type == SDP_TYPE_DIP_SERVER ? true : false;
}{...}
static int alloc_sdp_slot(bluetooth_sdp_record* in_record)
{
int i;
int record_size = get_sdp_record_size(in_record);
bluetooth_sdp_record *record = NULL;
sdp_slot_t **slot = NULL;
bool is_di_record = check_if_di_record(in_record);
bool is_primary_di_record = check_if_primary_di_record(in_record);
bool primary_di_record_found = false;
record = osi_malloc(record_size);
if (record == NULL) {
BTC_TRACE_ERROR("%s() osi_malloc failed!", __func__);
return -1;
}{...}
copy_sdp_record(in_record, record);
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
if (is_di_record && is_primary_di_record) {
for (i = 0; i < SDP_MAX_RECORDS; i++) {
slot = &sdp_local_param.sdp_slots[i];
if ((*slot) && (*slot)->flag.di && (*slot)->flag.primary_di) {
BTC_TRACE_WARNING("%s() overwrite primary di record!", __func__);
if ((*slot)->record_data) {
osi_free((*slot)->record_data);
}{...}
(*slot)->record_data = record;
primary_di_record_found = true;
break;
}{...}
}{...}
}{...}
if (!primary_di_record_found) {
for (i = 0; i < SDP_MAX_RECORDS; i++) {
slot = &sdp_local_param.sdp_slots[i];
if ((*slot) == NULL) {
if (((*slot) = (sdp_slot_t *)osi_malloc(sizeof(sdp_slot_t))) == NULL) {
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
BTC_TRACE_ERROR("%s() osi_malloc slot failed!", __func__);
osi_free(record);
return -1;
}{...}
(*slot)->flag.di = is_di_record;
(*slot)->flag.primary_di = is_primary_di_record;
(*slot)->state = SDP_RECORD_ALLOCED;
(*slot)->record_data = record;
break;
}{...}
}{...}
}{...}
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
if(i >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - no more free slots!", __func__);
osi_free(record);
return -1;
}{...}
return i;
}{...}
static int free_sdp_slot(int id)
{
int handle = -1;
bluetooth_sdp_record *record = NULL;
sdp_slot_t *slot = NULL;
if(id >= SDP_MAX_RECORDS) {
APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
return handle;
}{...}
slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) {
return handle;
}{...}
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
handle = slot->sdp_handle;
if (slot->state != SDP_RECORD_FREE) {
record = slot->record_data;
}{...}
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
if(record != NULL) {
osi_free(record);
}{...} else {
handle = -1;
}{...}
osi_free(sdp_local_param.sdp_slots[id]);
sdp_local_param.sdp_slots[id] = NULL;
return handle;
}{...}
static int add_raw_sdp(const bluetooth_sdp_raw_record *rec)
{
tSDP_PROTOCOL_ELEM protoList [2];
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT8 temp[LEN_UUID_128];
UINT8* p_temp = temp;
UINT32 sdp_handle = 0;
const esp_bt_uuid_t *p_uuid = &rec->hdr.uuid;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register raw sdp record", __func__);
return sdp_handle;
}{...}
if (p_uuid->len == ESP_UUID_LEN_16) {
UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
UINT16_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid16);
}{...} else if (p_uuid->len == ESP_UUID_LEN_32) {
UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
UINT32_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid32);
}{...} else if (p_uuid->len == ESP_UUID_LEN_128) {
UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
ARRAY_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid128, LEN_UUID_128);
}{...} else {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
return sdp_handle;
}{...}
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST,
DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p_temp - temp), temp);
memset( protoList, 0 , 2*sizeof(tSDP_PROTOCOL_ELEM) );
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
if (rec->hdr.rfcomm_channel_number < 0) {
status &= SDP_AddProtocolList(sdp_handle, 1, protoList);
}{...} else {
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
status &= SDP_AddProtocolList(sdp_handle, 2, protoList);
}{...}
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
if(rec->hdr.l2cap_psm != -1) {
p_temp = temp;
UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
UINT_DESC_TYPE, (UINT32)2, temp);
}{...}
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED, status = %d", __func__, status);
}{...} else {
if (p_uuid->len == ESP_UUID_LEN_16) {
bta_sys_add_uuid(p_uuid->uuid.uuid16);
}{...} else if (p_uuid->len == ESP_UUID_LEN_32) {
bta_sys_add_uuid_32(p_uuid->uuid.uuid32);
}{...} else if (p_uuid->len == ESP_UUID_LEN_128) {
bta_sys_add_uuid_128((UINT8 *)&p_uuid->uuid.uuid128);
}{...}
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_maps_sdp(const bluetooth_sdp_mas_record* rec)
{
tSDP_PROTOCOL_ELEM protoList [3];
UINT16 service = UUID_SERVCLASS_MESSAGE_ACCESS;
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT32 sdp_handle = 0;
UINT8 temp[4];
UINT8* p_temp = temp;
BTC_TRACE_DEBUG("%s(): MASID = 0x%02x, scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->mas_instance_id, rec->hdr.rfcomm_channel_number,
rec->hdr.l2cap_psm, rec->hdr.service_name);
BTC_TRACE_DEBUG(" msg_types: 0x%02x, feature_bits: 0x%08x",
rec->supported_message_types, rec->supported_features);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s() - Unable to register MAPS Service", __func__);
return sdp_handle;
}{...}
status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
protoList[2].num_params = 0;
status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_MAP_PROFILE,
rec->hdr.profile_version);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAS_INSTANCE_ID, UINT_DESC_TYPE,
(UINT32)1, (UINT8*)&rec->mas_instance_id);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_MSG_TYPE, UINT_DESC_TYPE,
(UINT32)1, (UINT8*)&rec->supported_message_types);
UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES,
UINT_DESC_TYPE, (UINT32)4, temp);
if(rec->hdr.l2cap_psm != -1) {
p_temp = temp;
UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
UINT_DESC_TYPE, (UINT32)2, temp);
}{...}
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED", __func__);
}{...} else {
bta_sys_add_uuid(service);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec)
{
tSDP_PROTOCOL_ELEM protoList [3];
UINT16 service = UUID_SERVCLASS_MESSAGE_NOTIFICATION;
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT32 sdp_handle = 0;
UINT8 temp[4];
UINT8* p_temp = temp;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
BTC_TRACE_DEBUG(" feature_bits: 0x%08x", rec->supported_features);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register MAP Notification Service", __func__);
return sdp_handle;
}{...}
status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
protoList[2].num_params = 0;
status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_MAP_PROFILE,
rec->hdr.profile_version);
UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES,
UINT_DESC_TYPE, (UINT32)4, temp);
if(rec->hdr.l2cap_psm != -1) {
p_temp = temp;
UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
UINT_DESC_TYPE, (UINT32)2, temp);
}{...}
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED", __func__);
}{...} else {
bta_sys_add_uuid(service);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec)
{
tSDP_PROTOCOL_ELEM protoList [3];
UINT16 service = UUID_SERVCLASS_PBAP_PSE;
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT32 sdp_handle = 0;
UINT8 temp[4];
UINT8* p_temp = temp;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
BTC_TRACE_DEBUG(" supported_repositories: 0x%08x, feature_bits: 0x%08x",
rec->supported_repositories, rec->supported_features);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register PBAP Server Service", __func__);
return sdp_handle;
}{...}
status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
protoList[2].num_params = 0;
status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_PHONE_ACCESS,
rec->hdr.profile_version);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_REPOSITORIES,
UINT_DESC_TYPE, (UINT32)1, (UINT8*)&rec->supported_repositories);
UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_PBAP_SUPPORTED_FEATURES,
UINT_DESC_TYPE, (UINT32)4, temp);
if(rec->hdr.l2cap_psm != -1) {
p_temp = temp;
UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
UINT_DESC_TYPE, (UINT32)2, temp);
}{...}
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED, status = %d", __func__, status);
}{...} else {
bta_sys_add_uuid(service);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_pbapc_sdp(const bluetooth_sdp_pce_record* rec)
{
UINT16 service = UUID_SERVCLASS_PBAP_PCE;
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT32 sdp_handle = 0;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register PBAP Client Service", __func__);
return sdp_handle;
}{...}
status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_PHONE_ACCESS,
rec->hdr.profile_version);
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED, status = %d", __func__, status);
}{...} else {
bta_sys_add_uuid(service);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_opps_sdp(const bluetooth_sdp_ops_record* rec)
{
tSDP_PROTOCOL_ELEM protoList [3];
UINT16 service = UUID_SERVCLASS_OBEX_OBJECT_PUSH;
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
UINT8 type_len[rec->supported_formats_list_len];
UINT8 desc_type[rec->supported_formats_list_len];
UINT8 *type_value[rec->supported_formats_list_len];
bool status = true;
UINT32 sdp_handle = 0;
UINT8 temp[4];
UINT8* p_temp = temp;
tBTA_UTL_COD cod;
int i,j;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
BTC_TRACE_DEBUG(" supported formats count: %d",
rec->supported_formats_list_len);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register Object Push Server Service", __func__);
return sdp_handle;
}{...}
status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
protoList[2].num_params = 0;
status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_OBEX_OBJECT_PUSH,
rec->hdr.profile_version);
for (i = 0, j = 0; i < rec->supported_formats_list_len; i++) {
type_value[j] = (UINT8 *) &rec->supported_formats_list[i];
desc_type[j] = UINT_DESC_TYPE;
type_len[j++] = 1;
}{...}
status &= SDP_AddSequence(sdp_handle, (UINT16) ATTR_ID_SUPPORTED_FORMATS_LIST,
(UINT8) rec->supported_formats_list_len, desc_type, type_len, type_value);
if(rec->hdr.l2cap_psm != -1) {
p_temp = temp;
UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
UINT_DESC_TYPE, (UINT32)2, temp);
}{...}
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s() FAILED", __func__);
}{...} else {
cod.service = BTM_COD_SERVICE_OBJ_TRANSFER;
utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
bta_sys_add_uuid(service);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_saps_sdp(const bluetooth_sdp_sap_record* rec)
{
tSDP_PROTOCOL_ELEM protoList [2];
UINT16 services[2];
UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
bool status = true;
UINT32 sdp_handle = 0;
BTC_TRACE_DEBUG("%s(): scn 0x%02x, service name %s", __func__,
rec->hdr.rfcomm_channel_number, rec->hdr.service_name);
if ((sdp_handle = SDP_CreateRecord()) == 0) {
BTC_TRACE_ERROR("%s(): Unable to register SAPS Service", __func__);
return sdp_handle;
}{...}
services[0] = UUID_SERVCLASS_SAP;
services[1] = UUID_SERVCLASS_GENERIC_TELEPHONY;
status &= SDP_AddServiceClassIdList(sdp_handle, 2, services);
memset(protoList, 0, 2 * sizeof(tSDP_PROTOCOL_ELEM));
protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
protoList[0].num_params = 0;
protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
protoList[1].num_params = 1;
protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
status &= SDP_AddProtocolList(sdp_handle, 2, protoList);
status &= SDP_AddAttribute(sdp_handle,
(UINT16)ATTR_ID_SERVICE_NAME,
(UINT8)TEXT_STR_DESC_TYPE,
(UINT32)(rec->hdr.service_name_length + 1),
(UINT8 *)rec->hdr.service_name);
status &= SDP_AddProfileDescriptorList(sdp_handle,
UUID_SERVCLASS_SAP,
rec->hdr.profile_version);
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) {
SDP_DeleteRecord(sdp_handle);
sdp_handle = 0;
BTC_TRACE_ERROR("%s(): FAILED deleting record", __func__);
}{...} else {
bta_sys_add_uuid(UUID_SERVCLASS_SAP);
BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle);
}{...}
return sdp_handle;
}{...}
static int add_dips_sdp(bluetooth_sdp_dip_record *rec)
{
UINT32 sdp_handle = 0;
tBTA_DI_RECORD device_info = {0};
device_info.vendor = rec->vendor;
device_info.vendor_id_source = rec->vendor_id_source;
device_info.product = rec->product;
device_info.version = rec->version;
device_info.primary_record = rec->primary_record;
BTA_DmSetLocalDiRecord(&device_info, &sdp_handle);
return sdp_handle;
}{...}
static int btc_handle_create_record_event(int id)
{
int sdp_handle = 0;
bluetooth_sdp_record *record = start_create_sdp(id);
esp_bt_uuid_t service_uuid = {0};
BTC_TRACE_DEBUG("Sdp Server %s", __func__);
if (record != NULL) {
switch (record->hdr.type) {
case SDP_TYPE_RAW:
sdp_handle = add_raw_sdp(&record->raw);
memcpy(&service_uuid, &record->hdr.uuid, sizeof(esp_bt_uuid_t));
break;...
case SDP_TYPE_MAP_MAS:
sdp_handle = add_maps_sdp(&record->mas);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_MESSAGE_ACCESS;
break;...
case SDP_TYPE_MAP_MNS:
sdp_handle = add_mapc_sdp(&record->mns);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_MESSAGE_NOTIFICATION;
break;...
case SDP_TYPE_PBAP_PSE:
sdp_handle = add_pbaps_sdp(&record->pse);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_PBAP_PSE;
break;...
case SDP_TYPE_PBAP_PCE:
sdp_handle = add_pbapc_sdp(&record->pce);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_PBAP_PCE;
break;...
case SDP_TYPE_OPP_SERVER:
sdp_handle = add_opps_sdp(&record->ops);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_OBEX_OBJECT_PUSH;
break;...
case SDP_TYPE_SAP_SERVER:
sdp_handle = add_saps_sdp(&record->sap);
service_uuid.len = ESP_UUID_LEN_16;
service_uuid.uuid.uuid16 = UUID_SERVCLASS_SAP;
break;...
case SDP_TYPE_DIP_SERVER:
sdp_handle = add_dips_sdp(&record->dip);
break;...
default:
BTC_TRACE_DEBUG("Record type %d is not supported", record->hdr.type);
break;...
}{...}
if(sdp_handle != 0) {
set_sdp_slot_info(id, sdp_handle, &service_uuid);
osi_free(record);
}{...} else {
sdp_handle = -1;
}{...}
}{...} else {
sdp_handle = -1;
}{...}
if (sdp_handle == -1) {
free_sdp_slot(id);
}{...}
return sdp_handle;
}{...}
static bool btc_sdp_remove_record_event(int id, int *p_sdp_handle)
{
BTC_TRACE_DEBUG("Sdp Server %s", __func__);
bool result = false;
int sdp_handle = -1;
sdp_flag_t flag = {0};
esp_bt_uuid_t service_uuid = {0};
get_sdp_slot_info(id, &sdp_handle, &service_uuid, &flag);
if (sdp_handle > 0) {
if (flag.di && BTA_DmRemoveLocalDiRecord(sdp_handle) == BTA_SUCCESS) {
result = true;
}{...} else {
do {
result = SDP_DeleteRecord(sdp_handle);
if (!result) {
BTC_TRACE_ERROR("Unable to remove handle 0x%08x", sdp_handle);
break;
}{...}
if (service_uuid.len == ESP_UUID_LEN_16) {
bta_sys_remove_uuid(service_uuid.uuid.uuid16);
}{...} else if (service_uuid.len == ESP_UUID_LEN_32) {
bta_sys_remove_uuid_32(service_uuid.uuid.uuid32);
}{...} else if (service_uuid.len == ESP_UUID_LEN_128) {
bta_sys_remove_uuid_128((UINT8 *)&service_uuid.uuid.uuid128);
}{...}
}{...} while (0);
}{...}
if (p_sdp_handle) {
*p_sdp_handle = sdp_handle;
}{...}
}{...}
return result;
}{...}
static void btc_sdp_cb_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
switch (msg->act) {
case BTA_SDP_SEARCH_COMP_EVT: {
tBTA_SDP_SEARCH_COMP *src_search_comp = (tBTA_SDP_SEARCH_COMP *)p_src;
tBTA_SDP_SEARCH_COMP *dest_search_comp = (tBTA_SDP_SEARCH_COMP *)p_dest;
int record_count = src_search_comp->record_count;
for (int i = 0; i < record_count; i++) {
bluetooth_sdp_record *src_record = &src_search_comp->records[i];
bluetooth_sdp_record *dest_record = &dest_search_comp->records[i];
uint32_t src_service_name_length = src_record->hdr.service_name_length;
char *src_service_name = src_record->hdr.service_name;
dest_record->hdr.service_name_length = 0;
dest_record->hdr.service_name = NULL;
if (src_service_name && src_service_name_length) {
char *service_name = (char *)osi_malloc(src_service_name_length + 1);
if (service_name) {
memcpy(service_name, src_service_name, src_service_name_length);
service_name[src_service_name_length] = '\0';
dest_record->hdr.service_name_length = src_service_name_length;
dest_record->hdr.service_name = service_name;
}{...} else {
BTC_TRACE_ERROR("%s malloc service name failed, orig service name:%s", __func__, src_service_name);
}{...}
}{...}
int src_user1_ptr_len = src_record->hdr.user1_ptr_len;
uint8_t *src_user1_ptr = src_record->hdr.user1_ptr;
dest_record->hdr.user1_ptr_len = 0;
dest_record->hdr.user1_ptr = NULL;
if (src_record->hdr.type == SDP_TYPE_RAW && src_user1_ptr && src_user1_ptr_len) {
uint8_t *user1_ptr = (uint8_t *)osi_malloc(src_user1_ptr_len);
if (user1_ptr) {
memcpy(user1_ptr, src_user1_ptr, src_user1_ptr_len);
dest_record->hdr.user1_ptr_len = src_user1_ptr_len;
dest_record->hdr.user1_ptr = user1_ptr;
}{...} else {
BTC_TRACE_ERROR("%s malloc user1_ptr failed", __func__);
}{...}
}{...}
}{...}
break;
}{...}
... default:
break;...
}{...}
}{...}
static void btc_sdp_cb_arg_deep_free(btc_msg_t *msg)
{
switch (msg->act) {
case BTA_SDP_SEARCH_COMP_EVT: {
tBTA_SDP_SEARCH_COMP *search_comp = (tBTA_SDP_SEARCH_COMP *)msg->arg;
for (size_t i = 0; i < search_comp->record_count; i++) {
bluetooth_sdp_record *record = &search_comp->records[i];
if (record->hdr.service_name) {
osi_free(record->hdr.service_name);
}{...}
if (record->hdr.user1_ptr) {
osi_free(record->hdr.user1_ptr);
}{...}
}{...}
break;
}{...}
... default:
break;...
}{...}
}{...}
static void btc_sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, void* user_data)
{
btc_msg_t msg;
bt_status_t status;
switch (event) {
case BTA_SDP_CREATE_RECORD_USER_EVT: {
if (p_data->sdp_create_record.status == BTA_SDP_SUCCESS) {
p_data->sdp_create_record.handle = btc_handle_create_record_event((int)user_data);
if (p_data->sdp_create_record.handle < 0) {
p_data->sdp_create_record.status = BTA_SDP_FAILURE;
}{...}
}{...}
}{...}
break;...
case BTA_SDP_REMOVE_RECORD_USER_EVT: {
if (p_data->sdp_remove_record.status == BTA_SDP_SUCCESS) {
if (btc_sdp_remove_record_event((int)user_data, &p_data->sdp_remove_record.handle) == false) {
p_data->sdp_remove_record.status = BTA_SDP_FAILURE;
}{...}
}{...}
}{...}
break;...
default:
break;...
}{...}
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_SDP;
msg.act = event;
status = btc_transfer_context(&msg, p_data, sizeof(tBTA_SDP), btc_sdp_cb_arg_deep_copy, btc_sdp_cb_arg_deep_free);
if (status != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_transfer_context failed", __func__);
}{...}
}{...}
static void btc_sdp_init(void)
{
esp_sdp_cb_param_t param;
esp_sdp_status_t ret = ESP_SDP_SUCCESS;
do {
if (is_sdp_init()) {
BTC_TRACE_ERROR("%s SDP has been initiated, shall deinit first!", __func__);
ret = ESP_SDP_NEED_DEINIT;
break;
}{...}
#if SDP_DYNAMIC_MEMORY == TRUE
if ((sdp_local_param_ptr = (sdp_local_param_t *)osi_malloc(sizeof(sdp_local_param_t))) == NULL) {
BTC_TRACE_ERROR("%s malloc failed\n", __func__);
ret = ESP_SDP_NO_RESOURCE;
break;
}{...}
/* ... */#endif
memset(&sdp_local_param, 0, sizeof(sdp_local_param_t));
if (osi_mutex_new(&sdp_local_param.sdp_slot_mutex) != 0) {
BTC_TRACE_ERROR("%s osi_mutex_new failed\n", __func__);
ret = ESP_SDP_NO_RESOURCE;
break;
}{...}
ret = BTA_SdpEnable(btc_sdp_dm_cback);
if (ret != ESP_SDP_SUCCESS) {
BTC_TRACE_ERROR("%s BTA_SdpEnable failed, ret = %d\n", __func__, ret);
ret = ESP_SDP_FAILURE;
break;
}{...}
sdp_local_param.search_allowed = true;
}{...} while(0);
if (ret != ESP_SDP_SUCCESS) {
btc_sdp_cleanup();
param.init.status = ret;
btc_sdp_cb_to_app(ESP_SDP_INIT_EVT, ¶m);
}{...}
}{...}
static void btc_sdp_deinit(void)
{
esp_sdp_cb_param_t param;
esp_sdp_status_t ret = ESP_SDP_SUCCESS;
do {
if (!is_sdp_init()) {
BTC_TRACE_ERROR("%s SDP has not been initiated, shall init first!", __func__);
ret = ESP_SDP_NEED_INIT;
break;
}{...}
for(int i = 0; i < SDP_MAX_RECORDS; i++) {
int sdp_handle = -1;
get_sdp_slot_info(i, &sdp_handle, NULL, NULL);
if (sdp_handle > 0) {
BTA_SdpRemoveRecordByUser((void*)i);
}{...}
}{...}
BTA_SdpDisable();
}{...} while(0);
if (ret != ESP_SDP_SUCCESS) {
param.deinit.status = ret;
btc_sdp_cb_to_app(ESP_SDP_DEINIT_EVT, ¶m);
}{...}
}{...}
static void btc_sdp_create_record(btc_sdp_args_t *arg)
{
int slot_id;
esp_sdp_cb_param_t param;
esp_sdp_status_t ret = ESP_SDP_SUCCESS;
do {
if (!is_sdp_init()) {
BTC_TRACE_ERROR("%s SDP has not been initiated, shall init first!", __func__);
ret = ESP_SDP_NEED_INIT;
break;
}{...}
slot_id = alloc_sdp_slot(arg->create_record.record);
if (slot_id < 0) {
ret = ESP_SDP_FAILURE;
break;
}{...}
BTA_SdpCreateRecordByUser((void *) slot_id);
}{...} while(0);
if (ret != ESP_SDP_SUCCESS) {
param.create_record.status = ret;
param.create_record.record_handle = -1;
btc_sdp_cb_to_app(ESP_SDP_CREATE_RECORD_COMP_EVT, ¶m);
}{...}
}{...}
static void btc_sdp_remove_record(btc_sdp_args_t *arg)
{
esp_sdp_cb_param_t param;
esp_sdp_status_t ret = ESP_SDP_SUCCESS;
do {
if (!is_sdp_init()) {
BTC_TRACE_ERROR("%s SDP has not been initiated, shall init first!", __func__);
ret = ESP_SDP_NEED_INIT;
break;
}{...}
int slot_id = get_sdp_slot_id_by_handle(arg->remove_record.record_handle);
if (slot_id < 0) {
BTC_TRACE_ERROR("%s SDP record with handle %d not found", __func__, arg->remove_record.record_handle);
ret = ESP_SDP_NO_CREATE_RECORD;
break;
}{...}
BTA_SdpRemoveRecordByUser((void *)slot_id);
}{...} while(0);
if (ret != ESP_SDP_SUCCESS) {
param.create_record.status = ret;
btc_sdp_cb_to_app(ESP_SDP_REMOVE_RECORD_COMP_EVT, ¶m);
}{...}
}{...}
static void btc_sdp_search(btc_sdp_args_t *arg)
{
esp_sdp_cb_param_t param = {0};
esp_sdp_status_t ret = ESP_SDP_SUCCESS;
do {
if (!is_sdp_init()) {
BTC_TRACE_ERROR("%s SDP has not been initiated, shall init first!", __func__);
ret = ESP_SDP_NEED_INIT;
break;
}{...}
if (!sdp_local_param.search_allowed) {
BTC_TRACE_ERROR("%s SDP search is not allowed!", __func__);
ret = ESP_SDP_NO_RESOURCE;
break;
}{...}
BTA_SdpSearch(arg->search.bd_addr, &arg->search.sdp_uuid);
/* ... */
sdp_local_param.search_allowed = false;
}{...} while(0);
if (ret != ESP_SDP_SUCCESS) {
param.search.status = ret;
btc_sdp_cb_to_app(ESP_SDP_SEARCH_COMP_EVT, ¶m);
}{...}
}{...}
void btc_sdp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
switch (msg->act) {
case BTC_SDP_ACT_CREATE_RECORD: {
bluetooth_sdp_record **dst_record = &((btc_sdp_args_t *)p_dest)->create_record.record;
bluetooth_sdp_record *src_record = ((btc_sdp_args_t *)p_src)->create_record.record;
bluetooth_sdp_record *record = (bluetooth_sdp_record *)osi_calloc(get_sdp_record_size(src_record));
if (record) {
copy_sdp_record(src_record, record);
}{...} else {
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
break;
}{...}
*dst_record = record;
break;
}{...}
... default:
break;...
}{...}
}{...}
void btc_sdp_arg_deep_free(btc_msg_t *msg)
{
switch (msg->act) {
case BTC_SDP_ACT_CREATE_RECORD: {
btc_sdp_args_t *arg = (btc_sdp_args_t *)msg->arg;
bluetooth_sdp_record *record = arg->create_record.record;
if (record) {
osi_free(record);
}{...}
break;
}{...}
... default:
break;...
}{...}
}{...}
void btc_sdp_call_handler(btc_msg_t *msg)
{
btc_sdp_args_t *arg = (btc_sdp_args_t *)(msg->arg);
BTC_TRACE_DEBUG("%s: event = %d\n", __func__, msg->act);
switch (msg->act) {
case BTC_SDP_ACT_INIT:
btc_sdp_init();
break;...
case BTC_SDP_ACT_DEINIT:
btc_sdp_deinit();
break;...
case BTC_SDP_ACT_SEARCH:
btc_sdp_search(arg);
break;...
case BTC_SDP_ACT_CREATE_RECORD:
btc_sdp_create_record(arg);
break;...
case BTC_SDP_ACT_REMOVE_RECORD:
btc_sdp_remove_record(arg);
break;...
default:
BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __func__, msg->act);
break;...
}{...}
btc_sdp_arg_deep_free(msg);
}{...}
void btc_sdp_cb_handler(btc_msg_t *msg)
{
esp_sdp_cb_param_t param = {0};
uint8_t event = msg->act;
tBTA_SDP *p_data = (tBTA_SDP *)msg->arg;
switch(event) {
case BTA_SDP_ENABLE_EVT:
param.init.status = p_data->status;
btc_sdp_cb_to_app(ESP_SDP_INIT_EVT, ¶m);
break;...
case BTA_SDP_DISABLE_EVT:
BTA_SdpCleanup();
btc_sdp_cleanup();
param.deinit.status = ESP_SDP_SUCCESS;
btc_sdp_cb_to_app(ESP_SDP_DEINIT_EVT, ¶m);
break;...
case BTA_SDP_SEARCH_COMP_EVT:
sdp_local_param.search_allowed = true;
param.search.status = p_data->sdp_search_comp.status;
memcpy(param.search.remote_addr, p_data->sdp_search_comp.remote_addr, sizeof(BD_ADDR));
bta_to_btc_uuid(¶m.search.sdp_uuid, &p_data->sdp_search_comp.uuid);
param.search.record_count = p_data->sdp_search_comp.record_count;
param.search.records = (esp_bluetooth_sdp_record_t *)p_data->sdp_search_comp.records;
btc_sdp_cb_to_app(ESP_SDP_SEARCH_COMP_EVT, ¶m);
break;...
case BTA_SDP_CREATE_RECORD_USER_EVT:
param.create_record.status = p_data->sdp_create_record.status;
param.create_record.record_handle = p_data->sdp_create_record.handle;
btc_sdp_cb_to_app(ESP_SDP_CREATE_RECORD_COMP_EVT, ¶m);
break;...
case BTA_SDP_REMOVE_RECORD_USER_EVT:
if (p_data->sdp_remove_record.status == BTA_SDP_SUCCESS) {
int slot_id = get_sdp_slot_id_by_handle(p_data->sdp_remove_record.handle);
if (slot_id < 0) {
p_data->sdp_remove_record.status = ESP_SDP_NO_CREATE_RECORD;
break;
}{...} else {
free_sdp_slot(slot_id);
}{...}
}{...}
param.remove_record.status = p_data->sdp_remove_record.status;
btc_sdp_cb_to_app(ESP_SDP_REMOVE_RECORD_COMP_EVT, ¶m);
break;...
default:
BTC_TRACE_DEBUG("%s: Unhandled event (%d)!", __func__, msg->act);
break;...
}{...}
btc_sdp_cb_arg_deep_free(msg);
}{...}
/* ... */
#endif