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