1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #![no_main]
19
20 use optee_utee::{
21 ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
22 };
23 use optee_utee::{Error, ErrorKind, Parameters, Result};
24 use optee_utee::net::TcpStream;
25 use proto::Command;
26 use std::io::Read;
27 use std::io::Write;
28
29 #[ta_create]
create() -> Result<()>30 fn create() -> Result<()> {
31 trace_println!("[+] TA create");
32 Ok(())
33 }
34
35 #[ta_open_session]
open_session(_params: &mut Parameters) -> Result<()>36 fn open_session(_params: &mut Parameters) -> Result<()> {
37 trace_println!("[+] TA open session");
38 Ok(())
39 }
40
41 #[ta_close_session]
close_session()42 fn close_session() {
43 trace_println!("[+] TA close session");
44 }
45
46 #[ta_destroy]
destroy()47 fn destroy() {
48 trace_println!("[+] TA destroy");
49 }
50
51 #[ta_invoke_command]
invoke_command(cmd_id: u32, _params: &mut Parameters) -> Result<()>52 fn invoke_command(cmd_id: u32, _params: &mut Parameters) -> Result<()> {
53 trace_println!("[+] TA invoke command");
54 match Command::from(cmd_id) {
55 Command::Start => {
56 tcp_client();
57 Ok(())
58 }
59 _ => Err(Error::new(ErrorKind::BadParameters)),
60 }
61 }
62
tcp_client()63 fn tcp_client() {
64 let mut stream = TcpStream::connect("teaclave.apache.org", 80).unwrap();
65 stream.write_all(b"GET / HTTP/1.0\r\nHost: teaclave.apache.org\r\n\r\n").unwrap();
66 let mut response = Vec::new();
67 let mut chunk = [0u8; 1024];
68 loop {
69 match stream.read(&mut chunk) {
70 Ok(0) => break,
71 Ok(n) => response.extend_from_slice(&chunk[..n]),
72 Err(_) => {
73 trace_println!("Error");
74 panic!();
75 }
76 }
77 }
78 trace_println!("{}", String::from_utf8_lossy(&response));
79 }
80
81 // TA configurations
82 const TA_FLAGS: u32 = 0;
83 const TA_DATA_SIZE: u32 = 1 * 1024 * 1024;
84 const TA_STACK_SIZE: u32 = 2 * 1024 * 1024;
85 const TA_VERSION: &[u8] = b"0.1\0";
86 const TA_DESCRIPTION: &[u8] = b"This is a hello world example.\0";
87 const EXT_PROP_VALUE_1: &[u8] = b"Hello World TA\0";
88 const EXT_PROP_VALUE_2: u32 = 0x0010;
89 const TRACE_LEVEL: i32 = 4;
90 const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
91 const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
92
93 include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
94