xref: /btstack/tool/bluetooth_sdp.py (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
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
15headers = {'user-agent': 'curl/7.63.0'}
16
17program_info = '''
18BTstack SDP UUID Scraper for BTstack
19Copyright 2017, BlueKitchen GmbH
20'''
21
22header = '''/**
23 * bluetooth_sdp.h generated from Bluetooth SIG website for BTstack by tool/bluetooth_sdp.py
24 * {page}
25 * {datetime}
26 */
27
28#ifndef BLUETOOTH_SDP_H
29#define BLUETOOTH_SDP_H
30
31'''
32
33trailer = '''
34#endif
35'''
36
37defines = []
38
39# Convert CamelCase to snake_case from http://stackoverflow.com/a/1176023
40def camel_to_underscore(name):
41    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
42    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
43
44def create_pretty_define(name):
45    name = name.lstrip()
46    to_delete = [ '(FTP v1.2 and later)', '(Deprecated)', '(FTP v1.2 and later)', '(GOEP v2.0 and later)',
47     '(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)']
48    for item in to_delete:
49        name = name.replace(item, '')
50    name = name.rstrip()
51    name = name.replace(' - ', '_')
52    name = name.replace(' ', '_')
53    name = name.replace('/','')
54    name = name.replace('(','_')
55    name = name.replace(')','')
56    name = name.replace('-','_')
57    name = name.replace('.','_')
58    name = name.replace('PnP', 'PNP')
59    name = name.replace('IPv', 'IPV')
60    name = name.replace('ServiceDiscoveryServerServiceClassID', 'ServiceDiscoveryServer')
61    name = name.replace('BrowseGroupDescriptorServiceClassID', 'BrowseGroupDescriptor')
62    name = name.replace('&','and')
63    name = name.replace('__','_')
64    return camel_to_underscore(name).replace('__','_').replace('3_D','3D').replace('L2_CAP','L2CAP')
65
66def remove_newlines(remark):
67    return " ".join(remark.split())
68
69def process_rows(fout, rows, pattern):
70    for row in rows:
71        columns = row.getchildren()
72        name = columns[0].text_content().encode('ascii','ignore')
73        value = columns[1].text_content().encode('ascii','ignore')
74        remark = ''
75        if (len(columns) > 2):
76            remark = columns[2].text_content().encode('ascii','ignore')
77        # skip tbody headers
78        if name in ["Protocol Name", "Service Class Name", "Attribute Name", "UUID Name",
79            "Reserved", 'Reserved for HID Attributes', 'Available for HID Language Strings']:
80            continue
81        # skip tbody footers
82        if value.startswith('(Max value '):
83            continue
84        name = create_pretty_define(name)
85        # skip duplicate attributes
86        if name in defines:
87            continue
88        value = remove_newlines(value)
89        remark = remove_newlines(remark)
90        fout.write(pattern % (name, value, remark))
91        defines.append(name)
92
93def scrape_page(fout, url):
94    print("Parsing %s" % url)
95    fout.write(header.format(page=url.replace('https://',''),datetime=str(datetime.datetime.now())))
96
97    # get from web
98    r = requests.get(url, headers=headers)
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    rows = tree.xpath("//table[2]/tbody/tr")
112    process_rows(fout, rows, '#define BLUETOOTH_PROTOCOL_%-55s %s // %s\n')
113    fout.write('\n')
114
115    # Service Classes
116    fout.write('/**\n')
117    fout.write(' * Service Classes\n')
118    fout.write(' */\n')
119    rows = tree.xpath("//table[3]/tr")
120    process_rows(fout, rows, '#define BLUETOOTH_SERVICE_CLASS_%-50s %s // %s\n')
121    fout.write('\n')
122
123    # Attributes
124    fout.write('/**\n')
125    fout.write(' * Attributes\n')
126    fout.write(' */\n')
127    table_names = [
128        # 'Base Universally Unique Identifier (UUID)',
129        'Browse Group Identifiers',
130        'Attribute Identifiers',
131        # 'Audio/Video Remote Control Profile (AVRCP)',
132        'Basic Imaging Profile (BIP)',
133        'Basic Printing Profile (BPP)',
134        'Bluetooth Core Specification: Universal Attributes',
135        'Bluetooth Core Specification: Service Discovery Service',
136        # 'Bluetooth Core Specification: Browse Group Descriptor Service',
137        # 'Cordless Telephony Profile [DEPRECATED]',
138        'Device Identification Profile',
139        # 'Fax Profile [DEPRECATED]',
140        'File Transfer Profile',
141        'Generic Object Exchange Profile',
142        # 'Global Navigation Satellite System Profile (GNSS)', -- note: SupportedFeatures, but different UUID
143        'Hands-Free Profile',
144        'Hardcopy Replacement Profile ',
145        'Headset Profile',
146        'Health Device Profile',
147        'Human Interface Device Profile',
148        # 'Interoperability Requirements for Bluetooth technology as a WAP Bearer [DEPRECATED]',
149        'Message Access Profile',
150        'Object Push Profile',
151        'Personal Area Networking Profile',
152        'Phone Book Access Profile',
153        'Synchronization Profile',
154        # 'Attribute ID Offsets for Strings',
155        # 'Protocol Parameters',
156        'Multi-Profile',
157        'Calendar Tasks and Notes',
158    ]
159    for table_name in table_names:
160        rows = tree.xpath("//table[preceding-sibling::h3 = '" + table_name +"']/tr")
161        process_rows(fout, rows, '#define BLUETOOTH_ATTRIBUTE_%-54s %s // %s\n')
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')
178