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