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