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 13, 2024
1 parent 6fd14cd commit 7712660
Show file tree
Hide file tree
Showing 5 changed files with 265 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.

20 changes: 20 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,20 @@
[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"]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
log = "0.4"
px4_nuttx = { path = "../../../lib/rust_px4_nuttx" }
px4_nuttx_macros = { path = "../../../lib/rust_px4_nuttx/px4_nuttx_macros" }
109 changes: 109 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,109 @@
#![no_std]
#![no_main]

extern crate px4_nuttx;
extern crate px4_nuttx_macros;

use px4_nuttx::logger::{log_raw, LogLevel};
use log::{info, warn};
use px4_nuttx::px4_module_main;
use px4_nuttx::nuttx::net::UdpSocket;
use px4_nuttx::nuttx::time::{hrt_time, Duration};
use px4_nuttx::alloc::vec::Vec;
use px4_nuttx::alloc::string::String;


#[px4_module_main]
pub fn run(_args: &[&str]) -> Result<(), ()> {
info!("Hello from Rust module test!");

let mut count: u32 = 0;
for arg in _args {
info!(" arg[{}]: {}", count, arg);
count += 1;
}

if _args.len() > 1 {
let mode: u32 = _args[1].parse::<u32>().unwrap_or(0);
match mode {
1 => vector_test(),
2 => timer_test(),
3 => udp_send_test(),
4 => udp_recv_test(),
5 => generate_panic(),
6 => test_raw(),
_ => warn!("Invalid test. Use 1-6"),
}

} else {
vector_test();
timer_test();
udp_send_test();
udp_recv_test()
}

Ok(())
}

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

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

fn udp_send_test() {
// Test udp socket send
info!("TEST: UDP socket send");
let socket = UdpSocket::bind("192.168.200.101:12222").unwrap();
info!("Created a socket which is bound to address 192.168.200.101:12222");
socket.send_to(b"Hello from Rust!\n", "192.168.200.100:12223").unwrap();
info!("Message sent to 192.168.200.100:12223");
}

fn udp_recv_test() {
let mut buf: [u8; 128] = [0; 128];
let timeout = 5;

// Test udp socket receive
info!("TEST: UDP socket receive");
let mut socket = UdpSocket::bind("192.168.200.101:12222").unwrap();
info!("Set read timeout {} secs", timeout);
socket.set_read_timeout(Some(Duration::from_secs(timeout))).unwrap();
info!("Created a socket which is bound to address 192.168.200.101:12222");
if let Ok(len) = socket.recv_from(&mut buf) {
info!("Receive {} bytes", len);
if let Ok(data) = core::str::from_utf8(&buf[..len]) {
info!("MSG: {}", data);
} else {
warn!("Can't read message data");
}
} else {
warn!("Receiving failed!")
}

}

fn test_raw() {
let message = String::from("hello from log_raw");
info!("Test raw print here");
log_raw(
LogLevel::Panic,
&message,
);
}

fn generate_panic() {
info!("This should cause panic");
let a: Option<u8> = None;
let _b = a.unwrap();
}

0 comments on commit 7712660

Please sign in to comment.