chengkai15 opened a new pull request, #3977:
URL: https://github.com/apache/nuttx-apps/pull/3977

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## 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`, default `v4.5.0`) and built without 
modification. The NuttX code is the porting layer and the build files in 
`wireless/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 is open until 9 November 2026, 00:00 UTC. This PR is opened as a 
draft so that review can start; it will be marked ready for review after the 
result.
   
   Roadmap and tracking: apache/nuttx#21478
   
   Previous discussion: #20686 (apache/nuttx), and the [DISCUSS] thread 
https://lists.apache.org/thread/c6pl46o5l1ngb2zb3qtotq38o30h6v4b
   
   ### The three steps
   
   1. Integrate the unmodified Zephyr Bluetooth host in nuttx-apps. The kernel 
only provides the existing HCI transport (`/dev/ttyHCIx`). (this series)
   2. Move HCI core, connection management and L2CAP into the kernel, replacing 
`wireless/bluetooth` and `net/bluetooth`, without losing the existing functions 
and tools.
   3. Make the userspace host use the kernel through the socket interface and 
drop its own copy of these layers.
   
   Each step is a separate patch series, reviewed on its own. Step 1 is split 
into these PRs:
   
   1. framework, LE core, PSA crypto, `sim:zblue` (this PR, and 
apache/nuttx#NUTTX-PR)
   2. SMP, privacy
   3. persistent storage (settings)
   4. bt shell
   5. BR/EDR profiles
   6. targets other than the simulator (iterable RAM sections, see Impact)
   7. BTP tester for AutoPTS, tests in CI
   
   ### Commits
   
   1. **Add Zephyr kernel porting layer**: the Zephyr kernel services used by 
the host on POSIX threads (threads, semaphores, mutexes, condition variables, 
queues, memory slabs, heaps, work queues, timeouts), logging on syslog. Zephyr 
cooperative threads are serialized by one lock that is released only while a 
thread blocks.
   2. **Add HCI driver on /dev/ttyHCIx**: uses the existing 
`bt_driver_register()` / `CONFIG_UART_BTH4` path, so every NuttX Bluetooth 
controller driver can be used. No new driver interface.
   3. **Add Zephyr configuration and syscall tools**: the `CONFIG_BT_*` options 
are the original Zephyr ones, read from a Zephyr configuration fragment 
(`CONFIG_ZBLUE_CONF`) and resolved by the Zephyr Kconfig (kconfiglib from the 
downloaded tree, no extra Python package). They are not added to the NuttX 
Kconfig.
   4. **Add build of the Zephyr Bluetooth host**: `CONFIG_ZBLUE`, Make and 
CMake. Sources are selected in the same way as the upstream `CMakeLists.txt` 
files.
   5. **crypto/mbedtls: Enable the minimal configuration for zblue**: same as 
for NimBLE.
   6. **Add peripheral example**: `zblue_peripheral`.
   
   ### Design notes
   
   - **Other stacks**: `CONFIG_ZBLUE` depends on `!WIRELESS_BLUETOOTH_HOST && 
!NIMBLE`. Nothing changes in the kernel stack or in NimBLE; one host is 
selected at build time.
   - **Naming**: the NuttX switch is `CONFIG_ZBLUE*`, like `CONFIG_NIMBLE`, so 
`CONFIG_BT` is not tied to one implementation. The Zephyr `CONFIG_BT_*` options 
only exist in the generated zblue `autoconf.h`. `CONFIG_BT_ZBLUE` was also 
suggested in #20686; changing the name is easy if reviewers prefer it.
   - **Why NuttX equivalents are not used inside zblue**: the Zephyr code is 
not modified, and some names mean different things. `<nuttx/atomic.h>` defines 
`atomic_t` as `_Atomic int32_t` and `atomic_*()` as macros, while Zephyr 
defines `atomic_t` as `long` and, 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's `net_buf` is used as is for the same reason.
   - **Iterable sections** (`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.
   - **Cryptography** uses the PSA API of `apps/crypto/mbedtls`. The vectors of 
the upstream `tests/bluetooth/bt_crypto` test pass with it (run on the host 
against the same Mbed TLS version, outside this build).
   
   ## Impact
   
   - New optional application, disabled by default. No change for existing 
configurations; the only change outside `wireless/bluetooth/zblue` is one 
`default y if ZBLUE` line in `crypto/mbedtls/Kconfig`.
   - **Simulator only**: `CONFIG_ZBLUE` depends on `ARCH_SIM`. On other targets 
the initialized iterable RAM section of the host (`net_buf` pools) is placed 
after `.data` and 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`).
   - Needs `CONFIG_TLS_NELEM > 0` (thread-specific data in the porting layer) 
and an entropy source (`CONFIG_DEV_URANDOM` on the simulator).
   - Build: downloads the Zephyr release with curl and needs python3, like 
other downloaded applications.
   - Not tested yet: protected and kernel builds.
   - Security: the host runs in userspace. Fixes to the host itself, including 
security fixes, go to Zephyr and are picked up by changing `CONFIG_ZBLUE_REF`.
   - License: the porting layer and build files are new code under the ASF 
license header. No Zephyr file is added to the repository. The downloaded 
sources that are compiled are Apache-2.0; kconfiglib (ISC) is only run as a 
build tool.
   - Documentation: in apache/nuttx#NUTTX-PR 
(`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` (apache/nuttx#NUTTX-PR) with Make and with CMake/Ninja, from 
clean trees of both repositories; `tools/testbuild.sh -N` with the CI entry 
`CMake,sim:zblue`.
   - `sim:zblue` also builds at commits 3, 4 and 5 of the series (commits 1 and 
2 add no build files yet).
   - `tools/checkpatch.sh -g` passes for all commits.
   - nrf52840-dk with the SoftDevice Controller (`sdc_nimble` with NimBLE 
replaced by zblue, before the `ARCH_SIM` dependency was added): builds and 
links; the section map showed the RAM section problem above.
   
   **Run**
   
   `sim:zblue` with a CSR USB controller (0a12:0001) attached with `--bt-dev`, 
and a second controller with BlueZ as central:
   
   ```
   $ sudo hciconfig hci2 down
   $ sudo ./nuttx --bt-dev=hci2
   nsh> zblue_peripheral &
   I: bt_hci_core: HCI transport: HCI
   I: bt_hci_core: Identity: P:00:1A:7D:DA:71:11
   I: bt_hci_core: HCI: version 4.0 (0x06) revision 0x22bb, manufacturer 0x000a
   I: bt_hci_core: LMP: version 4.0 (0x06) subver 0x22bb
   zblue: Bluetooth ready, identity P:00:1A:7D:DA:71:11
   zblue: advertising as "NuttX zblue"
   ```
   
   ```
   $ sudo hcitool -i hci1 lescan
   00:1A:7D:DA:71:11 NuttX zblue
   $ sudo gatttool -i hci1 -b 00:1A:7D:DA:71:11 --primary
   attr handle = 0x0001, end grp handle = 0x0008 uuid: 
00001801-0000-1000-8000-00805f9b34fb
   attr handle = 0x0009, end grp handle = 0x000f uuid: 
00001800-0000-1000-8000-00805f9b34fb
   $ sudo gatttool -i hci1 -b 00:1A:7D:DA:71:11 --char-read --uuid=0x2a00
   handle: 0x000b   value: 4e 75 74 74 58 20 7a 62 6c 75 65
   ```
   
   ```
   zblue: connected P:00:1B:DC:F4:B4:8E
   zblue: disconnected P:00:1B:DC:F4:B4:8E (0x13)
   zblue: advertising as "NuttX zblue"
   ```
   
   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), automated tests in CI (PR 7).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to