bluetooth_gatt.py 2.6 KB

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