| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Script for converting the Web Bluetooth GATT blacklist into the format | |
| 7 expected by ContentBrowserClient#GetWebBluetoothBlacklist. | |
| 8 | |
| 9 See: | |
| 10 https://github.com/WebBluetoothCG/registries/blob/master/gatt_blacklist.txt | |
| 11 content/public/browser/content_browser_client.h | |
| 12 | |
| 13 Usage: | |
| 14 compact_blacklist.py <gatt_blacklist.txt> | |
| 15 """ | |
| 16 | |
| 17 import collections | |
| 18 import string | |
| 19 import sys | |
| 20 | |
| 21 UUID_LENGTH = 36 | |
| 22 UUID_BASE_POSTFIX = '-0000-1000-8000-00805f9b34fb' | |
| 23 | |
| 24 | |
| 25 class BadLineException(Exception): | |
| 26 pass | |
| 27 | |
| 28 | |
| 29 class InvalidUUIDException(Exception): | |
| 30 pass | |
| 31 | |
| 32 | |
| 33 class DuplicateUUIDException(Exception): | |
| 34 pass | |
| 35 | |
| 36 | |
| 37 class InvalidExclusionException(Exception): | |
| 38 pass | |
| 39 | |
| 40 | |
| 41 def ValidUUID(uuid): | |
| 42 if len(uuid) != UUID_LENGTH: | |
| 43 return False | |
| 44 for i in range(UUID_LENGTH): | |
| 45 if i in [8, 13, 18, 23]: | |
| 46 if uuid[i] != '-': | |
| 47 return False | |
| 48 else: | |
| 49 if uuid[i] not in string.hexdigits: | |
| 50 return False | |
| 51 return True | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 def ShortenUUID(uuid): | |
| 57 """Shorten a UUUD that use Bluetooth base UUID. | |
| 58 | |
| 59 Note: this function shortens all UUIDs that use the Bluetooth base | |
| 60 UUID even though the specification states that only an assigned UUID | |
| 61 can be shortened. In this case it works fine, since the constructor in | |
| 62 bluetooth_uuid.cc also works the same way. | |
| 63 """ | |
| 64 | |
| 65 if uuid[8:] == UUID_BASE_POSTFIX: | |
| 66 new_uuid = '%x' % int(uuid[:8], 16) | |
| 67 if len(new_uuid) in [4, 8]: | |
| 68 uuid = new_uuid | |
| 69 return uuid | |
| 70 | |
| 71 | |
| 72 def Process(line, blacklist): | |
| 73 line = line.strip().lower() | |
| 74 if not line or line.startswith('#'): | |
| 75 return | |
| 76 fields = line.split() | |
| 77 if len(fields) not in [1, 2]: | |
| 78 raise BadLineException('Badly formatted line: %s' % line) | |
| 79 uuid = fields[0] | |
| 80 if not ValidUUID(uuid): | |
| 81 raise InvalidUUIDException('Invalid UUID: %s' % line) | |
| 82 uuid = ShortenUUID(uuid) | |
| 83 if uuid in blacklist: | |
| 84 raise DuplicateUUIDException('Duplicate UUID: %s' % line) | |
| 85 if len(fields) == 1: | |
| 86 blacklist[uuid] = 'e' | |
| 87 elif fields[1] == 'exclude-writes': | |
| 88 blacklist[uuid] = 'w' | |
| 89 elif fields[1] == 'exclude-reads': | |
| 90 blacklist[uuid] = 'r' | |
| 91 else: | |
| 92 raise InvalidExclusionException('Invalid exclusion value: %s' % line) | |
| 93 | |
| 94 | |
| 95 def main(): | |
| 96 if len(sys.argv) != 2: | |
| 97 print('Usage: %s <gatt_blacklist.txt>' % sys.argv[0]) | |
| 98 return 1 | |
| 99 | |
| 100 try: | |
| 101 blacklist = collections.OrderedDict() | |
| 102 with open(sys.argv[1]) as f: | |
| 103 for line in f: | |
| 104 Process(line, blacklist) | |
| 105 print(','.join('%s:%s' % (uuid, blacklist[uuid]) for uuid in blacklist)) | |
| 106 return 0 | |
| 107 except Exception as e: | |
| 108 print('Failed to compact blacklist. %s' % e) | |
| 109 return 1 | |
| 110 | |
| 111 | |
| 112 if __name__ == '__main__': | |
| 113 sys.exit(main()) | |
| OLD | NEW |