compile_gatt: add additional paths for .gatt files using -I

This commit is contained in:
Milanka Ringwald 2018-07-05 12:15:01 +02:00
parent 432f6cc7ec
commit dbb3997aee

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# #
# BLE GATT configuration generator for use with BTstack, v0.1 # BLE GATT configuration generator for use with BTstack
# Copyright 2011 Matthias Ringwald # Copyright 2018 BlueKitchen GmbH
# #
# Format of input file: # Format of input file:
# PRIMARY_SERVICE, SERVICE_UUID # PRIMARY_SERVICE, SERVICE_UUID
@ -14,6 +14,7 @@ import os
import re import re
import string import string
import sys import sys
import argparse
header = ''' header = '''
// {0} generated from {1} for BTstack // {0} generated from {1} for BTstack
@ -27,14 +28,9 @@ header = '''
const uint8_t profile_data[] = const uint8_t profile_data[] =
''' '''
usage = '''
Usage: ./compile_gatt.py profile.gatt profile.h
'''
print(''' print('''
BLE configuration generator for use with BTstack, v0.1 BLE configuration generator for use with BTstack
Copyright 2011 Matthias Ringwald Copyright 2018 BlueKitchen GmbH
''') ''')
assigned_uuids = { assigned_uuids = {
@ -104,7 +100,6 @@ property_flags = {
# 0x80 # 0x80
} }
btstack_root = ''
services = dict() services = dict()
characteristic_indices = dict() characteristic_indices = dict()
presentation_formats = dict() presentation_formats = dict()
@ -693,14 +688,15 @@ def parseLines(fname_in, fin, fout):
imported_file = '' imported_file = ''
parts = re.match('#import\s+<(.*)>\w*',line) parts = re.match('#import\s+<(.*)>\w*',line)
if parts and len(parts.groups()) == 1: if parts and len(parts.groups()) == 1:
imported_file = btstack_root+'/src/ble/gatt-service/' + parts.groups()[0] imported_file = parts.groups()[0]
parts = re.match('#import\s+"(.*)"\w*',line) parts = re.match('#import\s+"(.*)"\w*',line)
if parts and len(parts.groups()) == 1: if parts and len(parts.groups()) == 1:
imported_file = os.path.abspath(os.path.dirname(fname_in) + '/'+parts.groups()[0]) imported_file = parts.groups()[0]
if len(imported_file) == 0: if len(imported_file) == 0:
print('ERROR: #import in file %s - line %u neither <name.gatt> nor "name.gatt" form', (fname_in, line_count)) print('ERROR: #import in file %s - line %u neither <name.gatt> nor "name.gatt" form', (fname_in, line_count))
continue continue
imported_file = getFile( imported_file )
print("Importing %s" % imported_file) print("Importing %s" % imported_file)
try: try:
imported_fin = codecs.open (imported_file, encoding='utf-8') imported_fin = codecs.open (imported_file, encoding='utf-8')
@ -860,19 +856,47 @@ def listHandles(fout):
fout.write(define) fout.write(define)
fout.write('\n') fout.write('\n')
if (len(sys.argv) < 3): def getFile( fileName ):
print(usage) inc = args.I
sys.exit(1) for d in inc:
fullFile = d[0] + fileName
print("test %s" % fullFile)
if os.path.isfile( fullFile ) == True:
return fullFile
print ("'{0}' not found".format( fileName ))
print ("Include paths: %s" % ", ".join(inc))
exit(-1)
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
default_includes = [ btstack_root + '/src/', btstack_root + '/src/ble/gatt-service/']
parser = argparse.ArgumentParser(description='BLE GATT configuration generator for use with BTstack')
parser.add_argument('-I', action='append', nargs=1, metavar='includes',
help='include search path for .gatt service files and bluetooth_gatt.h (default: %s)' % ", ".join(default_includes))
parser.add_argument('gattfile', metavar='gattfile', type=str,
help='gatt file to be compiled')
parser.add_argument('hfile', metavar='hfile', type=str,
help='header file to be generated')
args = parser.parse_args()
# append default include paths
if args.I == None:
args.I = []
for d in default_includes:
args.I.append([d])
try: try:
# read defines from bluetooth_gatt.h # read defines from bluetooth_gatt.h
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') gen_path = getFile( 'bluetooth_gatt.h' )
gen_path = btstack_root + '/src/bluetooth_gatt.h'
bluetooth_gatt = read_defines(gen_path) bluetooth_gatt = read_defines(gen_path)
filename = sys.argv[2] filename = args.hfile
fin = codecs.open (sys.argv[1], encoding='utf-8') fin = codecs.open (args.gattfile, encoding='utf-8')
fout = open (filename, 'w') fout = open (filename, 'w')
parse(sys.argv[1], fin, filename, fout) parse(args.gattfile, fin, filename, fout)
listHandles(fout) listHandles(fout)
fout.close() fout.close()
print('Created %s' % filename) print('Created %s' % filename)