xref: /btstack/tool/bluetooth_gatt.py (revision c32dba203d4af5ccae95e4d7b5aa9cabd63fc4ec)
1#!/usr/bin/env python
2#
3# Scrape GATT UUIDs from Bluetooth SIG page
4# Copyright 2016 BlueKitchen GmbH
5#
6
7from lxml import html
8import datetime
9import requests
10import sys
11import os
12
13program_info = '''
14BTstack GATT UUID Scraper for BTstack
15Copyright 2016, BlueKitchen GmbH
16'''
17
18header = '''
19/**
20 * bluetooth_gatt.h generated from Bluetooth SIG website for BTstack tool/bluetooth_gatt.py
21 * {datetime}
22 */
23
24#ifndef __BLUETOOTH_GATT_H
25#define __BLUETOOTH_GATT_H
26'''
27
28page_info = '''
29/**
30 * Assigned numbers from {page}
31 */
32'''
33
34trailer = '''
35#endif
36'''
37
38def scrape_page(fout, url):
39    print("Parsing %s" % url)
40    fout.write(page_info.format(page=url))
41    page = requests.get(url)
42    tree = html.fromstring(page.content)
43    # get all <tr> elements in <table id="gattTable">
44    rows = tree.xpath('//table[@id="gattTable"]/tbody/tr')
45    for row in rows:
46        children = row.getchildren()
47        summary = children[0].text_content()
48        id      = children[1].text_content()
49        uuid    = children[2].text_content()
50        if (len(id)):
51            tag = id.upper().replace('.', '_').replace('-','_')
52            fout.write("#define %-80s %s // %s\n" %  (tag, uuid, summary))
53
54btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
55gen_path = btstack_root + '/src/bluetooth_gatt.h'
56
57print(program_info)
58
59with open(gen_path, 'wt') as fout:
60    fout.write(header.format(datetime=str(datetime.datetime.now())))
61    scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/declarations')
62    scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/services')
63    scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/characteristics')
64    scrape_page(fout, 'https://www.bluetooth.com/specifications/gatt/descriptors')
65    fout.write(trailer)
66
67print('Scraping successful!\n')
68