Repository navigation
wireless/bluetooth/zblue: Add Zephyr Bluetooth host - #3977
chengkai15 wants to merge 6 commits into
Conversation
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx-apps/actions/runs/42003438673 |
|
@xiaoxiang781216 @acassis @jerpelea @cederom @raiden00pl @simbit18 @btashton @anchao @LingaoM could you review this PR? It is the first PR of step 1 of option 1B in the [VOTE] (https://lists.apache.org/thread/9fy6bog3dcw3do5d8wd0yyvlpkjrhxpn); the board configuration and documentation are in apache/nuttx#21479. The Windows jobs failed on a |
|
@yamt @Ouss4 @masayuki2009 @johannes-nivus @Donny9 @davids5 @antmerlino you were reviewers of apache/nuttx#20686, the earlier proposal for the Zephyr Bluetooth host; your review of this userspace integration is welcome too. |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx-apps/actions/runs/42189036926 |
casaroli
left a comment
There was a problem hiding this comment.
The body says "This PR is opened as a draft so that review can start; it will be marked ready for review after the result.", but the PR is not a draft, so it can merge before the vote closes.
Fix: convert the PR to a draft until the vote result.
acassis
left a comment
There was a problem hiding this comment.
@chengkai15 please analyze the cpu consumption during this busy loop
|
Thanks. The vote closed on 9 November, 00:00 UTC, and 1B passed: https://lists.apache.org/thread/pbkljzzqh0zo3tc7xgdrvh6m86bvpl8v. I have removed that sentence from the description. |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx-apps/actions/runs/42613174780 |
|
Rebased both PRs onto the latest master (apps cf52652, nuttx d9b891b2be), as requested in apache/nuttx#21479. One change was needed in the porting layer. apache/nuttx@2ca5d1c142 ("include/atomic: add atomic_ptr_t type and operations") adds
With a NuttX tree that predates 2ca5d1c142 the change has no effect. The fix is squashed into the first commit ("Add Zephyr kernel porting layer"); the other five commits are unchanged. Verified: |
| * Public Functions | ||
| ****************************************************************************/ | ||
|
|
||
| void k_work_init(struct k_work *work, k_work_handler_t handler) |
There was a problem hiding this comment.
why not reuse work/usrwork
There was a problem hiding this comment.
I looked at it. The NuttX work queue covers part of k_work, but the Zephyr host relies on a few things it doesn't provide:
- Running state. NuttX clears
work->workerbefore calling the worker, so there is no "running" state. Zephyr needs it:k_work_busy_get()reports RUNNING/CANCELING/FLUSHING, and submitting a work item while it runs re-queues it on the same queue and returns 2. - Flush.
k_work_flush()andk_work_flush_delayable()wait for a work item to finish without cancelling it.work_cancel_sync()cancels it first. - Several queues. The host uses the system work queue and its own RX queue at a different priority.
work_queue_create()is only available in the flat build (or in the kernel); outside it, usrwork offers a single queue, so both queues would be merged and their ordering and priority would change.
It could be built on work_queue_create() + work_queue_wq() for the flat build, keeping a flags word in struct k_work for 1 and 2. That removes the thread loop and the delayed-work timer, but the state tracking stays, and zblue would be limited to the flat build. Would you like that in this PR, or as a follow-up after it is merged?
Implement the Zephyr kernel services used by the Zephyr Bluetooth host on POSIX threads, without NuttX internal interfaces, so that the host sources can be built unmodified: - threads: Zephyr cooperative threads are serialized by one lock that is released only while the thread blocks; k_sched_lock() takes the same lock. Zephyr priorities are mapped around CONFIG_ZBLUE_THREAD_PRIORITY. - semaphores, mutexes, condition variables, FIFO/LIFO queues, memory slabs and heaps. - work queues with Zephyr semantics (delayable work, cancel, flush). - timeouts, uptime and cycle counters on CLOCK_MONOTONIC. - logging and printk on syslog, random numbers. - headers that replace or extend the Zephyr ones: kernel.h, devicetree, SYS_INIT() and iterable sections. Iterable section entries go to sections named zblue_<type> and are collected with the linker generated __start_/__stop_ symbols, so no change to the board linker script is needed. - zblue_prefix.h, included first in every Zephyr source, resolves the name clashes between the Zephyr and NuttX headers. The layer is not built yet; the build is added later in this series. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
Provide the Zephyr HCI driver (the devicetree node chosen as zephyr,bt-hci) on a NuttX H4 character device, CONFIG_UART_BTH4, so that any controller registered with bt_driver_register() can be used: the simulator HCI socket, an on-chip controller or a UART controller. A receive thread reads the H4 packet type, header and payload into host buffers and passes them to bt_hci_recv(). Outgoing packets are written with their H4 type. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
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 <chengkai@xiaomi.com>
Add CONFIG_ZBLUE and the Make and CMake builds: - download the Zephyr release CONFIG_ZBLUE_REF (default v4.5.0) from GitHub, check it against CONFIG_ZBLUE_SHA256 and unpack only the directories used by the host; - resolve the Zephyr configuration CONFIG_ZBLUE_CONF and generate the syscall headers; - select the host sources from the resolved options in the same way as the upstream CMakeLists.txt files; - link the objects into one relocatable object. Some of them (e.g. services/gap_svc.c) only add entries to an iterable section and have no symbol referenced from elsewhere, so they would not be pulled out of a static library. Cryptography uses the PSA API of apps/crypto/mbedtls; the options the host needs are selected. The thread-specific data used by the porting layer needs CONFIG_TLS_NELEM. Only the Linux simulator is supported for now: the initialized iterable RAM sections of the host (net_buf pools) are placed after .data and are not yet initialized by the board startup code, and the sections are found through the ELF __start_/__stop_ symbols. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
Default CONFIG_MBEDTLS_MINIMAL to y with CONFIG_ZBLUE, as for NimBLE: the Bluetooth host only needs the PSA algorithms it selects. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
Add zblue_peripheral (CONFIG_ZBLUE_PERIPHERAL_EXAMPLE), written against the unmodified Zephyr Bluetooth API: it advertises with the name CONFIG_BT_DEVICE_NAME, accepts connections with the GAP and GATT services and advertises again after a disconnection. Tested on sim:zblue with a USB Bluetooth controller and a second controller with BlueZ as central: scan, connection, primary service discovery, read of the device name, disconnection and reconnection. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
|
Updated to cbd3f96: addressed the inline comments (details in each reply). |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
CI run: https://github.com/apache/nuttx-apps/actions/runs/43821706918 |
|
The failure in the latest Build run is not related to this PR. (job 133212529201). Other PRs fail on the same config, for example run 43958667026 (152 bytes over) and run 43896803000 (8 bytes over). This PR does not change that image: zblue depends on |
Summary
Add the Bluetooth host of the Zephyr project as an optional userspace stack in nuttx-apps, in the same way as NimBLE. The Zephyr source is downloaded from a Zephyr release (
CONFIG_ZBLUE_REF, defaultv4.5.0) and built without modification. The NuttX code is the porting layer and the build files inwireless/bluetooth/zblue(about 4,200 lines including license headers).This is the first PR of step 1 of option 1B in the [VOTE] on dev@:
https://lists.apache.org/thread/9fy6bog3dcw3do5d8wd0yyvlpkjrhxpn
The vote passed (1B, and 2B for the long-term boundary): https://lists.apache.org/thread/pbkljzzqh0zo3tc7xgdrvh6m86bvpl8v
Roadmap and tracking: apache/nuttx#21478
Previous discussion: #20686 (apache/nuttx), and the [DISCUSS] thread https://lists.apache.org/thread/c6pl46o5l1ngb2zb3qtotq38o30h6v4b
The three steps
/dev/ttyHCIx). (this series)wireless/bluetoothandnet/bluetooth, without losing the existing functions and tools.Each step is a separate patch series, reviewed on its own. Step 1 is split into these PRs:
sim:zblue(this PR, and boards/sim: Add zblue configuration nuttx#21479)Commits
bt_driver_register()/CONFIG_UART_BTH4path, so every NuttX Bluetooth controller driver can be used. No new driver interface.CONFIG_BT_*options are the original Zephyr ones, read from a Zephyr configuration fragment (CONFIG_ZBLUE_CONF) and resolved by the Zephyr Kconfig through the wrapperKconfig.zephyr(kconfiglib from the downloaded tree, no extra Python package). They are not added to the NuttX Kconfig.CONFIG_ZBLUE, Make and CMake. Sources are selected in the same way as the upstreamCMakeLists.txtfiles.zblue_peripheral.Design notes
CONFIG_ZBLUEdepends on!WIRELESS_BLUETOOTH_HOST && !NIMBLE. Nothing changes in the kernel stack or in NimBLE; one host is selected at build time.CONFIG_ZBLUE*, likeCONFIG_NIMBLE, soCONFIG_BTis not tied to one implementation. The ZephyrCONFIG_BT_*options only exist in the generated zblueautoconf.h.CONFIG_BT_ZBLUEwas also suggested in #20686; changing the name is easy if reviewers prefer it.<nuttx/atomic.h>definesatomic_tas_Atomic int32_tandatomic_*()as macros, while Zephyr definesatomic_taslongand, for example,atomic_set()returns the previous value.port/include/zblue_prefix.h, included first in every zblue source, resolves these clashes, and renames the Zephyr library functions that have the same name as libc functions with another signature (crc16(),hex2bin(), ...). Zephyr'snet_bufis used as is for the same reason.BT_CONN_CB_DEFINE(),BT_GATT_SERVICE_DEFINE(), ...) are collected with the linker generated__start_/__stop_symbols, without changes to the board linker scripts. The zblue objects are linked into one relocatable object, because some of them (e.g.services/gap_svc.c) only add section entries and would not be pulled out of a static library.apps/crypto/mbedtls. The vectors of the upstreamtests/bluetooth/bt_cryptotest pass with it (run on the host against the same Mbed TLS version, outside this build).Impact
wireless/bluetooth/zblueis onedefault y if ZBLUEline incrypto/mbedtls/Kconfig.CONFIG_ZBLUEdepends onARCH_SIM && HOST_LINUX(the iterable sections use the ELF__start_/__stop_symbols). On other targets the initialized iterable RAM section of the host (net_bufpools) is placed after.dataand is not initialized by the startup code (found with nrf52840-dk, see Testing). PR 6 adds the linker support, which needs a change in nuttx (include/nuttx/linker).bt_disable(): the HCI driver has noclose(), sobt_disable()returns-ENOSYS, as with upstream drivers withoutclose(). The RX thread waits inread()on/dev/ttyHCIxand cannot be stopped there; disable and enable again come with the shell PR (PR 4).CONFIG_TLS_NELEM > 0(thread-specific data in the porting layer) and an entropy source (CONFIG_DEV_URANDOMon the simulator).CONFIG_ZBLUE_SHA256, and needs python3, like other downloaded applications.CONFIG_ZBLUE_REF.Documentation/applications/wireless/zblue).Testing
Host: Ubuntu 22.04.5 x86_64, gcc 13.1.0, CMake 3.23.0, Python 3.10.12.
Build
sim:zblue(boards/sim: Add zblue configuration nuttx#21479) with Make and with CMake/Ninja, from clean trees of both repositories;tools/testbuild.sh -Nwith the CI entryCMake,sim:zblue.sim:zbluealso builds at commits 3, 4 and 5 of the series (commits 1 and 2 add no build files yet).tools/checkpatch.sh -gpasses for all commits.sdc_nimblewith NimBLE replaced by zblue, before theARCH_SIMdependency was added): builds and links; the section map showed the RAM section problem above.Run
sim:zbluewith a CSR USB controller (0a12:0001) attached with--bt-dev, and a second controller with BlueZ as central:Repeated reads of the device name: 10 with a new connection each time, 10/10; 20 on one connection, 20/20.
Not covered by this PR: pairing (PR 2),
bt_disable()(PR 4), automated tests in CI (PR 7).depends-on: apache/nuttx/pull/21479