|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +#![cfg_attr(not(target_os = "optee"), no_std)] |
18 | 19 | #![no_main] |
19 | 20 |
|
| 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 | + |
20 | 34 | use optee_utee::net::TcpStream; |
21 | 35 | use optee_utee::{ |
22 | 36 | ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println, |
23 | 37 | }; |
24 | 38 | use optee_utee::{Error, ErrorKind, Parameters, Result}; |
25 | 39 | use proto::Command; |
26 | | -use std::io::Read; |
27 | | -use std::io::Write; |
28 | 40 |
|
29 | 41 | #[ta_create] |
30 | 42 | fn create() -> Result<()> { |
@@ -52,32 +64,36 @@ fn destroy() { |
52 | 64 | fn invoke_command(cmd_id: u32, _params: &mut Parameters) -> Result<()> { |
53 | 65 | trace_println!("[+] TA invoke command"); |
54 | 66 | match Command::from(cmd_id) { |
55 | | - Command::Start => { |
56 | | - tcp_client(); |
57 | | - Ok(()) |
58 | | - } |
| 67 | + Command::Start => tcp_client(), |
59 | 68 | _ => Err(Error::new(ErrorKind::BadParameters)), |
60 | 69 | } |
61 | 70 | } |
62 | 71 |
|
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 | + })?; |
65 | 77 | stream |
66 | 78 | .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 | + })?; |
68 | 83 | let mut response = Vec::new(); |
69 | 84 | let mut chunk = [0u8; 1024]; |
70 | 85 | loop { |
71 | 86 | match stream.read(&mut chunk) { |
72 | 87 | Ok(0) => break, |
73 | 88 | 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()); |
77 | 92 | } |
78 | 93 | } |
79 | 94 | } |
80 | 95 | trace_println!("{}", String::from_utf8_lossy(&response)); |
| 96 | + Ok(()) |
81 | 97 | } |
82 | 98 |
|
83 | 99 | include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs")); |
0 commit comments