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