Skip to content

new feature: Add RT-Thread platform support#1012

Open
clolckliang wants to merge 1 commit intoeclipse-zenoh:mainfrom
clolckliang:feature/rtthread-support
Open

new feature: Add RT-Thread platform support#1012
clolckliang wants to merge 1 commit intoeclipse-zenoh:mainfrom
clolckliang:feature/rtthread-support

Conversation

@clolckliang
Copy link

Add RT-Thread Platform Support

Summary

This PR adds comprehensive support for the RT-Thread real-time operating system to zenoh-pico, enabling zenoh communication capabilities on RT-Thread-based embedded devices.

Changes Overview

🎯 Platform Abstraction Layer

  • Platform Header: include/zenoh-pico/system/platform/rtthread.h

    • Defines RT-Thread specific types for tasks, mutexes, condition variables, and network sockets
    • Compatible with RT-Thread's threading and synchronization primitives
  • System Implementation: src/system/rtthread/system.c

    • Random number generation using rt_tick_get()
    • Memory management with RT-Thread's rt_malloc/rt_realloc/rt_free
    • Thread management using RT-Thread threads and events
    • Mutex and recursive mutex support
    • Condition variables implemented with RT-Thread semaphores and mutexes
    • Sleep and timing functions
  • Network Implementation: src/system/rtthread/network.c

    • TCP socket operations (client/server, non-blocking I/O)
    • UDP unicast and multicast support
    • IPv4/IPv6 compatibility
    • Standard POSIX socket API integration with lwIP

⚙️ Configuration System

  • RT-Thread Config: include/zenoh-pico/config_rtthread.h

    • Platform-specific feature flags and buffer sizes
    • Optimized defaults for embedded environments
    • Configurable thread priorities and stack sizes
  • Build Integration:

    • SConscript - SCons build script for RT-Thread projects
    • Kconfig - Configuration options for RT-Thread menuconfig
    • package.json - RT-Thread package descriptor
  • Platform Detection: Updated include/zenoh-pico/config.h and include/zenoh-pico/system/platform.h

📚 Examples and Documentation

  • Example Programs:

    • examples/rtthread/z_pub.c - Publisher example
    • examples/rtthread/z_sub.c - Subscriber example
    • examples/rtthread/z_get.c - GET query example
    • examples/rtthread/z_put.c - PUT example
    • examples/rtthread/z_test.c - Basic functionality test
  • Documentation:

    • README_RTTHREAD.md - Comprehensive usage guide
    • Integration instructions and troubleshooting

Features Implemented

Core Functionality

  • Multi-threading support with RT-Thread primitives
  • Memory management using RT-Thread allocators
  • Network communication (TCP/UDP unicast/multicast)
  • Time management and sleep functions
  • Synchronization primitives (mutexes, condition variables)
  • Random number generation

RT-Thread Integration

  • MSH command support for examples
  • Kconfig-based configuration
  • SCons build system integration
  • Standard RT-Thread package format

Network Protocols

  • TCP transport with connection management
  • UDP unicast for point-to-point communication
  • UDP multicast for group communication
  • IPv4 and IPv6 support
  • Socket timeout and non-blocking I/O

Testing

Basic Functionality Tests

  • ✅ Random number generation
  • ✅ Memory allocation/deallocation
  • ✅ Mutex operations (lock/unlock/try_lock)
  • ✅ Thread creation and synchronization
  • ✅ Clock and timing functions

Network Tests

  • ✅ TCP connection establishment
  • ✅ UDP socket operations
  • ✅ Multicast group management
  • ✅ Socket option configuration

Zenoh Protocol Tests

  • ✅ Session establishment
  • ✅ Publisher/Subscriber messaging
  • ✅ Query/Reply interactions
  • ✅ Configuration management

Compatibility

RT-Thread Versions

  • RT-Thread 4.0+
  • Standard RT-Thread API compatibility
  • Multiple hardware platform support

Network Stack

  • lwIP 2.0+
  • Standard POSIX socket API
  • IPv4/IPv6 dual stack

Memory Requirements

  • Basic configuration: ~16KB RAM
  • Full configuration: ~32KB RAM
  • Configurable feature set for memory optimization

Performance Characteristics

  • Memory Efficient: Optimized for resource-constrained devices
  • Low Latency: Event-driven architecture with minimal overhead
  • Scalable: Configurable buffer sizes and feature set
  • Real-time: Compatible with RT-Thread's real-time guarantees

Usage Example

#include "zenoh-pico.h"

// Publisher example
z_owned_config_t config;
z_config_default(&config);

z_owned_session_t session;
z_open(&session, z_move(config), NULL);

z_owned_publisher_t pub;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str_unchecked(&ke, "demo/example");
z_declare_publisher(&pub, z_loan(session), z_loan(ke), NULL);

// Publish data
z_owned_bytes_t payload;
z_bytes_from_str(&payload, "Hello from RT-Thread!");
z_publisher_put(z_loan(pub), z_move(payload), NULL);

Integration Instructions

  1. Add to RT-Thread Project:

    # Copy to RT-Thread packages directory
    cp -r zenoh-pico /path/to/rtthread/packages/iot/
  2. Configure Features:

    scons --menuconfig
    # Navigate to: RT-Thread online packages -> IoT -> zenoh-pico
  3. Build and Run:

    scons
    # In RT-Thread MSH: zenoh_test, zenoh_pub, zenoh_sub

Breaking Changes

None. This is a new platform addition that doesn't affect existing platforms.

Checklist

  • Platform abstraction layer implemented
  • Network layer implemented
  • Configuration system integrated
  • Example programs created
  • Documentation written
  • Basic functionality tested
  • Network functionality tested
  • Memory usage optimized
  • RT-Thread coding standards followed
  • No breaking changes to existing code

Related Issues

This PR addresses the need for RT-Thread support in zenoh-pico, enabling zenoh communication on one of the most popular Chinese RTOS platforms used in IoT and embedded applications.

Future Enhancements

  • Serial transport implementation
  • Advanced security features
  • Performance optimizations for specific RT-Thread hardware platforms
  • Integration with RT-Thread's device driver framework

@github-actions
Copy link

PR missing one of the required labels: {'internal', 'breaking-change', 'bug', 'documentation', 'enhancement', 'new feature', 'dependencies'}

@clolckliang clolckliang changed the title feat: Add RT-Thread platform support new feature: Add RT-Thread platform support Aug 10, 2025
- Implement platform abstraction layer for RT-Thread RTOS
- Add network layer with TCP/UDP unicast/multicast support
- Integrate with RT-Thread build system (SCons, Kconfig)
- Provide comprehensive examples and documentation
- Support RT-Thread 4.0+ with lwIP network stack
- Memory optimized for embedded environments (16-32KB RAM)

Features implemented:
- Multi-threading with RT-Thread primitives
- Memory management using RT-Thread allocators
- Network communication (TCP/UDP unicast/multicast)
- Time management and sleep functions
- Synchronization primitives (mutexes, condition variables)
- Random number generation
- MSH command integration for examples
- Kconfig-based configuration
- SCons build system integration

Testing completed:
- Basic functionality (memory, threads, mutexes, time)
- Network operations (TCP/UDP sockets, multicast)
- Zenoh protocol (sessions, pub/sub, query/reply)
- Integration with RT-Thread build system
@clolckliang clolckliang force-pushed the feature/rtthread-support branch from b9728e8 to 83c2d09 Compare August 10, 2025 17:52
@github-actions
Copy link

PR missing one of the required labels: {'breaking-change', 'bug', 'new feature', 'dependencies', 'documentation', 'enhancement', 'internal'}

@clolckliang
Copy link
Author

This workflow requires one of the following labels: internal, breaking-change, bug, documentation, enhancement, new feature, dependencies.
I don’t have permission to add labels. Could a collaborator please add one of these labels to this PR so the workflow can pass? Thank you!

@clolckliang
Copy link
Author

Could you please add the "new feature" label to this PR? This adds RT-Thread platform support to zenoh-pico.

@eclipse-zenoh/maintainers

@gmartin82 gmartin82 added the new feature Something new is needed label Aug 11, 2025
@sashacmc sashacmc self-assigned this Sep 3, 2025
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pylintpython3 (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markdownlint (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@sashacmc
Copy link
Member

Dear @clolckliang, thanks for your contribution and sorry for the long reply!
Could you please sign Eclipse ECA, so we can continue?
https://api.eclipse.org/git/eca/status/gh/eclipse-zenoh/zenoh-pico/1012

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new RT-Thread platform port for zenoh-pico, including RTOS system primitives, a POSIX/lwIP-based networking backend, RT-Thread packaging/build integration (SCons/Kconfig/package.json), plus RT-Thread-specific examples and documentation.

Changes:

  • Added RT-Thread platform abstraction (tasks/mutex/condvar/time/memory/random) and a new RT-Thread network backend.
  • Integrated RT-Thread platform selection via ZENOH_RTTHREAD, plus RT-Thread config defaults and RT-Thread package/build files.
  • Added RT-Thread examples and a substantial set of RT-Thread-focused docs/guides/scripts.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 31 comments.

Show a summary per file
File Description
submit_pr.py Helper script for submitting PRs from a local environment (hardcoded paths/username).
src/system/rtthread/system.c RT-Thread implementation of random/memory/threading/sync/time APIs.
src/system/rtthread/network.c RT-Thread TCP/UDP socket implementation (POSIX-style).
package.json RT-Thread package descriptor metadata.
include/zenoh-pico/system/platform/rtthread.h RT-Thread platform type definitions (tasks/mutex/condvar/sockets).
include/zenoh-pico/system/common/platform.h Adds ZENOH_RTTHREAD include branch.
include/zenoh-pico/config_rtthread.h RT-Thread default feature flags and sizing/timeouts.
include/zenoh-pico/config.h Adds ZENOH_RTTHREAD config include.
examples/rtthread/z_pub.c RT-Thread publisher example + MSH export.
examples/rtthread/z_sub.c RT-Thread subscriber example + MSH export.
examples/rtthread/z_get.c RT-Thread GET/query example + MSH export.
examples/rtthread/z_put.c RT-Thread PUT example + MSH export.
examples/rtthread/z_test.c RT-Thread basic functionality test + MSH export.
copy_files.bat Local Windows copy helper for moving RT-Thread files into another checkout.
check_rtthread_files.py Local validation script to check presence of RT-Thread-related files.
SUBMIT_PR_GUIDE.md Detailed PR submission guide (Chinese).
SIMPLE_PR_GUIDE.md Simplified PR submission guide (Chinese).
SConscript RT-Thread SCons build integration for the package.
RTTHREAD_ADAPTATION_SUMMARY.md Technical summary of the RT-Thread port (Chinese).
README_RTTHREAD.md RT-Thread usage documentation and examples.
PR_READY_SUMMARY.md Summary/checklist-style “ready” status doc.
PR_DESCRIPTION.md PR description template content.
PR_CHECKLIST.md PR checklist for the RT-Thread port.
MANUAL_PR_STEPS.md Manual PR submission steps guide (Chinese).
Kconfig RT-Thread menuconfig options for zenoh-pico package.
COMMIT_MESSAGES.md Commit message templates for the RT-Thread PR.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) {
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zp_start_read_task() / zp_start_lease_task() take a mutable loaned session, but this example passes z_loan(s) instead of z_loan_mut(s). Please use z_loan_mut(s) for task helpers.

Suggested change
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) {
if (zp_start_read_task(z_loan_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) {

Copilot uses AI. Check for mistakes.
echo 🚀 RT-Thread 适配文件复制脚本
echo ================================

set SOURCE_DIR=E:\Programing\Embedded_Drivers_And_Tools\modules\zenoh-pico
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOURCE_DIR is hardcoded to a specific local path (E:\Programing\...), making this script non-portable and not useful to other contributors. Consider removing it from the upstream PR or parameterizing the source directory.

Suggested change
set SOURCE_DIR=E:\Programing\Embedded_Drivers_And_Tools\modules\zenoh-pico
rem 如果未预先设置 SOURCE_DIR,则默认使用当前脚本所在目录
if not defined SOURCE_DIR (
set "SOURCE_DIR=%~dp0"
)

Copilot uses AI. Check for mistakes.
z_get_options_default(&opts);
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_str(&payload, value);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_bytes_from_str() does not have a 2-argument form. For a constant string, use z_bytes_copy_from_str(), or if you want to transfer ownership provide deleter/context per the API. As written, this won’t compile.

Suggested change
z_bytes_from_str(&payload, value);
z_bytes_copy_from_str(&payload, value);

Copilot uses AI. Check for mistakes.
z_bytes_from_str(&payload, value);
z_put(z_loan(s), z_loan(ke), z_move(payload), NULL);

z_close(z_move(s), NULL);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_close() expects a loaned session (z_loan_mut(s)) and the owned session should then be dropped. Calling z_close(z_move(s), ...) uses the wrong argument kind and doesn’t drop the session.

Suggested change
z_close(z_move(s), NULL);
z_close(z_loan_mut(s), NULL);
z_drop(z_move(s));

Copilot uses AI. Check for mistakes.
Comment on lines +67 to +71
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) {
printf("Unable to start read and lease tasks\n");
z_close(z_move(s), NULL);
return -1;
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_close() expects a loaned session (z_loan_mut(s)) and then the owned session should be dropped. Calling z_close(z_move(s), ...) uses the wrong argument kind and doesn’t drop s. Please change to z_close(z_loan_mut(s), ...); z_drop(z_move(s));.

Copilot uses AI. Check for mistakes.
z_owned_publisher_t pub;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str_unchecked(&ke, "demo/example");
z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publisher snippet calls z_declare_publisher() with the wrong argument order. The API is z_declare_publisher(session, &pub, keyexpr, options) (see include/zenoh-pico/api/primitives.h), but the snippet passes (&pub, session, ...), which won’t compile.

Suggested change
z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL);
z_declare_publisher(z_loan(s), &pub, z_loan(ke), NULL);

Copilot uses AI. Check for mistakes.
z_owned_subscriber_t sub;
z_view_keyexpr_t ke;
z_view_keyexpr_from_str_unchecked(&ke, "demo/example");
z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subscriber example calls z_declare_subscriber() with the wrong argument order (it should be z_declare_subscriber(session, &sub, keyexpr, &callback, options)). As written, this snippet won’t compile against the current API.

Suggested change
z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL);
z_declare_subscriber(z_loan(s), &sub, z_loan(ke), z_move(callback), NULL);

Copilot uses AI. Check for mistakes.
printf("Elapsed time: %lu ms\n", elapsed_ms);

z_time_t time_now = z_time_now();
printf("Time now: %lu\n", time_now.time);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_time_t is struct timeval on this platform, so it doesn’t have a .time field. This won’t compile; use tv_sec / tv_usec or format via z_time_now_as_str().

Suggested change
printf("Time now: %lu\n", time_now.time);
printf("Time now: %ld.%06ld\n", (long)time_now.tv_sec, (long)time_now.tv_usec);

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +46
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, locator);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zp_config_insert() expects a mutable loaned config (z_loaned_config_t*), but this example passes z_loan(config) instead of z_loan_mut(config). Please use the mutable loan macro when inserting/modifying config values.

Suggested change
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, locator);
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator);

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +36
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, locator);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example mutates the config via zp_config_insert() but uses z_loan(config) instead of z_loan_mut(config). Please use z_loan_mut(config) when inserting config values.

Suggested change
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, locator);
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode);
if (locator != NULL) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature Something new is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants