xref: /btstack/tool/btstack_event_generator.py (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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    'V' : 'return &event[{offset}];',
163    'X' : 'gatt_client_deserialize_service(event, {offset}, {result_name});',
164    'Y' : 'gatt_client_deserialize_characteristic(event, {offset}, {result_name});',
165    'Z' : 'gatt_client_deserialize_characteristic_descriptor(event, {offset}, {result_name});',
166}
167
168def c_type_for_btstack_type(type):
169    param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t',
170                    'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *',
171                    'R' : 'const uint8_t *', 'S' : 'const uint8_t *',
172                    'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID',
173                    'X' : 'gatt_client_service_t *', 'Y' : 'gatt_client_characteristic_t *', 'Z' : 'gatt_client_characteristic_descriptor_t *',
174                    'T' : 'const char *'}
175    return param_types[type]
176
177def size_for_type(type):
178    param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16,
179                    'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1}
180    return param_sizes[type]
181
182def format_function_name(event_name):
183    event_name = event_name.lower()
184    if 'event' in event_name:
185        return event_name;
186    return event_name+'_event'
187
188def template_for_type(field_type):
189    global c_prototoype_simple_return
190    global c_prototoype_struct_return
191    types_with_struct_return = "BXYZ"
192    if field_type in types_with_struct_return:
193        return c_prototoype_struct_return
194    else:
195        return c_prototoype_simple_return
196
197def all_fields_supported(format):
198    global param_read
199    for f in format:
200        if not f in param_read:
201            return False
202    return True
203
204def create_getter(event_name, field_name, field_type, offset, supported):
205    global c_prototoype_unsupported
206    global param_read
207
208    description = "Get field %s from event %s" % (field_name, event_name.upper())
209    result_name = field_name
210    fn_name     = "%s_get_%s" % (event_name, field_name)
211    result_type = c_type_for_btstack_type(field_type)
212    template = c_prototoype_unsupported
213    code = ''
214    if supported and field_type in param_read:
215        template = template_for_type(field_type)
216        code = param_read[field_type].format(offset=offset, result_name=result_name)
217    return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type)
218
219def is_le_event(event_group):
220    return event_group in ['GATT', 'ANCS', 'SM']
221
222def create_events(events):
223    global gen_path
224    global copyright
225    global hfile_header_begin
226    global hfile_header_end
227    global meta_event_template
228
229    with open(gen_path, 'wt') as fout:
230        fout.write(copyright)
231        fout.write(hfile_header_begin)
232
233        meta_events = ['HSP', 'HFP', 'ANCS', 'LE'];
234        for meta_event in meta_events:
235            fout.write(meta_event_template.format(meta_event=meta_event.lower()))
236
237        for event_type, event_name, format, args in events:
238            parts = event_name.split("_")
239            event_group = parts[0]
240            if not event_group in [ 'BTSTACK', 'GAP', 'HCI', 'HSP', 'HFP', 'SDP', 'ANCS', 'SM', 'L2CAP', 'RFCOMM', 'GATT', 'BNEP', 'ATT']:
241                print("// %s " % event_name)
242                continue
243            print(event_name)
244            event_name = format_function_name(event_name)
245            length_name = ''
246            offset = 2
247            supported = all_fields_supported(format)
248            if is_le_event(event_group):
249                fout.write("#ifdef ENABLE_BLE\n")
250            for f, arg in zip(format, args):
251                field_name = arg
252                if field_name.lower() == 'subevent_code':
253                    offset += 1
254                    continue
255                field_type = f
256                text = create_getter(event_name, field_name, field_type, offset, supported)
257                fout.write(text)
258                if field_type in 'RT':
259                    break
260                offset += size_for_type(field_type)
261            if is_le_event(event_group):
262                fout.write("#endif\n")
263            fout.write("\n")
264
265        fout.write(hfile_header_end)
266
267btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
268gen_path = btstack_root + '/src/btstack_event.h'
269
270print(program_info)
271
272# parse events
273(events, le_events, event_types) = parser.parse_events()
274
275# create event field accesors
276create_events(events + le_events)
277
278# done
279print('Done!')
280