compile_gatt.py (c9454abcff316b35e532dc5cfc0df1ce2e2fe90a) compile_gatt.py (60b51a4c4ef80f81d34069db584efea8e628aa2e)
1#!/usr/bin/env python
2#
3# BLE GATT configuration generator for use with BTstack, v0.1
4# Copyright 2011 Matthias Ringwald
5#
6# Format of input file:
7# PRIMARY_SERVICE, SERVICE_UUID
8# CHARACTERISTIC, ATTRIBUTE_TYPE_UUID, [READ | WRITE | DYNAMIC], VALUE
9
10import codecs
11import csv
12import io
13import os
14import re
15import string
1#!/usr/bin/env python
2#
3# BLE GATT configuration generator for use with BTstack, v0.1
4# Copyright 2011 Matthias Ringwald
5#
6# Format of input file:
7# PRIMARY_SERVICE, SERVICE_UUID
8# CHARACTERISTIC, ATTRIBUTE_TYPE_UUID, [READ | WRITE | DYNAMIC], VALUE
9
10import codecs
11import csv
12import io
13import os
14import re
15import string
16import sys
16
17header = '''
18// {0} generated from {1} for BTstack
19
20// binary representation
21// attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...)
22
23#include <stdint.h>
24
25const uint8_t profile_data[] =
26'''
27
28usage = '''
29Usage: ./compile_gatt.py profile.gatt profile.h
30'''
31
17
18header = '''
19// {0} generated from {1} for BTstack
20
21// binary representation
22// attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...)
23
24#include <stdint.h>
25
26const uint8_t profile_data[] =
27'''
28
29usage = '''
30Usage: ./compile_gatt.py profile.gatt profile.h
31'''
32
32import re
33import sys
34
35print('''
36BLE configuration generator for use with BTstack, v0.1
37Copyright 2011 Matthias Ringwald
38''')
39
40assigned_uuids = {
41 'GAP_SERVICE' : 0x1800,

--- 30 unchanged lines hidden (view full) ---

72 'ENCRYPTION_KEY_SIZE_14': 0xd000,
73 'ENCRYPTION_KEY_SIZE_15': 0xe000,
74 'ENCRYPTION_KEY_SIZE_16': 0xf000,
75 # only used by gatt compiler >= 0xffff
76 # Extended Properties
77 'RELIABLE_WRITE': 0x10000,
78}
79
33
34print('''
35BLE configuration generator for use with BTstack, v0.1
36Copyright 2011 Matthias Ringwald
37''')
38
39assigned_uuids = {
40 'GAP_SERVICE' : 0x1800,

--- 30 unchanged lines hidden (view full) ---

71 'ENCRYPTION_KEY_SIZE_14': 0xd000,
72 'ENCRYPTION_KEY_SIZE_15': 0xe000,
73 'ENCRYPTION_KEY_SIZE_16': 0xf000,
74 # only used by gatt compiler >= 0xffff
75 # Extended Properties
76 'RELIABLE_WRITE': 0x10000,
77}
78
79btstack_root = ''
80services = dict()
81characteristic_indices = dict()
82presentation_formats = dict()
83current_service_uuid_string = ""
84current_service_start_handle = 0
85current_characteristic_uuid_string = ""
86defines_for_characteristics = []
87defines_for_services = []

--- 411 unchanged lines hidden (view full) ---

499 write_16(fout, size)
500 write_16(fout, property_read)
501 write_16(fout, handle)
502 write_16(fout, 0x2909)
503 write_sequence(fout, no_of_digitals)
504 fout.write("\n")
505 handle = handle + 1
506
80services = dict()
81characteristic_indices = dict()
82presentation_formats = dict()
83current_service_uuid_string = ""
84current_service_start_handle = 0
85current_characteristic_uuid_string = ""
86defines_for_characteristics = []
87defines_for_services = []

--- 411 unchanged lines hidden (view full) ---

499 write_16(fout, size)
500 write_16(fout, property_read)
501 write_16(fout, handle)
502 write_16(fout, 0x2909)
503 write_sequence(fout, no_of_digitals)
504 fout.write("\n")
505 handle = handle + 1
506
507
508def parse(fname_in, fin, fname_out, fout):
507def parseLines(fname_in, fin, fout):
509 global handle
510 global total_size
508 global handle
509 global total_size
511
512 fout.write(header.format(fname_out, fname_in))
513 fout.write('{\n')
514
510
515 line_count = 0;
516 for line in fin:
517 line = line.strip("\n\r ")
518 line_count += 1
519
520 if line.startswith("//"):
521 fout.write(" //" + line.lstrip('/') + '\n')
522 continue
523
511 line_count = 0;
512 for line in fin:
513 line = line.strip("\n\r ")
514 line_count += 1
515
516 if line.startswith("//"):
517 fout.write(" //" + line.lstrip('/') + '\n')
518 continue
519
524 if line.startswith("#"):
525 print ("WARNING: #TODO in line %u not handled, skipping declaration:" % line_count)
520 if line.startswith("#import"):
521 imported_file = ''
522 parts = re.match('#import\s+<(.*)>\w*',line)
523 if parts and len(parts.groups()) == 1:
524 imported_file = btstack_root+'/src/ble/' + parts.groups()[0]
525 parts = re.match('#import\s+"(.*)"\w*',line)
526 if parts and len(parts.groups()) == 1:
527 imported_file = os.path.abspath(os.path.dirname(fname_in) + '/'+parts.groups()[0])
528 if len(imported_file) == 0:
529 print('ERROR: #import in file %s - line %u neither <name.gatt> nor "name.gatt" form', (fname_in, line_count))
530 continue
531
532 print("Importing %s" % imported_file)
533 try:
534 imported_fin = codecs.open (imported_file, encoding='utf-8')
535 fout.write(' // ' + line + ' -- BEGIN\n')
536 parseLines(imported_file, imported_fin, fout)
537 fout.write(' // ' + line + ' -- END\n')
538 except IOError as e:
539 print('ERROR: Import failed. Please check path.')
540
541 continue
542
543 if line.startswith("#TODO"):
544 print ("WARNING: #TODO in file %s - line %u not handled, skipping declaration:" % (fname_in, line_count))
526 print ("'%s'" % line)
527 fout.write("// " + line + '\n')
528 continue
529
530 if len(line) == 0:
531 continue
532
533 f = io.StringIO(line)

--- 89 unchanged lines hidden (view full) ---

623 continue
624
625 # 2906
626 if parts[0] == 'VALID_RANGE':
627 print("WARNING: %s not implemented yet\n" % (parts[0]))
628 continue
629
630 print("WARNING: unknown token: %s\n" % (parts[0]))
545 print ("'%s'" % line)
546 fout.write("// " + line + '\n')
547 continue
548
549 if len(line) == 0:
550 continue
551
552 f = io.StringIO(line)

--- 89 unchanged lines hidden (view full) ---

642 continue
643
644 # 2906
645 if parts[0] == 'VALID_RANGE':
646 print("WARNING: %s not implemented yet\n" % (parts[0]))
647 continue
648
649 print("WARNING: unknown token: %s\n" % (parts[0]))
650
651def parse(fname_in, fin, fname_out, fout):
652 global handle
653 global total_size
631
654
655 fout.write(header.format(fname_out, fname_in))
656 fout.write('{\n')
657
658 parseLines(fname_in, fin, fout)
659
632 serviceDefinitionComplete(fout)
633 write_indent(fout)
634 fout.write("// END\n");
635 write_indent(fout)
636 write_16(fout,0)
637 fout.write("\n")
638 total_size = total_size + 2
639

--- 40 unchanged lines hidden ---
660 serviceDefinitionComplete(fout)
661 write_indent(fout)
662 fout.write("// END\n");
663 write_indent(fout)
664 write_16(fout,0)
665 fout.write("\n")
666 total_size = total_size + 2
667

--- 40 unchanged lines hidden ---