blob: b159eb500a3bcafdcb495416c8345e31b3f00071 [file] [log] [blame]
Kim Schulz8372aa52015-03-25 10:39:40 +01001/******************************************************************************
2 *
3 * Copyright (C) 2014 Samsung System LSI
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: btif_sdp_server.c
22 * Description: SDP server Bluetooth Interface to create and remove SDP records.
23 * To be used in combination with the RFCOMM/L2CAP(LE) sockets.
24 *
25 *
26 ***********************************************************************************/
27
28#include <hardware/bluetooth.h>
29#include <hardware/bt_sdp.h>
30#include <stdlib.h>
Ian Coolidge949baae2015-04-16 13:50:46 -070031#include <string.h>
Vinit Deshpandef54df6b2015-04-15 13:02:58 -070032#include <pthread.h>
Kim Schulz8372aa52015-03-25 10:39:40 +010033
Marie Janssen49120dc2015-07-07 16:47:20 -070034#define LOG_TAG "bt_btif_sdp_server"
Kim Schulz8372aa52015-03-25 10:39:40 +010035#include "btif_common.h"
36#include "btif_util.h"
37#include "bta_sdp_api.h"
38#include "bta_sys.h"
Kim Schulz8372aa52015-03-25 10:39:40 +010039#include "btif_sock_util.h"
Arman Uguray625ec342015-06-10 16:21:12 -070040#include "osi/include/allocator.h"
41#include "utl.h"
Kim Schulz8372aa52015-03-25 10:39:40 +010042
Kim Schulz8372aa52015-03-25 10:39:40 +010043static pthread_mutex_t sdp_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Kim Schulz8372aa52015-03-25 10:39:40 +010044
45
46/**
47 * The need for a state variable have been reduced to two states.
48 * The remaining state control is handled by program flow
49 */
50typedef enum {
51 SDP_RECORD_FREE = 0,
52 SDP_RECORD_ALLOCED,
53} sdp_state_t;
54
55typedef struct {
56 sdp_state_t state;
57 int sdp_handle;
58 bluetooth_sdp_record* record_data;
59} sdp_slot_t;
60
61#define MAX_SDP_SLOTS 128
62static sdp_slot_t sdp_slots[MAX_SDP_SLOTS];
63
64/*****************************************************************************
65 * LOCAL Functions
66 *****************************************************************************/
67static int add_maps_sdp(const bluetooth_sdp_mas_record* rec);
68static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec);
69static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec);
70static int add_opps_sdp(const bluetooth_sdp_ops_record* rec);
71bt_status_t remove_sdp_record(int record_id);
72static int free_sdp_slot(int id);
73
74/******************************************************************************
75 * WARNING: Functions below are not called in BTU context.
76 * Introduced to make it possible to create SDP records from JAVA with both a
77 * RFCOMM channel and a L2CAP PSM.
78 * Overall architecture:
79 * 1) JAVA calls createRecord() which returns a pseudo ID which at a later
80 * point will be linked to a specific SDP handle.
81 * 2) createRecord() requests the BTU task(thread) to call a callback in SDP
82 * which creates the actual record, and updates the ID<->SDPHandle map
83 * based on the ID beeing passed to BTA as user_data.
84 *****************************************************************************/
85
86static void init_sdp_slots()
87{
88 int i;
89 memset(sdp_slots, 0, sizeof(sdp_slot_t)*MAX_SDP_SLOTS);
90 /* if SDP_RECORD_FREE is zero - no need to set the value */
91 if(SDP_RECORD_FREE != 0) {
92 for(i = 0; i < MAX_SDP_SLOTS; i++)
93 {
94 sdp_slots[i].state = SDP_RECORD_FREE;
95 }
96 }
97}
98
99bt_status_t sdp_server_init()
100{
101 BTIF_TRACE_DEBUG("Sdp Server %s", __FUNCTION__);
102 init_sdp_slots();
103 return BT_STATUS_SUCCESS;
104}
105
106void sdp_server_cleanup()
107{
108 BTIF_TRACE_DEBUG("Sdp Server %s", __FUNCTION__);
109 pthread_mutex_lock(&sdp_lock);
110 int i;
111 for(i = 0; i < MAX_SDP_SLOTS; i++)
112 {
113 /*remove_sdp_record(i); we cannot send messages to the other threads, since they might
114 * have been shut down already. Just do local cleanup.
115 */
116 free_sdp_slot(i);
117 }
118 pthread_mutex_unlock(&sdp_lock);
119}
120
121
122int get_sdp_records_size(bluetooth_sdp_record* in_record, int count) {
123 bluetooth_sdp_record* record = in_record;
124 int records_size = 0;
125 int i;
126 for(i=0; i<count; i++) {
127 record = &in_record[i];
128 records_size += sizeof(bluetooth_sdp_record);
129 records_size += record->hdr.service_name_length;
130 if(record->hdr.service_name_length > 0){
131 records_size++; /* + '\0' termination of string */
132 }
133 records_size += record->hdr.user1_ptr_len;
134 records_size += record->hdr.user2_ptr_len;
135 }
136 return records_size;
137}
138
139/* Deep copy all content of in_records into out_records.
140 * out_records must point to a chunk of memory large enough to contain all
141 * the data. Use getSdpRecordsSize() to calculate the needed size. */
142void copy_sdp_records(bluetooth_sdp_record* in_records,
143 bluetooth_sdp_record* out_records, int count) {
144 int i;
145 bluetooth_sdp_record* in_record;
146 bluetooth_sdp_record* out_record;
147 char* free_ptr = (char*)(&out_records[count]); /* set pointer to after the last entry */
148
149 for(i=0; i<count; i++) {
150 in_record = &in_records[i];
151 out_record = &out_records[i];
152 *out_record = *in_record;
153
154 if(in_record->hdr.service_name == NULL || in_record->hdr.service_name_length == 0) {
155 out_record->hdr.service_name = NULL;
156 out_record->hdr.service_name_length = 0;
157 } else {
158 out_record->hdr.service_name = free_ptr; // Update service_name pointer
159 // Copy string
160 memcpy(free_ptr, in_record->hdr.service_name, in_record->hdr.service_name_length);
161 free_ptr += in_record->hdr.service_name_length;
162 *(free_ptr) = '\0'; // Set '\0' termination of string
163 free_ptr++;
164 }
165 if(in_record->hdr.user1_ptr != NULL) {
166 out_record->hdr.user1_ptr = (UINT8*)free_ptr; // Update pointer
167 memcpy(free_ptr, in_record->hdr.user1_ptr, in_record->hdr.user1_ptr_len); // Copy content
168 free_ptr += in_record->hdr.user1_ptr_len;
169 }
170 if(in_record->hdr.user2_ptr != NULL) {
171 out_record->hdr.user2_ptr = (UINT8*)free_ptr; // Update pointer
172 memcpy(free_ptr, in_record->hdr.user2_ptr, in_record->hdr.user2_ptr_len); // Copy content
173 free_ptr += in_record->hdr.user2_ptr_len;
174 }
175 }
176 return;
177}
178
179/* Reserve a slot in sdp_slots, copy data and set a reference to the copy.
180 * The record_data will contain both the record and any data pointed to by
181 * the record.
182 * Currently this covers:
183 * service_name string,
184 * user1_ptr and
185 * user2_ptr. */
186static int alloc_sdp_slot(bluetooth_sdp_record* in_record) {
187 int i;
Kim Schulz8372aa52015-03-25 10:39:40 +0100188 int record_size = get_sdp_records_size(in_record, 1);
Pavlin Radoslavoveae61662015-06-02 13:54:58 -0700189 bluetooth_sdp_record* record = osi_malloc(record_size);
Kim Schulz8372aa52015-03-25 10:39:40 +0100190
191 copy_sdp_records(in_record, record, 1);
192
193 /* We are optimists here, and preallocate the record.
194 * This is to reduce the time we hold the sdp_lock. */
195 pthread_mutex_lock(&sdp_lock);
196 for(i = 0; i < MAX_SDP_SLOTS; i++)
197 {
198 if(sdp_slots[i].state == SDP_RECORD_FREE) {
199 sdp_slots[i].state = SDP_RECORD_ALLOCED;
200 sdp_slots[i].record_data = record;
201 break;
202 }
203 }
204 pthread_mutex_unlock(&sdp_lock);
205 if(i >= MAX_SDP_SLOTS) {
206 APPL_TRACE_ERROR("alloc_sdp_slot failed - no more free slots!");
207 /* Rearly the optimist is too optimistic, and cleanup is needed...*/
Pavlin Radoslavoveae61662015-06-02 13:54:58 -0700208 osi_free(record);
Kim Schulz8372aa52015-03-25 10:39:40 +0100209 return -1;
210 }
211 return i;
212}
213
214static int free_sdp_slot(int id) {
215 int handle = -1;
216 bluetooth_sdp_record* record = NULL;
217 if(id >= MAX_SDP_SLOTS) {
218 APPL_TRACE_ERROR("free_sdp_slot failed - id %d is invalid", id);
219 return handle;
220 }
221 pthread_mutex_lock(&sdp_lock);
222 handle = sdp_slots[id].sdp_handle;
223 sdp_slots[id].sdp_handle = 0;
224 if(sdp_slots[id].state != SDP_RECORD_FREE)
225 {
226 /* safe a copy of the pointer, and free after unlock() */
227 record = sdp_slots[id].record_data;
228 }
229 sdp_slots[id].state = SDP_RECORD_FREE;
230 pthread_mutex_unlock(&sdp_lock);
231
232 if(record != NULL) {
Pavlin Radoslavoveae61662015-06-02 13:54:58 -0700233 osi_free(record);
Kim Schulz8372aa52015-03-25 10:39:40 +0100234 } else {
235 // Record have already been freed
236 handle = -1;
237 }
238 return handle;
239}
240
241/***
242 * Use this to get a reference to a SDP slot AND change the state to
243 * SDP_RECORD_CREATE_INITIATED.
244 */
245static const sdp_slot_t* start_create_sdp(int id) {
246 sdp_slot_t* sdp_slot;
247 if(id >= MAX_SDP_SLOTS) {
248 APPL_TRACE_ERROR("start_create_sdp failed - id %d is invalid", id);
249 return NULL;
250 }
251 pthread_mutex_lock(&sdp_lock);
252 if(sdp_slots[id].state == SDP_RECORD_ALLOCED) {
253 sdp_slot = &(sdp_slots[id]);
254 } else {
255 /* The record have been removed before this event occurred - e.g. deinit */
256 sdp_slot = NULL;
257 }
258 pthread_mutex_unlock(&sdp_lock);
259 if(sdp_slot == NULL) {
260 APPL_TRACE_ERROR("start_create_sdp failed - state for id %d is "
261 "sdp_slots[id].state = %d expected %d",
262 id, sdp_slots[id].state, SDP_RECORD_ALLOCED);
263 }
264 return sdp_slot;
265}
266
267static void set_sdp_handle(int id, int handle) {
268 pthread_mutex_lock(&sdp_lock);
269 sdp_slots[id].sdp_handle = handle;
270 pthread_mutex_unlock(&sdp_lock);
271 BTIF_TRACE_DEBUG("Sdp Server %s id=%d to handle=0x%08x",
272 __FUNCTION__, id, handle);
273
274}
275
276
277bt_status_t create_sdp_record(bluetooth_sdp_record *record, int* record_handle) {
278 int handle;
279
280 handle = alloc_sdp_slot(record);
281 BTIF_TRACE_DEBUG("Sdp Server %s handle = 0x%08x", __FUNCTION__, handle);
282
283 if(handle < 0)
284 return BT_STATUS_FAIL;
285
Arman Uguraybb954522015-06-02 21:11:07 -0700286 BTA_SdpCreateRecordByUser(INT_TO_PTR(handle));
Kim Schulz8372aa52015-03-25 10:39:40 +0100287
288 *record_handle = handle;
289
290 return BT_STATUS_SUCCESS;
291}
292
293bt_status_t remove_sdp_record(int record_id) {
294 int handle;
295
296 /* Get the Record handle, and free the slot */
297 handle = free_sdp_slot(record_id);
298 BTIF_TRACE_DEBUG("Sdp Server %s id=%d to handle=0x%08x",
299 __FUNCTION__, record_id, handle);
300
301 /* Pass the actual record handle */
302 if(handle > 0) {
Arman Uguraybb954522015-06-02 21:11:07 -0700303 BTA_SdpRemoveRecordByUser(INT_TO_PTR(handle));
Kim Schulz8372aa52015-03-25 10:39:40 +0100304 return BT_STATUS_SUCCESS;
305 }
306 BTIF_TRACE_DEBUG("Sdp Server %s - record already removed - or never created", __FUNCTION__);
307 return BT_STATUS_FAIL;
308}
309
310
311/******************************************************************************
312 * CALLBACK FUNCTIONS
313 * Called in BTA context to create/remove SDP records.
314 ******************************************************************************/
315
316void on_create_record_event(int id) {
317 /*
318 * 1) Fetch the record pointer, and change its state?
319 * 2) switch on the type to create the correct record
320 * 3) Update state on completion
321 * 4) What to do at fail?
322 * */
323 BTIF_TRACE_DEBUG("Sdp Server %s", __FUNCTION__);
324 const sdp_slot_t* sdp_slot = start_create_sdp(id);
325 /* In the case we are shutting down, sdp_slot is NULL */
326 if(sdp_slot != NULL) {
327 bluetooth_sdp_record* record = sdp_slot->record_data;
328 int handle = -1;
329 switch(record->hdr.type) {
330 case SDP_TYPE_MAP_MAS:
331 handle = add_maps_sdp(&record->mas);
332 break;
333 case SDP_TYPE_MAP_MNS:
334 handle = add_mapc_sdp(&record->mns);
335 break;
336 case SDP_TYPE_PBAP_PSE:
337 handle = add_pbaps_sdp(&record->pse);
338 break;
339 case SDP_TYPE_OPP_SERVER:
340 handle = add_opps_sdp(&record->ops);
341 break;
342 case SDP_TYPE_PBAP_PCE:
343 // break; not yet supported
344 default:
345 BTIF_TRACE_DEBUG("Record type %d is not supported",record->hdr.type);
346 break;
347 }
348 if(handle != -1) {
349 set_sdp_handle(id, handle);
350 }
351 }
352}
353
354void on_remove_record_event(int handle) {
355 BTIF_TRACE_DEBUG("Sdp Server %s", __FUNCTION__);
356
357 // User data carries the actual SDP handle, not the ID.
358 if(handle != -1 && handle != 0) {
359 BOOLEAN result;
360 result = SDP_DeleteRecord( handle );
361 if(result == FALSE) {
362 BTIF_TRACE_ERROR(" Unable to remove handle 0x%08x", handle);
363 }
364 }
365}
366
367/****
368 * Below the actual functions accessing BTA context data - hence only call from BTA context!
369 */
370
371/* Create a MAP MAS SDP record based on information stored in a bluetooth_sdp_mas_record */
372static int add_maps_sdp(const bluetooth_sdp_mas_record* rec)
373{
374
375 tSDP_PROTOCOL_ELEM protoList [3];
376 UINT16 service = UUID_SERVCLASS_MESSAGE_ACCESS;
377 UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
378 BOOLEAN status = TRUE;
379 UINT32 sdp_handle = 0;
380 UINT8 temp[4];
381 UINT8* p_temp = temp;
382
383 APPL_TRACE_DEBUG("add_mas_sdp: MASID = 0x%02x, scn 0x%02x, psm = 0x%04x\n service name %s",
384 rec->mas_instance_id, rec->hdr.rfcomm_channel_number,
385 rec->hdr.l2cap_psm, rec->hdr.service_name);
386
387 APPL_TRACE_DEBUG(" msg_types: 0x%02x, feature_bits: 0x%08x",
388 rec->supported_message_types, rec->supported_features);
389
390 if ((sdp_handle = SDP_CreateRecord()) == 0)
391 {
392 APPL_TRACE_ERROR("MAPS SDP: Unable to register MAPS Service");
393 return sdp_handle;
394 }
395
396 /* add service class */
397 status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
398 memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
399
400 /* add protocol list, including RFCOMM scn */
401 protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
402 protoList[0].num_params = 0;
403 protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
404 protoList[1].num_params = 1;
405 protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
406 protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
407 protoList[2].num_params = 0;
408 status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
409
410 /* Add a name entry */
411 status &= SDP_AddAttribute(sdp_handle,
412 (UINT16)ATTR_ID_SERVICE_NAME,
413 (UINT8)TEXT_STR_DESC_TYPE,
414 (UINT32)(rec->hdr.service_name_length + 1),
415 (UINT8 *)rec->hdr.service_name);
416
417 /* Add in the Bluetooth Profile Descriptor List */
418 status &= SDP_AddProfileDescriptorList(sdp_handle,
419 UUID_SERVCLASS_MAP_PROFILE,
420 rec->hdr.profile_version);
421
422 /* Add MAS instance ID */
423 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAS_INSTANCE_ID, UINT_DESC_TYPE,
424 (UINT32)1, (UINT8*)&rec->mas_instance_id);
425
426 /* Add supported message types */
427 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_MSG_TYPE, UINT_DESC_TYPE,
428 (UINT32)1, (UINT8*)&rec->supported_message_types);
429
430 /* Add supported feature */
431 UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
432 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES,
433 UINT_DESC_TYPE, (UINT32)4, temp);
434
435 /* Add the L2CAP PSM if present */
436 if(rec->hdr.l2cap_psm != -1) {
437 p_temp = temp;// The macro modifies p_temp, hence rewind.
438 UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
439 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
440 UINT_DESC_TYPE, (UINT32)2, temp);
441 }
442
443 /* Make the service browseable */
444 status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
445
446 if (!status)
447 {
448 SDP_DeleteRecord(sdp_handle);
449 sdp_handle = 0;
450 APPL_TRACE_ERROR("add_maps_sdp FAILED");
451 }
452 else
453 {
454 bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */
455 APPL_TRACE_DEBUG("MAPS: SDP Registered (handle 0x%08x)", sdp_handle);
456 }
457 return sdp_handle;
458}
459
460/* Create a MAP MNS SDP record based on information stored in a bluetooth_sdp_mns_record */
461static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec)
462{
463
464 tSDP_PROTOCOL_ELEM protoList [3];
465 UINT16 service = UUID_SERVCLASS_MESSAGE_NOTIFICATION;
466 UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
467 BOOLEAN status = TRUE;
468 UINT32 sdp_handle = 0;
469 UINT8 temp[4];
470 UINT8* p_temp = temp;
471
472 APPL_TRACE_DEBUG("add_mas_sdp: scn 0x%02x, psm = 0x%04x\n service name %s",
473 rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
474
475 APPL_TRACE_DEBUG(" feature_bits: 0x%08x", rec->supported_features);
476
477 if ((sdp_handle = SDP_CreateRecord()) == 0)
478 {
479 APPL_TRACE_ERROR("add_mapc_sdp: Unable to register MAP Notification Service");
480 return sdp_handle;
481 }
482
483 /* add service class */
484 status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
485 memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
486
487 /* add protocol list, including RFCOMM scn */
488 protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
489 protoList[0].num_params = 0;
490 protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
491 protoList[1].num_params = 1;
492 protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
493 protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
494 protoList[2].num_params = 0;
495 status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
496
497 /* Add a name entry */
498 status &= SDP_AddAttribute(sdp_handle,
499 (UINT16)ATTR_ID_SERVICE_NAME,
500 (UINT8)TEXT_STR_DESC_TYPE,
501 (UINT32)(rec->hdr.service_name_length + 1),
502 (UINT8 *)rec->hdr.service_name);
503
504 /* Add in the Bluetooth Profile Descriptor List */
505 status &= SDP_AddProfileDescriptorList(sdp_handle,
506 UUID_SERVCLASS_MAP_PROFILE,
507 rec->hdr.profile_version);
508
509 /* Add supported feature */
510 UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
511 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES,
512 UINT_DESC_TYPE, (UINT32)4, temp);
513
514 /* Add the L2CAP PSM if present */
515 if(rec->hdr.l2cap_psm != -1) {
516 p_temp = temp;// The macro modifies p_temp, hence rewind.
517 UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
518 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
519 UINT_DESC_TYPE, (UINT32)2, temp);
520 }
521
522 /* Make the service browseable */
523 status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
524
525 if (!status)
526 {
527 SDP_DeleteRecord(sdp_handle);
528 sdp_handle = 0;
529 APPL_TRACE_ERROR("add_mapc_sdp FAILED");
530 }
531 else
532 {
533 bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */
534 APPL_TRACE_DEBUG("MAPC: SDP Registered (handle 0x%08x)", sdp_handle);
535 }
536 return sdp_handle;
537}
538
539/* Create a PBAP Server SDP record based on information stored in a bluetooth_sdp_pse_record */
540static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec)
541{
542
543 tSDP_PROTOCOL_ELEM protoList [3];
544 UINT16 service = UUID_SERVCLASS_PBAP_PSE;
545 UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
546 BOOLEAN status = TRUE;
547 UINT32 sdp_handle = 0;
548 UINT8 temp[4];
549 UINT8* p_temp = temp;
550
551 APPL_TRACE_DEBUG("add_pbaps_sdp: scn 0x%02x, psm = 0x%04x\n service name %s",
552 rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
553
554 APPL_TRACE_DEBUG(" supported_repositories: 0x%08x, feature_bits: 0x%08x",
555 rec->supported_repositories, rec->supported_features);
556
557 if ((sdp_handle = SDP_CreateRecord()) == 0)
558 {
559 APPL_TRACE_ERROR("add_pbaps_sdp: Unable to register PBAP Server Service");
560 return sdp_handle;
561 }
562
563 /* add service class */
564 status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
565 memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
566
567 /* add protocol list, including RFCOMM scn */
568 protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
569 protoList[0].num_params = 0;
570 protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
571 protoList[1].num_params = 1;
572 protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
573 protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
574 protoList[2].num_params = 0;
575 status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
576
577 /* Add a name entry */
578 status &= SDP_AddAttribute(sdp_handle,
579 (UINT16)ATTR_ID_SERVICE_NAME,
580 (UINT8)TEXT_STR_DESC_TYPE,
581 (UINT32)(rec->hdr.service_name_length + 1),
582 (UINT8 *)rec->hdr.service_name);
583
584 /* Add in the Bluetooth Profile Descriptor List */
585 status &= SDP_AddProfileDescriptorList(sdp_handle,
586 UUID_SERVCLASS_PHONE_ACCESS,
587 rec->hdr.profile_version);
588
589 /* Add supported repositories 1 byte */
590 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_REPOSITORIES,
591 UINT_DESC_TYPE, (UINT32)1, (UINT8*)&rec->supported_repositories);
592
593 /* Add supported feature 4 bytes*/
594 UINT32_TO_BE_STREAM(p_temp, rec->supported_features);
595 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_PBAP_SUPPORTED_FEATURES,
596 UINT_DESC_TYPE, (UINT32)4, temp);
597
598 /* Add the L2CAP PSM if present */
599 if(rec->hdr.l2cap_psm != -1) {
600 p_temp = temp;// The macro modifies p_temp, hence rewind.
601 UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
602 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
603 UINT_DESC_TYPE, (UINT32)2, temp);
604 }
605
606 /* Make the service browseable */
607 status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
608
609 if (!status)
610 {
611 SDP_DeleteRecord(sdp_handle);
612 sdp_handle = 0;
613 APPL_TRACE_ERROR("add_pbaps_sdp FAILED");
614 }
615 else
616 {
617 bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */
618 APPL_TRACE_DEBUG("PBAPS: SDP Registered (handle 0x%08x)", sdp_handle);
619 }
620 return sdp_handle;
621}
622
623
624/* Create a OPP Server SDP record based on information stored in a bluetooth_sdp_ops_record */
625static int add_opps_sdp(const bluetooth_sdp_ops_record* rec)
626{
627
628 tSDP_PROTOCOL_ELEM protoList [3];
629 UINT16 service = UUID_SERVCLASS_OBEX_OBJECT_PUSH;
630 UINT16 browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
631 UINT8 type_len[rec->supported_formats_list_len];
632 UINT8 desc_type[rec->supported_formats_list_len];
633 UINT8 *type_value[rec->supported_formats_list_len];
634 BOOLEAN status = TRUE;
635 UINT32 sdp_handle = 0;
636 UINT8 temp[4];
637 UINT8* p_temp = temp;
638 tBTA_UTL_COD cod;
639 int i,j;
640
641 APPL_TRACE_DEBUG("add_opps_sdp: scn 0x%02x, psm = 0x%04x\n service name %s",
642 rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name);
643
644 APPL_TRACE_DEBUG(" supported formats count: %d",
645 rec->supported_formats_list_len);
646
647 if ((sdp_handle = SDP_CreateRecord()) == 0)
648 {
649 APPL_TRACE_ERROR("add_opps_sdp: Unable to register Object Push Server Service");
650 return sdp_handle;
651 }
652
653 /* add service class */
654 status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service);
655 memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) );
656
657 /* add protocol list, including RFCOMM scn */
658 protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
659 protoList[0].num_params = 0;
660 protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
661 protoList[1].num_params = 1;
662 protoList[1].params[0] = rec->hdr.rfcomm_channel_number;
663 protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX;
664 protoList[2].num_params = 0;
665 status &= SDP_AddProtocolList(sdp_handle, 3, protoList);
666
667 /* Add a name entry */
668 status &= SDP_AddAttribute(sdp_handle,
669 (UINT16)ATTR_ID_SERVICE_NAME,
670 (UINT8)TEXT_STR_DESC_TYPE,
671 (UINT32)(rec->hdr.service_name_length + 1),
672 (UINT8 *)rec->hdr.service_name);
673
674 /* Add in the Bluetooth Profile Descriptor List */
675 status &= SDP_AddProfileDescriptorList(sdp_handle,
676 UUID_SERVCLASS_OBEX_OBJECT_PUSH,
677 rec->hdr.profile_version);
678
679 /* add sequence for supported types */
680 for (i = 0, j = 0; i < rec->supported_formats_list_len; i++)
681 {
682 type_value[j] = (UINT8 *) &rec->supported_formats_list[i];
683 desc_type[j] = UINT_DESC_TYPE;
684 type_len[j++] = 1;
685 }
686
687 status &= SDP_AddSequence(sdp_handle, (UINT16) ATTR_ID_SUPPORTED_FORMATS_LIST,
688 (UINT8) rec->supported_formats_list_len, desc_type, type_len, type_value);
689
690 /* Add the L2CAP PSM if present */
691 if(rec->hdr.l2cap_psm != -1) {
692 p_temp = temp;// The macro modifies p_temp, hence rewind.
693 UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm);
694 status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM,
695 UINT_DESC_TYPE, (UINT32)2, temp);
696 }
697
698 /* Make the service browseable */
699 status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
700
701 if (!status)
702 {
703 SDP_DeleteRecord(sdp_handle);
704 sdp_handle = 0;
705 APPL_TRACE_ERROR("add_opps_sdp FAILED");
706 }
707 else
708 {
709 /* set class of device */
710 cod.service = BTM_COD_SERVICE_OBJ_TRANSFER;
711 utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
712
713 bta_sys_add_uuid(service); /* UUID_SERVCLASS_OBEX_OBJECT_PUSH */
714 APPL_TRACE_DEBUG("OPPS: SDP Registered (handle 0x%08x)", sdp_handle);
715 }
716 return sdp_handle;
717}
718
719