tool: scrape Bluetooth site for Data Types used in Advertisements, EIR, etc.

This commit is contained in:
Matthias Ringwald 2016-10-31 15:44:45 +01:00
parent 8a7d93dccc
commit 0f308e7392
2 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,51 @@
/**
* bluetooth_data_types.h generated from Bluetooth SIG website for BTstack
* https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
*/
#ifndef __BLUETOOTH_DATA_TYPES_H
#define __BLUETOOTH_DATA_TYPES_H
#define BLUETOOTH_DATA_TYPE_FLAGS 0x01 // Flags
#define BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS 0x02 // Incomplete List of 16-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS 0x03 // Complete List of 16-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_32_BIT_SERVICE_CLASS_UUIDS 0x04 // Incomplete List of 32-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_32_BIT_SERVICE_CLASS_UUIDS 0x05 // Complete List of 32-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS 0x06 // Incomplete List of 128-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS 0x07 // Complete List of 128-bit Service Class UUIDs
#define BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME 0x08 // Shortened Local Name
#define BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME 0x09 // Complete Local Name
#define BLUETOOTH_DATA_TYPE_TX_POWER_LEVEL 0x0A // Tx Power Level
#define BLUETOOTH_DATA_TYPE_CLASS_OF_DEVICE 0x0D // Class of Device
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_HASH_C 0x0E // Simple Pairing Hash C
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_HASH_C_192 0x0E // Simple Pairing Hash C-192
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_RANDOMIZER_R 0x0F // Simple Pairing Randomizer R
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_RANDOMIZER_R_192 0x0F // Simple Pairing Randomizer R-192
#define BLUETOOTH_DATA_TYPE_DEVICE_ID 0x10 // Device ID
#define BLUETOOTH_DATA_TYPE_SECURITY_MANAGER_TK_VALUE 0x10 // Security Manager TK Value
#define BLUETOOTH_DATA_TYPE_SECURITY_MANAGER_OUT_OF_BAND_FLAGS 0x11 // Security Manager Out of Band Flags
#define BLUETOOTH_DATA_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE 0x12 // Slave Connection Interval Range
#define BLUETOOTH_DATA_TYPE_LIST_OF_16_BIT_SERVICE_SOLICITATION_UUIDS 0x14 // List of 16-bit Service Solicitation UUIDs
#define BLUETOOTH_DATA_TYPE_LIST_OF_32_BIT_SERVICE_SOLICITATION_UUIDS 0x1F // List of 32-bit Service Solicitation UUIDs
#define BLUETOOTH_DATA_TYPE_LIST_OF_128_BIT_SERVICE_SOLICITATION_UUIDS 0x15 // List of 128-bit Service Solicitation UUIDs
#define BLUETOOTH_DATA_TYPE_SERVICE_DATA 0x16 // Service Data
#define BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID 0x16 // Service Data - 16-bit UUID
#define BLUETOOTH_DATA_TYPE_SERVICE_DATA_32_BIT_UUID 0x20 // Service Data - 32-bit UUID
#define BLUETOOTH_DATA_TYPE_SERVICE_DATA_128_BIT_UUID 0x21 // Service Data - 128-bit UUID
#define BLUETOOTH_DATA_TYPE_LE_SECURE_CONNECTIONS_CONFIRMATION_VALUE 0x22 // LE Secure Connections Confirmation Value
#define BLUETOOTH_DATA_TYPE_LE_SECURE_CONNECTIONS_RANDOM_VALUE 0x23 // LE Secure Connections Random Value
#define BLUETOOTH_DATA_TYPE_URI 0x24 // URI
#define BLUETOOTH_DATA_TYPE_INDOOR_POSITIONING 0x25 // Indoor Positioning
#define BLUETOOTH_DATA_TYPE_TRANSPORT_DISCOVERY_DATA 0x26 // Transport Discovery Data
#define BLUETOOTH_DATA_TYPE_PUBLIC_TARGET_ADDRESS 0x17 // Public Target Address
#define BLUETOOTH_DATA_TYPE_RANDOM_TARGET_ADDRESS 0x18 // Random Target Address
#define BLUETOOTH_DATA_TYPE_APPEARANCE 0x19 // Appearance
#define BLUETOOTH_DATA_TYPE_ADVERTISING_INTERVAL 0x1A // Advertising Interval
#define BLUETOOTH_DATA_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS 0x1B // LE Bluetooth Device Address
#define BLUETOOTH_DATA_TYPE_LE_ROLE 0x1C // LE Role
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_HASH_C_256 0x1D // Simple Pairing Hash C-256
#define BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_RANDOMIZER_R_256 0x1E // Simple Pairing Randomizer R-256
#define BLUETOOTH_DATA_TYPE_3D_INFORMATION_DATA 0x3D // 3D Information Data
#define BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA 0xFF // Manufacturer Specific Data
#endif

91
tool/bluetooth_data_types.py Executable file
View File

@ -0,0 +1,91 @@
#!/usr/bin/env python
#
# Scrape GAP Data Types from Bluetooth SIG page
# Copyright 2016 BlueKitchen GmbH
#
from lxml import html
import datetime
import re
import requests
import sys
import os
program_info = '''
BTstack Data Types Scraper for BTstack
Copyright 2016, BlueKitchen GmbH
'''
header = '''/**
* bluetooth_data_types.h generated from Bluetooth SIG website for BTstack
* {url}
*/
#ifndef __BLUETOOTH_DATA_TYPES_H
#define __BLUETOOTH_DATA_TYPES_H
'''
trailer = '''
#endif
'''
def clean(tag):
# << 0xab
# >> 0xbb
# \n
# non-visible whitespace 0x200b
# non-vicible whitespace 0xa0
return tag.replace(u'\xab','').replace(u'\xbb','').replace(u'\u200b','').replace('\n','').replace(u'\xa0',' ').strip()
def scrape_page(fout, url):
print("Parsing %s" % url)
page = requests.get(url)
tree = html.fromstring(page.content)
print('')
print('%-48s | %s' % ("Data Type Name", "Data Type Value"))
print('-' * 70)
# get all <tr> elements in <table id="gattTable">
rows = tree.xpath('//div[@class="table-time"]/table/tbody/tr')
for row in rows:
children = row.getchildren()
data_type_value = children[0].text_content()
data_type_name = children[1].text_content()
# table with references to where it was used
if (data_type_value == 'Data Type Value'):
continue
# clean up
data_type_name = clean(data_type_name)
data_type_value = clean(data_type_value)
tag = data_type_name
# uppper
tag = tag.upper()
# collapse ' - ' into ' '
tag = tag.replace(' - ', ' ')
# drop dashes otherwise
tag = tag.replace('-',' ')
# collect multiple spaces
tag = re.sub('\s+', ' ', tag).strip()
# replace space with under score
tag =tag.replace(' ', '_')
fout.write("#define BLUETOOTH_DATA_TYPE_%-50s %s // %s\n" % (tag, data_type_value, data_type_name))
print("%-48s | %s" % (data_type_name, data_type_value))
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
gen_path = btstack_root + '/src/bluetooth_data_types.h'
print(program_info)
with open(gen_path, 'wt') as fout:
url = 'https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile'
fout.write(header.format(datetime=str(datetime.datetime.now()), url=url))
scrape_page(fout, url)
fout.write(trailer)
print('')
print('Scraping successful into %s!\n' % gen_path)