From b165f97b80eef122c410af6eea7328b135983d75 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 28 Oct 2016 15:28:53 +0200 Subject: [PATCH] GATT Converter: resolve UUIDs from src/bluetooth_gatt.h --- tool/compile_gatt.py | 48 +++++++++++++++++++++++++++++------- tool/convert_gatt_service.py | 25 ++++++++++--------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/tool/compile_gatt.py b/tool/compile_gatt.py index 473980cc7..2d695701e 100755 --- a/tool/compile_gatt.py +++ b/tool/compile_gatt.py @@ -7,11 +7,12 @@ # PRIMARY_SERVICE, SERVICE_UUID # CHARACTERISTIC, ATTRIBUTE_TYPE_UUID, [READ | WRITE | DYNAMIC], VALUE -import re -import io -import csv -import string import codecs +import csv +import io +import os +import re +import string header = ''' // {0} generated from {1} for BTstack @@ -87,6 +88,16 @@ defines = [] handle = 1 total_size = 0 +def read_defines(infile): + defines = dict() + with open (infile, 'rt') as fin: + for line in fin: + parts = re.match('#define\s+(\w+)\s+(\w+)',line) + if parts and len(parts.groups()) == 2: + (key, value) = parts.groups() + defines[key] = int(value, 16) + return defines + def keyForUUID(uuid): keyUUID = "" for i in uuid: @@ -114,6 +125,9 @@ def parseUUID128(uuid): def parseUUID(uuid): if uuid in assigned_uuids: return twoByteLEFor(assigned_uuids[uuid]) + uuid_upper = uuid.upper().replace('.','_') + if uuid_upper in bluetooth_gatt: + return twoByteLEFor(bluetooth_gatt[uuid_upper]) if is_128bit_uuid(uuid): return parseUUID128(uuid) uuidInt = int(uuid, 16) @@ -492,11 +506,19 @@ def parse(fname_in, fin, fname_out, fout): fout.write(header.format(fname_out, fname_in)) fout.write('{\n') + line_count = 0; for line in fin: line = line.strip("\n\r ") - - if line.startswith("#") or line.startswith("//"): - fout.write("//" + line.lstrip('/#') + '\n') + line_count += 1 + + if line.startswith("//"): + fout.write("//" + line.lstrip('/') + '\n') + continue + + if line.startswith("#"): + print ("WARNING: #TODO in line %u not handled, skipping declaration:" % line_count) + print ("'%s'" % line) + fout.write("// " + line + '\n') continue if len(line) == 0: @@ -533,8 +555,11 @@ def parse(fname_in, fin, fname_out, fout): parseCharacteristicUserDescription(fout, parts) continue - # 2902 Client Characteristic Configuration - included in Characteristic if + + # 2902 Client Characteristic Configuration - automatically included in Characteristic if # notification / indication is supported + if parts[0] == 'CHARACTERISTIC_USER_DESCRIPTION': + continue # 2903 if parts[0] == 'SERVER_CHARACTERISTIC_CONFIGURATION': @@ -620,13 +645,18 @@ if (len(sys.argv) < 3): print(usage) sys.exit(1) try: + # read defines from bluetooth_gatt.h + btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') + gen_path = btstack_root + '/src/bluetooth_gatt.h' + bluetooth_gatt = read_defines(gen_path) + filename = sys.argv[2] fin = codecs.open (sys.argv[1], encoding='utf-8') fout = open (filename, 'w') parse(sys.argv[1], fin, filename, fout) listHandles(fout) fout.close() - print('Created', filename) + print('Created %s' % filename) except IOError as e: print(usage) diff --git a/tool/convert_gatt_service.py b/tool/convert_gatt_service.py index 05ede0b5f..3aba26d3c 100755 --- a/tool/convert_gatt_service.py +++ b/tool/convert_gatt_service.py @@ -77,6 +77,8 @@ def handle_todo(fout, name): print('-- Descriptor %-40s - TODO: Please set values' % name) fout.write('// TODO: %s: please set values\n' % name) +def define_for_type(type): + return type.upper().replace('.','_') def convert_service(fout, specification_type): url = 'https://www.bluetooth.com/api/gatt/xmlfile?xmlFileName=%s.xml' % specification_type @@ -105,7 +107,7 @@ def convert_service(fout, specification_type): fout.write('// %s\n' % url) fout.write('\n') fout.write('// %s %s\n' % (service_name, service_uuid)) - fout.write('PRIMARY_SERVICE, %s\n' % specification_type) + fout.write('PRIMARY_SERVICE, %s\n' % define_for_type(specification_type)) characteristics = tree.find('Characteristics') for characteristic in characteristics: @@ -114,7 +116,7 @@ def convert_service(fout, specification_type): property_list_human = parse_properties(characteristic) properties = ' | '.join( ['DYNAMIC'] + property_list_human).upper() print("- Characteristic %s - properties %s" % (name, property_list_human)) - fout.write('CHARACTERISTIC, %s, %s,\n' % (type, properties)) + fout.write('CHARACTERISTIC, %s, %s,\n' % (define_for_type(type), properties)) descriptors = characteristic.find('Descriptors') for descriptor in descriptors: @@ -124,27 +126,28 @@ def convert_service(fout, specification_type): properties = ' | '.join(property_list_human).upper() if (type == 'org.bluetooth.descriptor.gatt.client_characteristic_configuration'): - print('-- Descriptor %-40s - SKIPPED: automatically generated currently by compile_gatt.py' % name) + print('-- Descriptor %-40s' % name) + fout.write('CLIENT_CHARACTERISTIC_CONFIGURATION, %s,\n' % properties) + continue + + if (type == 'org.bluetooth.descriptor.gatt.server_characteristic_configuration'): + print('-- Descriptor %-40s' % name) + fout.write('SERVER_CHARACTERISTIC_CONFIGURATION, %s,\n' % properties) continue if (type == 'org.bluetooth.descriptor.gatt.characteristic_presentation_format'): handle_todo(fout, 'Characteristic Presentation Format') - fout.write('CHARACTERISTIC_FORMAT, %s, _format_, _exponent_, _unit_, _name_space_, _description_\n' % properties) + fout.write('#TODO CHARACTERISTIC_FORMAT, %s, _format_, _exponent_, _unit_, _name_space_, _description_\n' % properties) continue if (type == 'org.bluetooth.descriptor.gatt.characteristic_user_description'): handle_todo(fout, 'Characteristic User Description') - fout.write('CHARACTERISTIC_USER_DESCRIPTION, %s, _user_description_\n' % properties) - continue - - if (type == 'org.bluetooth.descriptor.gatt.server_characteristic_configuration'): - handle_todo(fout, 'Server Characteristic Configuration') - fout.write('SERVER_CHARACTERISTIC_CONFIGURATION, %s,\n' % properties) + fout.write('#TODO CHARACTERISTIC_USER_DESCRIPTION, %s, _user_description_\n' % properties) continue if (type == 'org.bluetooth.descriptor.gatt.characteristic_aggregate_format'): handle_todo(fout, 'Characteristic Aggregate Format') - fout.write('CHARACTERISTIC_AGGREGATE_FORMAT, %s, _list_of_handles_\n' % properties) + fout.write('#TODO CHARACTERISTIC_AGGREGATE_FORMAT, %s, _list_of_handles_\n' % properties) continue if (type == 'org.bluetooth.descriptor.valid_range'):