Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
|
scheib
2016/08/29 01:14:47
Let's place this file in tools/web_bluetooth/
| |
| 2 # Copyright (c) 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 blacklist = collections.OrderedDict() | |
| 22 | |
| 23 UUID_LENGTH = 36 | |
| 24 UUID_BASE_POSTFIX = '-0000-1000-8000-00805f9b34fb' | |
| 25 | |
| 26 | |
| 27 class BadLineException(Exception): | |
| 28 pass | |
| 29 | |
| 30 | |
| 31 class InvalidUUIDException(Exception): | |
| 32 pass | |
| 33 | |
| 34 | |
| 35 class DuplicateUUIDException(Exception): | |
| 36 pass | |
| 37 | |
| 38 | |
| 39 class InvalidExclusionException(Exception): | |
| 40 pass | |
| 41 | |
| 42 | |
| 43 def valid_uuid(uuid): | |
| 44 if len(uuid) != UUID_LENGTH: | |
| 45 return False | |
| 46 for i in range(UUID_LENGTH): | |
| 47 if i in [8, 13, 18, 23]: | |
| 48 if uuid[i] != '-': | |
| 49 return False | |
| 50 else: | |
| 51 if uuid[i] not in string.hexdigits: | |
| 52 return False | |
| 53 return True | |
| 54 | |
| 55 | |
| 56 # Note: this function shortens all UUIDs that uses the Bluetooth base | |
|
scheib
2016/08/29 01:14:47
s/uses/use/
| |
| 57 # UUID even thought the specification states that only an assigned | |
| 58 # UUID can be shortened. In this case it works fine, since the constructor in | |
| 59 # bluetooth_uuid.cc also works the same way. | |
| 60 def shorten_uuid(uuid): | |
| 61 if uuid[8:] == UUID_BASE_POSTFIX: | |
| 62 new_uuid = '%x' % int(uuid[:8], 16) | |
| 63 if len(new_uuid) in [4, 8]: | |
| 64 uuid = new_uuid | |
| 65 return uuid | |
| 66 | |
| 67 | |
| 68 def process(line): | |
| 69 line = line.strip().lower() | |
| 70 if not line or line.startswith('#'): | |
| 71 return | |
| 72 fields = line.split() | |
| 73 if len(fields) not in [1, 2]: | |
| 74 raise BadLineException('Badly formatted line: %s' % line) | |
| 75 uuid = fields[0] | |
| 76 if not valid_uuid(uuid): | |
| 77 raise InvalidUUIDException('Invalid UUID: %s' % line) | |
| 78 uuid = shorten_uuid(uuid) | |
| 79 if uuid in blacklist: | |
| 80 raise DuplicateUUIDException('Duplicate UUID: %s' % line) | |
| 81 if len(fields) == 1: | |
| 82 blacklist[uuid] = 'e' | |
| 83 elif fields[1] == 'exclude-writes': | |
| 84 blacklist[uuid] = 'w' | |
| 85 elif fields[1] == 'exclude-reads': | |
| 86 blacklist[uuid] = 'r' | |
| 87 else: | |
| 88 raise InvalidExclusionException('Invalid exclusion value: %s' % line) | |
| 89 | |
| 90 | |
| 91 def main(): | |
| 92 if len(sys.argv) != 2: | |
| 93 sys.exit('Usage: %s <gatt_blacklist.txt>' % sys.argv[0]) | |
| 94 | |
| 95 try: | |
| 96 with open(sys.argv[1]) as f: | |
| 97 for line in f: | |
| 98 process(line) | |
| 99 print(','.join('%s:%s' % (uuid, blacklist[uuid]) for uuid in blacklist)) | |
| 100 except Exception as e: | |
| 101 sys.exit('Failed to compact blacklist. %s' % e) | |
| 102 | |
| 103 | |
| 104 if __name__ == '__main__': | |
| 105 main() | |
| OLD | NEW |