tool/bluetooth_company_id: update script to use CSV from Bluetooth SIG

This commit is contained in:
Milanka Ringwald 2022-02-16 18:10:16 +01:00
parent 18e1cd232f
commit 77bd03813f
2 changed files with 1777 additions and 69 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,11 +14,13 @@ import codecs
import os
import re
headers = {'user-agent': 'curl/7.63.0'}
import csv
import getopt
program_info = '''
BTstack Company ID Scraper for BTstack
Copyright 2017, BlueKitchen GmbH
Copyright 2022, BlueKitchen GmbH
'''
header = '''
@ -64,52 +66,85 @@ def create_name(company):
tag = tag.replace('.', '')
tag = tag.replace('-','_')
tag = tag.replace(' ',' ')
tag = tag.replace(' ',' ')
tag = tag.replace(' ',' ')
tag = tag.replace(' ','_')
tag = tag.replace('&','AND')
tag = tag.replace("'","_")
tag = tag.replace('"','_')
tag = tag.replace('!','_')
tag = tag.replace('|','_')
tag = tag.replace('[','')
tag = tag.replace(']','')
return "BLUETOOTH_COMPANY_ID_" + tag
def scrape_page(fout, url):
global headers
print("Parsing %s" % url)
def parse_cvs(csv_file):
cvsreader = csv.reader(csv_file, delimiter=',', quotechar='\"')
# Skip header ['"Decimal","Hexadecimal","Company"']
next(cvsreader)
companies = {}
for row in cvsreader:
id_dec = int(row[0])
companies[id_dec] = (row[1],row[2])
return companies
def write_cvs(fout, companies, url):
global tags
fout.write(page_info.format(page=url.replace('https://','')))
# get from web
r = requests.get(url, headers=headers)
content = r.text
company_ids = sorted(list(companies.keys()))
# test: fetch from local file 'service-discovery.html'
# f = codecs.open("company-identifiers.html", "r", "utf-8")
# content = f.read();
tree = html.fromstring(content)
rows = tree.xpath('//table/tbody/tr')
for row in rows:
children = row.getchildren()
id_hex = children[1].text_content()
company = create_name(children[2].text_content())
for id_dec in company_ids:
id_hex = companies[id_dec][0]
company = create_name(companies[id_dec][1])
if company in tags:
company = company+"2"
else:
tags.append(company)
fout.write("#define %-80s %s\n" % (company, id_hex))
# map CSR onto QTIL
fout.write("#define BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO BLUETOOTH_COMPANY_ID_QUALCOMM_TECHNOLOGIES_INTERNATIONAL_LTD\n")
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
gen_path = btstack_root + '/src/bluetooth_company_id.h'
def main(argv):
url = "https://www.bluetooth.com/de/specifications/assigned-numbers/company-identifiers/#"
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
tools_root = btstack_root + '/tool'
src_root = btstack_root + '/src/'
header_filename = "bluetooth_company_id.h"
header_path = btstack_root + "/src/" + header_filename
cvs_filename = "CompanyIdentfiers - CSV.csv"
cvs_path = tools_root + "/" + cvs_filename
print(program_info)
print(program_info)
try:
header_file = open(header_path, "w")
except FileNotFoundError:
print("\nFile \'%s\' cannot be created in \'%s\' directory." % (header_filename, src_root))
exit(1)
try:
with open(cvs_path, "r") as csv_file:
companies = parse_cvs(csv_file)
header_file.write(header.format(datetime=str(datetime.datetime.now())))
write_cvs(header_file, companies, url)
header_file.write(trailer)
print("Company IDs are stored in \'%s\'\n" % (header_path))
except FileNotFoundError:
print("\nCVS file \'%s\' not found in \'%s\'." % (cvs_filename, tools_root))
print("Please download the CVS file, then start the skript again.\n")
print("The CVS file can be downloaded from:")
print(" %s\n" % url)
exit(1)
if __name__ == "__main__":
main(sys.argv[1:])
with open(gen_path, 'wt') as fout:
fout.write(header.format(datetime=str(datetime.datetime.now())))
scrape_page(fout, 'https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers')
fout.write(trailer)
print('Scraping successful!\n')