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