xref: /btstack/tool/btstack_event_generator.py (revision 2044a9b9ae072d40605300dae349cd05c5194e66)
1#!/usr/bin/env python
2# BlueKitchen GmbH (c) 2014
3
4import glob
5import re
6import sys
7import os
8
9import btstack_parser as parser
10
11program_info = '''
12BTstack Event Getter Generator for BTstack
13Copyright 2016, BlueKitchen GmbH
14'''
15
16copyright = """/*
17 * Copyright (C) 2016 BlueKitchen GmbH
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright
24 *    notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 *    notice, this list of conditions and the following disclaimer in the
27 *    documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the copyright holders nor the names of
29 *    contributors may be used to endorse or promote products derived
30 *    from this software without specific prior written permission.
31 * 4. Any redistribution, use, or modification is done solely for
32 *    personal benefit and not for any commercial purpose or for
33 *    monetary gain.
34 *
35 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
39 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
43 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
45 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * Please inquire about commercial licensing options at
49 * [email protected]
50 *
51 */
52"""
53
54hfile_header_begin = """
55
56/*
57 *  btstack_event.h
58 *
59 *  @brief BTstack event getter/setter
60 *  @note  Don't edit - generated by tool/btstack_event_generator.py
61 *
62 */
63
64#ifndef __BTSTACK_EVENT_H
65#define __BTSTACK_EVENT_H
66
67#if defined __cplusplus
68extern "C" {
69#endif
70
71#include "btstack_util.h"
72#include <stdint.h>
73
74/* API_START */
75
76"""
77
78hfile_header_end = """
79
80/* API_END */
81
82#if defined __cplusplus
83}
84#endif
85
86#endif // __BTSTACK_EVENT_H
87"""
88
89c_prototoype_simple_return = '''
90/**
91 * @brief {description}
92 * @param Event packet
93 * @return {result_name}
94 * @note: btstack_type {format}
95 */
96static inline {result_type} {fn_name}(const uint8_t * event){{
97    {code}
98}}
99'''
100
101c_prototoype_struct_return = '''
102/**
103 * @brief {description}
104 * @param Event packet
105 * @param Pointer to storage for {result_name}
106 * @note: btstack_type {format}
107 */
108static inline void {fn_name}(const uint8_t * event, {result_type} {result_name}){{
109    {code}
110}}
111'''
112
113c_prototoype_unsupported = '''
114/**
115 * @brief {description}
116 * @param Event packet
117 * @return {result_name}
118 * @note: btstack_type {format}
119 */
120//  static inline {result_type} {fn_name}(const uint8_t * event){{
121//      not implemented yet
122//  }}
123'''
124
125# global variables/defines
126gen_path = '../src/btstack_event.h'
127
128defines = dict()
129defines_used = set()
130
131param_read = {
132    '1' : 'return event[{offset}];',
133    'J' : 'return event[{offset}];',
134    'L' : 'return event[{offset}];',
135    '2' : 'return READ_BT_16(event, {offset});',
136    '3' : 'return READ_BT_24(event, {offset});',
137    '4' : 'return READ_BT_32(event, {offset});',
138    'H' : 'return READ_BT_16(event, {offset});',
139    'B' : 'bt_flip_addr({result_name}, (uint8_t*) &event[{offset}]);',
140    'R' : 'return &event[{offset}];'
141}
142
143def c_type_for_btstack_type(type):
144    param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t',
145                    'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *',
146                    'R' : 'const uint8_t *', 'S' : 'const uint8_t *',
147                    'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID',
148                    'X' : 'GATTService', 'Y' : 'GATTCharacteristic', 'Z' : 'GATTCharacteristicDescriptor',
149                    'T' : 'String'}
150    return param_types[type]
151
152def size_for_type(type):
153    param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16,
154                    'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1}
155    return param_sizes[type]
156
157def format_function_name(event_name):
158    return event_name.lower()
159
160def template_for_type(field_type):
161    global c_prototoype_simple_return
162    global c_prototoype_struct_return
163    types_with_struct_return = "B"
164    if field_type in types_with_struct_return:
165        return c_prototoype_struct_return
166    else:
167        return c_prototoype_simple_return
168
169def all_fields_supported(format):
170    global param_read
171    for f in format:
172        if not f in param_read:
173            return False
174    return True
175
176def create_getter(event_name, field_name, field_type, offset, supported):
177    global c_prototoype_unsupported
178    global param_read
179
180    description = "Get field %s from event %s" % (field_name, event_name)
181    result_name = field_name
182    fn_name     = "%s_get_%s" % (event_name, field_name)
183    result_type = c_type_for_btstack_type(field_type)
184    template = c_prototoype_unsupported
185    code = ''
186    if supported and field_type in param_read:
187        template = template_for_type(field_type)
188        code = param_read[field_type].format(offset=offset, result_name=result_name)
189    return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type)
190
191def create_events(events):
192    global gen_path
193    global copyright
194    global hfile_header_begin
195    global hfile_header_end
196
197    with open(gen_path, 'wt') as fout:
198        fout.write(copyright)
199        fout.write(hfile_header_begin)
200        for event_type, event_name, format, args in events:
201            event_name = format_function_name(event_name)
202            offset = 2
203            supported = all_fields_supported(format)
204            for f, arg in zip(format, args):
205                field_name = arg
206                field_type = f
207                if field_type == 'V':
208                    break
209                text = create_getter(event_name, field_name, field_type, offset, supported)
210                fout.write(text)
211                if field_type == 'R':
212                    break
213                offset += size_for_type(field_type)
214
215        fout.write(hfile_header_end)
216
217# set root
218parser.set_btstack_root('..')
219
220print(program_info)
221
222# parse events
223(events, le_events, event_types) = parser.parse_events()
224
225# create event field accesors
226create_events(events + le_events)
227
228# done
229print('Done!')
230