xref: /btstack/src/classic/pan.c (revision ab2c6ae4b737d5e801d3defe4117331eb244ebb7)
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 #define __BTSTACK_FILE__ "pan.c"
39 
40 /*
41  *  pan.h
42  *
43  *  Created by Milanka Ringwald on 10/16/14.
44  */
45 
46 #include "pan.h"
47 
48 #include <stdio.h>
49 #include <string.h>
50 
51 #include "btstack_config.h"
52 #include "classic/core.h"
53 #include "classic/sdp_util.h"
54 
55 static const char default_panu_service_name[] = "Personal Ad-hoc User Service";
56 static const char default_panu_service_desc[] = "Personal Ad-hoc User Service";
57 
58 static const char default_nap_service_name[] = "Network Access Point Service";
59 static const char default_nap_service_desc[] = "Personal Ad-hoc Network Service which provides access to a network";
60 
61 static const char default_gn_service_name[] = "Group Ad-hoc Network Service";
62 static const char default_gn_service_desc[] = "Personal Group Ad-hoc Network Service";
63 
64 static void pan_create_service(uint8_t *service, uint32_t service_record_handle,
65 	 uint32_t service_uuid, uint16_t * network_packet_types, const char *name, const char *descriptor,
66 	 security_description_t security_desc, net_access_type_t net_access_type, uint32_t max_net_access_rate,
67 	 const char *IPv4Subnet, const char *IPv6Subnet){
68 
69 	uint8_t* attribute;
70 	de_create_sequence(service);
71 
72 	// 0x0000 "Service Record Handle"
73 	de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
74 	de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
75 
76 	// 0x0001 "Service Class ID List"
77 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
78 	attribute = de_push_sequence(service);
79 	{
80 		//  "UUID for PAN Service"
81 		de_add_number(attribute, DE_UUID, DE_SIZE_32, service_uuid);
82 	}
83 	de_pop_sequence(service, attribute);
84 
85 	// 0x0004 "Protocol Descriptor List"
86 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
87 	attribute = de_push_sequence(service);
88 	{
89 		uint8_t* l2cpProtocol = de_push_sequence(attribute);
90 		{
91 			de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
92 			de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, PSM_BNEP);  // l2cap psm
93 		}
94 		de_pop_sequence(attribute, l2cpProtocol);
95 
96 		uint8_t* bnep = de_push_sequence(attribute);
97 		{
98 			de_add_number(bnep,  DE_UUID, DE_SIZE_16, SDP_BNEPProtocol);
99 			de_add_number(bnep,  DE_UINT, DE_SIZE_16, 0x0100);  // version
100 
101 			uint8_t * net_packet_type_list = de_push_sequence(bnep);
102 			{
103 				if (network_packet_types){
104 					while (*network_packet_types){
105 						de_add_number(net_packet_type_list,  DE_UINT, DE_SIZE_16, *network_packet_types++);
106 					}
107 				}
108 			}
109 			de_pop_sequence(bnep, net_packet_type_list);
110 		}
111 		de_pop_sequence(attribute, bnep);
112 	}
113 	de_pop_sequence(service, attribute);
114 
115 	// 0x0005 "Public Browse Group"
116 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
117 	attribute = de_push_sequence(service);
118 	{
119 		de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
120 	}
121 	de_pop_sequence(service, attribute);
122 
123 	// 0x0006 "LanguageBaseAttributeIDList"
124 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_LanguageBaseAttributeIDList);
125 	attribute = de_push_sequence(service);
126 	{
127 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e);
128 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a);
129 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100);	// Base for Service Name and Service Description
130 	}
131 	de_pop_sequence(service, attribute);
132 
133 	// 0x0008 "Service Availability", optional
134 	// de_add_number(service, DE_UINT, DE_SIZE_16, 0x0008);
135 	// de_add_number(service, DE_UINT, DE_SIZE_8, service_availability);
136 
137 	// 0x0009 "Bluetooth Profile Descriptor List"
138 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
139 	attribute = de_push_sequence(service);
140 	{
141 		uint8_t *sppProfile = de_push_sequence(attribute);
142 		{
143 			de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, service_uuid);
144 			de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0100); // Verision 1.0
145 		}
146 		de_pop_sequence(attribute, sppProfile);
147 	}
148 	de_pop_sequence(service, attribute);
149 
150 	// 0x0100 "Service Name"
151 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
152 	if (name){
153 		de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
154 	} else {
155 		switch (service_uuid){
156 			case SDP_PANU:
157 				de_add_data(service,  DE_STRING, strlen(default_panu_service_name), (uint8_t *) default_panu_service_name);
158 				break;
159 			case SDP_NAP:
160 				de_add_data(service,  DE_STRING, strlen(default_nap_service_name), (uint8_t *) default_nap_service_name);
161 				break;
162 			case SDP_GN:
163 				de_add_data(service,  DE_STRING, strlen(default_gn_service_name), (uint8_t *) default_gn_service_name);
164 				break;
165 			default:
166 				break;
167 		}
168 	}
169 
170 	// 0x0101 "Service Description"
171 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0101);
172 	if (descriptor){
173 		de_add_data(service,  DE_STRING, strlen(descriptor), (uint8_t *) descriptor);
174 	} else {
175 		switch (service_uuid){
176 			case SDP_PANU:
177 				de_add_data(service,  DE_STRING, strlen(default_panu_service_desc), (uint8_t *) default_panu_service_desc);
178 				break;
179 			case SDP_NAP:
180 				de_add_data(service,  DE_STRING, strlen(default_nap_service_desc), (uint8_t *) default_nap_service_desc);
181 				break;
182 			case SDP_GN:
183 				de_add_data(service,  DE_STRING, strlen(default_gn_service_desc), (uint8_t *) default_gn_service_desc);
184 				break;
185 			default:
186 				break;
187 		}
188 	}
189 
190 	// 0x030A "Security Description"
191 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030A);
192 	de_add_number(service, DE_UINT, DE_SIZE_16, security_desc);
193 
194 	if (service_uuid == SDP_PANU) return;
195 
196 	if (IPv4Subnet){
197 		// 0x030D "IPv4Subnet", optional
198 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030D);
199 		de_add_data(service,  DE_STRING, strlen(IPv4Subnet), (uint8_t *) IPv4Subnet);
200 	}
201 
202 	if (IPv6Subnet){
203 		// 0x030E "IPv6Subnet", optional
204 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030E);
205 		de_add_data(service,  DE_STRING, strlen(IPv6Subnet), (uint8_t *) IPv6Subnet);
206 	}
207 
208 	if (service_uuid == SDP_GN) return;
209 
210 	// 0x030B "NetAccessType"
211 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030B);
212 	de_add_number(service, DE_UINT, DE_SIZE_16, net_access_type);
213 
214 	// 0x030C "MaxNetAccessRate"
215 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C);
216 	de_add_number(service, DE_UINT, DE_SIZE_32, max_net_access_rate);
217 
218 }
219 
220 
221 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,
222 	net_access_type_t net_access_type, uint32_t max_net_access_rate, const char *IPv4Subnet, const char *IPv6Subnet){
223 
224 	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);
225 }
226 
227 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,
228 	const char *IPv4Subnet, const char *IPv6Subnet){
229 	pan_create_service(service, service_record_handle, SDP_GN, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, IPv4Subnet, IPv6Subnet);
230 }
231 
232 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){
233 	pan_create_service(service, service_record_handle, SDP_PANU, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, NULL, NULL);
234 }
235