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