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