xref: /btstack/tool/bluetooth_sdp.py (revision b816bb66c86a1464b68e38f5fce423423de28762)
1#!/usr/bin/env python
2#
3# Scrape SDP UUIDs from Bluetooth SIG page
4# Copyright 2017 BlueKitchen GmbH
5#
6
7from lxml import html
8import datetime
9import requests
10import sys
11import os
12import codecs
13import re
14
15program_info = '''
16BTstack SDP UUID Scraper for BTstack
17Copyright 2017, BlueKitchen GmbH
18'''
19
20header = '''/**
21 * bluetooth_sdp.h generated from Bluetooth SIG website for BTstack by tool/bluetooth_sdp.py
22 * {page}
23 */
24
25#ifndef __BLUETOOTH_SDP_H
26#define __BLUETOOTH_SDP_H
27
28'''
29
30trailer = '''
31#endif
32'''
33
34defines = []
35
36# Convert CamelCase to snake_case from http://stackoverflow.com/a/1176023
37def camel_to_underscore(name):
38    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
39    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
40
41def create_pretty_define(name):
42    name = name.lstrip()
43    to_delete = [ '(FTP v1.2 and later)', '(Deprecated)', '(FTP v1.2 and later)', '(GOEP v2.0 and later)',
44     '(BIP v1.1 and later)', '(MAP v1.2 and later)', '(OPP v1.2 and later)', '(Not used in PAN v1.0)', '(PBAP v1.2 and later)']
45    for item in to_delete:
46        name = name.replace(item, '')
47    name = name.rstrip()
48    name = name.replace(' - ', '_')
49    name = name.replace(' ', '_')
50    name = name.replace('/','')
51    name = name.replace('(','_')
52    name = name.replace(')','')
53    name = name.replace('-','_')
54    name = name.replace('PnP', 'PNP')
55    name = name.replace('IPv', 'IPV')
56    name = name.replace('ServiceClassID','')
57    name = name.replace('&','and')
58    return camel_to_underscore(name).replace('__','_').replace('3_D','3D').replace('L2_CAP','L2CAP')
59
60def remove_newlines(remark):
61    return " ".join(remark.split())
62
63def process_table(fout, tbody, pattern):
64    rows = tbody.getchildren()
65    for row in rows:
66        columns = row.getchildren()
67        name = columns[0].text_content().encode('ascii','ignore')
68        value = columns[1].text_content().encode('ascii','ignore')
69        remark = ''
70        if (len(columns) > 2):
71            remark = columns[2].text_content().encode('ascii','ignore')
72        # skip tbody headers
73        if name in ["Protocol Name", "Service Class Name", "Attribute Name",
74            "Reserved", 'Reserved for HID Attributes', 'Available for HID Language Strings']:
75            continue
76        # skip tbody footers
77        if value.startswith('(Max value '):
78            continue
79        name = create_pretty_define(name)
80        # skip duplicate attributes
81        if name in defines:
82            continue
83        value = remove_newlines(value)
84        remark = remove_newlines(remark)
85        fout.write(pattern % (name, value, remark))
86        defines.append(name)
87
88def scrape_attributes(fout, tree, table_name):
89    tables = tree.xpath("//table[preceding-sibling::h3 = '" + table_name +"']")
90    tbody = tables[0].getchildren()[0]
91    process_table(fout, tbody, '#define BLUETOOTH_ATTRIBUTE_%-54s %s%s\n')
92
93def scrape_page(fout, url):
94    print("Parsing %s" % url)
95    fout.write(header.format(page=url))
96
97    # get from web
98    r = requests.get(url)
99    content = r.text
100
101    # test: fetch from local file 'service-discovery.html'
102    # f = codecs.open("service-discovery.html", "r", "utf-8")
103    # content = f.read();
104
105    tree = html.fromstring(content)
106
107    # # Protocol Identifiers
108    fout.write('/**\n')
109    fout.write(' * Protocol Identifiers\n')
110    fout.write(' */\n')
111    tables = tree.xpath("//table[preceding-sibling::h3 = 'Protocol Identifiers']")
112    tbody = tables[0].getchildren()[0]
113    process_table(fout, tbody, '#define BLUETOOTH_PROTOCOL_%-55s %s // %s\n')
114    fout.write('\n')
115
116    # # Service Classes
117    fout.write('/**\n')
118    fout.write(' * Service Classes\n')
119    fout.write(' */\n')
120    tables = tree.xpath("//table[preceding-sibling::h3 = 'Protocol Identifiers']")
121    tbody = tables[1].getchildren()[0]
122    process_table(fout, tbody, '#define BLUETOOTH_SERVICE_CLASS_%-50s %s // %s\n')
123    fout.write('\n')
124
125    # Attributes
126    fout.write('/**\n')
127    fout.write(' * Attributes\n')
128    fout.write(' */\n')
129    table_names = [
130        # 'Base Universally Unique Identifier (UUID)',
131        # 'Browse Group Identifiers',
132        'Attribute Identifiers',
133        # 'Audio/Video Remote Control Profile (AVRCP)',
134        'Basic Imaging Profile (BIP)',
135        'Basic Printing Profile (BPP)',
136        'Bluetooth Core Specification: Universal Attributes',
137        'Bluetooth Core Specification: Service Discovery Service',
138        # 'Bluetooth Core Specification: Browse Group Descriptor Service',
139        # 'Cordless Telephony Profile [DEPRECATED]',
140        'Device Identification Profile',
141        # 'Fax Profile [DEPRECATED]',
142        'File Transfer Profile',
143        'Generic Object Exchange Profile',
144        # 'Global Navigation Satellite System Profile (GNSS)', -- note: SupportedFeatures, but different UUID
145        'Hands-Free Profile',
146        'Hardcopy Replacement Profile ',
147        'Headset Profile',
148        'Health Device Profile',
149        'Human Interface Device Profile',
150        # 'Interoperability Requirements for Bluetooth technology as a WAP Bearer [DEPRECATED]',
151        'Message Access Profile',
152        'Object Push Profile',
153        'Personal Area Networking Profile',
154        'Phone Book Access Profile',
155        'Synchronization Profile',
156        # 'Attribute ID Offsets for Strings',
157        # 'Protocol Parameters',
158        'Multi-Profile',
159        'Calendar Tasks and Notes',
160    ]
161    for table_name in table_names:
162        scrape_attributes(fout, tree, table_name)
163    # see above
164    fout.write('#define BLUETOOTH_ATTRIBUTE_GNSS_SUPPORTED_FEATURES                                0x0200\n');
165
166
167
168btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
169gen_path = btstack_root + '/src/bluetooth_sdp.h'
170
171print(program_info)
172
173with open(gen_path, 'wt') as fout:
174    scrape_page(fout, 'https://www.bluetooth.com/specifications/assigned-numbers/service-discovery')
175    fout.write(trailer)
176
177print('Scraping successful!\n')