3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2001-2002 Nokia Corporation
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
8 * Copyright (C) 2002-2003 Stephen Crane <steve.crane@rococosoft.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "lib/bluetooth.h"
36 #include "lib/sdp_lib.h"
41 static sdp_list_t *service_db;
42 static sdp_list_t *access_db;
50 * Ordering function called when inserting a service record.
51 * The service repository is a linked list in sorted order
52 * and the service record handle is the sort key
54 int record_sort(const void *r1, const void *r2)
56 const sdp_record_t *rec1 = r1;
57 const sdp_record_t *rec2 = r2;
60 error("NULL RECORD LIST FATAL");
64 return rec1->handle - rec2->handle;
67 static int access_sort(const void *r1, const void *r2)
69 const sdp_access_t *rec1 = r1;
70 const sdp_access_t *rec2 = r2;
73 error("NULL RECORD LIST FATAL");
77 return rec1->handle - rec2->handle;
80 static void access_free(void *p)
86 * Reset the service repository by deleting its contents
88 void sdp_svcdb_reset(void)
90 sdp_list_free(service_db, (sdp_free_func_t) sdp_record_free);
93 sdp_list_free(access_db, access_free);
97 typedef struct _indexed {
102 static sdp_list_t *socket_index;
105 * collect all services registered over this socket
107 void sdp_svcdb_collect_all(int sock)
111 for (p = socket_index, q = 0; p; ) {
112 sdp_indexed_t *item = p->data;
113 if (item->sock == sock) {
114 sdp_list_t *next = p->next;
115 sdp_record_remove(item->record->handle);
116 sdp_record_free(item->record);
124 } else if (item->sock > sock)
133 void sdp_svcdb_collect(sdp_record_t *rec)
137 for (p = socket_index, q = 0; p; q = p, p = p->next) {
138 sdp_indexed_t *item = p->data;
139 if (rec == item->record) {
144 socket_index = p->next;
151 static int compare_indices(const void *i1, const void *i2)
153 const sdp_indexed_t *s1 = i1;
154 const sdp_indexed_t *s2 = i2;
155 return s1->sock - s2->sock;
158 void sdp_svcdb_set_collectable(sdp_record_t *record, int sock)
160 sdp_indexed_t *item = malloc(sizeof(sdp_indexed_t));
168 item->record = record;
169 socket_index = sdp_list_insert_sorted(socket_index, item, compare_indices);
173 * Add a service record to the repository
175 void sdp_record_add(const bdaddr_t *device, sdp_record_t *rec)
179 SDPDBG("Adding rec : 0x%lx", (long) rec);
180 SDPDBG("with handle : 0x%x", rec->handle);
182 service_db = sdp_list_insert_sorted(service_db, rec, record_sort);
184 dev = malloc(sizeof(*dev));
188 bacpy(&dev->device, device);
189 dev->handle = rec->handle;
191 access_db = sdp_list_insert_sorted(access_db, dev, access_sort);
194 static sdp_list_t *record_locate(uint32_t handle)
201 p = sdp_list_find(service_db, &r, record_sort);
205 SDPDBG("Could not find svcRec for : 0x%x", handle);
209 static sdp_list_t *access_locate(uint32_t handle)
216 p = sdp_list_find(access_db, &a, access_sort);
220 SDPDBG("Could not find access data for : 0x%x", handle);
225 * Given a service record handle, find the record associated with it.
227 sdp_record_t *sdp_record_find(uint32_t handle)
229 sdp_list_t *p = record_locate(handle);
232 SDPDBG("Couldn't find record for : 0x%x", handle);
240 * Given a service record handle, remove its record from the repository
242 int sdp_record_remove(uint32_t handle)
244 sdp_list_t *p = record_locate(handle);
249 error("Remove : Couldn't find record for : 0x%x", handle);
255 service_db = sdp_list_remove(service_db, r);
257 p = access_locate(handle);
258 if (p == NULL || p->data == NULL)
263 access_db = sdp_list_remove(access_db, a);
270 * Return a pointer to the linked list containing the records in sorted order
272 sdp_list_t *sdp_get_record_list(void)
277 int sdp_check_access(uint32_t handle, bdaddr_t *device)
279 sdp_list_t *p = access_locate(handle);
289 if (bacmp(&a->device, device) &&
290 bacmp(&a->device, BDADDR_ANY) &&
291 bacmp(device, BDADDR_ANY))
297 uint32_t sdp_next_handle(void)
299 uint32_t handle = 0x10000;
301 while (sdp_record_find(handle))