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 'Q' : 'reverse_bytes(&event[{offset}], {result_name}, 32);', 191 'V' : 'return &event[{offset}];', 192 'X' : 'gatt_client_deserialize_service(event, {offset}, {result_name});', 193 'Y' : 'gatt_client_deserialize_characteristic(event, {offset}, {result_name});', 194 'Z' : 'gatt_client_deserialize_characteristic_descriptor(event, {offset}, {result_name});', 195 'V' : 'return &event[{offset}];', 196} 197 198def c_type_for_btstack_type(type): 199 param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t', 200 'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'const char *' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *', 201 'R' : 'const uint8_t *', 'S' : 'const uint8_t *', 202 'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID', 203 'Q' : 'uint8_t *', 204 'X' : 'gatt_client_service_t *', 'Y' : 'gatt_client_characteristic_t *', 'Z' : 'gatt_client_characteristic_descriptor_t *', 205 'T' : 'const char *'} 206 return param_types[type] 207 208def size_for_type(type): 209 param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16, 'Q':32, 210 'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1} 211 return param_sizes[type] 212 213def format_function_name(event_name): 214 event_name = event_name.lower() 215 if 'event' in event_name: 216 return event_name; 217 return event_name+'_event' 218 219def template_for_type(field_type): 220 global c_prototoype_simple_return 221 global c_prototoype_struct_return 222 types_with_struct_return = "BQXYZ" 223 if field_type in types_with_struct_return: 224 return c_prototoype_struct_return 225 else: 226 return c_prototoype_simple_return 227 228def all_fields_supported(format): 229 global param_read 230 for f in format: 231 if not f in param_read: 232 return False 233 return True 234 235def create_getter(event_name, field_name, field_type, offset, supported): 236 global c_prototoype_unsupported 237 global param_read 238 239 description = "Get field %s from event %s" % (field_name, event_name.upper()) 240 result_name = field_name 241 fn_name = "%s_get_%s" % (event_name, field_name) 242 result_type = c_type_for_btstack_type(field_type) 243 template = c_prototoype_unsupported 244 code = '' 245 if supported and field_type in param_read: 246 template = template_for_type(field_type) 247 code = param_read[field_type].format(offset=offset, result_name=result_name) 248 return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type) 249 250def is_le_event(event_group): 251 return event_group in ['GATT', 'ANCS', 'SM'] 252 253def create_events(events): 254 global gen_path 255 global copyright 256 global hfile_header_begin 257 global hfile_header_end 258 global meta_event_template 259 260 with open(gen_path, 'wt') as fout: 261 fout.write(copyright) 262 fout.write(hfile_header_begin) 263 264 for meta_event in meta_events: 265 fout.write(meta_event_template.format(meta_event=meta_event.lower())) 266 267 for event_type, event_name, format, args in events: 268 parts = event_name.split("_") 269 event_group = parts[0] 270 if not event_group in supported_event_groups: 271 print("// %s " % event_name) 272 continue 273 # print(event_name) 274 base_name = format_function_name(event_name) 275 length_name = '' 276 offset = 2 277 offset_is_number = 1 278 offset_unknown = 0 279 supported = all_fields_supported(format) 280 last_variable_length_field_pos = "" 281 if is_le_event(event_group): 282 fout.write("#ifdef ENABLE_BLE\n") 283 if len(format) != len(args): 284 print(event_name.upper()) 285 print ("Format %s does not match params %s " % (format, args)) 286 print 287 for f, arg in zip(format, args): 288 field_name = arg 289 if field_name.lower() == 'subevent_code': 290 offset += 1 291 continue 292 if offset_unknown: 293 print("Param after variable length field without preceding 'J' lenght field") 294 break 295 field_type = f 296 text = create_getter(base_name, field_name, field_type, offset, supported) 297 fout.write(text) 298 if field_type in 'RT': 299 break 300 if field_type in 'J': 301 if offset_is_number: 302 last_variable_length_field_pos = '%u' % offset 303 else: 304 last_variable_length_field_pos = offset 305 if field_type in 'V': 306 if last_variable_length_field_pos >= 0: 307 if offset_is_number: 308 # convert to string 309 offset = '%u' % offset 310 offset_is_number = 0 311 offset = offset + ' + event[%s]' % last_variable_length_field_pos 312 else: 313 offset_unknown = 1 314 else: 315 if offset_is_number: 316 offset += size_for_type(field_type) 317 else: 318 offset = offset + ' + %u' % size_for_type(field_type) 319 if is_le_event(event_group): 320 fout.write("#endif\n") 321 fout.write("\n") 322 323 fout.write(hfile_header_end) 324 325btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 326gen_path = btstack_root + '/src/btstack_event.h' 327 328print(program_info) 329 330# parse events 331(events, le_events, event_types) = parser.parse_events() 332 333# create event field accesors 334create_events(events + le_events) 335 336# done 337print('Done!') 338