xref: /freebsd/usr.sbin/bluetooth/sdpd/provider.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * provider.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: provider.c,v 1.5 2004/01/13 01:54:39 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/queue.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include "profile.h"
40 #include "provider.h"
41 
42 static TAILQ_HEAD(, provider)	providers = TAILQ_HEAD_INITIALIZER(providers);
43 static uint32_t			change_state = 0;
44 static uint32_t			handle = 0;
45 
46 /*
47  * Register Service Discovery provider.
48  * Should not be called more the once.
49  */
50 
51 int32_t
52 provider_register_sd(int32_t fd)
53 {
54 	extern profile_t	sd_profile_descriptor;
55 	extern profile_t	bgd_profile_descriptor;
56 
57 	provider_p		sd = calloc(1, sizeof(*sd));
58 	provider_p		bgd = calloc(1, sizeof(*bgd));
59 
60 	if (sd == NULL || bgd == NULL) {
61 		if (sd != NULL)
62 			free(sd);
63 
64 		if (bgd != NULL)
65 			free(bgd);
66 
67 		return (-1);
68 	}
69 
70 	sd->profile = &sd_profile_descriptor;
71 	bgd->handle = 0;
72 	sd->fd = fd;
73 	TAILQ_INSERT_HEAD(&providers, sd, provider_next);
74 
75 	bgd->profile = &bgd_profile_descriptor;
76 	bgd->handle = 1;
77 	sd->fd = fd;
78 	TAILQ_INSERT_AFTER(&providers, sd, bgd, provider_next);
79 
80 	change_state ++;
81 
82 	return (0);
83 }
84 
85 /*
86  * Register new provider for a given profile, bdaddr and session.
87  */
88 
89 provider_p
90 provider_register(profile_p const profile, bdaddr_p const bdaddr, int32_t fd,
91 	uint8_t const *data, uint32_t datalen)
92 {
93 	provider_p	provider = calloc(1, sizeof(*provider));
94 
95 	if (provider != NULL) {
96 		provider->data = malloc(datalen);
97 		if (provider->data != NULL) {
98 			provider->profile = profile;
99 			memcpy(provider->data, data, datalen);
100 
101 			/*
102 			 * Record handles 0x0 and 0x1 are reserved
103 			 * for SDP itself
104 			 */
105 
106 			if (++ handle <= 1)
107 				handle = 2;
108 
109 			provider->handle = handle;
110 
111 			memcpy(&provider->bdaddr, bdaddr,
112 				sizeof(provider->bdaddr));
113 			provider->fd = fd;
114 
115 			TAILQ_INSERT_TAIL(&providers, provider, provider_next);
116 			change_state ++;
117 		} else {
118 			free(provider);
119 			provider = NULL;
120 		}
121 	}
122 
123 	return (provider);
124 }
125 
126 /*
127  * Unregister provider
128  */
129 
130 void
131 provider_unregister(provider_p provider)
132 {
133 	TAILQ_REMOVE(&providers, provider, provider_next);
134 	if (provider->data != NULL)
135 		free(provider->data);
136 	free(provider);
137 	change_state ++;
138 }
139 
140 /*
141  * Update provider data
142  */
143 
144 int32_t
145 provider_update(provider_p provider, uint8_t const *data, uint32_t datalen)
146 {
147 	uint8_t	*new_data = (uint8_t *) realloc(provider->data, datalen);
148 
149 	if (new_data == NULL)
150 		return (-1);
151 
152 	memcpy(new_data, data, datalen);
153 	provider->data = new_data;
154 
155 	return (0);
156 }
157 
158 /*
159  * Get a provider for given record handle
160  */
161 
162 provider_p
163 provider_by_handle(uint32_t handle)
164 {
165 	provider_p	provider = NULL;
166 
167 	TAILQ_FOREACH(provider, &providers, provider_next)
168 		if (provider->handle == handle)
169 			break;
170 
171 	return (provider);
172 }
173 
174 /*
175  * Cursor access
176  */
177 
178 provider_p
179 provider_get_first(void)
180 {
181 	return (TAILQ_FIRST(&providers));
182 }
183 
184 provider_p
185 provider_get_next(provider_p provider)
186 {
187 	return (TAILQ_NEXT(provider, provider_next));
188 }
189 
190 /*
191  * Return change state
192  */
193 
194 uint32_t
195 provider_get_change_state(void)
196 {
197 	return (change_state);
198 }
199 
200