xref: /btstack/src/classic/pan.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  pan.h
40  *
41  *  Created by Milanka Ringwald on 10/16/14.
42  */
43 
44 #include "pan.h"
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 #include "btstack_config.h"
50 #include "classic/core.h"
51 #include "classic/sdp_util.h"
52 
53 static const char default_panu_service_name[] = "Personal Ad-hoc User Service";
54 static const char default_panu_service_desc[] = "Personal Ad-hoc User Service";
55 
56 static const char default_nap_service_name[] = "Network Access Point Service";
57 static const char default_nap_service_desc[] = "Personal Ad-hoc Network Service which provides access to a network";
58 
59 static const char default_gn_service_name[] = "Group Ad-hoc Network Service";
60 static const char default_gn_service_desc[] = "Personal Group Ad-hoc Network Service";
61 
62 static void pan_create_service(uint8_t *service, uint32_t service_record_handle,
63 	 uint32_t service_uuid, uint16_t * network_packet_types, const char *name, const char *descriptor,
64 	 security_description_t security_desc, net_access_type_t net_access_type, uint32_t max_net_access_rate,
65 	 const char *IPv4Subnet, const char *IPv6Subnet){
66 
67 	uint8_t* attribute;
68 	de_create_sequence(service);
69 
70 	// 0x0000 "Service Record Handle"
71 	de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
72 	de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
73 
74 	// 0x0001 "Service Class ID List"
75 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
76 	attribute = de_push_sequence(service);
77 	{
78 		//  "UUID for PAN Service"
79 		de_add_number(attribute, DE_UUID, DE_SIZE_32, service_uuid);
80 	}
81 	de_pop_sequence(service, attribute);
82 
83 	// 0x0004 "Protocol Descriptor List"
84 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
85 	attribute = de_push_sequence(service);
86 	{
87 		uint8_t* l2cpProtocol = de_push_sequence(attribute);
88 		{
89 			de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
90 			de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, PSM_BNEP);  // l2cap psm
91 		}
92 		de_pop_sequence(attribute, l2cpProtocol);
93 
94 		uint8_t* bnep = de_push_sequence(attribute);
95 		{
96 			de_add_number(bnep,  DE_UUID, DE_SIZE_16, SDP_BNEPProtocol);
97 			de_add_number(bnep,  DE_UINT, DE_SIZE_16, 0x0100);  // version
98 
99 			uint8_t * net_packet_type_list = de_push_sequence(bnep);
100 			{
101 				if (network_packet_types){
102 					while (*network_packet_types){
103 						de_add_number(net_packet_type_list,  DE_UINT, DE_SIZE_16, *network_packet_types++);
104 					}
105 				}
106 			}
107 			de_pop_sequence(bnep, net_packet_type_list);
108 		}
109 		de_pop_sequence(attribute, bnep);
110 	}
111 	de_pop_sequence(service, attribute);
112 
113 	// 0x0005 "Public Browse Group"
114 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
115 	attribute = de_push_sequence(service);
116 	{
117 		de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
118 	}
119 	de_pop_sequence(service, attribute);
120 
121 	// 0x0006 "LanguageBaseAttributeIDList"
122 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_LanguageBaseAttributeIDList);
123 	attribute = de_push_sequence(service);
124 	{
125 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e);
126 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a);
127 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100);	// Base for Service Name and Service Description
128 	}
129 	de_pop_sequence(service, attribute);
130 
131 	// 0x0008 "Service Availability", optional
132 	// de_add_number(service, DE_UINT, DE_SIZE_16, 0x0008);
133 	// de_add_number(service, DE_UINT, DE_SIZE_8, service_availability);
134 
135 	// 0x0009 "Bluetooth Profile Descriptor List"
136 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
137 	attribute = de_push_sequence(service);
138 	{
139 		uint8_t *sppProfile = de_push_sequence(attribute);
140 		{
141 			de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, service_uuid);
142 			de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0100); // Verision 1.0
143 		}
144 		de_pop_sequence(attribute, sppProfile);
145 	}
146 	de_pop_sequence(service, attribute);
147 
148 	// 0x0100 "Service Name"
149 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
150 	if (name){
151 		de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
152 	} else {
153 		switch (service_uuid){
154 			case SDP_PANU:
155 				de_add_data(service,  DE_STRING, strlen(default_panu_service_name), (uint8_t *) default_panu_service_name);
156 				break;
157 			case SDP_NAP:
158 				de_add_data(service,  DE_STRING, strlen(default_nap_service_name), (uint8_t *) default_nap_service_name);
159 				break;
160 			case SDP_GN:
161 				de_add_data(service,  DE_STRING, strlen(default_gn_service_name), (uint8_t *) default_gn_service_name);
162 				break;
163 			default:
164 				break;
165 		}
166 	}
167 
168 	// 0x0101 "Service Description"
169 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0101);
170 	if (descriptor){
171 		de_add_data(service,  DE_STRING, strlen(descriptor), (uint8_t *) descriptor);
172 	} else {
173 		switch (service_uuid){
174 			case SDP_PANU:
175 				de_add_data(service,  DE_STRING, strlen(default_panu_service_desc), (uint8_t *) default_panu_service_desc);
176 				break;
177 			case SDP_NAP:
178 				de_add_data(service,  DE_STRING, strlen(default_nap_service_desc), (uint8_t *) default_nap_service_desc);
179 				break;
180 			case SDP_GN:
181 				de_add_data(service,  DE_STRING, strlen(default_gn_service_desc), (uint8_t *) default_gn_service_desc);
182 				break;
183 			default:
184 				break;
185 		}
186 	}
187 
188 	// 0x030A "Security Description"
189 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030A);
190 	de_add_number(service, DE_UINT, DE_SIZE_16, security_desc);
191 
192 	if (service_uuid == SDP_PANU) return;
193 
194 	if (IPv4Subnet){
195 		// 0x030D "IPv4Subnet", optional
196 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030D);
197 		de_add_data(service,  DE_STRING, strlen(IPv4Subnet), (uint8_t *) IPv4Subnet);
198 	}
199 
200 	if (IPv6Subnet){
201 		// 0x030E "IPv6Subnet", optional
202 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030E);
203 		de_add_data(service,  DE_STRING, strlen(IPv6Subnet), (uint8_t *) IPv6Subnet);
204 	}
205 
206 	if (service_uuid == SDP_GN) return;
207 
208 	// 0x030B "NetAccessType"
209 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030B);
210 	de_add_number(service, DE_UINT, DE_SIZE_16, net_access_type);
211 
212 	// 0x030C "MaxNetAccessRate"
213 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C);
214 	de_add_number(service, DE_UINT, DE_SIZE_32, max_net_access_rate);
215 
216 }
217 
218 
219 void pan_create_nap_sdp_record(uint8_t *service, uint32_t service_record_handle, uint16_t * network_packet_types, const char *name, const char *description, security_description_t security_desc,
220 	net_access_type_t net_access_type, uint32_t max_net_access_rate, const char *IPv4Subnet, const char *IPv6Subnet){
221 
222 	pan_create_service(service, service_record_handle, SDP_NAP, network_packet_types, name, description, security_desc, net_access_type, max_net_access_rate, IPv4Subnet, IPv6Subnet);
223 }
224 
225 void pan_create_gn_sdp_service(uint8_t *service, uint32_t service_record_handle, uint16_t * network_packet_types, const char *name, const char *description, security_description_t security_desc,
226 	const char *IPv4Subnet, const char *IPv6Subnet){
227 	pan_create_service(service, service_record_handle, SDP_GN, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, IPv4Subnet, IPv6Subnet);
228 }
229 
230 void pan_create_panu_sdp_record(uint8_t *service, uint32_t service_record_handle, uint16_t * network_packet_types, const char *name, const char *description, security_description_t security_desc){
231 	pan_create_service(service, service_record_handle, SDP_PANU, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, NULL, NULL);
232 }
233