xref: /btstack/tool/btstack_event_generator.py (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
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#ifdef ENABLE_BLE
75#include "ble/gatt_client.h"
76#endif
77
78/* API_START */
79
80/**
81 * @brief Get event type
82 * @param event
83 * @return type of event
84 */
85static inline uint8_t hci_event_packet_get_type(const uint8_t * event){
86    return event[0];
87}
88
89"""
90
91hfile_header_end = """
92
93/* API_END */
94
95#if defined __cplusplus
96}
97#endif
98
99#endif // __BTSTACK_EVENT_H
100"""
101
102c_prototoype_simple_return = '''/**
103 * @brief {description}
104 * @param event packet
105 * @return {result_name}
106 * @note: btstack_type {format}
107 */
108static inline {result_type} {fn_name}(const uint8_t * event){{
109    {code}
110}}
111'''
112
113c_prototoype_struct_return = '''/**
114 * @brief {description}
115 * @param event packet
116 * @param Pointer to storage for {result_name}
117 * @note: btstack_type {format}
118 */
119static inline void {fn_name}(const uint8_t * event, {result_type} {result_name}){{
120    {code}
121}}
122'''
123
124c_prototoype_unsupported = '''/**
125 * @brief {description}
126 * @param event packet
127 * @return {result_name}
128 * @note: btstack_type {format}
129 */
130//  static inline {result_type} {fn_name}(const uint8_t * event){{
131//      not implemented yet
132//  }}
133'''
134
135meta_event_template = '''/***
136 * @brief Get subevent code for {meta_event} event
137 * @param event packet
138 * @return subevent_code
139 */
140static inline uint8_t hci_event_{meta_event}_meta_get_subevent_code(const uint8_t * event){{
141    return event[2];
142}}
143'''
144
145# global variables/defines
146# gen_path = '../src/btstack_event.h'
147
148defines = dict()
149defines_used = set()
150
151param_read = {
152    '1' : 'return event[{offset}];',
153    'J' : 'return event[{offset}];',
154    '2' : 'return little_endian_read_16(event, {offset});',
155    'L' : 'return little_endian_read_16(event, {offset});',
156    '3' : 'return little_endian_read_24(event, {offset});',
157    '4' : 'return little_endian_read_32(event, {offset});',
158    'H' : 'return little_endian_read_16(event, {offset});',
159    'B' : 'reverse_bd_addr(&event[{offset}], {result_name});',
160    'R' : 'return &event[{offset}];',
161    'T' : 'return (const char *) &event[{offset}];',
162    'Q' : 'reverse_bytes(&event[{offset}], {result_name}, 32);',
163    'V' : 'return &event[{offset}];',
164    'X' : 'gatt_client_deserialize_service(event, {offset}, {result_name});',
165    'Y' : 'gatt_client_deserialize_characteristic(event, {offset}, {result_name});',
166    'Z' : 'gatt_client_deserialize_characteristic_descriptor(event, {offset}, {result_name});',
167}
168
169def c_type_for_btstack_type(type):
170    param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t',
171                    'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *',
172                    'R' : 'const uint8_t *', 'S' : 'const uint8_t *',
173                    'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID',
174                    'Q' : 'uint8_t *',
175                    'X' : 'gatt_client_service_t *', 'Y' : 'gatt_client_characteristic_t *', 'Z' : 'gatt_client_characteristic_descriptor_t *',
176                    'T' : 'const char *'}
177    return param_types[type]
178
179def size_for_type(type):
180    param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16, 'Q':32,
181                    'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1}
182    return param_sizes[type]
183
184def format_function_name(event_name):
185    event_name = event_name.lower()
186    if 'event' in event_name:
187        return event_name;
188    return event_name+'_event'
189
190def template_for_type(field_type):
191    global c_prototoype_simple_return
192    global c_prototoype_struct_return
193    types_with_struct_return = "BQXYZ"
194    if field_type in types_with_struct_return:
195        return c_prototoype_struct_return
196    else:
197        return c_prototoype_simple_return
198
199def all_fields_supported(format):
200    global param_read
201    for f in format:
202        if not f in param_read:
203            return False
204    return True
205
206def create_getter(event_name, field_name, field_type, offset, supported):
207    global c_prototoype_unsupported
208    global param_read
209
210    description = "Get field %s from event %s" % (field_name, event_name.upper())
211    result_name = field_name
212    fn_name     = "%s_get_%s" % (event_name, field_name)
213    result_type = c_type_for_btstack_type(field_type)
214    template = c_prototoype_unsupported
215    code = ''
216    if supported and field_type in param_read:
217        template = template_for_type(field_type)
218        code = param_read[field_type].format(offset=offset, result_name=result_name)
219    return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type)
220
221def is_le_event(event_group):
222    return event_group in ['GATT', 'ANCS', 'SM']
223
224def create_events(events):
225    global gen_path
226    global copyright
227    global hfile_header_begin
228    global hfile_header_end
229    global meta_event_template
230
231    with open(gen_path, 'wt') as fout:
232        fout.write(copyright)
233        fout.write(hfile_header_begin)
234
235        meta_events = ['HSP', 'HFP', 'ANCS', 'LE'];
236        for meta_event in meta_events:
237            fout.write(meta_event_template.format(meta_event=meta_event.lower()))
238
239        for event_type, event_name, format, args in events:
240            parts = event_name.split("_")
241            event_group = parts[0]
242            if not event_group in [ 'BTSTACK', 'GAP', 'HCI', 'HSP', 'HFP', 'SDP', 'ANCS', 'SM', 'L2CAP', 'RFCOMM', 'GATT', 'BNEP', 'ATT', 'AVDTP']:
243                print("// %s " % event_name)
244                continue
245            print(event_name)
246            event_name = format_function_name(event_name)
247            length_name = ''
248            offset = 2
249            supported = all_fields_supported(format)
250            if is_le_event(event_group):
251                fout.write("#ifdef ENABLE_BLE\n")
252            for f, arg in zip(format, args):
253                field_name = arg
254                if field_name.lower() == 'subevent_code':
255                    offset += 1
256                    continue
257                field_type = f
258                text = create_getter(event_name, field_name, field_type, offset, supported)
259                fout.write(text)
260                if field_type in 'RT':
261                    break
262                offset += size_for_type(field_type)
263            if is_le_event(event_group):
264                fout.write("#endif\n")
265            fout.write("\n")
266
267        fout.write(hfile_header_end)
268
269btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
270gen_path = btstack_root + '/src/btstack_event.h'
271
272print(program_info)
273
274# parse events
275(events, le_events, event_types) = parser.parse_events()
276
277# create event field accesors
278create_events(events + le_events)
279
280# done
281print('Done!')
282