xref: /btstack/src/classic/pan.c (revision 9b1c3b4d7d0a803905c88f4526948289db492cfd)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald /*
393deb3ec6SMatthias Ringwald  *  pan.h
403deb3ec6SMatthias Ringwald  *
413deb3ec6SMatthias Ringwald  *  Created by Milanka Ringwald on 10/16/14.
423deb3ec6SMatthias Ringwald  */
433deb3ec6SMatthias Ringwald 
443deb3ec6SMatthias Ringwald #include "pan.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdio.h>
473deb3ec6SMatthias Ringwald #include <string.h>
483deb3ec6SMatthias Ringwald 
4966b1fcb3SMatthias Ringwald #include "btstack-config.h"
5066b1fcb3SMatthias Ringwald 
513edc84c5SMatthias Ringwald #include "classic/sdp_util.h"
523deb3ec6SMatthias Ringwald #include "hci_cmds.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald static const char default_panu_service_name[] = "Personal Ad-hoc User Service";
553deb3ec6SMatthias Ringwald static const char default_panu_service_desc[] = "Personal Ad-hoc User Service";
563deb3ec6SMatthias Ringwald 
573deb3ec6SMatthias Ringwald static const char default_nap_service_name[] = "Network Access Point Service";
583deb3ec6SMatthias Ringwald static const char default_nap_service_desc[] = "Personal Ad-hoc Network Service which provides access to a network";
593deb3ec6SMatthias Ringwald 
603deb3ec6SMatthias Ringwald static const char default_gn_service_name[] = "Group Ad-hoc Network Service";
613deb3ec6SMatthias Ringwald static const char default_gn_service_desc[] = "Personal Group Ad-hoc Network Service";
623deb3ec6SMatthias Ringwald 
63106e8b67SMatthias Ringwald static void pan_create_service(uint8_t *service, uint32_t service_record_handle,
64106e8b67SMatthias Ringwald 	 uint32_t service_uuid, uint16_t * network_packet_types, const char *name, const char *descriptor,
653deb3ec6SMatthias Ringwald 	 security_description_t security_desc, net_access_type_t net_access_type, uint32_t max_net_access_rate,
663deb3ec6SMatthias Ringwald 	 const char *IPv4Subnet, const char *IPv6Subnet){
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald 	uint8_t* attribute;
693deb3ec6SMatthias Ringwald 	de_create_sequence(service);
703deb3ec6SMatthias Ringwald 
713deb3ec6SMatthias Ringwald 	// 0x0000 "Service Record Handle"
723deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
73*9b1c3b4dSMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
743deb3ec6SMatthias Ringwald 
753deb3ec6SMatthias Ringwald 	// 0x0001 "Service Class ID List"
763deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
773deb3ec6SMatthias Ringwald 	attribute = de_push_sequence(service);
783deb3ec6SMatthias Ringwald 	{
793deb3ec6SMatthias Ringwald 		//  "UUID for PAN Service"
803deb3ec6SMatthias Ringwald 		de_add_number(attribute, DE_UUID, DE_SIZE_32, service_uuid);
813deb3ec6SMatthias Ringwald 	}
823deb3ec6SMatthias Ringwald 	de_pop_sequence(service, attribute);
833deb3ec6SMatthias Ringwald 
843deb3ec6SMatthias Ringwald 	// 0x0004 "Protocol Descriptor List"
853deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
863deb3ec6SMatthias Ringwald 	attribute = de_push_sequence(service);
873deb3ec6SMatthias Ringwald 	{
883deb3ec6SMatthias Ringwald 		uint8_t* l2cpProtocol = de_push_sequence(attribute);
893deb3ec6SMatthias Ringwald 		{
903deb3ec6SMatthias Ringwald 			de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
913deb3ec6SMatthias Ringwald 			de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, PSM_BNEP);  // l2cap psm
923deb3ec6SMatthias Ringwald 		}
933deb3ec6SMatthias Ringwald 		de_pop_sequence(attribute, l2cpProtocol);
943deb3ec6SMatthias Ringwald 
953deb3ec6SMatthias Ringwald 		uint8_t* bnep = de_push_sequence(attribute);
963deb3ec6SMatthias Ringwald 		{
973deb3ec6SMatthias Ringwald 			de_add_number(bnep,  DE_UUID, DE_SIZE_16, SDP_BNEPProtocol);
983deb3ec6SMatthias Ringwald 			de_add_number(bnep,  DE_UINT, DE_SIZE_16, 0x0100);  // version
993deb3ec6SMatthias Ringwald 
1003deb3ec6SMatthias Ringwald 			uint8_t * net_packet_type_list = de_push_sequence(bnep);
1013deb3ec6SMatthias Ringwald 			{
1023deb3ec6SMatthias Ringwald 				if (network_packet_types){
1033deb3ec6SMatthias Ringwald 					while (*network_packet_types){
1043deb3ec6SMatthias Ringwald 						de_add_number(net_packet_type_list,  DE_UINT, DE_SIZE_16, *network_packet_types++);
1053deb3ec6SMatthias Ringwald 					}
1063deb3ec6SMatthias Ringwald 				}
1073deb3ec6SMatthias Ringwald 			}
1083deb3ec6SMatthias Ringwald 			de_pop_sequence(bnep, net_packet_type_list);
1093deb3ec6SMatthias Ringwald 		}
1103deb3ec6SMatthias Ringwald 		de_pop_sequence(attribute, bnep);
1113deb3ec6SMatthias Ringwald 	}
1123deb3ec6SMatthias Ringwald 	de_pop_sequence(service, attribute);
1133deb3ec6SMatthias Ringwald 
1143deb3ec6SMatthias Ringwald 	// 0x0005 "Public Browse Group"
1153deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
1163deb3ec6SMatthias Ringwald 	attribute = de_push_sequence(service);
1173deb3ec6SMatthias Ringwald 	{
1183deb3ec6SMatthias Ringwald 		de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
1193deb3ec6SMatthias Ringwald 	}
1203deb3ec6SMatthias Ringwald 	de_pop_sequence(service, attribute);
1213deb3ec6SMatthias Ringwald 
1223deb3ec6SMatthias Ringwald 	// 0x0006 "LanguageBaseAttributeIDList"
1233deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_LanguageBaseAttributeIDList);
1243deb3ec6SMatthias Ringwald 	attribute = de_push_sequence(service);
1253deb3ec6SMatthias Ringwald 	{
1263deb3ec6SMatthias Ringwald 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e);
1273deb3ec6SMatthias Ringwald 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a);
1283deb3ec6SMatthias Ringwald 		de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100);	// Base for Service Name and Service Description
1293deb3ec6SMatthias Ringwald 	}
1303deb3ec6SMatthias Ringwald 	de_pop_sequence(service, attribute);
1313deb3ec6SMatthias Ringwald 
1323deb3ec6SMatthias Ringwald 	// 0x0008 "Service Availability", optional
1333deb3ec6SMatthias Ringwald 	// de_add_number(service, DE_UINT, DE_SIZE_16, 0x0008);
1343deb3ec6SMatthias Ringwald 	// de_add_number(service, DE_UINT, DE_SIZE_8, service_availability);
1353deb3ec6SMatthias Ringwald 
1363deb3ec6SMatthias Ringwald 	// 0x0009 "Bluetooth Profile Descriptor List"
1373deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
1383deb3ec6SMatthias Ringwald 	attribute = de_push_sequence(service);
1393deb3ec6SMatthias Ringwald 	{
1403deb3ec6SMatthias Ringwald 		uint8_t *sppProfile = de_push_sequence(attribute);
1413deb3ec6SMatthias Ringwald 		{
1423deb3ec6SMatthias Ringwald 			de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, service_uuid);
1433deb3ec6SMatthias Ringwald 			de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0100); // Verision 1.0
1443deb3ec6SMatthias Ringwald 		}
1453deb3ec6SMatthias Ringwald 		de_pop_sequence(attribute, sppProfile);
1463deb3ec6SMatthias Ringwald 	}
1473deb3ec6SMatthias Ringwald 	de_pop_sequence(service, attribute);
1483deb3ec6SMatthias Ringwald 
1493deb3ec6SMatthias Ringwald 	// 0x0100 "Service Name"
1503deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
1513deb3ec6SMatthias Ringwald 	if (name){
1523deb3ec6SMatthias Ringwald 		de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
1533deb3ec6SMatthias Ringwald 	} else {
1543deb3ec6SMatthias Ringwald 		switch (service_uuid){
1553deb3ec6SMatthias Ringwald 			case SDP_PANU:
1563deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_panu_service_name), (uint8_t *) default_panu_service_name);
1573deb3ec6SMatthias Ringwald 				break;
1583deb3ec6SMatthias Ringwald 			case SDP_NAP:
1593deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_nap_service_name), (uint8_t *) default_nap_service_name);
1603deb3ec6SMatthias Ringwald 				break;
1613deb3ec6SMatthias Ringwald 			case SDP_GN:
1623deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_gn_service_name), (uint8_t *) default_gn_service_name);
1633deb3ec6SMatthias Ringwald 				break;
1643deb3ec6SMatthias Ringwald 			default:
1653deb3ec6SMatthias Ringwald 				break;
1663deb3ec6SMatthias Ringwald 		}
1673deb3ec6SMatthias Ringwald 	}
1683deb3ec6SMatthias Ringwald 
1693deb3ec6SMatthias Ringwald 	// 0x0101 "Service Description"
1703deb3ec6SMatthias Ringwald 	de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0101);
1713deb3ec6SMatthias Ringwald 	if (descriptor){
1723deb3ec6SMatthias Ringwald 		de_add_data(service,  DE_STRING, strlen(descriptor), (uint8_t *) descriptor);
1733deb3ec6SMatthias Ringwald 	} else {
1743deb3ec6SMatthias Ringwald 		switch (service_uuid){
1753deb3ec6SMatthias Ringwald 			case SDP_PANU:
1763deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_panu_service_desc), (uint8_t *) default_panu_service_desc);
1773deb3ec6SMatthias Ringwald 				break;
1783deb3ec6SMatthias Ringwald 			case SDP_NAP:
1793deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_nap_service_desc), (uint8_t *) default_nap_service_desc);
1803deb3ec6SMatthias Ringwald 				break;
1813deb3ec6SMatthias Ringwald 			case SDP_GN:
1823deb3ec6SMatthias Ringwald 				de_add_data(service,  DE_STRING, strlen(default_gn_service_desc), (uint8_t *) default_gn_service_desc);
1833deb3ec6SMatthias Ringwald 				break;
1843deb3ec6SMatthias Ringwald 			default:
1853deb3ec6SMatthias Ringwald 				break;
1863deb3ec6SMatthias Ringwald 		}
1873deb3ec6SMatthias Ringwald 	}
1883deb3ec6SMatthias Ringwald 
1893deb3ec6SMatthias Ringwald 	// 0x030A "Security Description"
1903deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030A);
1913deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, security_desc);
1923deb3ec6SMatthias Ringwald 
1933deb3ec6SMatthias Ringwald 	if (service_uuid == SDP_PANU) return;
1943deb3ec6SMatthias Ringwald 
1953deb3ec6SMatthias Ringwald 	if (IPv4Subnet){
1963deb3ec6SMatthias Ringwald 		// 0x030D "IPv4Subnet", optional
1973deb3ec6SMatthias Ringwald 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030D);
1983deb3ec6SMatthias Ringwald 		de_add_data(service,  DE_STRING, strlen(IPv4Subnet), (uint8_t *) IPv4Subnet);
1993deb3ec6SMatthias Ringwald 	}
2003deb3ec6SMatthias Ringwald 
2013deb3ec6SMatthias Ringwald 	if (IPv6Subnet){
2023deb3ec6SMatthias Ringwald 		// 0x030E "IPv6Subnet", optional
2033deb3ec6SMatthias Ringwald 		de_add_number(service,  DE_UINT, DE_SIZE_16, 0x030E);
2043deb3ec6SMatthias Ringwald 		de_add_data(service,  DE_STRING, strlen(IPv6Subnet), (uint8_t *) IPv6Subnet);
2053deb3ec6SMatthias Ringwald 	}
2063deb3ec6SMatthias Ringwald 
2073deb3ec6SMatthias Ringwald 	if (service_uuid == SDP_GN) return;
2083deb3ec6SMatthias Ringwald 
2093deb3ec6SMatthias Ringwald 	// 0x030B "NetAccessType"
2103deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030B);
2113deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, net_access_type);
2123deb3ec6SMatthias Ringwald 
2133deb3ec6SMatthias Ringwald 	// 0x030C "MaxNetAccessRate"
2143deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C);
2153deb3ec6SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_32, max_net_access_rate);
2163deb3ec6SMatthias Ringwald 
2173deb3ec6SMatthias Ringwald }
2183deb3ec6SMatthias Ringwald 
2193deb3ec6SMatthias Ringwald 
220106e8b67SMatthias Ringwald 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,
2213deb3ec6SMatthias Ringwald 	net_access_type_t net_access_type, uint32_t max_net_access_rate, const char *IPv4Subnet, const char *IPv6Subnet){
2223deb3ec6SMatthias Ringwald 
223106e8b67SMatthias Ringwald 	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);
2243deb3ec6SMatthias Ringwald }
2253deb3ec6SMatthias Ringwald 
226106e8b67SMatthias Ringwald 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,
2273deb3ec6SMatthias Ringwald 	const char *IPv4Subnet, const char *IPv6Subnet){
228106e8b67SMatthias Ringwald 	pan_create_service(service, service_record_handle, SDP_GN, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, IPv4Subnet, IPv6Subnet);
2293deb3ec6SMatthias Ringwald }
2303deb3ec6SMatthias Ringwald 
231106e8b67SMatthias Ringwald 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){
232106e8b67SMatthias Ringwald 	pan_create_service(service, service_record_handle, SDP_PANU, network_packet_types, name, description, security_desc, PAN_NET_ACCESS_TYPE_NONE, 0, NULL, NULL);
2333deb3ec6SMatthias Ringwald }
234