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 11program_info = ''' 12BTstack Event Getter Generator for BTstack 13Copyright 2016, BlueKitchen GmbH 14''' 15 16copyright = """/* 17 * Copyright (C) 2016 BlueKitchen GmbH 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 23 * 1. Redistributions of source code must retain the above copyright 24 * notice, this list of conditions and the following disclaimer. 25 * 2. Redistributions in binary form must reproduce the above copyright 26 * notice, this list of conditions and the following disclaimer in the 27 * documentation and/or other materials provided with the distribution. 28 * 3. Neither the name of the copyright holders nor the names of 29 * contributors may be used to endorse or promote products derived 30 * from this software without specific prior written permission. 31 * 4. Any redistribution, use, or modification is done solely for 32 * personal benefit and not for any commercial purpose or for 33 * monetary gain. 34 * 35 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 39 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 43 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 45 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * Please inquire about commercial licensing options at 49 * [email protected] 50 * 51 */ 52""" 53 54hfile_header_begin = """ 55 56/* 57 * btstack_event.h 58 * 59 * @brief BTstack event getter/setter 60 * @note Don't edit - generated by tool/btstack_event_generator.py 61 * 62 */ 63 64#ifndef __BTSTACK_EVENT_H 65#define __BTSTACK_EVENT_H 66 67#if defined __cplusplus 68extern "C" { 69#endif 70 71#include "btstack_util.h" 72#include <stdint.h> 73 74/* API_START */ 75 76""" 77 78hfile_header_end = """ 79 80/* API_END */ 81 82#if defined __cplusplus 83} 84#endif 85 86#endif // __BTSTACK_EVENT_H 87""" 88 89c_prototoype_simple_return = ''' 90/** 91 * @brief {description} 92 * @param Event packet 93 * @return {result_name} 94 * @note: btstack_type {format} 95 */ 96static inline {result_type} {fn_name}(const uint8_t * event){{ 97 {code} 98}} 99''' 100 101c_prototoype_struct_return = ''' 102/** 103 * @brief {description} 104 * @param Event packet 105 * @param Pointer to storage for {result_name} 106 * @note: btstack_type {format} 107 */ 108static inline void {fn_name}(const uint8_t * event, {result_type} {result_name}){{ 109 {code} 110}} 111''' 112 113c_prototoype_unsupported = ''' 114/** 115 * @brief {description} 116 * @param Event packet 117 * @return {result_name} 118 * @note: btstack_type {format} 119 */ 120// static inline {result_type} {fn_name}(const uint8_t * event){{ 121// not implemented yet 122// }} 123''' 124 125# global variables/defines 126gen_path = '../src/btstack_event.h' 127 128defines = dict() 129defines_used = set() 130 131param_read = { 132 '1' : 'return event[{offset}];', 133 'J' : 'return event[{offset}];', 134 '2' : 'return READ_BT_16(event, {offset});', 135 'L' : 'return READ_BT_16(event, {offset});', 136 '3' : 'return READ_BT_24(event, {offset});', 137 '4' : 'return READ_BT_32(event, {offset});', 138 'H' : 'return READ_BT_16(event, {offset});', 139 'B' : 'swap48(&event[{offset}], {result_name});', 140 'R' : 'return &event[{offset}];', 141 'T' : 'return (const char *) &event[{offset}];', 142 'V' : 'return &event[{offset}];', 143} 144 145def c_type_for_btstack_type(type): 146 param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t', 147 'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *', 148 'R' : 'const uint8_t *', 'S' : 'const uint8_t *', 149 'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID', 150 'X' : 'GATTService', 'Y' : 'GATTCharacteristic', 'Z' : 'GATTCharacteristicDescriptor', 151 'T' : 'const char *'} 152 return param_types[type] 153 154def size_for_type(type): 155 param_sizes = { '1' : 1, '2' : 2, '3' : 3, '4' : 4, 'H' : 2, 'B' : 6, 'D' : 8, 'E' : 240, 'N' : 248, 'P' : 16, 156 'A' : 31, 'S' : -1, 'V': -1, 'J' : 1, 'L' : 2, 'U' : 16, 'X' : 20, 'Y' : 24, 'Z' : 18, 'T':-1} 157 return param_sizes[type] 158 159def format_function_name(event_name): 160 event_name = event_name.lower() 161 if 'event' in event_name: 162 return event_name; 163 return event_name+'_event' 164 165def template_for_type(field_type): 166 global c_prototoype_simple_return 167 global c_prototoype_struct_return 168 types_with_struct_return = "B" 169 if field_type in types_with_struct_return: 170 return c_prototoype_struct_return 171 else: 172 return c_prototoype_simple_return 173 174def all_fields_supported(format): 175 global param_read 176 for f in format: 177 if not f in param_read: 178 return False 179 return True 180 181def create_getter(event_name, field_name, field_type, offset, supported): 182 global c_prototoype_unsupported 183 global param_read 184 185 description = "Get field %s from event %s" % (field_name, event_name) 186 result_name = field_name 187 fn_name = "%s_get_%s" % (event_name, field_name) 188 result_type = c_type_for_btstack_type(field_type) 189 template = c_prototoype_unsupported 190 code = '' 191 if supported and field_type in param_read: 192 template = template_for_type(field_type) 193 code = param_read[field_type].format(offset=offset, result_name=result_name) 194 return template.format(description=description, fn_name=fn_name, result_name=result_name, result_type=result_type, code=code, format=field_type) 195 196def create_events(events): 197 global gen_path 198 global copyright 199 global hfile_header_begin 200 global hfile_header_end 201 202 with open(gen_path, 'wt') as fout: 203 fout.write(copyright) 204 fout.write(hfile_header_begin) 205 for event_type, event_name, format, args in events: 206 if not event_name in [ 207 'SDP_EVENT_QUERY_COMPLETE', 208 'SDP_EVENT_QUERY_RFCOMM_SERVICE', 209 'SDP_EVENT_QUERY_ATTRIBUTE_BYTE', 210 'SDP_EVENT_QUERY_SERVICE_RECORD_HANDLE', 211 'ANCS_EVENT_CLIENT_CONNECTED', 212 'ANCS_EVENT_CLIENT_NOTIFICATION', 213 'ANCS_EVENT_CLIENT_DISCONNECTED']: 214 continue 215 event_name = format_function_name(event_name) 216 length_name = '' 217 offset = 2 218 supported = all_fields_supported(format) 219 for f, arg in zip(format, args): 220 field_name = arg 221 field_type = f 222 text = create_getter(event_name, field_name, field_type, offset, supported) 223 fout.write(text) 224 if field_type in 'RT': 225 break 226 offset += size_for_type(field_type) 227 228 fout.write(hfile_header_end) 229 230# set root 231parser.set_btstack_root('..') 232 233print(program_info) 234 235# parse events 236(events, le_events, event_types) = parser.parse_events() 237 238# create event field accesors 239create_events(events + le_events) 240 241# done 242print('Done!') 243