Skip to content

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
return ret;
}

(void)memcpy(&ep->_sockaddr, result->ai_addr, result->ai_addrlen);

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data. Error

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
}

if (ret == _Z_RES_OK) {
(void)memcpy(&sock->_addr, &rep._sockaddr, sizeof(struct sockaddr_in));

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data. Error

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
case AF_INET6: {
struct ipv6_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
(void)memcpy(&mreq.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&rep._sockaddr)->sin6_addr,

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data. Error

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
}

if (ret == _Z_RES_OK) {
(void)memcpy(&sock->_addr, &rep._sockaddr, sizeof(struct sockaddr_in));

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data. Error

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.

if ((rb != SIZE_MAX) && (addr != NULL)) {
*addr = _z_bytes_make(raddrlen);
(void)memcpy((uint8_t *)addr->start, &raddr, raddrlen);

Check failure

Code scanning / Flawfinder (reported by Codacy)

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data. Error

Does not check for buffer overflows when copying to destination (CWE-120). Make sure destination can always hold the source data.
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.

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.

3 participants