summaryrefslogtreecommitdiff
path: root/components/rust/examples/component
diff options
context:
space:
mode:
Diffstat (limited to 'components/rust/examples/component')
-rw-r--r--components/rust/examples/component/SConscript71
-rw-r--r--components/rust/examples/component/component_registry/Cargo.toml19
-rw-r--r--components/rust/examples/component/component_registry/src/lib.rs43
-rw-r--r--components/rust/examples/component/log/Cargo.toml15
-rw-r--r--components/rust/examples/component/log/src/lib.rs30
-rw-r--r--components/rust/examples/component/log/src/logging.rs96
6 files changed, 274 insertions, 0 deletions
diff --git a/components/rust/examples/component/SConscript b/components/rust/examples/component/SConscript
new file mode 100644
index 0000000000..40d11c0446
--- /dev/null
+++ b/components/rust/examples/component/SConscript
@@ -0,0 +1,71 @@
+import os
+import sys
+from building import *
+
+cwd = GetCurrentDir()
+
+# Import component build module and build support
+sys.path.append(os.path.join(cwd, '../../tools'))
+from build_component import build_example_component
+from build_support import clean_rust_build
+
+# Load feature configurations
+try:
+ from feature_config_component import setup_all_component_features
+ setup_all_component_features()
+except ImportError:
+ print("Warning: Could not load component feature configurations")
+
+
+def _has(sym: str) -> bool:
+ """Helper function to check if a configuration symbol is enabled."""
+ try:
+ return bool(GetDepend([sym]))
+ except Exception:
+ return bool(GetDepend(sym))
+
+
+# Early return if Rust or log component is not enabled
+if not _has('RT_USING_RUST'):
+ group = []
+ Return('group')
+
+if not _has('RUST_LOG_COMPONENT'):
+ group = []
+ Return('group')
+
+# Source files – component glue code if needed
+src = []
+LIBS = []
+LIBPATH = []
+LINKFLAGS = ""
+
+# Handle clean operation
+if GetOption('clean'):
+ comp_build_dir = clean_rust_build(Dir('#').abspath, "example_component")
+ if os.path.exists(comp_build_dir):
+ print(f'Registering {comp_build_dir} for cleanup')
+ Clean('.', comp_build_dir)
+ else:
+ print('No example_component build artifacts to clean')
+else:
+ # Build the component using the extracted build module
+ import rtconfig
+ LIBS, LIBPATH, LINKFLAGS = build_example_component(
+ cwd=cwd,
+ has_func=_has,
+ rtconfig=rtconfig,
+ build_root=os.path.join(Dir('#').abspath, "build", "example_component")
+ )
+
+# Define component group for SCons
+group = DefineGroup(
+ 'example_component_log',
+ src,
+ depend=['RT_USING_RUST'],
+ LIBS=LIBS,
+ LIBPATH=LIBPATH,
+ LINKFLAGS=LINKFLAGS
+)
+
+Return('group') \ No newline at end of file
diff --git a/components/rust/examples/component/component_registry/Cargo.toml b/components/rust/examples/component/component_registry/Cargo.toml
new file mode 100644
index 0000000000..64ec88c2d4
--- /dev/null
+++ b/components/rust/examples/component/component_registry/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "em_component_registry"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+name = "em_component_registry"
+crate-type = ["staticlib"]
+
+[dependencies]
+rt-rust = { path = "../../../core" }
+rt_macros = { path = "../../../rt_macros" }
+
+# Optional dependencies - only included when features are enabled
+em_component_log = { path = "../log", optional = true }
+
+[features]
+default = []
+enable-log = ["em_component_log", "em_component_log/enable-log"] \ No newline at end of file
diff --git a/components/rust/examples/component/component_registry/src/lib.rs b/components/rust/examples/component/component_registry/src/lib.rs
new file mode 100644
index 0000000000..beff2df698
--- /dev/null
+++ b/components/rust/examples/component/component_registry/src/lib.rs
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author notes
+ * 2025-10-25 foxglove Component registry for unified component registration
+ * 2025-10-29 foxglove Updated to demonstrate new modular macro interface
+ */
+#![no_std]
+
+extern crate alloc;
+
+/* Demonstrate the new modular macro interface */
+use rt_macros::rt_component_export;
+use rt_rust::param::{Param, ParamItem};
+use rt_rust::println;
+
+/* Re-export component functionality for other modules */
+#[cfg(feature = "enable-log")]
+pub use em_component_log::*;
+#[cfg(feature = "enable-log")]
+use em_component_log::logging::Level;
+
+/** Unified component registration entrypoint */
+#[cfg(feature = "enable-log")]
+#[rt_component_export(name = "rust_component_registry")]
+fn init_registry_component() {
+ println!("[logging component init] hello world");
+ log!(Level::Info, "hello world");
+ info!("hello world");
+ warn!("hello world");
+ error!("hello world");
+ trace!("hello world");
+ debug!("hello world");
+}
+
+/** Provide a no-op implementation when no component feature is enabled */
+#[cfg(not(feature = "enable-log"))]
+pub extern "C" fn component_init() {
+ /* Empty implementation to ensure the library still links */
+}
diff --git a/components/rust/examples/component/log/Cargo.toml b/components/rust/examples/component/log/Cargo.toml
new file mode 100644
index 0000000000..901772b771
--- /dev/null
+++ b/components/rust/examples/component/log/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "em_component_log"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+name = "em_component_log"
+crate-type = ["rlib", "staticlib"]
+
+[features]
+default = []
+enable-log = []
+
+[dependencies]
+rt-rust = { path = "../../../core" }
diff --git a/components/rust/examples/component/log/src/lib.rs b/components/rust/examples/component/log/src/lib.rs
new file mode 100644
index 0000000000..ff7e75c32f
--- /dev/null
+++ b/components/rust/examples/component/log/src/lib.rs
@@ -0,0 +1,30 @@
+
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author notes
+ * 2025-10-23 foxglove log component demo
+ */
+#![no_std]
+
+extern crate alloc;
+
+/* When the `enable-log` feature is enabled, expose logging module and helpers */
+#[cfg(feature = "enable-log")]
+pub mod logging;
+#[cfg(feature = "enable-log")]
+use rt_rust::println;
+#[cfg(feature = "enable-log")]
+use crate::logging::Level;
+
+/* Import panic handler parameters from rt_rust library */
+use rt_rust::param::{Param, ParamItem};
+
+/* Provide a no-op implementation when the `enable-log` feature is disabled */
+#[cfg(not(feature = "enable-log"))]
+pub extern "C" fn component_init() {
+ /* Empty implementation to ensure the library still links */
+}
diff --git a/components/rust/examples/component/log/src/logging.rs b/components/rust/examples/component/log/src/logging.rs
new file mode 100644
index 0000000000..ccbf8ee1e2
--- /dev/null
+++ b/components/rust/examples/component/log/src/logging.rs
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2006-2024, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author notes
+ * 2025-10-20 foxglove micro rust log component
+ */
+/* Basic logging primitives and console output helpers */
+use alloc::string::String;
+use rt_rust::println;
+
+#[repr(usize)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub enum Level {
+ Error = 1,
+ Warn = 2,
+ Info = 3,
+ Debug = 4,
+ Trace = 5,
+}
+
+/* Top-level logging macro */
+#[macro_export]
+macro_rules! log {
+ ($level:expr, $($arg:tt)*) => ({
+ $crate::logging::_log($level, format_args!($($arg)*));
+ });
+}
+
+/* Error-level logging */
+#[macro_export]
+macro_rules! error {
+ ($($arg:tt)*) => ({
+ $crate::logging::_log($crate::logging::Level::Error, format_args!($($arg)*));
+ });
+}
+
+/* Warning-level logging */
+#[macro_export]
+macro_rules! warn {
+ ($($arg:tt)*) => ({
+ $crate::logging::_log($crate::logging::Level::Warn, format_args!($($arg)*));
+ });
+}
+
+/* Info-level logging */
+#[macro_export]
+macro_rules! info {
+ ($($arg:tt)*) => ({
+ $crate::logging::_log($crate::logging::Level::Info, format_args!($($arg)*));
+ });
+}
+
+/* Debug-level logging */
+#[macro_export]
+macro_rules! debug {
+ ($($arg:tt)*) => ({
+ $crate::logging::_log($crate::logging::Level::Debug, format_args!($($arg)*));
+ });
+}
+
+/* Trace-level logging */
+#[macro_export]
+macro_rules! trace {
+ ($($arg:tt)*) => ({
+ $crate::logging::_log($crate::logging::Level::Trace, format_args!($($arg)*));
+ });
+}
+
+/* Internal logging handler: formats message and prints with color */
+pub fn _log(level: Level, args: core::fmt::Arguments) {
+ use core::fmt::Write;
+ use rt_rust::time;
+ let mut s = String::new();
+ write!(&mut s, "[{:?}][{:?}]", level, time::get_time()).unwrap();
+ write!(&mut s, "{}", args).unwrap();
+ match level {
+ Level::Error => {
+ println!("\x1b[1;31m{}\x1b[0m", s);
+ }
+ Level::Warn => {
+ println!("\x1b[1;33m{}\x1b[0m", s);
+ }
+ Level::Info => {
+ println!("\x1b[1;32m{}\x1b[0m", s);
+ }
+ Level::Debug => {
+ println!("\x1b[1;34m{}\x1b[0m", s);
+ }
+ Level::Trace => {
+ println!("\x1b[1;30m{}\x1b[0m", s);
+ }
+ }
+}