This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit acda401aad493fa1c8d1b3518e7eae339f8494fc Author: Kai Cheng <[email protected]> AuthorDate: Thu Nov 5 14:14:47 2026 +0800 wireless/bluetooth/zblue: Add Zephyr configuration and syscall tools The Bluetooth host is configured with its own Zephyr CONFIG_BT_* options, kept out of the NuttX Kconfig namespace: - zblue.conf: default Zephyr configuration fragment (LE peripheral and central, GATT client). - Kconfig.zephyr: wrapper that sources the Zephyr Bluetooth Kconfig and defines the Zephyr kernel options the host depends on. It is not named Kconfig so that it stays out of the NuttX menuconfig. - tools/zkconfig.py: resolves the fragment with the Zephyr Kconfig (kconfiglib from the downloaded Zephyr tree), adds the architecture and endianness from the NuttX configuration, and writes autoconf.h and a make/cmake includable list of the enabled options. - tools/gen_syscalls.py: generates the Zephyr syscall headers as direct calls to the implementations (no user mode). Signed-off-by: Kai Cheng <[email protected]> --- wireless/bluetooth/zblue/Kconfig.zephyr | 118 +++++++++++++++++++ wireless/bluetooth/zblue/tools/gen_syscalls.py | 106 +++++++++++++++++ wireless/bluetooth/zblue/tools/zkconfig.py | 156 +++++++++++++++++++++++++ wireless/bluetooth/zblue/zblue.conf | 9 ++ 4 files changed, 389 insertions(+) diff --git a/wireless/bluetooth/zblue/Kconfig.zephyr b/wireless/bluetooth/zblue/Kconfig.zephyr new file mode 100644 index 000000000..1b7614fd2 --- /dev/null +++ b/wireless/bluetooth/zblue/Kconfig.zephyr @@ -0,0 +1,118 @@ +# Zephyr-side configuration of the zblue Bluetooth host. +# +# This file is not part of the NuttX menuconfig (it is not named Kconfig, +# and a kconfig/ directory next to Kconfig would clash on file systems that +# ignore case). tools/zkconfig.py resolves +# it with the kconfiglib shipped in the downloaded Zephyr tree, using the +# CONFIG_BT_* fragment selected by CONFIG_ZBLUE_CONF, and writes the result +# to a separate autoconf.h used only by zblue. +# +# The symbols below describe what the NuttX port provides. They exist only +# so that the upstream Kconfig dependencies resolve; their values are fixed. + +mainmenu "zblue (Zephyr Bluetooth host) configuration" + +config ZBLUE_PORT + def_bool y + select NET_BUF + select ATOMIC_OPERATIONS_BUILTIN + +config ATOMIC_OPERATIONS_BUILTIN + bool + +config MULTITHREADING + def_bool y + +config PRINTK + def_bool y + +config ASSERT + bool "Enable __ASSERT() checks" + +# subsys/debug/Kconfig is not sourced: __ASSERT() checks the level of the +# DEFAULT module, which is verbose when ASSERT is set. + +config ASSERT_MODULE_DEFAULT_LEVEL + int + default 3 if ASSERT + default 0 + +config LOG + bool "Logging" + +config LOG_DEFAULT_LEVEL + int "Default log level" + default 3 + range 0 4 + +config LOG_MAX_LEVEL + int + default 4 + +config SHELL + bool "Shell" + +config SETTINGS + bool "Settings" + +config CRC + def_bool y + +config UUID + bool + +config PSA_CRYPTO + bool + +config PSA_CRYPTO_PROVIDER_MBEDTLS + def_bool y + +config MBEDTLS_PROMPTLESS + def_bool y + +config NUM_PREEMPT_PRIORITIES + int + default 15 + +config NUM_COOP_PRIORITIES + int + default 16 + +config SYSTEM_WORKQUEUE_STACK_SIZE + int "System work queue stack size" + default 2048 + +config SYSTEM_WORKQUEUE_PRIORITY + int + default -1 + +config MAIN_STACK_SIZE + int + default 4096 + +config ZEPHYR_BT_HCI + string + default "" + +config SYS_CLOCK_MAX_TIMEOUT_DAYS + int + default 365 + +config SYS_CLOCK_EXISTS + def_bool y + +config TIMEOUT_64BIT + def_bool y + +config SYS_CLOCK_TICKS_PER_SEC + int + default 1000 + +config SYS_CLOCK_HW_CYCLES_PER_SEC + int + default 1000000 + +source "$(ZEPHYR_BASE)/Kconfig.constants" +source "$(ZEPHYR_BASE)/modules/mbedtls/Kconfig.psa.auto" +source "$(ZEPHYR_BASE)/lib/net_buf/Kconfig" +source "$(ZEPHYR_BASE)/subsys/bluetooth/Kconfig" diff --git a/wireless/bluetooth/zblue/tools/gen_syscalls.py b/wireless/bluetooth/zblue/tools/gen_syscalls.py new file mode 100644 index 000000000..bb6eeb0ce --- /dev/null +++ b/wireless/bluetooth/zblue/tools/gen_syscalls.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +############################################################################ +# apps/wireless/bluetooth/zblue/tools/gen_syscalls.py +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +"""Generate <zephyr/syscalls/*.h> for a build without CONFIG_USERSPACE. + +Upstream headers declare system calls as "__syscall T f(args);" and include +a generated <zephyr/syscalls/NAME.h> that defines f() to call z_impl_f(). +zblue runs in a single address space with no system call layer, so each +generated wrapper is just that call; the port implements z_impl_f(). +""" + +import os +import re +import sys + +PROTO = re.compile(r"__syscall\s+([^;{]*?)\b(\w+)\s*\(([^;{]*?)\)\s*;", re.S) +INCLUDE = re.compile(r"#\s*include\s*<zephyr/syscalls/(\w+)\.h>") + + +def arg_name(arg): + arg = arg.strip() + m = re.search(r"\(\s*\*\s*(\w+)\s*\)", arg) + if m: + return m.group(1) + m = re.search(r"(\w+)\s*(\[[^\]]*\])?$", arg) + return m.group(1) + + +def split_args(args): + out, depth, cur = [], 0, "" + for c in args: + if c == "," and depth == 0: + out.append(cur) + cur = "" + continue + depth += c == "(" + depth -= c == ")" + cur += c + if cur.strip(): + out.append(cur) + return out + + +def wrapper(ret, name, args): + ret = " ".join(ret.split()) + args = " ".join(args.split()) + params = [] if args in ("", "void") else split_args(args) + call = ", ".join(arg_name(a) for a in params) + body = "" if ret == "void" else "return " + return ( + "extern %s z_impl_%s(%s);\n" + "static inline %s %s(%s)\n{\n\t%sz_impl_%s(%s);\n}\n\n" + % (ret, name, args or "void", ret, name, args or "void", body, name, call) + ) + + +def main(): + incdir, outdir = sys.argv[1], sys.argv[2] + os.makedirs(outdir, exist_ok=True) + for root, _, files in os.walk(incdir): + for fn in files: + if not fn.endswith(".h"): + continue + text = open(os.path.join(root, fn), errors="ignore").read() + m = INCLUDE.search(text) + if not m: + continue + nocomment = re.sub(r"/\*.*?\*/", "", text, flags=re.S) + guard = "Z_ZBLUE_SYSCALLS_%s_H" % m.group(1).upper() + out = [ + "/* Generated by tools/gen_syscalls.py, do not edit */\n", + "#ifndef %s\n#define %s\n\n" % (guard, guard), + '#ifdef __cplusplus\nextern "C" {\n#endif\n\n', + ] + for p in PROTO.finditer(nocomment): + out.append(wrapper(*p.groups())) + out.append("#ifdef __cplusplus\n}\n#endif\n\n#endif\n") + path = os.path.join(outdir, m.group(1) + ".h") + new = "".join(out) + if not os.path.exists(path) or open(path).read() != new: + with open(path, "w") as f: + f.write(new) + + +if __name__ == "__main__": + main() diff --git a/wireless/bluetooth/zblue/tools/zkconfig.py b/wireless/bluetooth/zblue/tools/zkconfig.py new file mode 100644 index 000000000..fe97142e9 --- /dev/null +++ b/wireless/bluetooth/zblue/tools/zkconfig.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python3 +############################################################################ +# apps/wireless/bluetooth/zblue/tools/zkconfig.py +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +"""Resolve the Zephyr Bluetooth Kconfig for zblue. + +Reads the CONFIG_BT_* fragment(s), resolves them against the upstream +Kconfig with Zephyr's own kconfiglib and writes: + + <outdir>/zephyr/autoconf.h C macros, used with -imacros + <outdir>/zblue.config the same values, for the Makefile + +Zephyr symbols that also exist in the NuttX .config are dropped from the +output: both headers are visible in every zblue translation unit, and the +NuttX value must win. +""" + +import argparse +import os +import re +import sys + + +def nuttx_config(path): + syms = {} + with open(path) as f: + for line in f: + m = re.match(r"^CONFIG_([A-Za-z0-9_]+)=(.*)$", line.strip()) + if m: + syms[m.group(1)] = m.group(2) + return syms + + +def arch_facts(nx): + """Macros that upstream headers use to select architecture code.""" + + facts = {"ATOMIC_OPERATIONS_BUILTIN": "1"} + if "ENDIAN_BIG" in nx: + facts["BIG_ENDIAN"] = "1" + else: + facts["LITTLE_ENDIAN"] = "1" + if "ARCH_SIM" in nx: + if "HOST_X86_64" in nx and "SIM_M32" not in nx: + facts.update({"X86": "1", "64BIT": "1"}) + elif "HOST_ARM64" in nx: + facts.update({"ARM64": "1", "64BIT": "1"}) + else: + facts["X86"] = "1" + elif "ARCH_ARM" in nx: + facts["ARM"] = "1" + elif "ARCH_ARM64" in nx: + facts.update({"ARM64": "1", "64BIT": "1"}) + elif "ARCH_RISCV" in nx: + facts["RISCV"] = "1" + if "ARCH_RV64" in nx: + facts["64BIT"] = "1" + elif "ARCH_XTENSA" in nx: + facts["XTENSA"] = "1" + else: + sys.exit("zkconfig: unsupported architecture") + return facts + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--zephyr", required=True, help="Zephyr source tree") + ap.add_argument("--kconfig", required=True, help="wrapper Kconfig") + ap.add_argument("--nuttx-config", required=True, help="NuttX .config") + ap.add_argument("--outdir", required=True) + ap.add_argument("fragments", nargs="+", help="CONFIG_BT_* fragments") + args = ap.parse_args() + + zbase = os.path.abspath(args.zephyr) + os.environ["ZEPHYR_BASE"] = zbase + os.environ["srctree"] = zbase + os.environ.setdefault("KCONFIG_WARN_UNDEF_ASSIGN", "y") + sys.path.insert(0, os.path.join(zbase, "scripts", "kconfig")) + import kconfiglib + + kconf = kconfiglib.Kconfig(os.path.abspath(args.kconfig), warn_to_stderr=False) + kconf.load_config(args.fragments[0]) + for frag in args.fragments[1:]: + kconf.load_config(frag, replace=False) + + for w in kconf.warnings: + if "was assigned the value" in w or "undefined symbol" in w: + print("zkconfig: " + w.splitlines()[0], file=sys.stderr) + + nx = nuttx_config(args.nuttx_config) + values = {} + for sym in kconf.unique_defined_syms: + val = sym.str_value + if not sym._write_to_conf: + continue + if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE): + if val == "n": + continue + values[sym.name] = "1" + elif sym.orig_type == kconfiglib.STRING: + values[sym.name] = '"%s"' % kconfiglib.escape(val) + elif sym.orig_type == kconfiglib.HEX and not val.startswith("0x"): + values[sym.name] = "0x" + val + else: + values[sym.name] = val + + values.update(arch_facts(nx)) + + clashes = sorted(n for n in values if n in nx) + for n in clashes: + print("zkconfig: dropping CONFIG_%s, defined by NuttX" % n, file=sys.stderr) + del values[n] + + incdir = os.path.join(args.outdir, "zephyr") + os.makedirs(incdir, exist_ok=True) + hdr = os.path.join(incdir, "autoconf.h") + mk = os.path.join(args.outdir, "zblue.config") + + with open(hdr + ".tmp", "w") as f: + f.write("/* Generated by tools/zkconfig.py, do not edit */\n") + for n in sorted(values): + f.write("#define CONFIG_%s %s\n" % (n, values[n])) + with open(mk + ".tmp", "w") as f: + f.write("# Generated by tools/zkconfig.py, do not edit\n") + for n in sorted(values): + v = "y" if values[n] == "1" else values[n] + f.write("ZCONFIG_%s=%s\n" % (n, v)) + + for path in (hdr, mk): + new = open(path + ".tmp").read() + if not os.path.exists(path) or open(path).read() != new: + os.replace(path + ".tmp", path) + else: + os.remove(path + ".tmp") + + +if __name__ == "__main__": + main() diff --git a/wireless/bluetooth/zblue/zblue.conf b/wireless/bluetooth/zblue/zblue.conf new file mode 100644 index 000000000..c2cd7cc64 --- /dev/null +++ b/wireless/bluetooth/zblue/zblue.conf @@ -0,0 +1,9 @@ +# Default Zephyr Bluetooth configuration for zblue (CONFIG_ZBLUE_CONF). +# Any upstream CONFIG_BT_* option can be set here. + +CONFIG_BT=y +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_CENTRAL=y +CONFIG_BT_GATT_CLIENT=y +CONFIG_BT_DEVICE_NAME="NuttX zblue" +CONFIG_LOG=y
