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