Merge branch 'feat/add_nimble_log_compression_script_v6.0' into 'release/v6.0'

feat(nimble): Added missing NimBLE log compression scripts (v6.0)

See merge request espressif/esp-idf!48562
This commit is contained in:
Rahul Tank
2026-05-15 15:02:45 +05:30
2 changed files with 97 additions and 1 deletions

View File

@@ -328,10 +328,23 @@ class LogCompressor:
if not fmt_node:
return None
nimble_nodes = False
if fmt_node.type == 'concatenated_string':
log_fmt = self._process_concatenated_string(fmt_node)
elif fmt_node.type == 'string_literal':
log_fmt = fmt_node.text.decode('utf-8')[1:-1] # Remove quotes
elif fmt_node.type == 'identifier':
# NimBLE style: BLE_HS_LOG(level, "fmt", ...)
nimble_nodes = True
fmt_node = valid_arg_childrn[1] if len(valid_arg_childrn) > 1 else None
if not fmt_node:
return None
if fmt_node.type == 'concatenated_string':
log_fmt = self._process_concatenated_string(fmt_node)
elif fmt_node.type == 'string_literal':
log_fmt = fmt_node.text.decode('utf-8')[1:-1]
else:
return None
else:
return None
@@ -354,7 +367,11 @@ class LogCompressor:
log_info['hexify'] = False
return log_info
arguments: list[Node] = valid_arg_childrn[1:]
arguments: list[Node] = []
if nimble_nodes:
arguments = valid_arg_childrn[2:]
else:
arguments = valid_arg_childrn[1:]
if len(arguments) != need_args:
raise SyntaxError(f'LogSyntaxError:{node.text.decode("utf-8")}')
@@ -559,6 +576,30 @@ class LogCompressor:
with open(file_path, 'rb') as f:
content = f.read()
# NimBLE host macros are emitted to nimble_log_index.h (module BLE_HOST when NimBLE is enabled).
# Ensure each compressed NimBLE source includes that header.
if (
module == 'BLE_HOST'
and self.module_info[module].get('log_index_file') == 'nimble_log_index.h'
and b'#include "nimble_log_index.h"' not in content
):
lines = content.splitlines(keepends=True)
first_include = None
last_include = None
for idx, line in enumerate(lines):
if line.lstrip().startswith(b'#include'):
if first_include is None:
first_include = idx
last_include = idx
elif first_include is not None and line.strip() and not line.lstrip().startswith(b'//'):
break
if last_include is not None:
lines.insert(last_include + 1, b'#include "nimble_log_index.h"\n')
content = b''.join(lines)
else:
content = b'#include "nimble_log_index.h"\n' + content
new_content = bytearray(content)
logs = self.extract_log_calls(content, self.module_info[module]['tags'])
LOGGER.info(f'Processing {file_path} - found {len(logs)} logs')
@@ -783,6 +824,10 @@ class LogCompressor:
for module in module_names:
if module in modules:
self.module_info[module] = modules[module]
if module == 'BLE_HOST' and modules[module].get('log_index_file') == 'nimble_log_index.h':
# Force a one-time DB/config refresh for NimBLE compression
# when generator behavior changes (e.g. header injection).
self.module_info[module]['generator_rev'] = 'nimble_include_fix_v1'
module_script_path = self.module_info[module]['script']
spec = self.module_mod[module] = importlib.util.spec_from_file_location(module, module_script_path)
if spec and spec.loader:

View File

@@ -0,0 +1,51 @@
# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import textwrap
def generate_nimble_log_prefix(print_statm: str) -> str:
return f'{{if (MYNEWT_VAL(BLE_HS_LOG_LVL) <= LOG_LEVEL_ ##fmt) {print_statm};}}\\\n'
def gen_header_head() -> str:
head = textwrap.dedent("""
// Compression function declarations
extern int ble_log_compressed_hex_print
(uint8_t source, uint32_t log_index, size_t args_size_cnt, ...);
extern int ble_log_compressed_hex_print_buf
(uint8_t source, uint32_t log_index, uint8_t buf_idx, const uint8_t *buf, size_t len);
""")
return head
def gen_compressed_stmt(
log_index: int,
module_id: int,
func_name: str,
fmt: str,
args: list[dict],
buffer_args: list[dict],
) -> str:
if len(args) == 0:
stmt = f' ble_log_compressed_hex_print({module_id}, {log_index}, 0);'
for idx, buffer_arg in enumerate(buffer_args):
stmt += '\\\n'
stmt += (
f' ble_log_compressed_hex_print_buf('
f'{module_id}, {log_index}, {idx}, '
f'(const uint8_t *){buffer_arg["buffer"]}, {buffer_arg["length"]});'
)
stmt += '\\\n'
return ' ' + generate_nimble_log_prefix(stmt)
size_str = ', '.join([arg['size_type'] for arg in args])
args_str = ', '.join([arg['name'] for arg in args]).replace('\\\n', '').replace('\n', '')
stmt = f' ble_log_compressed_hex_print({module_id}, {log_index}, {len(args)}, {size_str}, {args_str});'
for idx, buffer_arg in enumerate(buffer_args):
stmt += '\\\n'
stmt += (
f' ble_log_compressed_hex_print_buf('
f'{module_id}, {log_index}, {idx}, '
f'(const uint8_t *){buffer_arg["buffer"]}, {buffer_arg["length"]});'
)
stmt += '\\\n'
return ' ' + generate_nimble_log_prefix(stmt)