summaryrefslogtreecommitdiffstats
path: root/tools/generate-bacnet-vendors.py
blob: 14fc5303e986561d1687be4438e4737b87d65793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3

'''
 Copyright 2023 Jaap Keuter <jaap.keuter@xs4all.nl>
 based on work by Anish Bhatt <anish@chelsio.com>

SPDX-License-Identifier: GPL-2.0-or-later
'''

import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup

req_headers = { 'User-Agent': 'Wireshark generate-bacnet-vendors' }
try:
    req = urllib.request.Request("https://bacnet.org/assigned-vendor-ids/", headers=req_headers)
    response = urllib.request.urlopen(req)
    lines = response.read().decode()
    response.close()
except urllib.error.HTTPError as err:
    exit_msg("HTTP error fetching {0}: {1}".format(url, err.reason))
except urllib.error.URLError as err:
    exit_msg("URL error fetching {0}: {1}".format(url, err.reason))
except OSError as err:
    exit_msg("OS error fetching {0}".format(url, err.strerror))
except Exception:
    exit_msg("Unexpected error:", sys.exc_info()[0])

soup = BeautifulSoup(lines, "html.parser")
table = soup.find('table')
rows = table.findAll('tr')

entry = "static const value_string\nBACnetVendorIdentifiers [] = {"

for tr in rows:
    cols = tr.findAll('td')
    for index,td in enumerate(cols[0:2]):
        text = ''.join(td.find(string=True))
        if index == 0:
            entry = "    { %4s" % text
        else:
            entry += ", \"%s\" }," % text.rstrip()
    print(entry)

entry = "    { 0, NULL }\n};"
print(entry)