Skip to content

Commit 12a7233

Browse files
committed
examples: update tcp_client-rs and udp_socket-rs to support no_std
Signed-off-by: ivila <390810839@qq.com> Reviewed-by: Yuan Zhuang <yuanz@apache.org>
1 parent 10bbcdc commit 12a7233

11 files changed

Lines changed: 70 additions & 51 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ branch (`main`), please refer to the
8181
- **Common**: See
8282
[Overview of OP-TEE Rust Examples](https://teaclave.apache.org/trustzone-sdk-docs/overview-of-optee-rust-examples/).
8383

84-
- **`no-std`**: Excludes `test_serde`, `test_tcp_client`, `test_udp_socket`,
85-
`test_message_passing_interface`, `test_tls_client`, `test_tls_server`.
84+
- **`no-std`**: Excludes `test_serde`, `test_message_passing_interface`,
85+
`test_tls_client`, `test_tls_server`.
8686

8787

8888
## Quick Start with the OP-TEE Repo for QEMUv8

examples/tcp_client-rs/host/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
2424

2525
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
2626

27-
ifeq ($(STD),)
28-
all:
29-
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
30-
else
3127
all: host strip
32-
endif
3328

3429
host:
3530
@cargo build --target $(TARGET_HOST) --release --config $(LINKER_CFG)

examples/tcp_client-rs/proto/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17+
#![no_std]
1718

1819
pub enum Command {
1920
Start,

examples/tcp_client-rs/ta/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ edition = "2018"
2828
proto = { path = "../proto" }
2929
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
3030
optee-utee = { path = "../../../optee-utee" }
31+
cfg_block = "0.2.0"
3132

3233
[build-dependencies]
3334
proto = { path = "../proto" }
3435
optee-utee-build = { path = "../../../optee-utee-build" }
3536

3637
[profile.release]
3738
panic = "abort"
38-
lto = false
39+
lto = true
3940
opt-level = 1

examples/tcp_client-rs/ta/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
2929
SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
3030
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
3131

32-
ifeq ($(STD),)
33-
all:
34-
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
35-
else
32+
BUILDER = $(if $(STD),xargo,cargo)
33+
3634
all: ta strip sign
37-
endif
3835

3936
ta:
40-
@xargo build --target $(TARGET) --release --config $(LINKER_CFG)
37+
@$(BUILDER) build --target $(TARGET) --release --config $(LINKER_CFG)
4138

4239
strip: ta
4340
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta

examples/tcp_client-rs/ta/src/main.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#![cfg_attr(not(target_os = "optee"), no_std)]
1819
#![no_main]
1920

21+
cfg_block::cfg_block! {
22+
// In Teaclave, if target_os = "optee", the codes is compiled with std.
23+
// Otherwise, no-std
24+
if #[cfg(target_os = "optee")] {
25+
use std::io::{Read, Write};
26+
} else {
27+
extern crate alloc;
28+
use optee_utee::net::{StdCompactConnect, StdCompactWrite, StdCompactRead};
29+
use alloc::vec::Vec;
30+
use alloc::string::String;
31+
}
32+
}
33+
2034
use optee_utee::net::TcpStream;
2135
use optee_utee::{
2236
ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
2337
};
2438
use optee_utee::{Error, ErrorKind, Parameters, Result};
2539
use proto::Command;
26-
use std::io::Read;
27-
use std::io::Write;
2840

2941
#[ta_create]
3042
fn create() -> Result<()> {
@@ -52,32 +64,36 @@ fn destroy() {
5264
fn invoke_command(cmd_id: u32, _params: &mut Parameters) -> Result<()> {
5365
trace_println!("[+] TA invoke command");
5466
match Command::from(cmd_id) {
55-
Command::Start => {
56-
tcp_client();
57-
Ok(())
58-
}
67+
Command::Start => tcp_client(),
5968
_ => Err(Error::new(ErrorKind::BadParameters)),
6069
}
6170
}
6271

63-
fn tcp_client() {
64-
let mut stream = TcpStream::connect("teaclave.apache.org", 80).unwrap();
72+
fn tcp_client() -> Result<()> {
73+
let mut stream = TcpStream::connect("teaclave.apache.org", 80).map_err(|err| {
74+
trace_println!("failed to connect due to {:?}", err);
75+
ErrorKind::Generic
76+
})?;
6577
stream
6678
.write_all(b"GET / HTTP/1.0\r\nHost: teaclave.apache.org\r\n\r\n")
67-
.unwrap();
79+
.map_err(|err| {
80+
trace_println!("failed to write_all due to {:?}", err);
81+
ErrorKind::Generic
82+
})?;
6883
let mut response = Vec::new();
6984
let mut chunk = [0u8; 1024];
7085
loop {
7186
match stream.read(&mut chunk) {
7287
Ok(0) => break,
7388
Ok(n) => response.extend_from_slice(&chunk[..n]),
74-
Err(_) => {
75-
trace_println!("Error");
76-
panic!();
89+
Err(err) => {
90+
trace_println!("failed to read due to {:?}", err);
91+
return Err(ErrorKind::Generic.into());
7792
}
7893
}
7994
}
8095
trace_println!("{}", String::from_utf8_lossy(&response));
96+
Ok(())
8197
}
8298

8399
include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));

examples/udp_socket-rs/host/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
2424

2525
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
2626

27-
ifeq ($(STD),)
28-
all:
29-
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
30-
else
3127
all: host strip
32-
endif
3328

3429
host:
3530
@cargo build --target $(TARGET_HOST) --release --config $(LINKER_CFG)

examples/udp_socket-rs/proto/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17+
#![no_std]
1718

1819
pub enum Command {
1920
Start,

examples/udp_socket-rs/ta/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ description = "An example of Rust OP-TEE TrustZone SDK."
2525
edition = "2018"
2626

2727
[dependencies]
28-
libc = { path = "../../../rust/libc" }
2928
proto = { path = "../proto" }
3029
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
3130
optee-utee = { path = "../../../optee-utee" }
31+
cfg_block = "0.2.0"
3232

3333
[build-dependencies]
3434
proto = { path = "../proto" }
3535
optee-utee-build = { path = "../../../optee-utee-build" }
3636

3737
[profile.release]
3838
panic = "abort"
39-
lto = false
39+
lto = true
4040
opt-level = 1

examples/udp_socket-rs/ta/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
2929
SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
3030
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
3131

32-
ifeq ($(STD),)
33-
all:
34-
@echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version"
35-
else
32+
BUILDER = $(if $(STD),xargo,cargo)
33+
3634
all: ta strip sign
37-
endif
3835

3936
ta:
40-
@xargo build --target $(TARGET) --release --config $(LINKER_CFG)
37+
@$(BUILDER) build --target $(TARGET) --release --config $(LINKER_CFG)
4138

4239
strip: ta
4340
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta

0 commit comments

Comments
 (0)