feat: openwrt QUECTEL EC200R

This commit is contained in:
anjingyu 2025-05-18 11:06:48 +08:00
parent a647de5c11
commit 5b9e3898e1
6 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,12 @@
[build]
target = "armv7-unknown-linux-musleabihf"
[target.armv7-unknown-linux-musleabihf]
linker = "/home/anjingyu/Documents/projs/work/sixents/tbox-foton/toolchains/z4/bin/arm-openwrt-linux-muslgnueabi-gcc"
rustflags = [
"-C", "link-arg=--sysroot=/home/anjingyu/Documents/projs/work/sixents/tbox-foton/toolchains/z4"]
[alias]
b = "build --target x86_64-unknown-linux-gnu"
# ba = "--config target.aarch64-unknown-linux-gnu.linker=\"/home/anjingyu/Public/J3/gcc-linaro-6.5.0-2018.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc\" --config target.aarch64-unknown-linux-gnu.rustflags=\"-Clink-arg=--sysroot=/home/anjingyu/Public/J3/gcc-linaro-6.5.0-2018.12-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc\" build --target aarch64-unknown-linux-gnu"

16
openwrt-rs/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "openwrtrs"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.7.4"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }
[profile.release]
strip = true
opt-level = "z" # Optimize for size
lto = true
codegen-units = 1

25
openwrt-rs/README.md Normal file
View File

@ -0,0 +1,25 @@
# Rust Demo for OpenWRT(QUECTEL EC200R) Platform
Install the build toolchain into `$HOME/Documents/projs/work/sixents/tbox-foton/toolchains/z4`.
## Build & Test
``` shell
# Make sure you have install the aarch64-unknown-linux-gnu target
# rustup target add armv7-unknown-linux-musleabihf
# Build the Release version
# Then you can deploy the program(target/armv7-unknown-linux-musleabihf/release/openwrtrs) to your MPU board
./build.sh --target armv7-unknown-linux-musleabihf -r
# Test locally
./build.sh -r
cargo run -r
# Use the cargo alias command
# Build x86_64-unknown-linux-gnu
cargo b
# Build armv7-unknown-linux-musleabihf
cargo build
```

12
openwrt-rs/build.sh Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env bash
#
# Build the Rust version demo
#
# Author: anjingyu <anjingyu@navinfo.com>
# Update the PATH environment variable
export PATH="$HOME/Documents/projs/work/sixents/tbox-foton/toolchains/z4/bin:$PATH"
# cargo build --target aarch64-unknown-linux-gnu
cargo build "$@"

96
openwrt-rs/src/main.rs Normal file
View File

@ -0,0 +1,96 @@
use axum::{
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, process::Command};
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(root))
.route("/run", post(run_command));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("listening on {} ...", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
fn run_cmd<I, S>(cmd: S, args: I) -> String
where
S: AsRef<OsStr>,
I: IntoIterator<Item = S>,
{
let output = Command::new(cmd)
.args(args)
.output()
.expect("failed to run {cmd}");
let output = String::from_utf8(output.stdout).unwrap();
let output = output.strip_suffix("\n").unwrap();
String::from(output)
}
// basic handler that responds with a static string
async fn root() -> impl IntoResponse {
// let arch = Command::new("uname")
// .arg("-m")
// .output()
// .expect("failed to run: uname -m");
let arch = run_cmd("uname", ["-m"]);
let kernel_name = run_cmd("uname", ["-s"]);
let os_name = run_cmd("uname", ["-o"]);
let hostname = run_cmd("uname", ["-n"]);
let kernel_release = run_cmd("uname", ["-r"]);
let kernel_version = run_cmd("uname", ["-v"]);
let r = MachineInfo {
arch,
kernel_name,
os_name,
hostname,
kernel_release,
kernel_version,
};
(StatusCode::OK, Json(r))
}
async fn run_command(Json(payload): Json<UserCommand>) -> impl IntoResponse {
let output = Command::new(payload.cmd)
.args(payload.args)
.output()
.expect("failed to run: {payload.cmd}");
let result = CommandResult {
code: output.status.code().unwrap(),
result: String::from_utf8(output.stdout).unwrap(),
};
(StatusCode::OK, Json(result))
}
// the input to our `create_user` handler
#[derive(Deserialize)]
struct UserCommand {
cmd: String,
args: Vec<String>,
}
#[derive(Serialize)]
struct CommandResult {
code: i32,
result: String,
}
#[derive(Serialize)]
struct MachineInfo {
arch: String,
kernel_name: String,
os_name: String,
hostname: String,
kernel_release: String,
kernel_version: String,
}

17
openwrt-rs/test.sh Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
#
# Test the demo via curl
#
# Author: anjingyu <anjingyu@navinfo.com>
readonly IP="172.22.52.20"
readonly IPLOOP="127.0.0.1"
readonly PORT="3000"
curl -s "http://${IP}:${PORT}/" | jq
sleep 1
echo "Run command: ls -l -a"
curl -s -X POST "http://${IP}:${PORT}/run" --header "Content-Type: application/json" --data '{"cmd": "ls", "args": ["-l", "-a"]}' | jq -c '.result' | xargs printf
# curl -s -X POST "http://${IP}:${PORT}/run" --header "Content-Type: application/json" --data '{"cmd": "ls", "args": ["-l", "-a"]}' | jq