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 11meta_events = [ 12 'ANCS', 13 'AVDTP', 14 'A2DP', 15 'AVRCP', 16 'GOEP', 17 'HFP', 18 'HSP', 19 'PBAP', 20 'LE' 21] 22 23supported_event_groups = meta_events + [ 24 'BTSTACK', 25 'GAP', 26 'HCI', 27 'SDP', 28 'SM', 29 'L2CAP', 30 'RFCOMM', 31 'GATT', 32 'BNEP', 33 'ATT', 34] 35 36program_info = ''' 37BTstack Event Getter Generator for BTstack 38Copyright 2016, BlueKitchen GmbH 39''' 40 41copyright = """/* 42 * Copyright (C) 2016 BlueKitchen GmbH 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. Neither the name of the copyright holders nor the names of 54 * contributors may be used to endorse or promote products derived 55 * from this software without specific prior written permission. 56 * 4. Any redistribution, use, or modification is done solely for 57 * personal benefit and not for any commercial purpose or for 58 * monetary gain. 59 * 60 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 62 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 63 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 64 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 65 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 66 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 67 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 68 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 70 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 * 73 * Please inquire about commercial licensing options at 74 * [email protected] 75 * 76 */ 77""" 78 79hfile_header_begin = """ 80 81/* 82 * btstack_event.h 83 * 84 * @brief BTstack event getter/setter 85 * @note Don't edit - generated by tool/btstack_event_generator.py 86 * 87 */ 88 89#ifndef __BTSTACK_EVENT_H 90#define __BTSTACK_EVENT_H 91 92#if defined __cplusplus 93extern "C" { 94#endif 95 96#include "btstack_util.h" 97#include <stdint.h> 98 99#ifdef ENABLE_BLE 100#include "ble/gatt_client.h" 101#endif 102 103/* API_START */ 104 105/** 106 * @brief Get event type 107 * @param event 108 * @return type of event 109 */ 110static inline uint8_t hci_event_packet_get_type(const uint8_t * event){ 111 return event[0]; 112} 113 114""" 115 116hfile_header_end = """ 117 118/* API_END */ 119 120#if defined __cplusplus 121} 122#endif 123 124#endif // __BTSTACK_EVENT_H 125""" 126 127c_prototoype_simple_return = '''/** 128 * @brief {description} 129 * @param event packet 130 * @return {result_name} 131 * @note: btstack_type {format} 132 */ 133static inline {result_type} {fn_name}(const uint8_t * event){{ 134 {code} 135}} 136''' 137 138c_prototoype_struct_return = '''/** 139 * @brief {description} 140 * @param event packet 141 * @param Pointer to storage for {result_name} 142 * @note: btstack_type {format} 143 */ 144static inline void {fn_name}(const uint8_t * event, {result_type} {result_name}){{ 145 {code} 146}} 147''' 148 149c_prototoype_unsupported = '''/** 150 * @brief {description} 151 * @param event packet 152 * @return {result_name} 153 * @note: btstack_type {format} 154 */ 155// static inline {result_type} {fn_name}(const uint8_t * event){{ 156// not implemented yet 157// }} 158''' 159 160meta_event_template = '''/*** 161 * @brief Get subevent code for {meta_event} event 162 * @param event packet 163 * @return subevent_code 164 */ 165static inline uint8_t hci_event_{meta_event}_meta_get_subevent_code(const uint8_t * event){{ 166 return event[2]; 167}} 168''' 169 170# global variables/defines 171# gen_path = '../src/btstack_event.h' 172 173defines = dict() 174defines_used = set() 175 176param_read = { 177 '1' : 'return event[{offset}];', 178 'J' : 'return event[{offset}];', 179 '2' : 'return little_endian_read_16(event, {offset});', 180 'L' : 'return little_endian_read_16(event, {offset});', 181 '3' : 'return little_endian_read_24(event, {offset});', 182 '4' : 'return little_endian_read_32(event, {offset});', 183 'H' : 'return little_endian_read_16(event, {offset});', 184 'B' : 'reverse_bd_addr(&event[{offset}], {result_name});', 185 'R' : 'return &event[{offset}];', 186 'T' : 'return (const char *) &event[{offset}];', 187 'Q' : 'reverse_bytes(&event[{offset}], {result_name}, 32);', 188 'V' : 'return &event[{offset}];', 189 'X' : 'gatt_client_deserialize_service(event, {offset}, {result_name});', 190 'Y' : 'gatt_client_deserialize_characteristic(event, {offset}, {result_name});', 191 'Z' : 'gatt_client_deserialize_characteristic_descriptor(event, {offset}, {result_name});', 192} 193 194def c_type_for_btstack_type(type): 195 param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t', 196 'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *', 197 'R' : 'const uint8_t *', 'S' : 'const uint8_t *', 198 'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID', 199 'Q' : 'uint8_t *', 200 'X' : 'gatt_client_service_t *', 'Y' : 'gatt_client_characteristic_t *', 'Z' : 'gatt_client_characteristic_descriptor_t *', 201 'T' : 'const char *'} 202 return param_types[type] 203 204def size_for_type(type): 205 param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16, 'Q':32, 206 'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1} 207 return param_sizes[type] 208 209def format_function_name(event_name): 210 event_name = event_name.lower() 211 if 'event' in event_name: 212 return event_name; 213 return event_name+'_event' 214 215def template_for_type(field_type): 216 global c_prototoype_simple_return 217 global c_prototoype_struct_return 218 types_with_struct_return = "BQXYZ" 219 if field_type in types_with_struct_return: 220 return c_prototoype_struct_return 221 else: 222 return c_prototoype_simple_return 223 224def all_fields_supported(format): 225 global param_read 226 for f in format: 227 if not f in param_read: 228 return False 229 return True 230 231def create_getter(event_name, field_name, field_type, offset, supported): 232 global c_prototoype_unsupported 233 global param_read 234 235 description = "Get field %s from event %s" % (field_name, event_name.upper()) 236 result_name = field_name 237 fn_name = "%s_get_%s" % (event_name, field_name) 238 result_type = c_type_for_btstack_type(field_type) 239 template = c_prototoype_unsupported 240 code = '' 241 if supported and field_type in param_read: 242 template = template_for_type(field_type) 243 code = param_read[field_type].format(offset=offset, result_name=result_name) 244 return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type) 245 246def is_le_event(event_group): 247 return event_group in ['GATT', 'ANCS', 'SM'] 248 249def create_events(events): 250 global gen_path 251 global copyright 252 global hfile_header_begin 253 global hfile_header_end 254 global meta_event_template 255 256 with open(gen_path, 'wt') as fout: 257 fout.write(copyright) 258 fout.write(hfile_header_begin) 259 260 for meta_event in meta_events: 261 fout.write(meta_event_template.format(meta_event=meta_event.lower())) 262 263 for event_type, event_name, format, args in events: 264 parts = event_name.split("_") 265 event_group = parts[0] 266 if not event_group in supported_event_groups: 267 print("// %s " % event_name) 268 continue 269 # print(event_name) 270 base_name = format_function_name(event_name) 271 length_name = '' 272 offset = 2 273 offset_is_number = 1 274 offset_unknown = 0 275 supported = all_fields_supported(format) 276 last_variable_length_field_pos = "" 277 if is_le_event(event_group): 278 fout.write("#ifdef ENABLE_BLE\n") 279 if len(format) != len(args): 280 print(event_name.upper()) 281 print ("Format %s does not match params %s " % (format, args)) 282 print 283 for f, arg in zip(format, args): 284 field_name = arg 285 if field_name.lower() == 'subevent_code': 286 offset += 1 287 continue 288 if offset_unknown: 289 print("Param after variable length field without preceding 'J' lenght field") 290 break 291 field_type = f 292 text = create_getter(base_name, field_name, field_type, offset, supported) 293 fout.write(text) 294 if field_type in 'RT': 295 break 296 if field_type in 'J': 297 if offset_is_number: 298 last_variable_length_field_pos = '%u' % offset 299 else: 300 last_variable_length_field_pos = offset 301 if field_type in 'V': 302 if last_variable_length_field_pos >= 0: 303 if offset_is_number: 304 # convert to string 305 offset = '%u' % offset 306 offset_is_number = 0 307 offset = offset + ' + event[%s]' % last_variable_length_field_pos 308 else: 309 offset_unknown = 1 310 else: 311 if offset_is_number: 312 offset += size_for_type(field_type) 313 else: 314 offset = offset + ' + %u' % size_for_type(field_type) 315 if is_le_event(event_group): 316 fout.write("#endif\n") 317 fout.write("\n") 318 319 fout.write(hfile_header_end) 320 321btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 322gen_path = btstack_root + '/src/btstack_event.h' 323 324print(program_info) 325 326# parse events 327(events, le_events, event_types) = parser.parse_events() 328 329# create event field accesors 330create_events(events + le_events) 331 332# done 333print('Done!') 334