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 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "pan.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald /* 413deb3ec6SMatthias Ringwald * pan.h 423deb3ec6SMatthias Ringwald * 433deb3ec6SMatthias Ringwald * Created by Milanka Ringwald on 10/16/14. 443deb3ec6SMatthias Ringwald */ 453deb3ec6SMatthias Ringwald 463deb3ec6SMatthias Ringwald #include "pan.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdio.h> 493deb3ec6SMatthias Ringwald #include <string.h> 503deb3ec6SMatthias Ringwald 51235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 527907f069SMatthias Ringwald #include "btstack_config.h" 5359c6af15SMatthias Ringwald #include "classic/core.h" 543edc84c5SMatthias Ringwald #include "classic/sdp_util.h" 553deb3ec6SMatthias Ringwald 563deb3ec6SMatthias Ringwald static const char default_panu_service_name[] = "Personal Ad-hoc User Service"; 573deb3ec6SMatthias Ringwald static const char default_panu_service_desc[] = "Personal Ad-hoc User Service"; 583deb3ec6SMatthias Ringwald 593deb3ec6SMatthias Ringwald static const char default_nap_service_name[] = "Network Access Point Service"; 603deb3ec6SMatthias Ringwald static const char default_nap_service_desc[] = "Personal Ad-hoc Network Service which provides access to a network"; 613deb3ec6SMatthias Ringwald 623deb3ec6SMatthias Ringwald static const char default_gn_service_name[] = "Group Ad-hoc Network Service"; 633deb3ec6SMatthias Ringwald static const char default_gn_service_desc[] = "Personal Group Ad-hoc Network Service"; 643deb3ec6SMatthias Ringwald 65106e8b67SMatthias Ringwald static void pan_create_service(uint8_t *service, uint32_t service_record_handle, 66106e8b67SMatthias Ringwald uint32_t service_uuid, uint16_t * network_packet_types, const char *name, const char *descriptor, 673deb3ec6SMatthias Ringwald security_description_t security_desc, net_access_type_t net_access_type, uint32_t max_net_access_rate, 683deb3ec6SMatthias Ringwald const char *IPv4Subnet, const char *IPv6Subnet){ 693deb3ec6SMatthias Ringwald 703deb3ec6SMatthias Ringwald uint8_t* attribute; 713deb3ec6SMatthias Ringwald de_create_sequence(service); 723deb3ec6SMatthias Ringwald 733deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 74235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 759b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 763deb3ec6SMatthias Ringwald 773deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 78235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 793deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 803deb3ec6SMatthias Ringwald { 813deb3ec6SMatthias Ringwald // "UUID for PAN Service" 823deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_32, service_uuid); 833deb3ec6SMatthias Ringwald } 843deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 853deb3ec6SMatthias Ringwald 863deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 87235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 883deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 893deb3ec6SMatthias Ringwald { 903deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 913deb3ec6SMatthias Ringwald { 92235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 933deb3ec6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_BNEP); // l2cap psm 943deb3ec6SMatthias Ringwald } 953deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 963deb3ec6SMatthias Ringwald 973deb3ec6SMatthias Ringwald uint8_t* bnep = de_push_sequence(attribute); 983deb3ec6SMatthias Ringwald { 99235946f1SMatthias Ringwald de_add_number(bnep, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_BNEP); 1003deb3ec6SMatthias Ringwald de_add_number(bnep, DE_UINT, DE_SIZE_16, 0x0100); // version 1013deb3ec6SMatthias Ringwald 1023deb3ec6SMatthias Ringwald uint8_t * net_packet_type_list = de_push_sequence(bnep); 1033deb3ec6SMatthias Ringwald { 1043deb3ec6SMatthias Ringwald if (network_packet_types){ 1053deb3ec6SMatthias Ringwald while (*network_packet_types){ 1063deb3ec6SMatthias Ringwald de_add_number(net_packet_type_list, DE_UINT, DE_SIZE_16, *network_packet_types++); 1073deb3ec6SMatthias Ringwald } 1083deb3ec6SMatthias Ringwald } 1093deb3ec6SMatthias Ringwald } 1103deb3ec6SMatthias Ringwald de_pop_sequence(bnep, net_packet_type_list); 1113deb3ec6SMatthias Ringwald } 1123deb3ec6SMatthias Ringwald de_pop_sequence(attribute, bnep); 1133deb3ec6SMatthias Ringwald } 1143deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 1153deb3ec6SMatthias Ringwald 1163deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 117235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 1183deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 1193deb3ec6SMatthias Ringwald { 120235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 1213deb3ec6SMatthias Ringwald } 1223deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 1233deb3ec6SMatthias Ringwald 1243deb3ec6SMatthias Ringwald // 0x0006 "LanguageBaseAttributeIDList" 125235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST); 1263deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 1273deb3ec6SMatthias Ringwald { 1283deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); 1293deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); 1303deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); // Base for Service Name and Service Description 1313deb3ec6SMatthias Ringwald } 1323deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 1333deb3ec6SMatthias Ringwald 1343deb3ec6SMatthias Ringwald // 0x0008 "Service Availability", optional 1353deb3ec6SMatthias Ringwald // de_add_number(service, DE_UINT, DE_SIZE_16, 0x0008); 1363deb3ec6SMatthias Ringwald // de_add_number(service, DE_UINT, DE_SIZE_8, service_availability); 1373deb3ec6SMatthias Ringwald 1383deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 139235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 1403deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 1413deb3ec6SMatthias Ringwald { 1423deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 1433deb3ec6SMatthias Ringwald { 1443deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, service_uuid); 1453deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0100); // Verision 1.0 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 1483deb3ec6SMatthias Ringwald } 1493deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 1503deb3ec6SMatthias Ringwald 1513deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 1523deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 1533deb3ec6SMatthias Ringwald if (name){ 1543deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 1553deb3ec6SMatthias Ringwald } else { 1563deb3ec6SMatthias Ringwald switch (service_uuid){ 157235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_PANU: 1583deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_panu_service_name), (uint8_t *) default_panu_service_name); 1593deb3ec6SMatthias Ringwald break; 160235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_NAP: 1613deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_nap_service_name), (uint8_t *) default_nap_service_name); 1623deb3ec6SMatthias Ringwald break; 163235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_GN: 1643deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_gn_service_name), (uint8_t *) default_gn_service_name); 1653deb3ec6SMatthias Ringwald break; 1663deb3ec6SMatthias Ringwald default: 1673deb3ec6SMatthias Ringwald break; 1683deb3ec6SMatthias Ringwald } 1693deb3ec6SMatthias Ringwald } 1703deb3ec6SMatthias Ringwald 1713deb3ec6SMatthias Ringwald // 0x0101 "Service Description" 1723deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0101); 1733deb3ec6SMatthias Ringwald if (descriptor){ 1743deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(descriptor), (uint8_t *) descriptor); 1753deb3ec6SMatthias Ringwald } else { 1763deb3ec6SMatthias Ringwald switch (service_uuid){ 177235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_PANU: 1783deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_panu_service_desc), (uint8_t *) default_panu_service_desc); 1793deb3ec6SMatthias Ringwald break; 180235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_NAP: 1813deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_nap_service_desc), (uint8_t *) default_nap_service_desc); 1823deb3ec6SMatthias Ringwald break; 183235946f1SMatthias Ringwald case BLUETOOTH_SERVICE_CLASS_GN: 1843deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(default_gn_service_desc), (uint8_t *) default_gn_service_desc); 1853deb3ec6SMatthias Ringwald break; 1863deb3ec6SMatthias Ringwald default: 1873deb3ec6SMatthias Ringwald break; 1883deb3ec6SMatthias Ringwald } 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 1913deb3ec6SMatthias Ringwald // 0x030A "Security Description" 1923deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x030A); 1933deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, security_desc); 1943deb3ec6SMatthias Ringwald 195235946f1SMatthias Ringwald if (service_uuid == BLUETOOTH_SERVICE_CLASS_PANU) return; 1963deb3ec6SMatthias Ringwald 1973deb3ec6SMatthias Ringwald if (IPv4Subnet){ 1983deb3ec6SMatthias Ringwald // 0x030D "IPv4Subnet", optional 1993deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x030D); 2003deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(IPv4Subnet), (uint8_t *) IPv4Subnet); 2013deb3ec6SMatthias Ringwald } 2023deb3ec6SMatthias Ringwald 2033deb3ec6SMatthias Ringwald if (IPv6Subnet){ 2043deb3ec6SMatthias Ringwald // 0x030E "IPv6Subnet", optional 2053deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x030E); 2063deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(IPv6Subnet), (uint8_t *) IPv6Subnet); 2073deb3ec6SMatthias Ringwald } 2083deb3ec6SMatthias Ringwald 209235946f1SMatthias Ringwald if (service_uuid == BLUETOOTH_SERVICE_CLASS_GN) return; 2103deb3ec6SMatthias Ringwald 2113deb3ec6SMatthias Ringwald // 0x030B "NetAccessType" 2123deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x030B); 2133deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, net_access_type); 2143deb3ec6SMatthias Ringwald 2153deb3ec6SMatthias Ringwald // 0x030C "MaxNetAccessRate" 2163deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C); 2173deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, max_net_access_rate); 2183deb3ec6SMatthias Ringwald 2193deb3ec6SMatthias Ringwald } 2203deb3ec6SMatthias Ringwald 2213deb3ec6SMatthias Ringwald 222a3bf6c7bSMatthias Ringwald 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, 2233deb3ec6SMatthias Ringwald net_access_type_t net_access_type, uint32_t max_net_access_rate, const char *IPv4Subnet, const char *IPv6Subnet){ 2243deb3ec6SMatthias Ringwald 225235946f1SMatthias Ringwald 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); 2263deb3ec6SMatthias Ringwald } 2273deb3ec6SMatthias Ringwald 228*0b91a77fSMatthias Ringwald 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, 2293deb3ec6SMatthias Ringwald const char *IPv4Subnet, const char *IPv6Subnet){ 230235946f1SMatthias Ringwald 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); 2313deb3ec6SMatthias Ringwald } 2323deb3ec6SMatthias Ringwald 233a3bf6c7bSMatthias Ringwald 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){ 234235946f1SMatthias Ringwald 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); 2353deb3ec6SMatthias Ringwald } 236