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