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