Skip to content

Commit

Permalink
Add test module: rust_module_test
Browse files Browse the repository at this point in the history
  • Loading branch information
jnippula committed Aug 8, 2024
1 parent 237b6ab commit 09b4804
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/modules/rust_module_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ###########################################################################
#
# Copyright (c) 2024 Technology Innovation Institute. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# ############################################################################

px4_add_rust_module(
MODULE modules__rust_module_test
MAIN rust_module_test
RUST_LIB lib__rust_module_test
RUST_MOD
rust_mod
)
12 changes: 12 additions & 0 deletions src/modules/rust_module_test/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
menuconfig MODULES_RUST_MODULE_TEST
bool "rust_module_test"
default n
---help---
Enable rust module test

menuconfig USER_RUST_MODULE_TEST
bool "rust module test in user"
default y
depends on BOARD_PROTECTED && MODULES_RUST_MODULE_TEST
---help---
Put rust module test in userspace memory
84 changes: 84 additions & 0 deletions src/modules/rust_module_test/rust_mod/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/modules/rust_module_test/rust_mod/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "rust_mod"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["staticlib"]

[dependencies]
log = "0.4"
px4_nuttx = { path = "../../../lib/rust_px4_nuttx" }
px4_nuttx_macros = { path = "../../../lib/rust_px4_nuttx/px4_nuttx_macros" }
40 changes: 40 additions & 0 deletions src/modules/rust_module_test/rust_mod/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![no_std]
#![no_main]

extern crate px4_nuttx;
extern crate px4_nuttx_macros;

use log::info;
use px4_nuttx::px4_module_main;
use px4_nuttx::nuttx::net::UdpSocket;
use px4_nuttx::nuttx::time::hrt_time;
use px4_nuttx::alloc::vec::Vec;

#[px4_module_main]
pub fn run(_args: &[&str]) -> Result<(), ()> {
info!("Hello from Rust module test!");
let mut count = 0;
for arg in _args {
info!(" arg[{}]: {}", count, arg);
count += 1;
}

// Test vector
let mut v1: Vec<u8> = Vec::new();
for i in 0..10 {
v1.push(i+3);
}
info!("This is new vector: {:?}", v1);

// Test hrt_timer
info!("System timer: {}", hrt_time());

// Test udp socket
let socket = UdpSocket::bind("192.168.200.101:12222").unwrap();
info!("Created socket binded to 0.0.0.0:12222");
socket.send_to(b"Hello from Rust!\n", "192.168.200.100:12223").unwrap();
info!("Sent message to 192.168.200.100:12223");

Ok(())
}

0 comments on commit 09b4804

Please sign in to comment.